Files
kryon/tests/android_frame_input_test.zi
T

177 lines
8.2 KiB
Ziran

// The software frame host driven through Android OS events and padded pixels.
Probe :: #import "android_frame_app";
#import "android_run"
#import "android_native"
#import "android_bundle_images"
#import "android_touch"
#import "android_jni"
#import "android_surface"
#import "frame_snapshot"
#import "geometry"
#import "input_protocol"
#import "session"
#import "tree_draw"
#import "std/c_string"
Event :: struct { kind: s32; action: s32; key: s32; x: float32; y: float32 }
watched: Session;
presents: s32;
presented_open: bool;
window_pixels: [64800]u32;
#program_export
ALooper_pollAll :: (timeout: s32, file: *s32, events: *s32, data: **void) -> s32 {
unused timeout; unused file; unused events; unused data; return -1
}
#program_export
ANativeWindow_getWidth :: (window: *void) -> s32 { unused window; return 320 }
#program_export
ANativeWindow_getHeight :: (window: *void) -> s32 { unused window; return 200 }
#program_export
ANativeWindow_setBuffersGeometry :: (window: *void, width: s32, height: s32, format: s32) -> s32 {
unused window; unused width; unused height; unused format; return 0
}
#program_export
ANativeWindow_lock :: (window: *void, output: *NativeWindowBuffer, bounds: *void) -> s32 {
unused window; unused bounds
output.* = NativeWindowBuffer.{.width = 320, .height = 200, .stride = 324,
.format = 1, .bits = cast(*u8)(*window_pixels[0])}
return 0
}
#program_export
ANativeWindow_unlockAndPost :: (window: *void) -> s32 {
unused window; presents += 1
if CaptureFrameInput(watched).available { presented_open = true }
return 0
}
#program_export
ANativeActivity_finish :: (activity: *void) { unused activity }
#program_export
AInputEvent_getType :: (event: *Event) -> s32 { return event.kind }
#program_export
AInputEvent_getSource :: (event: *Event) -> s32 { unused event; return 0x1002 }
#program_export
AKeyEvent_getAction :: (event: *Event) -> s32 { return event.action }
#program_export
AKeyEvent_getMetaState :: (event: *Event) -> s32 { unused event; return 0 }
#program_export
AInputEvent_getDeviceId :: (event: *Event) -> s32 { unused event; return -1 }
#program_export
AKeyEvent_getKeyCode :: (event: *Event) -> s32 { return event.key }
#program_export
AKeyEvent_getRepeatCount :: (event: *Event) -> s32 { unused event; return 0 }
#program_export
AMotionEvent_getAction :: (event: *Event) -> s32 { return event.action }
#program_export
AMotionEvent_getButtonState :: (event: *Event) -> s32 { unused event; return 0 }
#program_export
AMotionEvent_getX :: (event: *Event, pointer: usize) -> float32 { unused pointer; return event.x }
#program_export
AMotionEvent_getY :: (event: *Event, pointer: usize) -> float32 { unused pointer; return event.y }
#program_export
__android_log_write :: (priority: s32, tag: *u8, text: *u8) -> s32 {
unused priority; unused tag; unused text; return 1
}
Send :: (kind: s32, action: s32, key: s32 = 0, x: float32 = 0.0, y: float32 = 0.0) -> bool {
event := Event.{kind, action, key, x, y}
return AndroidTouchInput(null, cast(*void)(*event)) == 1
}
Draw :: (session: Session) -> bool {
viewport := AndroidViewport.{.width = 320, .height = 200, .ready = 1}
return DrawAndroidFrame(session, viewport, Rectangle.{0.0, 0.0, 320.0, 200.0}, 1.0, false)
}
#program_export
main :: () -> s32 {
app: NativeAndroidApp
app.window = cast(*void)(*app)
AndroidNativeStart(*app)
defer { AndroidNativeClose() }
AndroidNativeOnCommand(*app, AndroidNativeCommandInitWindow)
session := SessionOpen()
defer { unused CloseSession(session) }
Probe.foreign = SessionOpen()
defer { unused CloseSession(Probe.foreign) }
watched = session
// First frame works before the first event.
if !Draw(session) || Probe.frames != 1 || !Probe.seen.focused { return Failure(1) }
if !Send(1, 0, 29) || !Draw(session) { return Failure(2) }
first := Probe.seen
if !first.available || first.keys[65] != cast(u8)3 || first.delta_ms < 0 { return Failure(3) }
if CaptureFrameInput(session).available || presents == 0 || presented_open { return Failure(4) }
if !Draw(session) || Probe.seen.keys[65] != cast(u8)FrameKeyHeld ||
Probe.seen.sequence <= first.sequence { return Failure(5) }
Probe.fail = true
if !Send(1, 1, 29) || Draw(session) { return Failure(6) }
if Probe.seen.keys[65] != cast(u8)FrameKeyReleased || CaptureFrameInput(session).available { return Failure(7) }
Probe.fail = false
if !Send(1, 0, 29) || !Draw(session) { return Failure(8) }
AndroidNativeOnCommand(*app, AndroidNativeCommandLostFocus)
if !Draw(session) || Probe.seen.focused || Probe.seen.keys[65] != cast(u8)FrameKeyReleased { return Failure(9) }
AndroidNativeOnCommand(*app, AndroidNativeCommandGainedFocus)
// A whole tap between frames produces one press, then one release.
if !Send(2, 0, 0, 25.0, 30.0) || !Send(2, 1, 0, 35.0, 40.0) || !Draw(session) { return Failure(10) }
if !Probe.seen.pointer.pressed || !Probe.seen.pointer.down || Probe.seen.pointer.x != 25.0 ||
!Probe.pointer_matches { return Failure(11) }
if !Draw(session) || !Probe.seen.pointer.released || Probe.seen.pointer.down ||
Probe.seen.pointer.x != 35.0 || !Probe.pointer_matches { return Failure(12) }
if Probe.foreign_seen || CaptureFrameInput(session).available || presented_open { return Failure(13) }
// Surface insets are added to both replayed tap positions, before the
// host's camera mapping. The Android host's own zoom scales the offset.
AndroidTouchSetZoom(2.0)
AndroidTouchSetSurfaceOffset(18.0, 18.0)
if !Send(2, 0, 0, 10.0, 14.0) || !Send(2, 1, 0, 20.0, 24.0) {
return Failure(23)
}
offset_press := AndroidPointerFrame()
if !offset_press.pressed || offset_press.x != 14.0 || offset_press.y != 16.0 {
return Failure(24)
}
AndroidTouchFrameEnd()
offset_release := AndroidPointerFrame()
if !offset_release.released || offset_release.x != 19.0 || offset_release.y != 21.0 {
return Failure(25)
}
AndroidTouchFrameEnd()
AndroidTouchSetZoom(1.0)
AndroidTouchSetSurfaceOffset(0.0, 0.0)
// Pixel stride includes NDK row padding; ARGB becomes little-endian RGBA.
for index: 0..64799 { window_pixels[index] = 0x12345678 }
pixels := AndroidNativePixels()
for index: 0..63999 { pixels[index] = 0xffe61e28 }
if !AndroidNativeLock(1, 1, 3, 3) || !AndroidNativePresentDamage(1, 1, 3, 3) || !AndroidNativeUnlock() { return Failure(14) }
if window_pixels[325] != 0xff281ee6 || window_pixels[650] != 0xff281ee6 ||
window_pixels[324] != 0x12345678 || window_pixels[323] != 0x12345678 { return Failure(15) }
AndroidNativeOnCommand(*app, AndroidNativeCommandPause)
if !AndroidNativePaused() { return Failure(16) }
AndroidNativeOnCommand(*app, AndroidNativeCommandResume)
if AndroidNativePaused() { return Failure(17) }
AndroidNativeOnCommand(*app, AndroidNativeCommandTermWindow)
if AndroidNativeWindowReady() { return Failure(18) }
AndroidNativeOnCommand(*app, AndroidNativeCommandDestroy)
if !AndroidNativeDestroyRequested() { return Failure(19) }
// EXIF rotations must use the source dimensions on non-square images.
expected: [18]u32 = .[4, 1, 5, 2, 6, 3, 6, 3, 5, 2, 4, 1, 3, 6, 2, 5, 1, 4]
for orientation: 6..8 {
source := cast(*u32)TestAllocate(6, 4)
if source == null { return Failure(20) }
for i: 0..5 { source[i] = cast(u32)(i + 1) }
rotated := AndroidImageOrient(source, 3, 2, cast(s32)orientation)
if rotated == null { return Failure(21) }
for i: 0..5 { if rotated[i] != expected[(orientation - 6) * 6 + i] { return Failure(22) } }
TestRelease(cast(*void)rotated)
}
return 0
}
libc :: #system_library "c";
TestAllocate :: (count: usize, size: usize) -> *void #foreign libc "calloc";
TestRelease :: (memory: *void) #foreign libc "free";
PrintFailure :: (format: *u8, code: s32) -> s32 #foreign libc "printf";
Failure :: (code: s32) -> s32 {
format: [40]u8
unused CopyCString("android frame input failure %d\n", format[:])
unused PrintFailure(*format[0], code)
return code
}
// Bionic's nonblocking join probe has no glibc symbol. Ask the portable
// thread adapter to take its ordinary pthread_join path in this OS fixture.
#program_export
pthread_gettid_np :: (thread: *void) -> s32 { unused thread; return -1 }