|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a
|
3 | 3 | // BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
5 |
| -// This file contains mostly the same tests from package:jni's |
6 |
| -// test/jni_object_test.dart. This file can be run on android device using |
7 |
| -// flutter test integration_test/ and therefore useful for checking the |
8 |
| -// working of JNI on an Android device (or emulator). |
9 |
| - |
10 |
| -import 'dart:io'; |
11 |
| - |
12 | 5 | import 'package:flutter_test/flutter_test.dart';
|
13 | 6 |
|
14 |
| -import 'package:jni/jni.dart'; |
| 7 | +import '../../test/global_env_test.dart' as global_env_test; |
| 8 | +import '../../test/exception_test.dart' as exception_test; |
| 9 | +import '../../test/jobject_test.dart' as jobject_test; |
| 10 | +import '../../test/jarray_test.dart' as jarray_test; |
| 11 | +import '../../test/type_test.dart' as type_test; |
| 12 | + |
| 13 | +void integrationTestRunner(String description, void Function() testCallback) { |
| 14 | + testWidgets(description, (widgetTester) async => testCallback()); |
| 15 | +} |
15 | 16 |
|
16 | 17 | void main() {
|
17 |
| - if (!Platform.isAndroid) { |
18 |
| - try { |
19 |
| - Jni.spawn(dylibDir: "build/jni_libs"); |
20 |
| - } on JvmExistsException { |
21 |
| - // TODO(#51): Support destroying and restarting JVM. |
22 |
| - } |
| 18 | + final testSuites = [ |
| 19 | + global_env_test.run, |
| 20 | + exception_test.run, |
| 21 | + jobject_test.run, |
| 22 | + jarray_test.run, |
| 23 | + type_test.run, |
| 24 | + ]; |
| 25 | + for (var testSuite in testSuites) { |
| 26 | + testSuite(testRunner: integrationTestRunner); |
23 | 27 | }
|
24 |
| - |
25 |
| - testWidgets("Long.intValue() using JObject", (t) async { |
26 |
| - final longClass = Jni.findJniClass("java/lang/Long"); |
27 |
| - |
28 |
| - final longCtor = longClass.getCtorID("(J)V"); |
29 |
| - |
30 |
| - final long = longClass.newInstance(longCtor, [176]); |
31 |
| - |
32 |
| - final intValue = long.callMethodByName<int>("intValue", "()I", []); |
33 |
| - expect(intValue, equals(176)); |
34 |
| - |
35 |
| - long.delete(); |
36 |
| - longClass.delete(); |
37 |
| - }); |
38 |
| - |
39 |
| - testWidgets("call a static method using JniClass APIs", (t) async { |
40 |
| - final integerClass = JniClass.fromRef(Jni.findClass("java/lang/Integer")); |
41 |
| - final result = integerClass.callStaticMethodByName<JString>( |
42 |
| - "toHexString", "(I)Ljava/lang/String;", [JValueInt(31)]); |
43 |
| - |
44 |
| - final resultString = result.toDartString(); |
45 |
| - |
46 |
| - result.delete(); |
47 |
| - expect(resultString, equals("1f")); |
48 |
| - |
49 |
| - integerClass.delete(); |
50 |
| - }); |
51 |
| - |
52 |
| - testWidgets("Example for using getMethodID", (t) async { |
53 |
| - final longClass = Jni.findJniClass("java/lang/Long"); |
54 |
| - final bitCountMethod = longClass.getStaticMethodID("bitCount", "(J)I"); |
55 |
| - |
56 |
| - final random = Jni.newInstance("java/util/Random", "()V", []); |
57 |
| - |
58 |
| - final nextIntMethod = random.getMethodID("nextInt", "(I)I"); |
59 |
| - |
60 |
| - for (int i = 0; i < 100; i++) { |
61 |
| - int r = random.callMethod<int>(nextIntMethod, [JValueInt(256 * 256)]); |
62 |
| - int bits = 0; |
63 |
| - final jbc = longClass.callStaticMethod<int>(bitCountMethod, [r]); |
64 |
| - while (r != 0) { |
65 |
| - bits += r % 2; |
66 |
| - r = (r / 2).floor(); |
67 |
| - } |
68 |
| - expect(jbc, equals(bits)); |
69 |
| - } |
70 |
| - random.delete(); |
71 |
| - longClass.delete(); |
72 |
| - }); |
73 |
| - |
74 |
| - // Actually it's not even required to get a reference to class |
75 |
| - testWidgets("invoke_", (t) async { |
76 |
| - final m = Jni.invokeStaticMethod<int>( |
77 |
| - "java/lang/Long", "min", "(JJ)J", [1234, 1324], JniCallType.longType); |
78 |
| - expect(m, equals(1234)); |
79 |
| - }); |
80 |
| - |
81 |
| - testWidgets("retrieve_", (t) async { |
82 |
| - final maxLong = Jni.retrieveStaticField<int>( |
83 |
| - "java/lang/Short", "MAX_VALUE", "S", JniCallType.shortType); |
84 |
| - expect(maxLong, equals(32767)); |
85 |
| - }); |
86 |
| - |
87 |
| - testWidgets("Call method with null argument, expect exception", |
88 |
| - (tester) async { |
89 |
| - final integerClass = Jni.findJniClass("java/lang/Integer"); |
90 |
| - expect( |
91 |
| - () => integerClass.callStaticMethodByName<int>( |
92 |
| - "parseInt", "(Ljava/lang/String;)I", [nullptr]), |
93 |
| - throwsException); |
94 |
| - }); |
95 |
| - |
96 |
| - testWidgets("callStaticStringMethod", (t) async { |
97 |
| - final longClass = Jni.findJniClass("java/lang/Long"); |
98 |
| - const n = 1223334444; |
99 |
| - final strFromJava = longClass.callStaticMethodByName<String>( |
100 |
| - "toOctalString", "(J)Ljava/lang/String;", [n]); |
101 |
| - expect(strFromJava, equals(n.toRadixString(8))); |
102 |
| - longClass.delete(); |
103 |
| - }); |
104 |
| - |
105 |
| - testWidgets("Passing strings in arguments", (t) async { |
106 |
| - final twelve = Jni.invokeStaticMethod<int>("java/lang/Byte", "parseByte", |
107 |
| - "(Ljava/lang/String;)B", ["12"], JniCallType.byteType); |
108 |
| - expect(twelve, equals(12)); |
109 |
| - }); |
110 |
| - |
111 |
| - testWidgets("use() method", (t) async { |
112 |
| - final randomInt = Jni.newInstance("java/util/Random", "()V", []).use( |
113 |
| - (random) => |
114 |
| - random.callMethodByName<int>("nextInt", "(I)I", [JValueInt(15)])); |
115 |
| - expect(randomInt, lessThan(15)); |
116 |
| - }); |
117 |
| - |
118 |
| - testWidgets("enums", (t) async { |
119 |
| - final ordinal = Jni.retrieveStaticField<JObject>( |
120 |
| - "java/net/Proxy\$Type", "HTTP", "Ljava/net/Proxy\$Type;") |
121 |
| - .use((f) => f.callMethodByName<int>("ordinal", "()I", [])); |
122 |
| - expect(ordinal, equals(1)); |
123 |
| - }); |
124 | 28 | }
|
0 commit comments