For #27220: replaces manually deleting a file with deleting storage programmatically.

This commit is contained in:
oana.horvath 2022-11-15 12:39:38 +02:00 committed by mergify[bot]
parent cca309e0e6
commit 537c296f63
2 changed files with 48 additions and 52 deletions

View File

@ -20,8 +20,12 @@ import android.graphics.Canvas
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.os.storage.StorageManager
import android.os.storage.StorageVolume
import android.provider.Settings
import android.util.Log
import android.view.View
import androidx.annotation.RequiresApi
import androidx.browser.customtabs.CustomTabsIntent
import androidx.test.espresso.Espresso
import androidx.test.espresso.Espresso.onView
@ -41,7 +45,6 @@ import androidx.test.rule.ActivityTestRule
import androidx.test.uiautomator.By
import androidx.test.uiautomator.UiDevice
import androidx.test.uiautomator.UiObject
import androidx.test.uiautomator.UiObjectNotFoundException
import androidx.test.uiautomator.UiScrollable
import androidx.test.uiautomator.UiSelector
import androidx.test.uiautomator.Until
@ -52,12 +55,12 @@ import org.hamcrest.CoreMatchers
import org.hamcrest.CoreMatchers.allOf
import org.hamcrest.Matcher
import org.junit.Assert
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.customtabs.ExternalAppBrowserActivity
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.helpers.Constants.PackageName.GOOGLE_APPS_PHOTOS
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTime
import org.mozilla.fenix.helpers.TestAssetHelper.waitingTimeShort
import org.mozilla.fenix.helpers.ext.waitNotNull
@ -65,6 +68,7 @@ import org.mozilla.fenix.helpers.idlingresource.NetworkConnectionIdlingResource
import org.mozilla.fenix.ui.robots.BrowserRobot
import org.mozilla.fenix.utils.IntentUtils
import org.mozilla.gecko.util.ThreadUtils
import java.io.File
import java.util.Locale
import java.util.regex.Pattern
@ -147,24 +151,21 @@ object TestHelper {
}
}
// Remove test file from Google Photos (AOSP) on Firebase
fun deleteDownloadFromStorage() {
val deleteButton = mDevice.findObject(UiSelector().resourceId("$GOOGLE_APPS_PHOTOS:id/trash"))
deleteButton.waitForExists(waitingTime)
deleteButton.click()
// Sometimes there's a secondary confirmation
@RequiresApi(Build.VERSION_CODES.R)
fun deleteDownloadedFileOnStorage(fileName: String) {
val storageManager: 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)
try {
val deleteConfirm = mDevice.findObject(UiSelector().text("Got it"))
deleteConfirm.waitForExists(waitingTime)
deleteConfirm.click()
} catch (e: UiObjectNotFoundException) {
// Do nothing
file.delete()
Log.d("TestLog", "File delete try 1")
assertFalse("The file was not deleted", file.exists())
} catch (e: AssertionError) {
file.delete()
Log.d("TestLog", "File delete retried")
assertFalse("The file was not deleted", file.exists())
}
val trashIt = mDevice.findObject(UiSelector().resourceId("$GOOGLE_APPS_PHOTOS:id/move_to_trash"))
trashIt.waitForExists(waitingTime)
trashIt.click()
}
fun setNetworkEnabled(enabled: Boolean) {

View File

@ -5,20 +5,14 @@
package org.mozilla.fenix.ui
import androidx.core.net.toUri
import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.runner.permission.PermissionRequester
import androidx.test.uiautomator.UiDevice
import org.junit.After
import org.junit.Before
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.junit.rules.TestWatcher
import org.junit.runner.Description
import org.mozilla.fenix.customannotations.SmokeTest
import org.mozilla.fenix.helpers.HomeActivityIntentTestRule
import org.mozilla.fenix.helpers.TestHelper.deleteDownloadFromStorage
import org.mozilla.fenix.helpers.TestHelper.deleteDownloadedFileOnStorage
import org.mozilla.fenix.helpers.TestHelper.mDevice
import org.mozilla.fenix.ui.robots.browserScreen
import org.mozilla.fenix.ui.robots.downloadRobot
import org.mozilla.fenix.ui.robots.navigationToolbar
@ -33,8 +27,6 @@ import org.mozilla.fenix.ui.robots.notificationShade
* - Verifies managing downloads inside the Downloads listing.
**/
class DownloadTest {
private lateinit var mDevice: UiDevice
/* Remote test page managed by Mozilla Mobile QA team at https://github.com/mozilla-mobile/testapp */
private val downloadTestPage = "https://storage.googleapis.com/mobile_test_assets/test_app/downloads.html"
private var downloadFile: String = ""
@ -42,25 +34,8 @@ class DownloadTest {
@get:Rule
val activityTestRule = HomeActivityIntentTestRule.withDefaultSettingsOverrides()
// Making sure to grant storage access for this test running on API 28
@get: Rule
var watcher: TestRule = object : TestWatcher() {
override fun starting(description: Description) {
if (description.methodName == "pauseResumeCancelDownloadTest") {
PermissionRequester().apply {
addPermissions(
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_EXTERNAL_STORAGE,
)
requestPermissions()
}
}
}
}
@Before
fun setUp() {
mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
// clear all existing notifications
notificationShade {
mDevice.openNotification()
@ -157,13 +132,13 @@ class DownloadTest {
/* Verifies downloads in the Downloads Menu:
- downloads appear in the list
- deleting a download from device storage, removes it from the Downloads Menu too
*/
@Ignore("Failing, see: https://github.com/mozilla-mobile/fenix/issues/27220")
*/
@SmokeTest
@Test
fun manageDownloadsInDownloadsMenuTest() {
// a long filename to verify it's correctly displayed on the prompt and in the Downloads menu
downloadFile = "tAJwqaWjJsXS8AhzSninBMCfIZbHBGgcc001lx5DIdDwIcfEgQ6vE5Gb5VgAled17DFZ2A7ZDOHA0NpQPHXXFt.svg"
downloadFile =
"tAJwqaWjJsXS8AhzSninBMCfIZbHBGgcc001lx5DIdDwIcfEgQ6vE5Gb5VgAled17DFZ2A7ZDOHA0NpQPHXXFt.svg"
navigationToolbar {
}.enterURLAndEnterToBrowser(downloadTestPage.toUri()) {
@ -179,14 +154,34 @@ class DownloadTest {
waitForDownloadsListToExist()
verifyDownloadedFileName(downloadFile)
verifyDownloadedFileIcon()
openDownloadedFile(downloadFile)
verifyPhotosAppOpens()
deleteDownloadFromStorage()
waitForDownloadsListToExist()
deleteDownloadedFileOnStorage(downloadFile)
}.exitDownloadsManagerToBrowser {
}.openThreeDotMenu {
}.openDownloadsManager {
verifyEmptyDownloadsList()
}
}
@SmokeTest
@Test
fun openDownloadedFileTest() {
downloadFile = "web_icon.png"
navigationToolbar {
}.enterURLAndEnterToBrowser(downloadTestPage.toUri()) {
waitForPageToLoad()
}.clickDownloadLink(downloadFile) {
verifyDownloadPrompt(downloadFile)
}.clickDownload {
verifyDownloadNotificationPopup()
}
browserScreen {
}.openThreeDotMenu {
}.openDownloadsManager {
verifyDownloadedFileName(downloadFile)
openDownloadedFile(downloadFile)
verifyPhotosAppOpens()
mDevice.pressBack()
}
}
}