From e957fd8f3d72f2117dfa4e486cc6c9d92decb7ed Mon Sep 17 00:00:00 2001 From: Marius Constantin Date: Fri, 6 Dec 2019 08:51:47 +0100 Subject: [PATCH] RUMM-55 persist application packageName at init --- .../src/main/kotlin/com/datadog/android/Datadog.kt | 13 +++++++------ .../test/kotlin/com/datadog/android/DatadogTest.kt | 11 +++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/dd-sdk-android/src/main/kotlin/com/datadog/android/Datadog.kt b/dd-sdk-android/src/main/kotlin/com/datadog/android/Datadog.kt index 8ac6df277c..c41e3bfdce 100644 --- a/dd-sdk-android/src/main/kotlin/com/datadog/android/Datadog.kt +++ b/dd-sdk-android/src/main/kotlin/com/datadog/android/Datadog.kt @@ -46,9 +46,9 @@ object Datadog { private lateinit var logStrategy: LogStrategy private lateinit var networkInfoProvider: BroadcastReceiverNetworkInfoProvider private lateinit var handlerThread: LogHandlerThread - private lateinit var contextRef: WeakReference + private var contextRef: WeakReference = WeakReference(null) private lateinit var uploader: LogUploader - private var endpointUrl: String = DATADOG_US + internal var packageName: String = "" /** * Initializes the Datadog SDK. @@ -67,6 +67,7 @@ object Datadog { check(!initialized) { "Datadog has already been initialized." } val appContext = context.applicationContext + packageName = context.packageName contextRef = WeakReference(appContext) this.clientToken = clientToken logStrategy = LogFileStrategy(appContext) @@ -101,13 +102,13 @@ object Datadog { logStrategy.getLogReader().dropAllBatches() sdkLogger.w( "$TAG: old logs targeted at $endpointUrl " + - "will now be deleted" + "will now be deleted" ) } EndpointUpdateStrategy.SEND_OLD_LOGS_TO_NEW_ENDPOINT -> { sdkLogger.w( "$TAG: old logs targeted at $endpointUrl " + - "will now be sent to $endpointUrl" + "will now be sent to $endpointUrl" ) } } @@ -144,8 +145,8 @@ object Datadog { private fun checkInitialized() { check(initialized) { "Datadog has not been initialized.\n" + - "Please add the following code in your application's onCreate() method:\n" + - "Datadog.initialized(context, \"CLIENT_TOKEN\");" + "Please add the following code in your application's onCreate() method:\n" + + "Datadog.initialized(context, \"CLIENT_TOKEN\");" } } diff --git a/dd-sdk-android/src/test/kotlin/com/datadog/android/DatadogTest.kt b/dd-sdk-android/src/test/kotlin/com/datadog/android/DatadogTest.kt index 8c4c3cc030..19c31691e5 100644 --- a/dd-sdk-android/src/test/kotlin/com/datadog/android/DatadogTest.kt +++ b/dd-sdk-android/src/test/kotlin/com/datadog/android/DatadogTest.kt @@ -64,6 +64,8 @@ internal class DatadogTest { lateinit var fakeToken: String + lateinit var packageName: String + @BeforeEach fun `set up`(forge: Forge) { fakeToken = forge.anHexadecimalString() @@ -77,6 +79,9 @@ internal class DatadogTest { whenever(mockConnectivityMgr.activeNetworkInfo) doReturn mockNetworkInfo whenever(mockNetworkInfo.isConnected) doReturn true whenever(mockNetworkInfo.type) doReturn ConnectivityManager.TYPE_WIFI + + packageName = forge.anAlphabeticalString() + whenever(mockContext.packageName) doReturn packageName } @AfterEach @@ -88,6 +93,12 @@ internal class DatadogTest { } } + fun `it will initialize all dependencies at initialize`(){ + Datadog.initialize(mockContext, fakeToken) + + assertThat(Datadog.packageName).isEqualTo(packageName) + } + @Test fun `registers broadcast receiver on initialize`() { Datadog.initialize(mockContext, fakeToken)