Chromium Code Reviews| 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 |
| 11 file. | 11 file. |
| 12 """ | 12 """ |
| 13 | 13 |
| 14 import difflib | 14 import difflib |
| 15 import inspect | 15 import inspect |
| 16 import os | 16 import os |
| 17 import sys | 17 import sys |
| 18 import unittest | 18 import unittest |
| 19 import jni_generator | 19 import jni_generator |
| 20 from jni_generator import CalledByNative, JniParams, NativeMethod, Param | 20 from jni_generator import CalledByNative, JniParams, NativeMethod, Param |
| 21 | 21 |
| 22 | 22 |
| 23 SCRIPT_NAME = 'base/android/jni_generator/jni_generator.py' | 23 SCRIPT_NAME = 'base/android/jni_generator/jni_generator.py' |
| 24 INCLUDES = ( | |
| 25 'base/android/jni_generator/jni_generator_helper.h' | |
| 26 ) | |
| 24 | 27 |
| 25 # Set this environment variable in order to regenerate the golden text | 28 # Set this environment variable in order to regenerate the golden text |
| 26 # files. | 29 # files. |
| 27 REBASELINE_ENV = 'REBASELINE' | 30 REBASELINE_ENV = 'REBASELINE' |
| 28 | 31 |
| 29 class TestOptions(object): | 32 class TestOptions(object): |
| 30 """The mock options object which is passed to the jni_generator.py script.""" | 33 """The mock options object which is passed to the jni_generator.py script.""" |
| 31 | 34 |
| 32 def __init__(self): | 35 def __init__(self): |
| 33 self.namespace = None | 36 self.namespace = None |
| 34 self.script_name = SCRIPT_NAME | 37 self.script_name = SCRIPT_NAME |
| 38 self.includes = INCLUDES | |
| 39 self.pure_native_methods = False | |
| 35 self.ptr_type = 'int' | 40 self.ptr_type = 'int' |
| 41 self.jni_init_native_name = None | |
| 42 self.eager_called_by_natives = False | |
| 36 | 43 |
| 37 | 44 |
| 38 class TestGenerator(unittest.TestCase): | 45 class TestGenerator(unittest.TestCase): |
| 39 def assertObjEquals(self, first, second): | 46 def assertObjEquals(self, first, second): |
| 40 dict_first = first.__dict__ | 47 dict_first = first.__dict__ |
| 41 dict_second = second.__dict__ | 48 dict_second = second.__dict__ |
| 42 self.assertEquals(dict_first.keys(), dict_second.keys()) | 49 self.assertEquals(dict_first.keys(), dict_second.keys()) |
| 43 for key, value in dict_first.iteritems(): | 50 for key, value in dict_first.iteritems(): |
| 44 if (type(value) is list and len(value) and | 51 if (type(value) is list and len(value) and |
| 45 isinstance(type(value[0]), object)): | 52 isinstance(type(value[0]), object)): |
| (...skipping 746 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 792 """ | 799 """ |
| 793 self.assertRaises(SyntaxError, | 800 self.assertRaises(SyntaxError, |
| 794 jni_generator.JNIFromJavaSource, | 801 jni_generator.JNIFromJavaSource, |
| 795 test_data, 'foo/bar', TestOptions()) | 802 test_data, 'foo/bar', TestOptions()) |
| 796 | 803 |
| 797 def testJniSelfDocumentingExample(self): | 804 def testJniSelfDocumentingExample(self): |
| 798 script_dir = os.path.dirname(sys.argv[0]) | 805 script_dir = os.path.dirname(sys.argv[0]) |
| 799 content = file(os.path.join(script_dir, | 806 content = file(os.path.join(script_dir, |
| 800 'java/src/org/chromium/example/jni_generator/SampleForTests.java') | 807 'java/src/org/chromium/example/jni_generator/SampleForTests.java') |
| 801 ).read() | 808 ).read() |
| 802 golden_content = file(os.path.join(script_dir, | 809 golden_file = os.path.join(script_dir, 'golden_sample_for_tests_jni.h') |
| 803 'golden_sample_for_tests_jni.h')).read() | 810 golden_content = file(golden_file).read() |
| 804 jni_from_java = jni_generator.JNIFromJavaSource( | 811 jni_from_java = jni_generator.JNIFromJavaSource( |
| 805 content, 'org/chromium/example/jni_generator/SampleForTests', | 812 content, 'org/chromium/example/jni_generator/SampleForTests', |
| 806 TestOptions()) | 813 TestOptions()) |
| 807 self.assertTextEquals(golden_content, jni_from_java.GetContent()) | 814 generated_text = jni_from_java.GetContent() |
| 815 try: | |
| 816 self.assertTextEquals(golden_content, generated_text) | |
|
rmcilroy
2013/12/13 14:14:51
maybe rather than a try/catch just do a
if not eq
bulach
2013/12/13 15:23:58
yeah, good point. had to change a bit to expose th
| |
| 817 except Exception: | |
| 818 if os.environ.get(REBASELINE_ENV): | |
| 819 with file(golden_file, 'w') as f: | |
| 820 f.write(generated_text) | |
| 821 return | |
| 822 raise | |
| 808 | 823 |
| 809 def testNoWrappingPreprocessorLines(self): | 824 def testNoWrappingPreprocessorLines(self): |
| 810 test_data = """ | 825 test_data = """ |
| 811 package com.google.lookhowextremelylongiam.snarf.icankeepthisupallday; | 826 package com.google.lookhowextremelylongiam.snarf.icankeepthisupallday; |
| 812 | 827 |
| 813 class ReallyLongClassNamesAreAllTheRage { | 828 class ReallyLongClassNamesAreAllTheRage { |
| 814 private static native int nativeTest(); | 829 private static native int nativeTest(); |
| 815 } | 830 } |
| 816 """ | 831 """ |
| 817 jni_from_java = jni_generator.JNIFromJavaSource( | 832 jni_from_java = jni_generator.JNIFromJavaSource( |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 920 java_class_name=None, | 935 java_class_name=None, |
| 921 type='method', | 936 type='method', |
| 922 p0_type='ChromeBrowserProvider', | 937 p0_type='ChromeBrowserProvider', |
| 923 ptr_type=test_options.ptr_type), | 938 ptr_type=test_options.ptr_type), |
| 924 ] | 939 ] |
| 925 self.assertListEquals(golden_natives, natives) | 940 self.assertListEquals(golden_natives, natives) |
| 926 h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', | 941 h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', |
| 927 natives, [], test_options) | 942 natives, [], test_options) |
| 928 self.assertGoldenTextEquals(h.GetContent()) | 943 self.assertGoldenTextEquals(h.GetContent()) |
| 929 | 944 |
| 945 def testPureNativeMethodsOption(self): | |
| 946 test_data = """ | |
| 947 package org.chromium.example.jni_generator; | |
| 948 | |
| 949 /** The pointer to the native Test. */ | |
| 950 int nativeTest; | |
| 951 | |
| 952 class Test { | |
| 953 private static native int nativeMethod(int nativeTest, int arg1); | |
| 954 } | |
| 955 """ | |
| 956 options = TestOptions() | |
| 957 options.pure_native_methods = True | |
| 958 jni_from_java = jni_generator.JNIFromJavaSource( | |
| 959 test_data, 'org/chromium/example/jni_generator/Test', options) | |
| 960 self.assertGoldenTextEquals(jni_from_java.GetContent()) | |
| 961 | |
| 962 def testJNIInitNativeNameOption(self): | |
| 963 test_data = """ | |
| 964 package org.chromium.example.jni_generator; | |
| 965 | |
| 966 /** The pointer to the native Test. */ | |
| 967 int nativeTest; | |
| 968 | |
| 969 class Test { | |
| 970 private static native boolean initNativeClass(); | |
| 971 private static native int nativeMethod(int nativeTest, int arg1); | |
| 972 } | |
| 973 """ | |
| 974 options = TestOptions() | |
| 975 options.jni_init_native_name = 'initNativeClass' | |
| 976 jni_from_java = jni_generator.JNIFromJavaSource( | |
| 977 test_data, 'org/chromium/example/jni_generator/Test', options) | |
| 978 self.assertGoldenTextEquals(jni_from_java.GetContent()) | |
| 979 | |
| 980 def testEagerCalledByNativesOption(self): | |
| 981 test_data = """ | |
| 982 package org.chromium.example.jni_generator; | |
| 983 | |
| 984 /** The pointer to the native Test. */ | |
| 985 int nativeTest; | |
| 986 | |
| 987 class Test { | |
| 988 private static native boolean initNativeClass(); | |
| 989 private static native int nativeMethod(int nativeTest, int arg1); | |
| 990 @CalledByNative | |
| 991 private void testMethodWithParam(int iParam); | |
| 992 @CalledByNative | |
| 993 private static int testStaticMethodWithParam(int iParam); | |
| 994 @CalledByNative | |
| 995 private static double testMethodWithNoParam(); | |
| 996 @CalledByNative | |
| 997 private static String testStaticMethodWithNoParam(); | |
| 998 } | |
| 999 """ | |
| 1000 options = TestOptions() | |
| 1001 options.jni_init_native_name = 'initNativeClass' | |
| 1002 options.eager_called_by_natives = True | |
| 1003 jni_from_java = jni_generator.JNIFromJavaSource( | |
| 1004 test_data, 'org/chromium/example/jni_generator/Test', options) | |
| 1005 self.assertGoldenTextEquals(jni_from_java.GetContent()) | |
| 930 | 1006 |
| 931 if __name__ == '__main__': | 1007 if __name__ == '__main__': |
| 932 unittest.main() | 1008 unittest.main() |
| OLD | NEW |