def testEagerCalledByNativesOption(self): test_data = """ package org.chromium.example.jni_generator; /** The pointer to the native Test. */ long nativeTest; class Test { private static native boolean nativeInitNativeClass(); private static native int nativeMethod(long nativeTest, int arg1); @CalledByNative private void testMethodWithParam(int iParam); @CalledByNative private static int testStaticMethodWithParam(int iParam); @CalledByNative private static double testMethodWithNoParam(); @CalledByNative private static String testStaticMethodWithNoParam(); } """ options = TestOptions() options.jni_init_native_name = 'nativeInitNativeClass' options.eager_called_by_natives = True jni_from_java = jni_generator.JNIFromJavaSource( test_data, 'org/chromium/example/jni_generator/Test', options) self.assertGoldenTextEquals(jni_from_java.GetContent())
def testJarJarRemapping(self): test_data = """ package org.chromium.example.jni_generator; import org.chromium.example2.Test; import org.chromium.example3.PrefixFoo; import org.chromium.example3.Prefix; import org.chromium.example3.Bar$Inner; class Example { private static native void nativeTest(Test t); private static native void nativeTest2(PrefixFoo t); private static native void nativeTest3(Prefix t); private static native void nativeTest4(Bar$Inner t); } """ jni_generator.JniParams.SetJarJarMappings( """rule org.chromium.example.** com.test.@1 rule org.chromium.example2.** org.test2.@1 rule org.chromium.example3.Prefix org.test3.Test rule org.chromium.example3.Bar$** org.test3.TestBar$@1""") jni_from_java = jni_generator.JNIFromJavaSource( test_data, 'org/chromium/example/jni_generator/Example', TestOptions()) jni_generator.JniParams.SetJarJarMappings('') self.assertGoldenTextEquals(jni_from_java.GetContent())
def testJniSelfDocumentingExample(self): script_dir = os.path.dirname(sys.argv[0]) content = file(os.path.join(script_dir, 'SampleForTests.java')).read() golden_content = file( os.path.join(script_dir, 'golden_sample_for_tests_jni.h')).read() jni_from_java = jni_generator.JNIFromJavaSource( content, 'org/chromium/example/jni_generator/SampleForTests') self.assertTextEquals(golden_content, jni_from_java.GetContent())
def testNonMainDexFile(self): test_data = """ package org.chromium.example.jni_generator; class Test { private static native int nativeStaticMethod(long nativeTest, int arg1); } """ options = TestOptions() jni_from_java = jni_generator.JNIFromJavaSource( test_data, 'org/chromium/foo/Bar', options) self.assertGoldenTextEquals(jni_from_java.GetContent())
def testPureNativeMethodsOption(self): test_data = """ package org.chromium.example.jni_generator; /** The pointer to the native Test. */ long nativeTest; class Test { private static native long nativeMethod(long nativeTest, int arg1); } """ options = TestOptions() options.pure_native_methods = True jni_from_java = jni_generator.JNIFromJavaSource( test_data, 'org/chromium/example/jni_generator/Test', options) self.assertGoldenTextEquals(jni_from_java.GetContent())
def testNoWrappingPreprocessorLines(self): test_data = """ package com.google.lookhowextremelylongiam.snarf.icankeepthisupallday; class ReallyLongClassNamesAreAllTheRage { private static native int nativeTest(); } """ jni_from_java = jni_generator.JNIFromJavaSource( test_data, ('com/google/lookhowextremelylongiam/snarf/' 'icankeepthisupallday/ReallyLongClassNamesAreAllTheRage')) jni_lines = jni_from_java.GetContent().split('\n') line = filter(lambda line: line.lstrip().startswith('#ifndef'), jni_lines)[0] self.assertTrue(len(line) > 80, ('Expected #ifndef line to be > 80 chars: ', line))
def testJniSelfDocumentingExample(self): script_dir = os.path.dirname(sys.argv[0]) content = file(os.path.join(script_dir, 'java/src/org/chromium/example/jni_generator/SampleForTests.java') ).read() golden_file = os.path.join(script_dir, 'SampleForTests_jni.golden') golden_content = file(golden_file).read() jni_from_java = jni_generator.JNIFromJavaSource( content, 'org/chromium/example/jni_generator/SampleForTests', TestOptions()) generated_text = jni_from_java.GetContent() if not self.compareText(golden_content, generated_text): if os.environ.get(REBASELINE_ENV): with file(golden_file, 'w') as f: f.write(generated_text) return self.fail('testJniSelfDocumentingExample')
def testSingleJNIAdditionalImport(self): test_data = """ package org.chromium.foo; @JNIAdditionalImport(Bar.class) class Foo { @CalledByNative private static void calledByNative(Bar.Callback callback) { } private static native void nativeDoSomething(Bar.Callback callback); } """ jni_from_java = jni_generator.JNIFromJavaSource( test_data, 'org/chromium/foo/Foo', TestOptions()) self.assertGoldenTextEquals(jni_from_java.GetContent())
def testJNIInitNativeNameOption(self): test_data = """ package org.chromium.example.jni_generator; /** The pointer to the native Test. */ long nativeTest; class Test { private static native boolean nativeInitNativeClass(); private static native int nativeMethod(long nativeTest, int arg1); } """ options = TestOptions() options.jni_init_native_name = 'nativeInitNativeClass' jni_from_java = jni_generator.JNIFromJavaSource( test_data, 'org/chromium/example/jni_generator/Test', options) self.assertGoldenTextEquals(jni_from_java.GetContent())
def testREForNatives(self): # We should not match "native SyncSetupFlow" inside the comment. test_data = """ /** * Invoked when the setup process is complete so we can disconnect from the * native-side SyncSetupFlowHandler. */ public void destroy() { Log.v(TAG, "Destroying native SyncSetupFlow"); if (mNativeSyncSetupFlow != 0) { nativeSyncSetupEnded(mNativeSyncSetupFlow); mNativeSyncSetupFlow = 0; } } private native void nativeSyncSetupEnded( int nativeAndroidSyncSetupFlowHandler); """ jni_from_java = jni_generator.JNIFromJavaSource(test_data, 'foo/bar')
def runNativeExportsOption(self, optional): test_data = """ package org.chromium.example.jni_generator; /** The pointer to the native Test. */ long nativeTest; class Test { private static native boolean nativeInitNativeClass(); private static native int nativeStaticMethod(long nativeTest, int arg1); private native int nativeMethod(long nativeTest, int arg1); @CalledByNative private void testMethodWithParam(int iParam); @CalledByNative private String testMethodWithParamAndReturn(int iParam); @CalledByNative private static int testStaticMethodWithParam(int iParam); @CalledByNative private static double testMethodWithNoParam(); @CalledByNative private static String testStaticMethodWithNoParam(); class MyInnerClass { @NativeCall("MyInnerClass") private native int nativeInit(); } class MyOtherInnerClass { @NativeCall("MyOtherInnerClass") private native int nativeInit(); } } """ options = TestOptions() options.jni_init_native_name = 'nativeInitNativeClass' options.native_exports = True options.native_exports_optional = optional jni_from_java = jni_generator.JNIFromJavaSource( test_data, 'org/chromium/example/jni_generator/SampleForTests', options) return jni_from_java.GetContent()
def testTracing(self): test_data = """ package org.chromium.foo; @JNINamespace("org::chromium_foo") class Foo { @CalledByNative Foo(); @CalledByNative void callbackFromNative(); native void nativeInstanceMethod(long nativeInstance); static native void nativeStaticMethod(); } """ options_with_tracing = TestOptions() options_with_tracing.enable_tracing = True jni_from_java = jni_generator.JNIFromJavaSource( test_data, 'org/chromium/foo/Foo', options_with_tracing) self.assertGoldenTextEquals(jni_from_java.GetContent())
def testNativeExportsOnlyOption(self): test_data = """ package org.chromium.example.jni_generator; /** The pointer to the native Test. */ long nativeTest; class Test { private static native int nativeStaticMethod(long nativeTest, int arg1); private native int nativeMethod(long nativeTest, int arg1); @CalledByNative private void testMethodWithParam(int iParam); @CalledByNative private String testMethodWithParamAndReturn(int iParam); @CalledByNative private static int testStaticMethodWithParam(int iParam); @CalledByNative private static double testMethodWithNoParam(); @CalledByNative private static String testStaticMethodWithNoParam(); class MyInnerClass { @NativeCall("MyInnerClass") private native int nativeInit(); } class MyOtherInnerClass { @NativeCall("MyOtherInnerClass") private native int nativeInit(); } } """ options = TestOptions() options.native_exports_optional = False jni_from_java = jni_generator.JNIFromJavaSource( test_data, 'org/chromium/example/jni_generator/SampleForTests', options) self.assertGoldenTextEquals(jni_from_java.GetContent())
def willRaise(): jni_generator.JNIFromJavaSource( test_data, 'org/chromium/media/VideoCaptureFactory', TestOptions())
def generate(import_clause): jni_generator.JNIFromJavaSource( test_data % {'IMPORT': import_clause}, 'org/chromium/android_webview/AwContentStatics', TestOptions())