| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """Tests for jni_generator.py. | 6 """Tests for jni_generator.py. |
| 7 | 7 |
| 8 This test suite contains various tests for the JNI generator. | 8 This test suite contains various tests for the JNI generator. |
| 9 It exercises the low-level parser all the way up to the | 9 It exercises the low-level parser all the way up to the |
| 10 code generator and ensures the output matches a golden | 10 code generator and ensures the output matches a golden |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 for line in difflib.context_diff(stripped_golden, stripped_generated): | 49 for line in difflib.context_diff(stripped_golden, stripped_generated): |
| 50 print line | 50 print line |
| 51 print '\n\nGenerated' | 51 print '\n\nGenerated' |
| 52 print '=' * 80 | 52 print '=' * 80 |
| 53 print generated_text | 53 print generated_text |
| 54 print '=' * 80 | 54 print '=' * 80 |
| 55 self.fail('Golden text mismatch') | 55 self.fail('Golden text mismatch') |
| 56 | 56 |
| 57 def testNatives(self): | 57 def testNatives(self): |
| 58 test_data = """" | 58 test_data = """" |
| 59 interface OnFrameAvailableListener {} |
| 59 private native int nativeInit(); | 60 private native int nativeInit(); |
| 60 private native void nativeDestroy(int nativeChromeBrowserProvider); | 61 private native void nativeDestroy(int nativeChromeBrowserProvider); |
| 61 private native long nativeAddBookmark( | 62 private native long nativeAddBookmark( |
| 62 int nativeChromeBrowserProvider, | 63 int nativeChromeBrowserProvider, |
| 63 String url, String title, boolean isFolder, long parentId); | 64 String url, String title, boolean isFolder, long parentId); |
| 64 private static native String nativeGetDomainAndRegistry(String url); | 65 private static native String nativeGetDomainAndRegistry(String url); |
| 65 private static native void nativeCreateHistoricalTabFromState( | 66 private static native void nativeCreateHistoricalTabFromState( |
| 66 byte[] state, int tab_index); | 67 byte[] state, int tab_index); |
| 67 private native byte[] nativeGetStateAsByteArray(View view); | 68 private native byte[] nativeGetStateAsByteArray(View view); |
| 68 private static native String[] nativeGetAutofillProfileGUIDs(); | 69 private static native String[] nativeGetAutofillProfileGUIDs(); |
| 69 private native void nativeSetRecognitionResults( | 70 private native void nativeSetRecognitionResults( |
| 70 int sessionId, String[] results); | 71 int sessionId, String[] results); |
| 71 private native long nativeAddBookmarkFromAPI( | 72 private native long nativeAddBookmarkFromAPI( |
| 72 int nativeChromeBrowserProvider, | 73 int nativeChromeBrowserProvider, |
| 73 String url, Long created, Boolean isBookmark, | 74 String url, Long created, Boolean isBookmark, |
| 74 Long date, byte[] favicon, String title, Integer visits); | 75 Long date, byte[] favicon, String title, Integer visits); |
| 75 native int nativeFindAll(String find); | 76 native int nativeFindAll(String find); |
| 76 private static native OnFrameAvailableListener nativeGetInnerClass(); | 77 private static native OnFrameAvailableListener nativeGetInnerClass(); |
| 77 private native Bitmap nativeQueryBitmap( | 78 private native Bitmap nativeQueryBitmap( |
| 78 int nativeChromeBrowserProvider, | 79 int nativeChromeBrowserProvider, |
| 79 String[] projection, String selection, | 80 String[] projection, String selection, |
| 80 String[] selectionArgs, String sortOrder); | 81 String[] selectionArgs, String sortOrder); |
| 81 private native void nativeGotOrientation( | 82 private native void nativeGotOrientation( |
| 82 int nativeDataFetcherImplAndroid, | 83 int nativeDataFetcherImplAndroid, |
| 83 double alpha, double beta, double gamma); | 84 double alpha, double beta, double gamma); |
| 84 """ | 85 """ |
| 86 jni_generator.JniParams.ExtractImportsAndInnerClasses(test_data) |
| 85 natives = jni_generator.ExtractNatives(test_data) | 87 natives = jni_generator.ExtractNatives(test_data) |
| 86 golden_natives = [ | 88 golden_natives = [ |
| 87 NativeMethod(return_type='int', static=False, | 89 NativeMethod(return_type='int', static=False, |
| 88 name='Init', | 90 name='Init', |
| 89 params=[], | 91 params=[], |
| 90 java_class_name=None, | 92 java_class_name=None, |
| 91 type='function'), | 93 type='function'), |
| 92 NativeMethod(return_type='void', static=False, name='Destroy', | 94 NativeMethod(return_type='void', static=False, name='Destroy', |
| 93 params=[Param(datatype='int', | 95 params=[Param(datatype='int', |
| 94 name='nativeChromeBrowserProvider')], | 96 name='nativeChromeBrowserProvider')], |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 ")" | 384 ")" |
| 383 "J", reinterpret_cast<void*>(AddBookmarkFromAPI) }, | 385 "J", reinterpret_cast<void*>(AddBookmarkFromAPI) }, |
| 384 { "nativeFindAll", | 386 { "nativeFindAll", |
| 385 "(" | 387 "(" |
| 386 "Ljava/lang/String;" | 388 "Ljava/lang/String;" |
| 387 ")" | 389 ")" |
| 388 "I", reinterpret_cast<void*>(FindAll) }, | 390 "I", reinterpret_cast<void*>(FindAll) }, |
| 389 { "nativeGetInnerClass", | 391 { "nativeGetInnerClass", |
| 390 "(" | 392 "(" |
| 391 ")" | 393 ")" |
| 392 "Landroid/graphics/SurfaceTexture$OnFrameAvailableListener;", | 394 "Lorg/chromium/example/jni_generator/SampleForTests$OnFrameAvailableListener;", |
| 393 reinterpret_cast<void*>(GetInnerClass) }, | 395 reinterpret_cast<void*>(GetInnerClass) }, |
| 394 { "nativeQueryBitmap", | 396 { "nativeQueryBitmap", |
| 395 "(" | 397 "(" |
| 396 "I" | 398 "I" |
| 397 "[Ljava/lang/String;" | 399 "[Ljava/lang/String;" |
| 398 "Ljava/lang/String;" | 400 "Ljava/lang/String;" |
| 399 "[Ljava/lang/String;" | 401 "[Ljava/lang/String;" |
| 400 "Ljava/lang/String;" | 402 "Ljava/lang/String;" |
| 401 ")" | 403 ")" |
| 402 "Landroid/graphics/Bitmap;", reinterpret_cast<void*>(QueryBitmap) }, | 404 "Landroid/graphics/Bitmap;", reinterpret_cast<void*>(QueryBitmap) }, |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 711 | 713 |
| 712 return true; | 714 return true; |
| 713 } | 715 } |
| 714 | 716 |
| 715 #endif // org_chromium_TestJni_JNI | 717 #endif // org_chromium_TestJni_JNI |
| 716 """ | 718 """ |
| 717 self.assertTextEquals(golden_content, h.GetContent()) | 719 self.assertTextEquals(golden_content, h.GetContent()) |
| 718 | 720 |
| 719 def testCalledByNatives(self): | 721 def testCalledByNatives(self): |
| 720 test_data = """" | 722 test_data = """" |
| 723 import android.graphics.Bitmap; |
| 724 import android.view.View; |
| 725 import java.io.InputStream; |
| 726 |
| 727 class InnerClass {} |
| 728 |
| 721 @CalledByNative | 729 @CalledByNative |
| 722 OnFrameAvailableListener showConfirmInfoBar(int nativeInfoBar, | 730 InnerClass showConfirmInfoBar(int nativeInfoBar, |
| 723 String buttonOk, String buttonCancel, String title, Bitmap icon) { | 731 String buttonOk, String buttonCancel, String title, Bitmap icon) { |
| 724 InfoBar infobar = new ConfirmInfoBar(nativeInfoBar, mContext, | 732 InfoBar infobar = new ConfirmInfoBar(nativeInfoBar, mContext, |
| 725 buttonOk, buttonCancel, | 733 buttonOk, buttonCancel, |
| 726 title, icon); | 734 title, icon); |
| 727 return infobar; | 735 return infobar; |
| 728 } | 736 } |
| 729 @CalledByNative | 737 @CalledByNative |
| 730 OnFrameAvailableListener showAutoLoginInfoBar(int nativeInfoBar, | 738 InnerClass showAutoLoginInfoBar(int nativeInfoBar, |
| 731 String realm, String account, String args) { | 739 String realm, String account, String args) { |
| 732 AutoLoginInfoBar infobar = new AutoLoginInfoBar(nativeInfoBar, mContext, | 740 AutoLoginInfoBar infobar = new AutoLoginInfoBar(nativeInfoBar, mContext, |
| 733 realm, account, args); | 741 realm, account, args); |
| 734 if (infobar.displayedAccountCount() == 0) | 742 if (infobar.displayedAccountCount() == 0) |
| 735 infobar = null; | 743 infobar = null; |
| 736 return infobar; | 744 return infobar; |
| 737 } | 745 } |
| 738 @CalledByNative("InfoBar") | 746 @CalledByNative("InfoBar") |
| 739 void dismiss(); | 747 void dismiss(); |
| 740 @SuppressWarnings("unused") | 748 @SuppressWarnings("unused") |
| (...skipping 14 matching lines...) Expand all Loading... |
| 755 private void activateHardwareAcceleration(final boolean activated, | 763 private void activateHardwareAcceleration(final boolean activated, |
| 756 final int iPid, final int iType, | 764 final int iPid, final int iType, |
| 757 final int iPrimaryID, final int iSecondaryID) { | 765 final int iPrimaryID, final int iSecondaryID) { |
| 758 if (!activated) { | 766 if (!activated) { |
| 759 return | 767 return |
| 760 } | 768 } |
| 761 } | 769 } |
| 762 @CalledByNativeUnchecked | 770 @CalledByNativeUnchecked |
| 763 private void uncheckedCall(int iParam); | 771 private void uncheckedCall(int iParam); |
| 764 """ | 772 """ |
| 773 jni_generator.JniParams.SetFullyQualifiedClass('org/chromium/Foo') |
| 774 jni_generator.JniParams.ExtractImportsAndInnerClasses(test_data) |
| 765 called_by_natives = jni_generator.ExtractCalledByNatives(test_data) | 775 called_by_natives = jni_generator.ExtractCalledByNatives(test_data) |
| 766 golden_called_by_natives = [ | 776 golden_called_by_natives = [ |
| 767 CalledByNative( | 777 CalledByNative( |
| 768 return_type='OnFrameAvailableListener', | 778 return_type='InnerClass', |
| 769 system_class=False, | 779 system_class=False, |
| 770 static=False, | 780 static=False, |
| 771 name='showConfirmInfoBar', | 781 name='showConfirmInfoBar', |
| 772 method_id_var_name='showConfirmInfoBar', | 782 method_id_var_name='showConfirmInfoBar', |
| 773 java_class_name='', | 783 java_class_name='', |
| 774 params=[Param(datatype='int', name='nativeInfoBar'), | 784 params=[Param(datatype='int', name='nativeInfoBar'), |
| 775 Param(datatype='String', name='buttonOk'), | 785 Param(datatype='String', name='buttonOk'), |
| 776 Param(datatype='String', name='buttonCancel'), | 786 Param(datatype='String', name='buttonCancel'), |
| 777 Param(datatype='String', name='title'), | 787 Param(datatype='String', name='title'), |
| 778 Param(datatype='Bitmap', name='icon')], | 788 Param(datatype='Bitmap', name='icon')], |
| 779 env_call=('Object', ''), | 789 env_call=('Object', ''), |
| 780 unchecked=False, | 790 unchecked=False, |
| 781 ), | 791 ), |
| 782 CalledByNative( | 792 CalledByNative( |
| 783 return_type='OnFrameAvailableListener', | 793 return_type='InnerClass', |
| 784 system_class=False, | 794 system_class=False, |
| 785 static=False, | 795 static=False, |
| 786 name='showAutoLoginInfoBar', | 796 name='showAutoLoginInfoBar', |
| 787 method_id_var_name='showAutoLoginInfoBar', | 797 method_id_var_name='showAutoLoginInfoBar', |
| 788 java_class_name='', | 798 java_class_name='', |
| 789 params=[Param(datatype='int', name='nativeInfoBar'), | 799 params=[Param(datatype='int', name='nativeInfoBar'), |
| 790 Param(datatype='String', name='realm'), | 800 Param(datatype='String', name='realm'), |
| 791 Param(datatype='String', name='account'), | 801 Param(datatype='String', name='account'), |
| 792 Param(datatype='String', name='args')], | 802 Param(datatype='String', name='args')], |
| 793 env_call=('Object', ''), | 803 env_call=('Object', ''), |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 909 env, g_TestJni_clazz, | 919 env, g_TestJni_clazz, |
| 910 "showConfirmInfoBar", | 920 "showConfirmInfoBar", |
| 911 | 921 |
| 912 "(" | 922 "(" |
| 913 "I" | 923 "I" |
| 914 "Ljava/lang/String;" | 924 "Ljava/lang/String;" |
| 915 "Ljava/lang/String;" | 925 "Ljava/lang/String;" |
| 916 "Ljava/lang/String;" | 926 "Ljava/lang/String;" |
| 917 "Landroid/graphics/Bitmap;" | 927 "Landroid/graphics/Bitmap;" |
| 918 ")" | 928 ")" |
| 919 "Landroid/graphics/SurfaceTexture$OnFrameAvailableListener;", | 929 "Lorg/chromium/Foo$InnerClass;", |
| 920 &g_TestJni_showConfirmInfoBar); | 930 &g_TestJni_showConfirmInfoBar); |
| 921 | 931 |
| 922 jobject ret = | 932 jobject ret = |
| 923 env->CallObjectMethod(obj, | 933 env->CallObjectMethod(obj, |
| 924 method_id, nativeInfoBar, buttonOk, buttonCancel, title, icon); | 934 method_id, nativeInfoBar, buttonOk, buttonCancel, title, icon); |
| 925 base::android::CheckException(env); | 935 base::android::CheckException(env); |
| 926 return ScopedJavaLocalRef<jobject>(env, ret); | 936 return ScopedJavaLocalRef<jobject>(env, ret); |
| 927 } | 937 } |
| 928 | 938 |
| 929 static base::subtle::AtomicWord g_TestJni_showAutoLoginInfoBar = 0; | 939 static base::subtle::AtomicWord g_TestJni_showAutoLoginInfoBar = 0; |
| 930 static ScopedJavaLocalRef<jobject> Java_TestJni_showAutoLoginInfoBar(JNIEnv* | 940 static ScopedJavaLocalRef<jobject> Java_TestJni_showAutoLoginInfoBar(JNIEnv* |
| 931 env, jobject obj, jint nativeInfoBar, | 941 env, jobject obj, jint nativeInfoBar, |
| 932 jstring realm, | 942 jstring realm, |
| 933 jstring account, | 943 jstring account, |
| 934 jstring args) { | 944 jstring args) { |
| 935 /* Must call RegisterNativesImpl() */ | 945 /* Must call RegisterNativesImpl() */ |
| 936 DCHECK(g_TestJni_clazz); | 946 DCHECK(g_TestJni_clazz); |
| 937 jmethodID method_id = | 947 jmethodID method_id = |
| 938 base::android::MethodID::LazyGet< | 948 base::android::MethodID::LazyGet< |
| 939 base::android::MethodID::TYPE_INSTANCE>( | 949 base::android::MethodID::TYPE_INSTANCE>( |
| 940 env, g_TestJni_clazz, | 950 env, g_TestJni_clazz, |
| 941 "showAutoLoginInfoBar", | 951 "showAutoLoginInfoBar", |
| 942 | 952 |
| 943 "(" | 953 "(" |
| 944 "I" | 954 "I" |
| 945 "Ljava/lang/String;" | 955 "Ljava/lang/String;" |
| 946 "Ljava/lang/String;" | 956 "Ljava/lang/String;" |
| 947 "Ljava/lang/String;" | 957 "Ljava/lang/String;" |
| 948 ")" | 958 ")" |
| 949 "Landroid/graphics/SurfaceTexture$OnFrameAvailableListener;", | 959 "Lorg/chromium/Foo$InnerClass;", |
| 950 &g_TestJni_showAutoLoginInfoBar); | 960 &g_TestJni_showAutoLoginInfoBar); |
| 951 | 961 |
| 952 jobject ret = | 962 jobject ret = |
| 953 env->CallObjectMethod(obj, | 963 env->CallObjectMethod(obj, |
| 954 method_id, nativeInfoBar, realm, account, args); | 964 method_id, nativeInfoBar, realm, account, args); |
| 955 base::android::CheckException(env); | 965 base::android::CheckException(env); |
| 956 return ScopedJavaLocalRef<jobject>(env, ret); | 966 return ScopedJavaLocalRef<jobject>(env, ret); |
| 957 } | 967 } |
| 958 | 968 |
| 959 static base::subtle::AtomicWord g_InfoBar_dismiss = 0; | 969 static base::subtle::AtomicWord g_InfoBar_dismiss = 0; |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1473 if (mNativeSyncSetupFlow != 0) { | 1483 if (mNativeSyncSetupFlow != 0) { |
| 1474 nativeSyncSetupEnded(mNativeSyncSetupFlow); | 1484 nativeSyncSetupEnded(mNativeSyncSetupFlow); |
| 1475 mNativeSyncSetupFlow = 0; | 1485 mNativeSyncSetupFlow = 0; |
| 1476 } | 1486 } |
| 1477 } | 1487 } |
| 1478 private native void nativeSyncSetupEnded( | 1488 private native void nativeSyncSetupEnded( |
| 1479 int nativeAndroidSyncSetupFlowHandler); | 1489 int nativeAndroidSyncSetupFlowHandler); |
| 1480 """ | 1490 """ |
| 1481 jni_from_java = jni_generator.JNIFromJavaSource(test_data, 'foo/bar') | 1491 jni_from_java = jni_generator.JNIFromJavaSource(test_data, 'foo/bar') |
| 1482 | 1492 |
| 1483 def testRaisesOnUnknownDatatype(self): | |
| 1484 test_data = """ | |
| 1485 class MyInnerClass { | |
| 1486 private native int nativeInit(AnUnknownDatatype p0); | |
| 1487 } | |
| 1488 """ | |
| 1489 self.assertRaises(SyntaxError, | |
| 1490 jni_generator.JNIFromJavaSource, | |
| 1491 test_data, 'foo/bar') | |
| 1492 | |
| 1493 def testRaisesOnNonJNIMethod(self): | 1493 def testRaisesOnNonJNIMethod(self): |
| 1494 test_data = """ | 1494 test_data = """ |
| 1495 class MyInnerClass { | 1495 class MyInnerClass { |
| 1496 private int Foo(int p0) { | 1496 private int Foo(int p0) { |
| 1497 } | 1497 } |
| 1498 } | 1498 } |
| 1499 """ | 1499 """ |
| 1500 self.assertRaises(SyntaxError, | 1500 self.assertRaises(SyntaxError, |
| 1501 jni_generator.JNIFromJavaSource, | 1501 jni_generator.JNIFromJavaSource, |
| 1502 test_data, 'foo/bar') | 1502 test_data, 'foo/bar') |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1520 """ | 1520 """ |
| 1521 jni_from_java = jni_generator.JNIFromJavaSource( | 1521 jni_from_java = jni_generator.JNIFromJavaSource( |
| 1522 test_data, ('com/google/lookhowextremelylongiam/snarf/' | 1522 test_data, ('com/google/lookhowextremelylongiam/snarf/' |
| 1523 'icankeepthisupallday/ReallyLongClassNamesAreAllTheRage')) | 1523 'icankeepthisupallday/ReallyLongClassNamesAreAllTheRage')) |
| 1524 jni_lines = jni_from_java.GetContent().split('\n') | 1524 jni_lines = jni_from_java.GetContent().split('\n') |
| 1525 line = filter(lambda line: line.lstrip().startswith('#ifndef'), | 1525 line = filter(lambda line: line.lstrip().startswith('#ifndef'), |
| 1526 jni_lines)[0] | 1526 jni_lines)[0] |
| 1527 self.assertTrue(len(line) > 80, | 1527 self.assertTrue(len(line) > 80, |
| 1528 ('Expected #ifndef line to be > 80 chars: ', line)) | 1528 ('Expected #ifndef line to be > 80 chars: ', line)) |
| 1529 | 1529 |
| 1530 def testExternalParamList(self): | 1530 def testImports(self): |
| 1531 script_dir = os.path.dirname(sys.argv[0]) | 1531 import_header = """ |
| 1532 external_param_list = [os.path.join(script_dir, 'class_list.jni')] | 1532 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 1533 jni_generator.JniParams.ReadExternalParamList(external_param_list) | 1533 // Use of this source code is governed by a BSD-style license that can be |
| 1534 self.assertTrue('Lorg/chromium/base/SystemMessageHandler' in | 1534 // found in the LICENSE file. |
| 1535 jni_generator.JniParams._external_param_list) | 1535 |
| 1536 self.assertRaises(AssertionError, | 1536 package org.chromium.content.app; |
| 1537 jni_generator.JniParams.ReadExternalParamList, | 1537 |
| 1538 external_param_list) | 1538 import android.app.Service; |
| 1539 import android.content.Context; |
| 1540 import android.content.Intent; |
| 1541 import android.graphics.SurfaceTexture; |
| 1542 import android.os.Bundle; |
| 1543 import android.os.IBinder; |
| 1544 import android.os.ParcelFileDescriptor; |
| 1545 import android.os.Process; |
| 1546 import android.os.RemoteException; |
| 1547 import android.util.Log; |
| 1548 import android.view.Surface; |
| 1549 |
| 1550 import java.util.ArrayList; |
| 1551 |
| 1552 import org.chromium.base.CalledByNative; |
| 1553 import org.chromium.base.JNINamespace; |
| 1554 import org.chromium.content.app.ContentMain; |
| 1555 import org.chromium.content.browser.SandboxedProcessConnection; |
| 1556 import org.chromium.content.common.ISandboxedProcessCallback; |
| 1557 import org.chromium.content.common.ISandboxedProcessService; |
| 1558 import org.chromium.content.common.SurfaceCallback; |
| 1559 |
| 1560 import static org.chromium.Bar.Zoo; |
| 1561 |
| 1562 class Foo { |
| 1563 public static class BookmarkNode implements Parcelable { |
| 1564 } |
| 1565 public interface PasswordListObserver { |
| 1566 } |
| 1567 } |
| 1568 """ |
| 1569 jni_generator.JniParams.SetFullyQualifiedClass( |
| 1570 'org/chromium/content/app/Foo') |
| 1571 jni_generator.JniParams.ExtractImportsAndInnerClasses(import_header) |
| 1572 self.assertTrue('Lorg/chromium/content/common/ISandboxedProcessService' in |
| 1573 jni_generator.JniParams._imports) |
| 1574 self.assertTrue('Lorg/chromium/Bar/Zoo' in |
| 1575 jni_generator.JniParams._imports) |
| 1576 self.assertTrue('Lorg/chromium/content/app/Foo$BookmarkNode' in |
| 1577 jni_generator.JniParams._inner_classes) |
| 1578 self.assertTrue('Lorg/chromium/content/app/Foo$PasswordListObserver' in |
| 1579 jni_generator.JniParams._inner_classes) |
| 1580 |
| 1539 | 1581 |
| 1540 if __name__ == '__main__': | 1582 if __name__ == '__main__': |
| 1541 unittest.main() | 1583 unittest.main() |
| OLD | NEW |