コード例 #1
0
    def testNatives(self):
        test_data = """"
    import android.graphics.Bitmap;
    import android.view.View;

    interface OnFrameAvailableListener {}
    private native int nativeInit();
    private native void nativeDestroy(int nativeChromeBrowserProvider);
    private native long nativeAddBookmark(
            int nativeChromeBrowserProvider,
            String url, String title, boolean isFolder, long parentId);
    private static native String nativeGetDomainAndRegistry(String url);
    private static native void nativeCreateHistoricalTabFromState(
            byte[] state, int tab_index);
    private native byte[] nativeGetStateAsByteArray(View view);
    private static native String[] nativeGetAutofillProfileGUIDs();
    private native void nativeSetRecognitionResults(
            int sessionId, String[] results);
    private native long nativeAddBookmarkFromAPI(
            int nativeChromeBrowserProvider,
            String url, Long created, Boolean isBookmark,
            Long date, byte[] favicon, String title, Integer visits);
    native int nativeFindAll(String find);
    private static native OnFrameAvailableListener nativeGetInnerClass();
    private native Bitmap nativeQueryBitmap(
            int nativeChromeBrowserProvider,
            String[] projection, String selection,
            String[] selectionArgs, String sortOrder);
    private native void nativeGotOrientation(
            int nativeDataFetcherImplAndroid,
            double alpha, double beta, double gamma);
    private static native Throwable nativeMessWithJavaException(Throwable e);
    """
        jni_params = jni_generator.JniParams(
            'org/chromium/example/jni_generator/SampleForTests')
        jni_params.ExtractImportsAndInnerClasses(test_data)
        natives = jni_generator.ExtractNatives(test_data, 'int')
        golden_natives = [
            NativeMethod(return_type='int',
                         static=False,
                         name='Init',
                         params=[],
                         java_class_name=None,
                         type='function'),
            NativeMethod(return_type='void',
                         static=False,
                         name='Destroy',
                         params=[
                             Param(datatype='int',
                                   name='nativeChromeBrowserProvider')
                         ],
                         java_class_name=None,
                         type='method',
                         p0_type='ChromeBrowserProvider'),
            NativeMethod(return_type='long',
                         static=False,
                         name='AddBookmark',
                         params=[
                             Param(datatype='int',
                                   name='nativeChromeBrowserProvider'),
                             Param(datatype='String', name='url'),
                             Param(datatype='String', name='title'),
                             Param(datatype='boolean', name='isFolder'),
                             Param(datatype='long', name='parentId')
                         ],
                         java_class_name=None,
                         type='method',
                         p0_type='ChromeBrowserProvider'),
            NativeMethod(return_type='String',
                         static=True,
                         name='GetDomainAndRegistry',
                         params=[Param(datatype='String', name='url')],
                         java_class_name=None,
                         type='function'),
            NativeMethod(return_type='void',
                         static=True,
                         name='CreateHistoricalTabFromState',
                         params=[
                             Param(datatype='byte[]', name='state'),
                             Param(datatype='int', name='tab_index')
                         ],
                         java_class_name=None,
                         type='function'),
            NativeMethod(return_type='byte[]',
                         static=False,
                         name='GetStateAsByteArray',
                         params=[Param(datatype='View', name='view')],
                         java_class_name=None,
                         type='function'),
            NativeMethod(return_type='String[]',
                         static=True,
                         name='GetAutofillProfileGUIDs',
                         params=[],
                         java_class_name=None,
                         type='function'),
            NativeMethod(return_type='void',
                         static=False,
                         name='SetRecognitionResults',
                         params=[
                             Param(datatype='int', name='sessionId'),
                             Param(datatype='String[]', name='results')
                         ],
                         java_class_name=None,
                         type='function'),
            NativeMethod(return_type='long',
                         static=False,
                         name='AddBookmarkFromAPI',
                         params=[
                             Param(datatype='int',
                                   name='nativeChromeBrowserProvider'),
                             Param(datatype='String', name='url'),
                             Param(datatype='Long', name='created'),
                             Param(datatype='Boolean', name='isBookmark'),
                             Param(datatype='Long', name='date'),
                             Param(datatype='byte[]', name='favicon'),
                             Param(datatype='String', name='title'),
                             Param(datatype='Integer', name='visits')
                         ],
                         java_class_name=None,
                         type='method',
                         p0_type='ChromeBrowserProvider'),
            NativeMethod(return_type='int',
                         static=False,
                         name='FindAll',
                         params=[Param(datatype='String', name='find')],
                         java_class_name=None,
                         type='function'),
            NativeMethod(return_type='OnFrameAvailableListener',
                         static=True,
                         name='GetInnerClass',
                         params=[],
                         java_class_name=None,
                         type='function'),
            NativeMethod(return_type='Bitmap',
                         static=False,
                         name='QueryBitmap',
                         params=[
                             Param(datatype='int',
                                   name='nativeChromeBrowserProvider'),
                             Param(datatype='String[]', name='projection'),
                             Param(datatype='String', name='selection'),
                             Param(datatype='String[]', name='selectionArgs'),
                             Param(datatype='String', name='sortOrder'),
                         ],
                         java_class_name=None,
                         type='method',
                         p0_type='ChromeBrowserProvider'),
            NativeMethod(return_type='void',
                         static=False,
                         name='GotOrientation',
                         params=[
                             Param(datatype='int',
                                   name='nativeDataFetcherImplAndroid'),
                             Param(datatype='double', name='alpha'),
                             Param(datatype='double', name='beta'),
                             Param(datatype='double', name='gamma'),
                         ],
                         java_class_name=None,
                         type='method',
                         p0_type='content::DataFetcherImplAndroid'),
            NativeMethod(return_type='Throwable',
                         static=True,
                         name='MessWithJavaException',
                         params=[Param(datatype='Throwable', name='e')],
                         java_class_name=None,
                         type='function')
        ]
        self.assertListEquals(golden_natives, natives)
        h1 = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni',
                                                  natives, [], [], jni_params,
                                                  TestOptions())
        self.assertGoldenTextEquals(h1.GetContent())
        content = {}
        h2 = jni_registration_generator.HeaderGenerator(
            '', 'org/chromium/TestJni', natives, jni_params, content, True)
        h2.AddContent()
        self.assertGoldenTextEquals(
            jni_registration_generator.CreateFromDict(content),
            suffix='Registrations')
