diff --git a/.github/workflows/android.yml b/.github/workflows/android.yml index 600068034..426c6e2d9 100644 --- a/.github/workflows/android.yml +++ b/.github/workflows/android.yml @@ -2,6 +2,20 @@ name: Android Release CI on: workflow_dispatch: + pull_request: + paths: + - '.github/workflows/android.yml' + - 'build.gradle' + - 'settings.gradle' + - 'gradle.properties' + - 'gradle/**' + - 'app/build.gradle' + - 'app/src/main/assets/**' + - 'app/src/main/java/com/kazumaproject/markdownhelperkeyboard/converter/compact/**' + - 'app/src/main/java/com/kazumaproject/markdownhelperkeyboard/dictionary_override/**' + - 'app/src/test/java/com/kazumaproject/markdownhelperkeyboard/converter/compact/**' + - 'tools/release/**' + - 'tools/classified-dictionaries/**' push: tags: - 'v*' @@ -15,6 +29,7 @@ concurrency: jobs: build: + if: github.event_name != 'pull_request' runs-on: ubuntu-latest env: ZENZ_MODEL_CACHE_DIR: ${{ github.workspace }}/.zenz-model-cache @@ -92,6 +107,7 @@ jobs: --stacktrace \ --no-daemon \ --max-workers=1 + python3 tools/release/verify-lite-apk.py app/build/outputs/apk/liteStandard/release/*.apk - name: Build and verify Play bundle native symbols env: @@ -147,7 +163,7 @@ jobs: run: sdkmanager "platforms;android-37.0" "build-tools;36.0.0" "ndk;29.0.14206865" - name: Verify release source guard - run: python3 -m unittest discover -s tools/release -p 'test_verify_release_source.py' + run: python3 -m unittest discover -s tools/release - name: Build unsigned F-Droid APK without restored caches env: @@ -161,6 +177,17 @@ jobs: --stacktrace --no-daemon --max-workers=1 python3 tools/release/verify-release-source.py --revision "$GITHUB_SHA" \ app/build/outputs/apk/liteFdroid/release/app-liteFdroid-release.apk + python3 tools/release/verify-lite-apk.py \ + app/build/outputs/apk/liteFdroid/release/app-liteFdroid-release.apk + + - name: Test English dictionary decoding + env: + GRADLE_USER_HOME: ${{ runner.temp }}/fdroid-gradle-${{ matrix.build }} + run: | + ./gradlew :app:testLiteFdroidDebugUnitTest \ + --tests '*BundledEnglishDictionaryReaderTest' \ + --tests '*MappedDictionaryAssetCacheTest' \ + --no-daemon --max-workers=1 - name: Upload unsigned F-Droid APK for comparison uses: actions/upload-artifact@v4 @@ -169,8 +196,24 @@ jobs: if-no-files-found: error path: app/build/outputs/apk/liteFdroid/release/app-liteFdroid-release.apk + verify-fdroid: + needs: fdroid + runs-on: ubuntu-latest + steps: + - uses: actions/download-artifact@v4 + with: + name: fdroid-unsigned-first-${{ github.sha }} + path: fdroid/first + - uses: actions/download-artifact@v4 + with: + name: fdroid-unsigned-second-${{ github.sha }} + path: fdroid/second + - name: Require identical clean F-Droid builds + run: cmp fdroid/first/app-liteFdroid-release.apk fdroid/second/app-liteFdroid-release.apk + publish: - needs: [build, fdroid] + if: github.event_name != 'pull_request' + needs: [build, fdroid, verify-fdroid] runs-on: ubuntu-latest steps: - name: Checkout verification tools from the workflow commit diff --git a/app/build.gradle b/app/build.gradle index d8473a4ed..8cf466a90 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -5,6 +5,11 @@ import org.gradle.process.ExecOperations import com.android.build.api.artifact.SingleArtifact import com.android.build.gradle.internal.tasks.ExtractNativeDebugMetadataTask +buildscript { + repositories { mavenCentral() } + dependencies { classpath 'org.tukaani:xz:1.12' } +} + plugins { id 'com.android.application' id 'com.google.dagger.hilt.android' @@ -41,8 +46,8 @@ android { applicationId "com.kazumaproject.markdownhelperkeyboard" minSdk 24 targetSdk 37 - versionCode 847 - versionName "1.7.150" + versionCode 870 + versionName "1.7.159" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -151,9 +156,10 @@ android { } androidResources { - // These assets are already compact binary images. Keeping them stored lets - // Android read them directly instead of inflating them on the IME's cold path. - noCompress += ['kdict', 'compact', 'index', 'erol'] + // Debug assets remain directly mappable. Releases replace these six + // dictionaries with XZ streams and map their verified private copies. + noCompress += ['kdict', 'reading.compact', 'word.compact', 'token.compact', + 'english.lookup.index', 'erol', 'xz'] } sourceSets { @@ -213,9 +219,6 @@ abstract class GenerateDictionaryLicenseAssets extends DefaultTask { } abstract class PackageRuntimeDictionaryAssets extends DefaultTask { - @Input - abstract Property getStandardChannel() - @InputDirectory @PathSensitive(PathSensitivity.RELATIVE) abstract DirectoryProperty getMergedAssets() @@ -232,10 +235,28 @@ abstract class PackageRuntimeDictionaryAssets extends DefaultTask { from(mergedAssets) into(packagedAssets) exclude('english/reading.dat.zip', 'english/word.dat', 'english/token.dat.zip') - if (standardChannel.get()) { - exclude('system/tango.dat.zip', 'system/yomi.dat.zip', 'system/token.dat.zip') + exclude('system/tango.dat.zip', 'system/yomi.dat.zip', 'system/token.dat.zip') + } + def records = [:] + ['system/system.compact.kdict', 'english/reading.compact', 'english/word.compact', + 'english/token.compact', 'english/english.lookup.index', 'english/grammar/categories.erol'].each { path -> + def source = new File(packagedAssets.get().asFile, path) + def digest = java.security.MessageDigest.getInstance('SHA-256') + source.withInputStream { input -> + new java.security.DigestInputStream(input, digest).withCloseable { hashed -> + new File(source.path + '.xz').withOutputStream { destination -> + new org.tukaani.xz.XZOutputStream(destination, + new org.tukaani.xz.LZMA2Options(6), org.tukaani.xz.XZ.CHECK_CRC64).withCloseable { compressed -> + hashed.transferTo(compressed) + } + } + } } + records[path] = [size: source.length(), sha256: digest.digest().encodeHex().toString()] + if (!source.delete()) throw new GradleException("Could not remove uncompressed dictionary: $path") } + new File(packagedAssets.get().asFile, 'dictionary-assets.json').text = + groovy.json.JsonOutput.toJson([version: 1, assets: records]) + '\n' } } @@ -279,9 +300,7 @@ androidComponents { } variant.sources.assets.addGeneratedSourceDirectory(licenses) { it.outputDirectory } def compactAssets = tasks.register("package${variant.name.capitalize()}RuntimeDictionaryAssets", - PackageRuntimeDictionaryAssets) { - standardChannel.set(variant.productFlavors.any { it.first == 'channel' && it.second == 'standard' }) - } + PackageRuntimeDictionaryAssets) variant.artifacts.use(compactAssets) .wiredWithDirectories({ it.mergedAssets }, { it.packagedAssets }) .toTransform(SingleArtifact.ASSETS.INSTANCE) @@ -757,19 +776,41 @@ tasks.register("generateCompactSystemDictionary") { write(output) } } - writeEntries("reading.compact", 0x4B455231) { output -> // KER1 + // English tries use a small alphabet. Store one-byte alphabet indices, + // then restore the same CharArray at load time. Larger alphabets keep UTF-16. + def writeLabels = { DataOutputStream output, char[] labels -> + char[] alphabet = new String(labels).chars().distinct().sorted().toArray() + .collect { (char) it } as char[] + if (alphabet.length == 0 || alphabet.length > 256) { + output.writeInt(0) + writeArray(output, labels.length, Character.BYTES) { it.asCharBuffer().put(labels) } + } else { + writeArray(output, alphabet.length, Character.BYTES) { it.asCharBuffer().put(alphabet) } + byte[] indexByChar = new byte[Character.MAX_VALUE + 1] + for (int index = 0; index < alphabet.length; index++) { + indexByChar[(int) alphabet[index]] = (byte) index + } + byte[] indices = new byte[labels.length] + for (int index = 0; index < labels.length; index++) { + indices[index] = indexByChar[(int) labels[index]] + } + output.writeInt(indices.length) + output.write(indices) + } + } + writeEntries("reading.compact", 0x4B455232) { output -> // KER2 writeBitSet(output, englishReading[0] as BitSet) writeBitSet(output, englishReading[1] as BitSet) char[] labels = englishReading[2] as char[] int[] termIds = englishReading[3] as int[] - writeArray(output, labels.length, Character.BYTES) { it.asCharBuffer().put(labels) } + writeLabels(output, labels) writeArray(output, termIds.length, Integer.BYTES) { it.asIntBuffer().put(termIds) } } - writeEntries("word.compact", 0x4B455731) { output -> // KEW1 + writeEntries("word.compact", 0x4B455732) { output -> // KEW2 writeBitSet(output, englishWord[0] as BitSet) writeBitSet(output, englishWord[1] as BitSet) char[] labels = englishWord[2] as char[] - writeArray(output, labels.length, Character.BYTES) { it.asCharBuffer().put(labels) } + writeLabels(output, labels) } writeEntries("token.compact", 0x4B455431) { output -> // KET1 short[] costs = englishToken[0] as short[] @@ -1325,8 +1366,10 @@ tasks.named("preBuild").configure { } dependencies { + implementation 'org.tukaani:xz:1.12' implementation 'androidx.profileinstaller:profileinstaller:1.4.0' implementation 'androidx.appcompat:appcompat:1.7.1' + implementation 'androidx.drawerlayout:drawerlayout:1.2.0' implementation 'com.google.android.material:material:1.13.0' implementation 'androidx.constraintlayout:constraintlayout:2.2.1' diff --git a/app/src/androidTest/assets/classified-expected-records.json b/app/src/androidTest/assets/classified-expected-records.json index 2f22678f8..fb95da5fa 100644 --- a/app/src/androidTest/assets/classified-expected-records.json +++ b/app/src/androidTest/assets/classified-expected-records.json @@ -1,7 +1,7 @@ { "character": { - "count": 6856, - "digestSum": "be07c424f9c2cc08ded6919fd1aa9404ccddcbea24bca37837a0adbddf80fa50", + "count": 6858, + "digestSum": "fc4581d9765393f138a914cb69d2a44da8150b29088dd9262d665efc7053410a", "sample": { "reading": "あかざあかね", "surface": "赤座あかね" @@ -16,8 +16,8 @@ } }, "place": { - "count": 45092, - "digestSum": "e15e70b67821958b31c54cfcf281a1bd16c1981f6a54bc707785a3773cc3ca54", + "count": 45094, + "digestSum": "5720f1e2bba0d20eed0eacaa2567a6c17d84b187db030ad5228a8f0f9fb9433c", "sample": { "reading": "かろけん", "surface": "カロ県" @@ -168,8 +168,8 @@ } }, "facility/cultural/art_museum": { - "count": 380, - "digestSum": "70ebfcf76581fa3e1875159a46b6c7d026cf40b7ce3b345789aa0c0425673507", + "count": 381, + "digestSum": "d83ebc986b398d0d48c315e6015821ffa74f73af5ce302961787149fb0a442d8", "sample": { "reading": "えろーら", "surface": "エローラ" @@ -264,8 +264,8 @@ } }, "facility/commercial/shopping": { - "count": 1302, - "digestSum": "413b26d26580eb100db6b03eb7adfa84a93003f859eb34870f1e0cc7731b9830", + "count": 1303, + "digestSum": "7d5f045abac378e6a9e53bf494e3d03176658020fd06fb64d510e33e36a5b39e", "sample": { "reading": "きくのま", "surface": "菊の間" @@ -480,8 +480,8 @@ } }, "facility/residential/mixed_use_building": { - "count": 95, - "digestSum": "febb7e1db2cf3a44073242291760945953fd8197f239fdeef7e6cdf778228088", + "count": 98, + "digestSum": "fdef80a05f6e6f8483e32dc0b1951ef9e890cf5df1fffd46691d3994d2847296", "sample": { "reading": "くずだて", "surface": "くず館" @@ -520,11 +520,11 @@ } }, "organization": { - "count": 47519, - "digestSum": "92d411144249a96ae3dcdea4202864c08f44e517c9d7e3fb1621357327fe76cc", + "count": 47525, + "digestSum": "7fa3ca53fdd96912966e37cb79f626ef2e13eef7e187f5556b4baced5bf7ce1a", "sample": { - "reading": "のべるず", - "surface": "NOVELS" + "reading": "ねいばー", + "surface": "NAVER" } }, "lineage": { @@ -536,11 +536,11 @@ } }, "product": { - "count": 10198, - "digestSum": "73d4f52ad45b6de51a0bbf3775bc43871f70161635d4cadded1dbd1ee5cd5068", + "count": 10220, + "digestSum": "992fb6570be8ae6b4d03c837224dbedae1e1e29bedafc90e17869818e0050368", "sample": { - "reading": "ぽたぽたやき", - "surface": "ぽたぽた焼" + "reading": "のるもなーる", + "surface": "ノルモナール" } }, "vehicle": { @@ -640,8 +640,8 @@ } }, "term/general/internet_slang_culture": { - "count": 579, - "digestSum": "2d72b330ba4cbb9e9d86493309e6ab225430837409730c469c974cc41b81dd71", + "count": 580, + "digestSum": "c07ee990699feb7bf1b8998267bb977e574b49e4c601114a48eb002fca3b0547", "sample": { "reading": "あちぇけ", "surface": "アチェケ" @@ -664,8 +664,8 @@ } }, "term/specialist/information_communications": { - "count": 6092, - "digestSum": "936236ed6ceb0618e84226089f9a1e0f450c32734e6fae5a95e09eaa15aaf0fe", + "count": 6095, + "digestSum": "f7e74bf6d72abad24857bf3f7ba0f9e61db8ed09ffd5962dbcd66ddcd484d49f", "sample": { "reading": "あいじつり", "surface": "I字吊り" @@ -680,8 +680,8 @@ } }, "term/specialist/chemistry_life_sciences": { - "count": 27574, - "digestSum": "61ad416a3b8175956e3010ccbfa36b12580ac6fa89dabb6263286b27e845a6fa", + "count": 27577, + "digestSum": "2b85df7624e290099e73471efecf95b5de1c427f6819693986cfbab7f5dbfea6", "sample": { "reading": "あかひめ", "surface": "赤姫" @@ -696,24 +696,24 @@ } }, "term/specialist/earth_environment_resources": { - "count": 2117, - "digestSum": "fa6b1f47b38201e242614c5fa9e5b5b9b8c0beb8b4a646c9f9a805587356219f", + "count": 2118, + "digestSum": "49d8a49744254ef19a98abbcc2c45a22b2dd422dc3ccfa33bda72ebd8d29d4bf", "sample": { "reading": "あなはぜ", "surface": "孔鯊" } }, "term/specialist/economics_management": { - "count": 5911, - "digestSum": "14de6abf1880d80f1b0111ad7465cd5022d83792ca2da542953837fe6be88c26", + "count": 5913, + "digestSum": "a17908a937bdf3c5ac6b2101d156f8ede9625c04c57504ebd6ce52d12e782c2d", "sample": { "reading": "あいずごよう", "surface": "会津五葉" } }, "term/specialist/law_politics": { - "count": 5721, - "digestSum": "c7d43ce7dc4dca4554beb80ea969e0b6e0d89865322e3419ad1c5b56290d1b14", + "count": 5726, + "digestSum": "8cf8c7df6a73d1194de8ec9bb62946a986360dd6a7cc697763609ca1f5e435f9", "sample": { "reading": "ぎいんせき", "surface": "議院石" @@ -739,8 +739,8 @@ "count": 4064, "digestSum": "55e4a19ecb5964645e36c7ee9b64b093d3048756ff96c762dbd6d22e7001d525", "sample": { - "reading": "あこうぎし", - "surface": "赤穂義士" + "reading": "さんばしん", + "surface": "3馬身" } }, "work_franchise": { @@ -760,8 +760,8 @@ } }, "work_anime": { - "count": 1093, - "digestSum": "51932e57b9ca1533ac3127dbaace118dd1248268175c50dd52646609c5d795b3", + "count": 1094, + "digestSum": "a2bf592ddc66c5114b54fa0d079f1df8c5a83c9bcce606923033661a9ecd633c", "sample": { "reading": "あいどるじへん", "surface": "アイドル事変" @@ -816,8 +816,8 @@ } }, "media_demographic/audience/unspecified": { - "count": 5106, - "digestSum": "d10f124b26833f7ddf6c6efb37e4a42e79a28776b668a0e604e3a2c7e2abdc80", + "count": 5109, + "digestSum": "6078fad5c5b0b743d862c4582cddc0e2495d80e94fc38c48d87854174c73f0c3", "sample": { "reading": "あいばゆみ", "surface": "相葉夕美" @@ -856,16 +856,16 @@ } }, "work_game": { - "count": 3179, - "digestSum": "43b1d3a1826a67c9317f056cfacde28e2aa43258c65a7c0f09f2c2e6bbfcbfde", + "count": 3182, + "digestSum": "e60c2fb3c16fbc57ae1f6cfa4984e79b2d8aaa861f20e692011a319c6208d5d4", "sample": { "reading": "あじとつー", "surface": "AZITO2" } }, "work_music": { - "count": 10353, - "digestSum": "aed8aaeaa39884260d1d72cafb5c48e29b87844c89cf74ad5291d9dbea2198b6", + "count": 10355, + "digestSum": "ed9f4733278cee0bdec3caf8eb5d45ec2ee17bba27a410ba79cf24859c66a4ed", "sample": { "reading": "あいをくれ", "surface": "アイヲクレ" diff --git a/app/src/androidTest/assets/english-reading-expected.json b/app/src/androidTest/assets/english-reading-expected.json index 1df7daf7d..196b71940 100644 --- a/app/src/androidTest/assets/english-reading-expected.json +++ b/app/src/androidTest/assets/english-reading-expected.json @@ -1,5 +1,5 @@ { - "revision": "f14e2793ef07c93f0d7136f14bbd5270eb3e0c07", + "revision": "040b4d7bad800eb63ad589e7cf5c25e605d18d13", "catalogSha256": "dd49edb7e2e106ad683426e3fe97f538fd4d8be5fddedbe0169e8280311c694e", "count": 5138, "digestSum": "f668176b3c3ede6b135bfa454ebd5a4f56405c8f26f3b200999a1af3121fd5c2" diff --git a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/CustomFreeFlickKeyDeviceTest.kt b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/CustomFreeFlickKeyDeviceTest.kt new file mode 100644 index 000000000..45d22bd51 --- /dev/null +++ b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/CustomFreeFlickKeyDeviceTest.kt @@ -0,0 +1,230 @@ +package com.kazumaproject.markdownhelperkeyboard + +import android.content.Intent +import android.graphics.Rect +import android.os.Bundle +import android.os.ParcelFileDescriptor +import android.os.SystemClock +import android.view.InputDevice +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.view.inspector.WindowInspector +import android.widget.TextView +import androidx.navigation.fragment.NavHostFragment +import androidx.preference.PreferenceManager +import androidx.test.core.app.ActivityScenario +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.SdkSuppress +import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry +import androidx.test.runner.lifecycle.Stage +import androidx.test.platform.app.InstrumentationRegistry +import com.google.android.material.button.MaterialButton +import com.google.android.material.textfield.TextInputLayout +import com.kazumaproject.custom_keyboard.data.* +import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.ui.KeyboardEditorFragment +import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.ui.KeyEditorFragment +import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.ui.CustomFreeFlickEditorDialog +import com.kazumaproject.markdownhelperkeyboard.sumire_popup.PopupMapEditorFragment +import com.kazumaproject.markdownhelperkeyboard.sumire_popup.FreeFlickPreferences +import com.kazumaproject.markdownhelperkeyboard.setting_activity.MainActivity +import com.kazumaproject.custom_keyboard.view.FlickKeyboardView +import com.kazumaproject.markdownhelperkeyboard.ime_service.di.AppModule +import com.kazumaproject.markdownhelperkeyboard.repository.KeyboardRepository +import kotlinx.coroutines.flow.first +import kotlinx.coroutines.runBlocking +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith + +/** Exercises production editing and IME input in an isolated install. */ +@RunWith(AndroidJUnit4::class) +@SdkSuppress(minSdkVersion = 29) +class CustomFreeFlickKeyDeviceTest { + private val ins = InstrumentationRegistry.getInstrumentation() + private fun shell(command: String) = ParcelFileDescriptor.AutoCloseInputStream( + ins.uiAutomation.executeShellCommand(command)).bufferedReader().use { it.readText().trim() } + private fun main(block: () -> Unit) = ins.runOnMainSync(block) + private fun descendants(view: View): Sequence = sequence { + yield(view) + if (view is ViewGroup) for (i in 0 until view.childCount) yieldAll(descendants(view.getChildAt(i))) + } + private fun views() = WindowInspector.getGlobalWindowViews().asSequence().flatMap(::descendants) + .filter { it.isAttachedToWindow && it.isShown && it.width > 0 && it.height > 0 && + generateSequence(it as View?) { parent -> parent.parent as? View }.all { parent -> parent.alpha > 0.9f } && + it.getGlobalVisibleRect(Rect()) } + private fun await(message: String, predicate: () -> Boolean) { + val deadline = SystemClock.uptimeMillis() + 15000 + while (SystemClock.uptimeMillis() < deadline) { + var ready = false + main { ready = predicate() } + if (ready) return + SystemClock.sleep(50) + } + var details = "" + main { + details = views().filterIsInstance().joinToString { "surface=${it.id}/${it.width}x${it.height}" } + } + error("$message: $details") + } + private fun key(label: String): Rect { + var result: Rect? = null + await("Missing key $label") { + result = views().filterIsInstance().flatMap(::descendants) + .firstOrNull { it.isShown && ((it as? TextView)?.text?.toString() == label || it.contentDescription == label) } + ?.let { view -> IntArray(2).also(view::getLocationOnScreen).let { Rect(it[0], it[1], it[0] + view.width, it[1] + view.height) } } + result != null + } + return result!! + } + private var downTime = 0L + private fun send(action: Int, x: Float, y: Float) { + if (action == MotionEvent.ACTION_DOWN) downTime = SystemClock.uptimeMillis() + val event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), action, x, y, 0) + event.source = InputDevice.SOURCE_TOUCHSCREEN + try { check(ins.uiAutomation.injectInputEvent(event, true)) } finally { event.recycle() } + } + private fun tap(label: String) { + val bounds = key(label) + send(MotionEvent.ACTION_DOWN, bounds.exactCenterX(), bounds.exactCenterY()) + SystemClock.sleep(40) + send(MotionEvent.ACTION_UP, bounds.exactCenterX(), bounds.exactCenterY()) + SystemClock.sleep(120) + } + @Test fun editorConvertsKeyAndRestoresTheMapDraftAcrossRecreation() = runBlocking { + val context = ins.targetContext + check(context.packageName.endsWith(".customfreeflicktest")) + val db = AppModule.providesLearnDatabase(context) + val repository = KeyboardRepository(db.keyboardLayoutDao()) + val original = KeyData("Old", 0, 0, false, KeyAction.Text("a"), keyId = "editor-key") + val id = repository.saveLayout(KeyboardLayout(listOf(original), emptyMap(), 1, 1), "Free Flick editor verification", null) + try { + ActivityScenario.launch(Intent(context, MainActivity::class.java)).use { scenario -> + scenario.onActivity { it.window.addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) } + fun host(): NavHostFragment = ActivityLifecycleMonitorRegistry.getInstance() + .getActivitiesInStage(Stage.RESUMED).filterIsInstance().first() + .supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment + fun keyEditor() = host().childFragmentManager.primaryNavigationFragment as KeyEditorFragment + fun mapEditor(): PopupMapEditorFragment? { + val dialog = keyEditor().childFragmentManager.findFragmentByTag("freeFlickEditor") as? CustomFreeFlickEditorDialog + return dialog?.childFragmentManager?.findFragmentById(R.id.custom_free_flick_editor_host) as? PopupMapEditorFragment + } + await("Settings initialized") { runCatching { host().navController.currentDestination != null }.getOrDefault(false) } + main { host().navController.navigate(R.id.keyboardEditorFragment, Bundle().apply { putLong("layoutId", id) }) } + await("Layout editor loaded") { + val fragment = host().childFragmentManager.primaryNavigationFragment as? KeyboardEditorFragment + fragment?.view?.let { descendants(it).filterIsInstance().any { text -> text.text.toString() == "Old" } } == true + } + main { + val editor = host().childFragmentManager.primaryNavigationFragment as KeyboardEditorFragment + editor.onKeySelected("editor-key"); editor.onKeySelected("editor-key") + } + await("Key editor initialized") { runCatching { keyEditor().view?.findViewById(R.id.button_done)?.isEnabled == true }.getOrDefault(false) } + main { + keyEditor().requireView().findViewById(R.id.chip_free_flick).performClick() + keyEditor().requireView().findViewById(R.id.edit_free_flick_map).performClick() + } + await("Map editor opened") { runCatching { mapEditor()?.view != null }.getOrDefault(false) } + main { + descendants(mapEditor()!!.requireView()).filterIsInstance() + .single { it.hint == context.getString(R.string.popup_input) }.editText!!.setText("edited😀") + } + scenario.recreate() + await("Map draft restored") { runCatching { + descendants(mapEditor()!!.requireView()).filterIsInstance() + .any { it.hint == context.getString(R.string.popup_input) && it.editText?.text.toString() == "edited😀" } + }.getOrDefault(false) } + main { + descendants(mapEditor()!!.requireView()).filterIsInstance() + .single { it.text == context.getString(R.string.popup_apply) }.performClick() + } + await("Map applied to key draft") { keyEditor().childFragmentManager.findFragmentByTag("freeFlickEditor") == null } + scenario.recreate() + await("Key type restored") { runCatching { + keyEditor().requireView().findViewById(R.id.chip_free_flick).isChecked && + keyEditor().requireView().findViewById(R.id.button_done).isEnabled + }.getOrDefault(false) } + main { keyEditor().requireView().findViewById(R.id.button_done).performClick() } + await("Returned to layout editor") { host().childFragmentManager.primaryNavigationFragment is KeyboardEditorFragment } + await("Save action ready") { views().any { it.id == R.id.action_save } } + main { views().first { it.id == R.id.action_save }.performClick() } + val deadline = SystemClock.uptimeMillis() + 10000 + var saved: KeyboardLayout + do { + saved = repository.getFullLayout(id).first() + if (saved.keys.single().keyType == KeyType.FREE_FLICK) break + SystemClock.sleep(100) + } while (SystemClock.uptimeMillis() < deadline) + val key = saved.keys.single() + assertEquals(KeyType.FREE_FLICK, key.keyType) + assertEquals("edited😀", key.popupMapConfig!!.map(key.popupMapConfig!!.startMapId)!!.tapText) + assertEquals("editor-key", key.keyId) + } + } finally { repository.deleteLayoutConfirmed(id); db.close() } + } + + @Test fun actualCustomImeCommitsTapTextAndMapTransitionsInBothPresentations() = runBlocking { + val context = ins.targetContext + check(context.packageName.endsWith(".customfreeflicktest")) + val ime = "${context.packageName}/com.kazumaproject.markdownhelperkeyboard.ime_service.IMEService" + val oldIme = shell("settings get secure default_input_method") + val wasEnabled = shell("ime list -s").lines().contains(ime) + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + val db = AppModule.providesLearnDatabase(context) + val repository = KeyboardRepository(db.keyboardLayoutDao()) + val config = PopupKeyConfig("free", "base", listOf( + PopupMap("base", "Base", "tap😀", listOf( + PopupRegion("text", PopupBounds(-1.5f, -.5f, -.5f, .5f), PopupAction(text = "left")), + PopupRegion("switch", PopupBounds(.5f, -.5f, 1.5f, .5f), PopupAction(PopupActionType.SWITCH, targetMapId = "next")))), + PopupMap("next", "Next", "nextかな", listOf( + PopupRegion("back", PopupBounds(-.5f, -1.5f, .5f, -.5f), PopupAction(PopupActionType.BACK)))))) + val keys = (0 until 6).map { index -> + if (index == 4) KeyData("FREE", 1, 1, true, KeyAction.Text("tap😀"), keyType = KeyType.FREE_FLICK, + keyId = "free", popupMapConfig = config) + else KeyData("P$index", index / 3, index % 3, false, KeyAction.Text("p"), keyId = "plain$index") + } + val id = repository.saveLayout(KeyboardLayout(keys, emptyMap(), 3, 2), "Free Flick input verification", null) + try { + val stableId = repository.getLayoutsNotFlow().single { it.layoutId == id }.stableId + shell("ime enable $ime"); shell("ime set $ime") + for (presentation in PopupMapPresentation.entries) { + check(prefs.edit().clear().putString("keyboard_order_preference", "[\"CUSTOM\"]") + .putBoolean("save_last_used_keyboard", false).putBoolean("keyboard_floating_preference", false) + .putBoolean("remember_last_custom_keyboard_preference", true).putString("last_used_custom_keyboard_stable_id", stableId) + .putBoolean("live_conversion_preference", false).putBoolean("clipboard_preview_enable_preference", false) + .putBoolean(FreeFlickPreferences.NEAREST, false).putString(FreeFlickPreferences.PRESENTATION, presentation.preferenceValue).commit()) + ActivityScenario.launch(Intent(context, FastInputHostActivity::class.java)).use { scenario -> + scenario.onActivity { it.window.addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) } + await("Custom keyboard visible") { views().filterIsInstance().any { it.text == "FREE" } } + SystemClock.sleep(500) + fun gesture(vararg points: Pair, cancel: Boolean = false) { + val bounds = key("FREE") + send(MotionEvent.ACTION_DOWN, bounds.exactCenterX(), bounds.exactCenterY()) + points.forEach { (x, y) -> send(MotionEvent.ACTION_MOVE, + bounds.exactCenterX() + x * bounds.width(), bounds.exactCenterY() + y * bounds.height()); SystemClock.sleep(40) } + val last = points.lastOrNull() ?: (0f to 0f) + send(if (cancel) MotionEvent.ACTION_CANCEL else MotionEvent.ACTION_UP, + bounds.exactCenterX() + last.first * bounds.width(), bounds.exactCenterY() + last.second * bounds.height()) + } + var expected = "" + fun text(value: String) { + expected += value + await("IME output $expected") { + var actual = ""; scenario.onActivity { actual = it.editText.text.toString() }; actual == expected + } + } + gesture(); text("tap😀") + gesture(-1f to 0f); text("left") + gesture(1f to 0f, 0f to 0f); text("nextかな") + gesture(1f to 0f, 0f to -1f, 0f to 0f); text("tap😀") + gesture(-1f to 0f, cancel = true); text("") + } + } + } finally { + repository.deleteLayoutConfirmed(id) + if (oldIme.isNotEmpty() && oldIme != "null") shell("ime set $oldIme") + if (!wasEnabled) shell("ime disable $ime") + db.close() + } + } +} diff --git a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/CustomQwertyKeyDeviceTest.kt b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/CustomQwertyKeyDeviceTest.kt index 8dc098207..520506f05 100644 --- a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/CustomQwertyKeyDeviceTest.kt +++ b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/CustomQwertyKeyDeviceTest.kt @@ -19,6 +19,8 @@ import androidx.test.filters.SdkSuppress import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry import androidx.test.runner.lifecycle.Stage import androidx.test.platform.app.InstrumentationRegistry +import com.google.android.material.button.MaterialButton +import com.google.android.material.textfield.TextInputLayout import com.kazumaproject.core.domain.skin.KeyboardSkinId import com.kazumaproject.custom_keyboard.data.* import com.kazumaproject.markdownhelperkeyboard.custom_keyboard.ui.KeyboardEditorFragment @@ -296,7 +298,25 @@ class CustomQwertyKeyDeviceTest { main { val fragment = host().childFragmentManager.primaryNavigationFragment as KeyEditorFragment fragment.requireView().findViewById(R.id.chip_qwerty).performClick() - fragment.requireView().findViewById(R.id.qwerty_key_editor).setConfig(expected) + val editor = fragment.requireView().findViewById(R.id.qwerty_key_editor) + fun click(text: Int) = descendants(editor).filterIsInstance() + .first { it.text == context.getString(text) }.performClick() + fun edit(hint: Int, value: String) = descendants(editor).filterIsInstance() + .first { it.hint == context.getString(hint) }.editText!!.setText(value) + click(R.string.qwerty_edit_key) + edit(R.string.qwerty_tap_output, "edited") + edit(R.string.qwerty_key_label, "Key label") + edit(R.string.qwerty_preview_label, "Preview") + edit(R.string.qwerty_columns, "2") + click(R.string.qwerty_add_slot) + edit(R.string.qwerty_candidate_output, "one") + edit(R.string.qwerty_candidate_label, "First") + click(R.string.qwerty_add_slot) + click(R.string.qwerty_add_slot) + edit(R.string.qwerty_candidate_output, "👩‍💻") + assertEquals(expected, editor.config) + assertTrue(descendants(editor).filterIsInstance().any { + it.text == context.getString(R.string.qwerty_candidate_result, "👩‍💻") }) assertTrue(fragment.requireView().findViewById(R.id.chip_qwerty).isChecked) assertTrue(fragment.requireView().findViewById(R.id.button_done).isEnabled) } @@ -307,6 +327,10 @@ class CustomQwertyKeyDeviceTest { } main { val fragment = host().childFragmentManager.primaryNavigationFragment as KeyEditorFragment + val editor = fragment.requireView().findViewById(R.id.qwerty_key_editor) + val candidateInput = descendants(editor).filterIsInstance() + .first { it.hint == context.getString(R.string.qwerty_candidate_output) }.editText!! + assertEquals("👩‍💻", candidateInput.text.toString()) val done = fragment.requireView().findViewById(R.id.button_done) assertTrue(done.isEnabled) done.performClick() diff --git a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FastInputHostSession.kt b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FastInputHostSession.kt new file mode 100644 index 000000000..1efd1f3f5 --- /dev/null +++ b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FastInputHostSession.kt @@ -0,0 +1,111 @@ +package com.kazumaproject.markdownhelperkeyboard + +import android.content.Context +import android.content.Intent +import android.os.Handler +import android.os.Looper +import android.os.SystemClock +import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry +import androidx.test.runner.lifecycle.Stage +import java.util.UUID +import java.util.concurrent.ExecutionException +import java.util.concurrent.FutureTask +import java.util.concurrent.TimeUnit +import java.util.concurrent.TimeoutException + +/** + * Waits for the host's lifecycle, rather than for the entire application's message queue to idle. + * The IME remains active during the matrix. Every main-thread action and lifecycle wait is bounded; + * focus, editor readiness, IME geometry and committed text are checked by the matrix itself. + */ +internal class FastInputHostSession private constructor(private val token: String) : AutoCloseable { + private var closed = false + + fun onActivity(action: (FastInputHostActivity) -> Unit) { + check(!closed) { "Host session is closed" } + await("resumed host") { + val activity = activities(Stage.RESUMED).singleOrNull() ?: return@await false + action(activity) + true + } + } + + fun recreate() { + var previous: FastInputHostActivity? = null + onActivity { + previous = it + it.recreate() + } + await("recreated host") { + activities(Stage.RESUMED).any { it !== previous } + } + } + + override fun close() { + if (closed) return + try { + onMain { activities().forEach { it.finish() } } + await("destroyed host") { activities().isEmpty() } + } finally { + closed = true + } + } + + // Called only on the main thread. The token follows recreation but excludes unrelated hosts. + private fun activities(vararg stages: Stage): List { + val monitor = ActivityLifecycleMonitorRegistry.getInstance() + val included = stages.toList().ifEmpty { Stage.values().filter { it != Stage.DESTROYED } } + return included.flatMap { monitor.getActivitiesInStage(it) } + .filterIsInstance() + .filter { it.intent.getStringExtra(SESSION_EXTRA) == token } + .distinct() + } + + private fun await(description: String, condition: () -> Boolean) { + val deadline = SystemClock.uptimeMillis() + TIMEOUT_MS + do { + if (onMain(condition)) return + SystemClock.sleep(50) + } while (SystemClock.uptimeMillis() < deadline) + throw AssertionError("Timed out after ${TIMEOUT_MS}ms waiting for $description") + } + + companion object { + private const val SESSION_EXTRA = "fast_input_host_session" + private const val TIMEOUT_MS = 10_000L + + fun launch(context: Context): FastInputHostSession { + val session = FastInputHostSession(UUID.randomUUID().toString()) + try { + onMain { + context.startActivity( + Intent(context, FastInputHostActivity::class.java) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + .putExtra(SESSION_EXTRA, session.token) + ) + } + session.await("launched host") { session.activities(Stage.RESUMED).size == 1 } + return session + } catch (error: Throwable) { + runCatching { session.close() }.exceptionOrNull()?.let(error::addSuppressed) + throw error + } + } + + private fun onMain(action: () -> T): T { + check(Looper.myLooper() != Looper.getMainLooper()) { + "Host session actions must be called from the instrumentation thread" + } + val task = FutureTask(action) + check(Handler(Looper.getMainLooper()).post(task)) { "Main looper rejected host action" } + return try { + task.get(TIMEOUT_MS, TimeUnit.MILLISECONDS) + } catch (error: TimeoutException) { + task.cancel(false) + throw AssertionError("Main thread did not complete host action in ${TIMEOUT_MS}ms", error) + } catch (error: ExecutionException) { + throw error.cause ?: error + } + } + } +} diff --git a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FastInputHostSessionDeviceTest.kt b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FastInputHostSessionDeviceTest.kt new file mode 100644 index 000000000..035dd5d35 --- /dev/null +++ b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FastInputHostSessionDeviceTest.kt @@ -0,0 +1,31 @@ +package com.kazumaproject.markdownhelperkeyboard + +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import org.junit.Assert.assertNotSame +import org.junit.Assert.assertTrue +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class FastInputHostSessionDeviceTest { + @Test + fun launchRecreateAndCloseTrackOnlyThisHost() { + val session = FastInputHostSession.launch( + InstrumentationRegistry.getInstrumentation().targetContext + ) + lateinit var first: FastInputHostActivity + lateinit var second: FastInputHostActivity + try { + session.onActivity { first = it } + session.recreate() + session.onActivity { second = it } + assertNotSame(first, second) + assertTrue(first.isDestroyed) + } finally { + session.close() + } + assertTrue(second.isDestroyed) + session.close() + } +} diff --git a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FastInputMatrixInstrumentedTest.kt b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FastInputMatrixInstrumentedTest.kt index 7aa0737e9..a9bc2a71b 100644 --- a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FastInputMatrixInstrumentedTest.kt +++ b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FastInputMatrixInstrumentedTest.kt @@ -6,7 +6,6 @@ import android.app.KeyguardManager import android.app.UiAutomation import android.content.ComponentName import android.content.Context -import android.content.Intent import android.content.SharedPreferences import android.graphics.Bitmap import android.graphics.PointF @@ -32,7 +31,6 @@ import android.view.inputmethod.InputMethodManager import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import androidx.preference.PreferenceManager -import androidx.test.core.app.ActivityScenario import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import com.kazumaproject.custom_keyboard.data.KeyType @@ -404,7 +402,7 @@ class FastInputMatrixInstrumentedTest { @Test fun flickEditorPreviewFunctionalAndPerformanceOnPhysicalDevice() { runPhysicalDeviceSession("flick-editor-preview") { session -> - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { scenario = launchHost(session.context) rotateAndVerify(TestOrientation.PORTRAIT) @@ -416,7 +414,6 @@ class FastInputMatrixInstrumentedTest { "default", "circle", "second-flick", - "third-flick", "sumire", ) val functionalCases = buildList { @@ -835,7 +832,7 @@ class FastInputMatrixInstrumentedTest { toolbarIntegrated = false, orientation = TestOrientation.PORTRAIT ) - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { scenario = launchHost(session.context) rotateAndVerify(TestOrientation.PORTRAIT) @@ -967,7 +964,7 @@ class FastInputMatrixInstrumentedTest { toolbarIntegrated = false, orientation = TestOrientation.PORTRAIT ) - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { scenario = launchHost(session.context) rotateAndVerify(TestOrientation.PORTRAIT) @@ -1025,7 +1022,7 @@ class FastInputMatrixInstrumentedTest { toolbarIntegrated = false, orientation = TestOrientation.PORTRAIT ) - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { scenario = launchHost(session.context) rotateAndVerify(TestOrientation.PORTRAIT) @@ -1094,7 +1091,7 @@ class FastInputMatrixInstrumentedTest { @Test fun liquidGlassRepeatedDeleteDoesNotRestoreEarlierInputOnPhysicalDevice() { runPhysicalDeviceSession("liquid-glass-repeated-delete") { session -> - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { scenario = launchHost(session.context) rotateAndVerify(TestOrientation.PORTRAIT) @@ -1183,7 +1180,7 @@ class FastInputMatrixInstrumentedTest { toolbarIntegrated = false, orientation = TestOrientation.PORTRAIT ) - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { scenario = launchHost(session.context) rotateAndVerify(TestOrientation.PORTRAIT) @@ -1257,7 +1254,7 @@ class FastInputMatrixInstrumentedTest { toolbarIntegrated = false, orientation = TestOrientation.LANDSCAPE ) - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { scenario = launchHost(session.context) rotateAndVerify(TestOrientation.PORTRAIT) @@ -1338,7 +1335,7 @@ class FastInputMatrixInstrumentedTest { toolbarIntegrated = false, orientation = TestOrientation.LANDSCAPE ) - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { scenario = launchHost(session.context) rotateAndVerify(TestOrientation.PORTRAIT) @@ -1430,7 +1427,7 @@ class FastInputMatrixInstrumentedTest { toolbarIntegrated = false, orientation = TestOrientation.LANDSCAPE ) - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { scenario = launchHost(session.context) rotateAndVerify(TestOrientation.LANDSCAPE) @@ -1544,7 +1541,7 @@ class FastInputMatrixInstrumentedTest { val customFixture = installKeyboardSizeCustomFixture(keyboardLayoutDao) val failures = mutableListOf() var completedCases = 0 - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { applyKeyboardSizeBasePreferences( @@ -1776,7 +1773,7 @@ class FastInputMatrixInstrumentedTest { KanaKanjiEngineEntryPoint::class.java, ).keyboardLayoutDao() val customFixture = installKeyboardSizeCustomFixture(keyboardLayoutDao) - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { applyKeyboardSizeBasePreferences( @@ -1847,7 +1844,7 @@ class FastInputMatrixInstrumentedTest { var completedConfigurations = 0 var completedMeasurements = 0 var caseIndex = 0 - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { matrix@ for (orientation in TestOrientation.entries) { @@ -2028,7 +2025,7 @@ class FastInputMatrixInstrumentedTest { @Test fun emptyCandidatePresentationStaysHiddenAfterImeReopenOnCustomKeyboard() { runPhysicalDeviceSession("custom-empty-candidate-reopen") { session -> - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { check( session.preferences.edit() @@ -2117,7 +2114,7 @@ class FastInputMatrixInstrumentedTest { twoStepLongPressFlicksMap = emptyMap(), ) } - var host: ActivityScenario? = null + var host: FastInputHostSession? = null try { check(session.preferences.edit() .putString("keyboard_order_preference", """["CUSTOM","TENKEY","SUMIRE","QWERTY","ROMAJI"]""") @@ -2221,7 +2218,7 @@ class FastInputMatrixInstrumentedTest { session.context.applicationContext, KanaKanjiEngineEntryPoint::class.java, ).keyboardLayoutDao() val fixtureIds = mutableListOf() - var host: ActivityScenario? = null + var host: FastInputHostSession? = null try { for (direct in listOf(false, true)) { val stableId = "toggle-timeout-qa-$direct-${System.currentTimeMillis()}" @@ -2301,7 +2298,7 @@ class FastInputMatrixInstrumentedTest { @Test fun customKeyboardAcceptsInputOnPhysicalDevice() { runPhysicalDeviceSession("custom-input") { session -> - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { check( session.preferences.edit() @@ -2367,7 +2364,7 @@ class FastInputMatrixInstrumentedTest { var configurations = 0 var configurationIndex = 0 var trials = 0 - var scenario: ActivityScenario? = null + var scenario: FastInputHostSession? = null try { for (orientation in TestOrientation.entries) { @@ -2511,7 +2508,7 @@ class FastInputMatrixInstrumentedTest { } private fun runColdCandidateTransitionPhase( - scenario: ActivityScenario, + scenario: FastInputHostSession, keyboard: TestKeyboard ): PhaseResult { prepareEmptyEditor(scenario) @@ -2538,7 +2535,7 @@ class FastInputMatrixInstrumentedTest { } private fun runWarmCandidatePhase( - scenario: ActivityScenario, + scenario: FastInputHostSession, keyboard: TestKeyboard ): PhaseResult { prepareEmptyEditor(scenario) @@ -2577,7 +2574,7 @@ class FastInputMatrixInstrumentedTest { } private fun runSumireRateTrial( - scenario: ActivityScenario, + scenario: FastInputHostSession, intervalMs: Long ): RateTrialResult { prepareEmptyEditor(scenario) @@ -2861,7 +2858,7 @@ class FastInputMatrixInstrumentedTest { ) } - private fun assertGojuonInput(scenario: ActivityScenario) { + private fun assertGojuonInput(scenario: FastInputHostSession) { val key = awaitVisibleNodeBounds("key_51") check(injectTap(key.center)) { "Unable to tap representative Gojūon key" } val actual = awaitEditorText(scenario) { it == "あ" } @@ -3020,7 +3017,7 @@ class FastInputMatrixInstrumentedTest { "column${case.columns}=${preferences.getInt(columnKey, columnDefault)})" } - private fun restartInput(scenario: ActivityScenario) { + private fun restartInput(scenario: FastInputHostSession) { awaitHostWindowFocus(scenario) scenario.onActivity { activity -> activity.restartEditorInput(clearText = true) @@ -3075,7 +3072,7 @@ class FastInputMatrixInstrumentedTest { ) } - private fun prepareEmptyEditor(scenario: ActivityScenario) { + private fun prepareEmptyEditor(scenario: FastInputHostSession) { restartInput(scenario) val deadline = SystemClock.uptimeMillis() + SETUP_TIMEOUT_MS while (SystemClock.uptimeMillis() < deadline) { @@ -3091,7 +3088,7 @@ class FastInputMatrixInstrumentedTest { throw SetupException("Host editor did not become focused and empty") } - private fun readText(scenario: ActivityScenario): String { + private fun readText(scenario: FastInputHostSession): String { var text = "" scenario.onActivity { activity -> text = activity.editText.text?.toString().orEmpty() @@ -3100,7 +3097,7 @@ class FastInputMatrixInstrumentedTest { } private fun assertEditorPreviewDecoration( - scenario: ActivityScenario, + scenario: FastInputHostSession, expectedText: String, expectedBackgroundColor: Int, caseName: String, @@ -3129,7 +3126,7 @@ class FastInputMatrixInstrumentedTest { } private fun readEditorDecoration( - scenario: ActivityScenario + scenario: FastInputHostSession ): EditorDecorationSnapshot { var snapshot = EditorDecorationSnapshot.Empty scenario.onActivity { activity -> @@ -3165,7 +3162,7 @@ class FastInputMatrixInstrumentedTest { } private fun awaitEditorText( - scenario: ActivityScenario, + scenario: FastInputHostSession, predicate: (String) -> Boolean, ): String { val deadline = SystemClock.uptimeMillis() + RESULT_TIMEOUT_MS @@ -3190,7 +3187,7 @@ class FastInputMatrixInstrumentedTest { } private fun awaitTextSettled( - scenario: ActivityScenario + scenario: FastInputHostSession ): String { var previous: String? = null var stableSamples = 0 @@ -3956,17 +3953,14 @@ class FastInputMatrixInstrumentedTest { ) } - private fun launchHost(context: Context): ActivityScenario { - val scenario = ActivityScenario.launch( - Intent(context, FastInputHostActivity::class.java) - .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) - ) + private fun launchHost(context: Context): FastInputHostSession { + val scenario = FastInputHostSession.launch(context) awaitHostWindowFocus(scenario) return scenario } private fun awaitHostWindowFocus( - scenario: ActivityScenario + scenario: FastInputHostSession ) { val startedAt = SystemClock.uptimeMillis() val deadline = startedAt + HOST_WINDOW_FOCUS_TIMEOUT_MS @@ -4031,7 +4025,7 @@ class FastInputMatrixInstrumentedTest { private fun assertDeviceReady( context: Context, expectedIme: String, - scenario: ActivityScenario? = null + scenario: FastInputHostSession? = null ) { val powerManager = context.getSystemService(PowerManager::class.java) val keyguardManager = context.getSystemService(KeyguardManager::class.java) diff --git a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FeverImeInstrumentedTest.kt b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FeverImeInstrumentedTest.kt new file mode 100644 index 000000000..b3bab91ac --- /dev/null +++ b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FeverImeInstrumentedTest.kt @@ -0,0 +1,780 @@ +package com.kazumaproject.markdownhelperkeyboard + +import android.accessibilityservice.AccessibilityServiceInfo +import android.content.Context +import android.graphics.Bitmap +import android.graphics.Rect +import android.os.ParcelFileDescriptor +import android.os.SystemClock +import android.view.MotionEvent +import android.view.View +import android.view.ViewGroup +import android.view.accessibility.AccessibilityNodeInfo +import android.view.inputmethod.InputMethodManager +import android.view.inspector.WindowInspector +import androidx.preference.PreferenceManager +import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry +import androidx.test.runner.lifecycle.Stage +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.SdkSuppress +import androidx.test.platform.app.InstrumentationRegistry +import com.kazumaproject.markdownhelperkeyboard.ime_service.fever.* +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith +import java.io.File + +/** Actual editor, IME, window insets and touchscreen; runs only in an isolated application ID. */ +@RunWith(AndroidJUnit4::class) +@SdkSuppress(minSdkVersion = 29) +class FeverImeInstrumentedTest { + private val ins = InstrumentationRegistry.getInstrumentation() + private val ui get() = ins.uiAutomation + private val context get() = ins.targetContext + private fun shell(cmd: String) = ParcelFileDescriptor.AutoCloseInputStream(ui.executeShellCommand(cmd)).bufferedReader().use { it.readText().trim() } + private fun nodes(): List { + val nodes = mutableListOf() + fun visit(n: AccessibilityNodeInfo) { nodes.add(n); for (i in 0 until n.childCount) n.getChild(i)?.let(::visit) } + ui.windows.forEach { it.root?.let(::visit) } + return nodes + } + private fun await(condition: () -> Boolean) { + val end = SystemClock.uptimeMillis() + 45000 + while (SystemClock.uptimeMillis() < end) { if (condition()) return; SystemClock.sleep(100) } + error("Timed out; UI: " + nodes().joinToString { "${it.viewIdResourceName}: ${it.text}" }) + } + private fun key(id: String): AccessibilityNodeInfo { + var found: AccessibilityNodeInfo? = null + await { found = nodes().firstOrNull { it.isVisibleToUser && it.viewIdResourceName == "${context.packageName}:id/$id" }; found != null } + return found!! + } + private fun stableKeyboardBounds(floating: Boolean): Rect { + var previous: Rect? = null + var stable = 0 + var result = Rect() + await { + ins.runOnMainSync { + val id = if (floating) R.id.keyboard_view_floating else R.id.keyboard_view + val view = WindowInspector.getGlobalWindowViews().filter { + (it is com.kazumaproject.markdownhelperkeyboard.ime_service.floating_keyboard.FloatingKeyboardPanel) == floating + }.mapNotNull { it.findViewById(id)?.takeIf(View::isShown) }.single() + val point = IntArray(2).also(view::getLocationOnScreen) + result = Rect(point[0], point[1], point[0] + view.width, point[1] + view.height) + } + stable = if (result == previous) stable + 1 else 0 + previous = result + SystemClock.sleep(150) + stable >= 3 + } + return result + } + private fun nativeBounds(id: String): Rect? { + var result: Rect? = null + ins.runOnMainSync { + val wanted = context.resources.getIdentifier(id, "id", context.packageName) + fun find(view: View) { + if (result != null || !view.isShown) return + if (view.id == wanted && view.width > 0 && view.height > 0 && + view.getGlobalVisibleRect(Rect())) { + val point = IntArray(2).also(view::getLocationOnScreen) + result = Rect(point[0], point[1], point[0] + view.width, point[1] + view.height) + } else if (view is ViewGroup) for (index in 0 until view.childCount) find(view.getChildAt(index)) + } + val floating = PreferenceManager.getDefaultSharedPreferences(context) + .getBoolean("keyboard_floating_preference", false) + WindowInspector.getGlobalWindowViews().filter { + (it is com.kazumaproject.markdownhelperkeyboard.ime_service.floating_keyboard.FloatingKeyboardPanel) == floating + }.forEach(::find) + } + return result + } + private fun tap(id: String) { + var previous = Rect(); var stable = 0; var rect = Rect() + await { + val bounds = nativeBounds(id) + if (bounds != null) { + stable = if (bounds == previous && !bounds.isEmpty) stable + 1 else 0 + previous = bounds; rect = bounds + } else stable = 0 + SystemClock.sleep(80) + stable >= 2 + } + val accessible = nodes().firstOrNull { it.isVisibleToUser && + it.viewIdResourceName == "${context.packageName}:id/$id" }?.let { Rect().also(it::getBoundsInScreen) } + nativeBounds(id)?.let { rect = it } + println("FEVER tap $id native=$rect accessibility=$accessible") + val down = SystemClock.uptimeMillis() + for (action in listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP)) { + val event = MotionEvent.obtain(down, SystemClock.uptimeMillis(), action, + rect.centerX().toFloat(), rect.centerY().toFloat(), 0) + event.source = android.view.InputDevice.SOURCE_TOUCHSCREEN + try { assertTrue(ui.injectInputEvent(event, true)) } finally { event.recycle() } + if (action == MotionEvent.ACTION_DOWN) SystemClock.sleep(35) + } + } + private fun keyLabelBitmap(): Bitmap { + val rect = requireNotNull(nativeBounds("key_5")) + rect.inset(rect.width() / 4, rect.height() / 4) + val screen = requireNotNull(ui.takeScreenshot()) + return Bitmap.createBitmap(screen, rect.left, rect.top, rect.width(), rect.height()).also { screen.recycle() } + } + private fun ime(): FeverInputFrameLayout { + var result: FeverInputFrameLayout? = null + ins.runOnMainSync { + fun find(v: View) { + if (v is FeverInputFrameLayout && v.isShown) result = v + if (v is ViewGroup) for (i in 0 until v.childCount) find(v.getChildAt(i)) + } + WindowInspector.getGlobalWindowViews().forEach(::find) + } + return requireNotNull(result) + } + private fun assertCapBindings(floating: Boolean) { + ins.runOnMainSync { + val ids = listOf("key_1", "key_5", "key_delete").map { context.resources.getIdentifier(it, "id", context.packageName) } + val caps = mutableListOf() + fun visit(view: View) { + if (view.isShown && view.id in ids) caps.add(view) + if (view is ViewGroup) for (index in 0 until view.childCount) visit(view.getChildAt(index)) + } + WindowInspector.getGlobalWindowViews().filter { + (it is com.kazumaproject.markdownhelperkeyboard.ime_service.floating_keyboard.FloatingKeyboardPanel) == floating + }.forEach(::visit) + assertEquals(3, caps.size) + caps.forEach { cap -> + assertNotNull("Each key must retain its FEVER cap", cap.background) + assertNotNull("Key caps must support the keyboard's drawable cloning", cap.background.constantState) + } + } + } + private fun capture(name: String) { + val out = File(context.getExternalFilesDir(null), "fever-evidence").apply { mkdirs() } + val bitmap = requireNotNull(ui.takeScreenshot()) + File(out, "$name.png").outputStream().use { bitmap.compress(Bitmap.CompressFormat.PNG, 100, it) } + bitmap.recycle() + } + private fun assertLabelLegible(before: Bitmap, during: Bitmap, name: String) { + assertEquals(before.width, during.width); assertEquals(before.height, during.height) + val out = File(context.getExternalFilesDir(null), "fever-evidence").apply { mkdirs() } + for ((suffix, bitmap) in listOf("label-before" to before, "label-during" to during)) + File(out, "$name-$suffix.png").outputStream().use { bitmap.compress(Bitmap.CompressFormat.PNG, 100, it) } + var glyph = 0; var retained = 0; var dark = 0; var contrast = 0 + for (y in 0 until before.height) for (x in 0 until before.width) { + val a = before.getPixel(x, y); val b = during.getPixel(x, y) + fun low(c: Int) = minOf(android.graphics.Color.red(c), android.graphics.Color.green(c), android.graphics.Color.blue(c)) + fun high(c: Int) = maxOf(android.graphics.Color.red(c), android.graphics.Color.green(c), android.graphics.Color.blue(c)) + if (low(a) >= 190) { glyph++; if (low(b) >= 150) retained++ } + if (high(a) < 80) { dark++; if (high(b) < 170) contrast++ } + } + println("FEVER label $name glyph=$retained/$glyph dark=$contrast/$dark") + assertTrue("The real label must remain visible", glyph > 10 && retained >= glyph * .98) + assertTrue("The label must keep a dark contrasting surface", dark > 10 && contrast >= dark * .90) + } + private fun startEditor(landscape: Boolean): FastInputHostActivity { + shell("am start -n ${context.packageName}/com.kazumaproject.markdownhelperkeyboard.FastInputHostActivity") + var activity: FastInputHostActivity? = null + val wanted = if (landscape) android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE else android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT + await { + ins.runOnMainSync { + val resumed = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED) + .filterIsInstance().firstOrNull() + if (resumed != null) { + if (resumed.requestedOrientation != wanted) resumed.requestedOrientation = wanted + else if (resumed.window.decorView.width > 0 && (resumed.window.decorView.width > resumed.window.decorView.height) == landscape) + activity = resumed + } + } + activity != null + } + return requireNotNull(activity) + } + private fun verifyNumericAndPasswordEditors() { + val activity = startEditor(false) + try { + ins.runOnMainSync { activity.editText.inputType = android.text.InputType.TYPE_CLASS_NUMBER; activity.restartEditorInput(true) } + var numericKey: AccessibilityNodeInfo? = null + await { + numericKey = nodes().firstOrNull { it.isVisibleToUser && it.text?.toString()?.trim() == "1" && it.isClickable } + numericKey != null + } + val root = ime() + var numericAward = false + ins.runOnMainSync { root.controller.eventObserver = { println("FEVER numeric event $it"); numericAward = numericAward || it.award == FeverAward.JACKPOT } } + // The automatic numeric layout can appear before its window finishes moving after rotation. + var numberBounds = Rect() + var stable = 0 + await { + val current = nodes().firstOrNull { it.isVisibleToUser && it.text?.toString()?.trim() == "1" && it.isClickable } + val next = current?.let { Rect().also(it::getBoundsInScreen) } + stable = if (next != null && next == numberBounds && !next.isEmpty) stable + 1 else 0 + if (next != null) numberBounds = next + SystemClock.sleep(150) + stable >= 4 + } + println("FEVER numeric tap $numberBounds") + shell("input tap ${numberBounds.centerX()} ${numberBounds.centerY()}") + await { var applied = false; ins.runOnMainSync { applied = activity.editText.text.toString() == "1" }; applied } + ins.runOnMainSync { assertEquals(1, root.controller.combo); assertTrue(numericAward) } + ins.runOnMainSync { + activity.editText.inputType = android.text.InputType.TYPE_CLASS_TEXT or android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD + activity.restartEditorInput(true) + } + await { var disabled = false; ins.runOnMainSync { disabled = !root.controller.enabled }; disabled } + val events = mutableListOf() + ins.runOnMainSync { root.controller.eventObserver = { events.add(it) } } + var passwordKey: AccessibilityNodeInfo? = null + await { + passwordKey = nodes().firstOrNull { it.isVisibleToUser && + (it.viewIdResourceName == "${context.packageName}:id/key_a" || it.viewIdResourceName == "${context.packageName}:id/key_1") } + passwordKey != null + } + val passwordBounds = Rect().also(requireNotNull(passwordKey)::getBoundsInScreen) + shell("input tap ${passwordBounds.centerX()} ${passwordBounds.centerY()}") + await { var applied = false; ins.runOnMainSync { applied = activity.editText.text.isNotEmpty() }; applied } + ins.runOnMainSync { assertEquals(0, root.controller.combo); assertTrue(events.isEmpty()) } + } finally { ins.runOnMainSync { activity.finish() } } + } + @Test fun keyboardAndFullScreenKeepEditorTouchesAndStopWhenHidden() = verifyMatrix(false) + @Test fun floatingKeyboardAndFullScreenKeepEditorTouchesAndStopWhenHidden() = verifyMatrix(true) + + @Test fun variedTypingMilestonesAndEditingKeepRealCandidatesUsable() { + check(context.packageName.startsWith("com.kazumaproject.skinfidelity")) + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + val saved = prefs.all.toMap() + val original = shell("settings get secure default_input_method") + val scale = shell("settings get global animator_duration_scale") + val target = "${context.packageName}/com.kazumaproject.markdownhelperkeyboard.ime_service.IMEService" + val enabled = target in shell("ime list -s").lines() + var activity: FastInputHostActivity? = null + var recordingDone: java.util.concurrent.CountDownLatch? = null + try { + ui.serviceInfo = ui.serviceInfo.apply { flags = flags or AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS or AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS } + check(ui.setRotation(android.app.UiAutomation.ROTATION_FREEZE_0)) + prefs.edit().putString("keyboard_skin_preference", "fever") + .putString("keyboard_order_preference", "[\"TENKEY\"]").putBoolean("save_last_used_keyboard", false) + .putBoolean("keyboard_floating_preference", false).putInt("keyboard_width_preference", 100) + .putBoolean(FeverSettings.FULL_SCREEN, true).putInt(FeverSettings.INTENSITY, 2) + .putInt(FeverSettings.HIT_RATE, 100).putInt(FeverSettings.COMBO_SECONDS, 10) + .putBoolean("key_sound_preference", false).commit() + shell("settings put global animator_duration_scale 0") + shell("ime enable $target"); shell("ime set $target") + activity = startEditor(false) + key("key_1"); stableKeyboardBounds(false) + val root = ime() + val awards = mutableSetOf() + ins.runOnMainSync { root.controller.eventObserver = { it.award?.let(awards::add) } } + if (InstrumentationRegistry.getArguments().getString("feverVideo") == "true") { + val recording = ui.executeShellCommand("screenrecord --time-limit 45 /sdcard/Download/fever-variety.mp4") + recordingDone = java.util.concurrent.CountDownLatch(1) + val done = recordingDone + Thread { try { ParcelFileDescriptor.AutoCloseInputStream(recording).use { it.readBytes() } } finally { done.countDown() } }.start() + SystemClock.sleep(200) + } + fun touchKey(id: String, hold: Long = 25, flick: Boolean = false) { + val cap = Rect().also(key(id)::getBoundsInScreen) + val down = SystemClock.uptimeMillis() + fun send(action: Int, dx: Float = 0f) { + val event = MotionEvent.obtain(down, SystemClock.uptimeMillis(), action, + cap.centerX() + dx, cap.centerY().toFloat(), 0) + event.source = android.view.InputDevice.SOURCE_TOUCHSCREEN + try { check(ui.injectInputEvent(event, true)) } finally { event.recycle() } + } + send(MotionEvent.ACTION_DOWN); SystemClock.sleep(hold) + if (flick) { send(MotionEvent.ACTION_MOVE, cap.width() * .8f); SystemClock.sleep(180) } + send(MotionEvent.ACTION_UP, if (flick) cap.width() * .8f else 0f) + } + for (count in 1..50) { + touchKey(if (count % 2 == 0) "key_2" else "key_1") + await { var combo = 0; ins.runOnMainSync { combo = root.controller.combo }; combo == count } + if (count <= 5) { SystemClock.sleep(200); capture("variety-input-$count"); SystemClock.sleep(250) } + else SystemClock.sleep(60) + if (count in listOf(10, 25, 50)) { SystemClock.sleep(250); capture("variety-combo-$count"); SystemClock.sleep(1400) } + } + ins.runOnMainSync { assertTrue(awards.containsAll(listOf(FeverAward.COMBO, FeverAward.FEVER, FeverAward.HOT))) } + touchKey("key_5", 650, true) + touchKey("key_delete"); SystemClock.sleep(400) + touchKey("key_soft_left"); SystemClock.sleep(400) + touchKey("key_move_cursor_right"); SystemClock.sleep(400) + touchKey("key_space"); SystemClock.sleep(400) + touchKey("key_space"); SystemClock.sleep(400) + assertTrue(nodes().any { it.isVisibleToUser && it.viewIdResourceName == "${context.packageName}:id/suggestion_item_text_view" && !it.text.isNullOrBlank() }) + touchKey("suggestion_item_text_view") + ins.runOnMainSync { assertTrue(awards.contains(FeverAward.JACKPOT)); assertTrue(activity.editText.text.isNotEmpty()) } + SystemClock.sleep(700); capture("variety-confirmed") + } finally { + recordingDone?.await(50, java.util.concurrent.TimeUnit.SECONDS) + ins.runOnMainSync { activity?.finish() } + restorePreferences(prefs, saved) + if (scale == "null") shell("settings delete global animator_duration_scale") else shell("settings put global animator_duration_scale $scale") + shell("ime set $original"); if (!enabled) shell("ime disable $target") + ui.setRotation(android.app.UiAutomation.ROTATION_UNFREEZE) + } + } + + @Test(timeout = 180000) fun heldDeleteAnimatesEveryRepeatAndStopsOnReleaseAndCancel() { + check(context.packageName.startsWith("com.kazumaproject.skinfidelity")) + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + val saved = prefs.all.toMap() + val original = shell("settings get secure default_input_method") + val target = "${context.packageName}/com.kazumaproject.markdownhelperkeyboard.ime_service.IMEService" + val enabled = target in shell("ime list -s").lines() + var activity: FastInputHostActivity? = null + try { + ui.serviceInfo = ui.serviceInfo.apply { flags = flags or AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS or AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS } + check(ui.setRotation(android.app.UiAutomation.ROTATION_FREEZE_0)) + shell("ime enable $target"); shell("ime set $target") + for (floating in listOf(false, true)) for (full in listOf(false, true)) { + prefs.edit().putString("keyboard_skin_preference", "fever") + .putString("keyboard_order_preference", "[\"TENKEY\"]").putBoolean("save_last_used_keyboard", false) + .putBoolean("keyboard_floating_preference", floating) + .putInt("keyboard_width_preference", if (floating) 70 else 100) + .putInt("keyboard_floating_position_x", 24).putInt("keyboard_floating_position_y", 160) + .putInt("long_press_timeout_preference", 300) + .putBoolean(FeverSettings.FULL_SCREEN, full).putInt(FeverSettings.COMBO_SECONDS, 10) + .putBoolean("key_sound_preference", false).putBoolean(FeverSettings.VIBRATION, false).commit() + activity = startEditor(false) + key("key_1"); stableKeyboardBounds(floating) + val host = activity + val root = ime() + val cues = java.util.concurrent.CopyOnWriteArrayList() + ins.runOnMainSync { root.controller.eventObserver = { cues.add(it) } } + // Real composing input, followed by a held key; no reflected service mutation. + repeat(8) { tap(if (it % 2 == 0) "key_1" else "key_2") } + var combo = 0 + ins.runOnMainSync { combo = root.controller.combo } + assertEquals(8, combo) + for (cancel in listOf(false, true)) { + if (cancel) { + ins.runOnMainSync { + host.editText.setText("あいうえお".repeat(20)) + host.editText.setSelection(host.editText.length()) + host.restartEditorInput(false) + } + SystemClock.sleep(700); stableKeyboardBounds(floating) + } + val cap = requireNotNull(nativeBounds("key_delete")) + val down = SystemClock.uptimeMillis() + fun send(action: Int) { + val event = MotionEvent.obtain(down, SystemClock.uptimeMillis(), action, + cap.exactCenterX(), cap.exactCenterY(), 0) + event.source = android.view.InputDevice.SOURCE_TOUCHSCREEN + try { check(ui.injectInputEvent(event, true)) } finally { event.recycle() } + } + cues.clear() + var before = 0 + ins.runOnMainSync { before = host.editText.length(); combo = root.controller.combo } + send(MotionEvent.ACTION_DOWN) + try { + SystemClock.sleep(750) + val first = requireNotNull(ui.takeScreenshot()) + SystemClock.sleep(140) + val second = requireNotNull(ui.takeScreenshot()) + val a = IntArray(first.width * first.height); val b = IntArray(a.size) + first.getPixels(a, 0, first.width, 0, 0, first.width, first.height) + second.getPixels(b, 0, second.width, 0, 0, second.width, second.height) + assertTrue("Repeat must animate actual frames", a.indices.count { a[it] != b[it] } > 50) + first.recycle(); second.recycle() + assertTrue("Repeated DELETE cues: floating=$floating full=$full cancel=$cancel $cues", + cues.count { it.cue == FeverCue.DELETE } >= 4) + ins.runOnMainSync { + assertTrue("Held delete must remove text", host.editText.length() < before) + assertEquals("Delete must not add combo", combo, root.controller.combo) + } + } finally { send(if (cancel) MotionEvent.ACTION_CANCEL else MotionEvent.ACTION_UP) } + SystemClock.sleep(200) + val count = cues.count { it.cue == FeverCue.DELETE } + var remaining = 0 + ins.runOnMainSync { remaining = host.editText.length() } + SystemClock.sleep(350) + assertEquals("Repeat cue leaked after release", count, cues.count { it.cue == FeverCue.DELETE }) + ins.runOnMainSync { assertEquals("Deletion leaked after release", remaining, host.editText.length()) } + println("FEVER held delete PASS floating=$floating full=$full cancel=$cancel cues=$count") + } + ins.runOnMainSync { host.finish() }; activity = null + } + } finally { + ins.runOnMainSync { activity?.finish() } + restorePreferences(prefs, saved) + shell("ime set $original"); if (!enabled) shell("ime disable $target") + ui.setRotation(android.app.UiAutomation.ROTATION_UNFREEZE) + } + } + + @Test(timeout = 240000) fun sumirePopupsMovementAndCandidateButtonsStayVisible() { + check(context.packageName.startsWith("com.kazumaproject.skinfidelity")) + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + val saved = prefs.all.toMap() + val original = shell("settings get secure default_input_method") + val target = "${context.packageName}/com.kazumaproject.markdownhelperkeyboard.ime_service.IMEService" + val enabled = target in shell("ime list -s").lines() + var activity: FastInputHostActivity? = null + fun field(value: Any, name: String): Any? = value.javaClass.getDeclaredField(name).apply { isAccessible = true }.get(value) + fun sumire(): com.kazumaproject.custom_keyboard.view.FlickKeyboardView? { + var result: com.kazumaproject.custom_keyboard.view.FlickKeyboardView? = null + fun visit(view: View) { + if (!view.isShown) return + if (view is com.kazumaproject.custom_keyboard.view.FlickKeyboardView && view.width > 0) result = view + if (view is ViewGroup) for (i in 0 until view.childCount) visit(view.getChildAt(i)) + } + val floating = prefs.getBoolean("keyboard_floating_preference", false) + WindowInspector.getGlobalWindowViews().filter { + (it is com.kazumaproject.markdownhelperkeyboard.ime_service.floating_keyboard.FloatingKeyboardPanel) == floating + }.forEach(::visit) + return result + } + try { + ui.serviceInfo = ui.serviceInfo.apply { flags = flags or AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS or AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS } + check(ui.setRotation(android.app.UiAutomation.ROTATION_FREEZE_0)) + shell("ime enable $target"); shell("ime set $target") + for (floating in listOf(false, true)) for (full in listOf(false, true)) { + prefs.edit().clear().putString("keyboard_skin_preference", "fever") + .putString("keyboard_order_preference", "[\"SUMIRE\"]").putBoolean("save_last_used_keyboard", false) + .putBoolean("gojuon_keyboard_type_migrated_v1", true) + .putString("sumire_input_method_preference", "flick").putString("sumire_keyboard_style_preference", "default") + .putBoolean("sumire_restore_input_mode_on_restart_preference", false) + .putBoolean("landscape_force_qwerty_preference", false) + .putBoolean("flick_editor_preview_preference", false).putBoolean("live_conversion_preference", false) + .putBoolean("keyboard_floating_preference", floating).putInt("keyboard_width_preference", if (floating) 70 else 100) + .putInt("keyboard_floating_position_x", 24).putInt("keyboard_floating_position_y", 160) + .putInt("long_press_timeout_preference", 300).putBoolean(FeverSettings.FULL_SCREEN, full) + .putBoolean(FeverSettings.SOUND, true).putInt(FeverSettings.VOLUME, 100).putBoolean("key_sound_preference", false) + .putBoolean(FeverSettings.VIBRATION, false).commit() + activity = startEditor(false) + await { var found = false; ins.runOnMainSync { found = sumire() != null }; found } + SystemClock.sleep(700) + val inputFrame = ime() + for (label in listOf("あ", "わ")) for (hold in listOf(40L, 500L, 500L)) { + lateinit var keyView: android.widget.Button + lateinit var keyboard: com.kazumaproject.custom_keyboard.view.FlickKeyboardView + var x = 0f; var y = 0f; var keyWidth = 0 + var previousBounds: Rect? = null + var stable = 0 + await { + var bounds: Rect? = null + ins.runOnMainSync { + keyboard = requireNotNull(sumire()) + val buttons = mutableListOf() + fun visit(v: View) { + if (v is android.widget.Button && v.text.toString() == label) buttons.add(v) + if (v is ViewGroup) for (i in 0 until v.childCount) visit(v.getChildAt(i)) + } + visit(keyboard); keyView = buttons.first() + val position = IntArray(2).also(keyView::getLocationOnScreen) + keyWidth = keyView.width; x = position[0] + keyView.width / 2f; y = position[1] + keyView.height / 2f + bounds = Rect(position[0], position[1], position[0] + keyView.width, position[1] + keyView.height) + } + stable = if (bounds == previousBounds && bounds?.isEmpty == false) stable + 1 else 0 + previousBounds = bounds + SystemClock.sleep(100) + stable >= 3 + } + val down = SystemClock.uptimeMillis() + fun send(action: Int, dx: Float = 0f, dy: Float = 0f) { + val e = MotionEvent.obtain(down, SystemClock.uptimeMillis(), action, x + dx, y + dy, 0) + e.source = android.view.InputDevice.SOURCE_TOUCHSCREEN + try { assertTrue(ui.injectInputEvent(e, true)) } finally { e.recycle() } + } + send(MotionEvent.ACTION_DOWN) + lateinit var popup: View + try { + SystemClock.sleep(hold) + send(MotionEvent.ACTION_MOVE, -keyWidth * .8f) + SystemClock.sleep(25) + send(MotionEvent.ACTION_MOVE, 0f, -keyWidth * .8f) + ins.runOnMainSync { + val controllers = field(keyboard, "crossFlickControllers") as List<*> + val controller = controllers.filterNotNull().first { field(it, "anchorView") === keyView } + popup = if (hold < 100) requireNotNull(field(controller, "currentVisibleDirectionalPopup")) as View + else field(controller, "gridPopupView") as View + assertTrue("Popup must remain attached: $label $hold", popup.isAttachedToWindow) + val host = (field(controller, "popupOverlay") as Any).let { field(it, "host") as View } + assertTrue("Popup clipped: $label $hold ${popup.left},${popup.top}", popup.left >= 0 && popup.top >= 0 && popup.right <= host.width && popup.bottom <= host.height) + val trail = requireNotNull(field(popup, "fingerTrail")) + assertTrue("MOVE must reach the popup effect", (field(trail, "marks") as List<*>).isNotEmpty()) + } + SystemClock.sleep(80) + val screen = requireNotNull(ui.takeScreenshot()) + File(File(context.getExternalFilesDir(null), "fever-evidence"), "surface-$floating-$full-$label-$hold.png") + .outputStream().use { screen.compress(Bitmap.CompressFormat.PNG, 100, it) } + try { ins.runOnMainSync { + val rendered = Bitmap.createBitmap(popup.width, popup.height, Bitmap.Config.ARGB_8888) + try { + popup.draw(android.graphics.Canvas(rendered)) + val location = IntArray(2).also(popup::getLocationOnScreen) + val cells = if (popup is ViewGroup) { + val grid = (popup as ViewGroup).getChildAt(0) as ViewGroup + (0 until grid.childCount).map(grid::getChildAt).filter { it.background != null } + } else listOf(popup) + assertTrue(cells.isNotEmpty()) + cells.forEach { cell -> + cell.background?.let { assertTrue("Overlay background must stay visible after MOVE", it.isVisible) } + val cx = if (cell === popup) popup.width / 4 else cell.left + cell.width / 4 + val cy = if (cell === popup) popup.height / 4 else cell.top + cell.height / 4 + val expected = rendered.getPixel(cx, cy) + assertEquals("Popup surface must be opaque", 255, android.graphics.Color.alpha(expected)) + val actual = screen.getPixel(location[0] + cx, location[1] + cy) + for (shift in listOf(0, 8, 16)) assertTrue("Hardware popup surface differs: floating=$floating full=$full label=$label hold=$hold point=${location[0] + cx},${location[1] + cy} expected=${Integer.toHexString(expected)} actual=${Integer.toHexString(actual)}", + kotlin.math.abs((expected shr shift and 255) - (actual shr shift and 255)) < 18) + } + } finally { rendered.recycle() } + } } finally { screen.recycle() } + capture("sumire-$floating-$full-$label-$hold") + } finally { send(if (hold == 500L) MotionEvent.ACTION_CANCEL else MotionEvent.ACTION_UP) } + ins.runOnMainSync { + assertFalse(popup.isAttachedToWindow); assertEquals(label, keyView.text.toString()) + if (hold < 100) { + val feedback = requireNotNull(field(inputFrame.controller, "feedback")) + assertTrue("Dedicated sound must play in the real IME with ordinary key sounds OFF", + (field(feedback, "streams") as List<*>).any { it is Int && it > 0 }) + } + } + } + // Commit a real Sumire tap to expose the expand/restore control. + lateinit var kana: android.widget.Button + ins.runOnMainSync { + fun find(v: View) { + if (v is android.widget.Button && v.text.toString() == "あ") kana = v + if (v is ViewGroup) for (i in 0 until v.childCount) find(v.getChildAt(i)) + } + find(requireNotNull(sumire())) + val now = SystemClock.uptimeMillis() + for (action in listOf(MotionEvent.ACTION_DOWN, MotionEvent.ACTION_UP)) { + val e = MotionEvent.obtain(now, now + 30, action, kana.width / 2f, kana.height / 2f, 0) + try { kana.dispatchTouchEvent(e) } finally { e.recycle() } + } + } + await { nativeBounds("suggestion_visibility") != null } + repeat(2) { expanded -> + ins.runOnMainSync { + val id = context.resources.getIdentifier("suggestion_visibility", "id", context.packageName) + val buttons = WindowInspector.getGlobalWindowViews().mapNotNull { it.findViewById(id)?.takeIf(View::isShown) } + assertTrue(buttons.isNotEmpty()) + buttons.forEach { button -> + assertEquals("Both candidate button states use the FEVER rim", "FeverKeyDrawable", button.background.javaClass.simpleName) + assertNull("Tint must not erase the neon", button.backgroundTintList) + } + } + capture("sumire-button-$floating-$full-$expanded") + tap("suggestion_visibility"); SystemClock.sleep(250) + } + println("FEVER Sumire PASS floating=$floating full=$full") + ins.runOnMainSync { activity?.finish() }; activity = null + SystemClock.sleep(300) + } + } finally { + ins.runOnMainSync { activity?.finish() } + restorePreferences(prefs, saved) + shell("ime set $original"); if (!enabled) shell("ime disable $target") + ui.setRotation(android.app.UiAutomation.ROTATION_UNFREEZE) + } + } + + private fun restorePreferences(prefs: android.content.SharedPreferences, saved: Map) { + val edit = prefs.edit().clear() + saved.forEach { (key, value) -> when (value) { + is String -> edit.putString(key, value); is Boolean -> edit.putBoolean(key, value) + is Int -> edit.putInt(key, value); is Long -> edit.putLong(key, value); is Float -> edit.putFloat(key, value) + is Set<*> -> @Suppress("UNCHECKED_CAST") edit.putStringSet(key, value as Set) + } } + check(edit.commit()) + } + + private fun verifyMatrix(floating: Boolean) { + check(context.packageName.startsWith("com.kazumaproject.skinfidelity")) + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + val saved = prefs.all.toMap() + val original = shell("settings get secure default_input_method") + val originalAnimations = shell("settings get global animator_duration_scale") + val target = "${context.packageName}/com.kazumaproject.markdownhelperkeyboard.ime_service.IMEService" + val enabled = target in shell("ime list -s").lines() + ui.serviceInfo = ui.serviceInfo.apply { flags = flags or AccessibilityServiceInfo.FLAG_RETRIEVE_INTERACTIVE_WINDOWS or AccessibilityServiceInfo.FLAG_REPORT_VIEW_IDS } + try { + shell("settings put global animator_duration_scale 0") + shell("ime enable $target"); shell("ime set $target") + val baselineByOrientation = mutableMapOf() + val orientations = if (InstrumentationRegistry.getArguments().getString("feverOrientation") == "landscape") listOf(true) else listOf(false, true) + for (landscape in orientations) for (full in listOf(false, true)) { + assertTrue(ui.setRotation(if (landscape) android.app.UiAutomation.ROTATION_FREEZE_90 else android.app.UiAutomation.ROTATION_FREEZE_0)) + prefs.edit().putString("keyboard_skin_preference", "fever") + .putString("keyboard_order_preference", "[\"TENKEY\"]").putBoolean("save_last_used_keyboard", false) + .putBoolean("keyboard_floating_preference", floating) + .putInt("keyboard_floating_position_x", 24).putInt("keyboard_floating_position_y", 160) + .putInt("keyboard_width_preference", if (floating) 70 else 100) + .putInt("keyboard_width_landscape_preference", if (floating) 70 else 100) + .putBoolean(FeverSettings.FULL_SCREEN, full).putInt(FeverSettings.HIT_RATE, 100) + .putInt(FeverSettings.COMBO_SECONDS, 10).putBoolean("key_sound_preference", false).commit() + println("FEVER case floating=$floating landscape=$landscape full=$full") + val activity = startEditor(landscape) + var recordingDone: java.util.concurrent.CountDownLatch? = null + try { + println("FEVER activity ready") + key("key_1"); SystemClock.sleep(1000) + val root = ime() + assertCapBindings(floating) + var jackpotSeen = false + var lastCharacterAt = 0L + var recordedCombo = 0 + val cuesSeen = mutableSetOf() + val mode = "${if (floating) "floating" else "docked"}-${if (landscape) "landscape" else "portrait"}-${if (full) "screen" else "keyboard"}" + val captured = java.util.concurrent.CountDownLatch(1) + val captureFailure = java.util.concurrent.atomic.AtomicReference() + ins.runOnMainSync { root.controller.eventObserver = { + println("FEVER event combo=${it.combo} award=${it.award} cue=${it.cue} time=${SystemClock.uptimeMillis()}") + if (it.cue == null && it.combo > recordedCombo) { + recordedCombo = it.combo; lastCharacterAt = SystemClock.uptimeMillis() + } + it.cue?.let(cuesSeen::add) + if (it.award == FeverAward.JACKPOT) { + jackpotSeen = true + Thread { + try { SystemClock.sleep(500); capture(mode) } + catch (error: Throwable) { captureFailure.set(error) } + finally { captured.countDown() } + }.start() + } + } } + val before = stableKeyboardBounds(floating) + ins.runOnMainSync { + val location = IntArray(2).also(root.rootView::getLocationOnScreen) + println("FEVER geometry mode=$mode keyboard=$before host=${root.rootView.width}x${root.rootView.height} at=${location.toList()}") + } + // Compare both modes at the same saved panel position, independent of auto-placement. + if (!full) baselineByOrientation[landscape] = before + else assertEquals("Screen effects must retain the keyboard-only geometry", baselineByOrientation[landscape], before) + var labelBefore: Bitmap? = null + for (i in 1..12) { + if (i == 1 && !floating && !landscape && full && + InstrumentationRegistry.getArguments().getString("feverVideo") == "true") { + val recording = ui.executeShellCommand("screenrecord --time-limit 12 /sdcard/Download/fever-demo.mp4") + val done = java.util.concurrent.CountDownLatch(1) + recordingDone = done + Thread { try { ParcelFileDescriptor.AutoCloseInputStream(recording).use { it.readBytes() } } finally { done.countDown() } }.start() + } + if (i == 1) { + val cap = Rect().also(key("key_1")::getBoundsInScreen) + val down = SystemClock.uptimeMillis() + fun touch(action: Int, dx: Float = 0f) { + val event = MotionEvent.obtain(down, SystemClock.uptimeMillis(), action, + cap.centerX().toFloat() + dx, cap.centerY().toFloat(), 0) + event.source = android.view.InputDevice.SOURCE_TOUCHSCREEN + try { assertTrue(ui.injectInputEvent(event, true)) } finally { event.recycle() } + } + touch(MotionEvent.ACTION_DOWN); SystemClock.sleep(70) + ins.runOnMainSync { assertEquals("Down must not count a character", 0, root.controller.combo) } + touch(MotionEvent.ACTION_MOVE, 5f); SystemClock.sleep(40) + ins.runOnMainSync { assertEquals("Move must not count a character", 0, root.controller.combo) } + capture("$mode-move") + touch(MotionEvent.ACTION_UP, 5f) + } else tap(if (i % 2 == 0) "key_2" else "key_1") + await { var count = 0; ins.runOnMainSync { count = root.controller.combo }; count == i } + if (i == 9) labelBefore = keyLabelBitmap() + if (i == 10) { + SystemClock.sleep(350) + val labelDuring = keyLabelBitmap() + assertLabelLegible(requireNotNull(labelBefore), labelDuring, mode) + labelBefore?.recycle(); labelDuring.recycle() + capture("$mode-combo10") + assertTrue("Candidates must remain accessible during a celebration", nodes().any { + it.isVisibleToUser && it.viewIdResourceName == "${context.packageName}:id/suggestion_item_text_view" && !it.text.isNullOrBlank() + }) + } + } + ins.runOnMainSync { assertEquals("One combo per real tap", 12, root.controller.combo) } + fun assertEditingDoesNotCount() { + // Shell input can stall for seconds on an emulator under load. Editing must + // not extend the real combo deadline even when the test itself takes longer. + val expected = if (SystemClock.uptimeMillis() - lastCharacterAt >= root.controller.settings.comboTimeoutMillis) 0 else 12 + assertEquals("Editing must not count or extend the deadline", expected, root.controller.combo) + } + tap("key_delete") + ins.runOnMainSync { + assertEditingDoesNotCount() + assertTrue(cuesSeen.contains(FeverCue.DELETE)) + } + tap("key_soft_left"); tap("key_move_cursor_right") + ins.runOnMainSync { + assertTrue(cuesSeen.contains(FeverCue.CURSOR_LEFT)) + assertTrue(cuesSeen.contains(FeverCue.CURSOR_RIGHT)) + } + tap("key_space"); tap("key_space") + ins.runOnMainSync { + assertTrue("Conversion should have its own cue", cuesSeen.contains(FeverCue.CONVERSION)) + assertTrue("Changing a candidate should have its own cue", cuesSeen.contains(FeverCue.CANDIDATE)) + assertTrue("Showing candidates should have its own cue", cuesSeen.contains(FeverCue.CANDIDATES)) + assertEditingDoesNotCount() + } + SystemClock.sleep(160); capture("$mode-candidate") + // Exercise both tapping the actual candidate and the enter/confirm key. + SystemClock.sleep(2100) + if (full) tap("suggestion_item_text_view") else tap("key_enter") + ins.runOnMainSync { assertTrue("Successful confirmation must produce a jackpot at 100%", jackpotSeen) } + assertTrue("Capture a frame while the award is visible", captured.await(5, java.util.concurrent.TimeUnit.SECONDS)) + captureFailure.get()?.let { throw it } + val after = stableKeyboardBounds(floating) + assertEquals("Celebrations must not change key geometry", before, after) + var touches = 0 + lateinit var editorBounds: Rect + ins.runOnMainSync { + assertTrue(activity.editText.text.isNotEmpty()) + activity.editText.setOnTouchListener { _, event -> if (event.actionMasked == MotionEvent.ACTION_DOWN) touches++; false } + val location = IntArray(2).also(activity.editText::getLocationOnScreen) + editorBounds = Rect(location[0], location[1], location[0] + activity.editText.width, location[1] + activity.editText.height) + } + var obstruction = Rect() + ins.runOnMainSync { + val point = IntArray(2).also(root::getLocationOnScreen) + obstruction = Rect(point[0], point[1], point[0] + root.width, point[1] + root.height) + } + if (floating) ins.runOnMainSync { + val panel = WindowInspector.getGlobalWindowViews().first { + it is com.kazumaproject.markdownhelperkeyboard.ime_service.floating_keyboard.FloatingKeyboardPanel + } + val point = IntArray(2).also(panel::getLocationOnScreen) + obstruction = Rect(point[0], point[1], point[0] + panel.width, point[1] + panel.height) + } + // The floating panel includes a drag handle and candidates above the key bounds. + // Choose an exposed part of the editor, outside that entire input window. + var safe = Rect() + ins.runOnMainSync { + val decor = activity.window.decorView + val insets = androidx.core.view.ViewCompat.getRootWindowInsets(decor)?.getInsetsIgnoringVisibility( + androidx.core.view.WindowInsetsCompat.Type.systemBars() or androidx.core.view.WindowInsetsCompat.Type.displayCutout()) + safe = Rect(insets?.left ?: 0, insets?.top ?: 0, + decor.width - (insets?.right ?: 0), decor.height - (insets?.bottom ?: 0)) + } + val safeTop = maxOf(editorBounds.top, safe.top + 1) + val safeBottom = minOf(editorBounds.bottom, safe.bottom - 1) + val safeLeft = maxOf(editorBounds.left, safe.left + 1) + val safeRight = minOf(editorBounds.right, safe.right - 1) + val gaps = listOf( + Rect(safeLeft, safeTop, safeRight, minOf(safeBottom, obstruction.top)), + Rect(safeLeft, maxOf(safeTop, obstruction.bottom), safeRight, safeBottom), + Rect(safeLeft, safeTop, minOf(safeRight, obstruction.left), safeBottom), + Rect(maxOf(safeLeft, obstruction.right), safeTop, safeRight, safeBottom) + ).filter { !it.isEmpty } + val gap = gaps.maxByOrNull { it.width() * it.height() } + ?: error("No exposed editor region: editor=$editorBounds obstruction=$obstruction") + println("FEVER editor touch ($mode): $gap outside $obstruction") + shell("input tap ${gap.centerX()} ${gap.centerY()}") + ins.runOnMainSync { assertEquals("The app behind the effect must receive touch", 1, touches) } + ins.runOnMainSync { activity.getSystemService(InputMethodManager::class.java).hideSoftInputFromWindow(activity.editText.windowToken, 0) } + await { var combo = -1; ins.runOnMainSync { combo = root.controller.combo }; combo == 0 } + } finally { + recordingDone?.await(16, java.util.concurrent.TimeUnit.SECONDS) + ins.runOnMainSync { activity.finish() } + } + } + if (!floating) { + check(ui.setRotation(android.app.UiAutomation.ROTATION_FREEZE_0)) + prefs.edit().putBoolean(FeverSettings.FULL_SCREEN, false).commit() + verifyNumericAndPasswordEditors() + } + } finally { + restorePreferences(prefs, saved) + if (originalAnimations == "null") shell("settings delete global animator_duration_scale") + else shell("settings put global animator_duration_scale $originalAnimations") + shell("ime set $original"); if (!enabled) shell("ime disable $target") + ui.setRotation(android.app.UiAutomation.ROTATION_UNFREEZE) + } + } +} diff --git a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FeverPerformanceInstrumentedTest.kt b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FeverPerformanceInstrumentedTest.kt new file mode 100644 index 000000000..d490e8861 --- /dev/null +++ b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FeverPerformanceInstrumentedTest.kt @@ -0,0 +1,279 @@ +package com.kazumaproject.markdownhelperkeyboard + +import android.app.UiAutomation +import android.graphics.PointF +import android.inputmethodservice.InputMethodService +import android.os.* +import android.view.* +import android.view.inspector.WindowInspector +import androidx.preference.PreferenceManager +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry +import androidx.test.runner.lifecycle.Stage +import com.kazumaproject.markdownhelperkeyboard.converter.candidate.Candidate +import com.kazumaproject.markdownhelperkeyboard.ime_service.fever.FeverInputFrameLayout +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.StateFlow +import kotlin.coroutines.intrinsics.suspendCoroutineUninterceptedOrReturn +import com.kazumaproject.markdownhelperkeyboard.converter.session.KanaKanjiQueryResult +import com.kazumaproject.markdownhelperkeyboard.converter.session.CandidateQueryMode +import org.junit.Assert.* +import org.junit.Assume.assumeTrue +import org.junit.Test +import org.junit.runner.RunWith +import org.json.JSONArray +import org.json.JSONObject +import java.io.File +import java.util.Collections + +/** + * Opt-in real-IME benchmark; refuses to change the user's installed application. + * candidateReadyMs ends at the IME's candidate-refresh notification, before asynchronous diff + * completion/presentation. frameMs comes from the IME window, not the editor activity. + * feverCoreProbe measures the complete core entry point after timed typing, in a separate trial. + */ +@RunWith(AndroidJUnit4::class) +@androidx.test.filters.SdkSuppress(minSdkVersion = 29) +class FeverPerformanceInstrumentedTest { + private val ins = InstrumentationRegistry.getInstrumentation() + private val ui get() = ins.uiAutomation + private val context get() = ins.targetContext + private fun shell(command: String) = ParcelFileDescriptor.AutoCloseInputStream(ui.executeShellCommand(command)) + .bufferedReader().use { it.readText().trim() } + private fun await(what: String, test: () -> Boolean) { + val until = SystemClock.uptimeMillis() + 60_000 + while (SystemClock.uptimeMillis() < until) { if (test()) return; SystemClock.sleep(10) } + error("Timed out: $what") + } + private inline fun field(instance: Any, name: String): T = + instance.javaClass.getDeclaredField(name).apply { isAccessible = true }.get(instance) as T + private fun root(): FeverInputFrameLayout? { + fun find(v: View): FeverInputFrameLayout? { + if (v is FeverInputFrameLayout && v.isShown) return v + if (v is ViewGroup) for (i in 0 until v.childCount) find(v.getChildAt(i))?.let { return it } + return null + } + return WindowInspector.getGlobalWindowViews().firstNotNullOfOrNull(::find) + } + private fun memory(): JSONObject { + val info = Debug.MemoryInfo().also(Debug::getMemoryInfo) + val runtime = Runtime.getRuntime() + return JSONObject().put("pssKiB", info.totalPss).put("javaPssKiB", info.dalvikPss) + .put("nativePssKiB", info.nativePss).put("graphicsKiB", info.getMemoryStat("summary.graphics")) + .put("usedJavaBytes", runtime.totalMemory() - runtime.freeMemory()) + } + @Test fun longTypingConversionAndRendering() { + val args = InstrumentationRegistry.getArguments() + assumeTrue(args.getString("feverPerf") == "true") + check(context.packageName.startsWith("com.kazumaproject.skinfidelity.")) + val skin = args.getString("feverPerfSkin") ?: "fever" + require(skin in listOf("default", "fever")) + val label = args.getString("feverPerfLabel") ?: "local" + require(label.matches(Regex("[a-zA-Z0-9_-]+"))) + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + val saved = prefs.all.toMap() + val originalIme = shell("settings get secure default_input_method") + val target = "${context.packageName}/com.kazumaproject.markdownhelperkeyboard.ime_service.IMEService" + val enabled = target in shell("ime list -s").lines() + var activity: FastInputHostActivity? = null + val thread = HandlerThread("fever-frame-metrics").apply { start() } + val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate) + var window: Window? = null + val frames = Collections.synchronizedList(mutableListOf()) + val measuring = java.util.concurrent.atomic.AtomicBoolean(false) + val listener = Window.OnFrameMetricsAvailableListener { _, metrics, _ -> + if (measuring.get()) frames.add(metrics.getMetric(FrameMetrics.TOTAL_DURATION) / 1e6) + } + val full = args.getString("feverPerfFullScreen") != "false" + val probeCore = args.getString("feverCoreProbe") == "true" + val report = JSONObject().put("label", label).put("device", Build.MODEL).put("sdk", Build.VERSION.SDK_INT) + .put("skin", skin).put("fullScreen", full).put("intensity", 2).put("liveConversion", true).put("intervalMs", 120) + .put("coreProbe", probeCore).put("animatorScale", shell("settings get global animator_duration_scale")) + val results = JSONArray() + val file = File(context.getExternalFilesDir(null), "fever-perf/$label.json") + file.parentFile!!.mkdirs() + try { + check(ui.setRotation(UiAutomation.ROTATION_FREEZE_0)) + prefs.edit().putString("keyboard_skin_preference", skin) + .putString("keyboard_order_preference", "[\"TENKEY\"]").putBoolean("save_last_used_keyboard", false) + .putBoolean("keyboard_floating_preference", false).putInt("keyboard_width_preference", 100) + .putBoolean("live_conversion_preference", true).putInt("n_best_preference", 4) + .putBoolean("fever_full_screen", full).putInt("fever_intensity", 2) + .putInt("fever_hit_rate", 5).putInt("fever_combo_seconds", 3) + .putBoolean("key_sound_preference", false).putBoolean("fever_sound", false) + .putBoolean("fever_vibration", false).commit() + shell("ime enable $target"); shell("ime set $target") + shell("am start -n ${context.packageName}/com.kazumaproject.markdownhelperkeyboard.FastInputHostActivity") + var keyboard: FeverInputFrameLayout? = null + await("real editor and FEVER IME") { + ins.runOnMainSync { + activity = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED) + .filterIsInstance().firstOrNull() + keyboard = root() + } + activity != null && keyboard != null && keyboard!!.width > 0 + } + SystemClock.sleep(1800) + val service = keyboard!!.context as InputMethodService + report.put("conversionBackend", field(service, "conversionBackend").toString()) + .put("beamWidth", field(service, "conversionBeamWidth")) + .put("dictionaryCategories", JSONArray(field>(service, "enabledDictionaryCategories").map { it.toString() }.sorted())) + val input: StateFlow = field(service, "_inputString") + val candidates: StateFlow>?> = field(service, "customScreenCandidateResult") + val sent = mutableMapOf() + val ready = mutableMapOf() + val fingerprints = mutableMapOf() + ins.runOnMainSync { + report.put("refreshHz", keyboard!!.display.refreshRate) + window = service.window!!.window!! + window!!.addOnFrameMetricsAvailableListener(listener, Handler(thread.looper)) + scope.launch { + candidates.collect { result -> + if (result != null && result.second.isNotEmpty()) sent[result.first]?.let { start -> + ready.putIfAbsent(result.first, (SystemClock.elapsedRealtimeNanos() - start) / 1e6) + // Retain only the final list; serializing every prefix would inflate + // the measured allocation and heap usage with benchmark bookkeeping. + if (result.first.length == 120) fingerprints[result.first] = + result.second.joinToString("|") { "${it.string}:${it.type}:${it.score}" } + + } + } + } + } + val sentence = "わたしはきのうあたらしいみせにいきましたあしたはともにこうえんをあるいてからいえにかえります" + val reading = (sentence + sentence + sentence).take(120) + // Dispatch actual TenKey gestures on main. Accessibility's idle wait would serialize effects. + val rows = listOf("あいうえお", "かきくけこ", "さしすせそ", "たちつてと", "なにぬねの", + "はひふへほ", "まみむめも", "や(ゆ)よ", "らりるれろ", "わをんー〜") + fun type(ch: Char) { + val row = rows.indexOfFirst { ch in it }; check(row >= 0) + val index = rows[row].indexOf(ch) + ins.runOnMainSync { + keyboard = requireNotNull(root()) + val keyId = context.resources.getIdentifier("key_${if (row == 9) 11 else row + 1}", "id", context.packageName) + val cap = keyboard!!.findViewById(keyId) + check(cap != null && cap.isShown) + val rootLocation = IntArray(2).also(keyboard!!::getLocationOnScreen) + val capLocation = IntArray(2).also(cap::getLocationOnScreen) + val x = capLocation[0] - rootLocation[0] + cap.width / 2f + val y = capLocation[1] - rootLocation[1] + cap.height / 2f + val distance = 48 * context.resources.displayMetrics.density + val offset = when (index) { 1 -> PointF(-distance, 0f); 2 -> PointF(0f, -distance) + 3 -> PointF(distance, 0f); 4 -> PointF(0f, distance); else -> PointF() } + val down = SystemClock.uptimeMillis() + fun dispatch(action: Int, dx: Float, dy: Float) { + val event = MotionEvent.obtain(down, SystemClock.uptimeMillis(), action, x + dx + rootLocation[0], y + dy + rootLocation[1], 0) + event.offsetLocation(-rootLocation[0].toFloat(), -rootLocation[1].toFloat()) + event.source = InputDevice.SOURCE_TOUCHSCREEN + try { check(keyboard!!.dispatchTouchEvent(event)) } finally { event.recycle() } + } + dispatch(MotionEvent.ACTION_DOWN, 0f, 0f) + if (index != 0) dispatch(MotionEvent.ACTION_MOVE, offset.x, offset.y) + dispatch(MotionEvent.ACTION_UP, offset.x, offset.y) + } + } + reading.take(20).forEach { type(it); SystemClock.sleep(120) } + repeat(args.getString("feverPerfTrials")?.toIntOrNull() ?: 3) { trial -> + ins.runOnMainSync { activity!!.restartEditorInput(true) } + await("composition cleared") { input.value.isEmpty() } + SystemClock.sleep(2500) + System.gc(); SystemClock.sleep(200) + ins.runOnMainSync { sent.clear(); ready.clear(); fingerprints.clear() } + frames.clear() + val before = memory() + val statsBefore = Debug.getRuntimeStats() + val dispatchMs = mutableListOf() + val activeMemory = JSONArray() + val serial = JSONArray() + measuring.set(true) + val begin = SystemClock.elapsedRealtimeNanos() + reading.forEachIndexed { index, ch -> + val start = SystemClock.elapsedRealtimeNanos() + ins.runOnMainSync { sent[reading.take(index + 1)] = SystemClock.elapsedRealtimeNanos() } + val dispatch = SystemClock.elapsedRealtimeNanos() + type(ch) + dispatchMs.add((SystemClock.elapsedRealtimeNanos() - dispatch) / 1e6) + assertEquals("Reading at index $index", reading.take(index + 1), input.value) + if (index + 1 in listOf(24, 64, 120)) { + await("candidates length ${index + 1}") { + var done = false; ins.runOnMainSync { done = ready.containsKey(reading.take(index + 1)) }; done + } + ins.runOnMainSync { serial.put(JSONObject().put("length", index + 1).put("readyMs", ready.getValue(reading.take(index + 1)))) } + activeMemory.put(memory()) + } + val remaining = 120 - (SystemClock.elapsedRealtimeNanos() - start) / 1_000_000 + if (remaining > 0) SystemClock.sleep(remaining) + } + measuring.set(false) + val statsAfter = Debug.getRuntimeStats() + val samples = synchronized(frames) { frames.toList() } + val out = JSONObject().put("trial", trial).put("characters", reading.length) + .put("elapsedMs", (SystemClock.elapsedRealtimeNanos() - begin) / 1e6) + .put("beforeMemory", before).put("activeMemory", activeMemory).put("endMemory", memory()) + .put("dispatchMs", JSONArray(dispatchMs)).put("frameMs", JSONArray(samples)).put("settledConversion", serial) + .put("allocatedBytes", statsAfter.getValue("art.gc.bytes-allocated").toLong() - statsBefore.getValue("art.gc.bytes-allocated").toLong()) + .put("gcCount", statsAfter.getValue("art.gc.gc-count").toLong() - statsBefore.getValue("art.gc.gc-count").toLong()) + ins.runOnMainSync { + out.put("candidateReadyMs", JSONArray(ready.values.toList())).put("candidateUpdates", ready.size) + .put("finalCandidates", fingerprints[reading]) + assertTrue(candidates.value?.second?.isNotEmpty() == true) + assertEquals(if (skin == "fever") 120 else 0, keyboard!!.controller.combo) + } + if (probeCore) { + // A separate diagnostic measures the complete real core entry point. Stage + // counters alone omit work performed by some original-candidate search paths. + SystemClock.sleep(1000) + val sessionField = service.javaClass.getDeclaredField("kanaKanjiConversionSession").apply { isAccessible = true } + val savedSession = sessionField.get(service) + val method = service.javaClass.declaredMethods.single { it.name == "queryKanaKanjiCore" } + .apply { isAccessible = true } + val dispatcher: CoroutineDispatcher = field(service, "kanaKanjiConversionDispatcher") + val learnRepository = service.javaClass.getDeclaredMethod("learnedRepositoryForSuggestion") + .apply { isAccessible = true }.invoke(service) + val wholeCore = JSONArray() + suspend fun query(text: String): KanaKanjiQueryResult = suspendCoroutineUninterceptedOrReturn { continuation -> + method.invoke(service, text, CandidateQueryMode.NO_TAB_DEFAULT, learnRepository, continuation) + } + try { + runBlocking { withContext(dispatcher) { + for (length in listOf(24, 64, 120)) repeat(10) { iteration -> + // Warm dictionaries, a fresh composition session, the service's + // actual backend/preferences, and no synthetic candidate rendering. + sessionField.set(service, null) + val start = SystemClock.elapsedRealtimeNanos() + val result = query(reading.take(length)) + val elapsed = (SystemClock.elapsedRealtimeNanos() - start) / 1e6 + check(result.candidates.isNotEmpty()) + wholeCore.put(JSONObject().put("length", length).put("iteration", iteration) + .put("ms", elapsed).put("candidateCount", result.candidates.size) + .put("fingerprint", result.candidates.joinToString("|") { "${it.string}:${it.type}:${it.score}" })) + } + } } + } finally { sessionField.set(service, savedSession) } + out.put("freshSessionCore", wholeCore) + } + results.put(out) + report.put("trials", results) + file.writeText(report.toString(2)) + println("FEVER_PERF $label trial=$trial frames=${samples.size} elapsedMs=${out.getDouble("elapsedMs")} alloc=${out.getLong("allocatedBytes")}") + } + report.put("trials", results) + file.writeText(report.toString(2)) + println("FEVER_PERF report=$file") + } finally { + measuring.set(false) + ins.runOnMainSync { scope.cancel(); window?.removeOnFrameMetricsAvailableListener(listener); activity?.finish() } + val edit = prefs.edit().clear() + saved.forEach { (key, value) -> when(value) { + is String -> edit.putString(key, value); is Boolean -> edit.putBoolean(key, value) + is Int -> edit.putInt(key, value); is Long -> edit.putLong(key, value); is Float -> edit.putFloat(key, value) + is Set<*> -> @Suppress("UNCHECKED_CAST") edit.putStringSet(key, value as Set) + } } + check(edit.commit()) + shell("ime set $originalIme"); if (!enabled) shell("ime disable $target") + ui.setRotation(UiAutomation.ROTATION_UNFREEZE) + thread.quitSafely() + } + } +} diff --git a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FeverSettingsDeviceTest.kt b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FeverSettingsDeviceTest.kt new file mode 100644 index 000000000..542e6aa02 --- /dev/null +++ b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FeverSettingsDeviceTest.kt @@ -0,0 +1,199 @@ +package com.kazumaproject.markdownhelperkeyboard + +import android.os.SystemClock +import android.view.accessibility.AccessibilityNodeInfo +import com.google.android.material.tabs.TabLayout +import com.kazumaproject.markdownhelperkeyboard.setting_activity.ui.setting.SettingTabRegistry +import androidx.fragment.app.Fragment +import androidx.navigation.fragment.NavHostFragment +import androidx.preference.PreferenceManager +import androidx.preference.SeekBarPreference +import androidx.test.core.app.ActivityScenario +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import com.kazumaproject.markdownhelperkeyboard.ime_service.fever.FeverSettings +import com.kazumaproject.markdownhelperkeyboard.setting_activity.MainActivity +import com.kazumaproject.markdownhelperkeyboard.setting_activity.awaitSettingsContentReady +import com.kazumaproject.markdownhelperkeyboard.setting_activity.ui.keyboard_theme.FeverSettingsFragment +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class FeverSettingsDeviceTest { + private val ins = InstrumentationRegistry.getInstrumentation() + private val context get() = ins.targetContext + private fun descendants(fragment: Fragment): List = listOf(fragment) + fragment.childFragmentManager.fragments.flatMap(::descendants) + private fun click(text: String) { + val deadline = SystemClock.uptimeMillis() + 15000 + fun find(node: AccessibilityNodeInfo): AccessibilityNodeInfo? { + if (node.isVisibleToUser && node.text?.toString() == text) return node + for (i in 0 until node.childCount) node.getChild(i)?.let { find(it)?.let { hit -> return hit } } + return null + } + while (SystemClock.uptimeMillis() < deadline) { + var target = ins.uiAutomation.rootInActiveWindow?.let(::find) + while (target != null) { + if (target.isClickable && target.isEnabled && target.performAction(AccessibilityNodeInfo.ACTION_CLICK)) { ins.waitForIdleSync(); return } + target = target.parent + } + SystemClock.sleep(100) + } + error("Missing settings entry: $text; UI: " + ins.uiAutomation.rootInActiveWindow?.let { root -> + fun labels(node: AccessibilityNodeInfo): String = node.text.toString() + " " + + (0 until node.childCount).joinToString { node.getChild(it)?.let(::labels).orEmpty() } + labels(root) + }) + } + @Test(timeout = 180000) fun newAndLegacySettingsShareFeverControlsAndRetainThemAfterRecreation() { + check(context.packageName.startsWith("com.kazumaproject.skinfidelity")) + val prefs = PreferenceManager.getDefaultSharedPreferences(context) + fun shell(command: String) = android.os.ParcelFileDescriptor.AutoCloseInputStream(ins.uiAutomation.executeShellCommand(command)).bufferedReader().use { it.readText().trim() } + val target = "${context.packageName}/com.kazumaproject.markdownhelperkeyboard.ime_service.IMEService" + val originallyEnabled = target in shell("ime list -s").lines() + val originalSkin = prefs.getString("keyboard_skin_preference", null) + val original = prefs.all.filterKeys { it.startsWith(FeverSettings.PREFIX) } + try { + shell("ime enable $target") + prefs.edit().putString("keyboard_skin_preference", "fever").putBoolean(FeverSettings.FULL_SCREEN, false).commit() + for (legacy in listOf(false, true)) { + ActivityScenario.launch(MainActivity::class.java).use { scenario -> + scenario.awaitSettingsContentReady() + scenario.onActivity { + val host = it.supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment + host.navController.navigate(if (legacy) R.id.settingMainFragment else R.id.keyboardThemeFragment) + } + if (legacy) { + var selected = false + val deadline = SystemClock.uptimeMillis() + 15000 + while (!selected && SystemClock.uptimeMillis() < deadline) { + scenario.onActivity { activity -> + val host = activity.supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment + val current = host.childFragmentManager.primaryNavigationFragment + when (current) { + is com.kazumaproject.markdownhelperkeyboard.setting_activity.ui.setting.SettingMainFragment -> { + val index = SettingTabRegistry.createTabs().indexOfFirst { it.key == SettingTabRegistry.TAB_THEME } + activity.findViewById(R.id.setting_tab_layout).getTabAt(index)?.select() + selected = current.childFragmentManager.fragments.any { + it is com.kazumaproject.markdownhelperkeyboard.setting_activity.ui.keyboard_theme.KeyboardThemeFragment && it.isResumed + } + } + is com.kazumaproject.markdownhelperkeyboard.setting_activity.ui.setting.KeyboardDisplayPreferenceFragment -> + current.findPreference("setting_route_keyboard_theme")?.performClick() + is com.kazumaproject.markdownhelperkeyboard.setting_activity.ui.keyboard_theme.KeyboardThemeFragment -> selected = current.isResumed + } + } + SystemClock.sleep(100) + } + check(selected) + } + scenario.awaitSettingsContentReady() + for (skin in listOf("default", "fever", "cupertino_dark", "fever")) { + scenario.onActivity { activity -> + val host = activity.supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment + host.navController.navigate(R.id.keyboardSkinSelectionFragment) + } + ins.waitForIdleSync() + prefs.edit().putString("keyboard_skin_preference", skin).commit() + scenario.onActivity { activity -> + val host = activity.supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment + check(host.navController.popBackStack()) + } + val deadline = SystemClock.uptimeMillis() + 15000 + var correct = false + var observed = "" + while (!correct && SystemClock.uptimeMillis() < deadline) { + scenario.onActivity { activity -> + val theme = activity.supportFragmentManager.fragments.flatMap(::descendants) + .filterIsInstance() + .firstOrNull { it.isResumed } + val entry = theme?.findPreference("fever_settings") + val category = theme?.findPreference("settings_theme_skin") + category?.setExpanded(false, persist = false) + if (entry != null) assertFalse("Collapsed heading must hide FEVER settings", entry.isVisible) + category?.setExpanded(true, persist = false) + val host = activity.supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment + observed = "destination=${host.navController.currentDestination?.displayName} " + + "theme=$theme entry=$entry visible=${entry?.isVisible} " + + "stored=${prefs.getString("keyboard_skin_preference", null)} " + + "selected=${com.kazumaproject.markdownhelperkeyboard.setting_activity.AppPreference.keyboardSkin}" + correct = entry != null && entry.isVisible == (skin == "fever") + if (skin == "fever" && entry != null) assertTrue(entry.isEnabled) + } + SystemClock.sleep(100) + } + assertTrue("FEVER visibility legacy=$legacy skin=$skin $observed", correct) + } + click(context.getString(R.string.fever_settings_title)) + click(context.getString(R.string.fever_full_screen)) + assertEquals(!legacy, prefs.getBoolean(FeverSettings.FULL_SCREEN, false)) + scenario.onActivity { activity -> + val fragment = activity.supportFragmentManager.fragments.flatMap(::descendants).filterIsInstance().first() + val expected = if (legacy) 27 else 73 + fragment.findPreference(FeverSettings.HIT_RATE)!!.value = expected + assertEquals(expected, FeverSettings.read(prefs).hitRate) + } + scenario.recreate(); scenario.awaitSettingsContentReady() + assertEquals(!legacy, FeverSettings.read(prefs).fullScreen) + assertEquals(if (legacy) 27 else 73, FeverSettings.read(prefs).hitRate) + // Open the common screen through each real search scope as well. + scenario.onActivity { activity -> + val host = activity.supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment + host.navController.navigate(R.id.settingSearchFragment, androidx.core.os.bundleOf( + com.kazumaproject.markdownhelperkeyboard.setting_activity.ui.setting.SettingSearchFragment.ARG_SEARCH_SCOPE to + (if (legacy) "LEGACY_TABS" else "NEW_HOME") + )) + } + val deadline = SystemClock.uptimeMillis() + 15000 + var searched = false + while (!searched && SystemClock.uptimeMillis() < deadline) { + scenario.onActivity { activity -> + activity.findViewById(R.id.setting_search_input)?.let { + it.setText("fever"); searched = true + } + } + SystemClock.sleep(100) + } + check(searched) + click(context.getString(R.string.fever_settings_title)) + click(context.getString(R.string.fever_preview)) + SystemClock.sleep(500) + val bitmap = requireNotNull(ins.uiAutomation.takeScreenshot()) + val evidence = java.io.File(context.getExternalFilesDir(null), "fever-evidence").apply { mkdirs() } + java.io.File(evidence, "settings-preview-$legacy.png").outputStream().use { + bitmap.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, it) + } + bitmap.recycle() + click(context.getString(android.R.string.ok)) + fun editWords(value: String?) { + scenario.onActivity { activity -> + val fragment = activity.supportFragmentManager.fragments.flatMap(::descendants).filterIsInstance().first { it.isResumed } + fragment.findPreference(FeverSettings.KEYWORDS)!!.performClick() + } + if (value != null) ins.runOnMainSync { + fun find(view: android.view.View): android.widget.EditText? { + if (view is android.widget.EditText && view.isShown) return view + if (view is android.view.ViewGroup) for (i in 0 until view.childCount) find(view.getChildAt(i))?.let { return it } + return null + } + val editor = android.view.inspector.WindowInspector.getGlobalWindowViews().firstNotNullOf(::find) + editor.setText(value) + } + click(context.getString(if (value == null) R.string.fever_restore_words else android.R.string.ok)) + } + editWords("FEVER\nテスト") + assertEquals(listOf("FEVER", "テスト"), FeverSettings.read(prefs).keywords) + editWords(""); assertTrue(FeverSettings.read(prefs).keywords.isEmpty()) + editWords(null); assertEquals(FeverSettings.DEFAULT_KEYWORDS, FeverSettings.read(prefs).keywords) + } + } + } finally { + val edit = prefs.edit() + prefs.all.keys.filter { it.startsWith(FeverSettings.PREFIX) }.forEach(edit::remove) + original.forEach { (k,v) -> when(v) { is Boolean -> edit.putBoolean(k,v); is Int -> edit.putInt(k,v); is String -> edit.putString(k,v) } } + if (originalSkin == null) edit.remove("keyboard_skin_preference") else edit.putString("keyboard_skin_preference", originalSkin) + check(edit.commit()) + if (!originallyEnabled) shell("ime disable $target") + } + } +} diff --git a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FeverSoundInstrumentedTest.kt b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FeverSoundInstrumentedTest.kt new file mode 100644 index 000000000..61c1e48b9 --- /dev/null +++ b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FeverSoundInstrumentedTest.kt @@ -0,0 +1,52 @@ +package com.kazumaproject.markdownhelperkeyboard + +import android.media.AudioManager +import android.os.SystemClock +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import com.kazumaproject.markdownhelperkeyboard.ime_service.fever.* +import org.junit.Assert.* +import org.junit.Test +import org.junit.runner.RunWith + +@RunWith(AndroidJUnit4::class) +class FeverSoundInstrumentedTest { + @Test(timeout = 15000) fun generatedSamplesDecodeAndStartOnTheMediaStreamWithoutRecordingPermission() { + val ins = InstrumentationRegistry.getInstrumentation() + val context = ins.targetContext + check(context.packageName.startsWith("com.kazumaproject.skinfidelity")) + val audio = context.getSystemService(AudioManager::class.java) + assertEquals(AudioManager.RINGER_MODE_NORMAL, audio.ringerMode) + assertEquals(android.content.pm.PackageManager.PERMISSION_DENIED, + context.checkSelfPermission(android.Manifest.permission.RECORD_AUDIO)) + lateinit var feedback: FeverFeedback + fun field(name: String): Any? = feedback.javaClass.getDeclaredField(name).apply { isAccessible = true }.get(feedback) + ins.runOnMainSync { feedback = FeverFeedback(context) } + try { + val deadline = SystemClock.uptimeMillis() + 5000 + var ready = 0 + while (ready < 3 && SystemClock.uptimeMillis() < deadline) { + ins.runOnMainSync { ready = (field("ready") as Set<*>).size } + SystemClock.sleep(20) + } + assertEquals("All generated PCM files must decode", 3, ready) + ins.runOnMainSync { + assertTrue("Temporary wave files must be deleted after loading", (field("pendingFiles") as Map<*, *>).isEmpty()) + feedback.playEvent(FeverEvent(10, FeverAward.JACKPOT), FeverSettings(sound = true, vibration = false, volume = 100), false) + assertTrue("SoundPool must return a live stream", (field("streams") as List<*>).any { it is Int && it > 0 }) + } + // The public playback list anonymizes attributes without privileged audio access. + // Inspect AudioService through the test shell instead of adding app permissions. + SystemClock.sleep(220) + val report = android.os.ParcelFileDescriptor.AutoCloseInputStream( + ins.uiAutomation.executeShellCommand("dumpsys audio")).bufferedReader().use { it.readText() } + val player = report.lineSequence().lastOrNull { + it.contains("uid/pid:${android.os.Process.myUid()}/${android.os.Process.myPid()}") && it.contains("USAGE_GAME") + } + assertNotNull("AudioService must register our media sound pool", player) + val id = Regex("piid:([0-9]+)").find(requireNotNull(player))!!.groupValues[1] + assertTrue("AudioService must receive playback start", report.contains("player piid:$id event:started")) + println("FEVER synthesized audio PASS: 3 decoded samples, live media stream, no RECORD_AUDIO grant") + } finally { ins.runOnMainSync { feedback.release() } } + } +} diff --git a/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FreeFlickLayoutsDeviceTest.kt b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FreeFlickLayoutsDeviceTest.kt new file mode 100644 index 000000000..82cad77f9 --- /dev/null +++ b/app/src/androidTest/java/com/kazumaproject/markdownhelperkeyboard/FreeFlickLayoutsDeviceTest.kt @@ -0,0 +1,142 @@ +package com.kazumaproject.markdownhelperkeyboard + +import android.app.Activity +import android.app.Instrumentation +import android.content.Intent +import android.content.IntentFilter +import android.os.SystemClock +import android.view.View +import android.view.ViewGroup +import android.view.inspector.WindowInspector +import android.widget.* +import androidx.core.content.FileProvider +import androidx.navigation.fragment.NavHostFragment +import androidx.preference.PreferenceManager +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry +import androidx.test.runner.lifecycle.Stage +import com.google.android.material.button.MaterialButton +import com.google.android.material.card.MaterialCardView +import com.google.android.material.textfield.TextInputEditText +import com.kazumaproject.custom_keyboard.data.SumirePopupDefaults +import com.kazumaproject.custom_keyboard.view.PopupMapGuideView +import com.kazumaproject.markdownhelperkeyboard.setting_activity.MainActivity +import com.kazumaproject.markdownhelperkeyboard.sumire_popup.* +import org.junit.Assert.* +import org.junit.Assume.assumeTrue +import org.junit.Test +import org.junit.runner.RunWith +import java.io.File + +@RunWith(AndroidJUnit4::class) +class FreeFlickLayoutsDeviceTest { + private val ins = InstrumentationRegistry.getInstrumentation() + private val context get() = ins.targetContext + private val repository get() = FreeFlickLayoutRepository(context) + private fun activity() = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED).filterIsInstance().firstOrNull() + private fun all(v: View): List = listOf(v) + if (v is ViewGroup) (0 until v.childCount).flatMap { all(v.getChildAt(it)) } else emptyList() + private fun views() = activity()?.window?.decorView?.let(::all).orEmpty() + private fun global() = WindowInspector.getGlobalWindowViews().flatMap(::all) + private fun nav() = (activity()!!.supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment).navController + private fun await(test: () -> Boolean) { + repeat(400) { var ready = false; ins.runOnMainSync { ready = test() }; if (ready) return; SystemClock.sleep(50) } + fail("Layout UI did not reach the expected state") + } + private fun start() { + assumeTrue(context.packageName.endsWith(".popuptest")) + PreferenceManager.getDefaultSharedPreferences(context).edit().clear().putString("sumire_keyboard_style_preference", SumirePopupDefaults.STYLE).commit() + ins.startActivitySync(Intent(context, MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) + await { runCatching { nav().currentDestination != null }.getOrDefault(false) } + ins.runOnMainSync { nav().navigate(R.id.freeFlickLayoutsFragment) } + await { views().filterIsInstance().isNotEmpty() } + } + private fun button(res: Int) = ins.runOnMainSync { global().filterIsInstance().last { it.text == context.getString(res) }.performClick() } + private fun name(value: String) { + await { global().filterIsInstance().isNotEmpty() } + ins.runOnMainSync { global().filterIsInstance().last().setText(value); global().filterIsInstance