241 lines
8.5 KiB
Ziran
241 lines
8.5 KiB
Ziran
// Android's document picker and QR scanner: requests reach the activity with
|
|
// their number, each answer is taken once by the request that asked, and a
|
|
// newer request replaces an unanswered one. Runs with ANDROID_BUILD on the
|
|
// host; a fake JNI environment stands in for the activity.
|
|
#import "android_jni"
|
|
#import "android_native"
|
|
#import "android_native_test_host"
|
|
#import "android_picker"
|
|
#import "c_string"
|
|
|
|
c :: #system_library "c";
|
|
TestExit :: (status: s32) #foreign c "exit";
|
|
|
|
api: JniFunctions;
|
|
vm_api: JniVmFunctions;
|
|
environment: JniEnv;
|
|
vm: JniVm;
|
|
activity: NativeActivity;
|
|
app: NativeAndroidApp;
|
|
frames: s32;
|
|
exception_pending: u8;
|
|
method_name: [96]u8;
|
|
method_signature: [192]u8;
|
|
java_text: [256]u8;
|
|
java_strings: [32][256]u8;
|
|
java_string_count: s32;
|
|
calls: s32;
|
|
last_request: s32;
|
|
java_throws: bool;
|
|
commit_succeeds: bool = true;
|
|
|
|
Require :: (condition: bool, code: s32) {
|
|
if !condition {
|
|
TestExit(code)
|
|
}
|
|
}
|
|
|
|
CStringEquals :: (text: *u8, expected: string) -> bool {
|
|
if text == null {
|
|
return false
|
|
}
|
|
index: s64 = 0
|
|
while index < expected.count {
|
|
if text[index] != expected[index] {
|
|
return false
|
|
}
|
|
index += 1
|
|
}
|
|
return text[index] == 0
|
|
}
|
|
|
|
CopyText :: (text: *u8, output: []u8) {
|
|
index: s64 = 0
|
|
while index + 1 < output.count && text[index] != 0 {
|
|
output[index] = text[index]
|
|
index += 1
|
|
}
|
|
output[index] = 0
|
|
}
|
|
|
|
FakeGetEnv :: (machine: *JniVm, output: **JniEnv, version: s32) -> s32 {
|
|
unused machine
|
|
unused version
|
|
output[0] = *environment
|
|
return 0
|
|
}
|
|
FakePushFrame :: (env: *JniEnv, capacity: s32) -> s32 {
|
|
unused env
|
|
unused capacity
|
|
frames += 1
|
|
return 0
|
|
}
|
|
FakePopFrame :: (env: *JniEnv, result: *void) -> *void {
|
|
unused env
|
|
frames -= 1
|
|
return result
|
|
}
|
|
FakeException :: (env: *JniEnv) -> u8 {
|
|
unused env
|
|
return exception_pending
|
|
}
|
|
FakeDescribe :: (env: *JniEnv) {
|
|
unused env
|
|
}
|
|
FakeClear :: (env: *JniEnv) {
|
|
unused env
|
|
exception_pending = 0
|
|
}
|
|
FakeObjectClass :: (env: *JniEnv, object: *void) -> *void {
|
|
unused env
|
|
return object
|
|
}
|
|
FakeMethod :: (env: *JniEnv, klass: *void, name: *u8, signature: *u8) -> *void {
|
|
unused env
|
|
unused klass
|
|
CopyText(name, method_name[:])
|
|
CopyText(signature, method_signature[:])
|
|
return cast(*void)*method_name[0]
|
|
}
|
|
FakeString :: (env: *JniEnv, text: *u8) -> *void {
|
|
unused env
|
|
CopyText(text, java_text[:])
|
|
java_string_count += 1
|
|
CopyText(text, java_strings[java_string_count][:])
|
|
return cast(*void)*java_strings[java_string_count][0]
|
|
}
|
|
FakeVoid :: (env: *JniEnv, object: *void, method: *void, arguments: *JniValue) {
|
|
unused env
|
|
unused object
|
|
unused method
|
|
calls += 1
|
|
if CStringEquals(*method_name[0], "openSavePicker") {
|
|
last_request = arguments[2].integer
|
|
Require(CStringEquals(cast(*u8)arguments[0].reference, "account.key"), 20)
|
|
Require(CStringEquals(cast(*u8)arguments[1].reference, "application/octet-stream"), 21)
|
|
} else {
|
|
last_request = arguments[1].integer
|
|
}
|
|
if java_throws {
|
|
exception_pending = 1
|
|
}
|
|
}
|
|
FakeBoolean :: (env: *JniEnv, object: *void, method: *void, arguments: *JniValue) -> u8 {
|
|
unused env
|
|
unused object
|
|
unused method
|
|
Require(CStringEquals(*method_name[0], "savePickedFile") &&
|
|
CStringEquals(*method_signature[0], "(Ljava/lang/String;)Z"), 22)
|
|
Require(CStringEquals(cast(*u8)arguments[0].reference, "/data/cache/save.tmp"), 23)
|
|
if java_throws { exception_pending = 1 }
|
|
return ifx commit_succeeds then 1 else 0
|
|
}
|
|
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
|
|
}
|
|
|
|
#program_export
|
|
main :: () -> s32 {
|
|
environment.functions = *api
|
|
vm.functions = *vm_api
|
|
activity.vm = *vm
|
|
activity.activity = cast(*void)*activity
|
|
app.activity = *activity
|
|
vm_api.GetEnv = FakeGetEnv
|
|
api.PushLocalFrame = FakePushFrame
|
|
api.PopLocalFrame = FakePopFrame
|
|
api.ExceptionCheck = FakeException
|
|
api.ExceptionDescribe = FakeDescribe
|
|
api.ExceptionClear = FakeClear
|
|
api.GetObjectClass = FakeObjectClass
|
|
api.GetMethodID = FakeMethod
|
|
api.NewStringUTF = FakeString
|
|
api.CallVoidMethodA = FakeVoid
|
|
api.CallBooleanMethodA = FakeBoolean
|
|
api.GetStringUTFChars = FakeReadText
|
|
api.ReleaseStringUTFChars = FakeReleaseText
|
|
|
|
// NativeActivity, rather than a renderer-owned getter, registers the
|
|
// app used by picker calls. No UI or OS window is opened by this fixture.
|
|
before: AndroidJniSession
|
|
Require(AndroidJniAttachCurrent(*before) == 0, 34)
|
|
AndroidNativeStart(*app)
|
|
defer { AndroidNativeClose() }
|
|
Require(AndroidNativeApp() == *app && app.input_callback != null, 35)
|
|
// A document request reaches openImportPicker with its number.
|
|
Require(AndroidPickerOpen(4, "image/*"), 1)
|
|
Require(calls == 1 && last_request == 4 && frames == 0, 2)
|
|
Require(CStringEquals(*method_name[0], "openImportPicker") &&
|
|
CStringEquals(*method_signature[0], "(Ljava/lang/String;I)V") &&
|
|
CStringEquals(*java_text[0], "image/*"), 3)
|
|
output: [64]u8
|
|
Require(AndroidPickerPoll(4, output[:]) == .AndroidPickerNone && output[0] == 0, 4)
|
|
|
|
// The answer belongs to its request and is taken once.
|
|
path: [64]u8
|
|
unused CopyCString("/data/files/import/photo.jpg", path[:])
|
|
AndroidNativePickerSelected(*environment, null, 4, cast(*void)*path[0])
|
|
Require(AndroidPickerPoll(0, output[:]) == .AndroidPickerNone, 5)
|
|
Require(AndroidPickerPoll(4, output[:]) == .AndroidPickerSelected &&
|
|
CStringEquals(*output[0], "/data/files/import/photo.jpg"), 6)
|
|
Require(AndroidPickerPoll(4, output[:]) == .AndroidPickerNone && output[0] == 0, 7)
|
|
|
|
// A scan request reaches openQrScanner; its code arrives as text, cut to
|
|
// the caller's buffer.
|
|
Require(AndroidScannerOpen(5, "Scan the code on your other device"), 8)
|
|
Require(calls == 2 && last_request == 5 &&
|
|
CStringEquals(*method_name[0], "openQrScanner") &&
|
|
CStringEquals(*java_text[0], "Scan the code on your other device"), 9)
|
|
code: [64]u8
|
|
unused CopyCString("inbe-link:abcdef", code[:])
|
|
AndroidNativePickerSelected(*environment, null, 5, cast(*void)*code[0])
|
|
short: [6]u8
|
|
Require(AndroidPickerPoll(5, short[:]) == .AndroidPickerSelected &&
|
|
CStringEquals(*short[0], "inbe-"), 10)
|
|
|
|
// A newer request replaces an unanswered one, and cancelling reports so.
|
|
Require(AndroidPickerOpen(0, "*/*") && AndroidPickerOpen(2, "audio/*"), 11)
|
|
AndroidNativePickerCancelled(*environment, null, 2)
|
|
Require(AndroidPickerPoll(0, output[:]) == .AndroidPickerNone, 12)
|
|
Require(AndroidPickerPoll(2, output[:]) == .AndroidPickerCancelled && output[0] == 0, 13)
|
|
// Opening clears an answer nobody took.
|
|
AndroidNativePickerSelected(*environment, null, 3, cast(*void)*path[0])
|
|
Require(AndroidPickerOpen(3, "*/*") && AndroidPickerPoll(3, output[:]) == .AndroidPickerNone, 14)
|
|
|
|
// A Java exception fails the request and is cleared.
|
|
java_throws = true
|
|
Require(!AndroidPickerOpen(1, "*/*") && !AndroidScannerOpen(5, "Scan"), 15)
|
|
Require(exception_pending == 0 && frames == 0 && calls == 7, 16)
|
|
java_throws = false
|
|
Require(AndroidSavePickerOpen(7, "account.key", "application/octet-stream"), 24)
|
|
Require(last_request == 7 && CStringEquals(*method_signature[0],
|
|
"(Ljava/lang/String;Ljava/lang/String;I)V"), 25)
|
|
unused CopyCString("/data/cache/save.tmp", path[:])
|
|
AndroidNativePickerSelected(*environment, null, 7, cast(*void)*path[0])
|
|
Require(AndroidPickerPoll(7, output[:]) == .AndroidPickerSelected &&
|
|
CStringEquals(*output[0], "/data/cache/save.tmp"), 26)
|
|
Require(AndroidSavePickerCommit("/data/cache/save.tmp"), 27)
|
|
commit_succeeds = false
|
|
Require(!AndroidSavePickerCommit("/data/cache/save.tmp"), 28)
|
|
commit_succeeds = true
|
|
java_throws = true
|
|
Require(!AndroidSavePickerCommit("/data/cache/save.tmp"), 29)
|
|
Require(exception_pending == 0 && frames == 0, 30)
|
|
Require(!AndroidSavePickerOpen(7, "", "application/octet-stream") &&
|
|
!AndroidSavePickerCommit(""), 31)
|
|
java_throws = false
|
|
types := "application/pgp-keys,application/octet-stream,application/json,text/plain"
|
|
Require(AndroidPickerOpen(8, types), 32)
|
|
Require(last_request == 8 && CStringEquals(*java_text[0], types) &&
|
|
CStringEquals(*method_name[0], "openImportPicker"), 33)
|
|
Require(native_test_unexpected_effects == 0 && native_test_finish_calls == 0, 36)
|
|
return 0
|
|
}
|