diff --git a/app/src/androidTest/java/org/mozilla/fenix/ui/NimbusMessagingNotificationTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/NimbusMessagingNotificationTest.kt new file mode 100644 index 0000000000..8549bf3008 --- /dev/null +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/NimbusMessagingNotificationTest.kt @@ -0,0 +1,93 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.ui + +import android.content.Context +import android.os.Build +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.rule.GrantPermissionRule +import androidx.test.rule.GrantPermissionRule.grant +import androidx.test.uiautomator.UiDevice +import org.json.JSONObject +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.mozilla.experiments.nimbus.HardcodedNimbusFeatures +import org.mozilla.fenix.helpers.HomeActivityIntentTestRule +import org.mozilla.fenix.helpers.TestHelper +import org.mozilla.fenix.nimbus.FxNimbus +import org.mozilla.fenix.ui.robots.notificationShade + +/** + * A UI test for testing the notification surface for Nimbus Messaging. + */ +class NimbusMessagingNotificationTest { + private lateinit var mDevice: UiDevice + + private lateinit var context: Context + private lateinit var hardcodedNimbus: HardcodedNimbusFeatures + + @get:Rule + val activityTestRule = + HomeActivityIntentTestRule.withDefaultSettingsOverrides(skipOnboarding = true) + + @get:Rule + val grantPermissionRule: GrantPermissionRule = + if (Build.VERSION.SDK_INT >= 33) { + grant("android.permission.POST_NOTIFICATIONS") + } else { + grant() + } + + @Before + fun setUp() { + context = TestHelper.appContext + mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) + } + + @Test + fun testShowingNotificationMessage() { + hardcodedNimbus = HardcodedNimbusFeatures( + context, + "messaging" to JSONObject( + """ + { + "message-under-experiment": "test-default-browser-notification", + "messages": { + "test-default-browser-notification": { + "title": "preferences_set_as_default_browser", + "text": "default_browser_experiment_card_text", + "surface": "notification", + "style": "NOTIFICATION", + "action": "MAKE_DEFAULT_BROWSER", + "trigger": [ + "ALWAYS" + ] + } + } + } + """.trimIndent(), + ), + ) + // The scheduling of the Messaging Notification Worker happens in the HomeActivity + // onResume(). + // We need to have connected FxNimbus to hardcodedNimbus by the time it is scheduled, so + // we finishActivity, connect, _then_ re-launch the activity so that the worker has + // hardcodedNimbus by the time its re-scheduled. + // Because the scheduling happens for a second time, the work request needs to replace the + // existing one. + activityTestRule.finishActivity() + hardcodedNimbus.connectWith(FxNimbus) + activityTestRule.launchActivity(null) + + mDevice.openNotification() + notificationShade { + val data = + FxNimbus.features.messaging.value().messages["test-default-browser-notification"] + verifySystemNotificationExists(data!!.title!!) + verifySystemNotificationExists(data.text) + } + } +} diff --git a/app/src/main/java/org/mozilla/fenix/gleanplumb/MessageNotificationWorker.kt b/app/src/main/java/org/mozilla/fenix/gleanplumb/MessageNotificationWorker.kt index 76ee10fd6f..74abfe6837 100644 --- a/app/src/main/java/org/mozilla/fenix/gleanplumb/MessageNotificationWorker.kt +++ b/app/src/main/java/org/mozilla/fenix/gleanplumb/MessageNotificationWorker.kt @@ -147,7 +147,8 @@ class MessageNotificationWorker( * Initialize the [Worker] to begin polling Nimbus. */ fun setMessageNotificationWorker(context: Context) { - val featureConfig = FxNimbus.features.messaging.value() + val messaging = FxNimbus.features.messaging + val featureConfig = messaging.value() val notificationConfig = featureConfig.notificationConfig val pollingInterval = notificationConfig.refreshInterval.toLong() @@ -159,8 +160,13 @@ class MessageNotificationWorker( val instanceWorkManager = WorkManager.getInstance(context) instanceWorkManager.enqueueUniquePeriodicWork( MESSAGE_WORK_NAME, - // We want to keep any existing scheduled work. - ExistingPeriodicWorkPolicy.KEEP, + // We want to keep any existing scheduled work, unless + // when we're under test. + if (messaging.isUnderTest()) { + ExistingPeriodicWorkPolicy.REPLACE + } else { + ExistingPeriodicWorkPolicy.KEEP + }, messageWorkRequest, ) }