Skip to content

Commit

Permalink
add daily pixel
Browse files Browse the repository at this point in the history
seeing as this is the only place I want to synchronously get the show on app launch option I chose to runBlocking when getting the setting here rather than adding a function to the DataStore
  • Loading branch information
mikescamell committed Sep 19, 2024
1 parent 67c2a66 commit 6ed635a
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.generalsettings.showonapplaunch

import com.duckduckgo.app.generalsettings.showonapplaunch.model.ShowOnAppLaunchOption
import com.duckduckgo.app.generalsettings.showonapplaunch.store.ShowOnAppLaunchOptionDataStore
import com.duckduckgo.app.statistics.api.BrowserFeatureStateReporterPlugin
import com.duckduckgo.app.statistics.pixels.Pixel.PixelParameter
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.di.scopes.AppScope
import com.squareup.anvil.annotations.ContributesBinding
import com.squareup.anvil.annotations.ContributesMultibinding
import javax.inject.Inject
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking

interface ShowOnAppLaunchReporterPlugin

@ContributesMultibinding(
scope = AppScope::class,
boundType = BrowserFeatureStateReporterPlugin::class,
)
@ContributesBinding(scope = AppScope::class, boundType = ShowOnAppLaunchReporterPlugin::class)
class ShowOnAppLaunchStateReporterPlugin
@Inject
constructor(
private val dispatcherProvider: DispatcherProvider,
private val showOnAppLaunchOptionDataStore: ShowOnAppLaunchOptionDataStore,
) : ShowOnAppLaunchReporterPlugin, BrowserFeatureStateReporterPlugin {

override fun featureStateParams(): Map<String, String> {
val option =
runBlocking(dispatcherProvider.io()) {
showOnAppLaunchOptionDataStore.optionFlow.first()
}
val dailyPixelValue = ShowOnAppLaunchOption.getDailyPixelValue(option)
return mapOf(PixelParameter.LAUNCH_SCREEN to dailyPixelValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,11 @@ sealed class ShowOnAppLaunchOption(val id: Int) {
NewTabPage -> SETTINGS_GENERAL_APP_LAUNCH_NEW_TAB_PAGE_SELECTED
is SpecificPage -> SETTINGS_GENERAL_APP_LAUNCH_SPECIFIC_PAGE_SELECTED
}

fun getDailyPixelValue(option: ShowOnAppLaunchOption) = when (option) {
LastOpenedTab -> "last_opened_tab"
NewTabPage -> "new_tab_page"
is SpecificPage -> "specific_page"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.generalsettings.showonapplaunch

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.duckduckgo.app.generalsettings.showonapplaunch.model.ShowOnAppLaunchOption
import com.duckduckgo.app.generalsettings.showonapplaunch.store.FakeShowOnAppLaunchOptionDataStore
import com.duckduckgo.app.generalsettings.showonapplaunch.store.ShowOnAppLaunchOptionDataStore
import com.duckduckgo.app.statistics.pixels.Pixel.PixelParameter
import com.duckduckgo.common.test.CoroutineTestRule
import com.duckduckgo.common.utils.DispatcherProvider
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class ShowOnAppLaunchReporterPluginTest {

@get:Rule val coroutineTestRule = CoroutineTestRule()

private val dispatcherProvider: DispatcherProvider = coroutineTestRule.testDispatcherProvider
private lateinit var testee: ShowOnAppLaunchStateReporterPlugin
private lateinit var fakeDataStore: ShowOnAppLaunchOptionDataStore

@Before
fun setup() {
fakeDataStore = FakeShowOnAppLaunchOptionDataStore(ShowOnAppLaunchOption.LastOpenedTab)

testee = ShowOnAppLaunchStateReporterPlugin(dispatcherProvider, fakeDataStore)
}

@Test
fun whenOptionIsSetToLastOpenedPageThenShouldReturnDailyPixelValue() = runTest {
fakeDataStore.setShowOnAppLaunchOption(ShowOnAppLaunchOption.LastOpenedTab)
val result = testee.featureStateParams()
assertEquals("last_opened_tab", result[PixelParameter.LAUNCH_SCREEN])
}

@Test
fun whenOptionIsSetToNewTabPageThenShouldReturnDailyPixelValue() = runTest {
fakeDataStore.setShowOnAppLaunchOption(ShowOnAppLaunchOption.NewTabPage)
val result = testee.featureStateParams()
assertEquals("new_tab_page", result[PixelParameter.LAUNCH_SCREEN])
}

@Test
fun whenOptionIsSetToSpecificPageThenShouldReturnDailyPixelValue() = runTest {
val specificPage = ShowOnAppLaunchOption.SpecificPage("example.com")
fakeDataStore.setShowOnAppLaunchOption(specificPage)
val result = testee.featureStateParams()
assertEquals("specific_page", result[PixelParameter.LAUNCH_SCREEN])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ interface Pixel {

// Loading Bar Experiment
const val LOADING_BAR_EXPERIMENT = "loading_bar_exp"
const val LAUNCH_SCREEN = "launch_screen"
}

object PixelValues {
Expand Down

0 comments on commit 6ed635a

Please sign in to comment.