Skip to content

Commit

Permalink
For mozilla-mobile#19976: Adjust telemetry tests for credit cards.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarare committed Jul 30, 2021
1 parent ea5e3f5 commit 8a4e146
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 66 deletions.
6 changes: 0 additions & 6 deletions app/src/test/java/org/mozilla/fenix/FenixApplicationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,6 @@ class FenixApplicationTest {
every { settings.touchExplorationIsEnabled } returns true
every { settings.shouldUseLightTheme } returns true
every { settings.signedInFxaAccount } returns true
every { settings.creditCardsSavedCount } returns 1
every { settings.creditCardsDeletedCount } returns 2
every { settings.creditCardsAutofilledCount } returns 3

application.setStartupMetrics(browserStore, settings, browsersCache, mozillaProductDetector)

Expand All @@ -149,9 +146,6 @@ class FenixApplicationTest {
assertEquals(true, Metrics.hasTopSites.testGetValue())
assertEquals(2, Metrics.topSitesCount.testGetValue())
assertEquals(true, Addons.hasInstalledAddons.testGetValue())
assertEquals(1, Metrics.creditCardsSavedCount.testGetValue())
assertEquals(2, Metrics.creditCardsDeletedCount.testGetValue())
assertEquals(3, Metrics.creditCardsAutofillCount.testGetValue())
assertEquals(listOf("test1", "test2", "test3"), Addons.installedAddons.testGetValue())
assertEquals(true, Addons.hasEnabledAddons.testGetValue())
assertEquals(listOf("test1", "test2"), Addons.enabledAddons.testGetValue())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.verify
import io.mockk.verifyAll
import mozilla.components.feature.prompts.facts.CreditCardAutofillDialogFacts
import mozilla.components.feature.top.sites.facts.TopSitesFacts
import mozilla.components.support.base.Component
import mozilla.components.support.base.facts.Action
Expand Down Expand Up @@ -373,31 +372,4 @@ class MetricControllerTest {
assertEquals(settings.enabledAddonsCount, 2)
assertEquals(settings.enabledAddonsList, "test2,test4")
}

@Test
fun `credit card autofill fact shold set value in SharedPreference`() {
val enabled = true
val settings = Settings(testContext)
val controller = ReleaseMetricController(
services = listOf(dataService1),
isDataTelemetryEnabled = { enabled },
isMarketingDataTelemetryEnabled = { enabled },
settings
)
val fact = Fact(
component = Component.FEATURE_PROMPTS,
action = Action.INTERACTION,
item = CreditCardAutofillDialogFacts.Items.AUTOFILL_CREDIT_CARD_SUCCESS
)

assertEquals(0, settings.creditCardsAutofilledCount)

controller.factToEvent(fact)

assertEquals(1, settings.creditCardsAutofilledCount)

controller.factToEvent(fact)

assertEquals(2, settings.creditCardsAutofilledCount)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,42 @@ import mozilla.components.concept.storage.CreditCardNumber
import mozilla.components.concept.storage.NewCreditCardFields
import mozilla.components.concept.storage.UpdatableCreditCardFields
import mozilla.components.service.sync.autofill.AutofillCreditCardsAddressesStorage
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.rule.MainCoroutineRule
import mozilla.components.support.utils.CreditCardNetworkType
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MetricController
import org.mozilla.fenix.helpers.FenixRobolectricTestRunner
import org.mozilla.fenix.settings.creditcards.controller.DefaultCreditCardEditorController
import org.mozilla.fenix.utils.Settings

@ExperimentalCoroutinesApi
@RunWith(FenixRobolectricTestRunner::class)
class DefaultCreditCardEditorControllerTest {

private val storage: AutofillCreditCardsAddressesStorage = mockk(relaxed = true)
private val navController: NavController = mockk(relaxed = true)
private val metrics: MetricController = mockk(relaxed = true)

private val testCoroutineScope = TestCoroutineScope()
private val testDispatcher = TestCoroutineDispatcher()

private lateinit var controller: DefaultCreditCardEditorController
private lateinit var settings: Settings

@get:Rule
val coroutinesTestRule = MainCoroutineRule(testDispatcher)

@Before
fun setup() {
settings = Settings(testContext)
controller = spyk(
DefaultCreditCardEditorController(
storage = storage,
lifecycleScope = testCoroutineScope,
navController = navController,
settings = settings,
metrics = metrics,
ioDispatcher = testDispatcher
)
)
Expand All @@ -77,23 +75,20 @@ class DefaultCreditCardEditorControllerTest {

@Test
fun handleDeleteCreditCard() = testCoroutineScope.runBlockingTest {
assertEquals(0, settings.creditCardsDeletedCount)

val creditCardId = "id"

controller.handleDeleteCreditCard(creditCardId)

assertEquals(1, settings.creditCardsDeletedCount)

coVerify {
storage.deleteCreditCard(creditCardId)
metrics.track(Event.CreditCardDelete)
navController.popBackStack()
}
}

@Test
fun handleSaveCreditCard() = testCoroutineScope.runBlockingTest {
assertEquals(0, settings.creditCardsSavedCount)

val creditCardFields = NewCreditCardFields(
billingName = "Banana Apple",
Expand All @@ -106,10 +101,9 @@ class DefaultCreditCardEditorControllerTest {

controller.handleSaveCreditCard(creditCardFields)

assertEquals(1, settings.creditCardsSavedCount)

coVerify {
storage.addCreditCard(creditCardFields)
metrics.track(Event.CreditCardManualSave)
navController.popBackStack()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,47 @@

package org.mozilla.fenix.settings.creditcards

import androidx.navigation.NavController
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestCoroutineScope
import mozilla.components.concept.storage.CreditCard
import mozilla.components.concept.storage.CreditCardNumber
import mozilla.components.concept.storage.NewCreditCardFields
import mozilla.components.concept.storage.UpdatableCreditCardFields
import mozilla.components.service.sync.autofill.AutofillCreditCardsAddressesStorage
import mozilla.components.support.utils.CreditCardNetworkType
import org.junit.Before
import org.junit.Test
import org.mozilla.fenix.components.metrics.MetricController
import org.mozilla.fenix.settings.creditcards.controller.CreditCardEditorController
import org.mozilla.fenix.settings.creditcards.controller.DefaultCreditCardEditorController
import org.mozilla.fenix.settings.creditcards.interactor.DefaultCreditCardEditorInteractor

@ExperimentalCoroutinesApi
class DefaultCreditCardEditorInteractorTest {

private val controller: CreditCardEditorController = mockk(relaxed = true)
private val storage: AutofillCreditCardsAddressesStorage = mockk(relaxed = true)
private val navController: NavController = mockk(relaxed = true)
private val metrics: MetricController = mockk(relaxed = true)
private val testCoroutineScope = TestCoroutineScope()

private lateinit var controller: CreditCardEditorController
private lateinit var interactor: DefaultCreditCardEditorInteractor

@Before
fun setup() {
controller = spyk(
DefaultCreditCardEditorController(
storage,
testCoroutineScope,
navController,
metrics
)
)

interactor = DefaultCreditCardEditorInteractor(controller)
}

Expand Down
19 changes: 0 additions & 19 deletions app/src/test/java/org/mozilla/fenix/utils/SettingsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -675,25 +675,6 @@ class SettingsTest {
assertTrue(settings.amoCollectionOverrideConfigured())
}

@Test
fun creditCardsSavedCount() {
// When just created
// Then
assertEquals(0, settings.creditCardsSavedCount)

// When
settings.creditCardsSavedCount += 1

// Then
assertEquals(1, settings.creditCardsSavedCount)

// When
settings.creditCardsSavedCount += 1

// Then
assertEquals(2, settings.creditCardsSavedCount)
}

@Test
fun `GIVEN startOnHomeAlways is selected WHEN calling shouldStartOnHome THEN return true`() {
settings.startOnHomeAlways = true
Expand Down

0 comments on commit 8a4e146

Please sign in to comment.