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
4 # found in the LICENSE file.
6 """Tests for jni_generator.py.
8 This test suite contains various tests for the JNI generator.
9 It exercises the low-level parser all the way up to the
10 code generator and ensures the output matches a golden
19 from jni_generator
import CalledByNative
, JniParams
, NativeMethod
, Param
22 SCRIPT_NAME
= 'base/android/jni_generator/jni_generator.py'
25 class TestOptions(object):
26 """The mock options object which is passed to the jni_generator.py script."""
30 self
.script_name
= SCRIPT_NAME
34 class TestGenerator(unittest
.TestCase
):
35 def assertObjEquals(self
, first
, second
):
36 dict_first
= first
.__dict
__
37 dict_second
= second
.__dict
__
38 self
.assertEquals(dict_first
.keys(), dict_second
.keys())
39 for key
, value
in dict_first
.iteritems():
40 if (type(value
) is list and len(value
) and
41 isinstance(type(value
[0]), object)):
42 self
.assertListEquals(value
, second
.__getattribute
__(key
))
44 actual
= second
.__getattribute
__(key
)
45 self
.assertEquals(value
, actual
,
46 'Key ' + key
+ ': ' + str(value
) + '!=' + str(actual
))
48 def assertListEquals(self
, first
, second
):
49 self
.assertEquals(len(first
), len(second
))
50 for i
in xrange(len(first
)):
51 if isinstance(first
[i
], object):
52 self
.assertObjEquals(first
[i
], second
[i
])
54 self
.assertEquals(first
[i
], second
[i
])
56 def assertTextEquals(self
, golden_text
, generated_text
):
57 stripped_golden
= [l
.strip() for l
in golden_text
.split('\n')]
58 stripped_generated
= [l
.strip() for l
in generated_text
.split('\n')]
59 if stripped_golden
!= stripped_generated
:
61 for line
in difflib
.context_diff(stripped_golden
, stripped_generated
):
67 self
.fail('Golden text mismatch')
69 def testNatives(self
):
71 interface OnFrameAvailableListener {}
72 private native int nativeInit();
73 private native void nativeDestroy(int nativeChromeBrowserProvider);
74 private native long nativeAddBookmark(
75 int nativeChromeBrowserProvider,
76 String url, String title, boolean isFolder, long parentId);
77 private static native String nativeGetDomainAndRegistry(String url);
78 private static native void nativeCreateHistoricalTabFromState(
79 byte[] state, int tab_index);
80 private native byte[] nativeGetStateAsByteArray(View view);
81 private static native String[] nativeGetAutofillProfileGUIDs();
82 private native void nativeSetRecognitionResults(
83 int sessionId, String[] results);
84 private native long nativeAddBookmarkFromAPI(
85 int nativeChromeBrowserProvider,
86 String url, Long created, Boolean isBookmark,
87 Long date, byte[] favicon, String title, Integer visits);
88 native int nativeFindAll(String find);
89 private static native OnFrameAvailableListener nativeGetInnerClass();
90 private native Bitmap nativeQueryBitmap(
91 int nativeChromeBrowserProvider,
92 String[] projection, String selection,
93 String[] selectionArgs, String sortOrder);
94 private native void nativeGotOrientation(
95 int nativeDataFetcherImplAndroid,
96 double alpha, double beta, double gamma);
98 jni_generator
.JniParams
.ExtractImportsAndInnerClasses(test_data
)
99 natives
= jni_generator
.ExtractNatives(test_data
, 'int')
101 NativeMethod(return_type
='int', static
=False,
104 java_class_name
=None,
106 NativeMethod(return_type
='void', static
=False, name
='Destroy',
107 params
=[Param(datatype
='int',
108 name
='nativeChromeBrowserProvider')],
109 java_class_name
=None,
111 p0_type
='ChromeBrowserProvider'),
112 NativeMethod(return_type
='long', static
=False, name
='AddBookmark',
113 params
=[Param(datatype
='int',
114 name
='nativeChromeBrowserProvider'),
115 Param(datatype
='String',
117 Param(datatype
='String',
119 Param(datatype
='boolean',
121 Param(datatype
='long',
123 java_class_name
=None,
125 p0_type
='ChromeBrowserProvider'),
126 NativeMethod(return_type
='String', static
=True,
127 name
='GetDomainAndRegistry',
128 params
=[Param(datatype
='String',
130 java_class_name
=None,
132 NativeMethod(return_type
='void', static
=True,
133 name
='CreateHistoricalTabFromState',
134 params
=[Param(datatype
='byte[]',
136 Param(datatype
='int',
138 java_class_name
=None,
140 NativeMethod(return_type
='byte[]', static
=False,
141 name
='GetStateAsByteArray',
142 params
=[Param(datatype
='View', name
='view')],
143 java_class_name
=None,
145 NativeMethod(return_type
='String[]', static
=True,
146 name
='GetAutofillProfileGUIDs', params
=[],
147 java_class_name
=None,
149 NativeMethod(return_type
='void', static
=False,
150 name
='SetRecognitionResults',
151 params
=[Param(datatype
='int', name
='sessionId'),
152 Param(datatype
='String[]', name
='results')],
153 java_class_name
=None,
155 NativeMethod(return_type
='long', static
=False,
156 name
='AddBookmarkFromAPI',
157 params
=[Param(datatype
='int',
158 name
='nativeChromeBrowserProvider'),
159 Param(datatype
='String',
161 Param(datatype
='Long',
163 Param(datatype
='Boolean',
165 Param(datatype
='Long',
167 Param(datatype
='byte[]',
169 Param(datatype
='String',
171 Param(datatype
='Integer',
173 java_class_name
=None,
175 p0_type
='ChromeBrowserProvider'),
176 NativeMethod(return_type
='int', static
=False,
178 params
=[Param(datatype
='String',
180 java_class_name
=None,
182 NativeMethod(return_type
='OnFrameAvailableListener', static
=True,
183 name
='GetInnerClass',
185 java_class_name
=None,
187 NativeMethod(return_type
='Bitmap',
190 params
=[Param(datatype
='int',
191 name
='nativeChromeBrowserProvider'),
192 Param(datatype
='String[]',
194 Param(datatype
='String',
196 Param(datatype
='String[]',
197 name
='selectionArgs'),
198 Param(datatype
='String',
201 java_class_name
=None,
203 p0_type
='ChromeBrowserProvider'),
204 NativeMethod(return_type
='void', static
=False,
205 name
='GotOrientation',
206 params
=[Param(datatype
='int',
207 name
='nativeDataFetcherImplAndroid'),
208 Param(datatype
='double',
210 Param(datatype
='double',
212 Param(datatype
='double',
215 java_class_name
=None,
217 p0_type
='content::DataFetcherImplAndroid'),
219 self
.assertListEquals(golden_natives
, natives
)
220 h
= jni_generator
.InlHeaderFileGenerator('', 'org/chromium/TestJni',
221 natives
, [], TestOptions())
222 golden_content
= """\
223 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
224 // Use of this source code is governed by a BSD-style license that can be
225 // found in the LICENSE file.
227 // This file is autogenerated by
228 // base/android/jni_generator/jni_generator.py
230 // org/chromium/TestJni
232 #ifndef org_chromium_TestJni_JNI
233 #define org_chromium_TestJni_JNI
237 #include "base/android/jni_android.h"
238 #include "base/android/scoped_java_ref.h"
239 #include "base/basictypes.h"
240 #include "base/logging.h"
242 using base::android::ScopedJavaLocalRef;
244 // Step 1: forward declarations.
246 const char kTestJniClassPath[] = "org/chromium/TestJni";
247 // Leaking this jclass as we cannot use LazyInstance from some threads.
248 jclass g_TestJni_clazz = NULL;
251 static jint Init(JNIEnv* env, jobject obj);
253 static jstring GetDomainAndRegistry(JNIEnv* env, jclass clazz,
256 static void CreateHistoricalTabFromState(JNIEnv* env, jclass clazz,
260 static jbyteArray GetStateAsByteArray(JNIEnv* env, jobject obj,
263 static jobjectArray GetAutofillProfileGUIDs(JNIEnv* env, jclass clazz);
265 static void SetRecognitionResults(JNIEnv* env, jobject obj,
267 jobjectArray results);
269 static jint FindAll(JNIEnv* env, jobject obj,
272 static jobject GetInnerClass(JNIEnv* env, jclass clazz);
274 // Step 2: method stubs.
275 static void Destroy(JNIEnv* env, jobject obj,
276 jint nativeChromeBrowserProvider) {
277 DCHECK(nativeChromeBrowserProvider) << "Destroy";
278 ChromeBrowserProvider* native =
279 reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider);
280 return native->Destroy(env, obj);
283 static jlong AddBookmark(JNIEnv* env, jobject obj,
284 jint nativeChromeBrowserProvider,
289 DCHECK(nativeChromeBrowserProvider) << "AddBookmark";
290 ChromeBrowserProvider* native =
291 reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider);
292 return native->AddBookmark(env, obj, url, title, isFolder, parentId);
295 static jlong AddBookmarkFromAPI(JNIEnv* env, jobject obj,
296 jint nativeChromeBrowserProvider,
304 DCHECK(nativeChromeBrowserProvider) << "AddBookmarkFromAPI";
305 ChromeBrowserProvider* native =
306 reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider);
307 return native->AddBookmarkFromAPI(env, obj, url, created, isBookmark, date,
308 favicon, title, visits);
311 static jobject QueryBitmap(JNIEnv* env, jobject obj,
312 jint nativeChromeBrowserProvider,
313 jobjectArray projection,
315 jobjectArray selectionArgs,
317 DCHECK(nativeChromeBrowserProvider) << "QueryBitmap";
318 ChromeBrowserProvider* native =
319 reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider);
320 return native->QueryBitmap(env, obj, projection, selection, selectionArgs,
321 sortOrder).Release();
324 static void GotOrientation(JNIEnv* env, jobject obj,
325 jint nativeDataFetcherImplAndroid,
329 DCHECK(nativeDataFetcherImplAndroid) << "GotOrientation";
330 DataFetcherImplAndroid* native =
331 reinterpret_cast<DataFetcherImplAndroid*>(nativeDataFetcherImplAndroid);
332 return native->GotOrientation(env, obj, alpha, beta, gamma);
335 // Step 3: RegisterNatives.
337 static bool RegisterNativesImpl(JNIEnv* env) {
339 g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
340 base::android::GetClass(env, kTestJniClassPath).obj()));
341 static const JNINativeMethod kMethodsTestJni[] = {
345 "I", reinterpret_cast<void*>(Init) },
350 "V", reinterpret_cast<void*>(Destroy) },
351 { "nativeAddBookmark",
359 "J", reinterpret_cast<void*>(AddBookmark) },
360 { "nativeGetDomainAndRegistry",
364 "Ljava/lang/String;", reinterpret_cast<void*>(GetDomainAndRegistry) },
365 { "nativeCreateHistoricalTabFromState",
370 "V", reinterpret_cast<void*>(CreateHistoricalTabFromState) },
371 { "nativeGetStateAsByteArray",
373 "Landroid/view/View;"
375 "[B", reinterpret_cast<void*>(GetStateAsByteArray) },
376 { "nativeGetAutofillProfileGUIDs",
379 "[Ljava/lang/String;", reinterpret_cast<void*>(GetAutofillProfileGUIDs) },
380 { "nativeSetRecognitionResults",
383 "[Ljava/lang/String;"
385 "V", reinterpret_cast<void*>(SetRecognitionResults) },
386 { "nativeAddBookmarkFromAPI",
391 "Ljava/lang/Boolean;"
395 "Ljava/lang/Integer;"
397 "J", reinterpret_cast<void*>(AddBookmarkFromAPI) },
402 "I", reinterpret_cast<void*>(FindAll) },
403 { "nativeGetInnerClass",
406 "Lorg/chromium/example/jni_generator/SampleForTests$OnFrameAvailableListener;",
407 reinterpret_cast<void*>(GetInnerClass) },
408 { "nativeQueryBitmap",
411 "[Ljava/lang/String;"
413 "[Ljava/lang/String;"
416 "Landroid/graphics/Bitmap;", reinterpret_cast<void*>(QueryBitmap) },
417 { "nativeGotOrientation",
424 "V", reinterpret_cast<void*>(GotOrientation) },
426 const int kMethodsTestJniSize = arraysize(kMethodsTestJni);
428 if (env->RegisterNatives(g_TestJni_clazz,
430 kMethodsTestJniSize) < 0) {
431 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
438 #endif // org_chromium_TestJni_JNI
440 self
.assertTextEquals(golden_content
, h
.GetContent())
442 def testInnerClassNatives(self
):
445 @NativeCall("MyInnerClass")
446 private native int nativeInit();
449 natives
= jni_generator
.ExtractNatives(test_data
, 'int')
451 NativeMethod(return_type
='int', static
=False,
452 name
='Init', params
=[],
453 java_class_name
='MyInnerClass',
456 self
.assertListEquals(golden_natives
, natives
)
457 h
= jni_generator
.InlHeaderFileGenerator('', 'org/chromium/TestJni',
458 natives
, [], TestOptions())
459 golden_content
= """\
460 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
461 // Use of this source code is governed by a BSD-style license that can be
462 // found in the LICENSE file.
464 // This file is autogenerated by
465 // base/android/jni_generator/jni_generator.py
467 // org/chromium/TestJni
469 #ifndef org_chromium_TestJni_JNI
470 #define org_chromium_TestJni_JNI
474 #include "base/android/jni_android.h"
475 #include "base/android/scoped_java_ref.h"
476 #include "base/basictypes.h"
477 #include "base/logging.h"
479 using base::android::ScopedJavaLocalRef;
481 // Step 1: forward declarations.
483 const char kTestJniClassPath[] = "org/chromium/TestJni";
484 const char kMyInnerClassClassPath[] = "org/chromium/TestJni$MyInnerClass";
485 // Leaking this jclass as we cannot use LazyInstance from some threads.
486 jclass g_TestJni_clazz = NULL;
489 static jint Init(JNIEnv* env, jobject obj);
491 // Step 2: method stubs.
493 // Step 3: RegisterNatives.
495 static bool RegisterNativesImpl(JNIEnv* env) {
497 g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
498 base::android::GetClass(env, kTestJniClassPath).obj()));
499 static const JNINativeMethod kMethodsMyInnerClass[] = {
503 "I", reinterpret_cast<void*>(Init) },
505 const int kMethodsMyInnerClassSize = arraysize(kMethodsMyInnerClass);
507 if (env->RegisterNatives(g_MyInnerClass_clazz,
508 kMethodsMyInnerClass,
509 kMethodsMyInnerClassSize) < 0) {
510 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
517 #endif // org_chromium_TestJni_JNI
519 self
.assertTextEquals(golden_content
, h
.GetContent())
521 def testInnerClassNativesMultiple(self
):
524 @NativeCall("MyInnerClass")
525 private native int nativeInit();
527 class MyOtherInnerClass {
528 @NativeCall("MyOtherInnerClass")
529 private native int nativeInit();
532 natives
= jni_generator
.ExtractNatives(test_data
, 'int')
534 NativeMethod(return_type
='int', static
=False,
535 name
='Init', params
=[],
536 java_class_name
='MyInnerClass',
538 NativeMethod(return_type
='int', static
=False,
539 name
='Init', params
=[],
540 java_class_name
='MyOtherInnerClass',
543 self
.assertListEquals(golden_natives
, natives
)
544 h
= jni_generator
.InlHeaderFileGenerator('', 'org/chromium/TestJni',
545 natives
, [], TestOptions())
546 golden_content
= """\
547 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
548 // Use of this source code is governed by a BSD-style license that can be
549 // found in the LICENSE file.
551 // This file is autogenerated by
552 // base/android/jni_generator/jni_generator.py
554 // org/chromium/TestJni
556 #ifndef org_chromium_TestJni_JNI
557 #define org_chromium_TestJni_JNI
561 #include "base/android/jni_android.h"
562 #include "base/android/scoped_java_ref.h"
563 #include "base/basictypes.h"
564 #include "base/logging.h"
566 using base::android::ScopedJavaLocalRef;
568 // Step 1: forward declarations.
570 const char kMyOtherInnerClassClassPath[] =
571 "org/chromium/TestJni$MyOtherInnerClass";
572 const char kTestJniClassPath[] = "org/chromium/TestJni";
573 const char kMyInnerClassClassPath[] = "org/chromium/TestJni$MyInnerClass";
574 // Leaking this jclass as we cannot use LazyInstance from some threads.
575 jclass g_TestJni_clazz = NULL;
578 static jint Init(JNIEnv* env, jobject obj);
580 static jint Init(JNIEnv* env, jobject obj);
582 // Step 2: method stubs.
584 // Step 3: RegisterNatives.
586 static bool RegisterNativesImpl(JNIEnv* env) {
588 g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
589 base::android::GetClass(env, kTestJniClassPath).obj()));
590 static const JNINativeMethod kMethodsMyOtherInnerClass[] = {
594 "I", reinterpret_cast<void*>(Init) },
596 const int kMethodsMyOtherInnerClassSize =
597 arraysize(kMethodsMyOtherInnerClass);
599 if (env->RegisterNatives(g_MyOtherInnerClass_clazz,
600 kMethodsMyOtherInnerClass,
601 kMethodsMyOtherInnerClassSize) < 0) {
602 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
606 static const JNINativeMethod kMethodsMyInnerClass[] = {
610 "I", reinterpret_cast<void*>(Init) },
612 const int kMethodsMyInnerClassSize = arraysize(kMethodsMyInnerClass);
614 if (env->RegisterNatives(g_MyInnerClass_clazz,
615 kMethodsMyInnerClass,
616 kMethodsMyInnerClassSize) < 0) {
617 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
624 #endif // org_chromium_TestJni_JNI
626 self
.assertTextEquals(golden_content
, h
.GetContent())
628 def testInnerClassNativesBothInnerAndOuter(self
):
631 private native int nativeInit();
632 class MyOtherInnerClass {
633 @NativeCall("MyOtherInnerClass")
634 private native int nativeInit();
638 natives
= jni_generator
.ExtractNatives(test_data
, 'int')
640 NativeMethod(return_type
='int', static
=False,
641 name
='Init', params
=[],
642 java_class_name
=None,
644 NativeMethod(return_type
='int', static
=False,
645 name
='Init', params
=[],
646 java_class_name
='MyOtherInnerClass',
649 self
.assertListEquals(golden_natives
, natives
)
650 h
= jni_generator
.InlHeaderFileGenerator('', 'org/chromium/TestJni',
651 natives
, [], TestOptions())
652 golden_content
= """\
653 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
654 // Use of this source code is governed by a BSD-style license that can be
655 // found in the LICENSE file.
657 // This file is autogenerated by
658 // base/android/jni_generator/jni_generator.py
660 // org/chromium/TestJni
662 #ifndef org_chromium_TestJni_JNI
663 #define org_chromium_TestJni_JNI
667 #include "base/android/jni_android.h"
668 #include "base/android/scoped_java_ref.h"
669 #include "base/basictypes.h"
670 #include "base/logging.h"
672 using base::android::ScopedJavaLocalRef;
674 // Step 1: forward declarations.
676 const char kMyOtherInnerClassClassPath[] =
677 "org/chromium/TestJni$MyOtherInnerClass";
678 const char kTestJniClassPath[] = "org/chromium/TestJni";
679 // Leaking this jclass as we cannot use LazyInstance from some threads.
680 jclass g_TestJni_clazz = NULL;
683 static jint Init(JNIEnv* env, jobject obj);
685 static jint Init(JNIEnv* env, jobject obj);
687 // Step 2: method stubs.
689 // Step 3: RegisterNatives.
691 static bool RegisterNativesImpl(JNIEnv* env) {
693 g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
694 base::android::GetClass(env, kTestJniClassPath).obj()));
695 static const JNINativeMethod kMethodsMyOtherInnerClass[] = {
699 "I", reinterpret_cast<void*>(Init) },
701 const int kMethodsMyOtherInnerClassSize =
702 arraysize(kMethodsMyOtherInnerClass);
704 if (env->RegisterNatives(g_MyOtherInnerClass_clazz,
705 kMethodsMyOtherInnerClass,
706 kMethodsMyOtherInnerClassSize) < 0) {
707 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
711 static const JNINativeMethod kMethodsTestJni[] = {
715 "I", reinterpret_cast<void*>(Init) },
717 const int kMethodsTestJniSize = arraysize(kMethodsTestJni);
719 if (env->RegisterNatives(g_TestJni_clazz,
721 kMethodsTestJniSize) < 0) {
722 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
729 #endif // org_chromium_TestJni_JNI
731 self
.assertTextEquals(golden_content
, h
.GetContent())
733 def testCalledByNatives(self
):
735 import android.graphics.Bitmap;
736 import android.view.View;
737 import java.io.InputStream;
738 import java.util.List;
743 InnerClass showConfirmInfoBar(int nativeInfoBar,
744 String buttonOk, String buttonCancel, String title, Bitmap icon) {
745 InfoBar infobar = new ConfirmInfoBar(nativeInfoBar, mContext,
746 buttonOk, buttonCancel,
751 InnerClass showAutoLoginInfoBar(int nativeInfoBar,
752 String realm, String account, String args) {
753 AutoLoginInfoBar infobar = new AutoLoginInfoBar(nativeInfoBar, mContext,
754 realm, account, args);
755 if (infobar.displayedAccountCount() == 0)
759 @CalledByNative("InfoBar")
761 @SuppressWarnings("unused")
763 private static boolean shouldShowAutoLogin(View view,
764 String realm, String account, String args) {
765 AccountManagerContainer accountManagerContainer =
766 new AccountManagerContainer((Activity)contentView.getContext(),
767 realm, account, args);
768 String[] logins = accountManagerContainer.getAccountLogins(null);
769 return logins.length != 0;
772 static InputStream openUrl(String url) {
776 private void activateHardwareAcceleration(final boolean activated,
777 final int iPid, final int iType,
778 final int iPrimaryID, final int iSecondaryID) {
783 @CalledByNativeUnchecked
784 private void uncheckedCall(int iParam);
787 public byte[] returnByteArray();
790 public boolean[] returnBooleanArray();
793 public char[] returnCharArray();
796 public short[] returnShortArray();
799 public int[] returnIntArray();
802 public long[] returnLongArray();
805 public double[] returnDoubleArray();
808 public Object[] returnObjectArray();
811 public byte[][] returnArrayOfByteArray();
814 public Bitmap.CompressFormat getCompressFormat();
817 public List<Bitmap.CompressFormat> getCompressFormatList();
819 jni_generator
.JniParams
.SetFullyQualifiedClass('org/chromium/Foo')
820 jni_generator
.JniParams
.ExtractImportsAndInnerClasses(test_data
)
821 called_by_natives
= jni_generator
.ExtractCalledByNatives(test_data
)
822 golden_called_by_natives
= [
824 return_type
='InnerClass',
827 name
='showConfirmInfoBar',
828 method_id_var_name
='showConfirmInfoBar',
830 params
=[Param(datatype
='int', name
='nativeInfoBar'),
831 Param(datatype
='String', name
='buttonOk'),
832 Param(datatype
='String', name
='buttonCancel'),
833 Param(datatype
='String', name
='title'),
834 Param(datatype
='Bitmap', name
='icon')],
835 env_call
=('Object', ''),
839 return_type
='InnerClass',
842 name
='showAutoLoginInfoBar',
843 method_id_var_name
='showAutoLoginInfoBar',
845 params
=[Param(datatype
='int', name
='nativeInfoBar'),
846 Param(datatype
='String', name
='realm'),
847 Param(datatype
='String', name
='account'),
848 Param(datatype
='String', name
='args')],
849 env_call
=('Object', ''),
857 method_id_var_name
='dismiss',
858 java_class_name
='InfoBar',
860 env_call
=('Void', ''),
864 return_type
='boolean',
867 name
='shouldShowAutoLogin',
868 method_id_var_name
='shouldShowAutoLogin',
870 params
=[Param(datatype
='View', name
='view'),
871 Param(datatype
='String', name
='realm'),
872 Param(datatype
='String', name
='account'),
873 Param(datatype
='String', name
='args')],
874 env_call
=('Boolean', ''),
878 return_type
='InputStream',
882 method_id_var_name
='openUrl',
884 params
=[Param(datatype
='String', name
='url')],
885 env_call
=('Object', ''),
892 name
='activateHardwareAcceleration',
893 method_id_var_name
='activateHardwareAcceleration',
895 params
=[Param(datatype
='boolean', name
='activated'),
896 Param(datatype
='int', name
='iPid'),
897 Param(datatype
='int', name
='iType'),
898 Param(datatype
='int', name
='iPrimaryID'),
899 Param(datatype
='int', name
='iSecondaryID'),
901 env_call
=('Void', ''),
908 name
='uncheckedCall',
909 method_id_var_name
='uncheckedCall',
911 params
=[Param(datatype
='int', name
='iParam')],
912 env_call
=('Void', ''),
916 return_type
='byte[]',
919 name
='returnByteArray',
920 method_id_var_name
='returnByteArray',
923 env_call
=('Void', ''),
927 return_type
='boolean[]',
930 name
='returnBooleanArray',
931 method_id_var_name
='returnBooleanArray',
934 env_call
=('Void', ''),
938 return_type
='char[]',
941 name
='returnCharArray',
942 method_id_var_name
='returnCharArray',
945 env_call
=('Void', ''),
949 return_type
='short[]',
952 name
='returnShortArray',
953 method_id_var_name
='returnShortArray',
956 env_call
=('Void', ''),
963 name
='returnIntArray',
964 method_id_var_name
='returnIntArray',
967 env_call
=('Void', ''),
971 return_type
='long[]',
974 name
='returnLongArray',
975 method_id_var_name
='returnLongArray',
978 env_call
=('Void', ''),
982 return_type
='double[]',
985 name
='returnDoubleArray',
986 method_id_var_name
='returnDoubleArray',
989 env_call
=('Void', ''),
993 return_type
='Object[]',
996 name
='returnObjectArray',
997 method_id_var_name
='returnObjectArray',
1000 env_call
=('Void', ''),
1004 return_type
='byte[][]',
1007 name
='returnArrayOfByteArray',
1008 method_id_var_name
='returnArrayOfByteArray',
1011 env_call
=('Void', ''),
1015 return_type
='Bitmap.CompressFormat',
1018 name
='getCompressFormat',
1019 method_id_var_name
='getCompressFormat',
1022 env_call
=('Void', ''),
1026 return_type
='List<Bitmap.CompressFormat>',
1029 name
='getCompressFormatList',
1030 method_id_var_name
='getCompressFormatList',
1033 env_call
=('Void', ''),
1037 self
.assertListEquals(golden_called_by_natives
, called_by_natives
)
1038 h
= jni_generator
.InlHeaderFileGenerator('', 'org/chromium/TestJni',
1039 [], called_by_natives
,
1041 golden_content
= """\
1042 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
1043 // Use of this source code is governed by a BSD-style license that can be
1044 // found in the LICENSE file.
1046 // This file is autogenerated by
1047 // base/android/jni_generator/jni_generator.py
1049 // org/chromium/TestJni
1051 #ifndef org_chromium_TestJni_JNI
1052 #define org_chromium_TestJni_JNI
1056 #include "base/android/jni_android.h"
1057 #include "base/android/scoped_java_ref.h"
1058 #include "base/basictypes.h"
1059 #include "base/logging.h"
1061 using base::android::ScopedJavaLocalRef;
1063 // Step 1: forward declarations.
1065 const char kTestJniClassPath[] = "org/chromium/TestJni";
1066 const char kInfoBarClassPath[] = "org/chromium/TestJni$InfoBar";
1067 // Leaking this jclass as we cannot use LazyInstance from some threads.
1068 jclass g_TestJni_clazz = NULL;
1069 // Leaking this jclass as we cannot use LazyInstance from some threads.
1070 jclass g_InfoBar_clazz = NULL;
1073 // Step 2: method stubs.
1075 static base::subtle::AtomicWord g_TestJni_showConfirmInfoBar = 0;
1076 static ScopedJavaLocalRef<jobject> Java_TestJni_showConfirmInfoBar(JNIEnv* env,
1077 jobject obj, jint nativeInfoBar,
1079 jstring buttonCancel,
1082 /* Must call RegisterNativesImpl() */
1083 DCHECK(g_TestJni_clazz);
1084 jmethodID method_id =
1085 base::android::MethodID::LazyGet<
1086 base::android::MethodID::TYPE_INSTANCE>(
1087 env, g_TestJni_clazz,
1088 "showConfirmInfoBar",
1092 "Ljava/lang/String;"
1093 "Ljava/lang/String;"
1094 "Ljava/lang/String;"
1095 "Landroid/graphics/Bitmap;"
1097 "Lorg/chromium/Foo$InnerClass;",
1098 &g_TestJni_showConfirmInfoBar);
1101 env->CallObjectMethod(obj,
1102 method_id, nativeInfoBar, buttonOk, buttonCancel, title, icon);
1103 base::android::CheckException(env);
1104 return ScopedJavaLocalRef<jobject>(env, ret);
1107 static base::subtle::AtomicWord g_TestJni_showAutoLoginInfoBar = 0;
1108 static ScopedJavaLocalRef<jobject> Java_TestJni_showAutoLoginInfoBar(JNIEnv*
1109 env, jobject obj, jint nativeInfoBar,
1113 /* Must call RegisterNativesImpl() */
1114 DCHECK(g_TestJni_clazz);
1115 jmethodID method_id =
1116 base::android::MethodID::LazyGet<
1117 base::android::MethodID::TYPE_INSTANCE>(
1118 env, g_TestJni_clazz,
1119 "showAutoLoginInfoBar",
1123 "Ljava/lang/String;"
1124 "Ljava/lang/String;"
1125 "Ljava/lang/String;"
1127 "Lorg/chromium/Foo$InnerClass;",
1128 &g_TestJni_showAutoLoginInfoBar);
1131 env->CallObjectMethod(obj,
1132 method_id, nativeInfoBar, realm, account, args);
1133 base::android::CheckException(env);
1134 return ScopedJavaLocalRef<jobject>(env, ret);
1137 static base::subtle::AtomicWord g_InfoBar_dismiss = 0;
1138 static void Java_InfoBar_dismiss(JNIEnv* env, jobject obj) {
1139 /* Must call RegisterNativesImpl() */
1140 DCHECK(g_InfoBar_clazz);
1141 jmethodID method_id =
1142 base::android::MethodID::LazyGet<
1143 base::android::MethodID::TYPE_INSTANCE>(
1144 env, g_InfoBar_clazz,
1150 &g_InfoBar_dismiss);
1152 env->CallVoidMethod(obj,
1154 base::android::CheckException(env);
1158 static base::subtle::AtomicWord g_TestJni_shouldShowAutoLogin = 0;
1159 static jboolean Java_TestJni_shouldShowAutoLogin(JNIEnv* env, jobject view,
1163 /* Must call RegisterNativesImpl() */
1164 DCHECK(g_TestJni_clazz);
1165 jmethodID method_id =
1166 base::android::MethodID::LazyGet<
1167 base::android::MethodID::TYPE_STATIC>(
1168 env, g_TestJni_clazz,
1169 "shouldShowAutoLogin",
1172 "Landroid/view/View;"
1173 "Ljava/lang/String;"
1174 "Ljava/lang/String;"
1175 "Ljava/lang/String;"
1178 &g_TestJni_shouldShowAutoLogin);
1181 env->CallStaticBooleanMethod(g_TestJni_clazz,
1182 method_id, view, realm, account, args);
1183 base::android::CheckException(env);
1187 static base::subtle::AtomicWord g_TestJni_openUrl = 0;
1188 static ScopedJavaLocalRef<jobject> Java_TestJni_openUrl(JNIEnv* env, jstring
1190 /* Must call RegisterNativesImpl() */
1191 DCHECK(g_TestJni_clazz);
1192 jmethodID method_id =
1193 base::android::MethodID::LazyGet<
1194 base::android::MethodID::TYPE_STATIC>(
1195 env, g_TestJni_clazz,
1199 "Ljava/lang/String;"
1201 "Ljava/io/InputStream;",
1202 &g_TestJni_openUrl);
1205 env->CallStaticObjectMethod(g_TestJni_clazz,
1207 base::android::CheckException(env);
1208 return ScopedJavaLocalRef<jobject>(env, ret);
1211 static base::subtle::AtomicWord g_TestJni_activateHardwareAcceleration = 0;
1212 static void Java_TestJni_activateHardwareAcceleration(JNIEnv* env, jobject obj,
1217 jint iSecondaryID) {
1218 /* Must call RegisterNativesImpl() */
1219 DCHECK(g_TestJni_clazz);
1220 jmethodID method_id =
1221 base::android::MethodID::LazyGet<
1222 base::android::MethodID::TYPE_INSTANCE>(
1223 env, g_TestJni_clazz,
1224 "activateHardwareAcceleration",
1234 &g_TestJni_activateHardwareAcceleration);
1236 env->CallVoidMethod(obj,
1237 method_id, activated, iPid, iType, iPrimaryID, iSecondaryID);
1238 base::android::CheckException(env);
1242 static base::subtle::AtomicWord g_TestJni_uncheckedCall = 0;
1243 static void Java_TestJni_uncheckedCall(JNIEnv* env, jobject obj, jint iParam) {
1244 /* Must call RegisterNativesImpl() */
1245 DCHECK(g_TestJni_clazz);
1246 jmethodID method_id =
1247 base::android::MethodID::LazyGet<
1248 base::android::MethodID::TYPE_INSTANCE>(
1249 env, g_TestJni_clazz,
1256 &g_TestJni_uncheckedCall);
1258 env->CallVoidMethod(obj,
1263 static base::subtle::AtomicWord g_TestJni_returnByteArray = 0;
1264 static ScopedJavaLocalRef<jbyteArray> Java_TestJni_returnByteArray(JNIEnv* env,
1266 /* Must call RegisterNativesImpl() */
1267 DCHECK(g_TestJni_clazz);
1268 jmethodID method_id =
1269 base::android::MethodID::LazyGet<
1270 base::android::MethodID::TYPE_INSTANCE>(
1271 env, g_TestJni_clazz,
1277 &g_TestJni_returnByteArray);
1280 static_cast<jbyteArray>(env->CallObjectMethod(obj,
1282 base::android::CheckException(env);
1283 return ScopedJavaLocalRef<jbyteArray>(env, ret);
1286 static base::subtle::AtomicWord g_TestJni_returnBooleanArray = 0;
1287 static ScopedJavaLocalRef<jbooleanArray> Java_TestJni_returnBooleanArray(JNIEnv*
1289 /* Must call RegisterNativesImpl() */
1290 DCHECK(g_TestJni_clazz);
1291 jmethodID method_id =
1292 base::android::MethodID::LazyGet<
1293 base::android::MethodID::TYPE_INSTANCE>(
1294 env, g_TestJni_clazz,
1295 "returnBooleanArray",
1300 &g_TestJni_returnBooleanArray);
1303 static_cast<jbooleanArray>(env->CallObjectMethod(obj,
1305 base::android::CheckException(env);
1306 return ScopedJavaLocalRef<jbooleanArray>(env, ret);
1309 static base::subtle::AtomicWord g_TestJni_returnCharArray = 0;
1310 static ScopedJavaLocalRef<jcharArray> Java_TestJni_returnCharArray(JNIEnv* env,
1312 /* Must call RegisterNativesImpl() */
1313 DCHECK(g_TestJni_clazz);
1314 jmethodID method_id =
1315 base::android::MethodID::LazyGet<
1316 base::android::MethodID::TYPE_INSTANCE>(
1317 env, g_TestJni_clazz,
1323 &g_TestJni_returnCharArray);
1326 static_cast<jcharArray>(env->CallObjectMethod(obj,
1328 base::android::CheckException(env);
1329 return ScopedJavaLocalRef<jcharArray>(env, ret);
1332 static base::subtle::AtomicWord g_TestJni_returnShortArray = 0;
1333 static ScopedJavaLocalRef<jshortArray> Java_TestJni_returnShortArray(JNIEnv*
1335 /* Must call RegisterNativesImpl() */
1336 DCHECK(g_TestJni_clazz);
1337 jmethodID method_id =
1338 base::android::MethodID::LazyGet<
1339 base::android::MethodID::TYPE_INSTANCE>(
1340 env, g_TestJni_clazz,
1346 &g_TestJni_returnShortArray);
1349 static_cast<jshortArray>(env->CallObjectMethod(obj,
1351 base::android::CheckException(env);
1352 return ScopedJavaLocalRef<jshortArray>(env, ret);
1355 static base::subtle::AtomicWord g_TestJni_returnIntArray = 0;
1356 static ScopedJavaLocalRef<jintArray> Java_TestJni_returnIntArray(JNIEnv* env,
1358 /* Must call RegisterNativesImpl() */
1359 DCHECK(g_TestJni_clazz);
1360 jmethodID method_id =
1361 base::android::MethodID::LazyGet<
1362 base::android::MethodID::TYPE_INSTANCE>(
1363 env, g_TestJni_clazz,
1369 &g_TestJni_returnIntArray);
1372 static_cast<jintArray>(env->CallObjectMethod(obj,
1374 base::android::CheckException(env);
1375 return ScopedJavaLocalRef<jintArray>(env, ret);
1378 static base::subtle::AtomicWord g_TestJni_returnLongArray = 0;
1379 static ScopedJavaLocalRef<jlongArray> Java_TestJni_returnLongArray(JNIEnv* env,
1381 /* Must call RegisterNativesImpl() */
1382 DCHECK(g_TestJni_clazz);
1383 jmethodID method_id =
1384 base::android::MethodID::LazyGet<
1385 base::android::MethodID::TYPE_INSTANCE>(
1386 env, g_TestJni_clazz,
1392 &g_TestJni_returnLongArray);
1395 static_cast<jlongArray>(env->CallObjectMethod(obj,
1397 base::android::CheckException(env);
1398 return ScopedJavaLocalRef<jlongArray>(env, ret);
1401 static base::subtle::AtomicWord g_TestJni_returnDoubleArray = 0;
1402 static ScopedJavaLocalRef<jdoubleArray> Java_TestJni_returnDoubleArray(JNIEnv*
1404 /* Must call RegisterNativesImpl() */
1405 DCHECK(g_TestJni_clazz);
1406 jmethodID method_id =
1407 base::android::MethodID::LazyGet<
1408 base::android::MethodID::TYPE_INSTANCE>(
1409 env, g_TestJni_clazz,
1410 "returnDoubleArray",
1415 &g_TestJni_returnDoubleArray);
1418 static_cast<jdoubleArray>(env->CallObjectMethod(obj,
1420 base::android::CheckException(env);
1421 return ScopedJavaLocalRef<jdoubleArray>(env, ret);
1424 static base::subtle::AtomicWord g_TestJni_returnObjectArray = 0;
1425 static ScopedJavaLocalRef<jobjectArray> Java_TestJni_returnObjectArray(JNIEnv*
1427 /* Must call RegisterNativesImpl() */
1428 DCHECK(g_TestJni_clazz);
1429 jmethodID method_id =
1430 base::android::MethodID::LazyGet<
1431 base::android::MethodID::TYPE_INSTANCE>(
1432 env, g_TestJni_clazz,
1433 "returnObjectArray",
1437 "[Ljava/lang/Object;",
1438 &g_TestJni_returnObjectArray);
1441 static_cast<jobjectArray>(env->CallObjectMethod(obj,
1443 base::android::CheckException(env);
1444 return ScopedJavaLocalRef<jobjectArray>(env, ret);
1447 static base::subtle::AtomicWord g_TestJni_returnArrayOfByteArray = 0;
1448 static ScopedJavaLocalRef<jobjectArray>
1449 Java_TestJni_returnArrayOfByteArray(JNIEnv* env, jobject obj) {
1450 /* Must call RegisterNativesImpl() */
1451 DCHECK(g_TestJni_clazz);
1452 jmethodID method_id =
1453 base::android::MethodID::LazyGet<
1454 base::android::MethodID::TYPE_INSTANCE>(
1455 env, g_TestJni_clazz,
1456 "returnArrayOfByteArray",
1461 &g_TestJni_returnArrayOfByteArray);
1464 static_cast<jobjectArray>(env->CallObjectMethod(obj,
1466 base::android::CheckException(env);
1467 return ScopedJavaLocalRef<jobjectArray>(env, ret);
1470 static base::subtle::AtomicWord g_TestJni_getCompressFormat = 0;
1471 static ScopedJavaLocalRef<jobject> Java_TestJni_getCompressFormat(JNIEnv* env,
1473 /* Must call RegisterNativesImpl() */
1474 DCHECK(g_TestJni_clazz);
1475 jmethodID method_id =
1476 base::android::MethodID::LazyGet<
1477 base::android::MethodID::TYPE_INSTANCE>(
1478 env, g_TestJni_clazz,
1479 "getCompressFormat",
1483 "Landroid/graphics/Bitmap$CompressFormat;",
1484 &g_TestJni_getCompressFormat);
1487 env->CallObjectMethod(obj,
1489 base::android::CheckException(env);
1490 return ScopedJavaLocalRef<jobject>(env, ret);
1493 static base::subtle::AtomicWord g_TestJni_getCompressFormatList = 0;
1494 static ScopedJavaLocalRef<jobject> Java_TestJni_getCompressFormatList(JNIEnv*
1496 /* Must call RegisterNativesImpl() */
1497 DCHECK(g_TestJni_clazz);
1498 jmethodID method_id =
1499 base::android::MethodID::LazyGet<
1500 base::android::MethodID::TYPE_INSTANCE>(
1501 env, g_TestJni_clazz,
1502 "getCompressFormatList",
1507 &g_TestJni_getCompressFormatList);
1510 env->CallObjectMethod(obj,
1512 base::android::CheckException(env);
1513 return ScopedJavaLocalRef<jobject>(env, ret);
1516 // Step 3: RegisterNatives.
1518 static bool RegisterNativesImpl(JNIEnv* env) {
1520 g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
1521 base::android::GetClass(env, kTestJniClassPath).obj()));
1522 g_InfoBar_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
1523 base::android::GetClass(env, kInfoBarClassPath).obj()));
1527 #endif // org_chromium_TestJni_JNI
1529 self
.assertTextEquals(golden_content
, h
.GetContent())
1531 def testCalledByNativeParseError(self
):
1533 jni_generator
.ExtractCalledByNatives("""
1535 public static int foo(); // This one is fine
1540 self
.fail('Expected a ParseError')
1541 except jni_generator
.ParseError
, e
:
1542 self
.assertEquals(('@CalledByNative', 'scooby doo'), e
.context_lines
)
1544 def testFullyQualifiedClassName(self
):
1546 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
1547 // Use of this source code is governed by a BSD-style license that can be
1548 // found in the LICENSE file.
1550 package org.chromium.content.browser;
1552 import org.chromium.base.BuildInfo;
1554 self
.assertEquals('org/chromium/content/browser/Foo',
1555 jni_generator
.ExtractFullyQualifiedJavaClassName(
1556 'org/chromium/content/browser/Foo.java', contents
))
1557 self
.assertEquals('org/chromium/content/browser/Foo',
1558 jni_generator
.ExtractFullyQualifiedJavaClassName(
1559 'frameworks/Foo.java', contents
))
1560 self
.assertRaises(SyntaxError,
1561 jni_generator
.ExtractFullyQualifiedJavaClassName
,
1562 'com/foo/Bar', 'no PACKAGE line')
1564 def testMethodNameMangling(self
):
1565 self
.assertEquals('closeV',
1566 jni_generator
.GetMangledMethodName('close', [], 'void'))
1567 self
.assertEquals('readI_AB_I_I',
1568 jni_generator
.GetMangledMethodName('read',
1576 self
.assertEquals('openJIIS_JLS',
1577 jni_generator
.GetMangledMethodName('open',
1579 datatype
='java/lang/String'),],
1580 'java/io/InputStream'))
1582 def testFromJavaPGenerics(self
):
1584 public abstract class java.util.HashSet<T> extends java.util.AbstractSet<E>
1585 implements java.util.Set<E>, java.lang.Cloneable, java.io.Serializable {
1586 public void dummy();
1590 jni_from_javap
= jni_generator
.JNIFromJavaP(contents
.split('\n'),
1592 self
.assertEquals(1, len(jni_from_javap
.called_by_natives
))
1593 golden_content
= """\
1594 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
1595 // Use of this source code is governed by a BSD-style license that can be
1596 // found in the LICENSE file.
1598 // This file is autogenerated by
1599 // base/android/jni_generator/jni_generator.py
1601 // java/util/HashSet
1603 #ifndef java_util_HashSet_JNI
1604 #define java_util_HashSet_JNI
1608 #include "base/android/jni_android.h"
1609 #include "base/android/scoped_java_ref.h"
1610 #include "base/basictypes.h"
1611 #include "base/logging.h"
1613 using base::android::ScopedJavaLocalRef;
1615 // Step 1: forward declarations.
1617 const char kHashSetClassPath[] = "java/util/HashSet";
1618 // Leaking this jclass as we cannot use LazyInstance from some threads.
1619 jclass g_HashSet_clazz = NULL;
1622 namespace JNI_HashSet {
1624 // Step 2: method stubs.
1626 static base::subtle::AtomicWord g_HashSet_dummy = 0;
1627 static void Java_HashSet_dummy(JNIEnv* env, jobject obj) __attribute__
1629 static void Java_HashSet_dummy(JNIEnv* env, jobject obj) {
1630 /* Must call RegisterNativesImpl() */
1631 DCHECK(g_HashSet_clazz);
1632 jmethodID method_id =
1633 base::android::MethodID::LazyGet<
1634 base::android::MethodID::TYPE_INSTANCE>(
1635 env, g_HashSet_clazz,
1640 env->CallVoidMethod(obj,
1642 base::android::CheckException(env);
1646 // Step 3: RegisterNatives.
1648 static bool RegisterNativesImpl(JNIEnv* env) {
1650 g_HashSet_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
1651 base::android::GetClass(env, kHashSetClassPath).obj()));
1654 } // namespace JNI_HashSet
1656 #endif // java_util_HashSet_JNI
1658 self
.assertTextEquals(golden_content
, jni_from_javap
.GetContent())
1660 def testSnippnetJavap6_7(self
):
1661 content_javap6
= """
1662 public class java.util.HashSet {
1663 public boolean add(java.lang.Object);
1664 Signature: (Ljava/lang/Object;)Z
1668 content_javap7
= """
1669 public class java.util.HashSet {
1670 public boolean add(E);
1671 Signature: (Ljava/lang/Object;)Z
1674 jni_from_javap6
= jni_generator
.JNIFromJavaP(content_javap6
.split('\n'),
1676 jni_from_javap7
= jni_generator
.JNIFromJavaP(content_javap7
.split('\n'),
1678 self
.assertTrue(jni_from_javap6
.GetContent())
1679 self
.assertTrue(jni_from_javap7
.GetContent())
1680 # Ensure the javap7 is correctly parsed and uses the Signature field rather
1681 # than the "E" parameter.
1682 self
.assertTextEquals(jni_from_javap6
.GetContent(),
1683 jni_from_javap7
.GetContent())
1685 def testFromJavaP(self
):
1687 public abstract class java.io.InputStream extends
1688 java.lang.Object implements java.io.Closeable{
1689 public java.io.InputStream();
1691 public int available() throws java.io.IOException;
1693 public void close() throws java.io.IOException;
1695 public void mark(int);
1697 public boolean markSupported();
1699 public abstract int read() throws java.io.IOException;
1701 public int read(byte[]) throws java.io.IOException;
1703 public int read(byte[], int, int) throws java.io.IOException;
1705 public synchronized void reset() throws java.io.IOException;
1707 public long skip(long) throws java.io.IOException;
1711 jni_from_javap
= jni_generator
.JNIFromJavaP(contents
.split('\n'),
1713 self
.assertEquals(10, len(jni_from_javap
.called_by_natives
))
1714 golden_content
= """\
1715 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
1716 // Use of this source code is governed by a BSD-style license that can be
1717 // found in the LICENSE file.
1719 // This file is autogenerated by
1720 // base/android/jni_generator/jni_generator.py
1722 // java/io/InputStream
1724 #ifndef java_io_InputStream_JNI
1725 #define java_io_InputStream_JNI
1729 #include "base/android/jni_android.h"
1730 #include "base/android/scoped_java_ref.h"
1731 #include "base/basictypes.h"
1732 #include "base/logging.h"
1734 using base::android::ScopedJavaLocalRef;
1736 // Step 1: forward declarations.
1738 const char kInputStreamClassPath[] = "java/io/InputStream";
1739 // Leaking this jclass as we cannot use LazyInstance from some threads.
1740 jclass g_InputStream_clazz = NULL;
1743 namespace JNI_InputStream {
1745 // Step 2: method stubs.
1747 static base::subtle::AtomicWord g_InputStream_available = 0;
1748 static jint Java_InputStream_available(JNIEnv* env, jobject obj) __attribute__
1750 static jint Java_InputStream_available(JNIEnv* env, jobject obj) {
1751 /* Must call RegisterNativesImpl() */
1752 DCHECK(g_InputStream_clazz);
1753 jmethodID method_id =
1754 base::android::MethodID::LazyGet<
1755 base::android::MethodID::TYPE_INSTANCE>(
1756 env, g_InputStream_clazz,
1759 &g_InputStream_available);
1762 env->CallIntMethod(obj,
1764 base::android::CheckException(env);
1768 static base::subtle::AtomicWord g_InputStream_close = 0;
1769 static void Java_InputStream_close(JNIEnv* env, jobject obj) __attribute__
1771 static void Java_InputStream_close(JNIEnv* env, jobject obj) {
1772 /* Must call RegisterNativesImpl() */
1773 DCHECK(g_InputStream_clazz);
1774 jmethodID method_id =
1775 base::android::MethodID::LazyGet<
1776 base::android::MethodID::TYPE_INSTANCE>(
1777 env, g_InputStream_clazz,
1780 &g_InputStream_close);
1782 env->CallVoidMethod(obj,
1784 base::android::CheckException(env);
1788 static base::subtle::AtomicWord g_InputStream_mark = 0;
1789 static void Java_InputStream_mark(JNIEnv* env, jobject obj, jint p0)
1790 __attribute__ ((unused));
1791 static void Java_InputStream_mark(JNIEnv* env, jobject obj, jint p0) {
1792 /* Must call RegisterNativesImpl() */
1793 DCHECK(g_InputStream_clazz);
1794 jmethodID method_id =
1795 base::android::MethodID::LazyGet<
1796 base::android::MethodID::TYPE_INSTANCE>(
1797 env, g_InputStream_clazz,
1800 &g_InputStream_mark);
1802 env->CallVoidMethod(obj,
1804 base::android::CheckException(env);
1808 static base::subtle::AtomicWord g_InputStream_markSupported = 0;
1809 static jboolean Java_InputStream_markSupported(JNIEnv* env, jobject obj)
1810 __attribute__ ((unused));
1811 static jboolean Java_InputStream_markSupported(JNIEnv* env, jobject obj) {
1812 /* Must call RegisterNativesImpl() */
1813 DCHECK(g_InputStream_clazz);
1814 jmethodID method_id =
1815 base::android::MethodID::LazyGet<
1816 base::android::MethodID::TYPE_INSTANCE>(
1817 env, g_InputStream_clazz,
1820 &g_InputStream_markSupported);
1823 env->CallBooleanMethod(obj,
1825 base::android::CheckException(env);
1829 static base::subtle::AtomicWord g_InputStream_readI = 0;
1830 static jint Java_InputStream_readI(JNIEnv* env, jobject obj) __attribute__
1832 static jint Java_InputStream_readI(JNIEnv* env, jobject obj) {
1833 /* Must call RegisterNativesImpl() */
1834 DCHECK(g_InputStream_clazz);
1835 jmethodID method_id =
1836 base::android::MethodID::LazyGet<
1837 base::android::MethodID::TYPE_INSTANCE>(
1838 env, g_InputStream_clazz,
1841 &g_InputStream_readI);
1844 env->CallIntMethod(obj,
1846 base::android::CheckException(env);
1850 static base::subtle::AtomicWord g_InputStream_readI_AB = 0;
1851 static jint Java_InputStream_readI_AB(JNIEnv* env, jobject obj, jbyteArray p0)
1852 __attribute__ ((unused));
1853 static jint Java_InputStream_readI_AB(JNIEnv* env, jobject obj, jbyteArray p0) {
1854 /* Must call RegisterNativesImpl() */
1855 DCHECK(g_InputStream_clazz);
1856 jmethodID method_id =
1857 base::android::MethodID::LazyGet<
1858 base::android::MethodID::TYPE_INSTANCE>(
1859 env, g_InputStream_clazz,
1862 &g_InputStream_readI_AB);
1865 env->CallIntMethod(obj,
1867 base::android::CheckException(env);
1871 static base::subtle::AtomicWord g_InputStream_readI_AB_I_I = 0;
1872 static jint Java_InputStream_readI_AB_I_I(JNIEnv* env, jobject obj, jbyteArray
1875 jint p2) __attribute__ ((unused));
1876 static jint Java_InputStream_readI_AB_I_I(JNIEnv* env, jobject obj, jbyteArray
1880 /* Must call RegisterNativesImpl() */
1881 DCHECK(g_InputStream_clazz);
1882 jmethodID method_id =
1883 base::android::MethodID::LazyGet<
1884 base::android::MethodID::TYPE_INSTANCE>(
1885 env, g_InputStream_clazz,
1888 &g_InputStream_readI_AB_I_I);
1891 env->CallIntMethod(obj,
1892 method_id, p0, p1, p2);
1893 base::android::CheckException(env);
1897 static base::subtle::AtomicWord g_InputStream_reset = 0;
1898 static void Java_InputStream_reset(JNIEnv* env, jobject obj) __attribute__
1900 static void Java_InputStream_reset(JNIEnv* env, jobject obj) {
1901 /* Must call RegisterNativesImpl() */
1902 DCHECK(g_InputStream_clazz);
1903 jmethodID method_id =
1904 base::android::MethodID::LazyGet<
1905 base::android::MethodID::TYPE_INSTANCE>(
1906 env, g_InputStream_clazz,
1909 &g_InputStream_reset);
1911 env->CallVoidMethod(obj,
1913 base::android::CheckException(env);
1917 static base::subtle::AtomicWord g_InputStream_skip = 0;
1918 static jlong Java_InputStream_skip(JNIEnv* env, jobject obj, jlong p0)
1919 __attribute__ ((unused));
1920 static jlong Java_InputStream_skip(JNIEnv* env, jobject obj, jlong p0) {
1921 /* Must call RegisterNativesImpl() */
1922 DCHECK(g_InputStream_clazz);
1923 jmethodID method_id =
1924 base::android::MethodID::LazyGet<
1925 base::android::MethodID::TYPE_INSTANCE>(
1926 env, g_InputStream_clazz,
1929 &g_InputStream_skip);
1932 env->CallLongMethod(obj,
1934 base::android::CheckException(env);
1938 static base::subtle::AtomicWord g_InputStream_Constructor = 0;
1939 static ScopedJavaLocalRef<jobject> Java_InputStream_Constructor(JNIEnv* env)
1940 __attribute__ ((unused));
1941 static ScopedJavaLocalRef<jobject> Java_InputStream_Constructor(JNIEnv* env) {
1942 /* Must call RegisterNativesImpl() */
1943 DCHECK(g_InputStream_clazz);
1944 jmethodID method_id =
1945 base::android::MethodID::LazyGet<
1946 base::android::MethodID::TYPE_INSTANCE>(
1947 env, g_InputStream_clazz,
1950 &g_InputStream_Constructor);
1953 env->NewObject(g_InputStream_clazz,
1955 base::android::CheckException(env);
1956 return ScopedJavaLocalRef<jobject>(env, ret);
1959 // Step 3: RegisterNatives.
1961 static bool RegisterNativesImpl(JNIEnv* env) {
1963 g_InputStream_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
1964 base::android::GetClass(env, kInputStreamClassPath).obj()));
1967 } // namespace JNI_InputStream
1969 #endif // java_io_InputStream_JNI
1971 self
.assertTextEquals(golden_content
, jni_from_javap
.GetContent())
1973 def testREForNatives(self
):
1974 # We should not match "native SyncSetupFlow" inside the comment.
1977 * Invoked when the setup process is complete so we can disconnect from the
1978 * native-side SyncSetupFlowHandler.
1980 public void destroy() {
1981 Log.v(TAG, "Destroying native SyncSetupFlow");
1982 if (mNativeSyncSetupFlow != 0) {
1983 nativeSyncSetupEnded(mNativeSyncSetupFlow);
1984 mNativeSyncSetupFlow = 0;
1987 private native void nativeSyncSetupEnded(
1988 int nativeAndroidSyncSetupFlowHandler);
1990 jni_from_java
= jni_generator
.JNIFromJavaSource(
1991 test_data
, 'foo/bar', TestOptions())
1993 def testRaisesOnNonJNIMethod(self
):
1995 class MyInnerClass {
1996 private int Foo(int p0) {
2000 self
.assertRaises(SyntaxError,
2001 jni_generator
.JNIFromJavaSource
,
2002 test_data
, 'foo/bar', TestOptions())
2004 def testJniSelfDocumentingExample(self
):
2005 script_dir
= os
.path
.dirname(sys
.argv
[0])
2006 content
= file(os
.path
.join(script_dir
,
2007 'java/src/org/chromium/example/jni_generator/SampleForTests.java')
2009 golden_content
= file(os
.path
.join(script_dir
,
2010 'golden_sample_for_tests_jni.h')).read()
2011 jni_from_java
= jni_generator
.JNIFromJavaSource(
2012 content
, 'org/chromium/example/jni_generator/SampleForTests',
2014 self
.assertTextEquals(golden_content
, jni_from_java
.GetContent())
2016 def testNoWrappingPreprocessorLines(self
):
2018 package com.google.lookhowextremelylongiam.snarf.icankeepthisupallday;
2020 class ReallyLongClassNamesAreAllTheRage {
2021 private static native int nativeTest();
2024 jni_from_java
= jni_generator
.JNIFromJavaSource(
2025 test_data
, ('com/google/lookhowextremelylongiam/snarf/'
2026 'icankeepthisupallday/ReallyLongClassNamesAreAllTheRage'),
2028 jni_lines
= jni_from_java
.GetContent().split('\n')
2029 line
= filter(lambda line
: line
.lstrip().startswith('#ifndef'),
2031 self
.assertTrue(len(line
) > 80,
2032 ('Expected #ifndef line to be > 80 chars: ', line
))
2034 def testJarJarRemapping(self
):
2036 package org.chromium.example.jni_generator;
2038 import org.chromium.example2.Test;
2041 private static native void nativeTest(Test t);
2044 jni_generator
.JniParams
.SetJarJarMappings(
2045 """rule org.chromium.example.** com.test.@1
2046 rule org.chromium.example2.** org.test2.@0""")
2047 jni_from_java
= jni_generator
.JNIFromJavaSource(
2048 test_data
, 'org/chromium/example/jni_generator/Example', TestOptions())
2049 jni_generator
.JniParams
.SetJarJarMappings('')
2050 golden_content
= """\
2051 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2052 // Use of this source code is governed by a BSD-style license that can be
2053 // found in the LICENSE file.
2055 // This file is autogenerated by
2056 // base/android/jni_generator/jni_generator.py
2058 // org/chromium/example/jni_generator/Example
2060 #ifndef org_chromium_example_jni_generator_Example_JNI
2061 #define org_chromium_example_jni_generator_Example_JNI
2065 #include "base/android/jni_android.h"
2066 #include "base/android/scoped_java_ref.h"
2067 #include "base/basictypes.h"
2068 #include "base/logging.h"
2070 using base::android::ScopedJavaLocalRef;
2072 // Step 1: forward declarations.
2074 const char kExampleClassPath[] = "com/test/jni_generator/Example";
2075 // Leaking this jclass as we cannot use LazyInstance from some threads.
2076 jclass g_Example_clazz = NULL;
2079 static void Test(JNIEnv* env, jclass clazz,
2082 // Step 2: method stubs.
2084 // Step 3: RegisterNatives.
2086 static bool RegisterNativesImpl(JNIEnv* env) {
2088 g_Example_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
2089 base::android::GetClass(env, kExampleClassPath).obj()));
2090 static const JNINativeMethod kMethodsExample[] = {
2093 "Lorg/test2/org/chromium/example2/Test;"
2095 "V", reinterpret_cast<void*>(Test) },
2097 const int kMethodsExampleSize = arraysize(kMethodsExample);
2099 if (env->RegisterNatives(g_Example_clazz,
2101 kMethodsExampleSize) < 0) {
2102 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
2109 #endif // org_chromium_example_jni_generator_Example_JNI
2111 self
.assertTextEquals(golden_content
, jni_from_java
.GetContent())
2113 def testImports(self
):
2115 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2116 // Use of this source code is governed by a BSD-style license that can be
2117 // found in the LICENSE file.
2119 package org.chromium.content.app;
2121 import android.app.Service;
2122 import android.content.Context;
2123 import android.content.Intent;
2124 import android.graphics.SurfaceTexture;
2125 import android.os.Bundle;
2126 import android.os.IBinder;
2127 import android.os.ParcelFileDescriptor;
2128 import android.os.Process;
2129 import android.os.RemoteException;
2130 import android.util.Log;
2131 import android.view.Surface;
2133 import java.util.ArrayList;
2135 import org.chromium.base.CalledByNative;
2136 import org.chromium.base.JNINamespace;
2137 import org.chromium.content.app.ContentMain;
2138 import org.chromium.content.browser.SandboxedProcessConnection;
2139 import org.chromium.content.common.ISandboxedProcessCallback;
2140 import org.chromium.content.common.ISandboxedProcessService;
2141 import org.chromium.content.common.WillNotRaise.AnException;
2142 import org.chromium.content.common.WillRaise.AnException;
2144 import static org.chromium.Bar.Zoo;
2147 public static class BookmarkNode implements Parcelable {
2149 public interface PasswordListObserver {
2153 jni_generator
.JniParams
.SetFullyQualifiedClass(
2154 'org/chromium/content/app/Foo')
2155 jni_generator
.JniParams
.ExtractImportsAndInnerClasses(import_header
)
2156 self
.assertTrue('Lorg/chromium/content/common/ISandboxedProcessService' in
2157 jni_generator
.JniParams
._imports
)
2158 self
.assertTrue('Lorg/chromium/Bar/Zoo' in
2159 jni_generator
.JniParams
._imports
)
2160 self
.assertTrue('Lorg/chromium/content/app/Foo$BookmarkNode' in
2161 jni_generator
.JniParams
._inner
_classes
)
2162 self
.assertTrue('Lorg/chromium/content/app/Foo$PasswordListObserver' in
2163 jni_generator
.JniParams
._inner
_classes
)
2164 self
.assertEquals('Lorg/chromium/content/app/ContentMain$Inner;',
2165 jni_generator
.JniParams
.JavaToJni('ContentMain.Inner'))
2166 self
.assertRaises(SyntaxError,
2167 jni_generator
.JniParams
.JavaToJni
,
2170 def testJniParamsJavaToJni(self
):
2171 self
.assertTextEquals('I', JniParams
.JavaToJni('int'))
2172 self
.assertTextEquals('[B', JniParams
.JavaToJni('byte[]'))
2173 self
.assertTextEquals(
2174 '[Ljava/nio/ByteBuffer;', JniParams
.JavaToJni('java/nio/ByteBuffer[]'))
2176 def testNativesLong(self
):
2177 test_options
= TestOptions()
2178 test_options
.ptr_type
= 'long'
2180 private native void nativeDestroy(long nativeChromeBrowserProvider);
2182 jni_generator
.JniParams
.ExtractImportsAndInnerClasses(test_data
)
2183 natives
= jni_generator
.ExtractNatives(test_data
, test_options
.ptr_type
)
2185 NativeMethod(return_type
='void', static
=False, name
='Destroy',
2186 params
=[Param(datatype
='long',
2187 name
='nativeChromeBrowserProvider')],
2188 java_class_name
=None,
2190 p0_type
='ChromeBrowserProvider',
2191 ptr_type
=test_options
.ptr_type
),
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.
2201 // This file is autogenerated by
2202 // base/android/jni_generator/jni_generator.py
2204 // org/chromium/TestJni
2206 #ifndef org_chromium_TestJni_JNI
2207 #define org_chromium_TestJni_JNI
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"
2216 using base::android::ScopedJavaLocalRef;
2218 // Step 1: forward declarations.
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;
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);
2234 // Step 3: RegisterNatives.
2236 static bool RegisterNativesImpl(JNIEnv* env) {
2238 g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
2239 base::android::GetClass(env, kTestJniClassPath).obj()));
2240 static const JNINativeMethod kMethodsTestJni[] = {
2245 "V", reinterpret_cast<void*>(Destroy) },
2247 const int kMethodsTestJniSize = arraysize(kMethodsTestJni);
2249 if (env->RegisterNatives(g_TestJni_clazz,
2251 kMethodsTestJniSize) < 0) {
2252 LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
2259 #endif // org_chromium_TestJni_JNI
2261 self
.assertTextEquals(golden_content
, h
.GetContent())
2264 if __name__
== '__main__':