1
0
Fork 0
fenix/app/src/androidTest/java/org/mozilla/fenix/ui/robots/BrowserRobot.kt

471 lines
17 KiB
Kotlin
Raw Normal View History

/* 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/. */
@file:Suppress("TooManyFunctions")
package org.mozilla.fenix.ui.robots
import android.content.Context
import android.content.Intent
4281 remove qab (#6310) * For #4281: small ToolbarMenu refactor This makes it easier to see how items are ordered in the menuItems list * For 4281: add QAB buttons to menu * For 4281: removed menu back button per mocks I double checked with UX, and we'll be relying on the hardware back button for its functionality * For 4281: add content descriptions for bookmarking * For 4281: updated BrowserToolbarController for new functionality * For 4281: provided simple dependencies to browser controller More complex changes will be in a following commit, for review readability * For 4281: move toolbar controller dependencies up to BaseBrowserFragment The functionality they control is being moved into the toolbar menu, which is shared by both normal tabs and custom ones * For 4281: removed (now unused) code related to QAB * For 4281: fix test compilation after QAB removal Tests still need to be expanded to include added functionality * For 4281: updated menu to show if url is bookmarked This sloppy workaround is required because TwoStateButton requires that `isInPrimaryState` be a synchronous call, and checking whether or not the current site is bookmarked is quite slow (10-50 MS, in my tests). After days of work and many attempted solutions, this was the least abhorrent among them. https://github.com/mozilla-mobile/android-components/issues/4915 was opened against AC to evaluate potentially supporting async `isInPrimaryState` functions. https://github.com/mozilla-mobile/fenix/issues/6370 was opened against Fenix to investigate the unexpectedly slow call to `BookmarkStorage`. * For 4281: update reader mode switch * For 4281: selectively show/hide menu items * For 4281: add reader mode appearance * For 4281: update bookmark button when it is clicked * For 4281: removed unused QAB code * For 4281: removed QAB robot, updated UI tests * For 4281: removed QuickActionSheet metrics Since this behavior now lives in the toolbar, it is tracked via Event.BrowserMenuItemTapped * For 4281: fixed lint errors * For 4281: add new strings for buttons added to menu This is necessary because the location change (from QAB to toolbar menu) could affect the grammar in some languages * For 4281: remove outdated TODOs * For 4281: removed QAB container * For 4281: removed back button reference from UI test This button no longer exists * For 4821: Fixes a visual defect (extra padding on top of toolbar) * For 4281: update copy on reader mode * For 4281: fixed review nits
2019-11-12 02:10:14 +01:00
import android.net.Uri
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.Espresso.pressBack
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.intent.Intents
import androidx.test.espresso.intent.matcher.BundleMatchers
import androidx.test.espresso.intent.matcher.IntentMatchers
import androidx.test.espresso.matcher.RootMatchers.isDialog
import androidx.test.espresso.matcher.ViewMatchers
import androidx.test.espresso.matcher.ViewMatchers.Visibility
import androidx.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withEffectiveVisibility
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withResourceName
import androidx.test.espresso.matcher.ViewMatchers.withText
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
2020-03-10 10:44:21 +01:00
import androidx.test.uiautomator.By.text
import androidx.test.uiautomator.UiDevice
2020-03-10 10:44:21 +01:00
import androidx.test.uiautomator.UiSelector
import androidx.test.uiautomator.Until
import androidx.test.uiautomator.Until.hasObject
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.CoreMatchers.containsString
import org.junit.Assert.assertTrue
import org.mozilla.fenix.R
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.helpers.Constants.LONG_CLICK_DURATION
import org.mozilla.fenix.helpers.TestAssetHelper
2020-03-10 10:44:21 +01:00
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime
import org.mozilla.fenix.helpers.click
import org.mozilla.fenix.helpers.ext.waitNotNull
class BrowserRobot {
fun verifyCurrentPrivateSession(context: Context) {
val session = context.components.core.sessionManager.selectedSession
assertTrue("Current session is private", session?.private!!)
}
fun verifyUrl(url: String) {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
mDevice.waitNotNull(
Until.findObject(By.res("org.mozilla.fenix.debug:id/mozac_browser_toolbar_url_view")),
2020-03-10 10:44:21 +01:00
waitingTime
)
TestAssetHelper.waitingTime
onView(withId(R.id.mozac_browser_toolbar_url_view))
.check(matches(withText(containsString(url.replace("http://", "")))))
}
fun verifyHelpUrl() {
2020-03-20 21:48:54 +01:00
verifyUrl("support.mozilla.org/")
}
fun verifyWhatsNewURL() {
2020-03-20 21:48:54 +01:00
verifyUrl("support.mozilla.org/")
}
fun verifyRateOnGooglePlayURL() {
2020-03-20 21:48:54 +01:00
verifyUrl("play.google.com/store/apps/details?id=org.mozilla.fenix")
}
/* Asserts that the text within DOM element with ID="testContent" has the given text, i.e.
* document.querySelector('#testContent').innerText == expectedText
*
*/
2020-07-08 11:59:53 +02:00
fun verifyPageContent(expectedText: String) {
mDevice.waitNotNull(
Until.findObject(By.res("org.mozilla.fenix.debug:id/engineView")),
waitingTime
)
2020-07-08 11:59:53 +02:00
assertTrue(mDevice.findObject(UiSelector().text(expectedText)).waitForExists(waitingTime))
}
fun verifyTabCounter(expectedText: String) {
onView(withId(R.id.counter_text))
.check((matches(withText(containsString(expectedText)))))
2019-09-14 02:30:53 +02:00
}
fun verifySnackBarText(expectedText: String) {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
2020-03-10 10:44:21 +01:00
mDevice.waitNotNull(Until.findObject(text(expectedText)), waitingTime)
onView(withText(expectedText)).check(
matches(isCompletelyDisplayed())
)
}
fun verifyLinkContextMenuItems(containsURL: Uri) {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
mDevice.waitNotNull(
Until.findObject(By.textContains(containsURL.toString())),
2020-03-10 10:44:21 +01:00
waitingTime
)
mDevice.waitNotNull(
2020-03-10 10:44:21 +01:00
Until.findObject(text("Open link in new tab")),
waitingTime
)
mDevice.waitNotNull(
2020-03-10 10:44:21 +01:00
Until.findObject(text("Open link in private tab")),
waitingTime
)
2020-03-10 10:44:21 +01:00
mDevice.waitNotNull(Until.findObject(text("Copy link")), waitingTime)
mDevice.waitNotNull(Until.findObject(text("Share link")), waitingTime)
}
fun verifyLinkImageContextMenuItems(containsURL: Uri) {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
mDevice.waitNotNull(Until.findObject(By.textContains(containsURL.toString())))
mDevice.waitNotNull(
2020-03-10 10:44:21 +01:00
Until.findObject(text("Open link in new tab")), waitingTime
)
mDevice.waitNotNull(
2020-03-10 10:44:21 +01:00
Until.findObject(text("Open link in private tab")), waitingTime
)
2020-03-10 10:44:21 +01:00
mDevice.waitNotNull(Until.findObject(text("Copy link")), waitingTime)
mDevice.waitNotNull(Until.findObject(text("Share link")), waitingTime)
mDevice.waitNotNull(
2020-03-10 10:44:21 +01:00
Until.findObject(text("Open image in new tab")), waitingTime
)
2020-03-10 10:44:21 +01:00
mDevice.waitNotNull(Until.findObject(text("Save image")), waitingTime)
mDevice.waitNotNull(
2020-03-10 10:44:21 +01:00
Until.findObject(text("Copy image location")), waitingTime
)
}
fun verifyNavURLBar() = assertNavURLBar()
fun verifySecureConnectionLockIcon() = assertSecureConnectionLockIcon()
fun verifyEnhancedTrackingProtectionSwitch() = assertEnhancedTrackingProtectionSwitch()
fun clickEnhancedTrackingProtectionSwitchOffOn() =
onView(withResourceName("switch_widget")).click()
fun verifyProtectionSettingsButton() = assertProtectionSettingsButton()
fun verifyEnhancedTrackingOptions() {
clickEnhancedTrackingProtectionPanel()
verifyEnhancedTrackingProtectionSwitch()
verifyProtectionSettingsButton()
}
fun verifyMenuButton() = assertMenuButton()
fun verifyBlueDot() = assertBlueDot()
fun verifyNavURLBarItems() {
verifyEnhancedTrackingOptions()
pressBack()
waitingTime
verifySecureConnectionLockIcon()
verifyTabCounter("1")
verifyNavURLBar()
verifyMenuButton()
}
fun verifyNoLinkImageContextMenuItems(containsURL: Uri) {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
mDevice.waitNotNull(Until.findObject(By.textContains(containsURL.toString())))
mDevice.waitNotNull(
2020-03-10 10:44:21 +01:00
Until.findObject(text("Open image in new tab")),
waitingTime
)
2020-03-10 10:44:21 +01:00
mDevice.waitNotNull(Until.findObject(text("Save image")), waitingTime)
mDevice.waitNotNull(
2020-03-10 10:44:21 +01:00
Until.findObject(text("Copy image location")), waitingTime
)
}
fun dismissContentContextMenu(containsURL: Uri) {
onView(withText(containsURL.toString()))
.inRoot(isDialog())
.check(matches(isDisplayed()))
.perform(ViewActions.pressBack())
}
fun clickEnhancedTrackingProtectionPanel() = enhancedTrackingProtectionPanel().click()
fun verifyEnhancedTrackingProtectionPanelNotVisible() =
assertEnhancedTrackingProtectionPanelNotVisible()
fun clickContextOpenLinkInNewTab() {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
mDevice.waitNotNull(
2020-03-10 10:44:21 +01:00
Until.findObject(text("Open link in new tab")),
waitingTime
)
2020-03-10 10:44:21 +01:00
val menuOpenInNewTab = mDevice.findObject(text("Open link in new tab"))
menuOpenInNewTab.click()
}
fun clickContextOpenLinkInPrivateTab() {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
mDevice.waitNotNull(
2020-03-10 10:44:21 +01:00
Until.findObject(text("Open link in private tab")),
waitingTime
)
2020-03-10 10:44:21 +01:00
val menuOpenInPrivateTab = mDevice.findObject(text("Open link in private tab"))
menuOpenInPrivateTab.click()
}
fun clickContextCopyLink() {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
2020-03-10 10:44:21 +01:00
mDevice.waitNotNull(Until.findObject(text("Copy link")), waitingTime)
2020-03-10 10:44:21 +01:00
val menuCopyLink = mDevice.findObject(text("Copy link"))
menuCopyLink.click()
}
fun clickContextShareLink(url: Uri) {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
2020-03-10 10:44:21 +01:00
mDevice.waitNotNull(Until.findObject(text("Share link")), waitingTime)
2020-03-10 10:44:21 +01:00
val menuShareLink = mDevice.findObject(text("Share link"))
menuShareLink.click()
// verify share intent is launched and matched with associated passed in URL
Intents.intended(
allOf(
IntentMatchers.hasAction(Intent.ACTION_CHOOSER),
IntentMatchers.hasExtras(
allOf(
BundleMatchers.hasEntry(
Intent.EXTRA_INTENT,
allOf(
IntentMatchers.hasAction(Intent.ACTION_SEND),
IntentMatchers.hasType("text/plain"),
IntentMatchers.hasExtra(
Intent.EXTRA_TEXT,
url.toString()
)
)
)
)
)
)
)
}
fun clickContextCopyImageLocation() {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
mDevice.waitNotNull(
2020-03-10 10:44:21 +01:00
Until.findObject(text("Copy image location")),
waitingTime
)
2020-03-10 10:44:21 +01:00
val menuCopyImageLocation = mDevice.findObject(text("Copy image location"))
menuCopyImageLocation.click()
}
fun clickContextOpenImageNewTab() {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
mDevice.waitNotNull(
2020-03-10 10:44:21 +01:00
Until.findObject(text("Open image in new tab")),
waitingTime
)
2020-03-10 10:44:21 +01:00
val menuOpenImageNewTab = mDevice.findObject(text("Open image in new tab"))
menuOpenImageNewTab.click()
}
fun clickContextSaveImage() {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
2020-03-10 10:44:21 +01:00
mDevice.waitNotNull(Until.findObject(text("Save image")), waitingTime)
2020-03-10 10:44:21 +01:00
val menuSaveImage = mDevice.findObject(text("Save image"))
menuSaveImage.click()
}
4281 remove qab (#6310) * For #4281: small ToolbarMenu refactor This makes it easier to see how items are ordered in the menuItems list * For 4281: add QAB buttons to menu * For 4281: removed menu back button per mocks I double checked with UX, and we'll be relying on the hardware back button for its functionality * For 4281: add content descriptions for bookmarking * For 4281: updated BrowserToolbarController for new functionality * For 4281: provided simple dependencies to browser controller More complex changes will be in a following commit, for review readability * For 4281: move toolbar controller dependencies up to BaseBrowserFragment The functionality they control is being moved into the toolbar menu, which is shared by both normal tabs and custom ones * For 4281: removed (now unused) code related to QAB * For 4281: fix test compilation after QAB removal Tests still need to be expanded to include added functionality * For 4281: updated menu to show if url is bookmarked This sloppy workaround is required because TwoStateButton requires that `isInPrimaryState` be a synchronous call, and checking whether or not the current site is bookmarked is quite slow (10-50 MS, in my tests). After days of work and many attempted solutions, this was the least abhorrent among them. https://github.com/mozilla-mobile/android-components/issues/4915 was opened against AC to evaluate potentially supporting async `isInPrimaryState` functions. https://github.com/mozilla-mobile/fenix/issues/6370 was opened against Fenix to investigate the unexpectedly slow call to `BookmarkStorage`. * For 4281: update reader mode switch * For 4281: selectively show/hide menu items * For 4281: add reader mode appearance * For 4281: update bookmark button when it is clicked * For 4281: removed unused QAB code * For 4281: removed QAB robot, updated UI tests * For 4281: removed QuickActionSheet metrics Since this behavior now lives in the toolbar, it is tracked via Event.BrowserMenuItemTapped * For 4281: fixed lint errors * For 4281: add new strings for buttons added to menu This is necessary because the location change (from QAB to toolbar menu) could affect the grammar in some languages * For 4281: remove outdated TODOs * For 4281: removed QAB container * For 4281: removed back button reference from UI test This button no longer exists * For 4821: Fixes a visual defect (extra padding on top of toolbar) * For 4281: update copy on reader mode * For 4281: fixed review nits
2019-11-12 02:10:14 +01:00
fun createBookmark(url: Uri) {
navigationToolbar {
}.enterURLAndEnterToBrowser(url) {
}.openThreeDotMenu {
clickAddBookmarkButton()
}
}
fun clickLinkMatchingText(expectedText: String) {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
2020-03-10 10:44:21 +01:00
mDevice.waitNotNull(Until.findObject(text(expectedText)), waitingTime)
2020-03-10 10:44:21 +01:00
val element = mDevice.findObject(text(expectedText))
element.click()
}
fun longClickMatchingText(expectedText: String) {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
2020-03-10 10:44:21 +01:00
mDevice.waitNotNull(Until.findObject(text(expectedText)), waitingTime)
2020-03-10 10:44:21 +01:00
val element = mDevice.findObject(text(expectedText))
element.click(LONG_CLICK_DURATION)
}
fun snackBarButtonClick(expectedText: String) {
onView(allOf(withId(R.id.snackbar_btn), withText(expectedText))).check(
matches(withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE))
).perform(ViewActions.click())
}
fun verifySaveLoginPromptIsShown() {
2020-03-10 10:44:21 +01:00
mDevice.waitNotNull(Until.findObjects(text("test@example.com")), waitingTime)
val submitButton = mDevice.findObject(By.res("submit"))
2020-03-10 10:44:21 +01:00
submitButton.clickAndWait(Until.newWindow(), waitingTime)
// Click save to save the login
2020-03-10 10:44:21 +01:00
mDevice.waitNotNull(Until.findObjects(text("Save")))
}
fun saveLoginFromPrompt(optionToSaveLogin: String) {
2020-03-10 10:44:21 +01:00
mDevice.findObject(text(optionToSaveLogin)).click()
}
fun clickMediaPlayerPlayButton() {
mDevice.waitNotNull(
hasObject(
By
.clazz("android.widget.Button")
.textContains("Play")
),
waitingTime
)
mediaPlayerPlayButton().click()
}
fun waitForPlaybackToStart() {
2020-07-08 11:59:53 +02:00
val playStateMessage = mDevice.findObject(UiSelector().text("Media file is playing"))
assertTrue(playStateMessage.waitForExists(waitingTime))
}
fun verifyMediaIsPaused() {
2020-07-08 11:59:53 +02:00
val pausedStateMessage = mDevice.findObject(UiSelector().text("Media file is paused"))
assertTrue(pausedStateMessage.waitForExists(waitingTime))
}
class Transition {
private val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
private fun threeDotButton() = onView(
2020-03-10 10:44:21 +01:00
allOf(
ViewMatchers.withContentDescription(
"Menu"
)
)
)
fun openThreeDotMenu(interact: ThreeDotMenuMainRobot.() -> Unit): ThreeDotMenuMainRobot.Transition {
mDevice.waitForIdle(waitingTime)
threeDotButton().perform(ViewActions.click())
ThreeDotMenuMainRobot().interact()
return ThreeDotMenuMainRobot.Transition()
}
fun openNavigationToolbar(interact: NavigationToolbarRobot.() -> Unit): NavigationToolbarRobot.Transition {
mDevice.waitForIdle(waitingTime)
navURLBar().click()
NavigationToolbarRobot().interact()
return NavigationToolbarRobot.Transition()
}
fun openTabDrawer(interact: TabDrawerRobot.() -> Unit): TabDrawerRobot.Transition {
mDevice.waitForIdle(waitingTime)
tabsCounter().click()
mDevice.waitNotNull(
Until.findObject(By.res("org.mozilla.fenix.debug:id/tab_layout")),
2020-03-10 10:44:21 +01:00
waitingTime
)
TabDrawerRobot().interact()
return TabDrawerRobot.Transition()
}
2020-03-10 10:44:21 +01:00
fun openTabButtonShortcutsMenu(interact: NavigationToolbarRobot.() -> Unit): NavigationToolbarRobot.Transition {
mDevice.waitForIdle(waitingTime)
tabsCounter().perform(
ViewActions.longClick()
)
NavigationToolbarRobot().interact()
return NavigationToolbarRobot.Transition()
}
fun openNotificationShade(interact: NotificationRobot.() -> Unit): NotificationRobot.Transition {
mDevice.openNotification()
2020-03-10 10:44:21 +01:00
NotificationRobot().interact()
return NotificationRobot.Transition()
2020-03-10 10:44:21 +01:00
}
}
}
fun browserScreen(interact: BrowserRobot.() -> Unit): BrowserRobot.Transition {
BrowserRobot().interact()
return BrowserRobot.Transition()
}
private fun dismissOnboardingButton() = onView(withId(R.id.close_onboarding))
fun dismissTrackingOnboarding() {
2020-03-10 10:44:21 +01:00
mDevice.wait(Until.findObject(By.res("close_onboarding")), waitingTime)
dismissOnboardingButton().click()
}
2019-09-14 02:30:53 +02:00
fun navURLBar() = onView(withId(R.id.mozac_browser_toolbar_url_view))
private fun assertNavURLBar() = navURLBar()
.check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
fun enhancedTrackingProtectionPanel() =
onView(withId(R.id.mozac_browser_toolbar_tracking_protection_indicator))
private fun assertEnhancedTrackingProtectionPanelNotVisible() {
enhancedTrackingProtectionPanel()
.check(matches(withEffectiveVisibility(Visibility.GONE)))
}
private fun assertEnhancedTrackingProtectionSwitch() {
withText(R.id.trackingProtectionSwitch)
.matches(withEffectiveVisibility(Visibility.VISIBLE))
}
private fun assertProtectionSettingsButton() {
onView(withId(R.id.protection_settings))
.check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}
private fun assertSecureConnectionLockIcon() {
onView(withId(R.id.mozac_browser_toolbar_security_indicator))
.check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}
private fun menuButton() = onView(withId(R.id.icon))
private fun assertMenuButton() {
menuButton()
.check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}
private fun tabsCounter() = onView(withId(R.id.counter_box))
2020-03-10 10:44:21 +01:00
private fun mediaPlayerPlayButton() =
mDevice.findObject(
By
.clazz("android.widget.Button")
.textContains("Play")
)
private fun assertBlueDot() {
onView(withId(R.id.notification_dot))
.check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}
private fun addOnsReportSiteIssue() = onView(withText("Report Site Issue"))