Files
breeze/tests/android_jni_behavior.zi
T
waoziandClaude Opus 5.5 054a29f02c Remove raylib: run every host on Kryon and Ziran
Breeze builds and runs with no raylib anywhere, on Ziran 7fbc8152 and
Kryon b5a61b37, with plot, entity, apps, css and game2d pinned to that
Kryon, so the lock has one Kryon and no raylib package.

- Linux and Windows run on Kryon's SDL2 and Cairo desktop host; WebP
  decodes with the linked libwebp (KRYON_WEBP_LINKED).
- Android owns its NativeActivity loop on Kryon's android_runtime:
  Pixmap on the activity's surface, android_touch input and Android's
  typefaces, paced to the app's frame target. The CMake build drops
  breeze_raylib, GLES and the raylib link wraps, and links Kryon's fopen
  wrapper.
- The browser reaches Kryon's canvas modules directly.
- Audio is Ziran's std audio: its mixer, the SDL, AAudio and OpenSL ES
  sinks, the WAV, MP3 and Vorbis decoders and the audio package's Opus.
  The MP3 and Vorbis archives come from Ziran's own codec recipes. The
  browser keeps Kryon's Web Audio.
- Host modules choose the desktop host, Android, the canvas or, for
  native tests that select none, a headless host with no window, input
  or audio device. host_keys.zi and text_input_host.zi replace
  raylib_host.zi and raylib_text_input.zi; the log is one implementation,
  with logcat on Android and the C runtime's open flags on Windows.
- The Makefile, Click packaging, CI and release workflows build no raylib.
- Tests: radio, app log, asset files, DPI, legacy sessions, sync recovery
  and the Android JNI bridge run without raylib stubs; voice playback is
  a Ziran test of real Ogg Opus narration through Ziran's mixer. Tests
  that only exercised raylib internals are removed.
- AppsControllerSha256 is the controller built from Apps b96d9afe on
  this graph.

Local tests: Linux desktop window, Habits UI and the Ziran and native
tests above pass on a private Xvfb; the Android debug APK runs on a
Pixel 4a; the Windows package runs under Wine on a private Xvfb.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-18 13:16:23 -03:00

572 lines
20 KiB
Ziran

