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 1919 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1930 """ | 1930 """ |
1931 jni_from_java = jni_generator.JNIFromJavaSource( | 1931 jni_from_java = jni_generator.JNIFromJavaSource( |
1932 test_data, ('com/google/lookhowextremelylongiam/snarf/' | 1932 test_data, ('com/google/lookhowextremelylongiam/snarf/' |
1933 'icankeepthisupallday/ReallyLongClassNamesAreAllTheRage')) | 1933 'icankeepthisupallday/ReallyLongClassNamesAreAllTheRage')) |
1934 jni_lines = jni_from_java.GetContent().split('\n') | 1934 jni_lines = jni_from_java.GetContent().split('\n') |
1935 line = filter(lambda line: line.lstrip().startswith('#ifndef'), | 1935 line = filter(lambda line: line.lstrip().startswith('#ifndef'), |
1936 jni_lines)[0] | 1936 jni_lines)[0] |
1937 self.assertTrue(len(line) > 80, | 1937 self.assertTrue(len(line) > 80, |
1938 ('Expected #ifndef line to be > 80 chars: ', line)) | 1938 ('Expected #ifndef line to be > 80 chars: ', line)) |
1939 | 1939 |
| 1940 def testJarJarRemapping(self): |
| 1941 test_data = """ |
| 1942 package org.chromium.example.jni_generator; |
| 1943 |
| 1944 import org.chromium.example2.Test; |
| 1945 |
| 1946 class Example { |
| 1947 private static native void nativeTest(Test t); |
| 1948 } |
| 1949 """ |
| 1950 jni_generator.JniParams.SetJarJarMappings( |
| 1951 """rule org.chromium.example.** com.test.@1 |
| 1952 rule org.chromium.example2.** org.test2.@0""") |
| 1953 jni_from_java = jni_generator.JNIFromJavaSource( |
| 1954 test_data, 'org/chromium/example/jni_generator/Example') |
| 1955 jni_generator.JniParams.SetJarJarMappings('') |
| 1956 golden_content = """\ |
| 1957 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 1958 // Use of this source code is governed by a BSD-style license that can be |
| 1959 // found in the LICENSE file. |
| 1960 |
| 1961 // This file is autogenerated by |
| 1962 // base/android/jni_generator/jni_generator_tests.py |
| 1963 // For |
| 1964 // org/chromium/example/jni_generator/Example |
| 1965 |
| 1966 #ifndef org_chromium_example_jni_generator_Example_JNI |
| 1967 #define org_chromium_example_jni_generator_Example_JNI |
| 1968 |
| 1969 #include <jni.h> |
| 1970 |
| 1971 #include "base/android/jni_android.h" |
| 1972 #include "base/android/scoped_java_ref.h" |
| 1973 #include "base/basictypes.h" |
| 1974 #include "base/logging.h" |
| 1975 |
| 1976 using base::android::ScopedJavaLocalRef; |
| 1977 |
| 1978 // Step 1: forward declarations. |
| 1979 namespace { |
| 1980 const char kExampleClassPath[] = "com/test/jni_generator/Example"; |
| 1981 // Leaking this jclass as we cannot use LazyInstance from some threads. |
| 1982 jclass g_Example_clazz = NULL; |
| 1983 } // namespace |
| 1984 |
| 1985 static void Test(JNIEnv* env, jclass clazz, |
| 1986 jobject t); |
| 1987 |
| 1988 // Step 2: method stubs. |
| 1989 |
| 1990 // Step 3: RegisterNatives. |
| 1991 |
| 1992 static bool RegisterNativesImpl(JNIEnv* env) { |
| 1993 |
| 1994 g_Example_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( |
| 1995 base::android::GetClass(env, kExampleClassPath).obj())); |
| 1996 static const JNINativeMethod kMethodsExample[] = { |
| 1997 { "nativeTest", |
| 1998 "(" |
| 1999 "Lorg/test2/org/chromium/example2/Test;" |
| 2000 ")" |
| 2001 "V", reinterpret_cast<void*>(Test) }, |
| 2002 }; |
| 2003 const int kMethodsExampleSize = arraysize(kMethodsExample); |
| 2004 |
| 2005 if (env->RegisterNatives(g_Example_clazz, |
| 2006 kMethodsExample, |
| 2007 kMethodsExampleSize) < 0) { |
| 2008 LOG(ERROR) << "RegisterNatives failed in " << __FILE__; |
| 2009 return false; |
| 2010 } |
| 2011 |
| 2012 return true; |
| 2013 } |
| 2014 |
| 2015 #endif // org_chromium_example_jni_generator_Example_JNI |
| 2016 """ |
| 2017 self.assertTextEquals(golden_content, jni_from_java.GetContent()) |
| 2018 |
1940 def testImports(self): | 2019 def testImports(self): |
1941 import_header = """ | 2020 import_header = """ |
1942 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2021 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
1943 // Use of this source code is governed by a BSD-style license that can be | 2022 // Use of this source code is governed by a BSD-style license that can be |
1944 // found in the LICENSE file. | 2023 // found in the LICENSE file. |
1945 | 2024 |
1946 package org.chromium.content.app; | 2025 package org.chromium.content.app; |
1947 | 2026 |
1948 import android.app.Service; | 2027 import android.app.Service; |
1949 import android.content.Context; | 2028 import android.content.Context; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1996 | 2075 |
1997 def testJniParamsJavaToJni(self): | 2076 def testJniParamsJavaToJni(self): |
1998 self.assertTextEquals('I', JniParams.JavaToJni('int')) | 2077 self.assertTextEquals('I', JniParams.JavaToJni('int')) |
1999 self.assertTextEquals('[B', JniParams.JavaToJni('byte[]')) | 2078 self.assertTextEquals('[B', JniParams.JavaToJni('byte[]')) |
2000 self.assertTextEquals( | 2079 self.assertTextEquals( |
2001 '[Ljava/nio/ByteBuffer;', JniParams.JavaToJni('java/nio/ByteBuffer[]')) | 2080 '[Ljava/nio/ByteBuffer;', JniParams.JavaToJni('java/nio/ByteBuffer[]')) |
2002 | 2081 |
2003 | 2082 |
2004 if __name__ == '__main__': | 2083 if __name__ == '__main__': |
2005 unittest.main() | 2084 unittest.main() |
OLD | NEW |