コード例 #2
0
  def testNatives(self):
    test_data = """"
    private native int nativeInit();
    private native void nativeDestroy(int nativeChromeBrowserProvider);
    private native long nativeAddBookmark(
            int nativeChromeBrowserProvider,
            String url, String title, boolean isFolder, long parentId);
    private static native String nativeGetDomainAndRegistry(String url);
    private static native void nativeCreateHistoricalTabFromState(
            byte[] state, int tab_index);
    private native byte[] nativeGetStateAsByteArray(ChromeView view);
    private static native String[] nativeGetAutofillProfileGUIDs();
    private native void nativeSetRecognitionResults(
            int sessionId, String[] results);
    private native long nativeAddBookmarkFromAPI(
            int nativeChromeBrowserProvider,
            String url, Long created, Boolean isBookmark,
            Long date, byte[] favicon, String title, Integer visits);
    native int nativeFindAll(String find);
    private static native BookmarkNode nativeGetDefaultBookmarkFolder();
    private native SQLiteCursor nativeQueryBookmarkFromAPI(
            int nativeChromeBrowserProvider,
            String[] projection, String selection,
            String[] selectionArgs, String sortOrder);
    private native void nativeGotOrientation(
            int nativePtr /* device_orientation::DeviceOrientationAndroid */,
            double alpha, double beta, double gamma);
    """
    natives = jni_generator.ExtractNatives(test_data)
    golden_natives = [
        NativeMethod(return_type='int', static=False,
                     name='Init',
                     params=[],
                     java_class_name='',
                     type='function'),
        NativeMethod(return_type='void', static=False, name='Destroy',
                     params=[Param(datatype='int',
                                   name='nativeChromeBrowserProvider')],
                     java_class_name='',
                     type='method',
                     p0_type='ChromeBrowserProvider'),
        NativeMethod(return_type='long', static=False, name='AddBookmark',
                     params=[Param(datatype='int',
                                   name='nativeChromeBrowserProvider'),
                             Param(datatype='String',
                                   name='url'),
                             Param(datatype='String',
                                   name='title'),
                             Param(datatype='boolean',
                                   name='isFolder'),
                             Param(datatype='long',
                                   name='parentId')],
                     java_class_name='',
                     type='method',
                     p0_type='ChromeBrowserProvider'),
        NativeMethod(return_type='String', static=True,
                     name='GetDomainAndRegistry',
                     params=[Param(datatype='String',
                                   name='url')],
                     java_class_name='',
                     type='function'),
        NativeMethod(return_type='void', static=True,
                     name='CreateHistoricalTabFromState',
                     params=[Param(datatype='byte[]',
                                   name='state'),
                             Param(datatype='int',
                                   name='tab_index')],
                     java_class_name='',
                     type='function'),
        NativeMethod(return_type='byte[]', static=False,
                     name='GetStateAsByteArray',
                     params=[Param(datatype='ChromeView', name='view')],
                     java_class_name='',
                     type='function'),
        NativeMethod(return_type='String[]', static=True,
                     name='GetAutofillProfileGUIDs', params=[],
                     java_class_name='',
                     type='function'),
        NativeMethod(return_type='void', static=False,
                     name='SetRecognitionResults',
                     params=[Param(datatype='int', name='sessionId'),
                             Param(datatype='String[]', name='results')],
                     java_class_name='',
                     type='function'),
        NativeMethod(return_type='long', static=False,
                     name='AddBookmarkFromAPI',
                     params=[Param(datatype='int',
                                   name='nativeChromeBrowserProvider'),
                             Param(datatype='String',
                                   name='url'),
                             Param(datatype='Long',
                                   name='created'),
                             Param(datatype='Boolean',
                                   name='isBookmark'),
                             Param(datatype='Long',
                                   name='date'),
                             Param(datatype='byte[]',
                                   name='favicon'),
                             Param(datatype='String',
                                   name='title'),
                             Param(datatype='Integer',
                                   name='visits')],
                     java_class_name='',
                     type='method',
                     p0_type='ChromeBrowserProvider'),
        NativeMethod(return_type='int', static=False,
                     name='FindAll',
                     params=[Param(datatype='String',
                                   name='find')],
                     java_class_name='',
                     type='function'),
        NativeMethod(return_type='BookmarkNode', static=True,
                     name='GetDefaultBookmarkFolder',
                     params=[],
                     java_class_name='',
                     type='function'),
        NativeMethod(return_type='SQLiteCursor',
                     static=False,
                     name='QueryBookmarkFromAPI',
                     params=[Param(datatype='int',
                                   name='nativeChromeBrowserProvider'),
                             Param(datatype='String[]',
                                   name='projection'),
                             Param(datatype='String',
                                   name='selection'),
                             Param(datatype='String[]',
                                   name='selectionArgs'),
                             Param(datatype='String',
                                   name='sortOrder'),
                            ],
                     java_class_name='',
                     type='method',
                     p0_type='ChromeBrowserProvider'),
        NativeMethod(return_type='void', static=False,
                     name='GotOrientation',
                     params=[Param(datatype='int',
                                   cpp_class_name=
                                 'device_orientation::DeviceOrientationAndroid',
                                   name='nativePtr'),
                             Param(datatype='double',
                                   name='alpha'),
                             Param(datatype='double',
                                   name='beta'),
                             Param(datatype='double',
                                   name='gamma'),
                            ],
                     java_class_name='',
                     type='method',
                     p0_type='device_orientation::DeviceOrientationAndroid'),
    ]
    self.assertListEquals(golden_natives, natives)
    h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni',
                                             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";
