Bug 1885160 - Add logs to AppAndSystemHelper

fenix/125.0
oana.horvath 2 months ago committed by mergify[bot]
parent 2d7942033c
commit b41b5727eb

@ -46,6 +46,7 @@ import org.mozilla.fenix.customtabs.ExternalAppBrowserActivity
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.waitingTime
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTimeShort
import org.mozilla.fenix.helpers.TestHelper.appContext
import org.mozilla.fenix.helpers.TestHelper.mDevice
@ -61,10 +62,17 @@ object AppAndSystemHelper {
private val bookmarksStorage = PlacesBookmarksStorage(appContext.applicationContext)
suspend fun bookmarks() = bookmarksStorage.getTree(BookmarkRoot.Mobile.id)?.children
fun getPermissionAllowID(): String {
Log.i(TAG, "getPermissionAllowID: Trying to get the permission button resource ID based on API.")
return when
(Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
true -> "com.android.permissioncontroller"
false -> "com.android.packageinstaller"
true -> {
Log.i(TAG, "getPermissionAllowID: Getting the permission button resource ID for API ${Build.VERSION.SDK_INT}.")
"com.android.permissioncontroller"
}
false -> {
Log.i(TAG, "getPermissionAllowID: Getting the permission button resource ID for API ${Build.VERSION.SDK_INT}.")
"com.android.packageinstaller"
}
}
}
@ -76,6 +84,7 @@ object AppAndSystemHelper {
*/
fun deleteDownloadedFileOnStorage(fileName: String) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
Log.i(TAG, "deleteDownloadedFileOnStorage: Trying to delete file from API ${Build.VERSION.SDK_INT}.")
val storageManager: StorageManager? =
appContext.getSystemService(Context.STORAGE_SERVICE) as StorageManager?
val storageVolumes = storageManager!!.storageVolumes
@ -83,24 +92,28 @@ object AppAndSystemHelper {
val file = File(storageVolume.directory!!.path + "/Download/" + fileName)
try {
if (file.exists()) {
Log.i(TAG, "deleteDownloadedFileOnStorage: The file exists. Trying to delete $fileName, try 1.")
file.delete()
Log.d("TestLog", "File delete try 1")
Assert.assertFalse("The file was not deleted", file.exists())
Assert.assertFalse("$TAG deleteDownloadedFileOnStorage: The $fileName file was not deleted", file.exists())
Log.i(TAG, "deleteDownloadedFileOnStorage: Verified the $fileName file was deleted.")
}
} catch (e: AssertionError) {
Log.i(TAG, "deleteDownloadedFileOnStorage: AssertionError caught. Retrying to delete the file.")
file.delete()
Log.d("TestLog", "File delete retried")
Assert.assertFalse("The file was not deleted", file.exists())
Log.i(TAG, "deleteDownloadedFileOnStorage: Retrying to delete $fileName.")
Assert.assertFalse("$TAG deleteDownloadedFileOnStorage: The file was not deleted", file.exists())
Log.i(TAG, "deleteDownloadedFileOnStorage: Verified the $fileName file was deleted, try 2.")
}
} else {
runBlocking {
Log.i(TAG, "deleteDownloadedFileOnStorage: Trying to delete file from API ${Build.VERSION.SDK_INT}.")
val downloadedFile = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
fileName,
)
if (downloadedFile.exists()) {
Log.i(TAG, "deleteDownloadedFileOnStorage: Verifying if $downloadedFile exists.")
Log.i(TAG, "deleteDownloadedFileOnStorage: The file exists. Trying to delete the file.")
downloadedFile.delete()
Log.i(TAG, "deleteDownloadedFileOnStorage: $downloadedFile deleted.")
}
@ -114,8 +127,8 @@ object AppAndSystemHelper {
* Environment.getExternalStorageDirectory() is deprecated starting with API 29.
*/
fun clearDownloadsFolder() {
Log.i(TAG, "clearDownloadsFolder: Detected API ${Build.VERSION.SDK_INT}")
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.Q) {
Log.i(TAG, "clearDownloadsFolder: API > 29")
val storageManager: StorageManager? =
appContext.getSystemService(Context.STORAGE_SERVICE) as StorageManager?
val storageVolumes = storageManager!!.storageVolumes
@ -135,6 +148,10 @@ object AppAndSystemHelper {
)
// Delete all files in the folder
for (file in files!!) {
Log.i(
TAG,
"clearDownloadsFolder: Trying to delete $file from \"DOWNLOADS\" folder.",
)
file.delete()
Log.i(
TAG,
@ -155,11 +172,10 @@ object AppAndSystemHelper {
}
} else {
runBlocking {
Log.i(TAG, "clearDownloadsFolder: API <= 29")
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.")
Log.i(TAG, "clearDownloadsFolder: Trying to delete from storage: $it.")
it.delete()
Log.i(TAG, "clearDownloadsFolder: Download file $it deleted.")
}
@ -220,27 +236,53 @@ object AppAndSystemHelper {
when (enabled) {
true -> {
Log.i(
TAG,
"setNetworkEnabled: Trying to enable the network connection.",
)
mDevice.executeShellCommand("svc data enable")
Log.i(
TAG,
"setNetworkEnabled: Data network connection enable command sent.",
)
mDevice.executeShellCommand("svc wifi enable")
Log.i(
TAG,
"setNetworkEnabled: Wifi network connection enable command sent.",
)
// Wait for network connection to be completely enabled
Log.i(TAG, "setNetworkEnabled: Waiting for connection to be enabled.")
IdlingRegistry.getInstance().register(networkConnectedIdlingResource)
Espresso.onIdle {
IdlingRegistry.getInstance().unregister(networkConnectedIdlingResource)
}
Log.i(TAG, "setNetworkEnabled: Network connection was enabled")
Log.i(TAG, "setNetworkEnabled: Network connection was enabled.")
}
false -> {
Log.i(
TAG,
"setNetworkEnabled: Trying to disable the network connection.",
)
mDevice.executeShellCommand("svc data disable")
Log.i(
TAG,
"setNetworkEnabled: Data network connection disable command sent.",
)
mDevice.executeShellCommand("svc wifi disable")
Log.i(
TAG,
"setNetworkEnabled: Wifi network connection disable command sent.",
)
// Wait for network connection to be completely disabled
Log.i(TAG, "setNetworkEnabled: Waiting for connection to be disabled.")
IdlingRegistry.getInstance().register(networkDisconnectedIdlingResource)
Espresso.onIdle {
IdlingRegistry.getInstance().unregister(networkDisconnectedIdlingResource)
}
Log.i(TAG, "setNetworkEnabled: Network connection was disabled")
Log.i(TAG, "setNetworkEnabled: Network connection was disabled.")
}
}
}
@ -250,6 +292,8 @@ object AppAndSystemHelper {
return try {
val packageManager = InstrumentationRegistry.getInstrumentation().context.packageManager
packageManager.getApplicationInfo(packageName, 0).enabled
Log.i(TAG, "isPackageInstalled: $packageName is installed.")
true
} catch (e: PackageManager.NameNotFoundException) {
Log.i(TAG, "isPackageInstalled: $packageName is not installed - ${e.message}")
false
@ -258,18 +302,18 @@ object AppAndSystemHelper {
fun assertExternalAppOpens(appPackageName: String) {
if (isPackageInstalled(appPackageName)) {
Log.i(TAG, "assertExternalAppOpens: $appPackageName is installed on device")
try {
Log.i(TAG, "assertExternalAppOpens: Try block")
Log.i(TAG, "assertExternalAppOpens: Trying to check the intent sent.")
intended(toPackage(appPackageName))
Log.i(TAG, "assertExternalAppOpens: Matched intent to $appPackageName")
Log.i(TAG, "assertExternalAppOpens: Matched open intent to $appPackageName.")
} catch (e: AssertionFailedError) {
Log.i(TAG, "assertExternalAppOpens: Catch block - ${e.message}")
Log.i(TAG, "assertExternalAppOpens: Intent match failure. ${e.message}")
}
} else {
Log.i(TAG, "assertExternalAppOpens: Trying to verify the \"Could not open file\" message.")
mDevice.waitNotNull(
Until.findObject(By.text("Could not open file")),
TestAssetHelper.waitingTime,
waitingTime,
)
Log.i(TAG, "assertExternalAppOpens: Verified \"Could not open file\" message")
}
@ -277,17 +321,28 @@ object AppAndSystemHelper {
fun assertNativeAppOpens(appPackageName: String, url: String = "") {
if (isPackageInstalled(appPackageName)) {
mDevice.waitForIdle(TestAssetHelper.waitingTimeShort)
Log.i(TAG, "assertNativeAppOpens: Waiting for the device to be idle $waitingTimeShort ms.")
mDevice.waitForIdle(waitingTimeShort)
Log.i(TAG, "assertNativeAppOpens: Waited for the device to be idle $waitingTimeShort ms.")
Log.i(TAG, "assertNativeAppOpens: Trying to match the app package name is matched.")
Assert.assertTrue(
TestHelper.mDevice.findObject(UiSelector().packageName(appPackageName))
.waitForExists(TestAssetHelper.waitingTime),
"$TAG $appPackageName not found",
mDevice.findObject(UiSelector().packageName(appPackageName))
.waitForExists(waitingTime),
)
Log.i(TAG, "assertNativeAppOpens: App package name matched.")
} else {
Log.i(TAG, "assertNativeAppOpens: Trying to verify the page redirect URL.")
BrowserRobot().verifyUrl(url)
Log.i(TAG, "assertNativeAppOpens: Verified the page redirect URL.")
}
}
fun assertYoutubeAppOpens() = intended(toPackage(YOUTUBE_APP))
fun assertYoutubeAppOpens() {
Log.i(TAG, "assertYoutubeAppOpens: Trying to check the intent to YouTube.")
intended(toPackage(YOUTUBE_APP))
Log.i(TAG, "assertYoutubeAppOpens: Verified the intent matches YouTube.")
}
/**
* Checks whether the latest activity of the application is used for custom tabs or PWAs.
@ -295,11 +350,16 @@ object AppAndSystemHelper {
* @return Boolean value that helps us know if the current activity supports custom tabs or PWAs.
*/
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 = appContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
mDevice.waitForIdle(TestAssetHelper.waitingTimeShort)
Log.i(TAG, "isExternalAppBrowserActivityInCurrentTask: Waiting for the device to be idle for $waitingTimeShort ms")
mDevice.waitForIdle(waitingTimeShort)
Log.i(TAG, "isExternalAppBrowserActivityInCurrentTask: Waited for the device to be idle for $waitingTimeShort ms")
Log.i(
TAG,
"isExternalAppBrowserActivityInCurrentTask: Trying to verify that the latest activity of the application is used for custom tabs or PWAs",
)
return activityManager.appTasks[0].taskInfo.topActivity!!.className == ExternalAppBrowserActivity::class.java.name
}
@ -314,14 +374,18 @@ object AppAndSystemHelper {
testBlock: () -> Unit,
) {
idlingResources.forEach {
Log.i(TAG, "registerAndCleanupIdlingResources: Trying to register idling resource $it.")
IdlingRegistry.getInstance().register(it)
Log.i(TAG, "registerAndCleanupIdlingResources: Registered idling resource $it.")
}
try {
testBlock()
} finally {
idlingResources.forEach {
Log.i(TAG, "registerAndCleanupIdlingResources: Trying to unregister idling resource $it.")
IdlingRegistry.getInstance().unregister(it)
Log.i(TAG, "registerAndCleanupIdlingResources: Unregistered idling resource $it.")
}
}
}
@ -339,18 +403,26 @@ object AppAndSystemHelper {
)
if (Build.VERSION.SDK_INT >= 23) {
if (whileUsingTheAppPermissionButton.waitForExists(TestAssetHelper.waitingTimeShort)) {
if (whileUsingTheAppPermissionButton.waitForExists(waitingTimeShort)) {
Log.i(TAG, "grantSystemPermission: Trying to click the \"While using the app\" button.")
whileUsingTheAppPermissionButton.click()
} else if (allowPermissionButton.waitForExists(TestAssetHelper.waitingTimeShort)) {
Log.i(TAG, "grantSystemPermission: Clicked the \"While using the app\" button.")
} else if (allowPermissionButton.waitForExists(waitingTimeShort)) {
Log.i(TAG, "grantSystemPermission: Trying to click the \"Allow\" button.")
allowPermissionButton.click()
Log.i(TAG, "grantSystemPermission: Clicked the \"Allow\" button.")
}
}
}
// Permission deny dialogs differ on various Android APIs
fun denyPermission() {
mDevice.findObject(UiSelector().textContains("Deny")).waitForExists(TestAssetHelper.waitingTime)
Log.i(TAG, "denyPermission: Waiting $waitingTime ms for the \"Deny\" button to exist.")
mDevice.findObject(UiSelector().textContains("Deny")).waitForExists(waitingTime)
Log.i(TAG, "denyPermission: Waited for $waitingTime ms for the \"Deny\" button to exist.")
Log.i(TAG, "denyPermission: Trying to click the \"Deny\" button.")
mDevice.findObject(UiSelector().textContains("Deny")).click()
Log.i(TAG, "denyPermission: Clicked the \"Deny\" button.")
}
fun isTestLab(): Boolean {
@ -366,12 +438,17 @@ object AppAndSystemHelper {
val defaultLocale = Locale.getDefault()
try {
Log.i(TAG, "runWithSystemLocaleChanged: Trying to set the locale.")
setSystemLocale(locale)
Log.i(TAG, "runWithSystemLocaleChanged: Running the test block.")
testBlock()
ThreadUtils.runOnUiThread { testRule.activity.recreate() }
Log.i(TAG, "runWithSystemLocaleChanged: Test block finished.")
} catch (e: Exception) {
Log.i(TAG, "runWithSystemLocaleChanged: The test block has thrown an exception.${e.message}")
e.printStackTrace()
} finally {
Log.i(TAG, "runWithSystemLocaleChanged: Trying to reset the locale to default.")
setSystemLocale(defaultLocale)
}
}
@ -416,10 +493,14 @@ object AppAndSystemHelper {
}
fun putAppToBackground() {
Log.i(TAG, "putAppToBackground: Trying to press the device Recent apps button.")
mDevice.pressRecentApps()
Log.i(TAG, "putAppToBackground: Pressed the device Recent apps button.")
Log.i(TAG, "putAppToBackground: Waiting for the app to be gone for $waitingTime ms.")
mDevice.findObject(UiSelector().resourceId("${TestHelper.packageName}:id/container")).waitUntilGone(
TestAssetHelper.waitingTime,
waitingTime,
)
Log.i(TAG, "putAppToBackground: Waited for the app to be gone for $waitingTime ms.")
}
/**
@ -428,12 +509,19 @@ object AppAndSystemHelper {
* The recent apps tray on API 30 will always display only 2 apps, even if previously were opened more.
* The index of the most recent opened app will always have index 2, meaning that the previously opened app will have index 1.
*/
fun bringAppToForeground() =
mDevice.findObject(UiSelector().index(2).packageName(PIXEL_LAUNCHER)).clickAndWaitForNewWindow(waitingTimeShort)
fun bringAppToForeground() {
Log.i(TAG, "bringAppToForeground: Trying to select the app from the recent apps tray and wait for $waitingTime ms for a new window")
mDevice.findObject(UiSelector().index(2).packageName(PIXEL_LAUNCHER))
.clickAndWaitForNewWindow(waitingTimeShort)
Log.i(TAG, "bringAppToForeground: Selected the app from the recent apps tray.")
}
fun verifyKeyboardVisibility(isExpectedToBeVisible: Boolean = true) {
Log.i(TAG, "verifyKeyboardVisibility: Waiting for the device to be idle.")
mDevice.waitForIdle()
Log.i(TAG, "verifyKeyboardVisibility: Waited for the device to be idle.")
Log.i(TAG, "verifyKeyboardVisibility: Trying to verify the keyboard is visible.")
assertEquals(
"Keyboard not shown",
isExpectedToBeVisible,
@ -441,6 +529,7 @@ object AppAndSystemHelper {
.executeShellCommand("dumpsys input_method | grep mInputShown")
.contains("mInputShown=true"),
)
Log.i(TAG, "verifyKeyboardVisibility: Verified the keyboard is visible.")
}
fun openAppFromExternalLink(url: String) {
@ -452,10 +541,14 @@ object AppAndSystemHelper {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
try {
Log.i(TAG, "openAppFromExternalLink: Trying to start the activity from an external intent.")
context.startActivity(intent)
Log.i(TAG, "openAppFromExternalLink: Activity started from an external intent.")
} catch (ex: ActivityNotFoundException) {
Log.i(TAG, "openAppFromExternalLink: Exception caught. Trying to start the activity from a null intent.")
intent.setPackage(null)
context.startActivity(intent)
Log.i(TAG, "openAppFromExternalLink: Started the activity from a null intent.")
}
}
@ -464,6 +557,7 @@ object AppAndSystemHelper {
* For example: this method will avoid accidentally running a test on GV versions where the feature is disabled.
*/
fun runWithCondition(condition: Boolean, testBlock: () -> Unit) {
Log.i(TAG, "runWithCondition: Trying to run the test based on condition. The condition is: $condition.")
if (condition) {
testBlock()
}
@ -480,11 +574,15 @@ object AppAndSystemHelper {
addCategory(Intent.CATEGORY_LAUNCHER)
}
Log.i(TAG, "runWithLauncherIntent: Trying to launch the activity from an intent: $launcherIntent.")
activityTestRule.activityRule.withIntent(launcherIntent).launchActivity(launcherIntent)
Log.i(TAG, "runWithLauncherIntent: Launched the activity from an intent: $launcherIntent.")
try {
Log.i(TAG, "runWithLauncherIntent: Trying run the test block.")
testBlock()
Log.i(TAG, "runWithLauncherIntent: Finished running the test block.")
} catch (e: Exception) {
Log.i(TAG, "runWithLauncherIntent: Exception caught while running the test block: ${e.message}")
e.printStackTrace()
}
}

@ -19,6 +19,7 @@ class PwaRobot {
fun verifyCustomTabToolbarIsNotDisplayed() = assertUIObjectExists(itemWithResId("$packageName:id/toolbar"), exists = false)
fun verifyPwaActivityInCurrentTask() {
assertTrue("$TAG: The latest activity of the application is not used for custom tabs or PWAs", isExternalAppBrowserActivityInCurrentTask())
Log.i(TAG, "verifyPwaActivityInCurrentTask: Verified that the latest activity of the application is used for custom tabs or PWAs")
}
class Transition

Loading…
Cancel
Save