Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

Commit

Permalink
Bug 1879525 - Create TestSetup helper: Compose Test classes
Browse files Browse the repository at this point in the history
  • Loading branch information
sv-ohorvath committed Feb 20, 2024
1 parent 0d8d7a0 commit 6dd4284
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 323 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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/")
Expand All @@ -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 {
Expand All @@ -139,13 +153,42 @@ 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.")
}
}
}
}

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)
Expand Down Expand Up @@ -228,7 +271,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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,42 @@ 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

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")
// 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
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()
Expand All @@ -49,4 +51,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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -45,37 +40,21 @@ 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 }

@Rule(order = 1)
@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() {
Expand Down
Loading

0 comments on commit 6dd4284

Please sign in to comment.