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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.kazumaproject.markdownhelperkeyboard

import android.content.Intent
import android.os.SystemClock
import android.view.View
import android.view.ViewGroup
import android.view.inspector.WindowInspector
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.navigation.fragment.NavHostFragment
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager
import androidx.preference.SwitchPreferenceCompat
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.kazumaproject.custom_keyboard.data.SumirePopupDefaults
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

@RunWith(AndroidJUnit4::class)
class FreeFlickSettingsDeviceTest {
private val ins=InstrumentationRegistry.getInstrumentation()
private val context get()=ins.targetContext
private fun activity()=ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED).filterIsInstance<MainActivity>().firstOrNull()
private fun nav()=activity()!!.supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment
private fun all(view: View): List<View> = listOf(view)+if(view is ViewGroup) (0 until view.childCount).flatMap {all(view.getChildAt(it))} else emptyList()
private fun views()=activity()?.window?.decorView?.let(::all).orEmpty()
private fun preferences()=nav().childFragmentManager.primaryNavigationFragment as? PreferenceFragmentCompat
private fun await(test: ()->Boolean) {
repeat(400) {var ready=false;ins.runOnMainSync {ready=test()};if(ready)return;SystemClock.sleep(50)}
fail("Free Flick UI did not reach expected state")
}
private fun start() {
assumeTrue(context.packageName.endsWith(".popuptest"))
ins.startActivitySync(Intent(context,MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
await {activity()!=null}
}
@Test fun nativeSettingsAndSearchShareOptionsAndPopupLabelsAreReadable() {
val prefs=PreferenceManager.getDefaultSharedPreferences(context)
prefs.edit().remove(FreeFlickPreferences.NEAREST).remove(FreeFlickPreferences.TEXT)
.remove(FreeFlickPreferences.SWITCH).remove(FreeFlickPreferences.BACK).remove(FreeFlickPreferences.PRESENTATION).commit()
start()
ins.runOnMainSync {nav().navController.navigate(R.id.sumirePreferenceFragment)}
await {preferences()?.findPreference<Preference>("sumire_free_flick_settings")!=null}
ins.runOnMainSync {preferences()!!.findPreference<Preference>("sumire_free_flick_settings")!!.performClick()}
await {preferences()?.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.NEAREST)!=null}
ins.runOnMainSync {
val screen=preferences()!!
assertTrue(screen.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.NEAREST)!!.isChecked)
assertTrue(screen.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.TEXT)!!.isChecked)
assertFalse(screen.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.SWITCH)!!.isChecked)
assertFalse(screen.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.BACK)!!.isChecked)
screen.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.SWITCH)!!.isChecked=true
screen.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.NEAREST)!!.isChecked=false
assertFalse(screen.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.TEXT)!!.isEnabled)
assertFalse(screen.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.SWITCH)!!.isEnabled)
assertTrue(screen.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.SWITCH)!!.isChecked)
screen.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.NEAREST)!!.isChecked=true
screen.findPreference<ListPreference>(FreeFlickPreferences.PRESENTATION)!!.value="guide_above_key"
activity()!!.recreate()
}
await {preferences()?.findPreference<ListPreference>(FreeFlickPreferences.PRESENTATION)?.value=="guide_above_key"}
assertTrue(FreeFlickPreferences.read(context).nearestSwitch)
ins.runOnMainSync {nav().navController.navigate(R.id.settingSearchFragment)}
await {activity()?.findViewById<EditText>(R.id.setting_search_input)!=null}
ins.runOnMainSync {activity()!!.findViewById<EditText>(R.id.setting_search_input).setText(context.getString(R.string.free_flick_nearest_back))}
await {views().filterIsInstance<TextView>().any {it.id==R.id.setting_search_result_title && it.text==context.getString(R.string.free_flick_nearest_back)}}
ins.runOnMainSync {views().filterIsInstance<TextView>().firstOrNull {it.id==R.id.setting_search_result_title && it.text==context.getString(R.string.free_flick_nearest_back)}?.let { title ->
generateSequence(title.parent as? View) {it.parent as? View}.first {it.isClickable}.performClick()
}}
await {preferences()?.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.BACK)!=null}
ins.runOnMainSync {nav().navController.navigate(R.id.flickKeyboardPopupStyleListFragment)}
await {views().filterIsInstance<TextView>().any {it.text==context.getString(R.string.popup_style)}}
ins.runOnMainSync {
assertFalse(views().filterIsInstance<TextView>().any {"3段" in it.text || "3段" in it.text})
val row=views().filterIsInstance<TextView>().first {it.text==context.getString(R.string.popup_style)}
generateSequence(row.parent as? View) {it.parent as? View}.first {it.isClickable}.performClick()
}
await {preferences()?.findPreference<SwitchPreferenceCompat>(FreeFlickPreferences.NEAREST)!=null}
ins.runOnMainSync {activity()!!.finish()}
}
@Test fun templateConfirmationUndoAndSavePreserveOtherKeys() {
val repository=SumirePopupRepository(context)
val original=LegacyPopupFixture.create("は")
repository.save(original)
val other=repository.load("か")
start();ins.runOnMainSync {nav().navController.navigate(R.id.popupMapEditorFragment)}
await {views().filterIsInstance<PopupMapEditorCanvas>().isNotEmpty()}
fun apply() {
ins.runOnMainSync {views().filterIsInstance<MaterialButton>().single {it.text==context.getString(R.string.free_flick_template)}.performClick()}
await {WindowInspector.getGlobalWindowViews().flatMap(::all).filterIsInstance<Button>().any {it.id==android.R.id.button1}}
ins.runOnMainSync {WindowInspector.getGlobalWindowViews().flatMap(::all).filterIsInstance<Button>().last {it.id==android.R.id.button1}.performClick()}
await {views().filterIsInstance<PopupMapEditorCanvas>().first().config!!.maps.size==1}
}
apply()
assertEquals(original,repository.load("は"))
ins.runOnMainSync {views().filterIsInstance<MaterialButton>().single {it.text==context.getString(R.string.free_flick_settings)}.performClick()}
await {preferences()?.findPreference<Preference>(FreeFlickPreferences.NEAREST)!=null}
ins.runOnMainSync {nav().navController.popBackStack()}
await {views().filterIsInstance<PopupMapEditorCanvas>().firstOrNull()?.config?.maps?.size==1}
assertEquals(original,repository.load("は"))
ins.runOnMainSync {views().filterIsInstance<MaterialButton>().single {it.text==context.getString(R.string.popup_undo)}.performClick()}
await {views().filterIsInstance<PopupMapEditorCanvas>().first().config==original}
apply()
ins.runOnMainSync {views().filterIsInstance<MaterialButton>().single {it.text==context.getString(R.string.popup_save)}.performClick()}
assertEquals(SumirePopupDefaults.create("は"),repository.load("は"))
assertEquals(other,repository.load("か"))
ins.runOnMainSync {activity()!!.finish()}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.kazumaproject.markdownhelperkeyboard

import com.kazumaproject.custom_keyboard.data.*

/** Existing v1 configuration, deliberately independent of the new default template. */
internal object LegacyPopupFixture {
val rows = linkedMapOf("あ" to "あいうえお", "か" to "かきくけこ", "さ" to "さしすせそ", "た" to "たちつてと", "な" to "なにぬねの", "は" to "はひふへほ", "ま" to "まみむめも", "や" to "やゆよ", "ら" to "らりるれろ", "わ" to "わをんー")
private val voiced = mapOf("か" to "がぎぐげご", "さ" to "ざじずぜぞ", "た" to "だぢづでど", "は" to "ばびぶべぼ")
private val slots = listOf(PopupBounds(-1.4f,-1.1f,-.6f,-.3f), PopupBounds(-.4f,-1.4f,.4f,-.6f), PopupBounds(.6f,-1.1f,1.4f,-.3f), PopupBounds(-.4f,-2.4f,.4f,-1.6f))
fun create(keyId: String): PopupKeyConfig {
val row = rows.getValue(keyId)
fun page(id: String, chars: String): PopupMap {
val regions = chars.drop(1).mapIndexed { index, char -> PopupRegion("$id-$index", slots[index], PopupAction(text = char.toString())) }.toMutableList()
if (id != "normal") regions += PopupRegion("$id-back", PopupBounds(-.4f,-3.4f,.4f,-2.6f), PopupAction(PopupActionType.BACK), "戻る")
return PopupMap(id, chars.first().toString() + "行", chars.first().toString(), regions)
}
val normal = page("normal", row).copy(name = "通常")
val maps = mutableListOf(normal)
voiced[keyId]?.let { maps += page("voiced", it) }
if (keyId == "は") maps += page("semi", "ぱぴぷぺぽ")
val controls = maps.drop(1).mapIndexed { index, target ->
val x = if (index == 0) -1.4f else .6f
PopupRegion("open-${target.id}", PopupBounds(x,-2.4f,x+.8f,-1.6f), PopupAction(PopupActionType.SWITCH, targetMapId = target.id), target.name)
}
return PopupKeyConfig(keyId, "normal", listOf(normal.copy(regions = normal.regions + controls)) + maps.drop(1))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SumirePopupEditorDeviceTest {
@Test fun newAndLegacySettingsOpenTheSameEditorAndEditsPersist() {
assumeTrue(context.packageName.endsWith(".popuptest"))
val repository=SumirePopupRepository(context)
repository.save(SumirePopupDefaults.create("は"))
repository.save(LegacyPopupFixture.create("は"))
for(modern in listOf(true,false)) {
PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean("setting_use_new_home_screen_preference",modern).commit()
ins.startActivitySync(Intent(context,MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
Expand All @@ -62,7 +62,7 @@ class SumirePopupEditorDeviceTest {
assertTrue("Search result must open the editor",target?.performClick()==true)
}
} else {
await {nav()?.childFragmentManager?.primaryNavigationFragment is SumirePreferenceFragment}
await {(nav()?.childFragmentManager?.primaryNavigationFragment as? SumirePreferenceFragment)?.findPreference<Preference>("sumire_popup_map_editor_preference")!=null}
ins.runOnMainSync {(nav()!!.childFragmentManager.primaryNavigationFragment as SumirePreferenceFragment).findPreference<Preference>("sumire_popup_map_editor_preference")!!.performClick()}
}
await {canvas()!=null && canvas()!!.config!=null}
Expand All @@ -77,7 +77,7 @@ class SumirePopupEditorDeviceTest {
}
@Test fun popupTextDragConstraintsAndDraftSurviveRecreation() {
assumeTrue(context.packageName.endsWith(".popuptest"))
SumirePopupRepository(context).save(SumirePopupDefaults.create("は"))
SumirePopupRepository(context).save(LegacyPopupFixture.create("は"))
ins.startActivitySync(Intent(context,MainActivity::class.java).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
await {nav()?.navController?.currentDestination!=null}
ins.runOnMainSync {nav()!!.navController.navigate(R.id.popupMapEditorFragment)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import androidx.test.runner.lifecycle.Stage
import com.kazumaproject.custom_keyboard.data.*
import com.kazumaproject.custom_keyboard.controller.PopupMapInputController
import com.kazumaproject.custom_keyboard.view.PopupMapView
import com.kazumaproject.custom_keyboard.view.PopupMapGuideView
import com.kazumaproject.custom_keyboard.view.compactFlickGuideSize
import com.kazumaproject.markdownhelperkeyboard.sumire_popup.SumirePopupRepository
import com.kazumaproject.markdownhelperkeyboard.sumire_popup.FreeFlickPreferences
import org.junit.Assert.*
import org.junit.Assume.assumeTrue
import org.junit.Test
Expand All @@ -49,7 +52,7 @@ class SumirePopupInputDeviceTest {
key=TextView(themed).apply {text="は";rotation=30f}
frame.addView(key,FrameLayout.LayoutParams(200,100).apply {leftMargin=200;topMargin=500})
host.setContentView(frame)
controller=PopupMapInputController(SumirePopupDefaults.create("は"),{null},{it},{},{_,_->},{text,_->committed=text},{})
controller=PopupMapInputController(LegacyPopupFixture.create("は"),{null},{it},{},{_,_->},{text,_->committed=text},{})
controller.attach(key)
}
await {key.width==200 && key.isAttachedToWindow}
Expand Down Expand Up @@ -96,6 +99,7 @@ class SumirePopupInputDeviceTest {
val target="${context.packageName}/com.kazumaproject.markdownhelperkeyboard.ime_service.IMEService"
val oldIme=shell("settings get secure default_input_method").trim()
val enabled=shell("ime list -s").lineSequence().any {it.trim()==target}
val guide=InstrumentationRegistry.getArguments().getString("presentation")=="guide"
val floating=InstrumentationRegistry.getArguments().getString("surface","normal")=="floating"
val preferences=PreferenceManager.getDefaultSharedPreferences(context)
preferences.edit().clear()
Expand All @@ -105,13 +109,15 @@ class SumirePopupInputDeviceTest {
.putString("sumire_input_method_preference",InstrumentationRegistry.getArguments().getString("inputStyle","flick"))
.putString("sumire_keyboard_style_preference",SumirePopupDefaults.STYLE)
.putBoolean("keyboard_floating_preference",floating)
.putBoolean(FreeFlickPreferences.NEAREST,false)
.putString(FreeFlickPreferences.PRESENTATION,if(guide) "guide_above_key" else "around_key")
.putBoolean("sumire_restore_input_mode_on_restart_preference",false)
.putBoolean("landscape_force_qwerty_preference",false)
.putBoolean("live_conversion_preference",false)
.putBoolean("learn_dictionary_preference",false)
.putBoolean("flick_editor_preview_preference",false)
.putBoolean("key_sound_preference",false).commit()
val original=SumirePopupDefaults.create("は")
val original=LegacyPopupFixture.create("は")
val entry=original.map("normal")!!.regions.first {it.action.type==PopupActionType.SWITCH}
SumirePopupRepository(context).save(original.withMap(original.map("voiced")!!.let { page ->
page.copy(regions=page.regions.map { if (it.action.text=="び") it.copy(action=PopupAction(text="ありがとう")) else it }+PopupRegion("immediate",entry.bounds,PopupAction(text="★")))
Expand All @@ -137,6 +143,20 @@ class SumirePopupInputDeviceTest {
reset();key=findKey("は")
val down=SystemClock.uptimeMillis()
inject(down,MotionEvent.ACTION_DOWN,key.left+key.width()*fraction,key.centerY().toFloat())
if(guide) {
await {popup() is PopupMapGuideView}
ins.runOnMainSync {
val drawing=popup() as PopupMapGuideView
val xy=IntArray(2).also(drawing::getLocationOnScreen)
assertTrue("Guide must end above the physical key",xy[1]+drawing.height<=key.top+3)
val expected=compactFlickGuideSize(key.width(),key.height(),context.resources.displayMetrics.density)
assertEquals("Same width as the two-step guide",expected.width,drawing.width)
assertEquals("Same height as the two-step guide",expected.height,drawing.height)
assertEquals(key.width().toFloat(),drawing.sourceKeyWidth,1f)
assertNotNull(drawing.fingerPosition)
origins+=(xy[0]+drawing.originX).toInt() to (xy[1]+drawing.originY).toInt()
}
} else {
await {popup()!=null}
ins.runOnMainSync {
val view=popup()!!
Expand All @@ -146,6 +166,7 @@ class SumirePopupInputDeviceTest {
assertEquals("Popup x must use the original key",key.centerX().toFloat(),origin.first.toFloat(),3f)
assertEquals("Popup y must use the original key",key.centerY().toFloat(),origin.second.toFloat(),3f)
}
}
inject(down,MotionEvent.ACTION_UP,key.left+key.width()*fraction,key.centerY().toFloat())
awaitText("は")
}
Expand All @@ -162,7 +183,29 @@ class SumirePopupInputDeviceTest {
gesture(key,listOf(-1f to -2f,0f to -3f,-1f to -.7f));awaitText("ひ")
reset();key=findKey("は")
gesture(key,listOf(1.6f to .6f));awaitText("")
// Apply nearest options on a retained IME, without restarting it.
preferences.edit().putBoolean(FreeFlickPreferences.NEAREST,true).commit();SystemClock.sleep(150)
reset();key=findKey("は");gesture(key,listOf(3f to 0f));awaitText("へ")
preferences.edit().putBoolean(FreeFlickPreferences.TEXT,false).commit();SystemClock.sleep(150)
reset();key=findKey("は");gesture(key,listOf(3f to 0f));awaitText("")
preferences.edit().putBoolean(FreeFlickPreferences.SWITCH,true).commit();SystemClock.sleep(150)
reset();key=findKey("は");gesture(key,listOf(-2f to -3f,0f to 0f));awaitText("ば")
preferences.edit().putBoolean(FreeFlickPreferences.BACK,true).commit();SystemClock.sleep(150)
reset();key=findKey("は")
gesture(key,listOf(-2f to -3f,-2f to -3f,-2f to -3f,0f to 0f));awaitText("ば")
reset();key=findKey("は")
gesture(key,listOf(-2f to -3f,0f to 0f,-2f to -3f,0f to 0f));awaitText("は")
// A live preference change cancels an active gesture instead of committing stale text.
reset();key=findKey("は")
val activeDown=SystemClock.uptimeMillis()
inject(activeDown,MotionEvent.ACTION_DOWN,key.centerX().toFloat(),key.centerY().toFloat())
preferences.edit().putString(FreeFlickPreferences.PRESENTATION,if(guide) "around_key" else "guide_above_key").commit()
SystemClock.sleep(150)
inject(activeDown,MotionEvent.ACTION_UP,key.centerX().toFloat(),key.centerY().toFloat());awaitText("")
} finally {
val now=SystemClock.uptimeMillis()
val cancel=MotionEvent.obtain(now,now,MotionEvent.ACTION_CANCEL,0f,0f,0).apply {source=InputDevice.SOURCE_TOUCHSCREEN}
automation.injectInputEvent(cancel,true);cancel.recycle()
ins.runOnMainSync {host()?.let {it.requestedOrientation=android.content.pm.ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;it.finish()}}
if(oldIme.isNotBlank() && oldIme!="null") shell("ime set $oldIme")
if(!enabled) shell("ime disable $target")
Expand Down
Loading
Loading