OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/python |
| 2 # |
| 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. |
| 6 |
| 7 """Tests for jni_generator.py. |
| 8 |
| 9 This test suite contains various tests for the JNI generator. |
| 10 It exercises the low-level parser all the way up to the |
| 11 code generator and ensures the output matches a golden |
| 12 file. |
| 13 """ |
| 14 |
| 15 import difflib |
| 16 import os |
| 17 import sys |
| 18 import unittest |
| 19 import jni_generator |
| 20 from jni_generator import CalledByNative, NativeMethod, Param |
| 21 |
| 22 |
| 23 class TestGenerator(unittest.TestCase): |
| 24 def assertObjEquals(self, first, second): |
| 25 dict_first = first.__dict__ |
| 26 dict_second = second.__dict__ |
| 27 self.assertEquals(dict_first.keys(), dict_second.keys()) |
| 28 for key, value in dict_first.iteritems(): |
| 29 if (type(value) is list and len(value) and |
| 30 isinstance(type(value[0]), object)): |
| 31 self.assertListEquals(value, second.__getattribute__(key)) |
| 32 else: |
| 33 self.assertEquals(value, second.__getattribute__(key)) |
| 34 |
| 35 def assertListEquals(self, first, second): |
| 36 self.assertEquals(len(first), len(second)) |
| 37 for i in xrange(len(first)): |
| 38 if isinstance(first[i], object): |
| 39 self.assertObjEquals(first[i], second[i]) |
| 40 else: |
| 41 self.assertEquals(first[i], second[i]) |
| 42 |
| 43 def assertTextEquals(self, golden_text, generated_text): |
| 44 stripped_golden = [l.strip() for l in golden_text.split('\n')] |
| 45 stripped_generated = [l.strip() for l in generated_text.split('\n')] |
| 46 if stripped_golden != stripped_generated: |
| 47 print self.id() |
| 48 for line in difflib.context_diff(stripped_golden, stripped_generated): |
| 49 print line |
| 50 self.fail('Golden text mismatch') |
| 51 |
| 52 def testNatives(self): |
| 53 test_data = """" |
| 54 private native int nativeInit(); |
| 55 private native void nativeDestroy(int nativeChromeBrowserProvider); |
| 56 private native long nativeAddBookmark( |
| 57 int nativeChromeBrowserProvider, |
| 58 String url, String title, boolean isFolder, long parentId); |
| 59 private static native String nativeGetDomainAndRegistry(String url); |
| 60 private static native void nativeCreateHistoricalTabFromState(byte[] state,
int tab_index); |
| 61 private native byte[] nativeGetStateAsByteArray(ChromeView view); |
| 62 private static native String[] nativeGetAutofillProfileGUIDs(); |
| 63 private native void nativeSetRecognitionResults(int sessionId, String[] resu
lts); |
| 64 private native long nativeAddBookmarkFromAPI(int nativeChromeBrowserProvider
, |
| 65 String url, Long created, Boolean isBookmark, |
| 66 Long date, byte[] favicon, String title, Integer visits); |
| 67 native int nativeFindAll(String find); |
| 68 private static native BookmarkNode nativeGetDefaultBookmarkFolder(); |
| 69 private native SQLiteCursor nativeQueryBookmarkFromAPI(int nativeChromeBrows
erProvider, |
| 70 String[] projection, String selection, |
| 71 String[] selectionArgs, String sortOrder); |
| 72 private native void nativeGotOrientation(int nativePtr /* device_orientation
::DeviceOrientationAndroid */, |
| 73 double alpha, double beta, double gamma); |
| 74 """ |
| 75 natives = jni_generator.ExtractNatives(test_data) |
| 76 golden_natives = [ |
| 77 NativeMethod(return_type='int', static=False, |
| 78 name='Init', |
| 79 params=[], |
| 80 java_class_name='', |
| 81 type='function'), |
| 82 NativeMethod(return_type='void', static=False, name='Destroy', |
| 83 params=[Param(datatype='int', |
| 84 name='nativeChromeBrowserProvider')], |
| 85 java_class_name='', |
| 86 type='method', |
| 87 p0_type='ChromeBrowserProvider'), |
| 88 NativeMethod(return_type='long', static=False, name='AddBookmark', |
| 89 params=[Param(datatype='int', |
| 90 name='nativeChromeBrowserProvider'), |
| 91 Param(datatype='String', |
| 92 name='url'), |
| 93 Param(datatype='String', |
| 94 name='title'), |
| 95 Param(datatype='boolean', |
| 96 name='isFolder'), |
| 97 Param(datatype='long', |
| 98 name='parentId')], |
| 99 java_class_name='', |
| 100 type='method', |
| 101 p0_type='ChromeBrowserProvider'), |
| 102 NativeMethod(return_type='String', static=True, |
| 103 name='GetDomainAndRegistry', |
| 104 params=[Param(datatype='String', |
| 105 name='url')], |
| 106 java_class_name='', |
| 107 type='function'), |
| 108 NativeMethod(return_type='void', static=True, |
| 109 name='CreateHistoricalTabFromState', |
| 110 params=[Param(datatype='byte[]', |
| 111 name='state'), |
| 112 Param(datatype='int', |
| 113 name='tab_index')], |
| 114 java_class_name='', |
| 115 type='function'), |
| 116 NativeMethod(return_type='byte[]', static=False, |
| 117 name='GetStateAsByteArray', |
| 118 params=[Param(datatype='ChromeView', name='view')], |
| 119 java_class_name='', |
| 120 type='function'), |
| 121 NativeMethod(return_type='String[]', static=True, |
| 122 name='GetAutofillProfileGUIDs', params=[], |
| 123 java_class_name='', |
| 124 type='function'), |
| 125 NativeMethod(return_type='void', static=False, |
| 126 name='SetRecognitionResults', |
| 127 params=[Param(datatype='int', name='sessionId'), |
| 128 Param(datatype='String[]', name='results')], |
| 129 java_class_name='', |
| 130 type='function'), |
| 131 NativeMethod(return_type='long', static=False, |
| 132 name='AddBookmarkFromAPI', |
| 133 params=[Param(datatype='int', |
| 134 name='nativeChromeBrowserProvider'), |
| 135 Param(datatype='String', |
| 136 name='url'), |
| 137 Param(datatype='Long', |
| 138 name='created'), |
| 139 Param(datatype='Boolean', |
| 140 name='isBookmark'), |
| 141 Param(datatype='Long', |
| 142 name='date'), |
| 143 Param(datatype='byte[]', |
| 144 name='favicon'), |
| 145 Param(datatype='String', |
| 146 name='title'), |
| 147 Param(datatype='Integer', |
| 148 name='visits')], |
| 149 java_class_name='', |
| 150 type='method', |
| 151 p0_type='ChromeBrowserProvider'), |
| 152 NativeMethod(return_type='int', static=False, |
| 153 name='FindAll', |
| 154 params=[Param(datatype='String', |
| 155 name='find')], |
| 156 java_class_name='', |
| 157 type='function'), |
| 158 NativeMethod(return_type='BookmarkNode', static=True, |
| 159 name='GetDefaultBookmarkFolder', |
| 160 params=[], |
| 161 java_class_name='', |
| 162 type='function'), |
| 163 NativeMethod(return_type='SQLiteCursor', |
| 164 static=False, |
| 165 name='QueryBookmarkFromAPI', |
| 166 params=[Param(datatype='int', |
| 167 name='nativeChromeBrowserProvider'), |
| 168 Param(datatype='String[]', |
| 169 name='projection'), |
| 170 Param(datatype='String', |
| 171 name='selection'), |
| 172 Param(datatype='String[]', |
| 173 name='selectionArgs'), |
| 174 Param(datatype='String', |
| 175 name='sortOrder'), |
| 176 ], |
| 177 java_class_name='', |
| 178 type='method', |
| 179 p0_type='ChromeBrowserProvider'), |
| 180 NativeMethod(return_type='void', static=False, |
| 181 name='GotOrientation', |
| 182 params=[Param(datatype='int', |
| 183 cpp_class_name='device_orientation::DeviceOri
entationAndroid', |
| 184 name='nativePtr'), |
| 185 Param(datatype='double', |
| 186 name='alpha'), |
| 187 Param(datatype='double', |
| 188 name='beta'), |
| 189 Param(datatype='double', |
| 190 name='gamma'), |
| 191 ], |
| 192 java_class_name='', |
| 193 type='method', |
| 194 p0_type='device_orientation::DeviceOrientationAndroid'), |
| 195 ] |
| 196 self.assertListEquals(golden_natives, natives) |
| 197 h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', |
| 198 natives, []) |
| 199 golden_content = """\ |
| 200 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 201 // Use of this source code is governed by a BSD-style license that can be |
| 202 // found in the LICENSE file. |
| 203 |
| 204 |
| 205 // This file is autogenerated by base/android/jni_generator/jni_generator_tests.
py |
| 206 // For org/chromium/TestJni |
| 207 |
| 208 #ifndef org_chromium_TestJni_JNI |
| 209 #define org_chromium_TestJni_JNI |
| 210 |
| 211 #include <jni.h> |
| 212 |
| 213 #include "base/android/jni_android.h" |
| 214 #include "base/android/scoped_java_ref.h" |
| 215 #include "base/basictypes.h" |
| 216 #include "base/logging.h" |
| 217 |
| 218 using base::android::ScopedJavaLocalRef; |
| 219 |
| 220 // Step 1: forward declarations. |
| 221 namespace { |
| 222 const char* const kTestJniClassPath = "org/chromium/TestJni"; |
| 223 // Leaking this JavaRef as we cannot use LazyInstance from some threads. |
| 224 base::android::ScopedJavaGlobalRef<jclass>& |
| 225 g_TestJni_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>()); |
| 226 } // namespace |
| 227 |
| 228 static jint Init(JNIEnv* env, jobject obj); |
| 229 |
| 230 |
| 231 static jstring GetDomainAndRegistry(JNIEnv* env, jclass clazz, |
| 232 jstring url); |
| 233 |
| 234 |
| 235 static void CreateHistoricalTabFromState(JNIEnv* env, jclass clazz, |
| 236 jbyteArray state, |
| 237 jint tab_index); |
| 238 |
| 239 |
| 240 static jbyteArray GetStateAsByteArray(JNIEnv* env, jobject obj, |
| 241 jobject view); |
| 242 |
| 243 |
| 244 static jobjectArray GetAutofillProfileGUIDs(JNIEnv* env, jclass clazz); |
| 245 |
| 246 |
| 247 static void SetRecognitionResults(JNIEnv* env, jobject obj, |
| 248 jint sessionId, |
| 249 jobjectArray results); |
| 250 |
| 251 |
| 252 static jint FindAll(JNIEnv* env, jobject obj, |
| 253 jstring find); |
| 254 |
| 255 |
| 256 static jobject GetDefaultBookmarkFolder(JNIEnv* env, jclass clazz); |
| 257 |
| 258 |
| 259 // Step 2: method stubs. |
| 260 static void Destroy(JNIEnv* env, jobject obj, |
| 261 jint nativeChromeBrowserProvider) { |
| 262 DCHECK(nativeChromeBrowserProvider) << "Destroy"; |
| 263 ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativ
eChromeBrowserProvider); |
| 264 return native->Destroy(env, obj); |
| 265 } |
| 266 |
| 267 static jlong AddBookmark(JNIEnv* env, jobject obj, |
| 268 jint nativeChromeBrowserProvider, |
| 269 jstring url, |
| 270 jstring title, |
| 271 jboolean isFolder, |
| 272 jlong parentId) { |
| 273 DCHECK(nativeChromeBrowserProvider) << "AddBookmark"; |
| 274 ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativ
eChromeBrowserProvider); |
| 275 return native->AddBookmark(env, obj, url, title, isFolder, parentId); |
| 276 } |
| 277 |
| 278 static jlong AddBookmarkFromAPI(JNIEnv* env, jobject obj, |
| 279 jint nativeChromeBrowserProvider, |
| 280 jstring url, |
| 281 jobject created, |
| 282 jobject isBookmark, |
| 283 jobject date, |
| 284 jbyteArray favicon, |
| 285 jstring title, |
| 286 jobject visits) { |
| 287 DCHECK(nativeChromeBrowserProvider) << "AddBookmarkFromAPI"; |
| 288 ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativ
eChromeBrowserProvider); |
| 289 return native->AddBookmarkFromAPI(env, obj, url, created, isBookmark, date, fa
vicon, title, visits); |
| 290 } |
| 291 |
| 292 static jobject QueryBookmarkFromAPI(JNIEnv* env, jobject obj, |
| 293 jint nativeChromeBrowserProvider, |
| 294 jobjectArray projection, |
| 295 jstring selection, |
| 296 jobjectArray selectionArgs, |
| 297 jstring sortOrder) { |
| 298 DCHECK(nativeChromeBrowserProvider) << "QueryBookmarkFromAPI"; |
| 299 ChromeBrowserProvider* native = reinterpret_cast<ChromeBrowserProvider*>(nativ
eChromeBrowserProvider); |
| 300 return native->QueryBookmarkFromAPI(env, obj, projection, selection, selection
Args, sortOrder).Release(); |
| 301 } |
| 302 |
| 303 static void GotOrientation(JNIEnv* env, jobject obj, |
| 304 jint nativePtr, |
| 305 jdouble alpha, |
| 306 jdouble beta, |
| 307 jdouble gamma) { |
| 308 DCHECK(nativePtr) << "GotOrientation"; |
| 309 device_orientation::DeviceOrientationAndroid* native = reinterpret_cast<device
_orientation::DeviceOrientationAndroid*>(nativePtr); |
| 310 return native->GotOrientation(env, obj, alpha, beta, gamma); |
| 311 } |
| 312 |
| 313 |
| 314 // Step 3: GetMethodIDs and RegisterNatives. |
| 315 |
| 316 |
| 317 static void GetMethodIDsImpl(JNIEnv* env) { |
| 318 g_TestJni_clazz.Reset(base::android::GetClass(env, kTestJniClassPath)); |
| 319 } |
| 320 |
| 321 static bool RegisterNativesImpl(JNIEnv* env) { |
| 322 GetMethodIDsImpl(env); |
| 323 |
| 324 static const JNINativeMethod kMethodsTestJni[] = { |
| 325 { "nativeInit", "()I", reinterpret_cast<void*>(Init) }, |
| 326 { "nativeDestroy", "(I)V", reinterpret_cast<void*>(Destroy) }, |
| 327 { "nativeAddBookmark", "(ILjava/lang/String;Ljava/lang/String;ZJ)J", reinter
pret_cast<void*>(AddBookmark) }, |
| 328 { "nativeGetDomainAndRegistry", "(Ljava/lang/String;)Ljava/lang/String;", re
interpret_cast<void*>(GetDomainAndRegistry) }, |
| 329 { "nativeCreateHistoricalTabFromState", "([BI)V", reinterpret_cast<void*>(Cr
eateHistoricalTabFromState) }, |
| 330 { "nativeGetStateAsByteArray", "(Lorg/chromium/chromeview/ChromeView;)[B", r
einterpret_cast<void*>(GetStateAsByteArray) }, |
| 331 { "nativeGetAutofillProfileGUIDs", "()[Ljava/lang/String;", reinterpret_cast
<void*>(GetAutofillProfileGUIDs) }, |
| 332 { "nativeSetRecognitionResults", "(I[Ljava/lang/String;)V", reinterpret_cast
<void*>(SetRecognitionResults) }, |
| 333 { "nativeAddBookmarkFromAPI", "(ILjava/lang/String;Ljava/lang/Long;Ljava/lan
g/Boolean;Ljava/lang/Long;[BLjava/lang/String;Ljava/lang/Integer;)J", reinterpre
t_cast<void*>(AddBookmarkFromAPI) }, |
| 334 { "nativeFindAll", "(Ljava/lang/String;)I", reinterpret_cast<void*>(FindAll)
}, |
| 335 { "nativeGetDefaultBookmarkFolder", "()Lcom/android/chrome/ChromeBrowserProv
ider$BookmarkNode;", reinterpret_cast<void*>(GetDefaultBookmarkFolder) }, |
| 336 { "nativeQueryBookmarkFromAPI", "(I[Ljava/lang/String;Ljava/lang/String;[Lja
va/lang/String;Ljava/lang/String;)Lcom/android/chrome/database/SQLiteCursor;", r
einterpret_cast<void*>(QueryBookmarkFromAPI) }, |
| 337 { "nativeGotOrientation", "(IDDD)V", reinterpret_cast<void*>(GotOrientation)
}, |
| 338 }; |
| 339 const int kMethodsTestJniSize = arraysize(kMethodsTestJni); |
| 340 |
| 341 if (env->RegisterNatives(g_TestJni_clazz.obj(), |
| 342 kMethodsTestJni, |
| 343 kMethodsTestJniSize) < 0) { |
| 344 LOG(ERROR) << "RegisterNatives failed in " << __FILE__; |
| 345 return false; |
| 346 } |
| 347 |
| 348 return true; |
| 349 } |
| 350 |
| 351 #endif // org_chromium_TestJni_JNI |
| 352 """ |
| 353 self.assertTextEquals(golden_content, h.GetContent()) |
| 354 |
| 355 def testInnerClassNatives(self): |
| 356 test_data = """ |
| 357 class MyInnerClass { |
| 358 @NativeCall("MyInnerClass") |
| 359 private native int nativeInit(); |
| 360 } |
| 361 """ |
| 362 natives = jni_generator.ExtractNatives(test_data) |
| 363 golden_natives = [ |
| 364 NativeMethod(return_type='int', static=False, |
| 365 name='Init', params=[], |
| 366 java_class_name='MyInnerClass', |
| 367 type='function') |
| 368 ] |
| 369 self.assertListEquals(golden_natives, natives) |
| 370 h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', |
| 371 natives, []) |
| 372 golden_content = """\ |
| 373 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 374 // Use of this source code is governed by a BSD-style license that can be |
| 375 // found in the LICENSE file. |
| 376 |
| 377 |
| 378 // This file is autogenerated by base/android/jni_generator/jni_generator_tests.
py |
| 379 // For org/chromium/TestJni |
| 380 |
| 381 #ifndef org_chromium_TestJni_JNI |
| 382 #define org_chromium_TestJni_JNI |
| 383 |
| 384 #include <jni.h> |
| 385 |
| 386 #include "base/android/jni_android.h" |
| 387 #include "base/android/scoped_java_ref.h" |
| 388 #include "base/basictypes.h" |
| 389 #include "base/logging.h" |
| 390 |
| 391 using base::android::ScopedJavaLocalRef; |
| 392 |
| 393 // Step 1: forward declarations. |
| 394 namespace { |
| 395 const char* const kTestJniClassPath = "org/chromium/TestJni"; |
| 396 const char* const kMyInnerClassClassPath = "org/chromium/TestJni$MyInnerClass"; |
| 397 // Leaking this JavaRef as we cannot use LazyInstance from some threads. |
| 398 base::android::ScopedJavaGlobalRef<jclass>& |
| 399 g_TestJni_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>()); |
| 400 } // namespace |
| 401 |
| 402 static jint Init(JNIEnv* env, jobject obj); |
| 403 |
| 404 |
| 405 // Step 2: method stubs. |
| 406 |
| 407 |
| 408 // Step 3: GetMethodIDs and RegisterNatives. |
| 409 |
| 410 |
| 411 static void GetMethodIDsImpl(JNIEnv* env) { |
| 412 g_TestJni_clazz.Reset(base::android::GetClass(env, kTestJniClassPath)); |
| 413 } |
| 414 |
| 415 static bool RegisterNativesImpl(JNIEnv* env) { |
| 416 GetMethodIDsImpl(env); |
| 417 |
| 418 static const JNINativeMethod kMethodsMyInnerClass[] = { |
| 419 { "nativeInit", "()I", reinterpret_cast<void*>(Init) }, |
| 420 }; |
| 421 const int kMethodsMyInnerClassSize = arraysize(kMethodsMyInnerClass); |
| 422 |
| 423 if (env->RegisterNatives(g_MyInnerClass_clazz.obj(), |
| 424 kMethodsMyInnerClass, |
| 425 kMethodsMyInnerClassSize) < 0) { |
| 426 LOG(ERROR) << "RegisterNatives failed in " << __FILE__; |
| 427 return false; |
| 428 } |
| 429 |
| 430 return true; |
| 431 } |
| 432 |
| 433 #endif // org_chromium_TestJni_JNI |
| 434 """ |
| 435 self.assertTextEquals(golden_content, h.GetContent()) |
| 436 |
| 437 def testInnerClassNativesMultiple(self): |
| 438 test_data = """ |
| 439 class MyInnerClass { |
| 440 @NativeCall("MyInnerClass") |
| 441 private native int nativeInit(); |
| 442 } |
| 443 class MyOtherInnerClass { |
| 444 @NativeCall("MyOtherInnerClass") |
| 445 private native int nativeInit(); |
| 446 } |
| 447 """ |
| 448 natives = jni_generator.ExtractNatives(test_data) |
| 449 golden_natives = [ |
| 450 NativeMethod(return_type='int', static=False, |
| 451 name='Init', params=[], |
| 452 java_class_name='MyInnerClass', |
| 453 type='function'), |
| 454 NativeMethod(return_type='int', static=False, |
| 455 name='Init', params=[], |
| 456 java_class_name='MyOtherInnerClass', |
| 457 type='function') |
| 458 ] |
| 459 self.assertListEquals(golden_natives, natives) |
| 460 h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', |
| 461 natives, []) |
| 462 golden_content = """\ |
| 463 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 464 // Use of this source code is governed by a BSD-style license that can be |
| 465 // found in the LICENSE file. |
| 466 |
| 467 |
| 468 // This file is autogenerated by base/android/jni_generator/jni_generator_tests.
py |
| 469 // For org/chromium/TestJni |
| 470 |
| 471 #ifndef org_chromium_TestJni_JNI |
| 472 #define org_chromium_TestJni_JNI |
| 473 |
| 474 #include <jni.h> |
| 475 |
| 476 #include "base/android/jni_android.h" |
| 477 #include "base/android/scoped_java_ref.h" |
| 478 #include "base/basictypes.h" |
| 479 #include "base/logging.h" |
| 480 |
| 481 using base::android::ScopedJavaLocalRef; |
| 482 |
| 483 // Step 1: forward declarations. |
| 484 namespace { |
| 485 const char* const kMyOtherInnerClassClassPath = "org/chromium/TestJni$MyOtherInn
erClass"; |
| 486 const char* const kTestJniClassPath = "org/chromium/TestJni"; |
| 487 const char* const kMyInnerClassClassPath = "org/chromium/TestJni$MyInnerClass"; |
| 488 // Leaking this JavaRef as we cannot use LazyInstance from some threads. |
| 489 base::android::ScopedJavaGlobalRef<jclass>& |
| 490 g_TestJni_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>()); |
| 491 } // namespace |
| 492 |
| 493 static jint Init(JNIEnv* env, jobject obj); |
| 494 |
| 495 |
| 496 static jint Init(JNIEnv* env, jobject obj); |
| 497 |
| 498 |
| 499 // Step 2: method stubs. |
| 500 |
| 501 |
| 502 // Step 3: GetMethodIDs and RegisterNatives. |
| 503 |
| 504 |
| 505 static void GetMethodIDsImpl(JNIEnv* env) { |
| 506 g_TestJni_clazz.Reset(base::android::GetClass(env, kTestJniClassPath)); |
| 507 } |
| 508 |
| 509 static bool RegisterNativesImpl(JNIEnv* env) { |
| 510 GetMethodIDsImpl(env); |
| 511 |
| 512 static const JNINativeMethod kMethodsMyOtherInnerClass[] = { |
| 513 { "nativeInit", "()I", reinterpret_cast<void*>(Init) }, |
| 514 }; |
| 515 const int kMethodsMyOtherInnerClassSize = arraysize(kMethodsMyOtherInnerClass)
; |
| 516 |
| 517 if (env->RegisterNatives(g_MyOtherInnerClass_clazz.obj(), |
| 518 kMethodsMyOtherInnerClass, |
| 519 kMethodsMyOtherInnerClassSize) < 0) { |
| 520 LOG(ERROR) << "RegisterNatives failed in " << __FILE__; |
| 521 return false; |
| 522 } |
| 523 |
| 524 static const JNINativeMethod kMethodsMyInnerClass[] = { |
| 525 { "nativeInit", "()I", reinterpret_cast<void*>(Init) }, |
| 526 }; |
| 527 const int kMethodsMyInnerClassSize = arraysize(kMethodsMyInnerClass); |
| 528 |
| 529 if (env->RegisterNatives(g_MyInnerClass_clazz.obj(), |
| 530 kMethodsMyInnerClass, |
| 531 kMethodsMyInnerClassSize) < 0) { |
| 532 LOG(ERROR) << "RegisterNatives failed in " << __FILE__; |
| 533 return false; |
| 534 } |
| 535 |
| 536 return true; |
| 537 } |
| 538 |
| 539 #endif // org_chromium_TestJni_JNI |
| 540 """ |
| 541 self.assertTextEquals(golden_content, h.GetContent()) |
| 542 |
| 543 def testInnerClassNativesBothInnerAndOuter(self): |
| 544 test_data = """ |
| 545 class MyOuterClass { |
| 546 private native int nativeInit(); |
| 547 class MyOtherInnerClass { |
| 548 @NativeCall("MyOtherInnerClass") |
| 549 private native int nativeInit(); |
| 550 } |
| 551 } |
| 552 """ |
| 553 natives = jni_generator.ExtractNatives(test_data) |
| 554 golden_natives = [ |
| 555 NativeMethod(return_type='int', static=False, |
| 556 name='Init', params=[], |
| 557 java_class_name='', |
| 558 type='function'), |
| 559 NativeMethod(return_type='int', static=False, |
| 560 name='Init', params=[], |
| 561 java_class_name='MyOtherInnerClass', |
| 562 type='function') |
| 563 ] |
| 564 self.assertListEquals(golden_natives, natives) |
| 565 h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', |
| 566 natives, []) |
| 567 golden_content = """\ |
| 568 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 569 // Use of this source code is governed by a BSD-style license that can be |
| 570 // found in the LICENSE file. |
| 571 |
| 572 |
| 573 // This file is autogenerated by base/android/jni_generator/jni_generator_tests.
py |
| 574 // For org/chromium/TestJni |
| 575 |
| 576 #ifndef org_chromium_TestJni_JNI |
| 577 #define org_chromium_TestJni_JNI |
| 578 |
| 579 #include <jni.h> |
| 580 |
| 581 #include "base/android/jni_android.h" |
| 582 #include "base/android/scoped_java_ref.h" |
| 583 #include "base/basictypes.h" |
| 584 #include "base/logging.h" |
| 585 |
| 586 using base::android::ScopedJavaLocalRef; |
| 587 |
| 588 // Step 1: forward declarations. |
| 589 namespace { |
| 590 const char* const kMyOtherInnerClassClassPath = "org/chromium/TestJni$MyOtherInn
erClass"; |
| 591 const char* const kTestJniClassPath = "org/chromium/TestJni"; |
| 592 // Leaking this JavaRef as we cannot use LazyInstance from some threads. |
| 593 base::android::ScopedJavaGlobalRef<jclass>& |
| 594 g_TestJni_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>()); |
| 595 } // namespace |
| 596 |
| 597 static jint Init(JNIEnv* env, jobject obj); |
| 598 |
| 599 |
| 600 static jint Init(JNIEnv* env, jobject obj); |
| 601 |
| 602 |
| 603 // Step 2: method stubs. |
| 604 |
| 605 |
| 606 // Step 3: GetMethodIDs and RegisterNatives. |
| 607 |
| 608 |
| 609 static void GetMethodIDsImpl(JNIEnv* env) { |
| 610 g_TestJni_clazz.Reset(base::android::GetClass(env, kTestJniClassPath)); |
| 611 } |
| 612 |
| 613 static bool RegisterNativesImpl(JNIEnv* env) { |
| 614 GetMethodIDsImpl(env); |
| 615 |
| 616 static const JNINativeMethod kMethodsMyOtherInnerClass[] = { |
| 617 { "nativeInit", "()I", reinterpret_cast<void*>(Init) }, |
| 618 }; |
| 619 const int kMethodsMyOtherInnerClassSize = arraysize(kMethodsMyOtherInnerClass)
; |
| 620 |
| 621 if (env->RegisterNatives(g_MyOtherInnerClass_clazz.obj(), |
| 622 kMethodsMyOtherInnerClass, |
| 623 kMethodsMyOtherInnerClassSize) < 0) { |
| 624 LOG(ERROR) << "RegisterNatives failed in " << __FILE__; |
| 625 return false; |
| 626 } |
| 627 |
| 628 static const JNINativeMethod kMethodsTestJni[] = { |
| 629 { "nativeInit", "()I", reinterpret_cast<void*>(Init) }, |
| 630 }; |
| 631 const int kMethodsTestJniSize = arraysize(kMethodsTestJni); |
| 632 |
| 633 if (env->RegisterNatives(g_TestJni_clazz.obj(), |
| 634 kMethodsTestJni, |
| 635 kMethodsTestJniSize) < 0) { |
| 636 LOG(ERROR) << "RegisterNatives failed in " << __FILE__; |
| 637 return false; |
| 638 } |
| 639 |
| 640 return true; |
| 641 } |
| 642 |
| 643 #endif // org_chromium_TestJni_JNI |
| 644 """ |
| 645 self.assertTextEquals(golden_content, h.GetContent()) |
| 646 |
| 647 def testCalledByNatives(self): |
| 648 test_data = """" |
| 649 @CalledByNative |
| 650 NativeInfoBar showConfirmInfoBar(int nativeInfoBar, String buttonOk, |
| 651 String buttonCancel, String title, Bitmap i
con) { |
| 652 InfoBar infobar = new ConfirmInfoBar(nativeInfoBar, mContext, |
| 653 buttonOk, buttonCancel, |
| 654 title, icon); |
| 655 return infobar; |
| 656 } |
| 657 @CalledByNative |
| 658 NativeInfoBar showAutoLoginInfoBar(int nativeInfoBar, |
| 659 String realm, String account, String args
) { |
| 660 AutoLoginInfoBar infobar = new AutoLoginInfoBar(nativeInfoBar, mContext, |
| 661 realm, account, args); |
| 662 if (infobar.displayedAccountCount() == 0) |
| 663 infobar = null; |
| 664 return infobar; |
| 665 } |
| 666 @CalledByNative("InfoBar") |
| 667 void dismiss(); |
| 668 @SuppressWarnings("unused") |
| 669 @CalledByNative |
| 670 private static boolean shouldShowAutoLogin(ChromeView chromeView, |
| 671 String realm, String account, String args) { |
| 672 AccountManagerContainer accountManagerContainer = |
| 673 new AccountManagerContainer((Activity)chromeView.getContext(), realm
, account, args); |
| 674 String[] logins = accountManagerContainer.getAccountLogins(null); |
| 675 return logins.length != 0; |
| 676 } |
| 677 @CalledByNative |
| 678 static InputStream openUrl(String url) { |
| 679 return null; |
| 680 } |
| 681 @CalledByNative |
| 682 private void activateHardwareAcceleration(final boolean activated, final int
iPid, final int iType, |
| 683 final int iPrimaryID, final int iSecondaryID) { |
| 684 if (!activated) { |
| 685 return |
| 686 } |
| 687 } |
| 688 @CalledByNativeUnchecked |
| 689 private void uncheckedCall(int iParam); |
| 690 """ |
| 691 called_by_natives = jni_generator.ExtractCalledByNatives(test_data) |
| 692 golden_called_by_natives = [ |
| 693 CalledByNative( |
| 694 return_type='NativeInfoBar', |
| 695 system_class=False, |
| 696 static=False, |
| 697 name='showConfirmInfoBar', |
| 698 method_id_var_name='showConfirmInfoBar', |
| 699 java_class_name='', |
| 700 params=[Param(datatype='int', name='nativeInfoBar'), |
| 701 Param(datatype='String', name='buttonOk'), |
| 702 Param(datatype='String', name='buttonCancel'), |
| 703 Param(datatype='String', name='title'), |
| 704 Param(datatype='Bitmap', name='icon')], |
| 705 env_call=('Object', ''), |
| 706 unchecked=False, |
| 707 ), |
| 708 CalledByNative( |
| 709 return_type='NativeInfoBar', |
| 710 system_class=False, |
| 711 static=False, |
| 712 name='showAutoLoginInfoBar', |
| 713 method_id_var_name='showAutoLoginInfoBar', |
| 714 java_class_name='', |
| 715 params=[Param(datatype='int', name='nativeInfoBar'), |
| 716 Param(datatype='String', name='realm'), |
| 717 Param(datatype='String', name='account'), |
| 718 Param(datatype='String', name='args')], |
| 719 env_call=('Object', ''), |
| 720 unchecked=False, |
| 721 ), |
| 722 CalledByNative( |
| 723 return_type='void', |
| 724 system_class=False, |
| 725 static=False, |
| 726 name='dismiss', |
| 727 method_id_var_name='dismiss', |
| 728 java_class_name='InfoBar', |
| 729 params=[], |
| 730 env_call=('Void', ''), |
| 731 unchecked=False, |
| 732 ), |
| 733 CalledByNative( |
| 734 return_type='boolean', |
| 735 system_class=False, |
| 736 static=True, |
| 737 name='shouldShowAutoLogin', |
| 738 method_id_var_name='shouldShowAutoLogin', |
| 739 java_class_name='', |
| 740 params=[Param(datatype='ChromeView', name='chromeView'), |
| 741 Param(datatype='String', name='realm'), |
| 742 Param(datatype='String', name='account'), |
| 743 Param(datatype='String', name='args')], |
| 744 env_call=('Boolean', ''), |
| 745 unchecked=False, |
| 746 ), |
| 747 CalledByNative( |
| 748 return_type='InputStream', |
| 749 system_class=False, |
| 750 static=True, |
| 751 name='openUrl', |
| 752 method_id_var_name='openUrl', |
| 753 java_class_name='', |
| 754 params=[Param(datatype='String', name='url')], |
| 755 env_call=('Object', ''), |
| 756 unchecked=False, |
| 757 ), |
| 758 CalledByNative( |
| 759 return_type='void', |
| 760 system_class=False, |
| 761 static=False, |
| 762 name='activateHardwareAcceleration', |
| 763 method_id_var_name='activateHardwareAcceleration', |
| 764 java_class_name='', |
| 765 params=[Param(datatype='boolean', name='activated'), |
| 766 Param(datatype='int', name='iPid'), |
| 767 Param(datatype='int', name='iType'), |
| 768 Param(datatype='int', name='iPrimaryID'), |
| 769 Param(datatype='int', name='iSecondaryID'), |
| 770 ], |
| 771 env_call=('Void', ''), |
| 772 unchecked=False, |
| 773 ), |
| 774 CalledByNative( |
| 775 return_type='void', |
| 776 system_class=False, |
| 777 static=False, |
| 778 name='uncheckedCall', |
| 779 method_id_var_name='uncheckedCall', |
| 780 java_class_name='', |
| 781 params=[Param(datatype='int', name='iParam')], |
| 782 env_call=('Void', ''), |
| 783 unchecked=True, |
| 784 ), |
| 785 ] |
| 786 self.assertListEquals(golden_called_by_natives, called_by_natives) |
| 787 h = jni_generator.InlHeaderFileGenerator('', 'org/chromium/TestJni', |
| 788 [], called_by_natives) |
| 789 golden_content = """\ |
| 790 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 791 // Use of this source code is governed by a BSD-style license that can be |
| 792 // found in the LICENSE file. |
| 793 |
| 794 |
| 795 // This file is autogenerated by base/android/jni_generator/jni_generator_tests.
py |
| 796 // For org/chromium/TestJni |
| 797 |
| 798 #ifndef org_chromium_TestJni_JNI |
| 799 #define org_chromium_TestJni_JNI |
| 800 |
| 801 #include <jni.h> |
| 802 |
| 803 #include "base/android/jni_android.h" |
| 804 #include "base/android/scoped_java_ref.h" |
| 805 #include "base/basictypes.h" |
| 806 #include "base/logging.h" |
| 807 |
| 808 using base::android::ScopedJavaLocalRef; |
| 809 |
| 810 // Step 1: forward declarations. |
| 811 namespace { |
| 812 const char* const kTestJniClassPath = "org/chromium/TestJni"; |
| 813 const char* const kInfoBarClassPath = "org/chromium/TestJni$InfoBar"; |
| 814 // Leaking this JavaRef as we cannot use LazyInstance from some threads. |
| 815 base::android::ScopedJavaGlobalRef<jclass>& |
| 816 g_TestJni_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>()); |
| 817 // Leaking this JavaRef as we cannot use LazyInstance from some threads. |
| 818 base::android::ScopedJavaGlobalRef<jclass>& |
| 819 g_InfoBar_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>()); |
| 820 } // namespace |
| 821 |
| 822 |
| 823 // Step 2: method stubs. |
| 824 |
| 825 static jmethodID g_TestJni_showConfirmInfoBar = 0; |
| 826 static ScopedJavaLocalRef<jobject> Java_TestJni_showConfirmInfoBar(JNIEnv* env,
jobject obj, jint nativeInfoBar, |
| 827 jstring buttonOk, |
| 828 jstring buttonCancel, |
| 829 jstring title, |
| 830 jobject icon) { |
| 831 DCHECK(!g_TestJni_clazz.is_null()); /* Must call RegisterNativesImpl() */ |
| 832 DCHECK(g_TestJni_showConfirmInfoBar); |
| 833 jobject ret = |
| 834 env->CallObjectMethod(obj, |
| 835 g_TestJni_showConfirmInfoBar, nativeInfoBar, buttonOk, buttonCancel, title
, icon); |
| 836 base::android::CheckException(env); |
| 837 return ScopedJavaLocalRef<jobject>(env, ret); |
| 838 } |
| 839 |
| 840 static jmethodID g_TestJni_showAutoLoginInfoBar = 0; |
| 841 static ScopedJavaLocalRef<jobject> Java_TestJni_showAutoLoginInfoBar(JNIEnv* env
, jobject obj, jint nativeInfoBar, |
| 842 jstring realm, |
| 843 jstring account, |
| 844 jstring args) { |
| 845 DCHECK(!g_TestJni_clazz.is_null()); /* Must call RegisterNativesImpl() */ |
| 846 DCHECK(g_TestJni_showAutoLoginInfoBar); |
| 847 jobject ret = |
| 848 env->CallObjectMethod(obj, |
| 849 g_TestJni_showAutoLoginInfoBar, nativeInfoBar, realm, account, args); |
| 850 base::android::CheckException(env); |
| 851 return ScopedJavaLocalRef<jobject>(env, ret); |
| 852 } |
| 853 |
| 854 static jmethodID g_InfoBar_dismiss = 0; |
| 855 static void Java_InfoBar_dismiss(JNIEnv* env, jobject obj) { |
| 856 DCHECK(!g_InfoBar_clazz.is_null()); /* Must call RegisterNativesImpl() */ |
| 857 DCHECK(g_InfoBar_dismiss); |
| 858 |
| 859 env->CallVoidMethod(obj, |
| 860 g_InfoBar_dismiss); |
| 861 base::android::CheckException(env); |
| 862 |
| 863 } |
| 864 |
| 865 static jmethodID g_TestJni_shouldShowAutoLogin = 0; |
| 866 static jboolean Java_TestJni_shouldShowAutoLogin(JNIEnv* env, jobject chromeView
, |
| 867 jstring realm, |
| 868 jstring account, |
| 869 jstring args) { |
| 870 DCHECK(!g_TestJni_clazz.is_null()); /* Must call RegisterNativesImpl() */ |
| 871 DCHECK(g_TestJni_shouldShowAutoLogin); |
| 872 jboolean ret = |
| 873 env->CallStaticBooleanMethod(g_TestJni_clazz.obj(), |
| 874 g_TestJni_shouldShowAutoLogin, chromeView, realm, account, args); |
| 875 base::android::CheckException(env); |
| 876 return ret; |
| 877 } |
| 878 |
| 879 static jmethodID g_TestJni_openUrl = 0; |
| 880 static ScopedJavaLocalRef<jobject> Java_TestJni_openUrl(JNIEnv* env, jstring url
) { |
| 881 DCHECK(!g_TestJni_clazz.is_null()); /* Must call RegisterNativesImpl() */ |
| 882 DCHECK(g_TestJni_openUrl); |
| 883 jobject ret = |
| 884 env->CallStaticObjectMethod(g_TestJni_clazz.obj(), |
| 885 g_TestJni_openUrl, url); |
| 886 base::android::CheckException(env); |
| 887 return ScopedJavaLocalRef<jobject>(env, ret); |
| 888 } |
| 889 |
| 890 static jmethodID g_TestJni_activateHardwareAcceleration = 0; |
| 891 static void Java_TestJni_activateHardwareAcceleration(JNIEnv* env, jobject obj,
jboolean activated, |
| 892 jint iPid, |
| 893 jint iType, |
| 894 jint iPrimaryID, |
| 895 jint iSecondaryID) { |
| 896 DCHECK(!g_TestJni_clazz.is_null()); /* Must call RegisterNativesImpl() */ |
| 897 DCHECK(g_TestJni_activateHardwareAcceleration); |
| 898 |
| 899 env->CallVoidMethod(obj, |
| 900 g_TestJni_activateHardwareAcceleration, activated, iPid, iType, iPrimaryID
, iSecondaryID); |
| 901 base::android::CheckException(env); |
| 902 |
| 903 } |
| 904 |
| 905 static jmethodID g_TestJni_uncheckedCall = 0; |
| 906 static void Java_TestJni_uncheckedCall(JNIEnv* env, jobject obj, jint iParam) { |
| 907 DCHECK(!g_TestJni_clazz.is_null()); /* Must call RegisterNativesImpl() */ |
| 908 DCHECK(g_TestJni_uncheckedCall); |
| 909 |
| 910 env->CallVoidMethod(obj, |
| 911 g_TestJni_uncheckedCall, iParam); |
| 912 |
| 913 |
| 914 } |
| 915 |
| 916 // Step 3: GetMethodIDs and RegisterNatives. |
| 917 |
| 918 |
| 919 static void GetMethodIDsImpl(JNIEnv* env) { |
| 920 g_TestJni_clazz.Reset(base::android::GetClass(env, kTestJniClassPath)); |
| 921 g_InfoBar_clazz.Reset(base::android::GetClass(env, kInfoBarClassPath)); |
| 922 g_TestJni_showConfirmInfoBar = base::android::GetMethodID( |
| 923 env, g_TestJni_clazz, |
| 924 "showConfirmInfoBar", |
| 925 "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/graphics/B
itmap;)Lcom/android/chrome/infobar/InfoBarContainer$NativeInfoBar;"); |
| 926 |
| 927 g_TestJni_showAutoLoginInfoBar = base::android::GetMethodID( |
| 928 env, g_TestJni_clazz, |
| 929 "showAutoLoginInfoBar", |
| 930 "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)Lcom/android/chrom
e/infobar/InfoBarContainer$NativeInfoBar;"); |
| 931 |
| 932 g_InfoBar_dismiss = base::android::GetMethodID( |
| 933 env, g_InfoBar_clazz, |
| 934 "dismiss", |
| 935 "()V"); |
| 936 |
| 937 g_TestJni_shouldShowAutoLogin = base::android::GetStaticMethodID( |
| 938 env, g_TestJni_clazz, |
| 939 "shouldShowAutoLogin", |
| 940 "(Lorg/chromium/chromeview/ChromeView;Ljava/lang/String;Ljava/lang/String;Lj
ava/lang/String;)Z"); |
| 941 |
| 942 g_TestJni_openUrl = base::android::GetStaticMethodID( |
| 943 env, g_TestJni_clazz, |
| 944 "openUrl", |
| 945 "(Ljava/lang/String;)Ljava/io/InputStream;"); |
| 946 |
| 947 g_TestJni_activateHardwareAcceleration = base::android::GetMethodID( |
| 948 env, g_TestJni_clazz, |
| 949 "activateHardwareAcceleration", |
| 950 "(ZIIII)V"); |
| 951 |
| 952 g_TestJni_uncheckedCall = base::android::GetMethodID( |
| 953 env, g_TestJni_clazz, |
| 954 "uncheckedCall", |
| 955 "(I)V"); |
| 956 |
| 957 } |
| 958 |
| 959 static bool RegisterNativesImpl(JNIEnv* env) { |
| 960 GetMethodIDsImpl(env); |
| 961 |
| 962 return true; |
| 963 } |
| 964 |
| 965 #endif // org_chromium_TestJni_JNI |
| 966 """ |
| 967 self.assertTextEquals(golden_content, h.GetContent()) |
| 968 |
| 969 def testCalledByNativeParseError(self): |
| 970 try: |
| 971 jni_generator.ExtractCalledByNatives(""" |
| 972 @CalledByNative |
| 973 public static int foo(); // This one is fine |
| 974 |
| 975 @CalledByNative |
| 976 scooby doo |
| 977 """) |
| 978 self.fail('Expected a ParseError') |
| 979 except jni_generator.ParseError, e: |
| 980 self.assertEquals(('@CalledByNative', 'scooby doo'), e.context_lines) |
| 981 |
| 982 def testFullyQualifiedClassName(self): |
| 983 contents = """ |
| 984 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 985 // Use of this source code is governed by a BSD-style license that can be |
| 986 // found in the LICENSE file. |
| 987 |
| 988 package org.chromium.chromeview; |
| 989 |
| 990 import org.chromium.chromeview.legacy.DownloadListener; |
| 991 """ |
| 992 self.assertEquals('org/chromium/chromeview/Foo', |
| 993 jni_generator.ExtractFullyQualifiedJavaClassName( |
| 994 'org/chromium/chromeview/Foo.java', contents)) |
| 995 self.assertEquals('org/chromium/chromeview/Foo', |
| 996 jni_generator.ExtractFullyQualifiedJavaClassName( |
| 997 'frameworks/Foo.java', contents)) |
| 998 self.assertRaises(SyntaxError, |
| 999 jni_generator.ExtractFullyQualifiedJavaClassName, |
| 1000 'com/foo/Bar', 'no PACKAGE line') |
| 1001 |
| 1002 def testMethodNameMangling(self): |
| 1003 self.assertEquals('close_pqV', |
| 1004 jni_generator.GetMangledMethodName('close', '()V')) |
| 1005 self.assertEquals('read_paBIIqI', |
| 1006 jni_generator.GetMangledMethodName('read', '([BII)I')) |
| 1007 self.assertEquals('open_pLjava_lang_StringxqLjava_io_InputStreamx', |
| 1008 jni_generator.GetMangledMethodName( |
| 1009 'open', |
| 1010 '(Ljava/lang/String;)Ljava/io/InputStream;')) |
| 1011 |
| 1012 def testFromJavaP(self): |
| 1013 contents = """ |
| 1014 public abstract class java.io.InputStream extends java.lang.Object implements ja
va.io.Closeable{ |
| 1015 public java.io.InputStream(); |
| 1016 public int available() throws java.io.IOException; |
| 1017 public void close() throws java.io.IOException; |
| 1018 public void mark(int); |
| 1019 public boolean markSupported(); |
| 1020 public abstract int read() throws java.io.IOException; |
| 1021 public int read(byte[]) throws java.io.IOException; |
| 1022 public int read(byte[], int, int) throws java.io.IOException; |
| 1023 public synchronized void reset() throws java.io.IOException; |
| 1024 public long skip(long) throws java.io.IOException; |
| 1025 } |
| 1026 """ |
| 1027 jni_from_javap = jni_generator.JNIFromJavaP(contents.split('\n'), None) |
| 1028 self.assertEquals(9, len(jni_from_javap.called_by_natives)) |
| 1029 golden_content = """\ |
| 1030 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 1031 // Use of this source code is governed by a BSD-style license that can be |
| 1032 // found in the LICENSE file. |
| 1033 |
| 1034 |
| 1035 // This file is autogenerated by base/android/jni_generator/jni_generator_tests.
py |
| 1036 // For java/io/InputStream |
| 1037 |
| 1038 #ifndef java_io_InputStream_JNI |
| 1039 #define java_io_InputStream_JNI |
| 1040 |
| 1041 #include <jni.h> |
| 1042 |
| 1043 #include "base/android/jni_android.h" |
| 1044 #include "base/android/scoped_java_ref.h" |
| 1045 #include "base/basictypes.h" |
| 1046 #include "base/logging.h" |
| 1047 |
| 1048 using base::android::ScopedJavaLocalRef; |
| 1049 |
| 1050 // Step 1: forward declarations. |
| 1051 namespace { |
| 1052 const char* const kInputStreamClassPath = "java/io/InputStream"; |
| 1053 // Leaking this JavaRef as we cannot use LazyInstance from some threads. |
| 1054 base::android::ScopedJavaGlobalRef<jclass>& |
| 1055 g_InputStream_clazz = *(new base::android::ScopedJavaGlobalRef<jclass>()); |
| 1056 } // namespace |
| 1057 |
| 1058 |
| 1059 // Step 2: method stubs. |
| 1060 |
| 1061 static jmethodID g_InputStream_available = 0; |
| 1062 static jint Java_InputStream_available(JNIEnv* env, jobject obj) __attribute__ (
(unused)); |
| 1063 static jint Java_InputStream_available(JNIEnv* env, jobject obj) { |
| 1064 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() *
/ |
| 1065 DCHECK(g_InputStream_available); |
| 1066 jint ret = |
| 1067 env->CallIntMethod(obj, |
| 1068 g_InputStream_available); |
| 1069 base::android::CheckException(env); |
| 1070 return ret; |
| 1071 } |
| 1072 |
| 1073 static jmethodID g_InputStream_close = 0; |
| 1074 static void Java_InputStream_close(JNIEnv* env, jobject obj) __attribute__ ((unu
sed)); |
| 1075 static void Java_InputStream_close(JNIEnv* env, jobject obj) { |
| 1076 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() *
/ |
| 1077 DCHECK(g_InputStream_close); |
| 1078 |
| 1079 env->CallVoidMethod(obj, |
| 1080 g_InputStream_close); |
| 1081 base::android::CheckException(env); |
| 1082 |
| 1083 } |
| 1084 |
| 1085 static jmethodID g_InputStream_mark = 0; |
| 1086 static void Java_InputStream_mark(JNIEnv* env, jobject obj, jint p0) __attribute
__ ((unused)); |
| 1087 static void Java_InputStream_mark(JNIEnv* env, jobject obj, jint p0) { |
| 1088 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() *
/ |
| 1089 DCHECK(g_InputStream_mark); |
| 1090 |
| 1091 env->CallVoidMethod(obj, |
| 1092 g_InputStream_mark, p0); |
| 1093 base::android::CheckException(env); |
| 1094 |
| 1095 } |
| 1096 |
| 1097 static jmethodID g_InputStream_markSupported = 0; |
| 1098 static jboolean Java_InputStream_markSupported(JNIEnv* env, jobject obj) __attri
bute__ ((unused)); |
| 1099 static jboolean Java_InputStream_markSupported(JNIEnv* env, jobject obj) { |
| 1100 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() *
/ |
| 1101 DCHECK(g_InputStream_markSupported); |
| 1102 jboolean ret = |
| 1103 env->CallBooleanMethod(obj, |
| 1104 g_InputStream_markSupported); |
| 1105 base::android::CheckException(env); |
| 1106 return ret; |
| 1107 } |
| 1108 |
| 1109 static jmethodID g_InputStream_read_pqI = 0; |
| 1110 static jint Java_InputStream_read(JNIEnv* env, jobject obj) __attribute__ ((unus
ed)); |
| 1111 static jint Java_InputStream_read(JNIEnv* env, jobject obj) { |
| 1112 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() *
/ |
| 1113 DCHECK(g_InputStream_read_pqI); |
| 1114 jint ret = |
| 1115 env->CallIntMethod(obj, |
| 1116 g_InputStream_read_pqI); |
| 1117 base::android::CheckException(env); |
| 1118 return ret; |
| 1119 } |
| 1120 |
| 1121 static jmethodID g_InputStream_read_paBqI = 0; |
| 1122 static jint Java_InputStream_read(JNIEnv* env, jobject obj, jbyteArray p0) __att
ribute__ ((unused)); |
| 1123 static jint Java_InputStream_read(JNIEnv* env, jobject obj, jbyteArray p0) { |
| 1124 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() *
/ |
| 1125 DCHECK(g_InputStream_read_paBqI); |
| 1126 jint ret = |
| 1127 env->CallIntMethod(obj, |
| 1128 g_InputStream_read_paBqI, p0); |
| 1129 base::android::CheckException(env); |
| 1130 return ret; |
| 1131 } |
| 1132 |
| 1133 static jmethodID g_InputStream_read_paBIIqI = 0; |
| 1134 static jint Java_InputStream_read(JNIEnv* env, jobject obj, jbyteArray p0, |
| 1135 jint p1, |
| 1136 jint p2) __attribute__ ((unused)); |
| 1137 static jint Java_InputStream_read(JNIEnv* env, jobject obj, jbyteArray p0, |
| 1138 jint p1, |
| 1139 jint p2) { |
| 1140 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() *
/ |
| 1141 DCHECK(g_InputStream_read_paBIIqI); |
| 1142 jint ret = |
| 1143 env->CallIntMethod(obj, |
| 1144 g_InputStream_read_paBIIqI, p0, p1, p2); |
| 1145 base::android::CheckException(env); |
| 1146 return ret; |
| 1147 } |
| 1148 |
| 1149 static jmethodID g_InputStream_reset = 0; |
| 1150 static void Java_InputStream_reset(JNIEnv* env, jobject obj) __attribute__ ((unu
sed)); |
| 1151 static void Java_InputStream_reset(JNIEnv* env, jobject obj) { |
| 1152 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() *
/ |
| 1153 DCHECK(g_InputStream_reset); |
| 1154 |
| 1155 env->CallVoidMethod(obj, |
| 1156 g_InputStream_reset); |
| 1157 base::android::CheckException(env); |
| 1158 |
| 1159 } |
| 1160 |
| 1161 static jmethodID g_InputStream_skip = 0; |
| 1162 static jlong Java_InputStream_skip(JNIEnv* env, jobject obj, jlong p0) __attribu
te__ ((unused)); |
| 1163 static jlong Java_InputStream_skip(JNIEnv* env, jobject obj, jlong p0) { |
| 1164 DCHECK(!g_InputStream_clazz.is_null()); /* Must call RegisterNativesImpl() *
/ |
| 1165 DCHECK(g_InputStream_skip); |
| 1166 jlong ret = |
| 1167 env->CallLongMethod(obj, |
| 1168 g_InputStream_skip, p0); |
| 1169 base::android::CheckException(env); |
| 1170 return ret; |
| 1171 } |
| 1172 |
| 1173 // Step 3: GetMethodIDs and RegisterNatives. |
| 1174 namespace JNI_InputStream { |
| 1175 |
| 1176 static void GetMethodIDsImpl(JNIEnv* env) { |
| 1177 g_InputStream_clazz.Reset(base::android::GetClass(env, kInputStreamClassPath))
; |
| 1178 g_InputStream_available = base::android::GetMethodID( |
| 1179 env, g_InputStream_clazz, |
| 1180 "available", |
| 1181 "()I"); |
| 1182 |
| 1183 g_InputStream_close = base::android::GetMethodID( |
| 1184 env, g_InputStream_clazz, |
| 1185 "close", |
| 1186 "()V"); |
| 1187 |
| 1188 g_InputStream_mark = base::android::GetMethodID( |
| 1189 env, g_InputStream_clazz, |
| 1190 "mark", |
| 1191 "(I)V"); |
| 1192 |
| 1193 g_InputStream_markSupported = base::android::GetMethodID( |
| 1194 env, g_InputStream_clazz, |
| 1195 "markSupported", |
| 1196 "()Z"); |
| 1197 |
| 1198 g_InputStream_read_pqI = base::android::GetMethodID( |
| 1199 env, g_InputStream_clazz, |
| 1200 "read", |
| 1201 "()I"); |
| 1202 |
| 1203 g_InputStream_read_paBqI = base::android::GetMethodID( |
| 1204 env, g_InputStream_clazz, |
| 1205 "read", |
| 1206 "([B)I"); |
| 1207 |
| 1208 g_InputStream_read_paBIIqI = base::android::GetMethodID( |
| 1209 env, g_InputStream_clazz, |
| 1210 "read", |
| 1211 "([BII)I"); |
| 1212 |
| 1213 g_InputStream_reset = base::android::GetMethodID( |
| 1214 env, g_InputStream_clazz, |
| 1215 "reset", |
| 1216 "()V"); |
| 1217 |
| 1218 g_InputStream_skip = base::android::GetMethodID( |
| 1219 env, g_InputStream_clazz, |
| 1220 "skip", |
| 1221 "(J)J"); |
| 1222 |
| 1223 } |
| 1224 |
| 1225 static bool RegisterNativesImpl(JNIEnv* env) { |
| 1226 JNI_InputStream::GetMethodIDsImpl(env); |
| 1227 |
| 1228 return true; |
| 1229 } |
| 1230 } // namespace JNI_InputStream |
| 1231 |
| 1232 #endif // java_io_InputStream_JNI |
| 1233 """ |
| 1234 self.assertTextEquals(golden_content, jni_from_javap.GetContent()) |
| 1235 |
| 1236 def testREForNatives(self): |
| 1237 # We should not match "native SyncSetupFlow" inside the comment. |
| 1238 test_data = """ |
| 1239 /** |
| 1240 * Invoked when the setup process is complete so we can disconnect from the |
| 1241 * native-side SyncSetupFlowHandler. |
| 1242 */ |
| 1243 public void destroy() { |
| 1244 Log.v(TAG, "Destroying native SyncSetupFlow"); |
| 1245 if (mNativeSyncSetupFlow != 0) { |
| 1246 nativeSyncSetupEnded(mNativeSyncSetupFlow); |
| 1247 mNativeSyncSetupFlow = 0; |
| 1248 } |
| 1249 } |
| 1250 private native void nativeSyncSetupEnded(int nativeAndroidSyncSetupFlowHandl
er); |
| 1251 """ |
| 1252 jni_from_java = jni_generator.JNIFromJavaSource(test_data, 'foo/bar') |
| 1253 |
| 1254 def testRaisesOnUnknownDatatype(self): |
| 1255 test_data = """ |
| 1256 class MyInnerClass { |
| 1257 private native int nativeInit(AnUnknownDatatype p0); |
| 1258 } |
| 1259 """ |
| 1260 self.assertRaises(SyntaxError, |
| 1261 jni_generator.JNIFromJavaSource, |
| 1262 test_data, 'foo/bar') |
| 1263 |
| 1264 def testJniSelfDocumentingExample(self): |
| 1265 script_dir = os.path.dirname(sys.argv[0]) |
| 1266 content = file(os.path.join(script_dir, 'SampleForTests.java')).read() |
| 1267 golden_content = file(os.path.join(script_dir, |
| 1268 'golden_sample_for_tests_jni.h')).read() |
| 1269 jni_from_java = jni_generator.JNIFromJavaSource(content, |
| 1270 'org/chromium/example/jni_ge
nerator/SampleForTests') |
| 1271 self.assertTextEquals(golden_content, jni_from_java.GetContent()) |
| 1272 |
| 1273 def testCheckFilenames(self): |
| 1274 self.assertRaises(SystemExit, jni_generator.CheckFilenames, |
| 1275 ['more', 'input', 'than'], ['output']) |
| 1276 self.assertRaises(SystemExit, jni_generator.CheckFilenames, |
| 1277 ['more'], ['output', 'than', 'input']) |
| 1278 self.assertRaises(SystemExit, jni_generator.CheckFilenames, |
| 1279 ['NotTheSame.java'], ['not_good.h']) |
| 1280 self.assertRaises(SystemExit, jni_generator.CheckFilenames, |
| 1281 ['MissingJniSuffix.java'], ['missing_jni_suffix.h']) |
| 1282 jni_generator.CheckFilenames(['ThisIsFine.java'], ['this_is_fine_jni.h']) |
| 1283 jni_generator.CheckFilenames([], []) |
| 1284 |
| 1285 |
| 1286 if __name__ == '__main__': |
| 1287 unittest.main() |
OLD | NEW |