diff --git a/common/src/test/java/moe/feng/wheelplay/PluginSettingsUiTest.kt b/common/src/test/java/moe/feng/wheelplay/PluginSettingsUiTest.kt index 46d478c1..9a638644 100644 --- a/common/src/test/java/moe/feng/wheelplay/PluginSettingsUiTest.kt +++ b/common/src/test/java/moe/feng/wheelplay/PluginSettingsUiTest.kt @@ -28,6 +28,8 @@ import org.robolectric.Shadows.shadowOf import org.robolectric.annotation.Config import org.robolectric.annotation.GraphicsMode import org.robolectric.shadows.ShadowContentResolver +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit @RunWith(RobolectricTestRunner::class) @Config(sdk = [29], qualifiers = "en-mdpi") @@ -42,6 +44,7 @@ class PluginSettingsUiTest : ComposeSettingsTest() { var reads = 0 var remoteSupported = false var rejectRemote = false + @Volatile var afterWrite: (() -> Unit)? = null override val supportsRemoteCompose get() = remoteSupported override fun isHostAllowed(uid: Int) = true override fun readSettings(source: ComponentName): PluginSettingsSchema { @@ -53,6 +56,7 @@ class PluginSettingsUiTest : ComposeSettingsTest() { override fun writeSetting(source: ComponentName, key: String, value: Any?) { if (key == "counter") counter = value as Boolean else caption = value as String revision++ + afterWrite?.invoke() } fun externalChange() { counter = false; revision++; notifySettingsChanged() } fun externalCaptionChange() { caption = "Changed elsewhere"; revision++; notifySettingsChanged() } @@ -110,27 +114,47 @@ class PluginSettingsUiTest : ComposeSettingsTest() { shadowOf(Looper.getMainLooper()).idle() predicate() } + private fun clickEnabledText(text: String) { + compose.onNodeWithText(text).performScrollTo() + // Provider writes and observer reloads run outside Compose's idling + // resources. A visible button can still be disabled by an in-flight call. + waitFor { compose.onAllNodes(hasText(text) and isEnabled()).fetchSemanticsNodes().size == 1 } + compose.onNodeWithText(text).performClick() + } + private fun editCaption(value: String) { + clickEnabledText("Edit") + waitFor { compose.onAllNodesWithTag("plugin:setting-input").fetchSemanticsNodes().size == 1 } + compose.onNodeWithTag("plugin:setting-input").performTextReplacement(value) + } @Test fun hostActivityRendersSchemaPersistsEditsAndObservesExternalChangesWithoutLaunchingPluginActivity() { val (plugin, provider) = install() val controller = Robolectric.buildActivity(HostActivity::class.java).setup() val activity = controller.get() + val wrote = CountDownLatch(1) + val releaseWrite = CountDownLatch(1) + provider.afterWrite = { + wrote.countDown() + check(releaseWrite.await(10, TimeUnit.SECONDS)) { "Test did not release the settings write" } + } try { activity.setContent { PluginSettingsDialog(plugin, activity.lifecycle, {}, null) } waitFor { compose.onAllNodesWithTag("plugin:setting:counter:switch").fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithTag("plugin:setting:counter:switch").assertIsOff().performClick() - waitFor { provider.counter } + waitFor { wrote.count == 0L } + assertTrue(provider.counter) + compose.onNodeWithText("Edit").assertIsNotEnabled() + releaseWrite.countDown() + editCaption("Saved inside host") compose.onNodeWithTag("plugin:setting:counter:switch").assertIsOn() - compose.onNodeWithText("Edit").performScrollTo().performClick() - compose.onNodeWithTag("plugin:setting-input").performTextReplacement("Saved inside host") compose.onNodeWithText("Save").performClick() - waitFor { provider.caption == "Saved inside host" } - compose.onNodeWithText("Saved inside host").assertExists() + waitFor { compose.onAllNodesWithText("Saved inside host").fetchSemanticsNodes().size == 1 } + assertEquals("Saved inside host", provider.caption) provider.externalChange() waitFor { compose.onNodeWithTag("plugin:setting:counter:switch").fetchSemanticsNode() .config[androidx.compose.ui.semantics.SemanticsProperties.ToggleableState] == androidx.compose.ui.state.ToggleableState.Off } assertNull(shadowOf(activity).nextStartedActivity) - } finally { controller.pause().stop().destroy() } + } finally { releaseWrite.countDown(); controller.pause().stop().destroy() } } @Test fun leavingTheActivityStopsObservingAndResumeReloadsCurrentSettings() { @@ -159,8 +183,7 @@ class PluginSettingsUiTest : ComposeSettingsTest() { val activity = controller.get() activity.setContent { PluginSettingsDialog(plugin, activity.lifecycle, {}, null) } waitFor { compose.onAllNodesWithText("Edit").fetchSemanticsNodes().isNotEmpty() } - compose.onNodeWithText("Edit").performScrollTo().performClick() - compose.onNodeWithTag("plugin:setting-input").performTextReplacement("Old editor value") + editCaption("Old editor value") val reads = provider.reads provider.externalCaptionChange() waitFor { provider.reads > reads } @@ -179,13 +202,13 @@ class PluginSettingsUiTest : ComposeSettingsTest() { val activity = controller.get() activity.setContent { PluginSettingsDialog(plugin, activity.lifecycle, {}, null) } waitFor { compose.onAllNodesWithText("Plugin layout").fetchSemanticsNodes().isNotEmpty() } - compose.onNodeWithText("Plugin layout").performClick() + clickEnabledText("Plugin layout") waitFor { compose.onAllNodesWithTag("plugin:remote-compose").fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithTag("plugin:remote-compose").performScrollTo().performTouchInput { click(center) } waitFor { provider.counter } - compose.onNodeWithText("Standard settings").performScrollTo().performClick() + clickEnabledText("Standard settings") waitFor { compose.onAllNodesWithTag("plugin:setting:counter:switch").fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithTag("plugin:setting:counter:switch").assertIsOn() assertNull(shadowOf(activity).nextStartedActivity) @@ -201,9 +224,9 @@ class PluginSettingsUiTest : ComposeSettingsTest() { val activity = controller.get() activity.setContent { PluginSettingsDialog(plugin, activity.lifecycle, {}, null) } waitFor { compose.onAllNodesWithText("Plugin layout").fetchSemanticsNodes().isNotEmpty() } - compose.onNodeWithText("Plugin layout").performClick() + clickEnabledText("Plugin layout") waitFor { compose.onAllNodesWithText("Remote layout unavailable").fetchSemanticsNodes().isNotEmpty() } - compose.onNodeWithText("Standard settings").performClick() + clickEnabledText("Standard settings") waitFor { compose.onAllNodesWithTag("plugin:setting:counter:switch").fetchSemanticsNodes().isNotEmpty() } compose.onNodeWithTag("plugin:setting:counter:switch").assertIsOff() } finally { controller.pause().stop().destroy() } diff --git a/mobile/src/androidTest/java/moe/feng/wheelplay/plugin/PluginSurfaceIntegrationTest.kt b/mobile/src/androidTest/java/moe/feng/wheelplay/plugin/PluginSurfaceIntegrationTest.kt index 264f3707..0c095efc 100644 --- a/mobile/src/androidTest/java/moe/feng/wheelplay/plugin/PluginSurfaceIntegrationTest.kt +++ b/mobile/src/androidTest/java/moe/feng/wheelplay/plugin/PluginSurfaceIntegrationTest.kt @@ -5,6 +5,7 @@ import android.graphics.PixelFormat import android.media.Image import android.media.ImageReader import android.os.* +import android.util.Log import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import java.util.UUID @@ -24,7 +25,10 @@ class PluginSurfaceIntegrationTest { Intent(instrumentation.targetContext, PluginTestHostActivity::class.java) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), ) - try { repeat(2) { runSession() } } + try { repeat(2) { index -> + Log.i("PluginSurfaceTest", "Starting session ${index + 1}") + runSession() + } } finally { instrumentation.runOnMainSync { activity.finish() } } } private fun runSession() { @@ -133,6 +137,7 @@ class PluginSurfaceIntegrationTest { assertTrue(await("cleanup acknowledgement", stopped)) context.unbindService(connection); bound = false assertTrue("Dedicated plugin process did not exit", exited.await(10, TimeUnit.SECONDS)) + awaitSystemTeardown(component.packageName, service.processName) } finally { if (!stopped.isDone) { runCatching { remote?.stop(token); stopped.get(5, TimeUnit.SECONDS) } @@ -149,6 +154,31 @@ class PluginSurfaceIntegrationTest { } } + private fun awaitSystemTeardown(packageName: String, processName: String) { + val instrumentation = InstrumentationRegistry.getInstrumentation() + val deadline = SystemClock.uptimeMillis() + 10_000 + fun dump(kind: String): String = ParcelFileDescriptor.AutoCloseInputStream( + instrumentation.uiAutomation.executeShellCommand("dumpsys activity $kind $packageName"), + ).bufferedReader().use { it.readText() }.also { + check(it.startsWith("ACTIVITY MANAGER")) { "Cannot inspect plugin $kind teardown: $it" } + } + var services: String + var processes: String + do { + services = dump("services") + processes = dump("processes") + // Binder death can reach this test before ActivityManager handles the + // old process's death. Rebinding then targets the dying service and + // Android 16 defers its restart for up to 120 seconds. + val registeredProcess = processes.lineSequence().any { + it.contains("ProcessRecord{") && it.contains(":$processName/") + } + if (!services.contains("ServiceRecord{") && !registeredProcess) return + SystemClock.sleep(50) + } while (SystemClock.uptimeMillis() < deadline) + fail("Plugin remained registered after process exit:\n$services\n$processes") + } + private fun await(stage: String, future: CompletableFuture): T = try { future.get(10, TimeUnit.SECONDS) } catch (failure: TimeoutException) {