Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: base/android/jni_generator/jni_generator_tests.py

Issue 9384011: Chrome on Android: adds jni_generator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Patch Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 """Tests for jni_generator.py."""
8
9 import difflib
10 import os
11 import sys
12 import unittest
13 import jni_generator
14 from jni_generator import CalledByNative, NativeMethod, Param
15
16
17 class TestGenerator(unittest.TestCase):
18 def assertObjEquals(self, first, second):
19 dict_first = first.__dict__
20 dict_second = second.__dict__
21 self.assertEquals(dict_first.keys(), dict_second.keys())
22 for key, value in dict_first.iteritems():
23 if (type(value) is list and len(value) and
24 isinstance(type(value[0]), object)):
25 self.assertListEquals(value, second.__getattribute__(key))
26 else:
27 self.assertEquals(value, second.__getattribute__(key))
28
29 def assertListEquals(self, first, second):
30 self.assertEquals(len(first), len(second))
31 for i in xrange(len(first)):
32 if isinstance(first[i], object):
33 self.assertObjEquals(first[i], second[i])
34 else:
35 self.assertEquals(first[i], second[i])
36
37 def assertTextEquals(self, golden_text, generated_text):
38 stripped_golden = [l.strip() for l in golden_text.split('\n')]
39 stripped_generated = [l.strip() for l in generated_text.split('\n')]
40 if stripped_golden != stripped_generated:
41 print self.id()
42 for line in difflib.context_diff(stripped_golden, stripped_generated):
43 print line
44 self.fail('Golden text mismatch')
45
46 def testNatives(self):
47 test_data = """"
48 private native int nativeInit();
49 private native void nativeDestroy(int nativeChromeBrowserProvider);
50 private native long nativeAddBookmark(
51 int nativeChromeBrowserProvider,
52 String url, String title, boolean isFolder, long parentId);
53 private static native String nativeGetDomainAndRegistry(String url);
54 private static native void nativeCreateHistoricalTabFromState(byte[] state, int tab_index);
joth 2012/02/14 00:46:49 should .py be 80 col? Plenty to do here... ah han
bulach 2012/02/14 02:12:32 these are different levels / aspect of testing tha
55 private native byte[] nativeGetStateAsByteArray(ChromeView view);
56 private static native String[] nativeGetAutofillProfileGUIDs();
57 private native void nativeSetRecognitionResults(int sessionId, String[] resu lts);
58 private native long nativeAddBookmarkFromAPI(int nativeChromeBrowserProvider ,
59 String url, Long created, Boolean isBookmark,
60 Long date, byte[] favicon, String title, Integer visits);
61 native int nativeFindAll(String find);
62 private static native BookmarkNode nativeGetDefaultBookmarkFolder();
63 private native SQLiteCursor nativeQueryBookmarkFromAPI(int nativeChromeBrows erProvider,
64 String[] projection, String selection,
65 String[] selectionArgs, String sortOrder);
66 private native void nativeGotOrientation(int nativePtr /* device_orientation ::DeviceOrientationAndroid */,
67 double alpha, double beta, double gamma);
68 """
69 natives = jni_generator.ExtractNatives(test_data)
70 golden_natives = [
71 NativeMethod(return_type='int', static=False,
72 name='Init',
73 params=[],
74 java_class_name='',
75 type='function'),
76 NativeMethod(return_type='void', static=False, name='Destroy',
77 params=[Param(datatype='int',
78 name='nativeChromeBrowserProvider')],
79 java_class_name='',
80 type='method',
81 p0_type='ChromeBrowserProvider'),
82 NativeMethod(return_type='long', static=False, name='AddBookmark',
83 params=[Param(datatype='int',
84 name='nativeChromeBrowserProvider'),
85 Param(datatype='String',
86 name='url'),
87 Param(datatype='String',
88 name='title'),
89 Param(datatype='boolean',
90 name='isFolder'),
91 Param(datatype='long',
92 name='parentId')],
93 java_class_name='',
94 type='method',
95 p0_type='ChromeBrowserProvider'),
96 NativeMethod(return_type='String', static=True,
97 name='GetDomainAndRegistry',
98 params=[Param(datatype='String',
99 name='url')],
100 java_class_name='',
101 type='function'),
102 NativeMethod(return_type='void', static=True,
103 name='CreateHistoricalTabFromState',
104 params=[Param(datatype='byte[]',
105 name='state'),
106 Param(datatype='int',
107 name='tab_index')],
108 java_class_name='',
109 type='function'),
110 NativeMethod(return_type='byte[]', static=False,
111 name='GetStateAsByteArray',
112 params=[Param(datatype='ChromeView', name='view')],
113 java_class_name='',
114 type='function'),
115 NativeMethod(return_type='String[]', static=True,
116 name='GetAutofillProfileGUIDs', params=[],
117 java_class_name='',
118 type='function'),
119 NativeMethod(return_type='void', static=False,
120 name='SetRecognitionResults',
121 params=[Param(datatype='int', name='sessionId'),
122 Param(datatype='String[]', name='results')],
123 java_class_name='',
124 type='function'),
125 NativeMethod(return_type='long', static=False,
126 name='AddBookmarkFromAPI',
127 params=[Param(datatype='int',
128 name='nativeChromeBrowserProvider'),
129 Param(datatype='String',
130 name='url'),
131 Param(datatype='Long',
132 name='created'),
133 Param(datatype='Boolean',
134 name='isBookmark'),
135 Param(datatype='Long',
136 name='date'),
137 Param(datatype='byte[]',
138 name='favicon'),
139 Param(datatype='String',
140 name='title'),
141 Param(datatype='Integer',
142 name='visits')],
143 java_class_name='',
144 type='method',
145 p0_type='ChromeBrowserProvider'),
146 NativeMethod(return_type='int', static=False,
147 name='FindAll',
148 params=[Param(datatype='String',
149 name='find')],
150 java_class_name='',
151 type='function'),
152 NativeMethod(return_type='BookmarkNode', static=True,
153 name='GetDefaultBookmarkFolder',
154 params=[],
155 java_class_name='',
156 type='function'),
157 NativeMethod(return_type='SQLiteCursor',
158 static=False,
159 name='QueryBookmarkFromAPI',
160 params=[Param(datatype='int',
161 name='nativeChromeBrowserProvider'),
162 Param(datatype='String[]',
163 name='projection'),
164 Param(datatype='String',
165 name='selection'),
166 Param(datatype='String[]',
167 name='selectionArgs'),
168 Param(datatype='String',
169 name='sortOrder'),
170 ],
171 java_class_name='',
172 type='method',
173 p0_type='ChromeBrowserProvider'),
174 NativeMethod(return_type='void', static=False,
175 name='GotOrientation',
176 params=[Param(datatype='int',
177 cpp_class_name='device_orientation::DeviceOri entationAndroid',
178 name='nativePtr'),
179 Param(datatype='double',
180 name='alpha'),
181 Param(datatype='double',
182 name='beta'),
183 Param(datatype='double',
184 name='gamma'),
185 ],
186 java_class_name='',
187 type='method',
188 p0_type='device_orientation::DeviceOrientationAndroid'),
189 ]
190 self.assertListEquals(golden_natives, natives)
191 h = jni_generator.InlHeaderFileGenerator('', 'com/chrome/TestJni',
192 natives, [])
193 golden_content = """\
194 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
195 // Use of this source code is governed by a BSD-style license that can be
196 // found in the LICENSE file.
197
198
199 // This file is autogenerated by base/android/jni_generator/jni_generator_tests. py
200 // For com/chrome/TestJni
201
202 #ifndef com_chrome_TestJni_JNI
203 #define com_chrome_TestJni_JNI
204
205 #include <jni.h>
206
207 #include "base/android/jni_android.h"
208 #include "base/android/scoped_java_ref.h"
209 #include "base/basictypes.h"
210 #include "base/logging.h"
211
212 using base::android::ScopedJavaLocalRef;
213
214 // Step 1: forward declarations.
215 namespace {
216 const char* const kTestJniClassPath = "com/chrome/TestJni";
217 // Leaking this JavaRef as we cannot use LazyInstance from some threads.
218 base::android::ScopedJavaGlobalRef<jclass>&
219 g_TestJni_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>());
220 } // namespace
221
222 static jint Init(JNIEnv* env, jobject obj);
223
224
225 static jstring GetDomainAndRegistry(JNIEnv* env, jclass clazz,
226 jstring url);
227
228
229 static void CreateHistoricalTabFromState(JNIEnv* env, jclass clazz,
230 jbyteArray state,
231 jint tab_index);
232
233
234 static jbyteArray GetStateAsByteArray(JNIEnv* env, jobject obj,
235 jobject view);
236
237
238 static jobjectArray GetAutofillProfileGUIDs(JNIEnv* env, jclass clazz);
239
240
241 static void SetRecognitionResults(JNIEnv* env, jobject obj,
242 jint sessionId,
243 jobjectArray results);
244
245
246 static jint FindAll(JNIEnv* env, jobject obj,
247 jstring find);
248
249
250 static jobject GetDefaultBookmarkFolder(JNIEnv* env, jclass clazz);
251
252
253 // Step 2: method stubs.
254 static void Destroy(JNIEnv* env, jobject obj,
255 jint nativeChromeBrowserProvider) {
256 DCHECK(nativeChromeBrowserProvider) << "Destroy";
257 ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativ eChromeBrowserProvider);
258 return native->Destroy(env, obj);
259 }
260
261 static jlong AddBookmark(JNIEnv* env, jobject obj,
262 jint nativeChromeBrowserProvider,
263 jstring url,
264 jstring title,
265 jboolean isFolder,
266 jlong parentId) {
267 DCHECK(nativeChromeBrowserProvider) << "AddBookmark";
268 ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativ eChromeBrowserProvider);
269 return native->AddBookmark(env, obj, url, title, isFolder, parentId);
270 }
271
272 static jlong AddBookmarkFromAPI(JNIEnv* env, jobject obj,
273 jint nativeChromeBrowserProvider,
274 jstring url,
275 jobject created,
276 jobject isBookmark,
277 jobject date,
278 jbyteArray favicon,
279 jstring title,
280 jobject visits) {
281 DCHECK(nativeChromeBrowserProvider) << "AddBookmarkFromAPI";
282 ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativ eChromeBrowserProvider);
283 return native->AddBookmarkFromAPI(env, obj, url, created, isBookmark, date, fa vicon, title, visits);
284 }
285
286 static jobject QueryBookmarkFromAPI(JNIEnv* env, jobject obj,
287 jint nativeChromeBrowserProvider,
288 jobjectArray projection,
289 jstring selection,
290 jobjectArray selectionArgs,
291 jstring sortOrder) {
292 DCHECK(nativeChromeBrowserProvider) << "QueryBookmarkFromAPI";
293 ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativ eChromeBrowserProvider);
294 return native->QueryBookmarkFromAPI(env, obj, projection, selection, selection Args, sortOrder).Release();
295 }
296
297 static void GotOrientation(JNIEnv* env, jobject obj,
298 jint nativePtr,
299 jdouble alpha,
300 jdouble beta,
301 jdouble gamma) {
302 DCHECK(nativePtr) << "GotOrientation";
303 device_orientation::DeviceOrientationAndroid* native = reinterpret_cast<device _orientation::DeviceOrientationAndroid*>(nativePtr);
304 return native->GotOrientation(env, obj, alpha, beta, gamma);
305 }
306
307
308 // Step 3: GetMethodIDs and RegisterNatives.
309
310
311 static void GetMethodIDsImpl(JNIEnv* env) {
312 g_TestJni_clazz.Reset(base::android::GetClass(env, kTestJniClassPath));
313 }
314
315 static bool RegisterNativesImpl(JNIEnv* env) {
316 GetMethodIDsImpl(env);
317
318 static const JNINativeMethod kMethodsTestJni[] = {
319 { "nativeInit", "()I", reinterpret_cast<void*>(Init) },
320 { "nativeDestroy", "(I)V", reinterpret_cast<void*>(Destroy) },
321 { "nativeAddBookmark", "(ILjava/lang/String;Ljava/lang/String;ZJ)J", reinter pret_cast<void*>(AddBookmark) },
322 { "nativeGetDomainAndRegistry", "(Ljava/lang/String;)Ljava/lang/String;", re interpret_cast<void*>(GetDomainAndRegistry) },
323 { "nativeCreateHistoricalTabFromState", "([BI)V", reinterpret_cast<void*>(Cr eateHistoricalTabFromState) },
324 { "nativeGetStateAsByteArray", "(Lorg/chromium/chromeview/ChromeView;)[B", r einterpret_cast<void*>(GetStateAsByteArray) },
325 { "nativeGetAutofillProfileGUIDs", "()[Ljava/lang/String;", reinterpret_cast <void*>(GetAutofillProfileGUIDs) },
326 { "nativeSetRecognitionResults", "(I[Ljava/lang/String;)V", reinterpret_cast <void*>(SetRecognitionResults) },
327 { "nativeAddBookmarkFromAPI", "(ILjava/lang/String;Ljava/lang/Long;Ljava/lan g/Boolean;Ljava/lang/Long;[BLjava/lang/String;Ljava/lang/Integer;)J", reinterpre t_cast<void*>(AddBookmarkFromAPI) },
328 { "nativeFindAll", "(Ljava/lang/String;)I", reinterpret_cast<void*>(FindAll) },
329 { "nativeGetDefaultBookmarkFolder", "()Lcom/android/chrome/ChromeBrowserProv ider$BookmarkNode;", reinterpret_cast<void*>(GetDefaultBookmarkFolder) },
330 { "nativeQueryBookmarkFromAPI", "(I[Ljava/lang/String;Ljava/lang/String;[Lja va/lang/String;Ljava/lang/String;)Lcom/android/chrome/database/SQLiteCursor;", r einterpret_cast<void*>(QueryBookmarkFromAPI) },
331 { "nativeGotOrientation", "(IDDD)V", reinterpret_cast<void*>(GotOrientation) },
332 };
333 const int kMethodsTestJniSize = arraysize(kMethodsTestJni);
334
335 if (env->RegisterNatives(g_TestJni_clazz.obj(),
336 kMethodsTestJni,
337 kMethodsTestJniSize) < 0) {
338 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
339 return false;
340 }
341
342 return true;
343 }
344
345 #endif // com_chrome_TestJni_JNI
346 """
347 self.assertTextEquals(golden_content, h.GetContent())
348
349 def testInnerClassNatives(self):
350 test_data = """
351 class MyInnerClass {
352 @NativeCall("MyInnerClass")
353 private native int nativeInit();
354 }
355 """
356 natives = jni_generator.ExtractNatives(test_data)
357 golden_natives = [
358 NativeMethod(return_type='int', static=False,
359 name='Init', params=[],
360 java_class_name='MyInnerClass',
361 type='function')
362 ]
363 self.assertListEquals(golden_natives, natives)
364 h = jni_generator.InlHeaderFileGenerator('', 'com/chrome/TestJni',
365 natives, [])
366 golden_content = """\
367 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
368 // Use of this source code is governed by a BSD-style license that can be
369 // found in the LICENSE file.
370
371
372 // This file is autogenerated by base/android/jni_generator/jni_generator_tests. py
373 // For com/chrome/TestJni
374
375 #ifndef com_chrome_TestJni_JNI
376 #define com_chrome_TestJni_JNI
377
378 #include <jni.h>
379
380 #include "base/android/jni_android.h"
381 #include "base/android/scoped_java_ref.h"
382 #include "base/basictypes.h"
383 #include "base/logging.h"
384
385 using base::android::ScopedJavaLocalRef;
386
387 // Step 1: forward declarations.
388 namespace {
389 const char* const kTestJniClassPath = "com/chrome/TestJni";
390 const char* const kMyInnerClassClassPath = "com/chrome/TestJni$MyInnerClass";
391 // Leaking this JavaRef as we cannot use LazyInstance from some threads.
392 base::android::ScopedJavaGlobalRef<jclass>&
393 g_TestJni_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>());
394 } // namespace
395
396 static jint Init(JNIEnv* env, jobject obj);
397
398
399 // Step 2: method stubs.
400
401
402 // Step 3: GetMethodIDs and RegisterNatives.
403
404
405 static void GetMethodIDsImpl(JNIEnv* env) {
406 g_TestJni_clazz.Reset(base::android::GetClass(env, kTestJniClassPath));
407 }
408
409 static bool RegisterNativesImpl(JNIEnv* env) {
410 GetMethodIDsImpl(env);
411
412 static const JNINativeMethod kMethodsMyInnerClass[] = {
413 { "nativeInit", "()I", reinterpret_cast<void*>(Init) },
414 };
415 const int kMethodsMyInnerClassSize = arraysize(kMethodsMyInnerClass);
416
417 if (env->RegisterNatives(g_MyInnerClass_clazz.obj(),
418 kMethodsMyInnerClass,
419 kMethodsMyInnerClassSize) < 0) {
420 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
421 return false;
422 }
423
424 return true;
425 }
426
427 #endif // com_chrome_TestJni_JNI
428 """
429 self.assertTextEquals(golden_content, h.GetContent())
430
431 def testInnerClassNativesMultiple(self):
432 test_data = """
433 class MyInnerClass {
434 @NativeCall("MyInnerClass")
435 private native int nativeInit();
436 }
437 class MyOtherInnerClass {
438 @NativeCall("MyOtherInnerClass")
439 private native int nativeInit();
440 }
441 """
442 natives = jni_generator.ExtractNatives(test_data)
443 golden_natives = [
444 NativeMethod(return_type='int', static=False,
445 name='Init', params=[],
446 java_class_name='MyInnerClass',
447 type='function'),
448 NativeMethod(return_type='int', static=False,
449 name='Init', params=[],
450 java_class_name='MyOtherInnerClass',
451 type='function')
452 ]
453 self.assertListEquals(golden_natives, natives)
454 h = jni_generator.InlHeaderFileGenerator('', 'com/chrome/TestJni',
455 natives, [])
456 golden_content = """\
457 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
458 // Use of this source code is governed by a BSD-style license that can be
459 // found in the LICENSE file.
460
461
462 // This file is autogenerated by base/android/jni_generator/jni_generator_tests. py
463 // For com/chrome/TestJni
464
465 #ifndef com_chrome_TestJni_JNI
466 #define com_chrome_TestJni_JNI
467
468 #include <jni.h>
469
470 #include "base/android/jni_android.h"
471 #include "base/android/scoped_java_ref.h"
472 #include "base/basictypes.h"
473 #include "base/logging.h"
474
475 using base::android::ScopedJavaLocalRef;
476
477 // Step 1: forward declarations.
478 namespace {
479 const char* const kMyOtherInnerClassClassPath = "com/chrome/TestJni$MyOtherInner Class";
480 const char* const kTestJniClassPath = "com/chrome/TestJni";
481 const char* const kMyInnerClassClassPath = "com/chrome/TestJni$MyInnerClass";
482 // Leaking this JavaRef as we cannot use LazyInstance from some threads.
483 base::android::ScopedJavaGlobalRef<jclass>&
484 g_TestJni_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>());
485 } // namespace
486
487 static jint Init(JNIEnv* env, jobject obj);
488
489
490 static jint Init(JNIEnv* env, jobject obj);
491
492
493 // Step 2: method stubs.
494
495
496 // Step 3: GetMethodIDs and RegisterNatives.
497
498
499 static void GetMethodIDsImpl(JNIEnv* env) {
500 g_TestJni_clazz.Reset(base::android::GetClass(env, kTestJniClassPath));
501 }
502
503 static bool RegisterNativesImpl(JNIEnv* env) {
504 GetMethodIDsImpl(env);
505
506 static const JNINativeMethod kMethodsMyOtherInnerClass[] = {
507 { "nativeInit", "()I", reinterpret_cast<void*>(Init) },
508 };
509 const int kMethodsMyOtherInnerClassSize = arraysize(kMethodsMyOtherInnerClass) ;
510
511 if (env->RegisterNatives(g_MyOtherInnerClass_clazz.obj(),
512 kMethodsMyOtherInnerClass,
513 kMethodsMyOtherInnerClassSize) < 0) {
514 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
515 return false;
516 }
517
518 static const JNINativeMethod kMethodsMyInnerClass[] = {
519 { "nativeInit", "()I", reinterpret_cast<void*>(Init) },
520 };
521 const int kMethodsMyInnerClassSize = arraysize(kMethodsMyInnerClass);
522
523 if (env->RegisterNatives(g_MyInnerClass_clazz.obj(),
524 kMethodsMyInnerClass,
525 kMethodsMyInnerClassSize) < 0) {
526 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
527 return false;
528 }
529
530 return true;
531 }
532
533 #endif // com_chrome_TestJni_JNI
534 """
535 self.assertTextEquals(golden_content, h.GetContent())
536
537 def testInnerClassNativesBothInnerAndOuter(self):
538 test_data = """
539 class MyOuterClass {
540 private native int nativeInit();
541 class MyOtherInnerClass {
542 @NativeCall("MyOtherInnerClass")
543 private native int nativeInit();
544 }
545 }
546 """
547 natives = jni_generator.ExtractNatives(test_data)
548 golden_natives = [
549 NativeMethod(return_type='int', static=False,
550 name='Init', params=[],
551 java_class_name='',
552 type='function'),
553 NativeMethod(return_type='int', static=False,
554 name='Init', params=[],
555 java_class_name='MyOtherInnerClass',
556 type='function')
557 ]
558 self.assertListEquals(golden_natives, natives)
559 h = jni_generator.InlHeaderFileGenerator('', 'com/chrome/TestJni',
560 natives, [])
561 golden_content = """\
562 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
563 // Use of this source code is governed by a BSD-style license that can be
564 // found in the LICENSE file.
565
566
567 // This file is autogenerated by base/android/jni_generator/jni_generator_tests. py
568 // For com/chrome/TestJni
569
570 #ifndef com_chrome_TestJni_JNI
571 #define com_chrome_TestJni_JNI
572
573 #include <jni.h>
574
575 #include "base/android/jni_android.h"
576 #include "base/android/scoped_java_ref.h"
577 #include "base/basictypes.h"
578 #include "base/logging.h"
579
580 using base::android::ScopedJavaLocalRef;
581
582 // Step 1: forward declarations.
583 namespace {
584 const char* const kMyOtherInnerClassClassPath = "com/chrome/TestJni$MyOtherInner Class";
585 const char* const kTestJniClassPath = "com/chrome/TestJni";
586 // Leaking this JavaRef as we cannot use LazyInstance from some threads.
587 base::android::ScopedJavaGlobalRef<jclass>&
588 g_TestJni_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>());
589 } // namespace
590
591 static jint Init(JNIEnv* env, jobject obj);
592
593
594 static jint Init(JNIEnv* env, jobject obj);
595
596
597 // Step 2: method stubs.
598
599
600 // Step 3: GetMethodIDs and RegisterNatives.
601
602
603 static void GetMethodIDsImpl(JNIEnv* env) {
604 g_TestJni_clazz.Reset(base::android::GetClass(env, kTestJniClassPath));
605 }
606
607 static bool RegisterNativesImpl(JNIEnv* env) {
608 GetMethodIDsImpl(env);
609
610 static const JNINativeMethod kMethodsMyOtherInnerClass[] = {
611 { "nativeInit", "()I", reinterpret_cast<void*>(Init) },
612 };
613 const int kMethodsMyOtherInnerClassSize = arraysize(kMethodsMyOtherInnerClass) ;
614
615 if (env->RegisterNatives(g_MyOtherInnerClass_clazz.obj(),
616 kMethodsMyOtherInnerClass,
617 kMethodsMyOtherInnerClassSize) < 0) {
618 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
619 return false;
620 }
621
622 static const JNINativeMethod kMethodsTestJni[] = {
623 { "nativeInit", "()I", reinterpret_cast<void*>(Init) },
624 };
625 const int kMethodsTestJniSize = arraysize(kMethodsTestJni);
626
627 if (env->RegisterNatives(g_TestJni_clazz.obj(),
628 kMethodsTestJni,
629 kMethodsTestJniSize) < 0) {
630 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
631 return false;
632 }
633
634 return true;
635 }
636
637 #endif // com_chrome_TestJni_JNI
638 """
639 self.assertTextEquals(golden_content, h.GetContent())
640
641 def testCalledByNatives(self):
642 test_data = """"
643 @CalledByNative
644 NativeInfoBar showConfirmInfoBar(int nativeInfoBar, String buttonOk,
645 String buttonCancel, String title, Bitmap i con) {
646 InfoBar infobar = new ConfirmInfoBar(nativeInfoBar, mContext,
647 buttonOk, buttonCancel,
648 title, icon);
649 return infobar;
650 }
651 @CalledByNative
652 NativeInfoBar showAutoLoginInfoBar(int nativeInfoBar,
653 String realm, String account, String args ) {
654 AutoLoginInfoBar infobar = new AutoLoginInfoBar(nativeInfoBar, mContext,
655 realm, account, args);
656 if (infobar.displayedAccountCount() == 0)
657 infobar = null;
658 return infobar;
659 }
660 @CalledByNative("InfoBar")
661 void dismiss();
662 @SuppressWarnings("unused")
663 @CalledByNative
664 private static boolean shouldShowAutoLogin(ChromeView chromeView,
665 String realm, String account, String args) {
666 AccountManagerContainer accountManagerContainer =
667 new AccountManagerContainer((Activity)chromeView.getContext(), realm , account, args);
668 String[] logins = accountManagerContainer.getAccountLogins(null);
669 return logins.length != 0;
670 }
671 @CalledByNative
672 static InputStream openUrl(String url) {
673 return null;
674 }
675 @CalledByNative
676 private void activateHardwareAcceleration(final boolean activated, final int iPid, final int iType,
677 final int iPrimaryID, final int iSecondaryID) {
678 if (!activated) {
679 return
680 }
681 }
682 @CalledByNativeUnchecked
683 private void uncheckedCall(int iParam);
684 """
685 called_by_natives = jni_generator.ExtractCalledByNatives(test_data)
686 golden_called_by_natives = [
687 CalledByNative(
688 return_type='NativeInfoBar',
689 system_class=False,
690 static=False,
691 name='showConfirmInfoBar',
692 method_id_var_name='showConfirmInfoBar',
693 java_class_name='',
694 params=[Param(datatype='int', name='nativeInfoBar'),
695 Param(datatype='String', name='buttonOk'),
696 Param(datatype='String', name='buttonCancel'),
697 Param(datatype='String', name='title'),
698 Param(datatype='Bitmap', name='icon')],
699 env_call=('Object', ''),
700 unchecked=False,
701 ),
702 CalledByNative(
703 return_type='NativeInfoBar',
704 system_class=False,
705 static=False,
706 name='showAutoLoginInfoBar',
707 method_id_var_name='showAutoLoginInfoBar',
708 java_class_name='',
709 params=[Param(datatype='int', name='nativeInfoBar'),
710 Param(datatype='String', name='realm'),
711 Param(datatype='String', name='account'),
712 Param(datatype='String', name='args')],
713 env_call=('Object', ''),
714 unchecked=False,
715 ),
716 CalledByNative(
717 return_type='void',
718 system_class=False,
719 static=False,
720 name='dismiss',
721 method_id_var_name='dismiss',
722 java_class_name='InfoBar',
723 params=[],
724 env_call=('Void', ''),
725 unchecked=False,
726 ),
727 CalledByNative(
728 return_type='boolean',
729 system_class=False,
730 static=True,
731 name='shouldShowAutoLogin',
732 method_id_var_name='shouldShowAutoLogin',
733 java_class_name='',
734 params=[Param(datatype='ChromeView', name='chromeView'),
735 Param(datatype='String', name='realm'),
736 Param(datatype='String', name='account'),
737 Param(datatype='String', name='args')],
738 env_call=('Boolean', ''),
739 unchecked=False,
740 ),
741 CalledByNative(
742 return_type='InputStream',
743 system_class=False,
744 static=True,
745 name='openUrl',
746 method_id_var_name='openUrl',
747 java_class_name='',
748 params=[Param(datatype='String', name='url')],
749 env_call=('Object', ''),
750 unchecked=False,
751 ),
752 CalledByNative(
753 return_type='void',
754 system_class=False,
755 static=False,
756 name='activateHardwareAcceleration',
757 method_id_var_name='activateHardwareAcceleration',
758 java_class_name='',
759 params=[Param(datatype='boolean', name='activated'),
760 Param(datatype='int', name='iPid'),
761 Param(datatype='int', name='iType'),
762 Param(datatype='int', name='iPrimaryID'),
763 Param(datatype='int', name='iSecondaryID'),
764 ],
765 env_call=('Void', ''),
766 unchecked=False,
767 ),
768 CalledByNative(
769 return_type='void',
770 system_class=False,
771 static=False,
772 name='uncheckedCall',
773 method_id_var_name='uncheckedCall',
774 java_class_name='',
775 params=[Param(datatype='int', name='iParam')],
776 env_call=('Void', ''),
777 unchecked=True,
778 ),
779 ]
780 self.assertListEquals(golden_called_by_natives, called_by_natives)
781 h = jni_generator.InlHeaderFileGenerator('', 'com/chrome/TestJni',
782 [], called_by_natives)
783 golden_content = """\
784 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
785 // Use of this source code is governed by a BSD-style license that can be
786 // found in the LICENSE file.
787
788
789 // This file is autogenerated by base/android/jni_generator/jni_generator_tests. py
790 // For com/chrome/TestJni
791
792 #ifndef com_chrome_TestJni_JNI
793 #define com_chrome_TestJni_JNI
794
795 #include <jni.h>
796
797 #include "base/android/jni_android.h"
798 #include "base/android/scoped_java_ref.h"
799 #include "base/basictypes.h"
800 #include "base/logging.h"
801
802 using base::android::ScopedJavaLocalRef;
803
804 // Step 1: forward declarations.
805 namespace {
806 const char* const kTestJniClassPath = "com/chrome/TestJni";
807 const char* const kInfoBarClassPath = "com/chrome/TestJni$InfoBar";
808 // Leaking this JavaRef as we cannot use LazyInstance from some threads.
809 base::android::ScopedJavaGlobalRef<jclass>&
810 g_TestJni_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>());
811 // Leaking this JavaRef as we cannot use LazyInstance from some threads.
812 base::android::ScopedJavaGlobalRef<jclass>&
813 g_InfoBar_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>());
814 } // namespace
815
816
817 // Step 2: method stubs.
818
819 static jmethodID g_TestJni_showConfirmInfoBar = 0;
820 static ScopedJavaLocalRef<jobject> Java_TestJni_showConfirmInfoBar(JNIEnv* env, jobject obj, jint nativeInfoBar,
821 jstring buttonOk,
822 jstring buttonCancel,
823 jstring title,
824 jobject icon) {
825 DCHECK(!g_TestJni_clazz.is_null()); /* Must call RegisterNativesImpl() */
826 DCHECK(g_TestJni_showConfirmInfoBar);
827 jobject ret =
828 env->CallObjectMethod(obj,
829 g_TestJni_showConfirmInfoBar, nativeInfoBar, buttonOk, buttonCancel, title , icon);
830 base::android::CheckException(env);
831 return ScopedJavaLocalRef<jobject>(env, ret);
832 }
833
834 static jmethodID g_TestJni_showAutoLoginInfoBar = 0;
835 static ScopedJavaLocalRef<jobject> Java_TestJni_showAutoLoginInfoBar(JNIEnv* env , jobject obj, jint nativeInfoBar,
836 jstring realm,
837 jstring account,
838 jstring args) {
839 DCHECK(!g_TestJni_clazz.is_null()); /* Must call RegisterNativesImpl() */
840 DCHECK(g_TestJni_showAutoLoginInfoBar);
841 jobject ret =
842 env->CallObjectMethod(obj,
843 g_TestJni_showAutoLoginInfoBar, nativeInfoBar, realm, account, args);
844 base::android::CheckException(env);
845 return ScopedJavaLocalRef<jobject>(env, ret);
846 }
847
848 static jmethodID g_InfoBar_dismiss = 0;
849 static void Java_InfoBar_dismiss(JNIEnv* env, jobject obj) {
850 DCHECK(!g_InfoBar_clazz.is_null()); /* Must call RegisterNativesImpl() */
851 DCHECK(g_InfoBar_dismiss);
852
853 env->CallVoidMethod(obj,
854 g_InfoBar_dismiss);
855 base::android::CheckException(env);
856
857 }
858
859 static jmethodID g_TestJni_shouldShowAutoLogin = 0;
860 static jboolean Java_TestJni_shouldShowAutoLogin(JNIEnv* env, jobject chromeView ,
861 jstring realm,
862 jstring account,
863 jstring args) {
864 DCHECK(!g_TestJni_clazz.is_null()); /* Must call RegisterNativesImpl() */
865 DCHECK(g_TestJni_shouldShowAutoLogin);
866 jboolean ret =
867 env->CallStaticBooleanMethod(g_TestJni_clazz.obj(),
868 g_TestJni_shouldShowAutoLogin, chromeView, realm, account, args);
869 base::android::CheckException(env);
870 return ret;
871 }
872
873 static jmethodID g_TestJni_openUrl = 0;
874 static ScopedJavaLocalRef<jobject> Java_TestJni_openUrl(JNIEnv* env, jstring url ) {
875 DCHECK(!g_TestJni_clazz.is_null()); /* Must call RegisterNativesImpl() */
876 DCHECK(g_TestJni_openUrl);
877 jobject ret =
878 env->CallStaticObjectMethod(g_TestJni_clazz.obj(),
879 g_TestJni_openUrl, url);
880 base::android::CheckException(env);
881 return ScopedJavaLocalRef<jobject>(env, ret);
882 }
883
884 static jmethodID g_TestJni_activateHardwareAcceleration = 0;
885 static void Java_TestJni_activateHardwareAcceleration(JNIEnv* env, jobject obj, jboolean activated,
886 jint iPid,
887 jint iType,
888 jint iPrimaryID,
889 jint iSecondaryID) {
890 DCHECK(!g_TestJni_clazz.is_null()); /* Must call RegisterNativesImpl() */
891 DCHECK(g_TestJni_activateHardwareAcceleration);
892
893 env->CallVoidMethod(obj,
894 g_TestJni_activateHardwareAcceleration, activated, iPid, iType, iPrimaryID , iSecondaryID);
895 base::android::CheckException(env);
896
897 }
898
899 static jmethodID g_TestJni_uncheckedCall = 0;
900 static void Java_TestJni_uncheckedCall(JNIEnv* env, jobject obj, jint iParam) {
901 DCHECK(!g_TestJni_clazz.is_null()); /* Must call RegisterNativesImpl() */
902 DCHECK(g_TestJni_uncheckedCall);
903
904 env->CallVoidMethod(obj,
905 g_TestJni_uncheckedCall, iParam);
906
907
908 }
909
910 // Step 3: GetMethodIDs and RegisterNatives.
911
912
913 static void GetMethodIDsImpl(JNIEnv* env) {
914 g_TestJni_clazz.Reset(base::android::GetClass(env, kTestJniClassPath));
915 g_InfoBar_clazz.Reset(base::android::GetClass(env, kInfoBarClassPath));
916 g_TestJni_showConfirmInfoBar = base::android::GetMethodID(
917 env, g_TestJni_clazz,
918 "showConfirmInfoBar",
919 "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/graphics/B itmap;)Lcom/android/chrome/infobar/InfoBarContainer$NativeInfoBar;");
920
921 g_TestJni_showAutoLoginInfoBar = base::android::GetMethodID(
922 env, g_TestJni_clazz,
923 "showAutoLoginInfoBar",
924 "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lcom/android/chrom e/infobar/InfoBarContainer$NativeInfoBar;");
925
926 g_InfoBar_dismiss = base::android::GetMethodID(
927 env, g_InfoBar_clazz,
928 "dismiss",
929 "()V");
930
931 g_TestJni_shouldShowAutoLogin = base::android::GetStaticMethodID(
932 env, g_TestJni_clazz,
933 "shouldShowAutoLogin",
934 "(Lorg/chromium/chromeview/ChromeView;Ljava/lang/String;Ljava/lang/String;Lj ava/lang/String;)Z");
935
936 g_TestJni_openUrl = base::android::GetStaticMethodID(
937 env, g_TestJni_clazz,
938 "openUrl",
939 "(Ljava/lang/String;)Ljava/io/InputStream;");
940
941 g_TestJni_activateHardwareAcceleration = base::android::GetMethodID(
942 env, g_TestJni_clazz,
943 "activateHardwareAcceleration",
944 "(ZIIII)V");
945
946 g_TestJni_uncheckedCall = base::android::GetMethodID(
947 env, g_TestJni_clazz,
948 "uncheckedCall",
949 "(I)V");
950
951 }
952
953 static bool RegisterNativesImpl(JNIEnv* env) {
954 GetMethodIDsImpl(env);
955
956 return true;
957 }
958
959 #endif // com_chrome_TestJni_JNI
960 """
961 self.assertTextEquals(golden_content, h.GetContent())
962
963 def testCalledByNativeParseError(self):
964 try:
965 jni_generator.ExtractCalledByNatives("""
966 @CalledByNative
967 public static int foo(); // This one is fine
968
969 @CalledByNative
970 scooby doo
971 """)
972 self.fail('Expected a ParseError')
973 except jni_generator.ParseError, e:
974 self.assertEquals(('@CalledByNative', 'scooby doo'), e.context_lines)
975
976 def testFullyQualifiedClassName(self):
977 contents = """
978 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
979 // Use of this source code is governed by a BSD-style license that can be
980 // found in the LICENSE file.
981
982 package org.chromium.chromeview;
983
984 import org.chromium.chromeview.legacy.DownloadListener;
985 """
986 self.assertEquals('org/chromium/chromeview/Foo',
987 jni_generator.ExtractFullyQualifiedJavaClassName(
988 'org/chromium/chromeview/Foo.java', contents))
989 self.assertEquals('org/chromium/chromeview/Foo',
990 jni_generator.ExtractFullyQualifiedJavaClassName(
991 'frameworks/Foo.java', contents))
992 self.assertRaises(SyntaxError,
993 jni_generator.ExtractFullyQualifiedJavaClassName,
994 'com/foo/Bar', 'no PACKAGE line')
995
996 def testMethodNameMangling(self):
997 self.assertEquals('close_pqV',
998 jni_generator.GetMangledMethodName('close', '()V'))
999 self.assertEquals('read_paBIIqI',
1000 jni_generator.GetMangledMethodName('read', '([BII)I'))
1001 self.assertEquals('open_pLjava_lang_StringxqLjava_io_InputStreamx',
1002 jni_generator.GetMangledMethodName(
1003 'open',
1004 '(Ljava/lang/String;)Ljava/io/InputStream;'))
1005
1006 def testFromJavaP(self):
1007 contents = """
1008 public abstract class java.io.InputStream extends java.lang.Object implements ja va.io.Closeable{
1009 public java.io.InputStream();
1010 public int available() throws java.io.IOException;
1011 public void close() throws java.io.IOException;
1012 public void mark(int);
1013 public boolean markSupported();
1014 public abstract int read() throws java.io.IOException;
1015 public int read(byte[]) throws java.io.IOException;
1016 public int read(byte[], int, int) throws java.io.IOException;
1017 public synchronized void reset() throws java.io.IOException;
1018 public long skip(long) throws java.io.IOException;
1019 }
1020 """
1021 jni_from_javap = jni_generator.JNIFromJavaP(contents.split('\n'), None)
1022 self.assertEquals(9, len(jni_from_javap.called_by_natives))
1023 golden_content = """\
1024 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
1025 // Use of this source code is governed by a BSD-style license that can be
1026 // found in the LICENSE file.
1027
1028
1029 // This file is autogenerated by base/android/jni_generator/jni_generator_tests. py
1030 // For java/io/InputStream
1031
1032 #ifndef java_io_InputStream_JNI
1033 #define java_io_InputStream_JNI
1034
1035 #include <jni.h>
1036
1037 #include "base/android/jni_android.h"
1038 #include "base/android/scoped_java_ref.h"
1039 #include "base/basictypes.h"
1040 #include "base/logging.h"
1041
1042 using base::android::ScopedJavaLocalRef;
1043
1044 // Step 1: forward declarations.
1045 namespace {
1046 const char* const kInputStreamClassPath = "java/io/InputStream";
1047 // Leaking this JavaRef as we cannot use LazyInstance from some threads.
1048 base::android::ScopedJavaGlobalRef<jclass>&
1049 g_InputStream_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>());
1050 } // namespace
1051
1052
1053 // Step 2: method stubs.
1054
1055 static jmethodID g_InputStream_available = 0;
1056 static jint Java_InputStream_available(JNIEnv* env, jobject obj) __attribute__ ( (unused));
1057 static jint Java_InputStream_available(JNIEnv* env, jobject obj) {
1058 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() * /
1059 DCHECK(g_InputStream_available);
1060 jint ret =
1061 env->CallIntMethod(obj,
1062 g_InputStream_available);
1063 base::android::CheckException(env);
1064 return ret;
1065 }
1066
1067 static jmethodID g_InputStream_close = 0;
1068 static void Java_InputStream_close(JNIEnv* env, jobject obj) __attribute__ ((unu sed));
1069 static void Java_InputStream_close(JNIEnv* env, jobject obj) {
1070 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() * /
1071 DCHECK(g_InputStream_close);
1072
1073 env->CallVoidMethod(obj,
1074 g_InputStream_close);
1075 base::android::CheckException(env);
1076
1077 }
1078
1079 static jmethodID g_InputStream_mark = 0;
1080 static void Java_InputStream_mark(JNIEnv* env, jobject obj, jint p0) __attribute __ ((unused));
1081 static void Java_InputStream_mark(JNIEnv* env, jobject obj, jint p0) {
1082 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() * /
1083 DCHECK(g_InputStream_mark);
1084
1085 env->CallVoidMethod(obj,
1086 g_InputStream_mark, p0);
1087 base::android::CheckException(env);
1088
1089 }
1090
1091 static jmethodID g_InputStream_markSupported = 0;
1092 static jboolean Java_InputStream_markSupported(JNIEnv* env, jobject obj) __attri bute__ ((unused));
1093 static jboolean Java_InputStream_markSupported(JNIEnv* env, jobject obj) {
1094 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() * /
1095 DCHECK(g_InputStream_markSupported);
1096 jboolean ret =
1097 env->CallBooleanMethod(obj,
1098 g_InputStream_markSupported);
1099 base::android::CheckException(env);
1100 return ret;
1101 }
1102
1103 static jmethodID g_InputStream_read_pqI = 0;
1104 static jint Java_InputStream_read(JNIEnv* env, jobject obj) __attribute__ ((unus ed));
1105 static jint Java_InputStream_read(JNIEnv* env, jobject obj) {
1106 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() * /
1107 DCHECK(g_InputStream_read_pqI);
1108 jint ret =
1109 env->CallIntMethod(obj,
1110 g_InputStream_read_pqI);
1111 base::android::CheckException(env);
1112 return ret;
1113 }
1114
1115 static jmethodID g_InputStream_read_paBqI = 0;
1116 static jint Java_InputStream_read(JNIEnv* env, jobject obj, jbyteArray p0) __att ribute__ ((unused));
1117 static jint Java_InputStream_read(JNIEnv* env, jobject obj, jbyteArray p0) {
1118 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() * /
1119 DCHECK(g_InputStream_read_paBqI);
1120 jint ret =
1121 env->CallIntMethod(obj,
1122 g_InputStream_read_paBqI, p0);
1123 base::android::CheckException(env);
1124 return ret;
1125 }
1126
1127 static jmethodID g_InputStream_read_paBIIqI = 0;
1128 static jint Java_InputStream_read(JNIEnv* env, jobject obj, jbyteArray p0,
1129 jint p1,
1130 jint p2) __attribute__ ((unused));
1131 static jint Java_InputStream_read(JNIEnv* env, jobject obj, jbyteArray p0,
1132 jint p1,
1133 jint p2) {
1134 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() * /
1135 DCHECK(g_InputStream_read_paBIIqI);
1136 jint ret =
1137 env->CallIntMethod(obj,
1138 g_InputStream_read_paBIIqI, p0, p1, p2);
1139 base::android::CheckException(env);
1140 return ret;
1141 }
1142
1143 static jmethodID g_InputStream_reset = 0;
1144 static void Java_InputStream_reset(JNIEnv* env, jobject obj) __attribute__ ((unu sed));
1145 static void Java_InputStream_reset(JNIEnv* env, jobject obj) {
1146 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() * /
1147 DCHECK(g_InputStream_reset);
1148
1149 env->CallVoidMethod(obj,
1150 g_InputStream_reset);
1151 base::android::CheckException(env);
1152
1153 }
1154
1155 static jmethodID g_InputStream_skip = 0;
1156 static jlong Java_InputStream_skip(JNIEnv* env, jobject obj, jlong p0) __attribu te__ ((unused));
1157 static jlong Java_InputStream_skip(JNIEnv* env, jobject obj, jlong p0) {
1158 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() * /
1159 DCHECK(g_InputStream_skip);
1160 jlong ret =
1161 env->CallLongMethod(obj,
1162 g_InputStream_skip, p0);
1163 base::android::CheckException(env);
1164 return ret;
1165 }
1166
1167 // Step 3: GetMethodIDs and RegisterNatives.
1168 namespace JNI_InputStream {
1169
1170 static void GetMethodIDsImpl(JNIEnv* env) {
1171 g_InputStream_clazz.Reset(base::android::GetClass(env, kInputStreamClassPath)) ;
1172 g_InputStream_available = base::android::GetMethodID(
1173 env, g_InputStream_clazz,
1174 "available",
1175 "()I");
1176
1177 g_InputStream_close = base::android::GetMethodID(
1178 env, g_InputStream_clazz,
1179 "close",
1180 "()V");
1181
1182 g_InputStream_mark = base::android::GetMethodID(
1183 env, g_InputStream_clazz,
1184 "mark",
1185 "(I)V");
1186
1187 g_InputStream_markSupported = base::android::GetMethodID(
1188 env, g_InputStream_clazz,
1189 "markSupported",
1190 "()Z");
1191
1192 g_InputStream_read_pqI = base::android::GetMethodID(
1193 env, g_InputStream_clazz,
1194 "read",
1195 "()I");
1196
1197 g_InputStream_read_paBqI = base::android::GetMethodID(
1198 env, g_InputStream_clazz,
1199 "read",
1200 "([B)I");
1201
1202 g_InputStream_read_paBIIqI = base::android::GetMethodID(
1203 env, g_InputStream_clazz,
1204 "read",
1205 "([BII)I");
1206
1207 g_InputStream_reset = base::android::GetMethodID(
1208 env, g_InputStream_clazz,
1209 "reset",
1210 "()V");
1211
1212 g_InputStream_skip = base::android::GetMethodID(
1213 env, g_InputStream_clazz,
1214 "skip",
1215 "(J)J");
1216
1217 }
1218
1219 static bool RegisterNativesImpl(JNIEnv* env) {
1220 JNI_InputStream::GetMethodIDsImpl(env);
1221
1222 return true;
1223 }
1224 } // namespace JNI_InputStream
1225
1226 #endif // java_io_InputStream_JNI
1227 """
1228 self.assertTextEquals(golden_content, jni_from_javap.GetContent())
1229
1230 def testREForNatives(self):
1231 # We should not match "native SyncSetupFlow" inside the comment.
1232 test_data = """
1233 /**
1234 * Invoked when the setup process is complete so we can disconnect from the
1235 * native-side SyncSetupFlowHandler.
1236 */
1237 public void destroy() {
1238 Log.v(TAG, "Destroying native SyncSetupFlow");
1239 if (mNativeSyncSetupFlow != 0) {
1240 nativeSyncSetupEnded(mNativeSyncSetupFlow);
1241 mNativeSyncSetupFlow = 0;
1242 }
1243 }
1244 private native void nativeSyncSetupEnded(int nativeAndroidSyncSetupFlowHandl er);
1245 """
1246 jni_from_java = jni_generator.JNIFromJavaSource(test_data, 'foo/bar')
1247
1248 def testRaisesOnUnknownDatatype(self):
1249 test_data = """
1250 class MyInnerClass {
1251 private native int nativeInit(AnUnknownDatatype p0);
1252 }
1253 """
1254 self.assertRaises(SyntaxError,
1255 jni_generator.JNIFromJavaSource,
1256 test_data, 'foo/bar')
1257
1258 def testJniSelfDocumentingExample(self):
1259 script_dir = os.path.dirname(sys.argv[0])
1260 content = file(os.path.join(script_dir, 'SampleForTests.java')).read()
1261 golden_content = file(os.path.join(script_dir,
1262 'golden_sample_for_tests_jni.h')).read()
1263 jni_from_java = jni_generator.JNIFromJavaSource(content,
1264 'com/android/example/jni_gen erator/SampleForTests')
1265 self.assertTextEquals(golden_content, jni_from_java.GetContent())
1266
1267 def testCheckFilenames(self):
1268 self.assertRaises(SystemExit, jni_generator.CheckFilenames,
1269 ['more', 'input', 'than'], ['output'])
1270 self.assertRaises(SystemExit, jni_generator.CheckFilenames,
1271 ['more'], ['output', 'than', 'input'])
1272 self.assertRaises(SystemExit, jni_generator.CheckFilenames,
1273 ['NotTheSame.java'], ['not_good.h'])
1274 self.assertRaises(SystemExit, jni_generator.CheckFilenames,
1275 ['MissingJniSuffix.java'], ['missing_jni_suffix.h'])
1276 jni_generator.CheckFilenames(['ThisIsFine.java'], ['this_is_fine_jni.h'])
1277 jni_generator.CheckFilenames([], [])
1278
1279
1280 if __name__ == '__main__':
1281 unittest.main()
1282
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698