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
20 from jni_generator
import CalledByNative
, JniParams
, NativeMethod
, Param
23 SCRIPT_NAME
= 'base/android/jni_generator/jni_generator.py'
25 'base/android/jni_generator/jni_generator_helper.h'
28 # Set this environment variable in order to regenerate the golden text
30 REBASELINE_ENV
= 'REBASELINE'
32 class TestOptions(object):
33 """The mock options object which is passed to the jni_generator.py script."""
37 self
.script_name
= SCRIPT_NAME
38 self
.includes
= INCLUDES
39 self
.pure_native_methods
= False
40 self
.ptr_type
= 'long'
41 self
.jni_init_native_name
= None
42 self
.eager_called_by_natives
= False
45 self
.native_exports
= False
47 class TestGenerator(unittest
.TestCase
):
48 def assertObjEquals(self
, first
, second
):
49 dict_first
= first
.__dict
__
50 dict_second
= second
.__dict
__
51 self
.assertEquals(dict_first
.keys(), dict_second
.keys())
52 for key
, value
in dict_first
.iteritems():
53 if (type(value
) is list and len(value
) and
54 isinstance(type(value
[0]), object)):
55 self
.assertListEquals(value
, second
.__getattribute
__(key
))
57 actual
= second
.__getattribute
__(key
)
58 self
.assertEquals(value
, actual
,
59 'Key ' + key
+ ': ' + str(value
) + '!=' + str(actual
))
61 def assertListEquals(self
, first
, second
):
62 self
.assertEquals(len(first
), len(second
))
63 for i
in xrange(len(first
)):
64 if isinstance(first
[i
], object):
65 self
.assertObjEquals(first
[i
], second
[i
])
67 self
.assertEquals(first
[i
], second
[i
])
69 def assertTextEquals(self
, golden_text
, generated_text
):
70 if not self
.compareText(golden_text
, generated_text
):
71 self
.fail('Golden text mismatch.')
73 def compareText(self
, golden_text
, generated_text
):
76 l
.strip() for l
in text
.split('\n')
77 if not l
.startswith('// Copyright')
79 stripped_golden
= FilterText(golden_text
)
80 stripped_generated
= FilterText(generated_text
)
81 if stripped_golden
== stripped_generated
:
84 for line
in difflib
.context_diff(stripped_golden
, stripped_generated
):
91 print 'REBASELINE=1', sys
.argv
[0]
92 print 'to regenerate the data files.'
94 def _ReadGoldenFile(self
, golden_file
):
95 if not os
.path
.exists(golden_file
):
97 with
file(golden_file
, 'r') as f
:
100 def assertGoldenTextEquals(self
, generated_text
):
101 script_dir
= os
.path
.dirname(sys
.argv
[0])
102 # This is the caller test method.
103 caller
= inspect
.stack()[1][3]
104 self
.assertTrue(caller
.startswith('test'),
105 'assertGoldenTextEquals can only be called from a '
106 'test* method, not %s' % caller
)
107 golden_file
= os
.path
.join(script_dir
, caller
+ '.golden')
108 golden_text
= self
._ReadGoldenFile
(golden_file
)
109 if os
.environ
.get(REBASELINE_ENV
):
110 if golden_text
!= generated_text
:
111 with
file(golden_file
, 'w') as f
:
112 f
.write(generated_text
)
114 self
.assertTextEquals(golden_text
, generated_text
)
116 def testInspectCaller(self
):
118 # This function can only be called from a test* method.
119 self
.assertGoldenTextEquals('')
120 self
.assertRaises(AssertionError, willRaise
)
122 def testNatives(self
):
124 interface OnFrameAvailableListener {}
125 private native int nativeInit();
126 private native void nativeDestroy(int nativeChromeBrowserProvider);
127 private native long nativeAddBookmark(
128 int nativeChromeBrowserProvider,
129 String url, String title, boolean isFolder, long parentId);
130 private static native String nativeGetDomainAndRegistry(String url);
131 private static native void nativeCreateHistoricalTabFromState(
132 byte[] state, int tab_index);
133 private native byte[] nativeGetStateAsByteArray(View view);
134 private static native String[] nativeGetAutofillProfileGUIDs();
135 private native void nativeSetRecognitionResults(
136 int sessionId, String[] results);
137 private native long nativeAddBookmarkFromAPI(
138 int nativeChromeBrowserProvider,
139 String url, Long created, Boolean isBookmark,
140 Long date, byte[] favicon, String title, Integer visits);
141 native int nativeFindAll(String find);
142 private static native OnFrameAvailableListener nativeGetInnerClass();
143 private native Bitmap nativeQueryBitmap(
144 int nativeChromeBrowserProvider,
145 String[] projection, String selection,
146 String[] selectionArgs, String sortOrder);
147 private native void nativeGotOrientation(
148 int nativeDataFetcherImplAndroid,
149 double alpha, double beta, double gamma);
151 jni_generator
.JniParams
.SetFullyQualifiedClass(
152 'org/chromium/example/jni_generator/SampleForTests')
153 jni_generator
.JniParams
.ExtractImportsAndInnerClasses(test_data
)
154 natives
= jni_generator
.ExtractNatives(test_data
, 'int')
156 NativeMethod(return_type
='int', static
=False,
159 java_class_name
=None,
161 NativeMethod(return_type
='void', static
=False, name
='Destroy',
162 params
=[Param(datatype
='int',
163 name
='nativeChromeBrowserProvider')],
164 java_class_name
=None,
166 p0_type
='ChromeBrowserProvider'),
167 NativeMethod(return_type
='long', static
=False, name
='AddBookmark',
168 params
=[Param(datatype
='int',
169 name
='nativeChromeBrowserProvider'),
170 Param(datatype
='String',
172 Param(datatype
='String',
174 Param(datatype
='boolean',
176 Param(datatype
='long',
178 java_class_name
=None,
180 p0_type
='ChromeBrowserProvider'),
181 NativeMethod(return_type
='String', static
=True,
182 name
='GetDomainAndRegistry',
183 params
=[Param(datatype
='String',
185 java_class_name
=None,
187 NativeMethod(return_type
='void', static
=True,
188 name
='CreateHistoricalTabFromState',
189 params
=[Param(datatype
='byte[]',
191 Param(datatype
='int',
193 java_class_name
=None,
195 NativeMethod(return_type
='byte[]', static
=False,
196 name
='GetStateAsByteArray',
197 params
=[Param(datatype
='View', name
='view')],
198 java_class_name
=None,
200 NativeMethod(return_type
='String[]', static
=True,
201 name
='GetAutofillProfileGUIDs', params
=[],
202 java_class_name
=None,
204 NativeMethod(return_type
='void', static
=False,
205 name
='SetRecognitionResults',
206 params
=[Param(datatype
='int', name
='sessionId'),
207 Param(datatype
='String[]', name
='results')],
208 java_class_name
=None,
210 NativeMethod(return_type
='long', static
=False,
211 name
='AddBookmarkFromAPI',
212 params
=[Param(datatype
='int',
213 name
='nativeChromeBrowserProvider'),
214 Param(datatype
='String',
216 Param(datatype
='Long',
218 Param(datatype
='Boolean',
220 Param(datatype
='Long',
222 Param(datatype
='byte[]',
224 Param(datatype
='String',
226 Param(datatype
='Integer',
228 java_class_name
=None,
230 p0_type
='ChromeBrowserProvider'),
231 NativeMethod(return_type
='int', static
=False,
233 params
=[Param(datatype
='String',
235 java_class_name
=None,
237 NativeMethod(return_type
='OnFrameAvailableListener', static
=True,
238 name
='GetInnerClass',
240 java_class_name
=None,
242 NativeMethod(return_type
='Bitmap',
245 params
=[Param(datatype
='int',
246 name
='nativeChromeBrowserProvider'),
247 Param(datatype
='String[]',
249 Param(datatype
='String',
251 Param(datatype
='String[]',
252 name
='selectionArgs'),
253 Param(datatype
='String',
256 java_class_name
=None,
258 p0_type
='ChromeBrowserProvider'),
259 NativeMethod(return_type
='void', static
=False,
260 name
='GotOrientation',
261 params
=[Param(datatype
='int',
262 name
='nativeDataFetcherImplAndroid'),
263 Param(datatype
='double',
265 Param(datatype
='double',
267 Param(datatype
='double',
270 java_class_name
=None,
272 p0_type
='content::DataFetcherImplAndroid'),
274 self
.assertListEquals(golden_natives
, natives
)
275 h
= jni_generator
.InlHeaderFileGenerator('', 'org/chromium/TestJni',
276 natives
, [], [], TestOptions())
277 self
.assertGoldenTextEquals(h
.GetContent())
279 def testInnerClassNatives(self
):
282 @NativeCall("MyInnerClass")
283 private native int nativeInit();
286 natives
= jni_generator
.ExtractNatives(test_data
, 'int')
288 NativeMethod(return_type
='int', static
=False,
289 name
='Init', params
=[],
290 java_class_name
='MyInnerClass',
293 self
.assertListEquals(golden_natives
, natives
)
294 h
= jni_generator
.InlHeaderFileGenerator('', 'org/chromium/TestJni',
295 natives
, [], [], TestOptions())
296 self
.assertGoldenTextEquals(h
.GetContent())
298 def testInnerClassNativesMultiple(self
):
301 @NativeCall("MyInnerClass")
302 private native int nativeInit();
304 class MyOtherInnerClass {
305 @NativeCall("MyOtherInnerClass")
306 private native int nativeInit();
309 natives
= jni_generator
.ExtractNatives(test_data
, 'int')
311 NativeMethod(return_type
='int', static
=False,
312 name
='Init', params
=[],
313 java_class_name
='MyInnerClass',
315 NativeMethod(return_type
='int', static
=False,
316 name
='Init', params
=[],
317 java_class_name
='MyOtherInnerClass',
320 self
.assertListEquals(golden_natives
, natives
)
321 h
= jni_generator
.InlHeaderFileGenerator('', 'org/chromium/TestJni',
322 natives
, [], [], TestOptions())
323 self
.assertGoldenTextEquals(h
.GetContent())
325 def testInnerClassNativesBothInnerAndOuter(self
):
328 private native int nativeInit();
329 class MyOtherInnerClass {
330 @NativeCall("MyOtherInnerClass")
331 private native int nativeInit();
335 natives
= jni_generator
.ExtractNatives(test_data
, 'int')
337 NativeMethod(return_type
='int', static
=False,
338 name
='Init', params
=[],
339 java_class_name
=None,
341 NativeMethod(return_type
='int', static
=False,
342 name
='Init', params
=[],
343 java_class_name
='MyOtherInnerClass',
346 self
.assertListEquals(golden_natives
, natives
)
347 h
= jni_generator
.InlHeaderFileGenerator('', 'org/chromium/TestJni',
348 natives
, [], [], TestOptions())
349 self
.assertGoldenTextEquals(h
.GetContent())
351 def testCalledByNatives(self
):
353 import android.graphics.Bitmap;
354 import android.view.View;
355 import java.io.InputStream;
356 import java.util.List;
361 InnerClass showConfirmInfoBar(int nativeInfoBar,
362 String buttonOk, String buttonCancel, String title, Bitmap icon) {
363 InfoBar infobar = new ConfirmInfoBar(nativeInfoBar, mContext,
364 buttonOk, buttonCancel,
369 InnerClass showAutoLoginInfoBar(int nativeInfoBar,
370 String realm, String account, String args) {
371 AutoLoginInfoBar infobar = new AutoLoginInfoBar(nativeInfoBar, mContext,
372 realm, account, args);
373 if (infobar.displayedAccountCount() == 0)
377 @CalledByNative("InfoBar")
379 @SuppressWarnings("unused")
381 private static boolean shouldShowAutoLogin(View view,
382 String realm, String account, String args) {
383 AccountManagerContainer accountManagerContainer =
384 new AccountManagerContainer((Activity)contentView.getContext(),
385 realm, account, args);
386 String[] logins = accountManagerContainer.getAccountLogins(null);
387 return logins.length != 0;
390 static InputStream openUrl(String url) {
394 private void activateHardwareAcceleration(final boolean activated,
395 final int iPid, final int iType,
396 final int iPrimaryID, final int iSecondaryID) {
401 @CalledByNativeUnchecked
402 private void uncheckedCall(int iParam);
405 public byte[] returnByteArray();
408 public boolean[] returnBooleanArray();
411 public char[] returnCharArray();
414 public short[] returnShortArray();
417 public int[] returnIntArray();
420 public long[] returnLongArray();
423 public double[] returnDoubleArray();
426 public Object[] returnObjectArray();
429 public byte[][] returnArrayOfByteArray();
432 public Bitmap.CompressFormat getCompressFormat();
435 public List<Bitmap.CompressFormat> getCompressFormatList();
437 jni_generator
.JniParams
.SetFullyQualifiedClass('org/chromium/Foo')
438 jni_generator
.JniParams
.ExtractImportsAndInnerClasses(test_data
)
439 called_by_natives
= jni_generator
.ExtractCalledByNatives(test_data
)
440 golden_called_by_natives
= [
442 return_type
='InnerClass',
445 name
='showConfirmInfoBar',
446 method_id_var_name
='showConfirmInfoBar',
448 params
=[Param(datatype
='int', name
='nativeInfoBar'),
449 Param(datatype
='String', name
='buttonOk'),
450 Param(datatype
='String', name
='buttonCancel'),
451 Param(datatype
='String', name
='title'),
452 Param(datatype
='Bitmap', name
='icon')],
453 env_call
=('Object', ''),
457 return_type
='InnerClass',
460 name
='showAutoLoginInfoBar',
461 method_id_var_name
='showAutoLoginInfoBar',
463 params
=[Param(datatype
='int', name
='nativeInfoBar'),
464 Param(datatype
='String', name
='realm'),
465 Param(datatype
='String', name
='account'),
466 Param(datatype
='String', name
='args')],
467 env_call
=('Object', ''),
475 method_id_var_name
='dismiss',
476 java_class_name
='InfoBar',
478 env_call
=('Void', ''),
482 return_type
='boolean',
485 name
='shouldShowAutoLogin',
486 method_id_var_name
='shouldShowAutoLogin',
488 params
=[Param(datatype
='View', name
='view'),
489 Param(datatype
='String', name
='realm'),
490 Param(datatype
='String', name
='account'),
491 Param(datatype
='String', name
='args')],
492 env_call
=('Boolean', ''),
496 return_type
='InputStream',
500 method_id_var_name
='openUrl',
502 params
=[Param(datatype
='String', name
='url')],
503 env_call
=('Object', ''),
510 name
='activateHardwareAcceleration',
511 method_id_var_name
='activateHardwareAcceleration',
513 params
=[Param(datatype
='boolean', name
='activated'),
514 Param(datatype
='int', name
='iPid'),
515 Param(datatype
='int', name
='iType'),
516 Param(datatype
='int', name
='iPrimaryID'),
517 Param(datatype
='int', name
='iSecondaryID'),
519 env_call
=('Void', ''),
526 name
='uncheckedCall',
527 method_id_var_name
='uncheckedCall',
529 params
=[Param(datatype
='int', name
='iParam')],
530 env_call
=('Void', ''),
534 return_type
='byte[]',
537 name
='returnByteArray',
538 method_id_var_name
='returnByteArray',
541 env_call
=('Void', ''),
545 return_type
='boolean[]',
548 name
='returnBooleanArray',
549 method_id_var_name
='returnBooleanArray',
552 env_call
=('Void', ''),
556 return_type
='char[]',
559 name
='returnCharArray',
560 method_id_var_name
='returnCharArray',
563 env_call
=('Void', ''),
567 return_type
='short[]',
570 name
='returnShortArray',
571 method_id_var_name
='returnShortArray',
574 env_call
=('Void', ''),
581 name
='returnIntArray',
582 method_id_var_name
='returnIntArray',
585 env_call
=('Void', ''),
589 return_type
='long[]',
592 name
='returnLongArray',
593 method_id_var_name
='returnLongArray',
596 env_call
=('Void', ''),
600 return_type
='double[]',
603 name
='returnDoubleArray',
604 method_id_var_name
='returnDoubleArray',
607 env_call
=('Void', ''),
611 return_type
='Object[]',
614 name
='returnObjectArray',
615 method_id_var_name
='returnObjectArray',
618 env_call
=('Void', ''),
622 return_type
='byte[][]',
625 name
='returnArrayOfByteArray',
626 method_id_var_name
='returnArrayOfByteArray',
629 env_call
=('Void', ''),
633 return_type
='Bitmap.CompressFormat',
636 name
='getCompressFormat',
637 method_id_var_name
='getCompressFormat',
640 env_call
=('Void', ''),
644 return_type
='List<Bitmap.CompressFormat>',
647 name
='getCompressFormatList',
648 method_id_var_name
='getCompressFormatList',
651 env_call
=('Void', ''),
655 self
.assertListEquals(golden_called_by_natives
, called_by_natives
)
656 h
= jni_generator
.InlHeaderFileGenerator('', 'org/chromium/TestJni',
657 [], called_by_natives
, [],
659 self
.assertGoldenTextEquals(h
.GetContent())
661 def testCalledByNativeParseError(self
):
663 jni_generator
.ExtractCalledByNatives("""
665 public static int foo(); // This one is fine
670 self
.fail('Expected a ParseError')
671 except jni_generator
.ParseError
, e
:
672 self
.assertEquals(('@CalledByNative', 'scooby doo'), e
.context_lines
)
674 def testFullyQualifiedClassName(self
):
676 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
677 // Use of this source code is governed by a BSD-style license that can be
678 // found in the LICENSE file.
680 package org.chromium.content.browser;
682 import org.chromium.base.BuildInfo;
684 self
.assertEquals('org/chromium/content/browser/Foo',
685 jni_generator
.ExtractFullyQualifiedJavaClassName(
686 'org/chromium/content/browser/Foo.java', contents
))
687 self
.assertEquals('org/chromium/content/browser/Foo',
688 jni_generator
.ExtractFullyQualifiedJavaClassName(
689 'frameworks/Foo.java', contents
))
690 self
.assertRaises(SyntaxError,
691 jni_generator
.ExtractFullyQualifiedJavaClassName
,
692 'com/foo/Bar', 'no PACKAGE line')
694 def testMethodNameMangling(self
):
695 self
.assertEquals('closeV',
696 jni_generator
.GetMangledMethodName('close', [], 'void'))
697 self
.assertEquals('readI_AB_I_I',
698 jni_generator
.GetMangledMethodName('read',
706 self
.assertEquals('openJIIS_JLS',
707 jni_generator
.GetMangledMethodName('open',
709 datatype
='java/lang/String'),],
710 'java/io/InputStream'))
712 def testFromJavaPGenerics(self
):
714 public abstract class java.util.HashSet<T> extends java.util.AbstractSet<E>
715 implements java.util.Set<E>, java.lang.Cloneable, java.io.Serializable {
720 jni_from_javap
= jni_generator
.JNIFromJavaP(contents
.split('\n'),
722 self
.assertEquals(1, len(jni_from_javap
.called_by_natives
))
723 self
.assertGoldenTextEquals(jni_from_javap
.GetContent())
725 def testSnippnetJavap6_7(self
):
727 public class java.util.HashSet {
728 public boolean add(java.lang.Object);
729 Signature: (Ljava/lang/Object;)Z
734 public class java.util.HashSet {
735 public boolean add(E);
736 Signature: (Ljava/lang/Object;)Z
739 jni_from_javap6
= jni_generator
.JNIFromJavaP(content_javap6
.split('\n'),
741 jni_from_javap7
= jni_generator
.JNIFromJavaP(content_javap7
.split('\n'),
743 self
.assertTrue(jni_from_javap6
.GetContent())
744 self
.assertTrue(jni_from_javap7
.GetContent())
745 # Ensure the javap7 is correctly parsed and uses the Signature field rather
746 # than the "E" parameter.
747 self
.assertTextEquals(jni_from_javap6
.GetContent(),
748 jni_from_javap7
.GetContent())
750 def testFromJavaP(self
):
751 contents
= self
._ReadGoldenFile
(os
.path
.join(os
.path
.dirname(sys
.argv
[0]),
752 'testInputStream.javap'))
753 jni_from_javap
= jni_generator
.JNIFromJavaP(contents
.split('\n'),
755 self
.assertEquals(10, len(jni_from_javap
.called_by_natives
))
756 self
.assertGoldenTextEquals(jni_from_javap
.GetContent())
758 def testConstantsFromJavaP(self
):
759 for f
in ['testMotionEvent.javap', 'testMotionEvent.javap7']:
760 contents
= self
._ReadGoldenFile
(os
.path
.join(os
.path
.dirname(sys
.argv
[0]),
762 jni_from_javap
= jni_generator
.JNIFromJavaP(contents
.split('\n'),
764 self
.assertEquals(86, len(jni_from_javap
.called_by_natives
))
765 self
.assertGoldenTextEquals(jni_from_javap
.GetContent())
767 def testREForNatives(self
):
768 # We should not match "native SyncSetupFlow" inside the comment.
771 * Invoked when the setup process is complete so we can disconnect from the
772 * native-side SyncSetupFlowHandler.
774 public void destroy() {
775 Log.v(TAG, "Destroying native SyncSetupFlow");
776 if (mNativeSyncSetupFlow != 0) {
777 nativeSyncSetupEnded(mNativeSyncSetupFlow);
778 mNativeSyncSetupFlow = 0;
781 private native void nativeSyncSetupEnded(
782 int nativeAndroidSyncSetupFlowHandler);
784 jni_from_java
= jni_generator
.JNIFromJavaSource(
785 test_data
, 'foo/bar', TestOptions())
787 def testRaisesOnNonJNIMethod(self
):
790 private int Foo(int p0) {
794 self
.assertRaises(SyntaxError,
795 jni_generator
.JNIFromJavaSource
,
796 test_data
, 'foo/bar', TestOptions())
798 def testJniSelfDocumentingExample(self
):
799 script_dir
= os
.path
.dirname(sys
.argv
[0])
800 content
= file(os
.path
.join(script_dir
,
801 'java/src/org/chromium/example/jni_generator/SampleForTests.java')
803 golden_file
= os
.path
.join(script_dir
, 'golden_sample_for_tests_jni.h')
804 golden_content
= file(golden_file
).read()
805 jni_from_java
= jni_generator
.JNIFromJavaSource(
806 content
, 'org/chromium/example/jni_generator/SampleForTests',
808 generated_text
= jni_from_java
.GetContent()
809 if not self
.compareText(golden_content
, generated_text
):
810 if os
.environ
.get(REBASELINE_ENV
):
811 with
file(golden_file
, 'w') as f
:
812 f
.write(generated_text
)
814 self
.fail('testJniSelfDocumentingExample')
816 def testNoWrappingPreprocessorLines(self
):
818 package com.google.lookhowextremelylongiam.snarf.icankeepthisupallday;
820 class ReallyLongClassNamesAreAllTheRage {
821 private static native int nativeTest();
824 jni_from_java
= jni_generator
.JNIFromJavaSource(
825 test_data
, ('com/google/lookhowextremelylongiam/snarf/'
826 'icankeepthisupallday/ReallyLongClassNamesAreAllTheRage'),
828 jni_lines
= jni_from_java
.GetContent().split('\n')
829 line
= filter(lambda line
: line
.lstrip().startswith('#ifndef'),
831 self
.assertTrue(len(line
) > 80,
832 ('Expected #ifndef line to be > 80 chars: ', line
))
834 def testJarJarRemapping(self
):
836 package org.chromium.example.jni_generator;
838 import org.chromium.example2.Test;
840 import org.chromium.example3.PrefixFoo;
841 import org.chromium.example3.Prefix;
842 import org.chromium.example3.Bar$Inner;
845 private static native void nativeTest(Test t);
846 private static native void nativeTest2(PrefixFoo t);
847 private static native void nativeTest3(Prefix t);
848 private static native void nativeTest4(Bar$Inner t);
851 jni_generator
.JniParams
.SetJarJarMappings(
852 """rule org.chromium.example.** com.test.@1
853 rule org.chromium.example2.** org.test2.@1
854 rule org.chromium.example3.Prefix org.test3.Test
855 rule org.chromium.example3.Bar$** org.test3.TestBar$@1""")
856 jni_from_java
= jni_generator
.JNIFromJavaSource(
857 test_data
, 'org/chromium/example/jni_generator/Example', TestOptions())
858 jni_generator
.JniParams
.SetJarJarMappings('')
859 self
.assertGoldenTextEquals(jni_from_java
.GetContent())
861 def testImports(self
):
863 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
864 // Use of this source code is governed by a BSD-style license that can be
865 // found in the LICENSE file.
867 package org.chromium.content.app;
869 import android.app.Service;
870 import android.content.Context;
871 import android.content.Intent;
872 import android.graphics.SurfaceTexture;
873 import android.os.Bundle;
874 import android.os.IBinder;
875 import android.os.ParcelFileDescriptor;
876 import android.os.Process;
877 import android.os.RemoteException;
878 import android.util.Log;
879 import android.view.Surface;
881 import java.util.ArrayList;
883 import org.chromium.base.CalledByNative;
884 import org.chromium.base.JNINamespace;
885 import org.chromium.content.app.ContentMain;
886 import org.chromium.content.browser.SandboxedProcessConnection;
887 import org.chromium.content.common.ISandboxedProcessCallback;
888 import org.chromium.content.common.ISandboxedProcessService;
889 import org.chromium.content.common.WillNotRaise.AnException;
890 import org.chromium.content.common.WillRaise.AnException;
892 import static org.chromium.Bar.Zoo;
895 public static class BookmarkNode implements Parcelable {
897 public interface PasswordListObserver {
901 jni_generator
.JniParams
.SetFullyQualifiedClass(
902 'org/chromium/content/app/Foo')
903 jni_generator
.JniParams
.ExtractImportsAndInnerClasses(import_header
)
904 self
.assertTrue('Lorg/chromium/content/common/ISandboxedProcessService' in
905 jni_generator
.JniParams
._imports
)
906 self
.assertTrue('Lorg/chromium/Bar/Zoo' in
907 jni_generator
.JniParams
._imports
)
908 self
.assertTrue('Lorg/chromium/content/app/Foo$BookmarkNode' in
909 jni_generator
.JniParams
._inner
_classes
)
910 self
.assertTrue('Lorg/chromium/content/app/Foo$PasswordListObserver' in
911 jni_generator
.JniParams
._inner
_classes
)
912 self
.assertEquals('Lorg/chromium/content/app/ContentMain$Inner;',
913 jni_generator
.JniParams
.JavaToJni('ContentMain.Inner'))
914 self
.assertRaises(SyntaxError,
915 jni_generator
.JniParams
.JavaToJni
,
918 def testJniParamsJavaToJni(self
):
919 self
.assertTextEquals('I', JniParams
.JavaToJni('int'))
920 self
.assertTextEquals('[B', JniParams
.JavaToJni('byte[]'))
921 self
.assertTextEquals(
922 '[Ljava/nio/ByteBuffer;', JniParams
.JavaToJni('java/nio/ByteBuffer[]'))
924 def testNativesLong(self
):
925 test_options
= TestOptions()
926 test_options
.ptr_type
= 'long'
928 private native void nativeDestroy(long nativeChromeBrowserProvider);
930 jni_generator
.JniParams
.ExtractImportsAndInnerClasses(test_data
)
931 natives
= jni_generator
.ExtractNatives(test_data
, test_options
.ptr_type
)
933 NativeMethod(return_type
='void', static
=False, name
='Destroy',
934 params
=[Param(datatype
='long',
935 name
='nativeChromeBrowserProvider')],
936 java_class_name
=None,
938 p0_type
='ChromeBrowserProvider',
939 ptr_type
=test_options
.ptr_type
),
941 self
.assertListEquals(golden_natives
, natives
)
942 h
= jni_generator
.InlHeaderFileGenerator('', 'org/chromium/TestJni',
943 natives
, [], [], test_options
)
944 self
.assertGoldenTextEquals(h
.GetContent())
946 def testPureNativeMethodsOption(self
):
948 package org.chromium.example.jni_generator;
950 /** The pointer to the native Test. */
954 private static native long nativeMethod(long nativeTest, int arg1);
957 options
= TestOptions()
958 options
.pure_native_methods
= True
959 jni_from_java
= jni_generator
.JNIFromJavaSource(
960 test_data
, 'org/chromium/example/jni_generator/Test', options
)
961 self
.assertGoldenTextEquals(jni_from_java
.GetContent())
963 def testJNIInitNativeNameOption(self
):
965 package org.chromium.example.jni_generator;
967 /** The pointer to the native Test. */
971 private static native boolean nativeInitNativeClass();
972 private static native int nativeMethod(long nativeTest, int arg1);
975 options
= TestOptions()
976 options
.jni_init_native_name
= 'nativeInitNativeClass'
977 jni_from_java
= jni_generator
.JNIFromJavaSource(
978 test_data
, 'org/chromium/example/jni_generator/Test', options
)
979 self
.assertGoldenTextEquals(jni_from_java
.GetContent())
981 def testEagerCalledByNativesOption(self
):
983 package org.chromium.example.jni_generator;
985 /** The pointer to the native Test. */
989 private static native boolean nativeInitNativeClass();
990 private static native int nativeMethod(long nativeTest, int arg1);
992 private void testMethodWithParam(int iParam);
994 private static int testStaticMethodWithParam(int iParam);
996 private static double testMethodWithNoParam();
998 private static String testStaticMethodWithNoParam();
1001 options
= TestOptions()
1002 options
.jni_init_native_name
= 'nativeInitNativeClass'
1003 options
.eager_called_by_natives
= True
1004 jni_from_java
= jni_generator
.JNIFromJavaSource(
1005 test_data
, 'org/chromium/example/jni_generator/Test', options
)
1006 self
.assertGoldenTextEquals(jni_from_java
.GetContent())
1008 def testNativeExportsOption(self
):
1010 package org.chromium.example.jni_generator;
1012 /** The pointer to the native Test. */
1016 private static native boolean nativeInitNativeClass();
1017 private static native int nativeStaticMethod(long nativeTest, int arg1);
1018 private native int nativeMethod(long nativeTest, int arg1);
1020 private void testMethodWithParam(int iParam);
1022 private String testMethodWithParamAndReturn(int iParam);
1024 private static int testStaticMethodWithParam(int iParam);
1026 private static double testMethodWithNoParam();
1028 private static String testStaticMethodWithNoParam();
1030 class MyInnerClass {
1031 @NativeCall("MyInnerClass")
1032 private native int nativeInit();
1034 class MyOtherInnerClass {
1035 @NativeCall("MyOtherInnerClass")
1036 private native int nativeInit();
1040 options
= TestOptions()
1041 options
.jni_init_native_name
= 'nativeInitNativeClass'
1042 options
.native_exports
= True
1043 jni_from_java
= jni_generator
.JNIFromJavaSource(
1044 test_data
, 'org/chromium/example/jni_generator/SampleForTests', options
)
1045 self
.assertGoldenTextEquals(jni_from_java
.GetContent())
1047 def testOuterInnerRaises(self
):
1049 package org.chromium.media;
1052 static int getCaptureFormatWidth(VideoCapture.CaptureFormat format) {
1053 return format.getWidth();
1057 jni_generator
.JNIFromJavaSource(
1059 'org/chromium/media/VideoCaptureFactory',
1061 self
.assertRaises(SyntaxError, willRaise
)
1063 def testImplicitImport(self
):
1065 package org.chromium.android_webview;
1070 private static void clientCertificatesCleared(Runnable callback) {
1071 if (callbaback == null) return;
1075 def generate(import_clause
):
1076 jni_generator
.JNIFromJavaSource(
1077 test_data
% {'IMPORT': import_clause
},
1078 'org/chromium/android_webview/AwContentStatics',
1080 # Ensure it raises without the import.
1081 self
.assertRaises(SyntaxError, lambda: generate(''))
1083 # Ensure it's fine with the import.
1084 generate('import java.lang.Runnable;')
1086 def testSingleJNIAdditionalImport(self
):
1088 package org.chromium.foo;
1090 @JNIAdditionalImport(Bar.class)
1094 private static void calledByNative(Bar.Callback callback) {
1097 private static native void nativeDoSomething(Bar.Callback callback);
1100 jni_from_java
= jni_generator
.JNIFromJavaSource(test_data
,
1101 'org/chromium/foo/Foo',
1103 self
.assertGoldenTextEquals(jni_from_java
.GetContent())
1105 def testMultipleJNIAdditionalImport(self
):
1107 package org.chromium.foo;
1109 @JNIAdditionalImport({Bar1.class, Bar2.class})
1113 private static void calledByNative(Bar1.Callback callback1,
1114 Bar2.Callback callback2) {
1117 private static native void nativeDoSomething(Bar1.Callback callback1,
1118 Bar2.Callback callback2);
1121 jni_from_java
= jni_generator
.JNIFromJavaSource(test_data
,
1122 'org/chromium/foo/Foo',
1124 self
.assertGoldenTextEquals(jni_from_java
.GetContent())
1127 if __name__
== '__main__':