From c91ab5eed9beaf4f147060aa3249754f48ef4188 Mon Sep 17 00:00:00 2001 From: Hanson Ho Date: Tue, 17 Oct 2023 15:31:35 -0700 Subject: [PATCH 1/3] Add interface for an intra-SDK, cross-module, internal API --- .../api/embrace-android-sdk.api | 3 +++ .../io/embrace/android/embracesdk/Embrace.java | 7 +++++++ .../android/embracesdk/EmbraceImpl.java | 16 ++++++++++++++++ .../embracesdk/InternalInterfaceModule.kt | 9 +++++++++ .../android/embracesdk/internal/SdkApi.kt | 18 ++++++++++++++++++ .../android/embracesdk/internal/SdkApiImpl.kt | 7 +++++++ .../InternalInterfaceModuleImplTest.kt | 3 +++ 7 files changed, 63 insertions(+) create mode 100644 embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApi.kt create mode 100644 embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApiImpl.kt diff --git a/embrace-android-sdk/api/embrace-android-sdk.api b/embrace-android-sdk/api/embrace-android-sdk.api index d051b84fce..973e377292 100644 --- a/embrace-android-sdk/api/embrace-android-sdk.api +++ b/embrace-android-sdk/api/embrace-android-sdk.api @@ -40,6 +40,7 @@ public final class io/embrace/android/embracesdk/Embrace : io/embrace/android/em public fun getInternalInterface ()Lio/embrace/android/embracesdk/internal/EmbraceInternalInterface; public fun getLastRunEndState ()Lio/embrace/android/embracesdk/Embrace$LastRunEndState; public fun getReactNativeInternalInterface ()Lio/embrace/android/embracesdk/ReactNativeInternalInterface; + public fun getSdkApi ()Lio/embrace/android/embracesdk/internal/SdkApi; public fun getSessionProperties ()Ljava/util/Map; public fun getTraceIdHeader ()Ljava/lang/String; public fun getUnityInternalInterface ()Lio/embrace/android/embracesdk/UnityInternalInterface; @@ -177,6 +178,8 @@ public final class io/embrace/android/embracesdk/WebViewClientSwazzledHooks { public abstract interface annotation class io/embrace/android/embracesdk/annotation/StartupActivity : java/lang/annotation/Annotation { } +public abstract interface class io/embrace/android/embracesdk/internal/SdkApi { + public abstract fun getSdkCurrentTime ()J public abstract interface class io/embrace/android/embracesdk/internal/EmbraceInternalInterface { public abstract fun isNetworkSpanForwardingEnabled ()Z public abstract fun logComposeTap (Landroid/util/Pair;Ljava/lang/String;)V diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/Embrace.java b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/Embrace.java index 5f30fab7e0..f029e13b3e 100644 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/Embrace.java +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/Embrace.java @@ -14,6 +14,7 @@ import io.embrace.android.embracesdk.config.ConfigService; import io.embrace.android.embracesdk.internal.EmbraceInternalInterface; +import io.embrace.android.embracesdk.internal.SdkApi; import io.embrace.android.embracesdk.logging.InternalEmbraceLogger; import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger; import io.embrace.android.embracesdk.network.EmbraceNetworkRequest; @@ -737,6 +738,12 @@ public int getValue() { } } + @NonNull + @InternalApi + public SdkApi getSdkApi() { + return impl.getSdkApi(); + } + /** * Enum representing the end state of the last run of the application. */ diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.java b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.java index 5da8762084..45cec6e853 100644 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.java +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.java @@ -65,6 +65,8 @@ import io.embrace.android.embracesdk.internal.EmbraceInternalInterface; import io.embrace.android.embracesdk.internal.EmbraceInternalInterfaceKt; import io.embrace.android.embracesdk.internal.MessageType; +import io.embrace.android.embracesdk.internal.SdkApi; +import io.embrace.android.embracesdk.internal.SdkApiKt; import io.embrace.android.embracesdk.internal.TraceparentGenerator; import io.embrace.android.embracesdk.internal.crash.LastRunCrashVerifier; import io.embrace.android.embracesdk.internal.spans.EmbraceSpansService; @@ -223,6 +225,9 @@ final class EmbraceImpl { @Nullable private FlutterInternalInterface flutterInternalInterface; + @Nullable + private SdkApi sdkApi; + @Nullable private PushNotificationCaptureService pushNotificationService; @@ -612,6 +617,7 @@ private void startImpl(@NonNull Context context, // initialize internal interfaces InternalInterfaceModuleImpl internalInterfaceModule = new InternalInterfaceModuleImpl( + initModule, coreModule, androidServicesModule, essentialServiceModule, @@ -1741,4 +1747,14 @@ private void loadCrashVerifier(CrashModule crashModule, WorkerThreadModule worke workerThreadModule.backgroundExecutor(ExecutorName.BACKGROUND_REGISTRATION) ); } + + @InternalApi + @NonNull + public SdkApi getSdkApi() { + if (isStarted() && sdkApi != null) { + return sdkApi; + } else { + return SdkApiKt.getDefault(); + } + } } diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/InternalInterfaceModule.kt b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/InternalInterfaceModule.kt index 1b16a825eb..d5029863b6 100644 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/InternalInterfaceModule.kt +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/InternalInterfaceModule.kt @@ -4,17 +4,22 @@ import io.embrace.android.embracesdk.injection.AndroidServicesModule import io.embrace.android.embracesdk.injection.CoreModule import io.embrace.android.embracesdk.injection.CrashModule import io.embrace.android.embracesdk.injection.EssentialServiceModule +import io.embrace.android.embracesdk.injection.InitModule import io.embrace.android.embracesdk.injection.singleton import io.embrace.android.embracesdk.internal.EmbraceInternalInterface +import io.embrace.android.embracesdk.internal.SdkApi +import io.embrace.android.embracesdk.internal.SdkApiImpl internal interface InternalInterfaceModule { val embraceInternalInterface: EmbraceInternalInterface val reactNativeInternalInterface: ReactNativeInternalInterface val unityInternalInterface: UnityInternalInterface val flutterInternalInterface: FlutterInternalInterface + val sdkApi: SdkApi } internal class InternalInterfaceModuleImpl( + initModule: InitModule, coreModule: CoreModule, androidServicesModule: AndroidServicesModule, essentialServiceModule: EssentialServiceModule, @@ -55,4 +60,8 @@ internal class InternalInterfaceModuleImpl( coreModule.logger ) } + + override val sdkApi: SdkApi by singleton { + SdkApiImpl(initModule.clock) + } } diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApi.kt b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApi.kt new file mode 100644 index 0000000000..22d6d2b88d --- /dev/null +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApi.kt @@ -0,0 +1,18 @@ +package io.embrace.android.embracesdk.internal + +import io.embrace.android.embracesdk.InternalApi + +/** + * A set of methods exposed internally for other Embrace SDK modules to use - not supported publicly and can change at any time. + */ +@InternalApi +public interface SdkApi { + public fun getSdkCurrentTime(): Long +} + +/** + * Default implementation used when instance bound to the SDK cannot be used + */ +internal val default: SdkApi = object : SdkApi { + override fun getSdkCurrentTime(): Long = System.currentTimeMillis() +} diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApiImpl.kt b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApiImpl.kt new file mode 100644 index 0000000000..053584fae2 --- /dev/null +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApiImpl.kt @@ -0,0 +1,7 @@ +package io.embrace.android.embracesdk.internal + +import io.embrace.android.embracesdk.clock.Clock + +internal class SdkApiImpl(private val clock: Clock) : SdkApi { + override fun getSdkCurrentTime(): Long = clock.now() +} diff --git a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/InternalInterfaceModuleImplTest.kt b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/InternalInterfaceModuleImplTest.kt index d94f46a9fd..e547ace8db 100644 --- a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/InternalInterfaceModuleImplTest.kt +++ b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/InternalInterfaceModuleImplTest.kt @@ -4,6 +4,7 @@ import io.embrace.android.embracesdk.fakes.injection.FakeAndroidServicesModule import io.embrace.android.embracesdk.fakes.injection.FakeCoreModule import io.embrace.android.embracesdk.fakes.injection.FakeCrashModule import io.embrace.android.embracesdk.fakes.injection.FakeEssentialServiceModule +import io.embrace.android.embracesdk.fakes.injection.FakeInitModule import org.junit.Assert.assertNotNull import org.junit.Test @@ -12,6 +13,7 @@ internal class InternalInterfaceModuleImplTest { @Test fun testModule() { val module: InternalInterfaceModule = InternalInterfaceModuleImpl( + FakeInitModule(), FakeCoreModule(), FakeAndroidServicesModule(), FakeEssentialServiceModule(), @@ -23,5 +25,6 @@ internal class InternalInterfaceModuleImplTest { assertNotNull(module.unityInternalInterface) assertNotNull(module.reactNativeInternalInterface) assertNotNull(module.embraceInternalInterface) + assertNotNull(module.sdkApi) } } From 16a61f8314b0a2be1355f8f7d2b5b78363bf2d12 Mon Sep 17 00:00:00 2001 From: Hanson Ho Date: Tue, 17 Oct 2023 19:08:53 -0700 Subject: [PATCH 2/3] Add tests --- .../embracesdk/internal/SdkApiImplTest.kt | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/internal/SdkApiImplTest.kt diff --git a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/internal/SdkApiImplTest.kt b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/internal/SdkApiImplTest.kt new file mode 100644 index 0000000000..8b4ebde4f1 --- /dev/null +++ b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/internal/SdkApiImplTest.kt @@ -0,0 +1,37 @@ +package io.embrace.android.embracesdk.internal + +import io.embrace.android.embracesdk.fakes.FakeClock +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test + +internal class SdkApiImplTest { + + private lateinit var fakeClock: FakeClock + private lateinit var sdkApi: SdkApiImpl + + @Before + fun setup() { + fakeClock = FakeClock(currentTime = beforeObjectInitTime) + sdkApi = SdkApiImpl(fakeClock) + } + + @Test + fun `check usage of SDK time`() { + assertEquals(beforeObjectInitTime, sdkApi.getSdkCurrentTime()) + assertTrue(sdkApi.getSdkCurrentTime() < System.currentTimeMillis()) + fakeClock.tick(10L) + assertEquals(fakeClock.now(), sdkApi.getSdkCurrentTime()) + } + + @Test + fun `check default implementation`() { + assertTrue(beforeObjectInitTime < default.getSdkCurrentTime()) + assertTrue(default.getSdkCurrentTime() <= System.currentTimeMillis()) + } + + companion object { + val beforeObjectInitTime = System.currentTimeMillis() - 1 + } +} From 924a37b032d7f77c045d6969e81a3dbff90c98ea Mon Sep 17 00:00:00 2001 From: Hanson Ho Date: Tue, 24 Oct 2023 14:22:48 -0700 Subject: [PATCH 3/3] Remove SdkApi and merge functionality into EmbraceInternalInterface --- .../api/embrace-android-sdk.api | 4 +- .../testcases/EmbraceInternalInterfaceTest.kt | 11 ++++++ .../embrace/android/embracesdk/Embrace.java | 7 ---- .../android/embracesdk/EmbraceImpl.java | 15 -------- .../EmbraceInternalInterfaceImpl.kt | 6 ++- .../embracesdk/InternalInterfaceModule.kt | 9 +---- .../internal/EmbraceInternalInterface.kt | 8 ++++ .../android/embracesdk/internal/SdkApi.kt | 18 --------- .../android/embracesdk/internal/SdkApiImpl.kt | 7 ---- .../EmbraceInternalInterfaceImplTest.kt | 29 ++++++++++++++- .../InternalInterfaceModuleImplTest.kt | 1 - .../embracesdk/internal/SdkApiImplTest.kt | 37 ------------------- 12 files changed, 54 insertions(+), 98 deletions(-) delete mode 100644 embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApi.kt delete mode 100644 embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApiImpl.kt delete mode 100644 embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/internal/SdkApiImplTest.kt diff --git a/embrace-android-sdk/api/embrace-android-sdk.api b/embrace-android-sdk/api/embrace-android-sdk.api index 973e377292..12bdf2556a 100644 --- a/embrace-android-sdk/api/embrace-android-sdk.api +++ b/embrace-android-sdk/api/embrace-android-sdk.api @@ -40,7 +40,6 @@ public final class io/embrace/android/embracesdk/Embrace : io/embrace/android/em public fun getInternalInterface ()Lio/embrace/android/embracesdk/internal/EmbraceInternalInterface; public fun getLastRunEndState ()Lio/embrace/android/embracesdk/Embrace$LastRunEndState; public fun getReactNativeInternalInterface ()Lio/embrace/android/embracesdk/ReactNativeInternalInterface; - public fun getSdkApi ()Lio/embrace/android/embracesdk/internal/SdkApi; public fun getSessionProperties ()Ljava/util/Map; public fun getTraceIdHeader ()Ljava/lang/String; public fun getUnityInternalInterface ()Lio/embrace/android/embracesdk/UnityInternalInterface; @@ -178,9 +177,8 @@ public final class io/embrace/android/embracesdk/WebViewClientSwazzledHooks { public abstract interface annotation class io/embrace/android/embracesdk/annotation/StartupActivity : java/lang/annotation/Annotation { } -public abstract interface class io/embrace/android/embracesdk/internal/SdkApi { - public abstract fun getSdkCurrentTime ()J public abstract interface class io/embrace/android/embracesdk/internal/EmbraceInternalInterface { + public abstract fun getSdkCurrentTime ()J public abstract fun isNetworkSpanForwardingEnabled ()Z public abstract fun logComposeTap (Landroid/util/Pair;Ljava/lang/String;)V public abstract fun logError (Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Z)V diff --git a/embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testcases/EmbraceInternalInterfaceTest.kt b/embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testcases/EmbraceInternalInterfaceTest.kt index a5b33cac17..a4785c040f 100644 --- a/embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testcases/EmbraceInternalInterfaceTest.kt +++ b/embrace-android-sdk/src/integrationTest/kotlin/io/embrace/android/embracesdk/testcases/EmbraceInternalInterfaceTest.kt @@ -92,6 +92,7 @@ internal class EmbraceInternalInterfaceTest { assertFalse(shouldCaptureNetworkBody("", "")) setProcessStartedByNotification() assertFalse(isNetworkSpanForwardingEnabled()) + getSdkCurrentTime() } } @@ -241,6 +242,16 @@ internal class EmbraceInternalInterfaceTest { } } + @Test + fun `test sdk time`() { + with(testRule) { + embrace.start(harness.fakeCoreModule.context) + assertEquals(harness.fakeClock.now(), embrace.internalInterface.getSdkCurrentTime()) + harness.fakeClock.tick() + assertEquals(harness.fakeClock.now(), embrace.internalInterface.getSdkCurrentTime()) + } + } + companion object { private const val URL = "https://embrace.io" private const val START_TIME = 1692201601L diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/Embrace.java b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/Embrace.java index f029e13b3e..5f30fab7e0 100644 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/Embrace.java +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/Embrace.java @@ -14,7 +14,6 @@ import io.embrace.android.embracesdk.config.ConfigService; import io.embrace.android.embracesdk.internal.EmbraceInternalInterface; -import io.embrace.android.embracesdk.internal.SdkApi; import io.embrace.android.embracesdk.logging.InternalEmbraceLogger; import io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger; import io.embrace.android.embracesdk.network.EmbraceNetworkRequest; @@ -738,12 +737,6 @@ public int getValue() { } } - @NonNull - @InternalApi - public SdkApi getSdkApi() { - return impl.getSdkApi(); - } - /** * Enum representing the end state of the last run of the application. */ diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.java b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.java index 45cec6e853..d664ba6eae 100644 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.java +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceImpl.java @@ -65,8 +65,6 @@ import io.embrace.android.embracesdk.internal.EmbraceInternalInterface; import io.embrace.android.embracesdk.internal.EmbraceInternalInterfaceKt; import io.embrace.android.embracesdk.internal.MessageType; -import io.embrace.android.embracesdk.internal.SdkApi; -import io.embrace.android.embracesdk.internal.SdkApiKt; import io.embrace.android.embracesdk.internal.TraceparentGenerator; import io.embrace.android.embracesdk.internal.crash.LastRunCrashVerifier; import io.embrace.android.embracesdk.internal.spans.EmbraceSpansService; @@ -225,9 +223,6 @@ final class EmbraceImpl { @Nullable private FlutterInternalInterface flutterInternalInterface; - @Nullable - private SdkApi sdkApi; - @Nullable private PushNotificationCaptureService pushNotificationService; @@ -1747,14 +1742,4 @@ private void loadCrashVerifier(CrashModule crashModule, WorkerThreadModule worke workerThreadModule.backgroundExecutor(ExecutorName.BACKGROUND_REGISTRATION) ); } - - @InternalApi - @NonNull - public SdkApi getSdkApi() { - if (isStarted() && sdkApi != null) { - return sdkApi; - } else { - return SdkApiKt.getDefault(); - } - } } diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceInternalInterfaceImpl.kt b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceInternalInterfaceImpl.kt index 08b65506bc..df71f0f4df 100644 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceInternalInterfaceImpl.kt +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/EmbraceInternalInterfaceImpl.kt @@ -1,6 +1,7 @@ package io.embrace.android.embracesdk import android.util.Pair +import io.embrace.android.embracesdk.injection.InitModule import io.embrace.android.embracesdk.internal.EmbraceInternalInterface import io.embrace.android.embracesdk.network.EmbraceNetworkRequest import io.embrace.android.embracesdk.network.http.HttpMethod @@ -8,7 +9,8 @@ import io.embrace.android.embracesdk.network.http.NetworkCaptureData import io.embrace.android.embracesdk.payload.TapBreadcrumb internal class EmbraceInternalInterfaceImpl( - private val embraceImpl: EmbraceImpl + private val embraceImpl: EmbraceImpl, + private val initModule: InitModule ) : EmbraceInternalInterface { override fun logInfo(message: String, properties: Map?) { @@ -173,4 +175,6 @@ internal class EmbraceInternalInterfaceImpl( override fun isNetworkSpanForwardingEnabled(): Boolean { return embraceImpl.configService?.networkSpanForwardingBehavior?.isNetworkSpanForwardingEnabled() ?: false } + + override fun getSdkCurrentTime(): Long = initModule.clock.now() } diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/InternalInterfaceModule.kt b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/InternalInterfaceModule.kt index d5029863b6..13527911c2 100644 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/InternalInterfaceModule.kt +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/InternalInterfaceModule.kt @@ -7,15 +7,12 @@ import io.embrace.android.embracesdk.injection.EssentialServiceModule import io.embrace.android.embracesdk.injection.InitModule import io.embrace.android.embracesdk.injection.singleton import io.embrace.android.embracesdk.internal.EmbraceInternalInterface -import io.embrace.android.embracesdk.internal.SdkApi -import io.embrace.android.embracesdk.internal.SdkApiImpl internal interface InternalInterfaceModule { val embraceInternalInterface: EmbraceInternalInterface val reactNativeInternalInterface: ReactNativeInternalInterface val unityInternalInterface: UnityInternalInterface val flutterInternalInterface: FlutterInternalInterface - val sdkApi: SdkApi } internal class InternalInterfaceModuleImpl( @@ -28,7 +25,7 @@ internal class InternalInterfaceModuleImpl( ) : InternalInterfaceModule { override val embraceInternalInterface: EmbraceInternalInterface by singleton { - EmbraceInternalInterfaceImpl(embrace) + EmbraceInternalInterfaceImpl(embrace, initModule) } override val reactNativeInternalInterface: ReactNativeInternalInterface by singleton { @@ -60,8 +57,4 @@ internal class InternalInterfaceModuleImpl( coreModule.logger ) } - - override val sdkApi: SdkApi by singleton { - SdkApiImpl(initModule.clock) - } } diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/EmbraceInternalInterface.kt b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/EmbraceInternalInterface.kt index 15a3b4539a..e21964352b 100644 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/EmbraceInternalInterface.kt +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/EmbraceInternalInterface.kt @@ -126,6 +126,12 @@ public interface EmbraceInternalInterface { * Whether the Network Span Forwarding feature is enabled */ public fun isNetworkSpanForwardingEnabled(): Boolean + + /** + * Return internal time the SDK is using in milliseconds. It is equivalent to [System.currentTimeMillis] assuming the system clock did + * not change after the SDK has started. + */ + public fun getSdkCurrentTime(): Long } internal val defaultImpl = object : EmbraceInternalInterface { @@ -185,4 +191,6 @@ internal val defaultImpl = object : EmbraceInternalInterface { override fun setProcessStartedByNotification() { } override fun isNetworkSpanForwardingEnabled(): Boolean = false + + override fun getSdkCurrentTime(): Long = System.currentTimeMillis() } diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApi.kt b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApi.kt deleted file mode 100644 index 22d6d2b88d..0000000000 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApi.kt +++ /dev/null @@ -1,18 +0,0 @@ -package io.embrace.android.embracesdk.internal - -import io.embrace.android.embracesdk.InternalApi - -/** - * A set of methods exposed internally for other Embrace SDK modules to use - not supported publicly and can change at any time. - */ -@InternalApi -public interface SdkApi { - public fun getSdkCurrentTime(): Long -} - -/** - * Default implementation used when instance bound to the SDK cannot be used - */ -internal val default: SdkApi = object : SdkApi { - override fun getSdkCurrentTime(): Long = System.currentTimeMillis() -} diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApiImpl.kt b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApiImpl.kt deleted file mode 100644 index 053584fae2..0000000000 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/SdkApiImpl.kt +++ /dev/null @@ -1,7 +0,0 @@ -package io.embrace.android.embracesdk.internal - -import io.embrace.android.embracesdk.clock.Clock - -internal class SdkApiImpl(private val clock: Clock) : SdkApi { - override fun getSdkCurrentTime(): Long = clock.now() -} diff --git a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/EmbraceInternalInterfaceImplTest.kt b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/EmbraceInternalInterfaceImplTest.kt index cbe962a288..1a1b42eb98 100644 --- a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/EmbraceInternalInterfaceImplTest.kt +++ b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/EmbraceInternalInterfaceImplTest.kt @@ -2,6 +2,10 @@ package io.embrace.android.embracesdk import android.net.Uri import android.webkit.URLUtil +import io.embrace.android.embracesdk.fakes.FakeClock +import io.embrace.android.embracesdk.fakes.injection.FakeInitModule +import io.embrace.android.embracesdk.injection.InitModule +import io.embrace.android.embracesdk.internal.defaultImpl import io.embrace.android.embracesdk.network.EmbraceNetworkRequest import io.embrace.android.embracesdk.network.http.HttpMethod import io.mockk.every @@ -11,6 +15,7 @@ import io.mockk.slot import io.mockk.verify import org.junit.Assert.assertEquals import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test @@ -18,11 +23,15 @@ internal class EmbraceInternalInterfaceImplTest { private lateinit var impl: EmbraceInternalInterfaceImpl private lateinit var embrace: EmbraceImpl + private lateinit var fakeClock: FakeClock + private lateinit var initModule: InitModule @Before fun setUp() { embrace = mockk(relaxed = true) - impl = EmbraceInternalInterfaceImpl(embrace) + fakeClock = FakeClock(currentTime = beforeObjectInitTime) + initModule = FakeInitModule(clock = fakeClock) + impl = EmbraceInternalInterfaceImpl(embrace, initModule) } @Test @@ -176,4 +185,22 @@ internal class EmbraceInternalInterfaceImplTest { assertEquals(url, captor.captured.url) } + + @Test + fun `check usage of SDK time`() { + assertEquals(beforeObjectInitTime, impl.getSdkCurrentTime()) + assertTrue(impl.getSdkCurrentTime() < System.currentTimeMillis()) + fakeClock.tick(10L) + assertEquals(fakeClock.now(), impl.getSdkCurrentTime()) + } + + @Test + fun `check default implementation`() { + assertTrue(beforeObjectInitTime < defaultImpl.getSdkCurrentTime()) + assertTrue(defaultImpl.getSdkCurrentTime() <= System.currentTimeMillis()) + } + + companion object { + val beforeObjectInitTime = System.currentTimeMillis() - 1 + } } diff --git a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/InternalInterfaceModuleImplTest.kt b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/InternalInterfaceModuleImplTest.kt index e547ace8db..158ef66211 100644 --- a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/InternalInterfaceModuleImplTest.kt +++ b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/InternalInterfaceModuleImplTest.kt @@ -25,6 +25,5 @@ internal class InternalInterfaceModuleImplTest { assertNotNull(module.unityInternalInterface) assertNotNull(module.reactNativeInternalInterface) assertNotNull(module.embraceInternalInterface) - assertNotNull(module.sdkApi) } } diff --git a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/internal/SdkApiImplTest.kt b/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/internal/SdkApiImplTest.kt deleted file mode 100644 index 8b4ebde4f1..0000000000 --- a/embrace-android-sdk/src/test/java/io/embrace/android/embracesdk/internal/SdkApiImplTest.kt +++ /dev/null @@ -1,37 +0,0 @@ -package io.embrace.android.embracesdk.internal - -import io.embrace.android.embracesdk.fakes.FakeClock -import org.junit.Assert.assertEquals -import org.junit.Assert.assertTrue -import org.junit.Before -import org.junit.Test - -internal class SdkApiImplTest { - - private lateinit var fakeClock: FakeClock - private lateinit var sdkApi: SdkApiImpl - - @Before - fun setup() { - fakeClock = FakeClock(currentTime = beforeObjectInitTime) - sdkApi = SdkApiImpl(fakeClock) - } - - @Test - fun `check usage of SDK time`() { - assertEquals(beforeObjectInitTime, sdkApi.getSdkCurrentTime()) - assertTrue(sdkApi.getSdkCurrentTime() < System.currentTimeMillis()) - fakeClock.tick(10L) - assertEquals(fakeClock.now(), sdkApi.getSdkCurrentTime()) - } - - @Test - fun `check default implementation`() { - assertTrue(beforeObjectInitTime < default.getSdkCurrentTime()) - assertTrue(default.getSdkCurrentTime() <= System.currentTimeMillis()) - } - - companion object { - val beforeObjectInitTime = System.currentTimeMillis() - 1 - } -}