#import "android_jni"
// Kryon's Android device module brings its text menu widgets, which need
// these host functions; this test draws nothing.
#import, file "android_text_input_host.zi";
#import, file "../src/platform/android/android_health_host.zi";
#import, file "../src/platform/android/android_wakelock_host.zi";
#import "file_export"
#import "byte_text_linux"
#import "android_picker"
#import, file "../src/platform/android/android_push_host.zi";
#import, file "../src/platform/android/android_push_types.zi";
#import "android_device"
#import, file "../src/platform/android/android_activity.zi";
#import "file_download"
#import, file "../src/platform/android/android_insets.zi";
#import, file "../src/platform/network_transport.zi";
#import, file "../src/platform/android/android_startup.zi";
#import, file "../src/app/host_services.zi";
#import "net_http"
#import "async_client"
#import "open_uri"
#import "c_string"
#import "session"
c :: #system_library "c";
TestExit :: (status: s32) #foreign c "exit";
api: JniFunctions;
vm_api: JniVmFunctions;
environment: JniEnv;
vm: JniVm;
activity: NativeActivity;
app: NativeAndroidApp;
thread_detached: bool;
attach_count: s32;
detach_count: s32;
frame_count: s32;
frame_failed: bool;
exception_pending: u8;
exception_descriptions: s32;
method_name: [96]u8;
java_strings: [40][256]u8;
string_count: s32;
void_calls: s32;
last_integer: s32;
java_handle: s64;
java_asset_maximum: s64;
java_asset_cancelled: s64;
registered_methods: s32;
// Entity's shared account handoff registers on its own Kryon class.
handoff_class: u8;
handoff_methods: s32;
native_dark: NativeIntegerFn;
native_practice: NativePracticeFn;
resolve_available: bool = true;
launch_available: bool = true;
network_test: bool;
network_state: s32 = -1;
network_length: s32 = 3;
network_cancelled: s32;
network_header_failed: bool;
network_timeout: s32;
locale_preferences: [64]u8;
Require :: (condition: bool, code: s32) {
if !condition {
TestExit(code)
}
}
CStringEquals :: (text: *u8, expected: string) -> bool {
if text == null {
return false
}
index: s32 = 0
while cast(s64)index < expected.count {
if text[index] != expected[index] {
return false
}
index += 1
}
return text[index] == 0
}
FakeGetEnv :: (machine: *JniVm, output: **JniEnv, version: s32) -> s32 {
Require(machine == *vm && version == JniVersion, 11)
if thread_detached {
return -2
}
output[0] = *environment
return 0
}
FakeAttach :: (machine: *JniVm, output: **JniEnv,
arguments: *void) -> s32 {
unused machine
unused arguments
attach_count += 1
thread_detached = false
output[0] = *environment
return 0
}
FakeDetach :: (machine: *JniVm) -> s32 {
unused machine
detach_count += 1
thread_detached = true
return 0
}
FakePushFrame :: (env: *JniEnv, capacity: s32) -> s32 {
unused env
Require(capacity >= 16, 12)
if frame_failed {
exception_pending = 1
return -1
}
frame_count += 1
string_count = 0
return 0
}
FakePopFrame :: (env: *JniEnv, result: *void) -> *void {
unused env
frame_count -= 1
return result
}
FakeException :: (env: *JniEnv) -> u8 {
unused env
return exception_pending
}
FakeDescribe :: (env: *JniEnv) {
unused env
exception_descriptions += 1
}
FakeClear :: (env: *JniEnv) {
unused env
exception_pending = 0
}
FakeObjectClass :: (env: *JniEnv, object: *void) -> *void {
unused env
Require(object != null, 13)
return object
}
FakeFindClass :: (env: *JniEnv, name: *u8) -> *void {
unused env
Require(CStringEquals(name, "xyz/waozi/inbe/MainActivity") ||
CStringEquals(name, "android/net/Uri") ||
CStringEquals(name, "android/content/Intent") ||
CStringEquals(name, "org/kryonlabs/platform/AccountHandoff"), 14)
if CStringEquals(name, "org/kryonlabs/platform/AccountHandoff") { return cast(*void)*handoff_class }
return cast(*void)*activity
}
FakeMethod :: (env: *JniEnv, klass: *void, name: *u8,
signature: *u8) -> *void {
unused env
unused klass
Require(signature != null && signature[0] == #char "(", 15)
if CStringEquals(name, "startRuntimeAssetDownload") {
Require(CStringEquals(signature, "(Ljava/lang/String;Ljava/lang/String;JJ)V"), 97)
}
if CStringEquals(name, "cancelRuntimeAssetDownload") {
Require(CStringEquals(signature, "(J)V"), 98)
}
index: s32 = 0
while name[index] != 0 && index < 95 {
method_name[index] = name[index]
index += 1
}
method_name[index] = 0
return cast(*void)*method_name[0]
}
FakeString :: (env: *JniEnv, text: *u8) -> *void {
unused env
Require(string_count < 40, 16)
index: s32 = 0
while index < 255 && text[index] != 0 {
java_strings[string_count][index] = text[index]
index += 1
}
java_strings[string_count][index] = 0
result := cast(*void)*java_strings[string_count][0]
string_count += 1
return result
}
FakeVoid :: (env: *JniEnv, object: *void, method: *void,
arguments: *JniValue) {
unused env
unused object
unused method
void_calls += 1
if CStringEquals(*method_name[0], "startActivity") && !launch_available {
exception_pending = 1
}
if CStringEquals(*method_name[0], "httpCancel") {
Require(arguments[0].integer == 71, 90)
network_cancelled += 1
}
if CStringEquals(*method_name[0], "healthConnectExport") {
Require(CStringEquals(cast(*u8)arguments[0].reference, "/tmp/export.csv"), 17)
}
if CStringEquals(*method_name[0], "openImportPicker") {
last_integer = arguments[1].integer
}
if CStringEquals(*method_name[0], "applyOrientationMode") ||
CStringEquals(*method_name[0], "setSoftKeyboardVisible") {
last_integer = arguments[0].integer
}
if CStringEquals(*method_name[0], "startRuntimeAssetDownload") {
java_handle = arguments[2].long_integer
java_asset_maximum = arguments[3].long_integer
}
if CStringEquals(*method_name[0], "cancelRuntimeAssetDownload") {
java_asset_cancelled = arguments[0].long_integer
}
if CStringEquals(*method_name[0], "shareFile") {
Require(CStringEquals(cast(*u8)arguments[1].reference, "export.csv"), 18)
Require(CStringEquals(cast(*u8)arguments[2].reference, "application/octet-stream"), 19)
}
}
FakeObject :: (env: *JniEnv, object: *void, method: *void,
arguments: *JniValue) -> *void {
unused env
unused method
unused arguments
if CStringEquals(*method_name[0], "localePreferences") {
return cast(*void)*locale_preferences[0]
}
if CStringEquals(*method_name[0], "resolveActivity") && !resolve_available {
return null
}
return object
}
FakeStaticField :: (env: *JniEnv, klass: *void, field: *void) -> *void {
unused env
unused field
return klass
}
FakeStaticInteger :: (env: *JniEnv, klass: *void, field: *void) -> s32 {
unused env
unused klass
unused field
return 0x10000000
}
FakeField :: (env: *JniEnv, klass: *void, name: *u8,
signature: *u8) -> *void {
unused env
unused name
unused signature
return klass
}
FakeBoolean :: (env: *JniEnv, object: *void, method: *void,
arguments: *JniValue) -> u8 {
unused env
unused object
unused method
unused arguments
if CStringEquals(*method_name[0], "httpHeader") && network_header_failed {
return 0
}
return 1
}
FakeInteger :: (env: *JniEnv, object: *void, method: *void,
arguments: *JniValue) -> s32 {
unused env
unused object
unused method
if CStringEquals(*method_name[0], "httpCreate") {
Require(arguments[2].integer == 64, 91)
network_timeout = arguments[3].integer
return 71
}
if CStringEquals(*method_name[0], "httpPoll") {
return network_state
}
return 2
}
FakeDelete :: (env: *JniEnv, object: *void) {
unused env
unused object
}
FakeGlobal :: (env: *JniEnv, object: *void) -> *void {
unused env
return object
}
FakeVm :: (env: *JniEnv, output: **JniVm) -> s32 {
unused env
output[0] = *vm
return 0
}
FakeBytes :: (env: *JniEnv, count: s32) -> *void {
unused env
if !network_test {
Require(count == 3, 20)
}
return cast(*void)*activity
}
FakeSetBytes :: (env: *JniEnv, array: *void, start: s32,
count: s32, bytes: *u8) {
unused env
unused array
if !network_test {
Require(start == 0 && count == 3 && bytes[0] == 65, 21)
}
}
FakeGetBytes :: (env: *JniEnv, array: *void, start: s32,
count: s32, bytes: *u8) {
unused env
unused array
Require(start == 0 && count == 3, 92)
bytes[0] = 65
bytes[1] = 0
bytes[2] = 66
}
FakeReadText :: (env: *JniEnv, text: *void, copied: *u8) -> *u8 {
unused env
unused copied
return cast(*u8)text
}
FakeReleaseText :: (env: *JniEnv, text: *void, bytes: *u8) {
unused env
unused text
unused bytes
}
FakeArrayLength :: (env: *JniEnv, array: *void) -> s32 {
unused env
unused array
if network_test {
return network_length
}
return 0
}
FakeRegister :: (env: *JniEnv, klass: *void,
raw_methods: *void, count: s32) -> s32 {
unused env
methods := cast(*NativeMethod)raw_methods
if klass == cast(*void)*handoff_class {
handoff_methods = count
Require(count == 4 && CStringEquals(methods[0].name, "nativeAllowed") &&
CStringEquals(methods[3].name, "nativeSeal"), 117)
return 0
}
registered_methods = count
Require(count == 21, 22)
Require(CStringEquals(methods[4].name, "nativeSetSystemDark"), 23)
Require(CStringEquals(methods[19].name, "nativeSetWindowSize") &&
CStringEquals(methods[19].signature, "(II)V"), 110)
Require(CStringEquals(methods[20].name, "nativeAccessibilityAction") &&
CStringEquals(methods[20].signature, "(JI)Z"), 111)
Require(methods[20].procedure.accessibility(env, null, 0, 99) == 0, 112)
native_dark = methods[4].procedure.integer
native_practice = methods[15].procedure.practice
return 0
}
#program_export
android_lifecycle_reset :: () {
}
#program_export
android_sync_lifecycle :: (paused: s32, visible: s32) -> s32 {
return paused + visible
}
#program_export
android_invalidate_graphics_resources :: () {
}
#program_export
android_practice_to_start :: (practice: s32) -> s32 {
return practice
}
#program_export
android_debug_import_music_for_practice :: (path: *u8,
practice: s32) -> s32 {
unused path
unused practice
return 1
}
#program_export
android_debug_start_music_download :: () -> s32 {
return 1
}
#program_export
SetDeviceDensity :: (density: float32) {
unused density
}
#program_export
main :: () -> s32 {
environment.functions = *api
vm.functions = *vm_api
activity.vm = *vm
activity.activity = cast(*void)*activity
app.activity = *activity
// Kryon's JNI bridge reaches the activity through its native app.
jni_android_app = *app
vm_api.GetEnv = FakeGetEnv
vm_api.AttachCurrentThread = FakeAttach
vm_api.DetachCurrentThread = FakeDetach
api.PushLocalFrame = FakePushFrame
api.PopLocalFrame = FakePopFrame
api.ExceptionCheck = FakeException
api.ExceptionDescribe = FakeDescribe
api.ExceptionClear = FakeClear
api.FindClass = FakeFindClass
api.GetObjectClass = FakeObjectClass
api.GetMethodID = FakeMethod
api.GetStaticMethodID = FakeMethod
api.NewObjectA = FakeObject
api.CallStaticObjectMethodA = FakeObject
api.GetStaticFieldID = FakeField
api.GetStaticObjectField = FakeStaticField
api.GetStaticIntField = FakeStaticInteger
api.NewStringUTF = FakeString
api.CallVoidMethodA = FakeVoid
api.CallStaticVoidMethodA = FakeVoid
api.CallObjectMethodA = FakeObject
api.CallBooleanMethodA = FakeBoolean
api.CallIntMethodA = FakeInteger
api.DeleteLocalRef = FakeDelete
api.DeleteGlobalRef = FakeDelete
api.NewGlobalRef = FakeGlobal
api.GetJavaVM = FakeVm
api.NewByteArray = FakeBytes
api.SetByteArrayRegion = FakeSetBytes
api.GetByteArrayRegion = FakeGetBytes
api.GetStringUTFChars = FakeReadText
api.ReleaseStringUTFChars = FakeReleaseText
api.GetArrayLength = FakeArrayLength
api.RegisterNatives = FakeRegister
Require(JNI_OnLoad(*vm, null) == JniVersion && registered_methods == 21 && handoff_methods == 4, 30)
native_dark(*environment, cast(*void)*activity, 1)
Require(AndroidSystemDark() == 1, 31)
native_dark(*environment, cast(*void)*activity, 0)
Require(AndroidSystemDark() == 0, 32)
Require(native_practice(*environment, null, 3) == 1, 33)
Require(android_take_pending_practice_start() == 3, 34)
Require(android_take_pending_practice_start() == -1, 35)
path: [64]u8
CopyCString("/tmp/export.csv", path[:])
Require(health_connect_send_host(*path[0]), 40)
Require(frame_count == 0 && detach_count == 0, 41)
thread_detached = true
Require(health_connect_send_host(*path[0]), 42)
Require(attach_count == 1 && detach_count == 1 && frame_count == 0, 43)
exception_pending = 1
Require(!health_connect_send_host(*path[0]), 44)
Require(exception_pending == 0 && frame_count == 0, 45)
thread_detached = false
frame_failed = true
Require(!health_connect_send_host(*path[0]) && exception_pending == 0 &&
frame_count == 0, 49)
frame_failed = false
android_wakelock_set_activity(*environment, cast(*void)*activity)
Require(wakelock_acquire_host() && wakelock_release_host(), 46)
Require(wakelock_keep_screen_on_host() && wakelock_allow_screen_off_host(), 47)
Require(!wakelock_update_notification_host(null), 48)
Require(AndroidPickerOpen(3, "*/*") && last_integer == 3, 50)
AndroidNativePickerSelected(*environment, null, 3, cast(*void)*path[0])
output: [64]u8
Require(AndroidPickerPoll(2, output[:]) == .AndroidPickerNone, 51)
Require(AndroidPickerPoll(3, output[:]) == .AndroidPickerSelected, 52)
Require(CStringEquals(*output[0], "/tmp/export.csv"), 53)
Require(AndroidPickerPoll(3, output[:]) == .AndroidPickerNone, 54)
AndroidNativePickerCancelled(*environment, null, 3)
Require(AndroidPickerPoll(3, output[:]) == .AndroidPickerCancelled, 55)
Require(push_host_configure() && push_host_configure_with(*path[0]), 56)
Require(push_host_sync_scope(*path[0]) && CStringEquals(*method_name[0], "syncKryonPush"), 59)
Require(!push_host_sync_scope(null), 159)
Require(push_host_registered() == 1, 57)
catalog: PushCatalog
Require(push_host_read_catalog(*catalog) && catalog.count == 0, 58)
android_device_set_orientation_mode(2)
Require(last_integer == 2, 60)
Require(android_device_copy_text_and_toast(*path[0], null) == 1, 62)
Require(android_device_app_icon() == 2 && android_device_set_app_icon(1) == 1, 63)
input_session := SessionOpen()
AndroidTextInputOpen(input_session)
AndroidTextInputFrame(input_session, true)
Require(last_integer == 1, 64)
AndroidNativeTextCommit(*environment, null, 0x1f642)
AndroidNativeTextBackspace(*environment, null)
AndroidNativeTextEnter(*environment, null)
AndroidTextInputBeginFrame(input_session)
Require(AndroidTextInputCharacter() == 0x1f642 &&
AndroidTextInputCharacter() == 0, 65)
AndroidTextInputBeginFrame(input_session)
Require(AndroidTextInputKeyPressed(259), 66)
AndroidTextInputBeginFrame(input_session)
Require(AndroidTextInputKeyPressed(257), 67)
AndroidTextInputClose(input_session)
Require(last_integer == 0, 68)
SessionClose(input_session)
download: FileDownload
download.maximum_bytes = 65536
Require(FileDownloadStart(*download, "https://example.invalid/music.zip", "/data/music.zip") &&
download.handle > 0 && java_handle == cast(s64)download.handle, 70)
Require(java_asset_maximum == 65536, 99)
handle := download.handle
http_status: s32
FileDownloadNativeProgress(*environment, null, cast(s64)handle, 8, 10)
Require(FileDownloadPoll(*download) == FileDownloadStatus.Downloading &&
download.bytes == 8 && download.total_bytes == 10, 71)
FileDownloadNativeSucceeded(*environment, null, cast(s64)handle, 10, 200)
// A failure reported after success is ignored.
FileDownloadNativeFailed(*environment, null, cast(s64)handle, 500, null)
Require(FileDownloadPoll(*download) == FileDownloadStatus.Ready && download.bytes == 10 &&
download.http_status == 200, 72)
// A finished download is forgotten and the activity told so.
Require(java_asset_cancelled == cast(s64)handle && download.handle == 0, 100)
payload: [3]u8
payload[0] = 65
Require(ExportBytes(TextFromBytes(payload[:]), "export.csv", "application/octet-stream", "Share"), 80)
Require(!ExportBytes(TextFromBytes(BytesFromPointer(*payload[0], cast(u64)2147483648)),
"export.csv", "application/octet-stream", "Share"), 81)
wallet := "monero:84abc?tx_amount=0.1"
Require(CanOpenUri(wallet) && OpenUri(wallet), 84)
resolve_available = false
Require(!CanOpenUri(wallet) && OpenUri(wallet), 85)
launch_available = false
descriptions_before_launch := exception_descriptions
Require(!OpenUri(wallet) && exception_pending == 0 &&
exception_descriptions == descriptions_before_launch, 102)
launch_available = true
Require(frame_count == 0, 86)
unused CopyCString("zh-Hant-TW;ja-JP", locale_preferences[:])
Require(app_locale_preferences() == "zh-Hant-TW;ja-JP" && frame_count == 0, 89)
network_test = true
transfer: NetworkTransfer
transport := NetworkTransport(*transfer)
request: HttpRequest
request.method = "POST"
request.url = "https://example.invalid/sync"
request.content_type = "application/json"
request.body = "{}"
request.token = "token"
Require(transport.start(transport.context, request, output[:]) && network_timeout == 20000, 93)
Require(!transport.start(transport.context, request, output[:]), 94)
Require(transport.poll(transport.context, *http_status) == -1, 95)
network_state = 404
Require(transport.poll(transport.context, *http_status) == 1 &&
http_status == 404 && transfer.transfer.handle == 0 && output[0] == 65 &&
output[1] == 0 && output[2] == 66 && output[3] == 0 &&
network_cancelled == 1, 96)
Require(transport.start(transport.context, request, output[:]), 97)
network_length = 64
Require(transport.poll(transport.context, *http_status) == 0 &&
output[0] == 0 && network_cancelled == 2, 98)
network_header_failed = true
Require(!transport.start(transport.context, request, output[:]) &&
transfer.transfer.handle == 0 && network_cancelled == 3, 99)
network_header_failed = false
Require(transport.start(transport.context, request, output[:]), 100)
transport.cancel(transport.context)
Require(transfer.transfer.handle == 0 && transfer.transfer.output.count == 0 &&
network_cancelled == 4 && frame_count == 0, 101)
request.url = "https://example.invalid/api/v1/chat/completions"
Require(transport.start(transport.context, request, output[:]) && network_timeout == 45000, 115)
transport.cancel(transport.context)
Require(transfer.transfer.handle == 0 && network_cancelled == 5, 116)
before_startup_calls := void_calls
AndroidStartupComplete()
Require(void_calls == before_startup_calls + 1 &&
CStringEquals(*method_name[0], "onNativeFirstFrame") && frame_count == 0, 102)
jni_android_app = null
AndroidStartupComplete()
Require(void_calls == before_startup_calls + 1 && frame_count == 0, 103)
Require(!health_connect_send_host(*path[0]), 82)
Require(frame_count == 0, 83)
return 0
}