Bug 1881041 - TestSetup helper: refactor RetryTestRule

fenix/125.0
oana.horvath 3 months ago committed by mergify[bot]
parent b44e798924
commit a9b83fb300

@ -41,6 +41,7 @@ import org.junit.Assert
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.mozilla.fenix.Config import org.mozilla.fenix.Config
import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.components.PermissionStorage
import org.mozilla.fenix.customtabs.ExternalAppBrowserActivity import org.mozilla.fenix.customtabs.ExternalAppBrowserActivity
import org.mozilla.fenix.helpers.Constants.PackageName.PIXEL_LAUNCHER import org.mozilla.fenix.helpers.Constants.PackageName.PIXEL_LAUNCHER
import org.mozilla.fenix.helpers.Constants.PackageName.YOUTUBE_APP import org.mozilla.fenix.helpers.Constants.PackageName.YOUTUBE_APP
@ -183,14 +184,33 @@ object AppAndSystemHelper {
Log.i(TAG, "deleteBookmarksStorage before cleanup: Bookmarks storage contains: $bookmarks") Log.i(TAG, "deleteBookmarksStorage before cleanup: Bookmarks storage contains: $bookmarks")
if (bookmarks?.isNotEmpty() == true) { if (bookmarks?.isNotEmpty() == true) {
bookmarks.forEach { bookmarks.forEach {
Log.i(TAG, "deleteBookmarksStorage: Trying to delete $it bookmark from storage.") Log.i(
TAG,
"deleteBookmarksStorage: Trying to delete $it bookmark from storage.",
)
bookmarksStorage.deleteNode(it.guid) 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. // 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") Log.i(
TAG,
"deleteBookmarksStorage: Bookmark deleted. Bookmarks storage contains: $bookmarks",
)
} }
} }
} }
suspend fun deletePermissionsStorage() {
val permissionStorage = PermissionStorage(appContext.applicationContext)
Log.i(
TAG,
"deletePermissionsStorage: Trying to delete permissions. Permissions storage contains: ${permissionStorage.getSitePermissionsPaged()}",
)
permissionStorage.deleteAllSitePermissions()
Log.i(
TAG,
"deletePermissionsStorage: Permissions deleted. Permissions storage contains: ${permissionStorage.getSitePermissionsPaged()}",
)
}
fun setNetworkEnabled(enabled: Boolean) { fun setNetworkEnabled(enabled: Boolean) {
val networkDisconnectedIdlingResource = NetworkConnectionIdlingResource(false) val networkDisconnectedIdlingResource = NetworkConnectionIdlingResource(false)
val networkConnectedIdlingResource = NetworkConnectionIdlingResource(true) val networkConnectedIdlingResource = NetworkConnectionIdlingResource(true)

@ -9,16 +9,12 @@ import androidx.test.espresso.IdlingResourceTimeoutException
import androidx.test.espresso.NoMatchingViewException import androidx.test.espresso.NoMatchingViewException
import androidx.test.uiautomator.UiObjectNotFoundException import androidx.test.uiautomator.UiObjectNotFoundException
import junit.framework.AssertionFailedError import junit.framework.AssertionFailedError
import kotlinx.coroutines.runBlocking
import org.junit.rules.TestRule import org.junit.rules.TestRule
import org.junit.runner.Description import org.junit.runner.Description
import org.junit.runners.model.Statement import org.junit.runners.model.Statement
import org.mozilla.fenix.components.PermissionStorage
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.helpers.AppAndSystemHelper.setNetworkEnabled
import org.mozilla.fenix.helpers.Constants.TAG import org.mozilla.fenix.helpers.Constants.TAG
import org.mozilla.fenix.helpers.IdlingResourceHelper.unregisterAllIdlingResources 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. * Rule to retry flaky tests for a given number of times, catching some of the more common exceptions.
@ -27,9 +23,6 @@ import org.mozilla.fenix.helpers.TestHelper.appContext
* *
*/ */
class RetryTestRule(private val retryCount: Int = 5) : TestRule { class RetryTestRule(private val retryCount: Int = 5) : TestRule {
// Used for clearing all permission data after each test try
private val permissionStorage = PermissionStorage(appContext.applicationContext)
@Suppress("TooGenericExceptionCaught", "ComplexMethod") @Suppress("TooGenericExceptionCaught", "ComplexMethod")
override fun apply(base: Statement, description: Description): Statement { override fun apply(base: Statement, description: Description): Statement {
return statement { return statement {
@ -39,77 +32,50 @@ class RetryTestRule(private val retryCount: Int = 5) : TestRule {
base.evaluate() base.evaluate()
break break
} catch (t: AssertionError) { } catch (t: AssertionError) {
setNetworkEnabled(true)
unregisterAllIdlingResources() unregisterAllIdlingResources()
runBlocking { exitMenu()
appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true
permissionStorage.deleteAllSitePermissions()
}
if (i == retryCount) { if (i == retryCount) {
Log.i(TAG, "RetryTestRule: Max numbers of retries reached.") Log.i(TAG, "RetryTestRule: Max numbers of retries reached.")
throw t throw t
} }
} catch (t: AssertionFailedError) { } catch (t: AssertionFailedError) {
unregisterAllIdlingResources() unregisterAllIdlingResources()
runBlocking { exitMenu()
appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true
permissionStorage.deleteAllSitePermissions()
}
if (i == retryCount) { if (i == retryCount) {
Log.i(TAG, "RetryTestRule: Max numbers of retries reached.") Log.i(TAG, "RetryTestRule: Max numbers of retries reached.")
throw t throw t
} }
} catch (t: UiObjectNotFoundException) { } catch (t: UiObjectNotFoundException) {
setNetworkEnabled(true)
unregisterAllIdlingResources() unregisterAllIdlingResources()
runBlocking { exitMenu()
appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true
permissionStorage.deleteAllSitePermissions()
}
if (i == retryCount) { if (i == retryCount) {
Log.i(TAG, "RetryTestRule: Max numbers of retries reached.") Log.i(TAG, "RetryTestRule: Max numbers of retries reached.")
throw t throw t
} }
} catch (t: NoMatchingViewException) { } catch (t: NoMatchingViewException) {
setNetworkEnabled(true)
unregisterAllIdlingResources() unregisterAllIdlingResources()
runBlocking { exitMenu()
appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true
permissionStorage.deleteAllSitePermissions()
}
if (i == retryCount) { if (i == retryCount) {
Log.i(TAG, "RetryTestRule: Max numbers of retries reached.") Log.i(TAG, "RetryTestRule: Max numbers of retries reached.")
throw t throw t
} }
} catch (t: IdlingResourceTimeoutException) { } catch (t: IdlingResourceTimeoutException) {
setNetworkEnabled(true)
unregisterAllIdlingResources() unregisterAllIdlingResources()
runBlocking { exitMenu()
appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true
permissionStorage.deleteAllSitePermissions()
}
if (i == retryCount) { if (i == retryCount) {
Log.i(TAG, "RetryTestRule: Max numbers of retries reached.") Log.i(TAG, "RetryTestRule: Max numbers of retries reached.")
throw t throw t
} }
} catch (t: RuntimeException) { } catch (t: RuntimeException) {
setNetworkEnabled(true)
unregisterAllIdlingResources() unregisterAllIdlingResources()
runBlocking { exitMenu()
appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true
permissionStorage.deleteAllSitePermissions()
}
if (i == retryCount) { if (i == retryCount) {
Log.i(TAG, "RetryTestRule: Max numbers of retries reached.") Log.i(TAG, "RetryTestRule: Max numbers of retries reached.")
throw t throw t
} }
} catch (t: NullPointerException) { } catch (t: NullPointerException) {
setNetworkEnabled(true)
unregisterAllIdlingResources() unregisterAllIdlingResources()
runBlocking { exitMenu()
appContext.settings().alwaysOpenTheHomepageWhenOpeningTheApp = true
permissionStorage.deleteAllSitePermissions()
}
if (i == retryCount) { if (i == retryCount) {
Log.i(TAG, "RetryTestRule: Max numbers of retries reached.") Log.i(TAG, "RetryTestRule: Max numbers of retries reached.")
throw t throw t

@ -8,7 +8,6 @@ import android.content.Context
import android.net.Uri import android.net.Uri
import android.util.Log import android.util.Log
import android.view.View import android.view.View
import androidx.test.core.app.launchActivity
import androidx.test.espresso.Espresso.onView import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions.longClick import androidx.test.espresso.action.ViewActions.longClick
import androidx.test.espresso.assertion.ViewAssertions import androidx.test.espresso.assertion.ViewAssertions
@ -116,11 +115,12 @@ object TestHelper {
// exit from Menus to home screen or browser // exit from Menus to home screen or browser
fun exitMenu() { fun exitMenu() {
val toolbar = val menuToolbar =
mDevice.findObject(UiSelector().resourceId("$packageName:id/toolbar")) mDevice.findObject(UiSelector().resourceId("$packageName:id/navigationToolbar"))
while (!toolbar.waitForExists(waitingTimeShort)) { while (menuToolbar.waitForExists(waitingTimeShort)) {
Log.i(TAG, "exitMenu: Trying to press the device back button to return to the app home/browser view")
mDevice.pressBack() mDevice.pressBack()
Log.i(TAG, "exitMenu: Exiting app settings menus using device back button") Log.i(TAG, "exitMenu: Pressed the device back button to return to the app home/browser view")
} }
} }

@ -28,12 +28,14 @@ open class TestSetup {
Log.i(TAG, "TestSetup: Starting the @Before setup") Log.i(TAG, "TestSetup: Starting the @Before setup")
// Initializing this as part of class construction, below the rule would throw a NPE. // 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. // So we are initializing this here instead of in all related tests.
Log.i(TAG, "TestSetup: Initializing the browserStore instance") Log.i(TAG, "TestSetup: Trying to initialize the browserStore instance")
browserStore = appContext.components.core.store browserStore = appContext.components.core.store
Log.i(TAG, "TestSetup: Initialized the browserStore instance")
// Clear pre-existing notifications. // Clear pre-existing notifications.
notificationShade { notificationShade {
cancelAllShownNotifications() cancelAllShownNotifications()
} }
runBlocking { runBlocking {
// Reset locale to EN-US if needed. // Reset locale to EN-US if needed.
AppAndSystemHelper.resetSystemLocaleToEnUS() AppAndSystemHelper.resetSystemLocaleToEnUS()
@ -46,7 +48,10 @@ open class TestSetup {
AppAndSystemHelper.deleteBookmarksStorage() AppAndSystemHelper.deleteBookmarksStorage()
// Clear history left after a failed test, before a retry. // Clear history left after a failed test, before a retry.
AppAndSystemHelper.deleteHistoryStorage() AppAndSystemHelper.deleteHistoryStorage()
// Clear permissions left after a failed test, before a retry.
AppAndSystemHelper.deletePermissionsStorage()
} }
mockWebServer = MockWebServer().apply { mockWebServer = MockWebServer().apply {
dispatcher = AndroidAssetDispatcher() dispatcher = AndroidAssetDispatcher()
} }

Loading…
Cancel
Save