def testCalledByNativeParseError(self): try: jni_generator.ExtractCalledByNatives(""" @CalledByNative public static int foo(); // This one is fine @CalledByNative scooby doo """) self.fail('Expected a ParseError') except jni_generator.ParseError, e: self.assertEquals(('@CalledByNative', 'scooby doo'), e.context_lines)
def testCalledByNatives(self): test_data = """" import android.graphics.Bitmap; import android.view.View; import java.io.InputStream; import java.util.List; class InnerClass {} @CalledByNative InnerClass showConfirmInfoBar(int nativeInfoBar, String buttonOk, String buttonCancel, String title, Bitmap icon) { InfoBar infobar = new ConfirmInfoBar(nativeInfoBar, mContext, buttonOk, buttonCancel, title, icon); return infobar; } @CalledByNative InnerClass showAutoLoginInfoBar(int nativeInfoBar, String realm, String account, String args) { AutoLoginInfoBar infobar = new AutoLoginInfoBar(nativeInfoBar, mContext, realm, account, args); if (infobar.displayedAccountCount() == 0) infobar = null; return infobar; } @CalledByNative("InfoBar") void dismiss(); @SuppressWarnings("unused") @CalledByNative private static boolean shouldShowAutoLogin(View view, String realm, String account, String args) { AccountManagerContainer accountManagerContainer = new AccountManagerContainer((Activity)contentView.getContext(), realm, account, args); String[] logins = accountManagerContainer.getAccountLogins(null); return logins.length != 0; } @CalledByNative static InputStream openUrl(String url) { return null; } @CalledByNative private void activateHardwareAcceleration(final boolean activated, final int iPid, final int iType, final int iPrimaryID, final int iSecondaryID) { if (!activated) { return } } @CalledByNative public static @Status int updateStatus(@Status int status) { return getAndUpdateStatus(status); } @CalledByNativeUnchecked private void uncheckedCall(int iParam); @CalledByNative public byte[] returnByteArray(); @CalledByNative public boolean[] returnBooleanArray(); @CalledByNative public char[] returnCharArray(); @CalledByNative public short[] returnShortArray(); @CalledByNative public int[] returnIntArray(); @CalledByNative public long[] returnLongArray(); @CalledByNative public double[] returnDoubleArray(); @CalledByNative public Object[] returnObjectArray(); @CalledByNative public byte[][] returnArrayOfByteArray(); @CalledByNative public Bitmap.CompressFormat getCompressFormat(); @CalledByNative public List<Bitmap.CompressFormat> getCompressFormatList(); """ jni_params = jni_generator.JniParams('org/chromium/Foo') jni_params.ExtractImportsAndInnerClasses(test_data) called_by_natives = jni_generator.ExtractCalledByNatives(jni_params, test_data) golden_called_by_natives = [ CalledByNative( return_type='InnerClass', system_class=False, static=False, name='showConfirmInfoBar', method_id_var_name='showConfirmInfoBar', java_class_name='', params=[Param(datatype='int', name='nativeInfoBar'), Param(datatype='String', name='buttonOk'), Param(datatype='String', name='buttonCancel'), Param(datatype='String', name='title'), Param(datatype='Bitmap', name='icon')], env_call=('Object', ''), unchecked=False, ), CalledByNative( return_type='InnerClass', system_class=False, static=False, name='showAutoLoginInfoBar', method_id_var_name='showAutoLoginInfoBar', java_class_name='', params=[Param(datatype='int', name='nativeInfoBar'), Param(datatype='String', name='realm'), Param(datatype='String', name='account'), Param(datatype='String', name='args')], env_call=('Object', ''), unchecked=False, ), CalledByNative( return_type='void', system_class=False, static=False, name='dismiss', method_id_var_name='dismiss', java_class_name='InfoBar', params=[], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='boolean', system_class=False, static=True, name='shouldShowAutoLogin', method_id_var_name='shouldShowAutoLogin', java_class_name='', params=[Param(datatype='View', name='view'), Param(datatype='String', name='realm'), Param(datatype='String', name='account'), Param(datatype='String', name='args')], env_call=('Boolean', ''), unchecked=False, ), CalledByNative( return_type='InputStream', system_class=False, static=True, name='openUrl', method_id_var_name='openUrl', java_class_name='', params=[Param(datatype='String', name='url')], env_call=('Object', ''), unchecked=False, ), CalledByNative( return_type='void', system_class=False, static=False, name='activateHardwareAcceleration', method_id_var_name='activateHardwareAcceleration', java_class_name='', params=[Param(datatype='boolean', name='activated'), Param(datatype='int', name='iPid'), Param(datatype='int', name='iType'), Param(datatype='int', name='iPrimaryID'), Param(datatype='int', name='iSecondaryID'), ], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='int', system_class=False, static=True, name='updateStatus', method_id_var_name='updateStatus', java_class_name='', params=[Param(datatype='int', name='status')], env_call=('Integer', ''), unchecked=False, ), CalledByNative( return_type='void', system_class=False, static=False, name='uncheckedCall', method_id_var_name='uncheckedCall', java_class_name='', params=[Param(datatype='int', name='iParam')], env_call=('Void', ''), unchecked=True, ), CalledByNative( return_type='byte[]', system_class=False, static=False, name='returnByteArray', method_id_var_name='returnByteArray', java_class_name='', params=[], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='boolean[]', system_class=False, static=False, name='returnBooleanArray', method_id_var_name='returnBooleanArray', java_class_name='', params=[], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='char[]', system_class=False, static=False, name='returnCharArray', method_id_var_name='returnCharArray', java_class_name='', params=[], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='short[]', system_class=False, static=False, name='returnShortArray', method_id_var_name='returnShortArray', java_class_name='', params=[], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='int[]', system_class=False, static=False, name='returnIntArray', method_id_var_name='returnIntArray', java_class_name='', params=[], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='long[]', system_class=False, static=False, name='returnLongArray', method_id_var_name='returnLongArray', java_class_name='', params=[], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='double[]', system_class=False, static=False, name='returnDoubleArray', method_id_var_name='returnDoubleArray', java_class_name='', params=[], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='Object[]', system_class=False, static=False, name='returnObjectArray', method_id_var_name='returnObjectArray', java_class_name='', params=[], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='byte[][]', system_class=False, static=False, name='returnArrayOfByteArray', method_id_var_name='returnArrayOfByteArray', java_class_name='', params=[], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='Bitmap.CompressFormat', system_class=False, static=False, name='getCompressFormat', method_id_var_name='getCompressFormat', java_class_name='', params=[], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='List<Bitmap.CompressFormat>', system_class=False, static=False, name='getCompressFormatList', method_id_var_name='getCompressFormatList', java_class_name='', params=[], env_call=('Void', ''), unchecked=False, ), ] self.assertListEquals(golden_called_by_natives, called_by_natives) h = jni_generator.InlHeaderFileGenerator( '', 'org/chromium/TestJni', [], called_by_natives, [], jni_params, TestOptions()) self.assertGoldenTextEquals(h.GetContent())
def testCalledByNatives(self): test_data = """" @CalledByNative NativeInfoBar showConfirmInfoBar(int nativeInfoBar, String buttonOk, String buttonCancel, String title, Bitmap icon) { InfoBar infobar = new ConfirmInfoBar(nativeInfoBar, mContext, buttonOk, buttonCancel, title, icon); return infobar; } @CalledByNative NativeInfoBar showAutoLoginInfoBar(int nativeInfoBar, String realm, String account, String args) { AutoLoginInfoBar infobar = new AutoLoginInfoBar(nativeInfoBar, mContext, realm, account, args); if (infobar.displayedAccountCount() == 0) infobar = null; return infobar; } @CalledByNative("InfoBar") void dismiss(); @SuppressWarnings("unused") @CalledByNative private static boolean shouldShowAutoLogin(ContentView contentView, String realm, String account, String args) { AccountManagerContainer accountManagerContainer = new AccountManagerContainer((Activity)contentView.getContext(), realm, account, args); String[] logins = accountManagerContainer.getAccountLogins(null); return logins.length != 0; } @CalledByNative static InputStream openUrl(String url) { return null; } @CalledByNative private void activateHardwareAcceleration(final boolean activated, final int iPid, final int iType, final int iPrimaryID, final int iSecondaryID) { if (!activated) { return } } @CalledByNativeUnchecked private void uncheckedCall(int iParam); """ called_by_natives = jni_generator.ExtractCalledByNatives(test_data) golden_called_by_natives = [ CalledByNative( return_type='NativeInfoBar', system_class=False, static=False, name='showConfirmInfoBar', method_id_var_name='showConfirmInfoBar', java_class_name='', params=[ Param(datatype='int', name='nativeInfoBar'), Param(datatype='String', name='buttonOk'), Param(datatype='String', name='buttonCancel'), Param(datatype='String', name='title'), Param(datatype='Bitmap', name='icon') ], env_call=('Object', ''), unchecked=False, ), CalledByNative( return_type='NativeInfoBar', system_class=False, static=False, name='showAutoLoginInfoBar', method_id_var_name='showAutoLoginInfoBar', java_class_name='', params=[ Param(datatype='int', name='nativeInfoBar'), Param(datatype='String', name='realm'), Param(datatype='String', name='account'), Param(datatype='String', name='args') ], env_call=('Object', ''), unchecked=False, ), CalledByNative( return_type='void', system_class=False, static=False, name='dismiss', method_id_var_name='dismiss', java_class_name='InfoBar', params=[], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='boolean', system_class=False, static=True, name='shouldShowAutoLogin', method_id_var_name='shouldShowAutoLogin', java_class_name='', params=[ Param(datatype='ContentView', name='contentView'), Param(datatype='String', name='realm'), Param(datatype='String', name='account'), Param(datatype='String', name='args') ], env_call=('Boolean', ''), unchecked=False, ), CalledByNative( return_type='InputStream', system_class=False, static=True, name='openUrl', method_id_var_name='openUrl', java_class_name='', params=[Param(datatype='String', name='url')], env_call=('Object', ''), unchecked=False, ), CalledByNative( return_type='void', system_class=False, static=False, name='activateHardwareAcceleration', method_id_var_name='activateHardwareAcceleration', java_class_name='', params=[ Param(datatype='boolean', name='activated'), Param(datatype='int', name='iPid'), Param(datatype='int', name='iType'), Param(datatype='int', name='iPrimaryID'), Param(datatype='int', name='iSecondaryID'), ], env_call=('Void', ''), unchecked=False, ), CalledByNative( return_type='void', system_class=False, static=False, name='uncheckedCall', method_id_var_name='uncheckedCall', java_class_name='', params=[Param(datatype='int', name='iParam')], env_call=('Void', ''), unchecked=True, ), ] self.assertListEquals(golden_called_by_natives, called_by_natives) h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', [], called_by_natives) golden_content = """\ // Copyright (c) 2012 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. // This file is autogenerated by // base/android/jni_generator/jni_generator_tests.py // For // org/chromium/TestJni #ifndef org_chromium_TestJni_JNI #define org_chromium_TestJni_JNI #include <jni.h> #include "base/android/jni_android.h" #include "base/android/scoped_java_ref.h" #include "base/basictypes.h" #include "base/logging.h" using base::android::ScopedJavaLocalRef; // Step 1: forward declarations. namespace { const char kTestJniClassPath[] = "org/chromium/TestJni"; const char kInfoBarClassPath[] = "org/chromium/TestJni$InfoBar"; // Leaking this jclass as we cannot use LazyInstance from some threads. jclass g_TestJni_clazz = NULL; // Leaking this jclass as we cannot use LazyInstance from some threads. jclass g_InfoBar_clazz = NULL; } // namespace // Step 2: method stubs. static jmethodID g_TestJni_showConfirmInfoBar = 0; static ScopedJavaLocalRef<jobject> Java_TestJni_showConfirmInfoBar(JNIEnv* env, jobject obj, jint nativeInfoBar, jstring buttonOk, jstring buttonCancel, jstring title, jobject icon) { /* Must call RegisterNativesImpl() */ DCHECK(g_TestJni_clazz); DCHECK(g_TestJni_showConfirmInfoBar); jobject ret = env->CallObjectMethod(obj, g_TestJni_showConfirmInfoBar, nativeInfoBar, buttonOk, buttonCancel, title, icon); base::android::CheckException(env); return ScopedJavaLocalRef<jobject>(env, ret); } static jmethodID g_TestJni_showAutoLoginInfoBar = 0; static ScopedJavaLocalRef<jobject> Java_TestJni_showAutoLoginInfoBar(JNIEnv* env, jobject obj, jint nativeInfoBar, jstring realm, jstring account, jstring args) { /* Must call RegisterNativesImpl() */ DCHECK(g_TestJni_clazz); DCHECK(g_TestJni_showAutoLoginInfoBar); jobject ret = env->CallObjectMethod(obj, g_TestJni_showAutoLoginInfoBar, nativeInfoBar, realm, account, args); base::android::CheckException(env); return ScopedJavaLocalRef<jobject>(env, ret); } static jmethodID g_InfoBar_dismiss = 0; static void Java_InfoBar_dismiss(JNIEnv* env, jobject obj) { /* Must call RegisterNativesImpl() */ DCHECK(g_InfoBar_clazz); DCHECK(g_InfoBar_dismiss); env->CallVoidMethod(obj, g_InfoBar_dismiss); base::android::CheckException(env); } static jmethodID g_TestJni_shouldShowAutoLogin = 0; static jboolean Java_TestJni_shouldShowAutoLogin(JNIEnv* env, jobject contentView, jstring realm, jstring account, jstring args) { /* Must call RegisterNativesImpl() */ DCHECK(g_TestJni_clazz); DCHECK(g_TestJni_shouldShowAutoLogin); jboolean ret = env->CallStaticBooleanMethod(g_TestJni_clazz, g_TestJni_shouldShowAutoLogin, contentView, realm, account, args); base::android::CheckException(env); return ret; } static jmethodID g_TestJni_openUrl = 0; static ScopedJavaLocalRef<jobject> Java_TestJni_openUrl(JNIEnv* env, jstring url) { /* Must call RegisterNativesImpl() */ DCHECK(g_TestJni_clazz); DCHECK(g_TestJni_openUrl); jobject ret = env->CallStaticObjectMethod(g_TestJni_clazz, g_TestJni_openUrl, url); base::android::CheckException(env); return ScopedJavaLocalRef<jobject>(env, ret); } static jmethodID g_TestJni_activateHardwareAcceleration = 0; static void Java_TestJni_activateHardwareAcceleration(JNIEnv* env, jobject obj, jboolean activated, jint iPid, jint iType, jint iPrimaryID, jint iSecondaryID) { /* Must call RegisterNativesImpl() */ DCHECK(g_TestJni_clazz); DCHECK(g_TestJni_activateHardwareAcceleration); env->CallVoidMethod(obj, g_TestJni_activateHardwareAcceleration, activated, iPid, iType, iPrimaryID, iSecondaryID); base::android::CheckException(env); } static jmethodID g_TestJni_uncheckedCall = 0; static void Java_TestJni_uncheckedCall(JNIEnv* env, jobject obj, jint iParam) { /* Must call RegisterNativesImpl() */ DCHECK(g_TestJni_clazz); DCHECK(g_TestJni_uncheckedCall); env->CallVoidMethod(obj, g_TestJni_uncheckedCall, iParam); } // Step 3: GetMethodIDs and RegisterNatives. static void GetMethodIDsImpl(JNIEnv* env) { g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( base::android::GetUnscopedClass(env, kTestJniClassPath))); g_InfoBar_clazz = reinterpret_cast<jclass>(env->NewGlobalRef( base::android::GetUnscopedClass(env, kInfoBarClassPath))); g_TestJni_showConfirmInfoBar = base::android::GetMethodID( env, g_TestJni_clazz, "showConfirmInfoBar", "(" "I" "Ljava/lang/String;" "Ljava/lang/String;" "Ljava/lang/String;" "Landroid/graphics/Bitmap;" ")" "Lcom/google/android/apps/chrome/infobar/InfoBarContainer$NativeInfoBar;"); g_TestJni_showAutoLoginInfoBar = base::android::GetMethodID( env, g_TestJni_clazz, "showAutoLoginInfoBar", "(" "I" "Ljava/lang/String;" "Ljava/lang/String;" "Ljava/lang/String;" ")" "Lcom/google/android/apps/chrome/infobar/InfoBarContainer$NativeInfoBar;"); g_InfoBar_dismiss = base::android::GetMethodID( env, g_InfoBar_clazz, "dismiss", "(" ")" "V"); g_TestJni_shouldShowAutoLogin = base::android::GetStaticMethodID( env, g_TestJni_clazz, "shouldShowAutoLogin", "(" "Lorg/chromium/content/browser/ContentView;" "Ljava/lang/String;" "Ljava/lang/String;" "Ljava/lang/String;" ")" "Z"); g_TestJni_openUrl = base::android::GetStaticMethodID( env, g_TestJni_clazz, "openUrl", "(" "Ljava/lang/String;" ")" "Ljava/io/InputStream;"); g_TestJni_activateHardwareAcceleration = base::android::GetMethodID( env, g_TestJni_clazz, "activateHardwareAcceleration", "(" "Z" "I" "I" "I" "I" ")" "V"); g_TestJni_uncheckedCall = base::android::GetMethodID( env, g_TestJni_clazz, "uncheckedCall", "(" "I" ")" "V"); } static bool RegisterNativesImpl(JNIEnv* env) { GetMethodIDsImpl(env); return true; } #endif // org_chromium_TestJni_JNI """ self.assertTextEquals(golden_content, h.GetContent())