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

Commit

Permalink
For #15624 & #17484: improves tab drawer locator & avoids homescreen …
Browse files Browse the repository at this point in the history
…refreshing
  • Loading branch information
Oana Horvath committed Oct 29, 2021
1 parent da10c06 commit 1af80cf
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.mozilla.fenix.helpers

import android.content.Context
import androidx.test.platform.app.InstrumentationRegistry
import org.mozilla.fenix.ext.settings

class FeatureSettingsHelper {
private val context: Context = InstrumentationRegistry.getInstrumentation().targetContext
private val settings = context.settings()

// saving default values of feature flags
private var isPocketEnabled: Boolean = settings.showPocketRecommendationsFeature
private var isJumpBackInCFREnabled: Boolean = settings.shouldShowJumpBackInCFR
private var isRecentTabsFeatureEnabled: Boolean = settings.showRecentTabsFeature

fun setPocketEnabled(enabled: Boolean) {
settings.showPocketRecommendationsFeature = enabled
}

fun setJumpBackCFREnabled(enabled: Boolean) {
settings.shouldShowJumpBackInCFR = enabled
}

fun setRecentTabsFeatureEnabled(enabled: Boolean) {
settings.showRecentTabsFeature = enabled
}

// Important:
// Use this after each test if you have modified these feature settings
// to make sure the app goes back to the default state
fun resetAllFeatureFlags() {
settings.showPocketRecommendationsFeature = isPocketEnabled
settings.shouldShowJumpBackInCFR = isJumpBackInCFREnabled
settings.showRecentTabsFeature = isRecentTabsFeatureEnabled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import android.net.Uri
import android.os.Build
import android.os.Environment
import androidx.browser.customtabs.CustomTabsIntent
import androidx.preference.PreferenceManager
import androidx.test.espresso.Espresso
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.IdlingRegistry
Expand All @@ -33,6 +32,7 @@ import androidx.test.uiautomator.UiObject
import androidx.test.uiautomator.UiScrollable
import androidx.test.uiautomator.UiSelector
import androidx.test.uiautomator.Until
import java.io.File
import kotlinx.coroutines.runBlocking
import mozilla.components.support.ktx.android.content.appName
import org.hamcrest.CoreMatchers
Expand All @@ -43,7 +43,6 @@ import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime
import org.mozilla.fenix.helpers.ext.waitNotNull
import org.mozilla.fenix.helpers.idlingresource.NetworkConnectionIdlingResource
import org.mozilla.fenix.ui.robots.mDevice
import java.io.File

object TestHelper {

Expand Down Expand Up @@ -71,13 +70,6 @@ object TestHelper {
).perform(longClick())
}

fun setPreference(context: Context, pref: String, value: Int) {
val preferences = PreferenceManager.getDefaultSharedPreferences(context)
val editor = preferences.edit()
editor.putInt(pref, value)
editor.apply()
}

fun restartApp(activity: HomeActivityIntentTestRule) {
with(activity) {
finishActivity()
Expand Down
29 changes: 9 additions & 20 deletions app/src/androidTest/java/org/mozilla/fenix/ui/SmokeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.helpers.AndroidAssetDispatcher
import org.mozilla.fenix.helpers.Constants.PackageName.YOUTUBE_APP
import org.mozilla.fenix.helpers.FeatureSettingsHelper
import org.mozilla.fenix.helpers.HomeActivityIntentTestRule
import org.mozilla.fenix.helpers.RecyclerViewIdlingResource
import org.mozilla.fenix.helpers.TestAssetHelper
Expand Down Expand Up @@ -85,6 +86,8 @@ class SmokeTest {

private lateinit var browserStore: BrowserStore

private val featureSettingsHelper = FeatureSettingsHelper()

@get:Rule
val activityTestRule = AndroidComposeTestRule(
HomeActivityIntentTestRule(),
Expand All @@ -108,7 +111,9 @@ class SmokeTest {
// So we are initializing this here instead of in all related tests.
browserStore = activityTestRule.activity.components.core.store

activityTestRule.activity.applicationContext.settings().shouldShowJumpBackInCFR = false
// disabling the new homepage pop-up that interferes with the tests.
featureSettingsHelper.setJumpBackCFREnabled(false)

mockWebServer = MockWebServer().apply {
dispatcher = AndroidAssetDispatcher()
start()
Expand Down Expand Up @@ -144,6 +149,9 @@ class SmokeTest {
if (localeListIdlingResource != null) {
IdlingRegistry.getInstance().unregister(localeListIdlingResource)
}

// resetting modified features enabled setting to default
featureSettingsHelper.resetAllFeatureFlags()
}

// Verifies the first run onboarding screen
Expand Down Expand Up @@ -311,9 +319,6 @@ class SmokeTest {
@Test
// Verifies the Add to top sites option in a tab's 3 dot menu
fun openMainMenuAddTopSiteTest() {
val settings = activityTestRule.activity.applicationContext.settings()
settings.shouldShowJumpBackInCFR = false

val defaultWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)

navigationToolbar {
Expand Down Expand Up @@ -820,8 +825,6 @@ class SmokeTest {
@Test
@Ignore("https://github.com/mozilla-mobile/fenix/issues/21397")
fun createFirstCollectionTest() {
val settings = activityTestRule.activity.applicationContext.settings()
settings.shouldShowJumpBackInCFR = false
val firstWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)
val secondWebPage = TestAssetHelper.getGenericAsset(mockWebServer, 2)

Expand Down Expand Up @@ -854,8 +857,6 @@ class SmokeTest {
@Test
@Ignore("https://github.com/mozilla-mobile/fenix/issues/21397")
fun verifyExpandedCollectionItemsTest() {
val settings = activityTestRule.activity.applicationContext.settings()
settings.shouldShowJumpBackInCFR = false
val webPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)

navigationToolbar {
Expand Down Expand Up @@ -908,9 +909,6 @@ class SmokeTest {

@Test
fun shareCollectionTest() {
val settings = activityTestRule.activity.applicationContext.settings()
settings.shouldShowJumpBackInCFR = false

val webPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)

navigationToolbar {
Expand All @@ -934,8 +932,6 @@ class SmokeTest {
// Test running on beta/release builds in CI:
// caution when making changes to it, so they don't block the builds
fun deleteCollectionTest() {
val settings = activityTestRule.activity.applicationContext.settings()
settings.shouldShowJumpBackInCFR = false
val webPage = TestAssetHelper.getGenericAsset(mockWebServer, 1)

navigationToolbar {
Expand Down Expand Up @@ -1353,8 +1349,6 @@ class SmokeTest {

@Test
fun goToHomeScreenBottomToolbarTest() {
val settings = activityTestRule.activity.applicationContext.settings()
settings.shouldShowJumpBackInCFR = false
val genericURL = TestAssetHelper.getGenericAsset(mockWebServer, 1)

navigationToolbar {
Expand All @@ -1367,9 +1361,6 @@ class SmokeTest {

@Test
fun goToHomeScreenTopToolbarTest() {
val settings = activityTestRule.activity.applicationContext.settings()
settings.shouldShowJumpBackInCFR = false

val genericURL = TestAssetHelper.getGenericAsset(mockWebServer, 1)

homeScreen {
Expand Down Expand Up @@ -1437,8 +1428,6 @@ class SmokeTest {

@Test
fun startOnHomepageTest() {
val settings = activityTestRule.activity.applicationContext.settings()
settings.shouldShowJumpBackInCFR = false
val genericURL = TestAssetHelper.getGenericAsset(mockWebServer, 1)

navigationToolbar {
Expand Down
91 changes: 60 additions & 31 deletions app/src/androidTest/java/org/mozilla/fenix/ui/TabbedBrowsingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.helpers.AndroidAssetDispatcher
import org.mozilla.fenix.helpers.FeatureSettingsHelper
import org.mozilla.fenix.helpers.HomeActivityTestRule
import org.mozilla.fenix.helpers.TestAssetHelper
import org.mozilla.fenix.ui.robots.browserScreen
Expand Down Expand Up @@ -41,14 +41,17 @@ import org.mozilla.fenix.ui.robots.notificationShade
class TabbedBrowsingTest {
private val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
private lateinit var mockWebServer: MockWebServer
private val featureSettingsHelper = FeatureSettingsHelper()

/* ktlint-disable no-blank-line-before-rbrace */ // This imposes unreadable grouping.
@get:Rule
val activityTestRule = HomeActivityTestRule()

@Before
fun setUp() {
activityTestRule.activity.applicationContext.settings().shouldShowJumpBackInCFR = false
// disabling the new homepage pop-up that interferes with the tests.
featureSettingsHelper.setJumpBackCFREnabled(false)

mockWebServer = MockWebServer().apply {
dispatcher = AndroidAssetDispatcher()
start()
Expand All @@ -58,6 +61,7 @@ class TabbedBrowsingTest {
@After
fun tearDown() {
mockWebServer.shutdown()
featureSettingsHelper.resetAllFeatureFlags()
}

@Test
Expand Down Expand Up @@ -142,40 +146,53 @@ class TabbedBrowsingTest {

navigationToolbar {
}.enterURLAndEnterToBrowser(genericURL.url) {
mDevice.waitForIdle()
}.openTabDrawer {
verifyExistingOpenTabs("Test_Page_1")
closeTab()
verifySnackBarText("Tab closed")
snackBarButtonClick("UNDO")
}

mDevice.waitForIdle()

browserScreen {
homeScreen {
verifyNoTabsOpened()
}.openNavigationToolbar {
}.enterURLAndEnterToBrowser(genericURL.url) {
}.openTabDrawer {
verifyExistingOpenTabs("Test_Page_1")
swipeTabRight("Test_Page_1")
verifySnackBarText("Tab closed")
snackBarButtonClick("UNDO")
}
homeScreen {
verifyNoTabsOpened()
}.openNavigationToolbar {
}.enterURLAndEnterToBrowser(genericURL.url) {
}.openTabDrawer {
verifyExistingOpenTabs("Test_Page_1")
swipeTabLeft("Test_Page_1")
}
homeScreen {
verifyNoTabsOpened()
}
}

mDevice.waitForIdle()
@Test
fun verifyUndoSnackBarTest() {
// disabling these features because they interfere with the snackbar visibility
featureSettingsHelper.setPocketEnabled(false)
featureSettingsHelper.setRecentTabsFeatureEnabled(false)

browserScreen {
val genericURL = TestAssetHelper.getGenericAsset(mockWebServer, 1)

navigationToolbar {
}.enterURLAndEnterToBrowser(genericURL.url) {
}.openTabDrawer {
verifyExistingOpenTabs("Test_Page_1")
swipeTabLeft("Test_Page_1")
closeTab()
verifySnackBarText("Tab closed")
snackBarButtonClick("UNDO")
}

mDevice.waitForIdle()

browserScreen {
verifyTabCounter("1")
}.openTabDrawer {
verifyExistingOpenTabs("Test_Page_1")
}.closeTabDrawer { }
}
}

@Test
Expand All @@ -185,40 +202,52 @@ class TabbedBrowsingTest {
homeScreen { }.togglePrivateBrowsingMode()
navigationToolbar {
}.enterURLAndEnterToBrowser(genericURL.url) {
mDevice.waitForIdle()
}.openTabDrawer {
verifyExistingOpenTabs("Test_Page_1")
verifyCloseTabsButton("Test_Page_1")
closeTab()
verifySnackBarText("Private tab closed")
snackBarButtonClick("UNDO")
}

mDevice.waitForIdle()

browserScreen {
homeScreen {
verifyNoTabsOpened()
}.openNavigationToolbar {
}.enterURLAndEnterToBrowser(genericURL.url) {
}.openTabDrawer {
verifyExistingOpenTabs("Test_Page_1")
swipeTabRight("Test_Page_1")
verifySnackBarText("Private tab closed")
snackBarButtonClick("UNDO")
}
homeScreen {
verifyNoTabsOpened()
}.openNavigationToolbar {
}.enterURLAndEnterToBrowser(genericURL.url) {
}.openTabDrawer {
verifyExistingOpenTabs("Test_Page_1")
swipeTabLeft("Test_Page_1")
}
homeScreen {
verifyNoTabsOpened()
}
}

mDevice.waitForIdle()
@Test
fun verifyPrivateTabUndoSnackBarTest() {
val genericURL = TestAssetHelper.getGenericAsset(mockWebServer, 1)

browserScreen {
homeScreen { }.togglePrivateBrowsingMode()
navigationToolbar {
}.enterURLAndEnterToBrowser(genericURL.url) {
}.openTabDrawer {
verifyExistingOpenTabs("Test_Page_1")
swipeTabLeft("Test_Page_1")
verifyCloseTabsButton("Test_Page_1")
closeTab()
verifySnackBarText("Private tab closed")
snackBarButtonClick("UNDO")
}

mDevice.waitForIdle()

browserScreen {
verifyTabCounter("1")
}.openTabDrawer {
verifyExistingOpenTabs("Test_Page_1")
verifyPrivateModeSelected()
}
}

Expand Down
Loading

0 comments on commit 1af80cf

Please sign in to comment.