diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/helpers/AppAndSystemHelper.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/helpers/AppAndSystemHelper.kt index f9b95a2dc478..70ff9aceef61 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/helpers/AppAndSystemHelper.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/helpers/AppAndSystemHelper.kt @@ -34,6 +34,9 @@ import androidx.test.uiautomator.UiSelector import androidx.test.uiautomator.Until import junit.framework.AssertionFailedError import kotlinx.coroutines.runBlocking +import mozilla.appservices.places.BookmarkRoot +import mozilla.components.browser.storage.sync.PlacesBookmarksStorage +import mozilla.components.browser.storage.sync.PlacesHistoryStorage import org.junit.Assert import org.junit.Assert.assertEquals import org.mozilla.fenix.Config @@ -43,6 +46,7 @@ import org.mozilla.fenix.helpers.Constants.PackageName.PIXEL_LAUNCHER import org.mozilla.fenix.helpers.Constants.PackageName.YOUTUBE_APP import org.mozilla.fenix.helpers.Constants.TAG import org.mozilla.fenix.helpers.TestAssetHelper.waitingTimeShort +import org.mozilla.fenix.helpers.TestHelper.appContext import org.mozilla.fenix.helpers.TestHelper.mDevice import org.mozilla.fenix.helpers.ext.waitNotNull import org.mozilla.fenix.helpers.idlingresource.NetworkConnectionIdlingResource @@ -70,7 +74,7 @@ object AppAndSystemHelper { fun deleteDownloadedFileOnStorage(fileName: String) { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) { val storageManager: StorageManager? = - TestHelper.appContext.getSystemService(Context.STORAGE_SERVICE) as StorageManager? + appContext.getSystemService(Context.STORAGE_SERVICE) as StorageManager? val storageVolumes = storageManager!!.storageVolumes val storageVolume: StorageVolume = storageVolumes[0] val file = File(storageVolume.directory!!.path + "/Download/" + fileName) @@ -110,7 +114,7 @@ object AppAndSystemHelper { if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) { Log.i(TAG, "clearDownloadsFolder: API > 29") val storageManager: StorageManager? = - TestHelper.appContext.getSystemService(Context.STORAGE_SERVICE) as StorageManager? + appContext.getSystemService(Context.STORAGE_SERVICE) as StorageManager? val storageVolumes = storageManager!!.storageVolumes val storageVolume: StorageVolume = storageVolumes[0] val downloadsFolder = File(storageVolume.directory!!.path + "/Download/") @@ -121,16 +125,26 @@ object AppAndSystemHelper { val files = downloadsFolder.listFiles() // Check if the folder is not empty + // If you run this method before a test, files.isNotEmpty() will always return false. if (files != null && files.isNotEmpty()) { Log.i( TAG, - "clearDownloadsFolder: Verified that \"DOWNLOADS\" folder is not empty", + "clearDownloadsFolder: Before cleanup: Downloads storage contains: ${files.size} file(s)", ) // Delete all files in the folder for (file in files) { file.delete() - Log.i(TAG, "clearDownloadsFolder: Deleted $file from \"DOWNLOADS\" folder") + Log.i( + TAG, + "clearDownloadsFolder: Deleted $file from \"DOWNLOADS\" folder." + + " Downloads storage contains ${files.size} file(s): $file", + ) } + } else { + Log.i( + TAG, + "clearDownloadsFolder: Downloads storage is empty.", + ) } } } else { @@ -139,6 +153,7 @@ object AppAndSystemHelper { Log.i(TAG, "clearDownloadsFolder: Verifying if any download files exist.") Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) .listFiles()?.forEach { + Log.i(TAG, "clearDownloadsFolder: Downloads storage contains: $it.") it.delete() Log.i(TAG, "clearDownloadsFolder: Download file $it deleted.") } @@ -146,6 +161,34 @@ object AppAndSystemHelper { } } + suspend fun deleteHistoryStorage() { + val historyStorage = PlacesHistoryStorage(appContext.applicationContext) + Log.i( + TAG, + "Before cleanup: History storage contains: ${historyStorage.getVisited()}", + ) + if (historyStorage.getVisited().isNotEmpty()) { + historyStorage.deleteEverything() + Log.i( + TAG, + "After cleanup: History storage contains: ${historyStorage.getVisited()}", + ) + } + } + + suspend fun deleteBookmarksStorage() { + val bookmarksStorage = PlacesBookmarksStorage(appContext.applicationContext) + val bookmarks = bookmarksStorage.getTree(BookmarkRoot.Mobile.id)?.children + Log.i(TAG, "Before cleanup: Bookmarks storage contains: $bookmarks") + if (bookmarks?.isNotEmpty() == true) { + bookmarks.forEach { + bookmarksStorage.deleteNode(it.guid) + // TODO: Follow-up with a method to handle the DB update; the logs will still show the bookmarks in the storage before the test starts. + Log.i(TAG, "After cleanup: Bookmarks storage contains: $bookmarks") + } + } + } + fun setNetworkEnabled(enabled: Boolean) { val networkDisconnectedIdlingResource = NetworkConnectionIdlingResource(false) val networkConnectedIdlingResource = NetworkConnectionIdlingResource(true) @@ -226,7 +269,7 @@ object AppAndSystemHelper { * @return Boolean value that helps us know if the current activity supports custom tabs or PWAs. */ fun isExternalAppBrowserActivityInCurrentTask(): Boolean { - val activityManager = TestHelper.appContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + val activityManager = appContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager mDevice.waitForIdle(TestAssetHelper.waitingTimeShort) diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/helpers/RetryTestRule.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/helpers/RetryTestRule.kt index 12b10268db27..3b22266c92b4 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/helpers/RetryTestRule.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/helpers/RetryTestRule.kt @@ -19,6 +19,7 @@ import org.mozilla.fenix.helpers.AppAndSystemHelper.setNetworkEnabled import org.mozilla.fenix.helpers.Constants.TAG import org.mozilla.fenix.helpers.IdlingResourceHelper.unregisterAllIdlingResources import org.mozilla.fenix.helpers.TestHelper.appContext +import org.mozilla.fenix.helpers.TestHelper.exitMenu /** * Rule to retry flaky tests for a given number of times, catching some of the more common exceptions. @@ -39,6 +40,7 @@ class RetryTestRule(private val retryCount: Int = 5) : TestRule { base.evaluate() break } catch (t: AssertionError) { + exitMenu() setNetworkEnabled(true) unregisterAllIdlingResources() runBlocking { @@ -51,6 +53,7 @@ class RetryTestRule(private val retryCount: Int = 5) : TestRule { } } catch (t: AssertionFailedError) { unregisterAllIdlingResources() + exitMenu() runBlocking { appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true permissionStorage.deleteAllSitePermissions() @@ -62,6 +65,7 @@ class RetryTestRule(private val retryCount: Int = 5) : TestRule { } catch (t: UiObjectNotFoundException) { setNetworkEnabled(true) unregisterAllIdlingResources() + exitMenu() runBlocking { appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true permissionStorage.deleteAllSitePermissions() @@ -73,6 +77,7 @@ class RetryTestRule(private val retryCount: Int = 5) : TestRule { } catch (t: NoMatchingViewException) { setNetworkEnabled(true) unregisterAllIdlingResources() + exitMenu() runBlocking { appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true permissionStorage.deleteAllSitePermissions() @@ -84,6 +89,7 @@ class RetryTestRule(private val retryCount: Int = 5) : TestRule { } catch (t: IdlingResourceTimeoutException) { setNetworkEnabled(true) unregisterAllIdlingResources() + exitMenu() runBlocking { appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true permissionStorage.deleteAllSitePermissions() @@ -95,6 +101,7 @@ class RetryTestRule(private val retryCount: Int = 5) : TestRule { } catch (t: RuntimeException) { setNetworkEnabled(true) unregisterAllIdlingResources() + exitMenu() runBlocking { appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true permissionStorage.deleteAllSitePermissions() @@ -106,6 +113,7 @@ class RetryTestRule(private val retryCount: Int = 5) : TestRule { } catch (t: NullPointerException) { setNetworkEnabled(true) unregisterAllIdlingResources() + exitMenu() runBlocking { appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true permissionStorage.deleteAllSitePermissions() diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/helpers/TestSetup.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/helpers/TestSetup.kt index b080154c4a1b..b21ceb68ea3a 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/helpers/TestSetup.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/helpers/TestSetup.kt @@ -1,22 +1,32 @@ package org.mozilla.fenix.helpers import android.util.Log +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.uiautomator.UiDevice import kotlinx.coroutines.runBlocking -import mozilla.appservices.places.BookmarkRoot -import mozilla.components.browser.storage.sync.PlacesBookmarksStorage +import mozilla.components.browser.state.store.BrowserStore import okhttp3.mockwebserver.MockWebServer +import org.junit.After import org.junit.Before +import org.mozilla.fenix.ext.components import org.mozilla.fenix.helpers.Constants.TAG import org.mozilla.fenix.helpers.TestHelper.appContext import org.mozilla.fenix.ui.robots.notificationShade open class TestSetup { lateinit var mockWebServer: MockWebServer - private val bookmarksStorage = PlacesBookmarksStorage(appContext.applicationContext) + lateinit var mDevice: UiDevice + lateinit var browserStore: BrowserStore @Before - fun setUp() { + open fun setUp() { Log.i(TAG, "TestSetup: Starting the @Before setup") + Log.i(TAG, "TestSetup: Initializing the UI device instance") + mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) + // Initializing this as part of class construction, below the rule would throw a NPE + // So we are initializing this here instead of in all related tests. + Log.i(TAG, "TestSetup: Initializing the browserStore instance") + browserStore = appContext.components.core.store // Clear pre-existing notifications notificationShade { cancelAllShownNotifications() @@ -24,18 +34,15 @@ open class TestSetup { runBlocking { // Reset locale to EN-US if needed. AppAndSystemHelper.resetSystemLocaleToEnUS() - // Check and clear the downloads folder + // Check and clear the downloads folder, in case the tearDown method is not executed. + // This will only work in case of a RetryTestRule execution. AppAndSystemHelper.clearDownloadsFolder() // Make sure the Wifi and Mobile Data connections are on AppAndSystemHelper.setNetworkEnabled(true) - // Clear bookmarks left after a failed test - val bookmarks = bookmarksStorage.getTree(BookmarkRoot.Mobile.id)?.children - Log.i(TAG, "Before cleanup: Bookmarks storage contains: $bookmarks") - bookmarks?.forEach { - bookmarksStorage.deleteNode(it.guid) - // TODO: Follow-up with a method to handle the DB update; the logs will still show the bookmarks in the storage before the test starts. - Log.i(TAG, "After cleanup: Bookmarks storage contains: $bookmarks") - } + // Clear bookmarks left after a failed test, before a retry + AppAndSystemHelper.deleteBookmarksStorage() + // Clear history left after a failed test, before a retry + AppAndSystemHelper.deleteHistoryStorage() } mockWebServer = MockWebServer().apply { dispatcher = AndroidAssetDispatcher() @@ -49,4 +56,11 @@ open class TestSetup { mockWebServer.start() } } + + @After + open fun tearDown() { + Log.i(TAG, "TestSetup: Starting the @After tearDown methods.") + // Check and clear the downloads folder. + AppAndSystemHelper.clearDownloadsFolder() + } } diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeBookmarksTest.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeBookmarksTest.kt index 26d31d920f28..263a30f932d8 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeBookmarksTest.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeBookmarksTest.kt @@ -8,18 +8,10 @@ import androidx.compose.ui.test.junit4.AndroidComposeTestRule import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu import androidx.test.espresso.Espresso.pressBack import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation -import androidx.test.uiautomator.UiDevice -import kotlinx.coroutines.runBlocking -import mozilla.appservices.places.BookmarkRoot -import okhttp3.mockwebserver.MockWebServer -import org.junit.After -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.bookmarkStorage -import org.mozilla.fenix.helpers.AndroidAssetDispatcher import org.mozilla.fenix.helpers.AppAndSystemHelper.registerAndCleanupIdlingResources import org.mozilla.fenix.helpers.HomeActivityIntentTestRule import org.mozilla.fenix.helpers.RecyclerViewIdlingResource @@ -28,7 +20,9 @@ import org.mozilla.fenix.helpers.TestAssetHelper import org.mozilla.fenix.helpers.TestHelper.clickSnackbarButton import org.mozilla.fenix.helpers.TestHelper.exitMenu import org.mozilla.fenix.helpers.TestHelper.longTapSelectItem +import org.mozilla.fenix.helpers.TestHelper.mDevice import org.mozilla.fenix.helpers.TestHelper.verifySnackBarText +import org.mozilla.fenix.helpers.TestSetup import org.mozilla.fenix.ui.robots.bookmarksMenu import org.mozilla.fenix.ui.robots.browserScreen import org.mozilla.fenix.ui.robots.homeScreen @@ -38,9 +32,7 @@ import org.mozilla.fenix.ui.robots.navigationToolbar /** * Tests for verifying basic functionality of bookmarks */ -class ComposeBookmarksTest { - private lateinit var mockWebServer: MockWebServer - private lateinit var mDevice: UiDevice +class ComposeBookmarksTest : TestSetup() { private val bookmarksFolderName = "New Folder" private val testBookmark = object { var title: String = "Bookmark title" @@ -59,26 +51,6 @@ class ComposeBookmarksTest { @JvmField val retryTestRule = RetryTestRule(3) - @Before - fun setUp() { - mDevice = UiDevice.getInstance(getInstrumentation()) - mockWebServer = MockWebServer().apply { - dispatcher = AndroidAssetDispatcher() - start() - } - } - - @After - fun tearDown() { - mockWebServer.shutdown() - // Clearing all bookmarks data after each test to avoid overlapping data - val bookmarksStorage = activityTestRule.activity?.bookmarkStorage - runBlocking { - val bookmarks = bookmarksStorage?.getTree(BookmarkRoot.Mobile.id)?.children - bookmarks?.forEach { bookmarksStorage.deleteNode(it.guid) } - } - } - // TestRail link: https://testrail.stage.mozaws.net/index.php?/cases/view/522919 @Test fun verifyEmptyBookmarksMenuTest() { diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeCollectionTest.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeCollectionTest.kt index a18366cc7453..e570ae59e604 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeCollectionTest.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeCollectionTest.kt @@ -5,19 +5,15 @@ package org.mozilla.fenix.ui import androidx.compose.ui.test.junit4.AndroidComposeTestRule -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.uiautomator.UiDevice -import okhttp3.mockwebserver.MockWebServer -import org.junit.After -import org.junit.Before import org.junit.Rule import org.junit.Test import org.mozilla.fenix.customannotations.SmokeTest -import org.mozilla.fenix.helpers.AndroidAssetDispatcher import org.mozilla.fenix.helpers.HomeActivityIntentTestRule import org.mozilla.fenix.helpers.TestAssetHelper.getGenericAsset import org.mozilla.fenix.helpers.TestHelper.clickSnackbarButton +import org.mozilla.fenix.helpers.TestHelper.mDevice import org.mozilla.fenix.helpers.TestHelper.verifySnackBarText +import org.mozilla.fenix.helpers.TestSetup import org.mozilla.fenix.ui.robots.browserScreen import org.mozilla.fenix.ui.robots.collectionRobot import org.mozilla.fenix.ui.robots.composeTabDrawer @@ -29,9 +25,7 @@ import org.mozilla.fenix.ui.robots.navigationToolbar * */ -class ComposeCollectionTest { - private lateinit var mDevice: UiDevice - private lateinit var mockWebServer: MockWebServer +class ComposeCollectionTest : TestSetup() { private val firstCollectionName = "testcollection_1" private val secondCollectionName = "testcollection_2" private val collectionName = "First Collection" @@ -51,20 +45,6 @@ class ComposeCollectionTest { ), ) { it.activity } - @Before - fun setUp() { - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) - mockWebServer = MockWebServer().apply { - dispatcher = AndroidAssetDispatcher() - start() - } - } - - @After - fun tearDown() { - mockWebServer.shutdown() - } - // TestRail link: https://testrail.stage.mozaws.net/index.php?/cases/view/353823 @SmokeTest @Test diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeContextMenusTest.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeContextMenusTest.kt index 5e554b49d0a2..0261c72d896d 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeContextMenusTest.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeContextMenusTest.kt @@ -6,15 +6,8 @@ package org.mozilla.fenix.ui import androidx.compose.ui.test.junit4.AndroidComposeTestRule import androidx.core.net.toUri -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.uiautomator.UiDevice -import okhttp3.mockwebserver.MockWebServer -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.AppAndSystemHelper.assertExternalAppOpens import org.mozilla.fenix.helpers.Constants import org.mozilla.fenix.helpers.HomeActivityIntentTestRule @@ -23,7 +16,9 @@ import org.mozilla.fenix.helpers.MatcherHelper.itemWithText import org.mozilla.fenix.helpers.RetryTestRule import org.mozilla.fenix.helpers.TestAssetHelper import org.mozilla.fenix.helpers.TestHelper.clickSnackbarButton +import org.mozilla.fenix.helpers.TestHelper.mDevice import org.mozilla.fenix.helpers.TestHelper.verifySnackBarText +import org.mozilla.fenix.helpers.TestSetup import org.mozilla.fenix.ui.robots.clickContextMenuItem import org.mozilla.fenix.ui.robots.clickPageObject import org.mozilla.fenix.ui.robots.downloadRobot @@ -45,15 +40,14 @@ import org.mozilla.fenix.ui.robots.shareOverlay * */ -class ComposeContextMenusTest { - private lateinit var mDevice: UiDevice - private lateinit var mockWebServer: MockWebServer +class ComposeContextMenusTest : TestSetup() { @get:Rule(order = 0) val composeTestRule = AndroidComposeTestRule( - HomeActivityIntentTestRule.withDefaultSettingsOverrides( + HomeActivityIntentTestRule( tabsTrayRewriteEnabled = true, + isJumpBackInCFREnabled = false, ), ) { it.activity } @@ -61,21 +55,6 @@ class ComposeContextMenusTest { @JvmField val retryTestRule = RetryTestRule(3) - @Before - fun setUp() { - composeTestRule.activity.applicationContext.settings().shouldShowJumpBackInCFR = false - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) - mockWebServer = MockWebServer().apply { - dispatcher = AndroidAssetDispatcher() - start() - } - } - - @After - fun tearDown() { - mockWebServer.shutdown() - } - // TestRail link: https://testrail.stage.mozaws.net/index.php?/cases/view/243837 @Test fun verifyOpenLinkNewTabContextMenuOptionTest() { diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHistoryTest.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHistoryTest.kt index 138ffa7c300a..ec29e465e8fa 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHistoryTest.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHistoryTest.kt @@ -4,24 +4,14 @@ package org.mozilla.fenix.ui -import android.content.Context import androidx.compose.ui.test.junit4.AndroidComposeTestRule import androidx.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu import androidx.test.espresso.Espresso.pressBack -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.uiautomator.UiDevice -import kotlinx.coroutines.runBlocking -import mozilla.components.browser.storage.sync.PlacesHistoryStorage -import okhttp3.mockwebserver.MockWebServer -import org.junit.After -import org.junit.Before import org.junit.Ignore 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.AppAndSystemHelper.registerAndCleanupIdlingResources import org.mozilla.fenix.helpers.HomeActivityIntentTestRule import org.mozilla.fenix.helpers.RecyclerViewIdlingResource @@ -29,6 +19,7 @@ import org.mozilla.fenix.helpers.TestAssetHelper import org.mozilla.fenix.helpers.TestHelper.exitMenu import org.mozilla.fenix.helpers.TestHelper.longTapSelectItem import org.mozilla.fenix.helpers.TestHelper.verifySnackBarText +import org.mozilla.fenix.helpers.TestSetup import org.mozilla.fenix.ui.robots.browserScreen import org.mozilla.fenix.ui.robots.historyMenu import org.mozilla.fenix.ui.robots.homeScreen @@ -39,42 +30,17 @@ import org.mozilla.fenix.ui.robots.navigationToolbar * Tests for verifying basic functionality of history * */ -class ComposeHistoryTest { - private lateinit var mockWebServer: MockWebServer - private lateinit var mDevice: UiDevice +class ComposeHistoryTest : TestSetup() { @get:Rule val activityTestRule = AndroidComposeTestRule( - HomeActivityIntentTestRule.withDefaultSettingsOverrides( + HomeActivityIntentTestRule( tabsTrayRewriteEnabled = true, + isJumpBackInCFREnabled = false, ), ) { it.activity } - @Before - fun setUp() { - InstrumentationRegistry.getInstrumentation().targetContext.settings() - .shouldShowJumpBackInCFR = false - - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) - mockWebServer = MockWebServer().apply { - dispatcher = AndroidAssetDispatcher() - start() - } - } - - @After - fun tearDown() { - mockWebServer.shutdown() - // Clearing all history data after each test to avoid overlapping data - val applicationContext: Context = activityTestRule.activity.applicationContext - val historyStorage = PlacesHistoryStorage(applicationContext) - - runBlocking { - historyStorage.deleteEverything() - } - } - // TestRail link: https://testrail.stage.mozaws.net/index.php?/cases/view/243285 @Test fun verifyEmptyHistoryMenuTest() { diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHomeScreenTest.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHomeScreenTest.kt index e6930672a92e..38ed0da774ea 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHomeScreenTest.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHomeScreenTest.kt @@ -5,19 +5,14 @@ package org.mozilla.fenix.ui import androidx.compose.ui.test.junit4.AndroidComposeTestRule -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.uiautomator.UiDevice -import okhttp3.mockwebserver.MockWebServer -import org.junit.After -import org.junit.Before import org.junit.Ignore import org.junit.Rule import org.junit.Test import org.mozilla.fenix.customannotations.SmokeTest -import org.mozilla.fenix.helpers.AndroidAssetDispatcher import org.mozilla.fenix.helpers.HomeActivityTestRule import org.mozilla.fenix.helpers.RetryTestRule import org.mozilla.fenix.helpers.TestAssetHelper +import org.mozilla.fenix.helpers.TestSetup import org.mozilla.fenix.ui.robots.homeScreen import org.mozilla.fenix.ui.robots.navigationToolbar @@ -28,10 +23,7 @@ import org.mozilla.fenix.ui.robots.navigationToolbar * */ -class ComposeHomeScreenTest { - private lateinit var mDevice: UiDevice - private lateinit var mockWebServer: MockWebServer - +class ComposeHomeScreenTest : TestSetup() { @get:Rule(order = 0) val activityTestRule = AndroidComposeTestRule( @@ -44,21 +36,6 @@ class ComposeHomeScreenTest { @JvmField val retryTestRule = RetryTestRule(3) - @Before - fun setUp() { - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) - - mockWebServer = MockWebServer().apply { - dispatcher = AndroidAssetDispatcher() - start() - } - } - - @After - fun tearDown() { - mockWebServer.shutdown() - } - // TestRail link: https://testrail.stage.mozaws.net/index.php?/cases/view/235396 @Ignore("Failing, see: https://bugzilla.mozilla.org/show_bug.cgi?id=1844580") @Test diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeMediaNotificationTest.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeMediaNotificationTest.kt index 63c0ad07b675..6a9749b3b1d3 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeMediaNotificationTest.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeMediaNotificationTest.kt @@ -5,23 +5,16 @@ package org.mozilla.fenix.ui import androidx.compose.ui.test.junit4.AndroidComposeTestRule -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.uiautomator.UiDevice -import mozilla.components.browser.state.store.BrowserStore import mozilla.components.concept.engine.mediasession.MediaSession -import okhttp3.mockwebserver.MockWebServer -import org.junit.After -import org.junit.Before import org.junit.Rule import org.junit.Test import org.mozilla.fenix.customannotations.SmokeTest -import org.mozilla.fenix.ext.components -import org.mozilla.fenix.helpers.AndroidAssetDispatcher import org.mozilla.fenix.helpers.HomeActivityTestRule import org.mozilla.fenix.helpers.MatcherHelper import org.mozilla.fenix.helpers.RetryTestRule import org.mozilla.fenix.helpers.TestAssetHelper import org.mozilla.fenix.helpers.TestHelper.verifySnackBarText +import org.mozilla.fenix.helpers.TestSetup import org.mozilla.fenix.ui.robots.browserScreen import org.mozilla.fenix.ui.robots.clickPageObject import org.mozilla.fenix.ui.robots.homeScreen @@ -34,11 +27,7 @@ import org.mozilla.fenix.ui.robots.notificationShade * - a media notification icon is displayed on the homescreen for the tab playing media content * Note: this test only verifies media notifications, not media itself */ -class ComposeMediaNotificationTest { - private lateinit var mockWebServer: MockWebServer - private lateinit var mDevice: UiDevice - private lateinit var browserStore: BrowserStore - +class ComposeMediaNotificationTest : TestSetup() { @get:Rule(order = 0) val composeTestRule = AndroidComposeTestRule( @@ -51,24 +40,6 @@ class ComposeMediaNotificationTest { @JvmField val retryTestRule = RetryTestRule(3) - @Before - fun setUp() { - // Initializing this as part of class construction, below the rule would throw a NPE - // So we are initializing this here instead of in all tests. - browserStore = composeTestRule.activity.components.core.store - - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) - mockWebServer = MockWebServer().apply { - dispatcher = AndroidAssetDispatcher() - start() - } - } - - @After - fun tearDown() { - mockWebServer.shutdown() - } - // TestRail link: https://testrail.stage.mozaws.net/index.php?/cases/view/1347033 @SmokeTest @Test diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeNavigationToolbarTest.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeNavigationToolbarTest.kt index ee4964197f6f..2ff866316b57 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeNavigationToolbarTest.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeNavigationToolbarTest.kt @@ -5,19 +5,13 @@ package org.mozilla.fenix.ui import androidx.compose.ui.test.junit4.AndroidComposeTestRule -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.uiautomator.UiDevice -import okhttp3.mockwebserver.MockWebServer -import org.junit.After -import org.junit.Before import org.junit.Rule import org.junit.Test import org.mozilla.fenix.customannotations.SmokeTest -import org.mozilla.fenix.helpers.AndroidAssetDispatcher -import org.mozilla.fenix.helpers.AppAndSystemHelper.resetSystemLocaleToEnUS import org.mozilla.fenix.helpers.AppAndSystemHelper.runWithSystemLocaleChanged import org.mozilla.fenix.helpers.HomeActivityTestRule import org.mozilla.fenix.helpers.TestAssetHelper +import org.mozilla.fenix.helpers.TestSetup import org.mozilla.fenix.ui.robots.navigationToolbar import java.util.Locale @@ -31,10 +25,7 @@ import java.util.Locale * - Find in page */ -class ComposeNavigationToolbarTest { - private lateinit var mDevice: UiDevice - private lateinit var mockWebServer: MockWebServer - +class ComposeNavigationToolbarTest : TestSetup() { @get:Rule val composeTestRule = AndroidComposeTestRule( @@ -43,21 +34,6 @@ class ComposeNavigationToolbarTest { ), ) { it.activity } - @Before - fun setUp() { - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) - mockWebServer = MockWebServer().apply { - dispatcher = AndroidAssetDispatcher() - start() - } - } - - @After - fun tearDown() { - mockWebServer.shutdown() - resetSystemLocaleToEnUS() - } - // TestRail link: https://testrail.stage.mozaws.net/index.php?/cases/view/987326 // Swipes the nav bar left/right to switch between tabs @SmokeTest diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSearchTest.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSearchTest.kt index 27f46d84fd63..eaa76b148196 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSearchTest.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSearchTest.kt @@ -32,6 +32,7 @@ import org.mozilla.fenix.helpers.TestAssetHelper import org.mozilla.fenix.helpers.TestHelper import org.mozilla.fenix.helpers.TestHelper.exitMenu import org.mozilla.fenix.helpers.TestHelper.verifySnackBarText +import org.mozilla.fenix.helpers.TestSetup import org.mozilla.fenix.ui.robots.clickContextMenuItem import org.mozilla.fenix.ui.robots.clickPageObject import org.mozilla.fenix.ui.robots.homeScreen @@ -50,8 +51,8 @@ import org.mozilla.fenix.ui.robots.searchScreen * */ -class ComposeSearchTest { - lateinit var searchMockServer: MockWebServer +class ComposeSearchTest : TestSetup() { + private lateinit var searchMockServer: MockWebServer private val queryString: String = "firefox" private val generalEnginesList = listOf("DuckDuckGo", "Google", "Bing") private val topicEnginesList = listOf("Amazon.com", "Wikipedia", "eBay") @@ -70,7 +71,8 @@ class ComposeSearchTest { ) { it.activity } @Before - fun setUp() { + override fun setUp() { + super.setUp() searchMockServer = MockWebServer().apply { dispatcher = SearchDispatcher() start() @@ -78,7 +80,8 @@ class ComposeSearchTest { } @After - fun tearDown() { + override fun tearDown() { + super.tearDown() searchMockServer.shutdown() } diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataOnQuitTest.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataOnQuitTest.kt index 6994cd450442..d83029eaf7bb 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataOnQuitTest.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataOnQuitTest.kt @@ -9,15 +9,10 @@ import androidx.compose.ui.test.junit4.AndroidComposeTestRule import androidx.core.net.toUri import androidx.test.espresso.Espresso.pressBack import androidx.test.rule.GrantPermissionRule -import okhttp3.mockwebserver.MockWebServer -import org.junit.After -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.helpers.AndroidAssetDispatcher -import org.mozilla.fenix.helpers.AppAndSystemHelper.clearDownloadsFolder import org.mozilla.fenix.helpers.AppAndSystemHelper.setNetworkEnabled import org.mozilla.fenix.helpers.DataGenerationHelper.getStringResource import org.mozilla.fenix.helpers.HomeActivityIntentTestRule @@ -25,8 +20,8 @@ import org.mozilla.fenix.helpers.MatcherHelper import org.mozilla.fenix.helpers.TestAssetHelper import org.mozilla.fenix.helpers.TestAssetHelper.getStorageTestAsset import org.mozilla.fenix.helpers.TestHelper.exitMenu -import org.mozilla.fenix.helpers.TestHelper.mDevice import org.mozilla.fenix.helpers.TestHelper.restartApp +import org.mozilla.fenix.helpers.TestSetup import org.mozilla.fenix.ui.robots.clickPageObject import org.mozilla.fenix.ui.robots.downloadRobot import org.mozilla.fenix.ui.robots.homeScreen @@ -37,9 +32,7 @@ import org.mozilla.fenix.ui.robots.navigationToolbar * Delete Browsing Data on quit * */ -class ComposeSettingsDeleteBrowsingDataOnQuitTest { - private lateinit var mockWebServer: MockWebServer - +class ComposeSettingsDeleteBrowsingDataOnQuitTest : TestSetup() { @get:Rule(order = 0) val composeTestRule = AndroidComposeTestRule( @@ -55,22 +48,6 @@ class ComposeSettingsDeleteBrowsingDataOnQuitTest { Manifest.permission.RECORD_AUDIO, ) - @Before - fun setUp() { - mockWebServer = MockWebServer().apply { - dispatcher = AndroidAssetDispatcher() - start() - } - } - - @After - fun tearDown() { - mockWebServer.shutdown() - - // Check and clear the downloads folder - clearDownloadsFolder() - } - // TestRail link: https://testrail.stage.mozaws.net/index.php?/cases/view/416048 @Test fun deleteBrowsingDataOnQuitSettingTest() { diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataTest.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataTest.kt index 49c809a7c25d..8a7661f79abe 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataTest.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataTest.kt @@ -5,14 +5,10 @@ package org.mozilla.fenix.ui import androidx.compose.ui.test.junit4.AndroidComposeTestRule -import okhttp3.mockwebserver.MockWebServer -import org.junit.After -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.helpers.AndroidAssetDispatcher import org.mozilla.fenix.helpers.AppAndSystemHelper.setNetworkEnabled import org.mozilla.fenix.helpers.DataGenerationHelper.getStringResource import org.mozilla.fenix.helpers.HomeActivityIntentTestRule @@ -20,8 +16,8 @@ import org.mozilla.fenix.helpers.MatcherHelper.itemWithResId import org.mozilla.fenix.helpers.TestAssetHelper import org.mozilla.fenix.helpers.TestAssetHelper.getStorageTestAsset import org.mozilla.fenix.helpers.TestHelper.exitMenu -import org.mozilla.fenix.helpers.TestHelper.mDevice import org.mozilla.fenix.helpers.TestHelper.restartApp +import org.mozilla.fenix.helpers.TestSetup import org.mozilla.fenix.ui.robots.browserScreen import org.mozilla.fenix.ui.robots.clickPageObject import org.mozilla.fenix.ui.robots.homeScreen @@ -33,9 +29,7 @@ import org.mozilla.fenix.ui.robots.settingsScreen * Delete Browsing Data */ -class ComposeSettingsDeleteBrowsingDataTest { - private lateinit var mockWebServer: MockWebServer - +class ComposeSettingsDeleteBrowsingDataTest : TestSetup() { @get:Rule val composeTestRule = AndroidComposeTestRule( @@ -45,19 +39,6 @@ class ComposeSettingsDeleteBrowsingDataTest { ), ) { it.activity } - @Before - fun setUp() { - mockWebServer = MockWebServer().apply { - dispatcher = AndroidAssetDispatcher() - start() - } - } - - @After - fun tearDown() { - mockWebServer.shutdown() - } - // TestRail link: https://testrail.stage.mozaws.net/index.php?/cases/view/937561 @Test fun deleteBrowsingDataOptionStatesTest() { diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTabbedBrowsingTest.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTabbedBrowsingTest.kt index 0137600492fa..1f838596fb94 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTabbedBrowsingTest.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTabbedBrowsingTest.kt @@ -5,19 +5,11 @@ package org.mozilla.fenix.ui import androidx.compose.ui.test.junit4.AndroidComposeTestRule -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.uiautomator.UiDevice import com.google.android.material.bottomsheet.BottomSheetBehavior -import mozilla.components.browser.state.store.BrowserStore import mozilla.components.concept.engine.mediasession.MediaSession -import okhttp3.mockwebserver.MockWebServer -import org.junit.After -import org.junit.Before import org.junit.Rule import org.junit.Test import org.mozilla.fenix.customannotations.SmokeTest -import org.mozilla.fenix.ext.components -import org.mozilla.fenix.helpers.AndroidAssetDispatcher import org.mozilla.fenix.helpers.HomeActivityIntentTestRule import org.mozilla.fenix.helpers.MatcherHelper import org.mozilla.fenix.helpers.RetryTestRule @@ -26,6 +18,7 @@ import org.mozilla.fenix.helpers.TestHelper.clickSnackbarButton import org.mozilla.fenix.helpers.TestHelper.closeApp import org.mozilla.fenix.helpers.TestHelper.restartApp import org.mozilla.fenix.helpers.TestHelper.verifySnackBarText +import org.mozilla.fenix.helpers.TestSetup import org.mozilla.fenix.ui.robots.browserScreen import org.mozilla.fenix.ui.robots.clickPageObject import org.mozilla.fenix.ui.robots.homeScreen @@ -49,11 +42,7 @@ import org.mozilla.fenix.ui.robots.notificationShade * - Shortcut context menu navigation */ -class ComposeTabbedBrowsingTest { - private lateinit var mDevice: UiDevice - private lateinit var mockWebServer: MockWebServer - private lateinit var browserStore: BrowserStore - +class ComposeTabbedBrowsingTest : TestSetup() { @get:Rule(order = 0) val composeTestRule = AndroidComposeTestRule( @@ -67,24 +56,6 @@ class ComposeTabbedBrowsingTest { @JvmField val retryTestRule = RetryTestRule(3) - @Before - fun setUp() { - // Initializing this as part of class construction, below the rule would throw a NPE - // So we are initializing this here instead of in all related tests. - browserStore = composeTestRule.activity.components.core.store - - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) - mockWebServer = MockWebServer().apply { - dispatcher = AndroidAssetDispatcher() - start() - } - } - - @After - fun tearDown() { - mockWebServer.shutdown() - } - // TestRail link: https://testrail.stage.mozaws.net/index.php?/cases/view/903599 @Test fun closeAllTabsTest() { diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTopSitesTest.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTopSitesTest.kt index 50bfadce1205..e783f9825dce 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTopSitesTest.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTopSitesTest.kt @@ -5,16 +5,10 @@ package org.mozilla.fenix.ui import androidx.compose.ui.test.junit4.AndroidComposeTestRule -import androidx.test.platform.app.InstrumentationRegistry -import androidx.test.uiautomator.UiDevice -import okhttp3.mockwebserver.MockWebServer -import org.junit.After -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.helpers.AndroidAssetDispatcher import org.mozilla.fenix.helpers.DataGenerationHelper.generateRandomString import org.mozilla.fenix.helpers.DataGenerationHelper.getStringResource import org.mozilla.fenix.helpers.HomeActivityTestRule @@ -22,6 +16,7 @@ import org.mozilla.fenix.helpers.TestAssetHelper.getGenericAsset import org.mozilla.fenix.helpers.TestHelper.clickSnackbarButton import org.mozilla.fenix.helpers.TestHelper.verifySnackBarText import org.mozilla.fenix.helpers.TestHelper.waitUntilSnackbarGone +import org.mozilla.fenix.helpers.TestSetup import org.mozilla.fenix.ui.robots.browserScreen import org.mozilla.fenix.ui.robots.homeScreen import org.mozilla.fenix.ui.robots.homeScreenWithComposeTopSites @@ -36,10 +31,7 @@ import org.mozilla.fenix.ui.robots.navigationToolbar * - Verifies existence of default top sites available on the home-screen */ -class ComposeTopSitesTest { - private lateinit var mDevice: UiDevice - private lateinit var mockWebServer: MockWebServer - +class ComposeTopSitesTest : TestSetup() { @get:Rule val composeTestRule = AndroidComposeTestRule( @@ -48,20 +40,6 @@ class ComposeTopSitesTest { ), ) { it.activity } - @Before - fun setUp() { - mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) - mockWebServer = MockWebServer().apply { - dispatcher = AndroidAssetDispatcher() - start() - } - } - - @After - fun tearDown() { - mockWebServer.shutdown() - } - // TestRail link: https://testrail.stage.mozaws.net/index.php?/cases/view/532598 @SmokeTest @Test diff --git a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/robots/HistoryRobot.kt b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/robots/HistoryRobot.kt index fc6ee4d1ad2f..8229bf18801f 100644 --- a/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/robots/HistoryRobot.kt +++ b/fenix/app/src/androidTest/java/org/mozilla/fenix/ui/robots/HistoryRobot.kt @@ -223,7 +223,7 @@ fun historyMenu(interact: HistoryRobot.() -> Unit): HistoryRobot.Transition { return HistoryRobot.Transition() } -private fun testPageTitle() = onView(allOf(withId(R.id.title), withText("Test_Page_1"))) +private fun testPageTitle() = onView(withId(R.id.title)) private fun pageUrl(url: String) = onView(allOf(withId(R.id.url), withText(url)))