154 lines
6.9 KiB
Ziran
154 lines
6.9 KiB
Ziran
// The Android input method editor: snapshots reach the focused field as text
|
|
// requests, stale revisions are refused, a replaced field ignores late edits
|
|
// of its predecessor, and a blurred field takes none. Runs with ANDROID_BUILD
|
|
// on the host; Java stays stubbed.
|
|
#import, file "../src/backend/android_device.zi";
|
|
#import, file "android_device_host.zi";
|
|
#import "android_text_editor"
|
|
#import "android_jni"
|
|
#import "byte_text_linux"
|
|
#import "session"
|
|
#import "tree"
|
|
#import "tree_input"
|
|
#import "text_host"
|
|
#import "text_request"
|
|
// This test stands in for an editing widget, which registers the segmentation.
|
|
#import "text_boundary"
|
|
#import "widget_kind"
|
|
using WidgetKind;
|
|
|
|
c :: #system_library "c";
|
|
TestExit :: (status: s32) #foreign c "exit";
|
|
|
|
Require :: (condition: bool, code: s32) {
|
|
if !condition {
|
|
TestExit(code)
|
|
}
|
|
}
|
|
|
|
EditorFrame :: (session: Session, key: u64, value: string) -> s32 {
|
|
TreeStart(session, 1, .{0.0, 0.0, 400.0, 600.0})
|
|
node := TreeSubmitCurrent(session, key, WidgetKindTextArea, .{10.0, 20.0, 300.0, 50.0})
|
|
TreeSetInteractive(session, node, false, false, 1)
|
|
TreeSetSelected(session, node, true)
|
|
TreeSetTextValue(session, node, value, cast(s32)value.count, cast(s32)value.count, false, false)
|
|
TextHostPresent(session, node, value, cast(s32)value.count, cast(s32)value.count, false, false, false, false)
|
|
if !TreeFinish(session) {
|
|
return -1
|
|
}
|
|
unused TreeFocusAt(session, node)
|
|
AndroidTextInputFrame(session, true)
|
|
return node
|
|
}
|
|
|
|
ime_calls: s32;
|
|
ime_visible: bool;
|
|
ImeMethod :: (env: *JniEnv, klass: *void, name: *u8, signature: *u8) -> *void {
|
|
if TextFromCString(name) == "setSoftKeyboardVisible" && TextFromCString(signature) == "(Z)V" { return klass }
|
|
return null
|
|
}
|
|
ImeCall :: (env: *JniEnv, object: *void, method: *void, arguments: *JniValue) {
|
|
ime_calls += 1; ime_visible = arguments[0].integer != 0
|
|
}
|
|
|
|
// Opening the editor and each change of the focused field start a new
|
|
// target, which Java names in its snapshots.
|
|
#program_export
|
|
main :: () -> s32 {
|
|
editor := SessionOpen()
|
|
AndroidTextInputOpen(editor)
|
|
node := EditorFrame(editor, 100, "")
|
|
Require(node >= 0 && AndroidTextEditorUpdate(2, 1, "hel", 3, 3), 1)
|
|
Require(AndroidTextEditorUpdate(2, 2, "hello 水😀", 13, 13) &&
|
|
!AndroidTextEditorUpdate(2, 1, "old", 3, 3) && !AndroidTextEditorUpdate(2, 2, "again", 5, 5), 2)
|
|
AndroidTextInputBeginFrame(editor)
|
|
{
|
|
request := TextRequestTake(editor, node)
|
|
Require(request.pending && request.replace && request.value == "hello 水😀" && request.cursor == 13, 3)
|
|
}
|
|
// An IME correction in the middle retains its nonterminal byte selection,
|
|
// including a subsequent selection-only update on the same target.
|
|
Require(AndroidTextEditorUpdate(2, 3, "hello 水😀 rest", 9, 6), 8)
|
|
AndroidTextInputBeginFrame(editor)
|
|
{
|
|
request := TextRequestTake(editor, node)
|
|
Require(request.pending && request.cursor == 9 && request.anchor == 6, 9)
|
|
}
|
|
Require(AndroidTextEditorUpdate(2, 4, "hello 水😀 rest", 6, 6), 10)
|
|
AndroidTextInputBeginFrame(editor)
|
|
{
|
|
request := TextRequestTake(editor, node)
|
|
Require(request.pending && request.cursor == 6 && request.anchor == 6, 11)
|
|
}
|
|
// Replacing a focused field changes its identity: late edits from the
|
|
// previous editor never reach the new field.
|
|
node = EditorFrame(editor, 101, "saved draft")
|
|
Require(!AndroidTextEditorUpdate(2, 3, "late", 4, 4) && AndroidTextEditorUpdate(3, 1, "new", 3, 3), 4)
|
|
AndroidTextInputBeginFrame(editor)
|
|
{
|
|
request := TextRequestTake(editor, node)
|
|
Require(request.pending && request.value == "new", 5)
|
|
}
|
|
AndroidTextInputFrame(editor, false)
|
|
Require(!AndroidTextEditorUpdate(3, 2, "after blur", 10, 10), 6)
|
|
// An invalid snapshot is refused.
|
|
node = EditorFrame(editor, 102, "")
|
|
Require(!AndroidTextEditorUpdate(5, 1, "ab", 5, 0) && AndroidTextEditorUpdate(5, 1, "ab", 2, 0), 7)
|
|
AndroidTextInputClose(editor)
|
|
AndroidTextInputOpen(editor)
|
|
AndroidTextEditorExternalPresent(editor, 901, "", 0, 0, 0)
|
|
AndroidTextInputFrame(editor, true)
|
|
Require(AndroidTextEditorUpdate(8, 1, "水😀", 7, 3), 12)
|
|
AndroidTextInputBeginFrame(editor)
|
|
copied: [64]u8
|
|
foreign := SessionOpen()
|
|
Require(!AndroidTextEditorExternalTake(foreign, 901, copied[:]).pending &&
|
|
!AndroidTextEditorExternalTake(editor, 900, copied[:]).pending, 13)
|
|
request := AndroidTextEditorExternalTake(editor, 901, copied[:])
|
|
Require(request.pending && request.value == "水😀" && request.cursor == 7 && request.anchor == 3, 14)
|
|
// A subsequent callback cannot mutate the snapshot copied for the VM.
|
|
Require(AndroidTextEditorUpdate(8, 2, "new", 3, 3) && request.value == "水😀", 15)
|
|
AndroidTextEditorExternalPresent(editor, 902, "other", 5, 5, 0)
|
|
AndroidTextInputFrame(editor, true)
|
|
other_copy: [64]u8
|
|
Require(!AndroidTextEditorUpdate(8, 3, "late", 4, 4) &&
|
|
!AndroidTextEditorExternalTake(editor, 901, other_copy[:]).pending, 16)
|
|
AndroidTextEditorExternalPresent(editor, 0, "", 0, 0, 0)
|
|
AndroidTextInputFrame(editor, false)
|
|
Require(!AndroidTextEditorUpdate(9, 1, "blurred", 7, 7), 17)
|
|
// The raster VM has no field in the host tree. Its committed hit intent
|
|
// must explicitly reopen a dismissed IME while keyboard focus stays true.
|
|
OrientationVmOpen()
|
|
orientation_api.GetMethodID = ImeMethod
|
|
orientation_api.CallVoidMethodA = ImeCall
|
|
AndroidTextInputBeginFrame(editor)
|
|
AndroidTextEditorExternalPresent(editor, 903, "focus", 5, 5, 0)
|
|
AndroidTextInputFrame(editor, true)
|
|
DeviceSoftKeyboard(false)
|
|
Require(ime_calls == 1 && !ime_visible, 18)
|
|
AndroidTextInputBeginFrame(editor)
|
|
AndroidTextEditorExternalPresent(editor, 903, "focus", 5, 5, 16)
|
|
AndroidTextInputFrame(editor, true)
|
|
Require(ime_calls == 2 && ime_visible && !AndroidTextEditorWantsKeyboard(foreign), 19)
|
|
// A later outside press carries no hit intent. Foreign or stale work
|
|
// cannot restore an earlier frame's intent, and blur revokes it too.
|
|
DeviceSoftKeyboard(false)
|
|
AndroidTextInputBeginFrame(editor)
|
|
AndroidTextEditorExternalPresent(editor, 903, "focus", 5, 5, 0)
|
|
AndroidTextEditorExternalPresent(foreign, 903, "foreign", 7, 7, 16)
|
|
AndroidTextInputFrame(editor, true)
|
|
Require(ime_calls == 3 && !ime_visible, 20)
|
|
AndroidTextInputBeginFrame(editor)
|
|
AndroidTextEditorExternalPresent(editor, 904, "replacement", 11, 11, 0)
|
|
AndroidTextInputFrame(editor, true)
|
|
Require(ime_calls == 3 && !AndroidTextEditorWantsKeyboard(editor), 21)
|
|
AndroidTextInputBeginFrame(editor)
|
|
AndroidTextEditorExternalPresent(editor, 0, "", 0, 0, 16)
|
|
AndroidTextInputFrame(editor, false)
|
|
Require(ime_calls == 3 && !AndroidTextEditorWantsKeyboard(editor), 22)
|
|
AndroidTextInputClose(editor)
|
|
SessionClose(foreign)
|
|
SessionClose(editor)
|
|
return 0
|
|
}
|