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

Commit

Permalink
For #21002: Adds UI test for Addons in private mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Oana Horvath committed Feb 11, 2022
1 parent 72f0191 commit f8001b4
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ object TestAssetHelper {

fun getEnhancedTrackingProtectionAsset(server: MockWebServer): TestAsset {
val url = server.url("pages/trackingPage.html").toString().toUri()!!
val content = "Level 1 (Basic) List"

return TestAsset(url, "", "")
return TestAsset(url, content, "")
}

fun getImageAsset(server: MockWebServer): TestAsset {
Expand Down
102 changes: 77 additions & 25 deletions app/src/androidTest/java/org/mozilla/fenix/ui/SettingsAddonsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mozilla.fenix.R
import org.mozilla.fenix.customannotations.SmokeTest
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.helpers.AndroidAssetDispatcher
import org.mozilla.fenix.helpers.HomeActivityTestRule
import org.mozilla.fenix.helpers.RecyclerViewIdlingResource
import org.mozilla.fenix.helpers.TestAssetHelper
import org.mozilla.fenix.helpers.ViewVisibilityIdlingResource
import org.mozilla.fenix.ui.robots.addonsMenu
import org.mozilla.fenix.ui.robots.homeScreen
import org.mozilla.fenix.ui.robots.navigationToolbar

/**
* Tests for verifying the functionality of installing or removing addons
*
*/

class SettingsAddonsTest {
/* ktlint-disable no-blank-line-before-rbrace */ // This imposes unreadable grouping.

private lateinit var mockWebServer: MockWebServer
private var addonsListIdlingResource: RecyclerViewIdlingResource? = null
private var addonContainerIdlingResource: ViewVisibilityIdlingResource? = null
Expand Down Expand Up @@ -72,14 +72,12 @@ class SettingsAddonsTest {
}
}

// Opens a webpage and installs an add-on from the three-dot menu
// Installs an add-on from the Add-ons menu and verifies the prompts
@Test
fun installAddonFromThreeDotMenu() {
val defaultWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
fun installAddonTest() {
val addonName = "uBlock Origin"

navigationToolbar {}
.enterURLAndEnterToBrowser(defaultWebPage.url) {}
homeScreen {}
.openThreeDotMenu {}
.openAddonsManagerMenu {
addonsListIdlingResource =
Expand All @@ -89,32 +87,24 @@ class SettingsAddonsTest {
)
IdlingRegistry.getInstance().register(addonsListIdlingResource!!)
clickInstallAddon(addonName)
verifyAddonPrompt(addonName)
verifyAddonPermissionPrompt(addonName)
cancelInstallAddon()
clickInstallAddon(addonName)
acceptInstallAddon()

verifyDownloadAddonPrompt(addonName, activityTestRule)
acceptPermissionToInstallAddon()
closeAddonInstallCompletePrompt(addonName, activityTestRule)
verifyAddonIsInstalled(addonName)
verifyEnabledTitleDisplayed()
}
}

// Opens the addons settings menu, installs an addon, then uninstalls
// Installs an addon, then uninstalls it
@Test
fun verifyAddonsCanBeUninstalled() {
val addonName = "uBlock Origin"

homeScreen {
}.openThreeDotMenu {
}.openSettings {
verifyAdvancedHeading()
verifyAddons()
}.openAddonsManagerMenu {
addonsListIdlingResource =
RecyclerViewIdlingResource(activityTestRule.activity.findViewById(R.id.add_ons_list), 1)
IdlingRegistry.getInstance().register(addonsListIdlingResource!!)
clickInstallAddon(addonName)
acceptInstallAddon()
verifyDownloadAddonPrompt(addonName, activityTestRule)
addonsMenu {
installAddon(addonName)
closeAddonInstallCompletePrompt(addonName, activityTestRule)
IdlingRegistry.getInstance().unregister(addonsListIdlingResource!!)
}.openDetailedMenuForAddon(addonName) {
addonContainerIdlingResource = ViewVisibilityIdlingResource(
Expand All @@ -123,6 +113,68 @@ class SettingsAddonsTest {
)
IdlingRegistry.getInstance().register(addonContainerIdlingResource!!)
}.removeAddon {
verifyAddonCanBeInstalled(addonName)
}
}

@SmokeTest
@Test
// Installs uBlock add-on and checks that the app doesn't crash while loading pages with trackers
fun noCrashWithAddonInstalledTest() {
// setting ETP to Strict mode to test it works with add-ons
activityTestRule.activity.settings().setStrictETP()

val addonName = "uBlock Origin"
val trackingProtectionPage =
TestAssetHelper.getEnhancedTrackingProtectionAsset(mockWebServer)

addonsMenu {
installAddon(addonName)
closeAddonInstallCompletePrompt(addonName, activityTestRule)
IdlingRegistry.getInstance().unregister(addonsListIdlingResource!!)
}.goBack {
}.openNavigationToolbar {
}.enterURLAndEnterToBrowser(trackingProtectionPage.url) {
verifyPageContent(trackingProtectionPage.content)
}
}

@SmokeTest
@Test
fun useAddonsInPrivateModeTest() {
val addonName = "uBlock Origin"
val trackingPage = TestAssetHelper.getEnhancedTrackingProtectionAsset(mockWebServer)

homeScreen {
}.togglePrivateBrowsingMode()
addonsMenu {
installAddon(addonName)
selectAllowInPrivateBrowsing(activityTestRule)
closeAddonInstallCompletePrompt(addonName, activityTestRule)
IdlingRegistry.getInstance().unregister(addonsListIdlingResource!!)
}.goBack {}
navigationToolbar {
}.enterURLAndEnterToBrowser(trackingPage.url) {
verifyPageContent(trackingPage.content)
}.openThreeDotMenu {
openAddonsSubList()
verifyAddonAvailableInMainMenu(addonName)
}
}

private fun installAddon(addonName: String) {
homeScreen {
}.openThreeDotMenu {
}.openAddonsManagerMenu {
addonsListIdlingResource =
RecyclerViewIdlingResource(
activityTestRule.activity.findViewById(R.id.add_ons_list),
1
)
IdlingRegistry.getInstance().register(addonsListIdlingResource!!)
clickInstallAddon(addonName)
verifyAddonPermissionPrompt(addonName)
acceptPermissionToInstallAddon()
}
}
}
51 changes: 20 additions & 31 deletions app/src/androidTest/java/org/mozilla/fenix/ui/SmokeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import org.mozilla.fenix.IntentReceiverActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.customannotations.SmokeTest
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.helpers.AndroidAssetDispatcher
import org.mozilla.fenix.helpers.Constants
import org.mozilla.fenix.helpers.FeatureSettingsHelper
Expand Down Expand Up @@ -262,6 +261,26 @@ class SmokeTest {
}
}

@Test
// Verifies the Add-ons menu opens from a tab's 3 dot menu
fun openMainMenuAddonsTest() {
val defaultWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)

navigationToolbar {
}.enterURLAndEnterToBrowser(defaultWebPage.url) {
}.openThreeDotMenu {
}.openAddonsManagerMenu {
addonsListIdlingResource =
RecyclerViewIdlingResource(
activityTestRule.activity.findViewById(R.id.add_ons_list),
1
)
IdlingRegistry.getInstance().register(addonsListIdlingResource!!)
verifyAddonsItems()
IdlingRegistry.getInstance().unregister(addonsListIdlingResource!!)
}
}

@Test
// Verifies the Synced tabs menu or Sync Sign In menu opens from a tab's 3 dot menu.
// The test is assuming we are NOT signed in.
Expand Down Expand Up @@ -565,36 +584,6 @@ class SmokeTest {
}
}

@Test
// Installs uBlock add-on and checks that the app doesn't crash while loading pages with trackers
fun noCrashWithAddonInstalledTest() {
// setting ETP to Strict mode to test it works with add-ons
activityTestRule.activity.settings().setStrictETP()

val addonName = "uBlock Origin"
val trackingProtectionPage =
TestAssetHelper.getEnhancedTrackingProtectionAsset(mockWebServer)

homeScreen {
}.openThreeDotMenu {
}.openAddonsManagerMenu {
addonsListIdlingResource =
RecyclerViewIdlingResource(
activityTestRule.activity.findViewById(R.id.add_ons_list),
1
)
IdlingRegistry.getInstance().register(addonsListIdlingResource!!)
clickInstallAddon(addonName)
acceptInstallAddon()
verifyDownloadAddonPrompt(addonName, activityTestRule.activityRule)
IdlingRegistry.getInstance().unregister(addonsListIdlingResource!!)
}.goBack {
}.openNavigationToolbar {
}.enterURLAndEnterToBrowser(trackingProtectionPage.url) {
verifyPageContent(trackingProtectionPage.content)
}
}

@Test
// Verifies that a recently closed item is properly opened
fun openRecentlyClosedItemTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import org.mozilla.fenix.R
import org.mozilla.fenix.helpers.IdlingResourceHelper.registerAddonInstallingIdlingResource
import org.mozilla.fenix.helpers.IdlingResourceHelper.unregisterAddonInstallingIdlingResource
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime
import org.mozilla.fenix.helpers.TestHelper
import org.mozilla.fenix.helpers.TestHelper.appName
import org.mozilla.fenix.helpers.TestHelper.scrollToElementByText
import org.mozilla.fenix.helpers.click
import org.mozilla.fenix.helpers.ext.waitNotNull

Expand All @@ -43,30 +43,46 @@ import org.mozilla.fenix.helpers.ext.waitNotNull
*/

class SettingsSubMenuAddonsManagerRobot {
fun verifyAddonPrompt(addonName: String) = assertAddonPrompt(addonName)
fun verifyAddonPermissionPrompt(addonName: String) = assertAddonPermissionPrompt(addonName)

fun clickInstallAddon(addonName: String) = selectInstallAddon(addonName)

fun verifyDownloadAddonPrompt(
fun closeAddonInstallCompletePrompt(
addonName: String,
activityTestRule: ActivityTestRule<HomeActivity>
) {
try {
assertDownloadingAddonPrompt(addonName, activityTestRule)
assertAddonInstallCompletePrompt(addonName, activityTestRule)
} catch (e: IdlingResourceTimeoutException) {
if (mDevice.findObject(UiSelector().text("Failed to install $addonName")).exists()) {
clickInstallAddon(addonName)
acceptInstallAddon()
assertDownloadingAddonPrompt(addonName, activityTestRule)
acceptPermissionToInstallAddon()
assertAddonInstallCompletePrompt(addonName, activityTestRule)
}
}
}

fun verifyAddonIsInstalled(addonName: String) {
scrollToElementByText(addonName)
assertAddonIsInstalled(addonName)
}

fun verifyEnabledTitleDisplayed() {
onView(withText("Enabled"))
.check(matches(isCompletelyDisplayed()))
}

fun cancelInstallAddon() = cancelInstall()
fun acceptInstallAddon() = allowInstall()
fun acceptPermissionToInstallAddon() = allowPermissionToInstall()
fun verifyAddonsItems() = assertAddonsItems()
fun verifyAddonCanBeInstalled(addonName: String) = assertAddonCanBeInstalled(addonName)

fun selectAllowInPrivateBrowsing(activityTestRule: ActivityTestRule<HomeActivity>) {
registerAddonInstallingIdlingResource(activityTestRule)
onView(withId(R.id.allow_in_private_browsing)).click()
unregisterAddonInstallingIdlingResource(activityTestRule)
}

class Transition {
fun goBack(interact: HomeScreenRobot.() -> Unit): HomeScreenRobot.Transition {
fun goBackButton() = onView(allOf(withContentDescription("Navigate up")))
Expand All @@ -80,7 +96,7 @@ class SettingsSubMenuAddonsManagerRobot {
addonName: String,
interact: SettingsSubMenuAddonsManagerAddonDetailedMenuRobot.() -> Unit
): SettingsSubMenuAddonsManagerAddonDetailedMenuRobot.Transition {
addonName.chars()
scrollToElementByText(addonName)

onView(
allOf(
Expand Down Expand Up @@ -125,7 +141,7 @@ class SettingsSubMenuAddonsManagerRobot {
.check(matches(not(isCompletelyDisplayed())))
}

private fun assertAddonPrompt(addonName: String) {
private fun assertAddonPermissionPrompt(addonName: String) {
onView(allOf(withId(R.id.title), withText("Add $addonName?")))
.check(matches(isCompletelyDisplayed()))

Expand All @@ -144,7 +160,7 @@ class SettingsSubMenuAddonsManagerRobot {
.check(matches(isCompletelyDisplayed()))
}

private fun assertDownloadingAddonPrompt(
private fun assertAddonInstallCompletePrompt(
addonName: String,
activityTestRule: ActivityTestRule<HomeActivity>
) {
Expand All @@ -163,10 +179,6 @@ class SettingsSubMenuAddonsManagerRobot {
.perform(click())

unregisterAddonInstallingIdlingResource(activityTestRule)

TestHelper.scrollToElementByText(addonName)

assertAddonIsInstalled(addonName)
}

private fun assertAddonIsInstalled(addonName: String) {
Expand All @@ -185,7 +197,7 @@ class SettingsSubMenuAddonsManagerRobot {
.perform(click())
}

private fun allowInstall() {
private fun allowPermissionToInstall() {
onView(allOf(withId(R.id.allow_button), withText("Add")))
.check(matches(isCompletelyDisplayed()))
.perform(click())
Expand All @@ -201,11 +213,6 @@ class SettingsSubMenuAddonsManagerRobot {
.check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}

private fun assertEnabledTitleDisplayed() {
onView(withText("Enabled"))
.check(matches(isCompletelyDisplayed()))
}

private fun assertAddons() {
assertAddonUblock()
}
Expand All @@ -231,6 +238,7 @@ class SettingsSubMenuAddonsManagerRobot {
}

private fun assertAddonCanBeInstalled(addonName: String) {
scrollToElementByText(addonName)
device.waitNotNull(Until.findObject(By.text(addonName)), waitingTime)

onView(
Expand All @@ -248,3 +256,8 @@ class SettingsSubMenuAddonsManagerRobot {
).check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
}
}

fun addonsMenu(interact: SettingsSubMenuAddonsManagerRobot.() -> Unit): SettingsSubMenuAddonsManagerRobot.Transition {
SettingsSubMenuAddonsManagerRobot().interact()
return SettingsSubMenuAddonsManagerRobot.Transition()
}
Loading

0 comments on commit f8001b4

Please sign in to comment.