Loading tests/functional/com/android/documentsui/TrampolineActivityTest.kt +22 −21 Original line number Diff line number Diff line Loading @@ -15,10 +15,8 @@ */ package com.android.documentsui import android.app.Instrumentation import android.content.Intent import android.content.Intent.ACTION_GET_CONTENT import android.content.IntentFilter import android.os.Build.VERSION_CODES import android.platform.test.annotations.RequiresFlagsEnabled import android.platform.test.flag.junit.CheckFlagsRule Loading @@ -34,7 +32,6 @@ import com.android.documentsui.flags.Flags.FLAG_REDIRECT_GET_CONTENT_RO import com.android.documentsui.picker.TrampolineActivity import java.util.Optional import java.util.regex.Pattern import org.junit.After import org.junit.Assert.assertNotNull import org.junit.Before import org.junit.BeforeClass Loading @@ -56,25 +53,28 @@ class TrampolineActivityTest() { const val UI_TIMEOUT = 5000L val PHOTOPICKER_PACKAGE_REGEX: Pattern = Pattern.compile(".*(photopicker|media\\.module).*") val DOCUMENTSUI_PACKAGE_REGEX: Pattern = Pattern.compile(".*documentsui.*") val STACK_LIST_REGEX: Pattern = Pattern.compile( "taskId=(?<taskId>[0-9]+):(.+?)(photopicker|media\\.module|documentsui)", Pattern.MULTILINE ) private lateinit var device: UiDevice private lateinit var monitor: Instrumentation.ActivityMonitor fun removePhotopickerAndDocumentsUITasks() { // Get the current list of tasks that are visible. val result = device.executeShellCommand("am stack list") // Identify any that are from DocumentsUI or Photopicker and close them. val matcher = STACK_LIST_REGEX.matcher(result) while (matcher.find()) { device.executeShellCommand("am stack remove ${matcher.group("taskId")}") } } @BeforeClass @JvmStatic fun setUp() { device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) // Monitor to wait for the activity that starts with the `ACTION_GET_CONTENT` intent. val intentFilter = IntentFilter().apply { addAction(ACTION_GET_CONTENT) } monitor = Instrumentation.ActivityMonitor( intentFilter, null, // Expected result from startActivityForResult. true, // Whether to block until activity started or not. ) InstrumentationRegistry.getInstrumentation().addMonitor(monitor) } } Loading Loading @@ -157,24 +157,20 @@ class TrampolineActivityTest() { @Before fun setUp() { removePhotopickerAndDocumentsUITasks() val context = InstrumentationRegistry.getInstrumentation().targetContext val intent = Intent(ACTION_GET_CONTENT) intent.setClass(context, TrampolineActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.setType(testData.mimeType) if (testData.extraMimeTypes.isPresent) { testData.extraMimeTypes.get() .forEach { intent.putExtra(Intent.EXTRA_MIME_TYPES, it) } intent.putExtra(Intent.EXTRA_MIME_TYPES, testData.extraMimeTypes.get()) } context.startActivity(intent) } @After fun tearDown() { monitor.waitForActivityWithTimeout(UI_TIMEOUT)?.finish() } @Test fun testCorrectAppIsLaunched() { val bySelector = when (testData.expectedApp) { Loading Loading @@ -208,6 +204,11 @@ class TrampolineActivityTest() { @get:Rule val checkFlagsRule: CheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule() @Before fun setUp() { removePhotopickerAndDocumentsUITasks() } @Test fun testReferredGetContentFromPhotopickerShouldNotRedirectBack() { val context = InstrumentationRegistry.getInstrumentation().targetContext Loading Loading
tests/functional/com/android/documentsui/TrampolineActivityTest.kt +22 −21 Original line number Diff line number Diff line Loading @@ -15,10 +15,8 @@ */ package com.android.documentsui import android.app.Instrumentation import android.content.Intent import android.content.Intent.ACTION_GET_CONTENT import android.content.IntentFilter import android.os.Build.VERSION_CODES import android.platform.test.annotations.RequiresFlagsEnabled import android.platform.test.flag.junit.CheckFlagsRule Loading @@ -34,7 +32,6 @@ import com.android.documentsui.flags.Flags.FLAG_REDIRECT_GET_CONTENT_RO import com.android.documentsui.picker.TrampolineActivity import java.util.Optional import java.util.regex.Pattern import org.junit.After import org.junit.Assert.assertNotNull import org.junit.Before import org.junit.BeforeClass Loading @@ -56,25 +53,28 @@ class TrampolineActivityTest() { const val UI_TIMEOUT = 5000L val PHOTOPICKER_PACKAGE_REGEX: Pattern = Pattern.compile(".*(photopicker|media\\.module).*") val DOCUMENTSUI_PACKAGE_REGEX: Pattern = Pattern.compile(".*documentsui.*") val STACK_LIST_REGEX: Pattern = Pattern.compile( "taskId=(?<taskId>[0-9]+):(.+?)(photopicker|media\\.module|documentsui)", Pattern.MULTILINE ) private lateinit var device: UiDevice private lateinit var monitor: Instrumentation.ActivityMonitor fun removePhotopickerAndDocumentsUITasks() { // Get the current list of tasks that are visible. val result = device.executeShellCommand("am stack list") // Identify any that are from DocumentsUI or Photopicker and close them. val matcher = STACK_LIST_REGEX.matcher(result) while (matcher.find()) { device.executeShellCommand("am stack remove ${matcher.group("taskId")}") } } @BeforeClass @JvmStatic fun setUp() { device = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) // Monitor to wait for the activity that starts with the `ACTION_GET_CONTENT` intent. val intentFilter = IntentFilter().apply { addAction(ACTION_GET_CONTENT) } monitor = Instrumentation.ActivityMonitor( intentFilter, null, // Expected result from startActivityForResult. true, // Whether to block until activity started or not. ) InstrumentationRegistry.getInstrumentation().addMonitor(monitor) } } Loading Loading @@ -157,24 +157,20 @@ class TrampolineActivityTest() { @Before fun setUp() { removePhotopickerAndDocumentsUITasks() val context = InstrumentationRegistry.getInstrumentation().targetContext val intent = Intent(ACTION_GET_CONTENT) intent.setClass(context, TrampolineActivity::class.java) intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) intent.setType(testData.mimeType) if (testData.extraMimeTypes.isPresent) { testData.extraMimeTypes.get() .forEach { intent.putExtra(Intent.EXTRA_MIME_TYPES, it) } intent.putExtra(Intent.EXTRA_MIME_TYPES, testData.extraMimeTypes.get()) } context.startActivity(intent) } @After fun tearDown() { monitor.waitForActivityWithTimeout(UI_TIMEOUT)?.finish() } @Test fun testCorrectAppIsLaunched() { val bySelector = when (testData.expectedApp) { Loading Loading @@ -208,6 +204,11 @@ class TrampolineActivityTest() { @get:Rule val checkFlagsRule: CheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule() @Before fun setUp() { removePhotopickerAndDocumentsUITasks() } @Test fun testReferredGetContentFromPhotopickerShouldNotRedirectBack() { val context = InstrumentationRegistry.getInstrumentation().targetContext Loading