Skip to content

Commit

Permalink
fix:给Web端增加设置和清除UserKey的接口 (#132)
Browse files Browse the repository at this point in the history
* fix:给Web端增加设置和清除UserKey的接口

* fix:调整代码格式

* fix:修复单元测试

* fix:测试用例增加import
  • Loading branch information
gio-yanruixue authored Dec 9, 2021
1 parent 31849fe commit 92d0ee4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,22 @@ public void run() {
}
});
}

void setNativeUserIdAndUserKey(String userId, String userKey) {
TrackMainThread.trackMain().postActionToTrackMain(new Runnable() {
@Override
public void run() {
UserInfoProvider.get().setLoginUserId(userId, userKey);
}
});
}

void clearNativeUserIdAndUserKey() {
TrackMainThread.trackMain().postActionToTrackMain(new Runnable() {
@Override
public void run() {
UserInfoProvider.get().setLoginUserId(null, null);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,18 @@ public void clearNativeUserId() {
mNativeBridge.clearNativeUserId();
}

@JavascriptInterface
@com.uc.webview.export.JavascriptInterface
public void setNativeUserIdAndUserKey(String userId, String userKey) {
Logger.d(TAG, "setNativeUserIdAndUserKey: " + userId + ", " + userKey);
mNativeBridge.setNativeUserIdAndUserKey(userId, userKey);
}

@JavascriptInterface
@com.uc.webview.export.JavascriptInterface
public void clearNativeUserIdAndUserKey() {
Logger.d(TAG, "clearNativeUserIdAndUserKey: ");
mNativeBridge.clearNativeUserIdAndUserKey();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.google.common.truth.Truth;
import com.google.common.util.concurrent.Uninterruptibles;
import com.growingio.android.sdk.CoreConfiguration;
import com.growingio.android.sdk.TrackerContext;
import com.growingio.android.sdk.track.events.hybrid.HybridCustomEvent;
import com.growingio.android.sdk.track.events.hybrid.HybridPageAttributesEvent;
Expand All @@ -40,6 +41,8 @@
import com.growingio.android.sdk.track.modelloader.data.HybridBridge;
import com.growingio.android.sdk.track.modelloader.data.HybridDom;
import com.growingio.android.sdk.track.modelloader.data.HybridJson;
import com.growingio.android.sdk.track.providers.ConfigurationProvider;
import com.growingio.android.sdk.track.providers.UserInfoProvider;

import org.json.JSONObject;
import org.junit.Before;
Expand All @@ -49,6 +52,7 @@
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Config(manifest = Config.NONE, sdk = 23)
Expand All @@ -61,6 +65,7 @@ public class HybridTest {
public void setup() {
TrackerContext.init(application);
TrackerContext.initSuccess();
ConfigurationProvider.initWithConfig(new CoreConfiguration("test", "test"), new HashMap<>());
}


Expand Down Expand Up @@ -140,7 +145,24 @@ public void bridgeInterfaceTest() {
WebViewJavascriptBridgeConfiguration configuration = new WebViewJavascriptBridgeConfiguration("test", "test", "test", "test", 23);
WebViewBridgeJavascriptInterface webInterface = new WebViewBridgeJavascriptInterface(configuration);
webInterface.setNativeUserId("cpacm");
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
Truth.assertThat(UserInfoProvider.get().getLoginUserId()).isEqualTo("cpacm");
webInterface.clearNativeUserId();
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
Truth.assertThat(UserInfoProvider.get().getLoginUserId()).isEqualTo("");

ConfigurationProvider.core().setIdMappingEnabled(true);
webInterface.setNativeUserIdAndUserKey("cpacm", "email");
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
Truth.assertThat(UserInfoProvider.get().getLoginUserId()).isEqualTo("cpacm");
Truth.assertThat(UserInfoProvider.get().getLoginUserKey()).isEqualTo("email");

webInterface.clearNativeUserIdAndUserKey();
Uninterruptibles.sleepUninterruptibly(1, TimeUnit.SECONDS);
Truth.assertThat(UserInfoProvider.get().getLoginUserId()).isEqualTo("");
Truth.assertThat(UserInfoProvider.get().getLoginUserKey()).isEqualTo("");
ConfigurationProvider.core().setIdMappingEnabled(false);

String testJson = "{\"eventType\":\"LOGIN_USER_ATTRIBUTES\",\"attributes\":{\"grow_index\":\"苹果\",\"grow_click\":14}}";
webInterface.dispatchEvent(testJson);
Truth.assertThat(webInterface.getConfiguration()).contains("23");
Expand Down

0 comments on commit 92d0ee4

Please sign in to comment.