From 92d0ee46110b0555a7ec1dc6f5a5298315f05696 Mon Sep 17 00:00:00 2001 From: gio-yanruixue <82151125+gio-yanruixue@users.noreply.github.com> Date: Thu, 9 Dec 2021 18:10:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E7=BB=99Web=E7=AB=AF=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E5=92=8C=E6=B8=85=E9=99=A4UserKey=E7=9A=84?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=20(#132)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix:给Web端增加设置和清除UserKey的接口 * fix:调整代码格式 * fix:修复单元测试 * fix:测试用例增加import --- .../android/hybrid/NativeBridge.java | 18 +++++++++++++++ .../WebViewBridgeJavascriptInterface.java | 14 ++++++++++++ .../growingio/android/hybrid/HybridTest.java | 22 +++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/growingio-hybrid/src/main/java/com/growingio/android/hybrid/NativeBridge.java b/growingio-hybrid/src/main/java/com/growingio/android/hybrid/NativeBridge.java index c6f4ec33..585d1959 100644 --- a/growingio-hybrid/src/main/java/com/growingio/android/hybrid/NativeBridge.java +++ b/growingio-hybrid/src/main/java/com/growingio/android/hybrid/NativeBridge.java @@ -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); + } + }); + } } diff --git a/growingio-hybrid/src/main/java/com/growingio/android/hybrid/WebViewBridgeJavascriptInterface.java b/growingio-hybrid/src/main/java/com/growingio/android/hybrid/WebViewBridgeJavascriptInterface.java index 704f40f6..c28bb0ba 100644 --- a/growingio-hybrid/src/main/java/com/growingio/android/hybrid/WebViewBridgeJavascriptInterface.java +++ b/growingio-hybrid/src/main/java/com/growingio/android/hybrid/WebViewBridgeJavascriptInterface.java @@ -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(); + } + } diff --git a/growingio-hybrid/src/test/java/com/growingio/android/hybrid/HybridTest.java b/growingio-hybrid/src/test/java/com/growingio/android/hybrid/HybridTest.java index 97380335..f4cc4bc4 100644 --- a/growingio-hybrid/src/test/java/com/growingio/android/hybrid/HybridTest.java +++ b/growingio-hybrid/src/test/java/com/growingio/android/hybrid/HybridTest.java @@ -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; @@ -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; @@ -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) @@ -61,6 +65,7 @@ public class HybridTest { public void setup() { TrackerContext.init(application); TrackerContext.initSuccess(); + ConfigurationProvider.initWithConfig(new CoreConfiguration("test", "test"), new HashMap<>()); } @@ -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");