Commit 742d3f18 authored by Aiden Benner's avatar Aiden Benner Committed by Commit Bot

jni_generator: Refactor/clean up tests

- Move golden/javap files to jni_generator/golden/
- Make method capitalization more consistent with jni_generator py files
- Split proxy native tests to a different test suite
- Decouple utility functions from TestGenerator into a base test class
- Fix bug where tests would fail if ran from a different directory
- Adds tests for enable/disable/require mock flags

Bug: 898261
Change-Id: Ieae930c73539b00653dfcf32c89d9a79a3ceecf6
Reviewed-on: https://chromium-review.googlesource.com/c/1345239
Commit-Queue: Aiden Benner <abenner@google.com>
Reviewed-by: default avatarEric Stevenson <estevenson@chromium.org>
Reviewed-by: default avataragrieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610266}
parent e930a441
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.base.natives;
// This file is autogenerated by
// base/android/jni_generator/jni_registration_generator.py
// Please do not change its content.
public class GEN_JNI {
public static final boolean TESTING_ENABLED = false;
public static final boolean REQUIRE_MOCK = false;
public static native void org_chromium_example_jni_1generator_SampleForAnnotationProcessor_foo();
public static native Object org_chromium_example_jni_1generator_SampleForAnnotationProcessor_bar(Object sample);
public static native String org_chromium_example_jni_1generator_SampleForAnnotationProcessor_revString(String stringToReverse);
public static native String[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_sendToNative(String[] strs);
public static native Object[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_sendSamplesToNative(Object[] strs);
public static native boolean org_chromium_example_jni_1generator_SampleForAnnotationProcessor_hasPhalange();
public static native int[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools);
public static native void org_chromium_example_jni_1generator_SampleForAnnotationProcessor_testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects);
public static native Throwable org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnThrowable();
public static native Throwable[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnThrowables();
public static native Class org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnClass();
public static native Class[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnClasses();
public static native String org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnString();
public static native String[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnStrings();
public static native Object org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnStruct();
public static native Object[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnStructs();
public static native Object org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnObject();
public static native Object[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnObjects();
}
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.base.natives;
// This file is autogenerated by
// base/android/jni_generator/jni_registration_generator.py
// Please do not change its content.
public class GEN_JNI {
public static final boolean TESTING_ENABLED = true;
public static final boolean REQUIRE_MOCK = false;
public static native void org_chromium_example_jni_1generator_SampleForAnnotationProcessor_foo();
public static native Object org_chromium_example_jni_1generator_SampleForAnnotationProcessor_bar(Object sample);
public static native String org_chromium_example_jni_1generator_SampleForAnnotationProcessor_revString(String stringToReverse);
public static native String[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_sendToNative(String[] strs);
public static native Object[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_sendSamplesToNative(Object[] strs);
public static native boolean org_chromium_example_jni_1generator_SampleForAnnotationProcessor_hasPhalange();
public static native int[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools);
public static native void org_chromium_example_jni_1generator_SampleForAnnotationProcessor_testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects);
public static native Throwable org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnThrowable();
public static native Throwable[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnThrowables();
public static native Class org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnClass();
public static native Class[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnClasses();
public static native String org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnString();
public static native String[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnStrings();
public static native Object org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnStruct();
public static native Object[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnStructs();
public static native Object org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnObject();
public static native Object[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnObjects();
}
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.base.natives;
// This file is autogenerated by
// base/android/jni_generator/jni_registration_generator.py
// Please do not change its content.
public class GEN_JNI {
public static final boolean TESTING_ENABLED = true;
public static final boolean REQUIRE_MOCK = true;
public static native void org_chromium_example_jni_1generator_SampleForAnnotationProcessor_foo();
public static native Object org_chromium_example_jni_1generator_SampleForAnnotationProcessor_bar(Object sample);
public static native String org_chromium_example_jni_1generator_SampleForAnnotationProcessor_revString(String stringToReverse);
public static native String[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_sendToNative(String[] strs);
public static native Object[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_sendSamplesToNative(Object[] strs);
public static native boolean org_chromium_example_jni_1generator_SampleForAnnotationProcessor_hasPhalange();
public static native int[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_testAllPrimitives(int zint, int[] ints, long zlong, long[] longs, short zshort, short[] shorts, char zchar, char[] chars, byte zbyte, byte[] bytes, double zdouble, double[] doubles, float zfloat, float[] floats, boolean zbool, boolean[] bools);
public static native void org_chromium_example_jni_1generator_SampleForAnnotationProcessor_testSpecialTypes(Class clazz, Class[] classes, Throwable throwable, Throwable[] throwables, String string, String[] strings, Object tStruct, Object[] structs, Object obj, Object[] objects);
public static native Throwable org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnThrowable();
public static native Throwable[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnThrowables();
public static native Class org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnClass();
public static native Class[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnClasses();
public static native String org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnString();
public static native String[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnStrings();
public static native Object org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnStruct();
public static native Object[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnStructs();
public static native Object org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnObject();
public static native Object[] org_chromium_example_jni_1generator_SampleForAnnotationProcessor_returnObjects();
}
...@@ -50,36 +50,78 @@ class TestOptions(object): ...@@ -50,36 +50,78 @@ class TestOptions(object):
self.use_proxy_hash = False self.use_proxy_hash = False
class TestGenerator(unittest.TestCase): class BaseTest(unittest.TestCase):
def assertObjEquals(self, first, second):
@staticmethod
def _MergeRegistrationForTests(results,
header_guard='HEADER_GUARD',
namespace='test'):
results.sort(key=lambda d: d['FULL_CLASS_NAME'])
combined_dict = {}
for key in jni_registration_generator.MERGEABLE_KEYS:
combined_dict[key] = ''.join(d.get(key, '') for d in results)
combined_dict['HEADER_GUARD'] = header_guard
combined_dict['NAMESPACE'] = namespace
return combined_dict
def _JoinScriptDir(self, path):
script_dir = os.path.dirname(sys.argv[0])
return os.path.join(script_dir, path)
def _JoinGoldenPath(self, golden_file_name):
return self._JoinScriptDir(os.path.join('golden', golden_file_name))
def _ReadGoldenFile(self, golden_file_name):
golden_file_name = self._JoinGoldenPath(golden_file_name)
if not os.path.exists(golden_file_name):
return None
with file(golden_file_name, 'r') as f:
return f.read()
def _CreateJniHeaderFromFile(self, fname, qualified_clazz, options=None):
content = file(self._JoinScriptDir(fname)).read()
opts = options
if opts is None:
opts = TestOptions()
jni_from_java = jni_generator.JNIFromJavaSource(content, qualified_clazz,
opts)
return jni_from_java.GetContent()
def AssertObjEquals(self, first, second):
dict_first = first.__dict__ dict_first = first.__dict__
dict_second = second.__dict__ dict_second = second.__dict__
self.assertEquals(dict_first.keys(), dict_second.keys()) self.assertEquals(dict_first.keys(), dict_second.keys())
for key, value in dict_first.iteritems(): for key, value in dict_first.iteritems():
if (type(value) is list and len(value) and if (type(value) is list and len(value) and
isinstance(type(value[0]), object)): isinstance(type(value[0]), object)):
self.assertListEquals(value, second.__getattribute__(key)) self.AssertListEquals(value, second.__getattribute__(key))
else: else:
actual = second.__getattribute__(key) actual = second.__getattribute__(key)
self.assertEquals(value, actual, self.assertEquals(value, actual,
'Key ' + key + ': ' + str(value) + '!=' + str(actual)) 'Key ' + key + ': ' + str(value) + '!=' + str(actual))
def assertListEquals(self, first, second): def AssertListEquals(self, first, second):
self.assertEquals(len(first), len(second)) self.assertEquals(len(first), len(second))
for i in xrange(len(first)): for i in xrange(len(first)):
if isinstance(first[i], object): if isinstance(first[i], object):
self.assertObjEquals(first[i], second[i]) self.AssertObjEquals(first[i], second[i])
else: else:
self.assertEquals(first[i], second[i]) self.assertEquals(first[i], second[i])
def assertTextEquals(self, golden_text, generated_text): def AssertTextEquals(self, golden_text, generated_text):
if not self.compareText(golden_text, generated_text): if not self.CompareText(golden_text, generated_text):
self.fail('Golden text mismatch.') self.fail('Golden text mismatch.')
def compareText(self, golden_text, generated_text): def CompareText(self, golden_text, generated_text):
def FilterText(text): def FilterText(text):
return [ return [
l.strip() for l in text.split('\n') l.strip()
for l in text.split('\n')
if not l.startswith('// Copyright') if not l.startswith('// Copyright')
] ]
stripped_golden = FilterText(golden_text) stripped_golden = FilterText(golden_text)
...@@ -97,256 +139,44 @@ class TestGenerator(unittest.TestCase): ...@@ -97,256 +139,44 @@ class TestGenerator(unittest.TestCase):
print 'REBASELINE=1', sys.argv[0] print 'REBASELINE=1', sys.argv[0]
print 'to regenerate the data files.' print 'to regenerate the data files.'
@staticmethod def AssertGoldenTextEquals(self, generated_text, suffix='', golden_file=None):
def _MergeRegistrationForTests(results,
header_guard='HEADER_GUARD',
namespace='test'):
results.sort(key=lambda d: d['FULL_CLASS_NAME'])
combined_dict = {}
for key in jni_registration_generator.MERGEABLE_KEYS:
combined_dict[key] = ''.join(d.get(key, '') for d in results)
combined_dict['HEADER_GUARD'] = header_guard
combined_dict['NAMESPACE'] = namespace
return combined_dict
def _ReadGoldenFile(self, golden_file):
if not os.path.exists(golden_file):
return None
with file(golden_file, 'r') as f:
return f.read()
def assertGoldenTextEquals(self, generated_text, suffix='', golden_file=None):
"""Compares generated text with the corresponding golden_file """Compares generated text with the corresponding golden_file
By default compares generated_text with the file at By default compares generated_text with the file at
script_dir/{caller_name}[suffix].golden. If the parameter golden_file is script_dir/golden/{caller_name}[suffix].golden. If the parameter golden_file is
provided it will instead compare the generated text with provided it will instead compare the generated text with
script_dir/golden_file.""" script_dir/golden/golden_file."""
script_dir = os.path.dirname(sys.argv[0])
# This is the caller test method. # This is the caller test method.
caller = inspect.stack()[1][3] caller = inspect.stack()[1][3]
if golden_file is None: if golden_file is None:
self.assertTrue( self.assertTrue(
caller.startswith('test'), caller.startswith('test'),
'assertGoldenTextEquals can only be called from a ' 'AssertGoldenTextEquals can only be called from a '
'test* method, not %s' % caller) 'test* method, not %s' % caller)
golden_file = os.path.join(script_dir, '%s%s.golden' % (caller, suffix)) golden_file = '%s%s.golden' % (caller, suffix)
self.assertTrue(
os.path.exists(golden_file), '%s does not exist' % golden_file)
golden_text = self._ReadGoldenFile(golden_file) golden_text = self._ReadGoldenFile(golden_file)
if os.environ.get(REBASELINE_ENV): if os.environ.get(REBASELINE_ENV):
if golden_text != generated_text: if golden_text != generated_text:
with file(golden_file, 'w') as f: with file(self._JoinGoldenPath(golden_file), 'w') as f:
f.write(generated_text) f.write(generated_text)
return return
self.assertTextEquals(golden_text, generated_text) # golden_text is None if no file is found. Better to fail than in
# AssertTextEquals so we can give a clearer message.
if golden_text is None:
self.fail(
'Golden file %s does not exist.' % self._JoinGoldenPath(golden_file))
self.AssertTextEquals(golden_text, generated_text)
class TestGenerator(BaseTest):
def testInspectCaller(self): def testInspectCaller(self):
def willRaise(): def willRaise():
# This function can only be called from a test* method. # This function can only be called from a test* method.
self.assertGoldenTextEquals('') self.AssertGoldenTextEquals('')
self.assertRaises(AssertionError, willRaise) self.assertRaises(AssertionError, willRaise)
def testEscapingProxyNatives(self):
test_data = """
class SampleProxyJni {
@JniStaticNatives
interface Natives {
void foo_bar();
void foo__bar();
}
}
"""
qualified_clazz = 'org/chromium/example/SampleProxyJni'
natives = jni_generator.NativeProxyHelpers.ExtractStaticProxyNatives(
qualified_clazz, test_data, 'long')
golden_natives = [
NativeMethod(
return_type='void',
static=True,
name='foo_bar',
params=[],
java_class_name=None,
is_proxy=True,
proxy_name='org_chromium_example_SampleProxyJni_foo_1bar',
type='function'),
NativeMethod(
return_type='void',
static=True,
name='foo__bar',
params=[],
java_class_name=None,
is_proxy=True,
proxy_name='org_chromium_example_SampleProxyJni_foo_1_1bar',
type='function'),
]
self.assertListEquals(natives, golden_natives)
def testProxyNativesMainDex(self):
test_data = """
@MainDex
class Foo() {
@JniStaticNatives
interface Natives {
void thisismaindex();
}
void dontmatchme();
public static void metoo();
public static native void this_is_a_non_proxy_native();
}
"""
non_main_dex_test_data = """
class Bar() {
@JniStaticNatives
interface Natives {
void foo();
void bar();
}
}
"""
qualified_clazz = 'test/foo/Foo'
jni_params = TestOptions()
natives = jni_generator.NativeProxyHelpers.ExtractStaticProxyNatives(
qualified_clazz, test_data, 'long')
golden_natives = [
NativeMethod(
return_type='void',
static=True,
name='thisismaindex',
params=[],
java_class_name=None,
is_proxy=True,
proxy_name='test_foo_Foo_thisismaindex',
type='function'),
]
self.assertListEquals(natives, golden_natives)
jni_params = jni_generator.JniParams(qualified_clazz)
main_dex_header = jni_registration_generator.HeaderGenerator(
'', qualified_clazz, natives, jni_params, main_dex=True).Generate()
content = TestGenerator._MergeRegistrationForTests([main_dex_header])
self.assertGoldenTextEquals(
jni_registration_generator.CreateFromDict(content))
other_qualified_clazz = 'test/foo/Bar'
other_natives = jni_generator.NativeProxyHelpers.ExtractStaticProxyNatives(
other_qualified_clazz, non_main_dex_test_data, 'long')
jni_params = jni_generator.JniParams(other_qualified_clazz)
non_main_dex_header = jni_registration_generator.HeaderGenerator(
'', other_qualified_clazz, other_natives, jni_params,
main_dex=False).Generate()
content = TestGenerator._MergeRegistrationForTests([main_dex_header] +
[non_main_dex_header])
self.assertGoldenTextEquals(
jni_registration_generator.CreateFromDict(content), 'AndNonMainDex')
def testProxyNatives(self):
test_data = """
class SampleProxyJni {
private void do_not_match();
@JniStaticNatives
interface Natives {
void foo();
int bar(int x, int y);
String foobar(String x, String y);
}
void dontmatchme();
public static void metoo();
public static native void this_is_a_non_proxy_native();
}
"""
bad_spaced_test_data = """
class SampleProxyJni{
@JniStaticNatives interface
Natives
{ void foo();
int bar(int
x, int y); String
foobar(String x, String y);
}
}
"""
qualified_clazz = 'org/chromium/example/SampleProxyJni'
natives = jni_generator.NativeProxyHelpers.ExtractStaticProxyNatives(
qualified_clazz, test_data, 'long')
bad_spacing_natives = jni_generator.NativeProxyHelpers\
.ExtractStaticProxyNatives(qualified_clazz, bad_spaced_test_data, 'long')
golden_natives = [
NativeMethod(
return_type='void',
static=True,
name='foo',
params=[],
java_class_name=None,
is_proxy=True,
proxy_name='org_chromium_example_SampleProxyJni_foo',
type='function'),
NativeMethod(
return_type='int',
static=True,
name='bar',
params=[
Param(datatype='int', name='x'),
Param(datatype='int', name='y')
],
java_class_name=None,
is_proxy=True,
proxy_name='org_chromium_example_SampleProxyJni_bar',
type='function'),
NativeMethod(
return_type='String',
static=True,
name='foobar',
params=[
Param(datatype='String', name='x'),
Param(datatype='String', name='y')
],
java_class_name=None,
is_proxy=True,
proxy_name='org_chromium_example_SampleProxyJni_foobar',
type='function'),
]
self.assertListEquals(golden_natives, natives)
self.assertListEquals(golden_natives, bad_spacing_natives)
jni_params = jni_generator.JniParams(qualified_clazz)
h1 = jni_generator.InlHeaderFileGenerator('', qualified_clazz, natives, [],
[], jni_params, TestOptions())
self.assertGoldenTextEquals(h1.GetContent())
h2 = jni_registration_generator.HeaderGenerator('', qualified_clazz,
natives, jni_params, False)
content = TestGenerator._MergeRegistrationForTests([h2.Generate()])
proxy_opts = jni_registration_generator.ProxyOptions()
self.assertGoldenTextEquals(
jni_registration_generator.CreateProxyJavaFromDict(content, proxy_opts),
suffix='Java')
self.assertGoldenTextEquals(
jni_registration_generator.CreateFromDict(content),
suffix='Registrations')
def testNatives(self): def testNatives(self):
test_data = """" test_data = """"
import android.graphics.Bitmap; import android.graphics.Bitmap;
...@@ -508,20 +338,19 @@ class TestGenerator(unittest.TestCase): ...@@ -508,20 +338,19 @@ class TestGenerator(unittest.TestCase):
java_class_name=None, java_class_name=None,
type='function') type='function')
] ]
self.assertListEquals(golden_natives, natives) self.AssertListEquals(golden_natives, natives)
h1 = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', h1 = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni',
natives, [], [], jni_params, natives, [], [], jni_params,
TestOptions()) TestOptions())
self.assertGoldenTextEquals(h1.GetContent()) self.AssertGoldenTextEquals(h1.GetContent())
h2 = jni_registration_generator.HeaderGenerator( h2 = jni_registration_generator.HeaderGenerator(
'', 'org/chromium/TestJni', natives, jni_params, True) '', 'org/chromium/TestJni', natives, jni_params, True)
content = TestGenerator._MergeRegistrationForTests([h2.Generate()]) content = TestGenerator._MergeRegistrationForTests([h2.Generate()])
self.assertGoldenTextEquals( self.AssertGoldenTextEquals(
jni_registration_generator.CreateFromDict(content), jni_registration_generator.CreateFromDict(content),
suffix='Registrations') suffix='Registrations')
def testInnerClassNatives(self): def testInnerClassNatives(self):
test_data = """ test_data = """
class MyInnerClass { class MyInnerClass {
...@@ -536,12 +365,12 @@ class TestGenerator(unittest.TestCase): ...@@ -536,12 +365,12 @@ class TestGenerator(unittest.TestCase):
java_class_name='MyInnerClass', java_class_name='MyInnerClass',
type='function') type='function')
] ]
self.assertListEquals(golden_natives, natives) self.AssertListEquals(golden_natives, natives)
jni_params = jni_generator.JniParams('') jni_params = jni_generator.JniParams('')
h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni',
natives, [], [], jni_params, natives, [], [], jni_params,
TestOptions()) TestOptions())
self.assertGoldenTextEquals(h.GetContent()) self.AssertGoldenTextEquals(h.GetContent())
def testInnerClassNativesMultiple(self): def testInnerClassNativesMultiple(self):
test_data = """ test_data = """
...@@ -565,12 +394,12 @@ class TestGenerator(unittest.TestCase): ...@@ -565,12 +394,12 @@ class TestGenerator(unittest.TestCase):
java_class_name='MyOtherInnerClass', java_class_name='MyOtherInnerClass',
type='function') type='function')
] ]
self.assertListEquals(golden_natives, natives) self.AssertListEquals(golden_natives, natives)
jni_params = jni_generator.JniParams('') jni_params = jni_generator.JniParams('')
h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni',
natives, [], [], jni_params, natives, [], [], jni_params,
TestOptions()) TestOptions())
self.assertGoldenTextEquals(h.GetContent()) self.AssertGoldenTextEquals(h.GetContent())
def testInnerClassNativesBothInnerAndOuter(self): def testInnerClassNativesBothInnerAndOuter(self):
test_data = """ test_data = """
...@@ -593,18 +422,18 @@ class TestGenerator(unittest.TestCase): ...@@ -593,18 +422,18 @@ class TestGenerator(unittest.TestCase):
java_class_name='MyOtherInnerClass', java_class_name='MyOtherInnerClass',
type='function') type='function')
] ]
self.assertListEquals(golden_natives, natives) self.AssertListEquals(golden_natives, natives)
jni_params = jni_generator.JniParams('') jni_params = jni_generator.JniParams('')
h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni',
natives, [], [], jni_params, natives, [], [], jni_params,
TestOptions()) TestOptions())
self.assertGoldenTextEquals(h.GetContent()) self.AssertGoldenTextEquals(h.GetContent())
h2 = jni_registration_generator.HeaderGenerator( h2 = jni_registration_generator.HeaderGenerator(
'', 'org/chromium/TestJni', natives, jni_params, True) '', 'org/chromium/TestJni', natives, jni_params, True)
content = TestGenerator._MergeRegistrationForTests([h2.Generate()]) content = TestGenerator._MergeRegistrationForTests([h2.Generate()])
self.assertGoldenTextEquals( self.AssertGoldenTextEquals(
jni_registration_generator.CreateFromDict(content), jni_registration_generator.CreateFromDict(content),
suffix='Registrations') suffix='Registrations')
...@@ -930,11 +759,11 @@ class TestGenerator(unittest.TestCase): ...@@ -930,11 +759,11 @@ class TestGenerator(unittest.TestCase):
unchecked=False, unchecked=False,
), ),
] ]
self.assertListEquals(golden_called_by_natives, called_by_natives) self.AssertListEquals(golden_called_by_natives, called_by_natives)
h = jni_generator.InlHeaderFileGenerator( h = jni_generator.InlHeaderFileGenerator(
'', 'org/chromium/TestJni', [], called_by_natives, [], jni_params, '', 'org/chromium/TestJni', [], called_by_natives, [], jni_params,
TestOptions()) TestOptions())
self.assertGoldenTextEquals(h.GetContent()) self.AssertGoldenTextEquals(h.GetContent())
def testCalledByNativeParseError(self): def testCalledByNativeParseError(self):
try: try:
...@@ -1002,7 +831,7 @@ public abstract class java.util.HashSet<T> extends java.util.AbstractSet<E> ...@@ -1002,7 +831,7 @@ public abstract class java.util.HashSet<T> extends java.util.AbstractSet<E>
jni_from_javap = jni_generator.JNIFromJavaP(contents.split('\n'), jni_from_javap = jni_generator.JNIFromJavaP(contents.split('\n'),
TestOptions()) TestOptions())
self.assertEquals(2, len(jni_from_javap.called_by_natives)) self.assertEquals(2, len(jni_from_javap.called_by_natives))
self.assertGoldenTextEquals(jni_from_javap.GetContent()) self.AssertGoldenTextEquals(jni_from_javap.GetContent())
def testSnippnetJavap6_7_8(self): def testSnippnetJavap6_7_8(self):
content_javap6 = """ content_javap6 = """
...@@ -1037,28 +866,26 @@ public class java.util.HashSet { ...@@ -1037,28 +866,26 @@ public class java.util.HashSet {
self.assertTrue(jni_from_javap8.GetContent()) self.assertTrue(jni_from_javap8.GetContent())
# Ensure the javap7 is correctly parsed and uses the Signature field rather # Ensure the javap7 is correctly parsed and uses the Signature field rather
# than the "E" parameter. # than the "E" parameter.
self.assertTextEquals(jni_from_javap6.GetContent(), self.AssertTextEquals(jni_from_javap6.GetContent(),
jni_from_javap7.GetContent()) jni_from_javap7.GetContent())
# Ensure the javap8 is correctly parsed and uses the descriptor field. # Ensure the javap8 is correctly parsed and uses the descriptor field.
self.assertTextEquals(jni_from_javap7.GetContent(), self.AssertTextEquals(jni_from_javap7.GetContent(),
jni_from_javap8.GetContent()) jni_from_javap8.GetContent())
def testFromJavaP(self): def testFromJavaP(self):
contents = self._ReadGoldenFile(os.path.join(os.path.dirname(sys.argv[0]), contents = self._ReadGoldenFile('testInputStream.javap')
'testInputStream.javap'))
jni_from_javap = jni_generator.JNIFromJavaP(contents.split('\n'), jni_from_javap = jni_generator.JNIFromJavaP(contents.split('\n'),
TestOptions()) TestOptions())
self.assertEquals(10, len(jni_from_javap.called_by_natives)) self.assertEquals(10, len(jni_from_javap.called_by_natives))
self.assertGoldenTextEquals(jni_from_javap.GetContent()) self.AssertGoldenTextEquals(jni_from_javap.GetContent())
def testConstantsFromJavaP(self): def testConstantsFromJavaP(self):
for f in ['testMotionEvent.javap', 'testMotionEvent.javap7']: for f in ['testMotionEvent.javap', 'testMotionEvent.javap7']:
contents = self._ReadGoldenFile(os.path.join(os.path.dirname(sys.argv[0]), contents = self._ReadGoldenFile(f)
f))
jni_from_javap = jni_generator.JNIFromJavaP(contents.split('\n'), jni_from_javap = jni_generator.JNIFromJavaP(contents.split('\n'),
TestOptions()) TestOptions())
self.assertEquals(86, len(jni_from_javap.called_by_natives)) self.assertEquals(86, len(jni_from_javap.called_by_natives))
self.assertGoldenTextEquals(jni_from_javap.GetContent()) self.AssertGoldenTextEquals(jni_from_javap.GetContent())
def testREForNatives(self): def testREForNatives(self):
# We should not match "native SyncSetupFlow" inside the comment. # We should not match "native SyncSetupFlow" inside the comment.
...@@ -1091,49 +918,11 @@ public class java.util.HashSet { ...@@ -1091,49 +918,11 @@ public class java.util.HashSet {
jni_generator.JNIFromJavaSource, jni_generator.JNIFromJavaSource,
test_data, 'foo/bar', TestOptions()) test_data, 'foo/bar', TestOptions())
def _createJniHeaderFromFile(self, fname, qualified_clazz, options=None):
script_dir = os.path.dirname(sys.argv[0])
content = file(os.path.join(script_dir, fname)).read()
opts = options
if opts is None:
opts = TestOptions()
jni_from_java = jni_generator.JNIFromJavaSource(content, qualified_clazz,
opts)
return jni_from_java.GetContent()
def testHashedProxyExample(self):
opts = TestOptions()
opts.use_proxy_hash = True
path = 'java/src/org/chromium/example/jni_generator/SampleForAnnotationProcessor.java'
generated_text = self._createJniHeaderFromFile(
path, 'org/chromium/example/jni_generator/SampleForAnnotationProcessor',
opts)
self.assertGoldenTextEquals(
generated_text,
golden_file='HashedSampleForAnnotationProcessor_jni.golden')
reg_dict = jni_registration_generator._DictForPath(path)
reg_dict = self._MergeRegistrationForTests([reg_dict])
proxy_opts = jni_registration_generator.ProxyOptions()
self.assertGoldenTextEquals(
jni_registration_generator.CreateProxyJavaFromDict(reg_dict,
proxy_opts),
golden_file='HashedSampleForAnnotationProcessorGenJni.golden')
def testJniProxyExample(self):
generated_text = self._createJniHeaderFromFile(
'java/src/org/chromium/example/jni_generator/SampleForAnnotationProcessor.java',
'org/chromium/example/jni_generator/SampleForAnnotationProcessor')
self.assertGoldenTextEquals(
generated_text, golden_file='SampleForAnnotationProcessor_jni.golden')
def testJniSelfDocumentingExample(self): def testJniSelfDocumentingExample(self):
generated_text = self._createJniHeaderFromFile( generated_text = self._CreateJniHeaderFromFile(
'java/src/org/chromium/example/jni_generator/SampleForTests.java', 'java/src/org/chromium/example/jni_generator/SampleForTests.java',
'org/chromium/example/jni_generator/SampleForTests') 'org/chromium/example/jni_generator/SampleForTests')
self.assertGoldenTextEquals( self.AssertGoldenTextEquals(
generated_text, golden_file='SampleForTests_jni.golden') generated_text, golden_file='SampleForTests_jni.golden')
def testNoWrappingPreprocessorLines(self): def testNoWrappingPreprocessorLines(self):
...@@ -1211,10 +1000,10 @@ class Foo { ...@@ -1211,10 +1000,10 @@ class Foo {
def testJniParamsJavaToJni(self): def testJniParamsJavaToJni(self):
jni_params = jni_generator.JniParams('') jni_params = jni_generator.JniParams('')
self.assertTextEquals('I', jni_params.JavaToJni('int')) self.AssertTextEquals('I', jni_params.JavaToJni('int'))
self.assertTextEquals('[B', jni_params.JavaToJni('byte[]')) self.AssertTextEquals('[B', jni_params.JavaToJni('byte[]'))
self.assertTextEquals( self.AssertTextEquals('[Ljava/nio/ByteBuffer;',
'[Ljava/nio/ByteBuffer;', jni_params.JavaToJni('java/nio/ByteBuffer[]')) jni_params.JavaToJni('java/nio/ByteBuffer[]'))
def testNativesLong(self): def testNativesLong(self):
test_options = TestOptions() test_options = TestOptions()
...@@ -1234,11 +1023,11 @@ class Foo { ...@@ -1234,11 +1023,11 @@ class Foo {
p0_type='ChromeBrowserProvider', p0_type='ChromeBrowserProvider',
ptr_type=test_options.ptr_type), ptr_type=test_options.ptr_type),
] ]
self.assertListEquals(golden_natives, natives) self.AssertListEquals(golden_natives, natives)
h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni',
natives, [], [], jni_params, natives, [], [], jni_params,
test_options) test_options)
self.assertGoldenTextEquals(h.GetContent()) self.AssertGoldenTextEquals(h.GetContent())
def testMainDexAnnotation(self): def testMainDexAnnotation(self):
mainDexEntries = [ mainDexEntries = [
...@@ -1320,7 +1109,7 @@ class Foo { ...@@ -1320,7 +1109,7 @@ class Foo {
options.native_exports_optional = False options.native_exports_optional = False
jni_from_java = jni_generator.JNIFromJavaSource( jni_from_java = jni_generator.JNIFromJavaSource(
test_data, 'org/chromium/example/jni_generator/SampleForTests', options) test_data, 'org/chromium/example/jni_generator/SampleForTests', options)
self.assertGoldenTextEquals(jni_from_java.GetContent()) self.AssertGoldenTextEquals(jni_from_java.GetContent())
def testOuterInnerRaises(self): def testOuterInnerRaises(self):
test_data = """ test_data = """
...@@ -1355,7 +1144,7 @@ class Foo { ...@@ -1355,7 +1144,7 @@ class Foo {
jni_from_java = jni_generator.JNIFromJavaSource(test_data, jni_from_java = jni_generator.JNIFromJavaSource(test_data,
'org/chromium/foo/Foo', 'org/chromium/foo/Foo',
TestOptions()) TestOptions())
self.assertGoldenTextEquals(jni_from_java.GetContent()) self.AssertGoldenTextEquals(jni_from_java.GetContent())
def testMultipleJNIAdditionalImport(self): def testMultipleJNIAdditionalImport(self):
test_data = """ test_data = """
...@@ -1376,7 +1165,46 @@ class Foo { ...@@ -1376,7 +1165,46 @@ class Foo {
jni_from_java = jni_generator.JNIFromJavaSource(test_data, jni_from_java = jni_generator.JNIFromJavaSource(test_data,
'org/chromium/foo/Foo', 'org/chromium/foo/Foo',
TestOptions()) TestOptions())
self.assertGoldenTextEquals(jni_from_java.GetContent()) self.AssertGoldenTextEquals(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())
class ProxyTestGenerator(BaseTest):
def _BuildRegDictFromSample(self, options=None):
if options is None:
options = TestOptions()
path = self._JoinScriptDir(
'java/src/org/chromium/example/jni_generator/SampleForAnnotationProcessor.java'
)
reg_dict = jni_registration_generator._DictForPath(path)
reg_dict = self._MergeRegistrationForTests([reg_dict])
return reg_dict
def testProxyNativesWithNatives(self): def testProxyNativesWithNatives(self):
test_data = """ test_data = """
...@@ -1402,34 +1230,249 @@ class Foo { ...@@ -1402,34 +1230,249 @@ class Foo {
options_with_tracing.enable_tracing = True options_with_tracing.enable_tracing = True
jni_from_java = jni_generator.JNIFromJavaSource( jni_from_java = jni_generator.JNIFromJavaSource(
test_data, 'org/chromium/foo/Foo', options_with_tracing) test_data, 'org/chromium/foo/Foo', options_with_tracing)
self.assertGoldenTextEquals(jni_from_java.GetContent()) self.AssertGoldenTextEquals(jni_from_java.GetContent())
def testEscapingProxyNatives(self):
test_data = """
class SampleProxyJni {
@JniStaticNatives
interface Natives {
void foo_bar();
void foo__bar();
}
}
"""
qualified_clazz = 'org/chromium/example/SampleProxyJni'
natives = jni_generator.NativeProxyHelpers.ExtractStaticProxyNatives(
qualified_clazz, test_data, 'long')
def testTracing(self): golden_natives = [
NativeMethod(
return_type='void',
static=True,
name='foo_bar',
params=[],
java_class_name=None,
is_proxy=True,
proxy_name='org_chromium_example_SampleProxyJni_foo_1bar',
type='function'),
NativeMethod(
return_type='void',
static=True,
name='foo__bar',
params=[],
java_class_name=None,
is_proxy=True,
proxy_name='org_chromium_example_SampleProxyJni_foo_1_1bar',
type='function'),
]
self.AssertListEquals(natives, golden_natives)
def testProxyNativesMainDex(self):
test_data = """ test_data = """
package org.chromium.foo; @MainDex
class Foo() {
@JniStaticNatives
interface Natives {
void thisismaindex();
}
void dontmatchme();
public static void metoo();
public static native void this_is_a_non_proxy_native();
}
"""
@JNINamespace("org::chromium_foo") non_main_dex_test_data = """
class Foo { class Bar() {
@JniStaticNatives
interface Natives {
void foo();
void bar();
}
}
"""
qualified_clazz = 'test/foo/Foo'
jni_params = TestOptions()
@CalledByNative natives = jni_generator.NativeProxyHelpers.ExtractStaticProxyNatives(
Foo(); qualified_clazz, test_data, 'long')
@CalledByNative golden_natives = [
void callbackFromNative(); NativeMethod(
return_type='void',
static=True,
name='thisismaindex',
params=[],
java_class_name=None,
is_proxy=True,
proxy_name='test_foo_Foo_thisismaindex',
type='function'),
]
native void nativeInstanceMethod(long nativeInstance); self.AssertListEquals(natives, golden_natives)
static native void nativeStaticMethod(); jni_params = jni_generator.JniParams(qualified_clazz)
main_dex_header = jni_registration_generator.HeaderGenerator(
'', qualified_clazz, natives, jni_params, main_dex=True).Generate()
content = TestGenerator._MergeRegistrationForTests([main_dex_header])
self.AssertGoldenTextEquals(
jni_registration_generator.CreateFromDict(content))
other_qualified_clazz = 'test/foo/Bar'
other_natives = jni_generator.NativeProxyHelpers.ExtractStaticProxyNatives(
other_qualified_clazz, non_main_dex_test_data, 'long')
jni_params = jni_generator.JniParams(other_qualified_clazz)
non_main_dex_header = jni_registration_generator.HeaderGenerator(
'', other_qualified_clazz, other_natives, jni_params,
main_dex=False).Generate()
content = TestGenerator._MergeRegistrationForTests([main_dex_header] +
[non_main_dex_header])
self.AssertGoldenTextEquals(
jni_registration_generator.CreateFromDict(content), 'AndNonMainDex')
def testProxyNatives(self):
test_data = """
class SampleProxyJni {
private void do_not_match();
@JniStaticNatives
interface Natives {
void foo();
int bar(int x, int y);
String foobar(String x, String y);
}
void dontmatchme();
public static void metoo();
public static native void this_is_a_non_proxy_native();
} }
""" """
options_with_tracing = TestOptions()
options_with_tracing.enable_tracing = True bad_spaced_test_data = """
jni_from_java = jni_generator.JNIFromJavaSource(test_data, class SampleProxyJni{
'org/chromium/foo/Foo', @JniStaticNatives interface
options_with_tracing) Natives
self.assertGoldenTextEquals(jni_from_java.GetContent())
{ void foo();
int bar(int
x, int y); String
foobar(String x, String y);
}
}
"""
qualified_clazz = 'org/chromium/example/SampleProxyJni'
natives = jni_generator.NativeProxyHelpers.ExtractStaticProxyNatives(
qualified_clazz, test_data, 'long')
bad_spacing_natives = jni_generator.NativeProxyHelpers \
.ExtractStaticProxyNatives(qualified_clazz, bad_spaced_test_data, 'long')
golden_natives = [
NativeMethod(
return_type='void',
static=True,
name='foo',
params=[],
java_class_name=None,
is_proxy=True,
proxy_name='org_chromium_example_SampleProxyJni_foo',
type='function'),
NativeMethod(
return_type='int',
static=True,
name='bar',
params=[
Param(datatype='int', name='x'),
Param(datatype='int', name='y')
],
java_class_name=None,
is_proxy=True,
proxy_name='org_chromium_example_SampleProxyJni_bar',
type='function'),
NativeMethod(
return_type='String',
static=True,
name='foobar',
params=[
Param(datatype='String', name='x'),
Param(datatype='String', name='y')
],
java_class_name=None,
is_proxy=True,
proxy_name='org_chromium_example_SampleProxyJni_foobar',
type='function'),
]
self.AssertListEquals(golden_natives, natives)
self.AssertListEquals(golden_natives, bad_spacing_natives)
jni_params = jni_generator.JniParams(qualified_clazz)
h1 = jni_generator.InlHeaderFileGenerator('', qualified_clazz, natives, [],
[], jni_params, TestOptions())
self.AssertGoldenTextEquals(h1.GetContent())
h2 = jni_registration_generator.HeaderGenerator('', qualified_clazz,
natives, jni_params, False)
content = TestGenerator._MergeRegistrationForTests([h2.Generate()])
proxy_opts = jni_registration_generator.ProxyOptions()
self.AssertGoldenTextEquals(
jni_registration_generator.CreateProxyJavaFromDict(content, proxy_opts),
suffix='Java')
self.AssertGoldenTextEquals(
jni_registration_generator.CreateFromDict(content),
suffix='Registrations')
def testProxyHashedExample(self):
opts = TestOptions()
opts.use_proxy_hash = True
path = 'java/src/org/chromium/example/jni_generator/SampleForAnnotationProcessor.java'
generated_text = self._CreateJniHeaderFromFile(
path, 'org/chromium/example/jni_generator/SampleForAnnotationProcessor',
opts)
self.AssertGoldenTextEquals(
generated_text,
golden_file='HashedSampleForAnnotationProcessor_jni.golden')
reg_dict = jni_registration_generator._DictForPath(
self._JoinScriptDir(path))
reg_dict = self._MergeRegistrationForTests([reg_dict])
proxy_opts = jni_registration_generator.ProxyOptions()
self.AssertGoldenTextEquals(
jni_registration_generator.CreateProxyJavaFromDict(reg_dict, proxy_opts),
golden_file='HashedSampleForAnnotationProcessorGenJni.golden')
def testProxyJniExample(self):
generated_text = self._CreateJniHeaderFromFile(
'java/src/org/chromium/example/jni_generator/SampleForAnnotationProcessor.java',
'org/chromium/example/jni_generator/SampleForAnnotationProcessor')
self.AssertGoldenTextEquals(
generated_text, golden_file='SampleForAnnotationProcessor_jni.golden')
def testGenJniFlags(self):
reg_dict = self._BuildRegDictFromSample()
proxy_options = jni_registration_generator.ProxyOptions()
content = jni_registration_generator.CreateProxyJavaFromDict(
reg_dict, proxy_options)
self.AssertGoldenTextEquals(content, 'Disabled')
proxy_options = jni_registration_generator.ProxyOptions(enable_mocks=True)
content = jni_registration_generator.CreateProxyJavaFromDict(
reg_dict, proxy_options)
self.AssertGoldenTextEquals(content, 'MocksEnabled')
proxy_options = jni_registration_generator.ProxyOptions(
enable_mocks=True, require_mocks=True)
content = jni_registration_generator.CreateProxyJavaFromDict(
reg_dict, proxy_options)
self.AssertGoldenTextEquals(content, 'MocksRequired')
def TouchStamp(stamp_path): def TouchStamp(stamp_path):
...@@ -1444,8 +1487,8 @@ def TouchStamp(stamp_path): ...@@ -1444,8 +1487,8 @@ def TouchStamp(stamp_path):
def main(argv): def main(argv):
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option('--stamp', help='Path to touch on success.') parser.add_option('--stamp', help='Path to touch on success.')
parser.add_option('--verbose', action='store_true', parser.add_option(
help='Whether to output details.') '-v', '--verbose', action='store_true', help='Whether to output details.')
options, _ = parser.parse_args(argv[1:]) options, _ = parser.parse_args(argv[1:])
test_result = unittest.main( test_result = unittest.main(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment