OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Tests for jni_generator.py. | 6 """Tests for jni_generator.py. |
7 | 7 |
8 This test suite contains various tests for the JNI generator. | 8 This test suite contains various tests for the JNI generator. |
9 It exercises the low-level parser all the way up to the | 9 It exercises the low-level parser all the way up to the |
10 code generator and ensures the output matches a golden | 10 code generator and ensures the output matches a golden |
(...skipping 10 matching lines...) Expand all Loading... |
21 | 21 |
22 SCRIPT_NAME = 'base/android/jni_generator/jni_generator.py' | 22 SCRIPT_NAME = 'base/android/jni_generator/jni_generator.py' |
23 | 23 |
24 | 24 |
25 class TestOptions(object): | 25 class TestOptions(object): |
26 """The mock options object which is passed to the jni_generator.py script.""" | 26 """The mock options object which is passed to the jni_generator.py script.""" |
27 | 27 |
28 def __init__(self): | 28 def __init__(self): |
29 self.namespace = None | 29 self.namespace = None |
30 self.script_name = SCRIPT_NAME | 30 self.script_name = SCRIPT_NAME |
| 31 self.ptr_type = 'int' |
31 | 32 |
32 | 33 |
33 class TestGenerator(unittest.TestCase): | 34 class TestGenerator(unittest.TestCase): |
34 def assertObjEquals(self, first, second): | 35 def assertObjEquals(self, first, second): |
35 dict_first = first.__dict__ | 36 dict_first = first.__dict__ |
36 dict_second = second.__dict__ | 37 dict_second = second.__dict__ |
37 self.assertEquals(dict_first.keys(), dict_second.keys()) | 38 self.assertEquals(dict_first.keys(), dict_second.keys()) |
38 for key, value in dict_first.iteritems(): | 39 for key, value in dict_first.iteritems(): |
39 if (type(value) is list and len(value) and | 40 if (type(value) is list and len(value) and |
40 isinstance(type(value[0]), object)): | 41 isinstance(type(value[0]), object)): |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 private static native OnFrameAvailableListener nativeGetInnerClass(); | 89 private static native OnFrameAvailableListener nativeGetInnerClass(); |
89 private native Bitmap nativeQueryBitmap( | 90 private native Bitmap nativeQueryBitmap( |
90 int nativeChromeBrowserProvider, | 91 int nativeChromeBrowserProvider, |
91 String[] projection, String selection, | 92 String[] projection, String selection, |
92 String[] selectionArgs, String sortOrder); | 93 String[] selectionArgs, String sortOrder); |
93 private native void nativeGotOrientation( | 94 private native void nativeGotOrientation( |
94 int nativeDataFetcherImplAndroid, | 95 int nativeDataFetcherImplAndroid, |
95 double alpha, double beta, double gamma); | 96 double alpha, double beta, double gamma); |
96 """ | 97 """ |
97 jni_generator.JniParams.ExtractImportsAndInnerClasses(test_data) | 98 jni_generator.JniParams.ExtractImportsAndInnerClasses(test_data) |
98 natives = jni_generator.ExtractNatives(test_data) | 99 natives = jni_generator.ExtractNatives(test_data, 'int') |
99 golden_natives = [ | 100 golden_natives = [ |
100 NativeMethod(return_type='int', static=False, | 101 NativeMethod(return_type='int', static=False, |
101 name='Init', | 102 name='Init', |
102 params=[], | 103 params=[], |
103 java_class_name=None, | 104 java_class_name=None, |
104 type='function'), | 105 type='function'), |
105 NativeMethod(return_type='void', static=False, name='Destroy', | 106 NativeMethod(return_type='void', static=False, name='Destroy', |
106 params=[Param(datatype='int', | 107 params=[Param(datatype='int', |
107 name='nativeChromeBrowserProvider')], | 108 name='nativeChromeBrowserProvider')], |
108 java_class_name=None, | 109 java_class_name=None, |
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
438 """ | 439 """ |
439 self.assertTextEquals(golden_content, h.GetContent()) | 440 self.assertTextEquals(golden_content, h.GetContent()) |
440 | 441 |
441 def testInnerClassNatives(self): | 442 def testInnerClassNatives(self): |
442 test_data = """ | 443 test_data = """ |
443 class MyInnerClass { | 444 class MyInnerClass { |
444 @NativeCall("MyInnerClass") | 445 @NativeCall("MyInnerClass") |
445 private native int nativeInit(); | 446 private native int nativeInit(); |
446 } | 447 } |
447 """ | 448 """ |
448 natives = jni_generator.ExtractNatives(test_data) | 449 natives = jni_generator.ExtractNatives(test_data, 'int') |
449 golden_natives = [ | 450 golden_natives = [ |
450 NativeMethod(return_type='int', static=False, | 451 NativeMethod(return_type='int', static=False, |
451 name='Init', params=[], | 452 name='Init', params=[], |
452 java_class_name='MyInnerClass', | 453 java_class_name='MyInnerClass', |
453 type='function') | 454 type='function') |
454 ] | 455 ] |
455 self.assertListEquals(golden_natives, natives) | 456 self.assertListEquals(golden_natives, natives) |
456 h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', | 457 h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', |
457 natives, [], TestOptions()) | 458 natives, [], TestOptions()) |
458 golden_content = """\ | 459 golden_content = """\ |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
521 test_data = """ | 522 test_data = """ |
522 class MyInnerClass { | 523 class MyInnerClass { |
523 @NativeCall("MyInnerClass") | 524 @NativeCall("MyInnerClass") |
524 private native int nativeInit(); | 525 private native int nativeInit(); |
525 } | 526 } |
526 class MyOtherInnerClass { | 527 class MyOtherInnerClass { |
527 @NativeCall("MyOtherInnerClass") | 528 @NativeCall("MyOtherInnerClass") |
528 private native int nativeInit(); | 529 private native int nativeInit(); |
529 } | 530 } |
530 """ | 531 """ |
531 natives = jni_generator.ExtractNatives(test_data) | 532 natives = jni_generator.ExtractNatives(test_data, 'int') |
532 golden_natives = [ | 533 golden_natives = [ |
533 NativeMethod(return_type='int', static=False, | 534 NativeMethod(return_type='int', static=False, |
534 name='Init', params=[], | 535 name='Init', params=[], |
535 java_class_name='MyInnerClass', | 536 java_class_name='MyInnerClass', |
536 type='function'), | 537 type='function'), |
537 NativeMethod(return_type='int', static=False, | 538 NativeMethod(return_type='int', static=False, |
538 name='Init', params=[], | 539 name='Init', params=[], |
539 java_class_name='MyOtherInnerClass', | 540 java_class_name='MyOtherInnerClass', |
540 type='function') | 541 type='function') |
541 ] | 542 ] |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 def testInnerClassNativesBothInnerAndOuter(self): | 628 def testInnerClassNativesBothInnerAndOuter(self): |
628 test_data = """ | 629 test_data = """ |
629 class MyOuterClass { | 630 class MyOuterClass { |
630 private native int nativeInit(); | 631 private native int nativeInit(); |
631 class MyOtherInnerClass { | 632 class MyOtherInnerClass { |
632 @NativeCall("MyOtherInnerClass") | 633 @NativeCall("MyOtherInnerClass") |
633 private native int nativeInit(); | 634 private native int nativeInit(); |
634 } | 635 } |
635 } | 636 } |
636 """ | 637 """ |
637 natives = jni_generator.ExtractNatives(test_data) | 638 natives = jni_generator.ExtractNatives(test_data, 'int') |
638 golden_natives = [ | 639 golden_natives = [ |
639 NativeMethod(return_type='int', static=False, | 640 NativeMethod(return_type='int', static=False, |
640 name='Init', params=[], | 641 name='Init', params=[], |
641 java_class_name=None, | 642 java_class_name=None, |
642 type='function'), | 643 type='function'), |
643 NativeMethod(return_type='int', static=False, | 644 NativeMethod(return_type='int', static=False, |
644 name='Init', params=[], | 645 name='Init', params=[], |
645 java_class_name='MyOtherInnerClass', | 646 java_class_name='MyOtherInnerClass', |
646 type='function') | 647 type='function') |
647 ] | 648 ] |
(...skipping 1517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2165 self.assertRaises(SyntaxError, | 2166 self.assertRaises(SyntaxError, |
2166 jni_generator.JniParams.JavaToJni, | 2167 jni_generator.JniParams.JavaToJni, |
2167 'AnException') | 2168 'AnException') |
2168 | 2169 |
2169 def testJniParamsJavaToJni(self): | 2170 def testJniParamsJavaToJni(self): |
2170 self.assertTextEquals('I', JniParams.JavaToJni('int')) | 2171 self.assertTextEquals('I', JniParams.JavaToJni('int')) |
2171 self.assertTextEquals('[B', JniParams.JavaToJni('byte[]')) | 2172 self.assertTextEquals('[B', JniParams.JavaToJni('byte[]')) |
2172 self.assertTextEquals( | 2173 self.assertTextEquals( |
2173 '[Ljava/nio/ByteBuffer;', JniParams.JavaToJni('java/nio/ByteBuffer[]')) | 2174 '[Ljava/nio/ByteBuffer;', JniParams.JavaToJni('java/nio/ByteBuffer[]')) |
2174 | 2175 |
| 2176 def testNativesLong(self): |
| 2177 test_options = TestOptions() |
| 2178 test_options.ptr_type = 'long' |
| 2179 test_data = """" |
| 2180 private native void nativeDestroy(long nativeChromeBrowserProvider); |
| 2181 """ |
| 2182 jni_generator.JniParams.ExtractImportsAndInnerClasses(test_data) |
| 2183 natives = jni_generator.ExtractNatives(test_data, test_options.ptr_type) |
| 2184 golden_natives = [ |
| 2185 NativeMethod(return_type='void', static=False, name='Destroy', |
| 2186 params=[Param(datatype='long', |
| 2187 name='nativeChromeBrowserProvider')], |
| 2188 java_class_name=None, |
| 2189 type='method', |
| 2190 p0_type='ChromeBrowserProvider', |
| 2191 ptr_type=test_options.ptr_type), |
| 2192 ] |
| 2193 self.assertListEquals(golden_natives, natives) |
| 2194 h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', |
| 2195 natives, [], test_options) |
| 2196 golden_content = """\ |
| 2197 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2198 // Use of this source code is governed by a BSD-style license that can be |
| 2199 // found in the LICENSE file. |
| 2200 |
| 2201 // This file is autogenerated by |
| 2202 // base/android/jni_generator/jni_generator.py |
| 2203 // For |
| 2204 // org/chromium/TestJni |
| 2205 |
| 2206 #ifndef org_chromium_TestJni_JNI |
| 2207 #define org_chromium_TestJni_JNI |
| 2208 |
| 2209 #include <jni.h> |
| 2210 |
| 2211 #include "base/android/jni_android.h" |
| 2212 #include "base/android/scoped_java_ref.h" |
| 2213 #include "base/basictypes.h" |
| 2214 #include "base/logging.h" |
| 2215 |
| 2216 using base::android::ScopedJavaLocalRef; |
| 2217 |
| 2218 // Step 1: forward declarations. |
| 2219 namespace { |
| 2220 const char kTestJniClassPath[] = "org/chromium/TestJni"; |
| 2221 // Leaking this jclass as we cannot use LazyInstance from some threads. |
| 2222 jclass g_TestJni_clazz = NULL; |
| 2223 } // namespace |
| 2224 |
| 2225 // Step 2: method stubs. |
| 2226 static void Destroy(JNIEnv* env, jobject obj, |
| 2227 jlong nativeChromeBrowserProvider) { |
| 2228 DCHECK(nativeChromeBrowserProvider) << "Destroy"; |
| 2229 ChromeBrowserProvider* native = |
| 2230 reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider); |
| 2231 return native->Destroy(env, obj); |
| 2232 } |
| 2233 |
| 2234 // Step 3: RegisterNatives. |
| 2235 |
| 2236 static bool RegisterNativesImpl(JNIEnv* env) { |
| 2237 |
| 2238 g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( |
| 2239 base::android::GetClass(env, kTestJniClassPath).obj())); |
| 2240 static const JNINativeMethod kMethodsTestJni[] = { |
| 2241 { "nativeDestroy", |
| 2242 "(" |
| 2243 "J" |
| 2244 ")" |
| 2245 "V", reinterpret_cast<void*>(Destroy) }, |
| 2246 }; |
| 2247 const int kMethodsTestJniSize = arraysize(kMethodsTestJni); |
| 2248 |
| 2249 if (env->RegisterNatives(g_TestJni_clazz, |
| 2250 kMethodsTestJni, |
| 2251 kMethodsTestJniSize) < 0) { |
| 2252 LOG(ERROR) << "RegisterNatives failed in " << __FILE__; |
| 2253 return false; |
| 2254 } |
| 2255 |
| 2256 return true; |
| 2257 } |
| 2258 |
| 2259 #endif // org_chromium_TestJni_JNI |
| 2260 """ |
| 2261 self.assertTextEquals(golden_content, h.GetContent()) |
| 2262 |
2175 | 2263 |
2176 if __name__ == '__main__': | 2264 if __name__ == '__main__': |
2177 unittest.main() | 2265 unittest.main() |
OLD | NEW |