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 | 24 |
| 25 # Set this environment variable in order to regenerate the golden text |
| 26 # files. |
| 27 REBASELINE_ENV = 'REBASELINE' |
25 | 28 |
26 class TestOptions(object): | 29 class TestOptions(object): |
27 """The mock options object which is passed to the jni_generator.py script.""" | 30 """The mock options object which is passed to the jni_generator.py script.""" |
28 | 31 |
29 def __init__(self): | 32 def __init__(self): |
30 self.namespace = None | 33 self.namespace = None |
31 self.script_name = SCRIPT_NAME | 34 self.script_name = SCRIPT_NAME |
32 self.ptr_type = 'int' | 35 self.ptr_type = 'int' |
33 | 36 |
34 | 37 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 self.fail('Golden text mismatch.') | 79 self.fail('Golden text mismatch.') |
77 | 80 |
78 def _ReadGoldenFile(self, golden_file): | 81 def _ReadGoldenFile(self, golden_file): |
79 if not os.path.exists(golden_file): | 82 if not os.path.exists(golden_file): |
80 return None | 83 return None |
81 with file(golden_file, 'r') as f: | 84 with file(golden_file, 'r') as f: |
82 return f.read() | 85 return f.read() |
83 | 86 |
84 def assertGoldenTextEquals(self, generated_text): | 87 def assertGoldenTextEquals(self, generated_text): |
85 script_dir = os.path.dirname(sys.argv[0]) | 88 script_dir = os.path.dirname(sys.argv[0]) |
| 89 # This is the caller test method. |
86 caller = inspect.stack()[1][3] | 90 caller = inspect.stack()[1][3] |
| 91 self.assertTrue(caller.startswith('test'), |
| 92 'assertGoldenTextEquals can only be called from a ' |
| 93 'test* method, not %s' % caller) |
87 golden_file = os.path.join(script_dir, caller + '.golden') | 94 golden_file = os.path.join(script_dir, caller + '.golden') |
88 golden_text = self._ReadGoldenFile(golden_file) | 95 golden_text = self._ReadGoldenFile(golden_file) |
89 if os.environ.get('REBASELINE'): | 96 if os.environ.get(REBASELINE_ENV): |
90 if golden_text != generated_text: | 97 if golden_text != generated_text: |
91 with file(golden_file, 'w') as f: | 98 with file(golden_file, 'w') as f: |
92 f.write(generated_text) | 99 f.write(generated_text) |
93 return | 100 return |
94 self.assertTextEquals(golden_text, generated_text) | 101 self.assertTextEquals(golden_text, generated_text) |
95 | 102 |
| 103 def testInspectCaller(self): |
| 104 def willRaise(): |
| 105 # This function can only be called from a test* method. |
| 106 self.assertGoldenTextEquals('') |
| 107 self.assertRaises(AssertionError, willRaise) |
| 108 |
96 def testNatives(self): | 109 def testNatives(self): |
97 test_data = """" | 110 test_data = """" |
98 interface OnFrameAvailableListener {} | 111 interface OnFrameAvailableListener {} |
99 private native int nativeInit(); | 112 private native int nativeInit(); |
100 private native void nativeDestroy(int nativeChromeBrowserProvider); | 113 private native void nativeDestroy(int nativeChromeBrowserProvider); |
101 private native long nativeAddBookmark( | 114 private native long nativeAddBookmark( |
102 int nativeChromeBrowserProvider, | 115 int nativeChromeBrowserProvider, |
103 String url, String title, boolean isFolder, long parentId); | 116 String url, String title, boolean isFolder, long parentId); |
104 private static native String nativeGetDomainAndRegistry(String url); | 117 private static native String nativeGetDomainAndRegistry(String url); |
105 private static native void nativeCreateHistoricalTabFromState( | 118 private static native void nativeCreateHistoricalTabFromState( |
(...skipping 804 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
910 ptr_type=test_options.ptr_type), | 923 ptr_type=test_options.ptr_type), |
911 ] | 924 ] |
912 self.assertListEquals(golden_natives, natives) | 925 self.assertListEquals(golden_natives, natives) |
913 h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', | 926 h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', |
914 natives, [], test_options) | 927 natives, [], test_options) |
915 self.assertGoldenTextEquals(h.GetContent()) | 928 self.assertGoldenTextEquals(h.GetContent()) |
916 | 929 |
917 | 930 |
918 if __name__ == '__main__': | 931 if __name__ == '__main__': |
919 unittest.main() | 932 unittest.main() |
OLD | NEW |