Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 35 additions & 12 deletions common/src/test/java/moe/feng/wheelplay/PluginSettingsUiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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 {
Expand All @@ -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() }
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 }
Expand All @@ -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)
Expand All @@ -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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -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) }
Expand All @@ -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 <T> await(stage: String, future: CompletableFuture<T>): T = try {
future.get(10, TimeUnit.SECONDS)
} catch (failure: TimeoutException) {
Expand Down
Loading