// Leaking this jclass as we cannot use LazyInstance from some threads.
jclass g_TestJni_clazz = NULL;
}  // namespace

static jint Init(JNIEnv* env, jobject obj);


static jstring GetDomainAndRegistry(JNIEnv* env, jclass clazz,
    jstring url);


static void CreateHistoricalTabFromState(JNIEnv* env, jclass clazz,
    jbyteArray state,
    jint tab_index);


static jbyteArray GetStateAsByteArray(JNIEnv* env, jobject obj,
    jobject view);


static jobjectArray GetAutofillProfileGUIDs(JNIEnv* env, jclass clazz);


static void SetRecognitionResults(JNIEnv* env, jobject obj,
    jint sessionId,
    jobjectArray results);


static jint FindAll(JNIEnv* env, jobject obj,
    jstring find);


static jobject GetDefaultBookmarkFolder(JNIEnv* env, jclass clazz);


// Step 2: method stubs.
static void Destroy(JNIEnv* env, jobject obj,
    jint nativeChromeBrowserProvider) {
  DCHECK(nativeChromeBrowserProvider) << "Destroy";
  ChromeBrowserProvider* native =
      reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider);
  return native->Destroy(env, obj);
}

static jlong AddBookmark(JNIEnv* env, jobject obj,
    jint nativeChromeBrowserProvider,
    jstring url,
    jstring title,
    jboolean isFolder,
    jlong parentId) {
  DCHECK(nativeChromeBrowserProvider) << "AddBookmark";
  ChromeBrowserProvider* native =
      reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider);
  return native->AddBookmark(env, obj, url, title, isFolder, parentId);
}

