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
31 * Implementation of Robot Pattern for the settings Site Permissions sub menu.
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()
74 fun verifySitePermissionsCommonSubMenuItems() {
75 verifyAskToAllowButton()
79 fun verifyBlockedByAndroidSection() {
80 verifyBlockedByAndroid()
82 verifyGotoAndroidSettings()
83 verifyTapPermissions()
84 verifyGoToSettingsButton()
87 fun verifyNotificationSubMenuItems() {
88 verifyNotificationToolbar()
89 verifySitePermissionsCommonSubMenuItems()
92 fun verifySitePermissionsPersistentStorageSubMenuItems() {
93 verifyAskToAllowButton()
97 fun verifyDRMControlledContentSubMenuItems() {
98 verifyAskToAllowButton()
100 // Third option is "Allowed"
101 thirdRadioButton().check(matches(withText("Allowed")))
104 fun clickGoToSettingsButton() {
105 goToSettingsButton().click()
106 mDevice.findObject(UiSelector().resourceId("com.android.settings:id/list"))
107 .waitForExists(waitingTime)
110 fun openAppSystemPermissionsSettings() {
111 mDevice.findObject(UiSelector().textContains("Permissions")).click()
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()
120 mDevice.findObject(UiSelector().textContains("Deny")).click()
124 fun goBackToSystemAppPermissionSettings() {
126 mDevice.waitForIdle(waitingTime)
129 fun goBackToPermissionsSettingsSubMenu() {
130 while (!permissionSettingMenu().waitForExists(waitingTimeShort)) {
132 mDevice.waitForIdle(waitingTime)
136 fun verifySystemGrantedPermission(permissionCategory: String) {
137 assertUIObjectExists(
138 itemWithClassName("android.widget.RelativeLayout")
141 .resourceId("android:id/title")
142 .textContains(permissionCategory),
144 itemWithClassName("android.widget.RelativeLayout")
147 .resourceId("android:id/summary")
148 .textContains("Only while app is in use"),
153 fun verifyNotificationToolbar() =
154 assertUIObjectExists(
155 itemContainingText(getStringResource(R.string.preference_phone_feature_notification)),
156 itemWithDescription(getStringResource(R.string.action_bar_up_description)),
159 fun selectAutoplayOption(text: String) {
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()
168 fun selectPermissionSettingOption(text: String) {
170 "Ask to allow" -> askToAllowRadioButton().click()
171 "Blocked" -> blockRadioButton().click()
175 fun selectDRMControlledContentPermissionSettingOption(text: String) {
177 "Ask to allow" -> askToAllowRadioButton().click()
178 "Blocked" -> blockRadioButton().click()
179 "Allowed" -> thirdRadioButton().click()
184 fun goBack(interact: SettingsSubMenuSitePermissionsRobot.() -> Unit): SettingsSubMenuSitePermissionsRobot.Transition {
185 goBackButton().click()
187 SettingsSubMenuSitePermissionsRobot().interact()
188 return SettingsSubMenuSitePermissionsRobot.Transition()
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
223 .assertIsChecked(isChecked = false)
225 // Block audio only (default)
227 .assertIsChecked(isChecked = true)
229 // Block audio and video
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))