Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Closes #6172 - Convert waitNotNull into extension function for UI tes…
Browse files Browse the repository at this point in the history
…ts (#6220)
  • Loading branch information
AaronMT committed Oct 23, 2019
1 parent c31adf2 commit 23c98f9
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* 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.helpers.ext

import androidx.test.uiautomator.SearchCondition
import androidx.test.uiautomator.UiDevice
import org.junit.Assert.assertNotNull
import org.mozilla.fenix.helpers.TestAssetHelper

/**
* Blocks the test for [waitTime] miliseconds before continuing.
*
* Will cause the test to fail is the condition is not met before the timeout.
*/
fun UiDevice.waitNotNull(
searchCondition: SearchCondition<*>,
waitTime: Long = TestAssetHelper.waitingTimeShort
) = assertNotNull(wait(searchCondition, waitTime))
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import androidx.test.uiautomator.Until
import org.hamcrest.Matchers.allOf
import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.TestAssetHelper
import org.junit.Assert.assertNotNull
import org.mozilla.fenix.helpers.ext.waitNotNull

@Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS")
class SyncIntegrationTest {
Expand Down Expand Up @@ -111,7 +111,7 @@ class SyncIntegrationTest {
}

fun tapOnSignIn() {
assertNotNull(mDevice.wait(Until.findObjects(By.text("Sign in")), TestAssetHelper.waitingTimeShort))
mDevice.waitNotNull(Until.findObjects(By.text("Sign in")))
// Let's tap on enter, sometimes depending on the device the sign in button is
// hidden by the keyboard
mDevice.pressEnter()
Expand All @@ -133,7 +133,7 @@ class SyncIntegrationTest {
}

fun seeBookmark() {
assertNotNull(mDevice.wait(Until.findObjects(By.text("Bookmark")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObjects(By.text("Bookmark")), TestAssetHelper.waitingTime)
val bookmarkButton = mDevice.findObject(By.text("Bookmark"))
bookmarkButton.click()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
package org.mozilla.fenix.ui

import androidx.test.uiautomator.UiDevice
import org.junit.Assert.assertNotNull
import org.junit.Rule
import org.junit.Test
import org.junit.Ignore
import org.mozilla.fenix.helpers.HomeActivityTestRule
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Until
import org.mozilla.fenix.helpers.ext.waitNotNull
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime
import org.mozilla.fenix.ui.robots.PRIVATE_SESSION_MESSAGE
import org.mozilla.fenix.ui.robots.homeScreen
Expand Down Expand Up @@ -122,7 +122,7 @@ class HomeScreenTest {
homeScreen {
// To deal with the race condition where multiple "add tab" buttons are present,
// we need to wait until previous HomeFragment View objects are gone.
assertNotNull(mDevice.wait(Until.gone(By.text(PRIVATE_SESSION_MESSAGE)), waitingTime))
mDevice.waitNotNull(Until.gone(By.text(PRIVATE_SESSION_MESSAGE)), waitingTime)
verifyHomeScreen()
verifyNavigationToolbar()
verifyHomePrivateBrowsingButton()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.Until
import okhttp3.mockwebserver.MockWebServer
import org.junit.Assert.assertNotNull
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mozilla.fenix.helpers.AndroidAssetDispatcher
import org.mozilla.fenix.helpers.ext.waitNotNull
import org.mozilla.fenix.helpers.HomeActivityTestRule
import org.mozilla.fenix.helpers.TestAssetHelper
import org.mozilla.fenix.ui.robots.homeScreen
Expand Down Expand Up @@ -76,7 +76,7 @@ class TabbedBrowsingTest {

homeScreen {
// Timing issue on slow devices on Firebase
assertNotNull(mDevice.wait(Until.findObjects(By.res("org.mozilla.fenix.debug:id/item_tab")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObjects(By.res("org.mozilla.fenix.debug:id/item_tab")), TestAssetHelper.waitingTime)
verifyExistingTabList()

}.openTabsListThreeDotMenu {
Expand Down Expand Up @@ -106,7 +106,7 @@ class TabbedBrowsingTest {
verifyTabCounter("1")
}.openHomeScreen {
// Timing issue on slow devices on Firebase
assertNotNull(mDevice.wait(Until.findObjects(By.res("org.mozilla.fenix.debug:id/item_tab")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObjects(By.res("org.mozilla.fenix.debug:id/item_tab")), TestAssetHelper.waitingTime)
verifyExistingTabList()
verifyShareTabsButton(true)
verifyCloseTabsButton(true)
Expand Down Expand Up @@ -135,7 +135,7 @@ class TabbedBrowsingTest {

homeScreen {
// Timing issue on slow devices on Firebase
assertNotNull(mDevice.wait(Until.findObjects(By.res("org.mozilla.fenix.debug:id/item_tab")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObjects(By.res("org.mozilla.fenix.debug:id/item_tab")), TestAssetHelper.waitingTime)
verifyExistingTabList()
}.openTabsListThreeDotMenu {
verifyCloseAllTabsButton()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.Until
import org.hamcrest.CoreMatchers.containsString
import org.junit.Assert.assertNotNull
import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.TestAssetHelper
import org.mozilla.fenix.helpers.click
import org.mozilla.fenix.helpers.ext.waitNotNull

class BrowserRobot {

fun verifyHelpUrl() {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
val redirectUrl = "https://support.mozilla.org/"
assertNotNull(mDevice.wait(Until.findObject(By.res("org.mozilla.fenix.debug:id/mozac_browser_toolbar_url_view")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObject(By.res("org.mozilla.fenix.debug:id/mozac_browser_toolbar_url_view")), TestAssetHelper.waitingTime)
onView(withId(R.id.mozac_browser_toolbar_url_view))
.check(matches(withText(containsString(redirectUrl))))
}

fun verifyWhatsNewURL() {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
val redirectUrl = "https://support.mozilla.org/"
assertNotNull(mDevice.wait(Until.findObject(By.res("org.mozilla.fenix.debug:id/mozac_browser_toolbar_url_view")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObject(By.res("org.mozilla.fenix.debug:id/mozac_browser_toolbar_url_view")), TestAssetHelper.waitingTime)
onView(withId(R.id.mozac_browser_toolbar_url_view))
.check(matches(withText(containsString(redirectUrl))))
}
Expand All @@ -43,7 +43,7 @@ class BrowserRobot {
*/
fun verifyPageContent(expectedText: String) {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
assertNotNull(mDevice.wait(Until.findObject(By.text(expectedText)), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObject(By.text(expectedText)), TestAssetHelper.waitingTime)
}

fun verifyTabCounter(expectedText: String) {
Expand All @@ -67,19 +67,19 @@ class BrowserRobot {

tabsCounter().click()

assertNotNull(mDevice.wait(
mDevice.waitNotNull(
Until.findObject(By.res("org.mozilla.fenix.debug:id/header_text")),
TestAssetHelper.waitingTime
))
)

HomeScreenRobot().interact()
return HomeScreenRobot.Transition()
}

fun openQuickActionBar(interact: QuickActionBarRobot.() -> Unit): QuickActionBarRobot.Transition {
assertNotNull(mDevice.wait(Until.gone(By.res("org.mozilla.fenix.debug:id/quick_action_sheet")),
mDevice.waitNotNull(Until.gone(By.res("org.mozilla.fenix.debug:id/quick_action_sheet")),
TestAssetHelper.waitingTime
))
)
quickActionBarHandle().click()

QuickActionBarRobot().interact()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.Until
import org.junit.Assert.assertNotNull
import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime
import org.mozilla.fenix.helpers.click
import org.mozilla.fenix.helpers.ext.waitNotNull

/**
* Implementation of Robot Pattern for the find in page UI.
Expand All @@ -34,23 +34,23 @@ class FindInPageRobot {
fun verifyFindInPageCloseButton() = assertFindInPageCloseButton()!!

fun enterFindInPageQuery(expectedText: String) {
assertNotNull(mDevice.wait(Until.findObject(By.res("org.mozilla.fenix.debug:id/find_in_page_query_text")), waitingTime))
mDevice.waitNotNull(Until.findObject(By.res("org.mozilla.fenix.debug:id/find_in_page_query_text")), waitingTime)
findInPageQuery().perform(clearText())
assertNotNull(mDevice.wait(Until.gone(By.res("org.mozilla.fenix.debug:id/find_in_page_result_text")), waitingTime))
mDevice.waitNotNull(Until.gone(By.res("org.mozilla.fenix.debug:id/find_in_page_result_text")), waitingTime)
findInPageQuery().perform(typeText(expectedText))
assertNotNull(mDevice.wait(Until.findObject(By.res("org.mozilla.fenix.debug:id/find_in_page_result_text")), waitingTime))
mDevice.waitNotNull(Until.findObject(By.res("org.mozilla.fenix.debug:id/find_in_page_result_text")), waitingTime)
}

fun verifyFindNextInPageResult(ratioCounter: String) {
assertNotNull(mDevice.wait(Until.findObject(By.text(ratioCounter)), waitingTime))
mDevice.waitNotNull(Until.findObject(By.text(ratioCounter)), waitingTime)
val element = mDevice.findObject(By.text(ratioCounter))
findInPageResult().check(matches(withText((ratioCounter))))
findInPageNextButton().click()
element.wait(Until.textNotEquals(ratioCounter), waitingTime)
}

fun verifyFindPrevInPageResult(ratioCounter: String) {
assertNotNull(mDevice.wait(Until.findObject(By.text(ratioCounter)), waitingTime))
mDevice.waitNotNull(Until.findObject(By.text(ratioCounter)), waitingTime)
val element = mDevice.findObject(By.text(ratioCounter))
findInPageResult().check(matches(withText((ratioCounter))))
findInPagePrevButton().click()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.uiautomator.By
import androidx.test.uiautomator.Until
import org.hamcrest.CoreMatchers.allOf
import org.junit.Assert.assertNotNull
import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.TestAssetHelper
import org.mozilla.fenix.helpers.click
import org.mozilla.fenix.helpers.ext.waitNotNull

/**
* Implementation of Robot Pattern for the your library menu.
Expand Down Expand Up @@ -52,15 +52,15 @@ class LibraryRobot {
}

fun openBookmarks(interact: BookmarksRobot.() -> Unit): BookmarksRobot.Transition {
assertNotNull(mDevice.wait(Until.findObject(By.text("Bookmarks")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObject(By.text("Bookmarks")), TestAssetHelper.waitingTime)
bookmarksButton().click()

BookmarksRobot().interact()
return BookmarksRobot.Transition()
}

fun openHistory(interact: HistoryRobot.() -> Unit): HistoryRobot.Transition {
assertNotNull(mDevice.wait(Until.findObject(By.text("History")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObject(By.text("History")), TestAssetHelper.waitingTime)
historyButton().click()

HistoryRobot().interact()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import androidx.test.uiautomator.Until
import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime
import org.mozilla.fenix.helpers.click
import org.junit.Assert.assertNotNull
import org.mozilla.fenix.helpers.ext.waitNotNull

/**
* Implementation of Robot Pattern for the URL toolbar.
Expand All @@ -29,7 +29,7 @@ class NavigationToolbarRobot {
val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

fun enterURLAndEnterToBrowser(url: Uri, interact: BrowserRobot.() -> Unit): BrowserRobot.Transition {
assertNotNull(mDevice.wait(Until.findObject(By.res("org.mozilla.fenix.debug:id/toolbar")), waitingTime))
mDevice.waitNotNull(Until.findObject(By.res("org.mozilla.fenix.debug:id/toolbar")), waitingTime)
urlBar().click()
awesomeBar().perform(replaceText(url.toString()), pressImeActionButton())

Expand All @@ -38,15 +38,15 @@ class NavigationToolbarRobot {
}

fun openThreeDotMenu(interact: ThreeDotMenuMainRobot.() -> Unit): ThreeDotMenuMainRobot.Transition {
assertNotNull(mDevice.wait(Until.findObject(By.descContains("Menu")), waitingTime))
mDevice.waitNotNull(Until.findObject(By.descContains("Menu")), waitingTime)
threeDotButton().click()

ThreeDotMenuMainRobot().interact()
return ThreeDotMenuMainRobot.Transition()
}

fun openNewTabAndEnterToBrowser(url: Uri, interact: BrowserRobot.() -> Unit): BrowserRobot.Transition {
assertNotNull(mDevice.wait(Until.findObject(By.descContains("Add tab")), waitingTime))
mDevice.waitNotNull(Until.findObject(By.descContains("Add tab")), waitingTime)
newTab().click()
awesomeBar().perform(replaceText(url.toString()), pressImeActionButton())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import androidx.test.uiautomator.Until
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.CoreMatchers.startsWith
import org.hamcrest.Matchers
import org.junit.Assert.assertNotNull
import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.TestAssetHelper
import org.mozilla.fenix.helpers.ext.waitNotNull

/**
* Implementation of Robot Pattern for the search fragment.
Expand Down Expand Up @@ -65,7 +65,7 @@ class SearchRobot {
}

fun clickDuckDuckGoResult() {
assertNotNull(mDevice.wait(Until.findObjects(By.text("DuckDuckGo")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObjects(By.text("DuckDuckGo")), TestAssetHelper.waitingTime)
awesomeBar().perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(0, click()))
}

Expand Down Expand Up @@ -101,29 +101,29 @@ private fun awesomeBar() = onView(withId(R.id.awesomeBar))
private fun browserToolbarEditView() = onView(Matchers.allOf(withId(R.id.mozac_browser_toolbar_edit_url_view)))

private fun duckDuckGoEngineButton(): ViewInteraction {
assertNotNull(mDevice.wait(Until.findObject(By.text("DuckDuckGo")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObject(By.text("DuckDuckGo")), TestAssetHelper.waitingTime)
return onView(Matchers.allOf(withText("DuckDuckGo")))
}

private fun denyPermissionButton(): UiObject {
assertNotNull(mDevice.wait(Until.findObjects(By.text("Deny")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObjects(By.text("Deny")), TestAssetHelper.waitingTime)
return mDevice.findObject(UiSelector().text("Deny"))
}

private fun allowPermissionButton(): UiObject {
assertNotNull(mDevice.wait(Until.findObjects(By.text("Allow")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObjects(By.text("Allow")), TestAssetHelper.waitingTime)
return mDevice.findObject(UiSelector().text("Allow"))
}

private fun scanButton(): ViewInteraction {
assertNotNull(mDevice.wait(Until.findObject(By.res("org.mozilla.fenix.debug:id/search_scan_button")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObject(By.res("org.mozilla.fenix.debug:id/search_scan_button")), TestAssetHelper.waitingTime)
return onView(allOf(withId(R.id.searchScanButton)))
}

private fun clearButton() = onView(withId(R.id.mozac_browser_toolbar_clear_view))

private fun assertDuckDuckGoURL() {
assertNotNull(mDevice.wait(Until.findObject(By.textContains("https://duckduckgo.com/?q=mozilla")), TestAssetHelper.waitingTime))
mDevice.waitNotNull(Until.findObject(By.textContains("https://duckduckgo.com/?q=mozilla")), TestAssetHelper.waitingTime)
onView(allOf(withText(startsWith("https://duckduckgo.com"))))
.check(matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
}
Expand Down
Loading

0 comments on commit 23c98f9

Please sign in to comment.