static jlong AddBookmarkFromAPI(JNIEnv* env, jobject obj,
    jint nativeChromeBrowserProvider,
    jstring url,
    jobject created,
    jobject isBookmark,
    jobject date,
    jbyteArray favicon,
    jstring title,
    jobject visits) {
  DCHECK(nativeChromeBrowserProvider) << "AddBookmarkFromAPI";
  ChromeBrowserProvider* native =
      reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider);
  return native->AddBookmarkFromAPI(env, obj, url, created, isBookmark, date,
      favicon, title, visits);
}

static jobject QueryBookmarkFromAPI(JNIEnv* env, jobject obj,
    jint nativeChromeBrowserProvider,
    jobjectArray projection,
    jstring selection,
    jobjectArray selectionArgs,
    jstring sortOrder) {
  DCHECK(nativeChromeBrowserProvider) << "QueryBookmarkFromAPI";
  ChromeBrowserProvider* native =
      reinterpret_cast<ChromeBrowserProvider*>(nativeChromeBrowserProvider);
  return native->QueryBookmarkFromAPI(env, obj, projection, selection,
      selectionArgs, sortOrder).Release();
}

static void GotOrientation(JNIEnv* env, jobject obj,
    jint nativePtr,
    jdouble alpha,
    jdouble beta,
    jdouble gamma) {
  DCHECK(nativePtr) << "GotOrientation";
  device_orientation::DeviceOrientationAndroid* native =
    reinterpret_cast<device_orientation::DeviceOrientationAndroid*>(nativePtr);
  return native->GotOrientation(env, obj, alpha, beta, gamma);
}


// Step 3: GetMethodIDs and RegisterNatives.


static void GetMethodIDsImpl(JNIEnv* env) {
  g_TestJni_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
      base::android::GetUnscopedClass(env, kTestJniClassPath)));
}

