Bug 1882473 - Convert private variables to functions so they don't get initialised
[gecko.git] / mobile / android / fenix / app / src / androidTest / java / org / mozilla / fenix / ui / robots / SettingsSubMenuSitePermissionsCommonRobot.kt
blob4348eab06783f464a50537db6009f0f2e93750f0
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.robots
7 import androidx.test.espresso.Espresso.onView
8 import androidx.test.espresso.assertion.ViewAssertions.matches
9 import androidx.test.espresso.matcher.ViewMatchers.Visibility
10 import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
11 import androidx.test.espresso.matcher.ViewMatchers.withContentDescription
12 import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
13 import androidx.test.espresso.matcher.ViewMatchers.withId
14 import androidx.test.espresso.matcher.ViewMatchers.withText
15 import androidx.test.uiautomator.UiSelector
16 import org.hamcrest.CoreMatchers.allOf
17 import org.mozilla.fenix.R
18 import org.mozilla.fenix.helpers.DataGenerationHelper.getStringResource
19 import org.mozilla.fenix.helpers.MatcherHelper.assertUIObjectExists
20 import org.mozilla.fenix.helpers.MatcherHelper.itemContainingText
21 import org.mozilla.fenix.helpers.MatcherHelper.itemWithClassName
22 import org.mozilla.fenix.helpers.MatcherHelper.itemWithDescription
23 import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime
24 import org.mozilla.fenix.helpers.TestAssetHelper.waitingTimeShort
25 import org.mozilla.fenix.helpers.TestHelper.mDevice
26 import org.mozilla.fenix.helpers.TestHelper.packageName
27 import org.mozilla.fenix.helpers.assertIsChecked
28 import org.mozilla.fenix.helpers.click
30 /**
31  * Implementation of Robot Pattern for the settings Site Permissions sub menu.
32  */
33 class SettingsSubMenuSitePermissionsCommonRobot {
35     fun verifyNavigationToolBarHeader(header: String) = assertNavigationToolBarHeader(header)
37     fun verifyBlockAudioAndVideoOnMobileDataOnlyAudioAndVideoWillPlayOnWiFi() = assertBlockAudioAndVideoOnMobileDataOnlyAudioAndVideoWillPlayOnWiFi()
39     fun verifyBlockAudioOnly() = assertBlockAudioOnly()
41     fun verifyVideoAndAudioBlockedRecommended() = assertVideoAndAudioBlockedRecommended()
43     fun verifyCheckAutoPlayRadioButtonDefault() = assertCheckAutoPayRadioButtonDefault()
45     fun verifyAskToAllowButton(isChecked: Boolean = true) =
46         onView(withId(R.id.ask_to_allow_radio))
47             .check((matches(isDisplayed()))).assertIsChecked(isChecked)
49     fun verifyBlockedButton(isChecked: Boolean = false) =
50         onView(withId(R.id.block_radio))
51             .check((matches(isDisplayed()))).assertIsChecked(isChecked)
53     fun verifyBlockedByAndroid() = assertBlockedByAndroid()
55     fun verifyUnblockedByAndroid() = assertUnblockedByAndroid()
57     fun verifyToAllowIt() = assertToAllowIt()
59     fun verifyGotoAndroidSettings() = assertGotoAndroidSettings()
61     fun verifyTapPermissions() = assertTapPermissions()
63     fun verifyToggleNameToON(name: String) = assertToggleNameToON(name)
65     fun verifyGoToSettingsButton() = assertGoToSettingsButton()
67     fun verifySitePermissionsAutoPlaySubMenuItems() {
68         verifyBlockAudioAndVideoOnMobileDataOnlyAudioAndVideoWillPlayOnWiFi()
69         verifyBlockAudioOnly()
70         verifyVideoAndAudioBlockedRecommended()
71         verifyCheckAutoPlayRadioButtonDefault()
72     }
74     fun verifySitePermissionsCommonSubMenuItems() {
75         verifyAskToAllowButton()
76         verifyBlockedButton()
77     }
79     fun verifyBlockedByAndroidSection() {
80         verifyBlockedByAndroid()
81         verifyToAllowIt()
82         verifyGotoAndroidSettings()
83         verifyTapPermissions()
84         verifyGoToSettingsButton()
85     }
87     fun verifyNotificationSubMenuItems() {
88         verifyNotificationToolbar()
89         verifySitePermissionsCommonSubMenuItems()
90     }
92     fun verifySitePermissionsPersistentStorageSubMenuItems() {
93         verifyAskToAllowButton()
94         verifyBlockedButton()
95     }
97     fun verifyDRMControlledContentSubMenuItems() {
98         verifyAskToAllowButton()
99         verifyBlockedButton()
100         // Third option is "Allowed"
101         thirdRadioButton().check(matches(withText("Allowed")))
102     }
104     fun clickGoToSettingsButton() {
105         goToSettingsButton().click()
106         mDevice.findObject(UiSelector().resourceId("com.android.settings:id/list"))
107             .waitForExists(waitingTime)
108     }
110     fun openAppSystemPermissionsSettings() {
111         mDevice.findObject(UiSelector().textContains("Permissions")).click()
112     }
114     fun switchAppPermissionSystemSetting(permissionCategory: String, permission: String) {
115         mDevice.findObject(UiSelector().textContains(permissionCategory)).click()
117         if (permission == "Allow") {
118             mDevice.findObject(UiSelector().textContains("Allow")).click()
119         } else {
120             mDevice.findObject(UiSelector().textContains("Deny")).click()
121         }
122     }
124     fun goBackToSystemAppPermissionSettings() {
125         mDevice.pressBack()
126         mDevice.waitForIdle(waitingTime)
127     }
129     fun goBackToPermissionsSettingsSubMenu() {
130         while (!permissionSettingMenu().waitForExists(waitingTimeShort)) {
131             mDevice.pressBack()
132             mDevice.waitForIdle(waitingTime)
133         }
134     }
136     fun verifySystemGrantedPermission(permissionCategory: String) {
137         assertUIObjectExists(
138             itemWithClassName("android.widget.RelativeLayout")
139                 .getChild(
140                     UiSelector()
141                         .resourceId("android:id/title")
142                         .textContains(permissionCategory),
143                 ),
144             itemWithClassName("android.widget.RelativeLayout")
145                 .getChild(
146                     UiSelector()
147                         .resourceId("android:id/summary")
148                         .textContains("Only while app is in use"),
149                 ),
150         )
151     }
153     fun verifyNotificationToolbar() =
154         assertUIObjectExists(
155             itemContainingText(getStringResource(R.string.preference_phone_feature_notification)),
156             itemWithDescription(getStringResource(R.string.action_bar_up_description)),
157         )
159     fun selectAutoplayOption(text: String) {
160         when (text) {
161             "Allow audio and video" -> askToAllowRadioButton().click()
162             "Block audio and video on cellular data only" -> blockRadioButton().click()
163             "Block audio only" -> thirdRadioButton().click()
164             "Block audio and video" -> fourthRadioButton().click()
165         }
166     }
168     fun selectPermissionSettingOption(text: String) {
169         when (text) {
170             "Ask to allow" -> askToAllowRadioButton().click()
171             "Blocked" -> blockRadioButton().click()
172         }
173     }
175     fun selectDRMControlledContentPermissionSettingOption(text: String) {
176         when (text) {
177             "Ask to allow" -> askToAllowRadioButton().click()
178             "Blocked" -> blockRadioButton().click()
179             "Allowed" -> thirdRadioButton().click()
180         }
181     }
183     class Transition {
184         fun goBack(interact: SettingsSubMenuSitePermissionsRobot.() -> Unit): SettingsSubMenuSitePermissionsRobot.Transition {
185             goBackButton().click()
187             SettingsSubMenuSitePermissionsRobot().interact()
188             return SettingsSubMenuSitePermissionsRobot.Transition()
189         }
190     }
193 // common Blocked radio button for all settings
194 private fun blockRadioButton() = onView(withId(R.id.block_radio))
196 // common Ask to Allow radio button for all settings
197 private fun askToAllowRadioButton() = onView(withId(R.id.ask_to_allow_radio))
199 // common extra 3rd radio button for all settings
200 private fun thirdRadioButton() = onView(withId(R.id.third_radio))
202 // common extra 4th radio button for all settings
203 private fun fourthRadioButton() = onView(withId(R.id.fourth_radio))
205 private fun assertNavigationToolBarHeader(header: String) = onView(allOf(withContentDescription(header)))
207 private fun assertBlockAudioAndVideoOnMobileDataOnlyAudioAndVideoWillPlayOnWiFi() =
208     blockRadioButton().check((matches(withEffectiveVisibility(Visibility.VISIBLE))))
210 private fun assertBlockAudioOnly() =
211     thirdRadioButton().check((matches(withEffectiveVisibility(Visibility.VISIBLE))))
213 private fun assertVideoAndAudioBlockedRecommended() = onView(withId(R.id.fourth_radio))
214     .check((matches(withEffectiveVisibility(Visibility.VISIBLE))))
216 private fun assertCheckAutoPayRadioButtonDefault() {
217     // Allow audio and video
218     askToAllowRadioButton()
219         .assertIsChecked(isChecked = false)
221     // Block audio and video on cellular data only
222     blockRadioButton()
223         .assertIsChecked(isChecked = false)
225     // Block audio only (default)
226     thirdRadioButton()
227         .assertIsChecked(isChecked = true)
229     // Block audio and video
230     fourthRadioButton()
231         .assertIsChecked(isChecked = false)
234 private fun assertBlockedByAndroid() {
235     blockedByAndroidContainer().waitForExists(waitingTime)
236     assertUIObjectExists(itemContainingText(getStringResource(R.string.phone_feature_blocked_by_android)))
239 private fun assertUnblockedByAndroid() {
240     blockedByAndroidContainer().waitUntilGone(waitingTime)
241     assertUIObjectExists(itemContainingText(getStringResource(R.string.phone_feature_blocked_by_android)), exists = false)
244 private fun blockedByAndroidContainer() = mDevice.findObject(UiSelector().resourceId("$packageName:id/permissions_blocked_container"))
246 private fun permissionSettingMenu() = mDevice.findObject(UiSelector().resourceId("$packageName:id/container"))
248 private fun assertToAllowIt() = onView(withText(R.string.phone_feature_blocked_intro))
249     .check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
251 private fun assertGotoAndroidSettings() = onView(withText(R.string.phone_feature_blocked_step_settings))
252     .check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
254 private fun assertTapPermissions() = onView(withText("2. Tap Permissions"))
255     .check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
257 private fun assertToggleNameToON(name: String) = onView(withText(name))
258     .check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
260 private fun assertGoToSettingsButton() =
261     goToSettingsButton().check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
263 private fun goBackButton() =
264     onView(allOf(withContentDescription("Navigate up")))
266 private fun goToSettingsButton() = onView(withId(R.id.settings_button))