Bug 1807268 - Re-enable verifyShowClipboardSuggestionsToggleTest UI test r=jajohnson
[gecko.git] / mobile / android / fenix / app / src / androidTest / java / org / mozilla / fenix / ui / NimbusMessagingNotificationTest.kt
blob1348ff00715d9312c02f627284f4b49e8910ab8f
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.fenix.ui
7 import android.content.Context
8 import android.os.Build
9 import androidx.test.rule.GrantPermissionRule
10 import androidx.test.rule.GrantPermissionRule.grant
11 import mozilla.components.service.nimbus.messaging.FxNimbusMessaging
12 import org.json.JSONObject
13 import org.junit.Before
14 import org.junit.Rule
15 import org.junit.Test
16 import org.mozilla.experiments.nimbus.HardcodedNimbusFeatures
17 import org.mozilla.fenix.helpers.HomeActivityIntentTestRule
18 import org.mozilla.fenix.helpers.TestHelper
19 import org.mozilla.fenix.helpers.TestHelper.mDevice
20 import org.mozilla.fenix.helpers.TestSetup
21 import org.mozilla.fenix.nimbus.FxNimbus
22 import org.mozilla.fenix.ui.robots.notificationShade
24 /**
25  * A UI test for testing the notification surface for Nimbus Messaging.
26  */
27 class NimbusMessagingNotificationTest : TestSetup() {
28     private lateinit var context: Context
29     private lateinit var hardcodedNimbus: HardcodedNimbusFeatures
31     @get:Rule
32     val activityTestRule =
33         HomeActivityIntentTestRule.withDefaultSettingsOverrides(skipOnboarding = true)
35     @get:Rule
36     val grantPermissionRule: GrantPermissionRule =
37         if (Build.VERSION.SDK_INT >= 33) {
38             grant("android.permission.POST_NOTIFICATIONS")
39         } else {
40             grant()
41         }
43     @Before
44     override fun setUp() {
45         super.setUp()
46         context = TestHelper.appContext
47     }
49     @Test
50     fun testShowingNotificationMessage() {
51         hardcodedNimbus = HardcodedNimbusFeatures(
52             context,
53             "messaging" to JSONObject(
54                 """
55                 {
56                   "message-under-experiment": "test-default-browser-notification",
57                   "messages": {
58                     "test-default-browser-notification": {
59                       "title": "preferences_set_as_default_browser",
60                       "text": "default_browser_experiment_card_text",
61                       "surface": "notification",
62                       "style": "NOTIFICATION",
63                       "action": "MAKE_DEFAULT_BROWSER",
64                       "trigger": [
65                         "ALWAYS"
66                       ]
67                     }
68                   }
69                 }
70                 """.trimIndent(),
71             ),
72         )
73         // The scheduling of the Messaging Notification Worker happens in the HomeActivity
74         // onResume().
75         // We need to have connected FxNimbus to hardcodedNimbus by the time it is scheduled, so
76         // we finishActivity, connect, _then_ re-launch the activity so that the worker has
77         // hardcodedNimbus by the time its re-scheduled.
78         // Because the scheduling happens for a second time, the work request needs to replace the
79         // existing one.
80         activityTestRule.finishActivity()
81         hardcodedNimbus.connectWith(FxNimbus)
82         activityTestRule.launchActivity(null)
84         mDevice.openNotification()
85         notificationShade {
86             val data =
87                 FxNimbusMessaging.features.messaging.value().messages["test-default-browser-notification"]
88             verifySystemNotificationExists(data!!.title!!)
89             verifySystemNotificationExists(data.text)
90         }
91     }