From dbb3c610a2c3f781c5f566e404495d17c7ce523d Mon Sep 17 00:00:00 2001 From: "oana.horvath" Date: Tue, 13 Feb 2024 15:41:33 +0200 Subject: [PATCH] Bug 1879525 - Create TestSetup helper: Compose Test classes --- .../fenix/helpers/AppAndSystemHelper.kt | 55 +++++++++++++++++-- .../org/mozilla/fenix/helpers/TestSetup.kt | 47 +++++++++++----- .../mozilla/fenix/ui/ComposeBookmarksTest.kt | 34 +----------- .../mozilla/fenix/ui/ComposeCollectionTest.kt | 26 +-------- .../fenix/ui/ComposeContextMenusTest.kt | 31 ++--------- .../mozilla/fenix/ui/ComposeHistoryTest.kt | 44 ++------------- .../mozilla/fenix/ui/ComposeHomeScreenTest.kt | 27 +-------- .../fenix/ui/ComposeMediaNotificationTest.kt | 34 +----------- .../fenix/ui/ComposeNavigationToolbarTest.kt | 28 +--------- .../org/mozilla/fenix/ui/ComposeSearchTest.kt | 11 ++-- ...oseSettingsDeleteBrowsingDataOnQuitTest.kt | 26 +-------- .../ComposeSettingsDeleteBrowsingDataTest.kt | 22 +------- .../fenix/ui/ComposeTabbedBrowsingTest.kt | 34 +----------- .../mozilla/fenix/ui/ComposeTopSitesTest.kt | 27 +-------- .../mozilla/fenix/ui/robots/HistoryRobot.kt | 2 +- 15 files changed, 123 insertions(+), 325 deletions(-) diff --git a/app/src/androidTest/java/org/mozilla/fenix/helpers/AppAndSystemHelper.kt b/app/src/androidTest/java/org/mozilla/fenix/helpers/AppAndSystemHelper.kt index bc10b79abd..6162bbf0f3 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/helpers/AppAndSystemHelper.kt +++ b/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,36 @@ object AppAndSystemHelper { } } + suspend fun deleteHistoryStorage() { + val historyStorage = PlacesHistoryStorage(appContext.applicationContext) + Log.i( + TAG, + "deleteHistoryStorage before cleanup: History storage contains: ${historyStorage.getVisited()}", + ) + if (historyStorage.getVisited().isNotEmpty()) { + Log.i(TAG, "deleteHistoryStorage: Trying to delete all history storage.") + historyStorage.deleteEverything() + Log.i( + TAG, + "deleteHistoryStorage 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, "deleteBookmarksStorage before cleanup: Bookmarks storage contains: $bookmarks") + if (bookmarks?.isNotEmpty() == true) { + bookmarks.forEach { + Log.i(TAG, "deleteBookmarksStorage: Trying to delete $it bookmark from storage.") + 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, "deleteBookmarksStorage: Bookmark deleted. Bookmarks storage contains: $bookmarks") + } + } + } + fun setNetworkEnabled(enabled: Boolean) { val networkDisconnectedIdlingResource = NetworkConnectionIdlingResource(false) val networkConnectedIdlingResource = NetworkConnectionIdlingResource(true) @@ -228,7 +273,7 @@ object AppAndSystemHelper { */ fun isExternalAppBrowserActivityInCurrentTask(): Boolean { Log.i(TAG, "Trying to verify that the latest activity of the application is used for custom tabs or PWAs") - 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/app/src/androidTest/java/org/mozilla/fenix/helpers/TestSetup.kt b/app/src/androidTest/java/org/mozilla/fenix/helpers/TestSetup.kt index b080154c4a..02b24f439b 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/helpers/TestSetup.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/helpers/TestSetup.kt @@ -1,41 +1,51 @@ +/* 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 import android.util.Log 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 +/** + * Standard Test setup and tear down methods to run before each test. + * Some extra clean-up is required when we're using the org.mozilla.fenix.helpers.RetryTestRule (the instrumentation does not do that in this case). + * + */ open class TestSetup { lateinit var mockWebServer: MockWebServer - private val bookmarksStorage = PlacesBookmarksStorage(appContext.applicationContext) + lateinit var browserStore: BrowserStore @Before - fun setUp() { + open fun setUp() { Log.i(TAG, "TestSetup: Starting the @Before setup") - // Clear pre-existing notifications + // 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() } 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 + // 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 +59,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/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeBookmarksTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeBookmarksTest.kt index 26d31d920f..263a30f932 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeBookmarksTest.kt +++ b/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/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeCollectionTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeCollectionTest.kt index a18366cc74..e570ae59e6 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeCollectionTest.kt +++ b/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/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeContextMenusTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeContextMenusTest.kt index 5e554b49d0..0261c72d89 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeContextMenusTest.kt +++ b/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/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHistoryTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHistoryTest.kt index 138ffa7c30..d3b17f57d9 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHistoryTest.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHistoryTest.kt @@ -4,31 +4,23 @@ 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 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.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.historyMenu import org.mozilla.fenix.ui.robots.homeScreen @@ -39,42 +31,16 @@ 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/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHomeScreenTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHomeScreenTest.kt index e6930672a9..38ed0da774 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeHomeScreenTest.kt +++ b/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/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeMediaNotificationTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeMediaNotificationTest.kt index 63c0ad07b6..c673cbe3de 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeMediaNotificationTest.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeMediaNotificationTest.kt @@ -5,23 +5,17 @@ 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.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.clickPageObject import org.mozilla.fenix.ui.robots.homeScreen @@ -34,11 +28,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 +41,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/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeNavigationToolbarTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeNavigationToolbarTest.kt index ee4964197f..2ff866316b 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeNavigationToolbarTest.kt +++ b/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/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSearchTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSearchTest.kt index 27f46d84fd..eaa76b1481 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSearchTest.kt +++ b/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/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataOnQuitTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataOnQuitTest.kt index 6994cd4504..21521871b3 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataOnQuitTest.kt +++ b/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 @@ -27,6 +22,7 @@ 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 +33,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 +49,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/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataTest.kt index 49c809a7c2..7b1fc996b2 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeSettingsDeleteBrowsingDataTest.kt +++ b/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 @@ -22,6 +18,7 @@ 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 +30,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 +40,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/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTabbedBrowsingTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTabbedBrowsingTest.kt index 0137600492..7ef1513532 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTabbedBrowsingTest.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTabbedBrowsingTest.kt @@ -5,27 +5,21 @@ 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 import org.mozilla.fenix.helpers.TestAssetHelper import org.mozilla.fenix.helpers.TestHelper.clickSnackbarButton import org.mozilla.fenix.helpers.TestHelper.closeApp +import org.mozilla.fenix.helpers.TestHelper.mDevice 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 +43,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 +57,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/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTopSitesTest.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTopSitesTest.kt index 50bfadce12..16ea4b1275 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTopSitesTest.kt +++ b/app/src/androidTest/java/org/mozilla/fenix/ui/ComposeTopSitesTest.kt @@ -5,23 +5,19 @@ 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 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.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 +32,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 +41,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/app/src/androidTest/java/org/mozilla/fenix/ui/robots/HistoryRobot.kt b/app/src/androidTest/java/org/mozilla/fenix/ui/robots/HistoryRobot.kt index fc6ee4d1ad..8229bf1880 100644 --- a/app/src/androidTest/java/org/mozilla/fenix/ui/robots/HistoryRobot.kt +++ b/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)))