static bool RegisterNativesImpl(JNIEnv* env) {
  GetMethodIDsImpl(env);

  static const JNINativeMethod kMethodsTestJni[] = {
    { "nativeInit",
"("
")"
"I", reinterpret_cast<void*>(Init) },
    { "nativeDestroy",
"("
"I"
")"
"V", reinterpret_cast<void*>(Destroy) },
    { "nativeAddBookmark",
"("
"I"
"Ljava/lang/String;"
"Ljava/lang/String;"
"Z"
"J"
")"
"J", reinterpret_cast<void*>(AddBookmark) },
    { "nativeGetDomainAndRegistry",
"("
"Ljava/lang/String;"
")"
"Ljava/lang/String;", reinterpret_cast<void*>(GetDomainAndRegistry) },
    { "nativeCreateHistoricalTabFromState",
"("
"[B"
"I"
")"
"V", reinterpret_cast<void*>(CreateHistoricalTabFromState) },
    { "nativeGetStateAsByteArray",
"("
"Lorg/chromium/chromeview/ChromeView;"
")"
"[B", reinterpret_cast<void*>(GetStateAsByteArray) },
    { "nativeGetAutofillProfileGUIDs",
"("
")"
"[Ljava/lang/String;", reinterpret_cast<void*>(GetAutofillProfileGUIDs) },
    { "nativeSetRecognitionResults",
"("
"I"
"[Ljava/lang/String;"
")"
"V", reinterpret_cast<void*>(SetRecognitionResults) },
    { "nativeAddBookmarkFromAPI",
"("
"I"
"Ljava/lang/String;"
"Ljava/lang/Long;"
"Ljava/lang/Boolean;"
"Ljava/lang/Long;"
"[B"
"Ljava/lang/String;"
"Ljava/lang/Integer;"
")"
"J", reinterpret_cast<void*>(AddBookmarkFromAPI) },
    { "nativeFindAll",
"("
"Ljava/lang/String;"
")"
"I", reinterpret_cast<void*>(FindAll) },
    { "nativeGetDefaultBookmarkFolder",
"("
")"
"Lcom/android/chrome/ChromeBrowserProvider$BookmarkNode;",
    reinterpret_cast<void*>(GetDefaultBookmarkFolder) },
    { "nativeQueryBookmarkFromAPI",
"("
"I"
"[Ljava/lang/String;"
"Ljava/lang/String;"
"[Ljava/lang/String;"
"Ljava/lang/String;"
")"
"Lcom/android/chrome/database/SQLiteCursor;",
    reinterpret_cast<void*>(QueryBookmarkFromAPI) },
    { "nativeGotOrientation",
"("
"I"
"D"
"D"
"D"
")"
"V", reinterpret_cast<void*>(GotOrientation) },
  };
  const int kMethodsTestJniSize = arraysize(kMethodsTestJni);

  if (env->RegisterNatives(g_TestJni_clazz,
                           kMethodsTestJni,
                           kMethodsTestJniSize) < 0) {
    LOG(ERROR) << "RegisterNatives failed in " << __FILE__;
    return false;
  }

  return true;
}

#endif  // org_chromium_TestJni_JNI
"""
    self.assertTextEquals(golden_content, h.GetContent())
コード例 #3
0
  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(ChromeView chromeView,
            String realm, String account, String args) {
        AccountManagerContainer accountManagerContainer =
            new AccountManagerContainer((Activity)chromeView.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='ChromeView', name='chromeView'),
                    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
    chromeView,
    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, chromeView, 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/android/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/android/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/chromeview/ChromeView;"
"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())
コード例 #4
0
    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
      }
    }
    @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_generator.JniParams.SetFullyQualifiedClass('org/chromium/Foo')
        jni_generator.JniParams.ExtractImportsAndInnerClasses(test_data)
        called_by_natives = jni_generator.ExtractCalledByNatives(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='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, [],
                                                 TestOptions())
        self.assertGoldenTextEquals(h.GetContent())