diff --git a/app/src/androidTest/java/org/mozilla/fenix/helpers/HomeActivityTestRule.kt b/app/src/androidTest/java/org/mozilla/fenix/helpers/HomeActivityTestRule.kt index efda021861..eb1950b629 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/helpers/HomeActivityTestRule.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/helpers/HomeActivityTestRule.kt @@ -6,6 +6,7 @@ package org.mozilla.fenix.helpers +import android.content.Intent import android.view.ViewConfiguration.getLongPressTimeout import androidx.test.espresso.intent.rule.IntentsTestRule import androidx.test.rule.ActivityTestRule @@ -161,6 +162,8 @@ class HomeActivityIntentTestRule internal constructor( private val longTapUserPreference = getLongPressTimeout() + private lateinit var intent: Intent + /** * Update settings after the activity was created. */ @@ -171,6 +174,19 @@ class HomeActivityIntentTestRule internal constructor( } } + override fun getActivityIntent(): Intent { + return if(this::intent.isInitialized) { + this.intent + } else { + super.getActivityIntent() + } + } + + fun withIntent(intent: Intent): HomeActivityIntentTestRule { + this.intent = intent + return this + } + override fun beforeActivityLaunched() { super.beforeActivityLaunched() setLongTapTimeout(3000) diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/NimbusEventTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/NimbusEventTest.kt new file mode 100644 index 0000000000..3b36ceaf33 --- /dev/null +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/NimbusEventTest.kt @@ -0,0 +1,51 @@ +package org.mozilla.fenix.ui + +import android.content.Intent +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.uiautomator.UiDevice +import okhttp3.mockwebserver.MockWebServer +import org.junit.* +import org.mozilla.fenix.helpers.AndroidAssetDispatcher +import org.mozilla.fenix.helpers.HomeActivityIntentTestRule +import org.mozilla.fenix.helpers.RetryTestRule +import org.mozilla.fenix.ui.robots.homeScreen + +class NimbusEventTest { + private lateinit var mDevice: UiDevice + private lateinit var mockWebServer: MockWebServer + + @get:Rule + val activityTestRule = HomeActivityIntentTestRule.withDefaultSettingsOverrides() + .withIntent(Intent().apply { + action = Intent.ACTION_VIEW + }) + + @Rule + @JvmField + val retryTestRule = RetryTestRule(3) + + @Before + fun setUp() { + mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) + + mockWebServer = MockWebServer().apply { + dispatcher = AndroidAssetDispatcher() + start() + } + } + + @After + fun tearDown() { + mockWebServer.shutdown() + } + + @Test + fun homeScreenNimbusEventsTest() { + homeScreen { }.dismissOnboarding() + + homeScreen { + verifyHomeScreen() + Assert.assertTrue(evaluateAgainstNimbusTargetingHelper("'app_opened'|eventSum('Days', 28, 0) > 0")) + } + } +} diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/HomeScreenRobot.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/HomeScreenRobot.kt index 346788f345..9ce36b07ba 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/HomeScreenRobot.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/HomeScreenRobot.kt @@ -90,6 +90,7 @@ class HomeScreenRobot { fun verifyDefaultSearchEngine(searchEngine: String) = verifySearchEngineIcon(searchEngine) fun verifyNoTabsOpened() = assertNoTabsOpened() fun verifyKeyboardVisible() = assertKeyboardVisibility(isExpectedToBeVisible = true) + fun evaluateAgainstNimbusTargetingHelper(jexl: String): Boolean = evaluateAgainstNimbus(jexl) fun verifyWallpaperImageApplied(isEnabled: Boolean) { if (isEnabled) { @@ -806,6 +807,14 @@ private fun verifySearchEngineIcon(searchEngineName: String) { verifySearchEngineIcon(defaultSearchEngine.icon, defaultSearchEngine.name) } +private fun getNimbus() = + appContext.components.analytics.experiments + +private fun evaluateAgainstNimbus(jexl: String): Boolean { + val helper = getNimbus().createMessageHelper() + return helper.evalJexl(jexl) +} + // First Run elements private fun assertWelcomeHeader() = assertTrue( diff --git a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt index aa972e5b81..fa5b10fa7b 100644 --- a/app/src/main/java/org/mozilla/fenix/HomeActivity.kt +++ b/app/src/main/java/org/mozilla/fenix/HomeActivity.kt @@ -276,7 +276,10 @@ open class HomeActivity : LocaleAwareAppCompatActivity(), NavHostActivity { val safeIntent = intent?.toSafeIntent() safeIntent ?.let(::getIntentSource) - ?.also { Events.appOpened.record(Events.AppOpenedExtra(it)) } + ?.also { + Events.appOpened.record(Events.AppOpenedExtra(it)) + components.analytics.experiments.recordEvent("app_opened") + } } supportActionBar?.hide()