Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure InstallSource extraction logic runs after privacy config downloaded #5013

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ import android.content.Context
import android.content.SharedPreferences
import androidx.annotation.VisibleForTesting
import androidx.core.content.edit
import androidx.lifecycle.LifecycleOwner
import com.duckduckgo.app.di.AppCoroutineScope
import com.duckduckgo.app.lifecycle.MainProcessLifecycleObserver
import com.duckduckgo.app.statistics.pixels.Pixel
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.duckduckgo.installation.impl.installer.InstallationPixelName.APP_INSTALLER_PACKAGE_NAME
import com.duckduckgo.privacy.config.api.PrivacyConfigCallbackPlugin
import com.squareup.anvil.annotations.ContributesMultibinding
import dagger.SingleInstanceIn
import javax.inject.Inject
Expand All @@ -36,24 +35,22 @@ import timber.log.Timber

@ContributesMultibinding(
scope = AppScope::class,
boundType = MainProcessLifecycleObserver::class,
boundType = PrivacyConfigCallbackPlugin::class,
)
@SingleInstanceIn(AppScope::class)
class InstallSourceLifecycleObserver @Inject constructor(
class InstallSourcePrivacyConfigObserver @Inject constructor(
private val installSourceExtractor: InstallSourceExtractor,
private val context: Context,
private val pixel: Pixel,
private val dispatchers: DispatcherProvider,
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
) : MainProcessLifecycleObserver {
) : PrivacyConfigCallbackPlugin {

private val sharedPreferences: SharedPreferences by lazy {
context.getSharedPreferences(SHARED_PREFERENCES_FILENAME, Context.MODE_PRIVATE)
}

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)

override fun onPrivacyConfigDownloaded() {
appCoroutineScope.launch(dispatchers.io()) {
if (!hasAlreadyProcessed()) {
val installationSource = installSourceExtractor.extract()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package com.duckduckgo.installation.impl.installer

import androidx.lifecycle.LifecycleOwner
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.duckduckgo.app.statistics.pixels.Pixel
import com.duckduckgo.app.statistics.pixels.Pixel.PixelType.COUNT
Expand All @@ -34,17 +33,16 @@ import org.mockito.kotlin.verify
import org.robolectric.RuntimeEnvironment

@RunWith(AndroidJUnit4::class)
class InstallSourceLifecycleObserverTest {
class InstallSourcePrivacyConfigObserverTest {

@get:Rule
val coroutineTestRule: CoroutineTestRule = CoroutineTestRule()

private val mockLifecycleOwner = mock<LifecycleOwner>()
private val mockPixel = mock<Pixel>()
private val context = RuntimeEnvironment.getApplication()
private val mockInstallSourceExtractor = mock<InstallSourceExtractor>()

private val testee = InstallSourceLifecycleObserver(
private val testee = InstallSourcePrivacyConfigObserver(
context = context,
pixel = mockPixel,
dispatchers = coroutineTestRule.testDispatcherProvider,
Expand All @@ -54,14 +52,14 @@ class InstallSourceLifecycleObserverTest {

@Test
fun whenNotPreviouslyProcessedThenPixelSent() = runTest {
testee.onCreate(mockLifecycleOwner)
testee.onPrivacyConfigDownloaded()
verify(mockPixel).fire(eq(APP_INSTALLER_PACKAGE_NAME), any(), any(), eq(COUNT))
}

@Test
fun whenPreviouslyProcessedThenPixelNotSent() = runTest {
testee.recordInstallSourceProcessed()
testee.onCreate(mockLifecycleOwner)
testee.onPrivacyConfigDownloaded()
verify(mockPixel, never()).fire(eq(APP_INSTALLER_PACKAGE_NAME), any(), any(), eq(COUNT))
}
}
Loading