Robolectric.java revision 47f14e6f9ae61cc4ee9d04f58e65bc950b8bfe93
1package com.xtremelabs.robolectric; 2 3import android.app.*; 4import android.appwidget.AppWidgetManager; 5import android.bluetooth.BluetoothAdapter; 6import android.bluetooth.BluetoothDevice; 7import android.content.*; 8import android.content.pm.ResolveInfo; 9import android.content.res.AssetManager; 10import android.content.res.Configuration; 11import android.content.res.Resources; 12import android.database.sqlite.*; 13import android.graphics.*; 14import android.graphics.drawable.*; 15import android.hardware.Camera; 16import android.hardware.SensorManager; 17import android.location.Address; 18import android.location.Geocoder; 19import android.location.LocationManager; 20import android.media.AudioManager; 21import android.media.MediaPlayer; 22import android.media.MediaRecorder; 23import android.net.ConnectivityManager; 24import android.net.NetworkInfo; 25import android.net.wifi.ScanResult; 26import android.net.wifi.WifiConfiguration; 27import android.net.wifi.WifiInfo; 28import android.net.wifi.WifiManager; 29import android.os.*; 30import android.preference.*; 31import android.support.v4.app.DialogFragment; 32import android.support.v4.app.Fragment; 33import android.support.v4.app.FragmentActivity; 34import android.telephony.PhoneNumberUtils; 35import android.telephony.SmsManager; 36import android.telephony.TelephonyManager; 37import android.text.ClipboardManager; 38import android.text.TextPaint; 39import android.text.format.DateFormat; 40import android.text.method.PasswordTransformationMethod; 41import android.util.SparseArray; 42import android.view.*; 43import android.view.animation.Animation; 44import android.view.animation.AnimationUtils; 45import android.view.animation.TranslateAnimation; 46import android.view.inputmethod.InputMethodManager; 47import android.webkit.*; 48import android.widget.*; 49import com.xtremelabs.robolectric.bytecode.RobolectricInternals; 50import com.xtremelabs.robolectric.bytecode.ShadowWrangler; 51import com.xtremelabs.robolectric.shadows.*; 52import com.xtremelabs.robolectric.tester.org.apache.http.FakeHttpLayer; 53import com.xtremelabs.robolectric.tester.org.apache.http.HttpRequestInfo; 54import com.xtremelabs.robolectric.tester.org.apache.http.RequestMatcher; 55import com.xtremelabs.robolectric.util.Scheduler; 56import org.apache.http.Header; 57import org.apache.http.HttpRequest; 58import org.apache.http.HttpResponse; 59import org.apache.http.impl.client.DefaultRequestDirector; 60 61import java.lang.reflect.Field; 62import java.lang.reflect.Modifier; 63import java.util.Arrays; 64import java.util.List; 65 66public class Robolectric { 67 public static Application application; 68 69 public static <T> T newInstanceOf(Class<T> clazz) { 70 return RobolectricInternals.newInstanceOf(clazz); 71 } 72 73 public static Object newInstanceOf(String className) { 74 try { 75 Class<?> clazz = Class.forName(className); 76 if (clazz != null) { 77 return newInstanceOf(clazz); 78 } 79 } catch (ClassNotFoundException e) { 80 } 81 return null; 82 } 83 84 public static void bindShadowClass(Class<?> shadowClass) { 85 RobolectricInternals.bindShadowClass(shadowClass); 86 } 87 88 public static void bindDefaultShadowClasses() { 89 bindShadowClasses(getDefaultShadowClasses()); 90 } 91 92 public static void bindShadowClasses(List<Class<?>> shadowClasses) { 93 for (Class<?> shadowClass : shadowClasses) { 94 bindShadowClass(shadowClass); 95 } 96 } 97 98 /** 99 * Invoke this utility method in tests to reveal which Android api classes and methods are being invoked 100 * for which there are no shadows or shadow methods. This helps expose which methods are being invoked 101 * either by a third party library or application code which need new shadow methods to be written. Generates 102 * output for the current test only. 103 */ 104 public static void logMissingInvokedShadowMethods() { 105 ShadowWrangler.getInstance().logMissingInvokedShadowMethods(); 106 } 107 108 public static List<Class<?>> getDefaultShadowClasses() { 109 return Arrays.asList( 110 ShadowAbsListView.class, 111 ShadowAbsoluteLayout.class, 112 ShadowAbsSeekBar.class, 113 ShadowAbsSpinner.class, 114 ShadowAbstractCursor.class, 115 ShadowAccount.class, 116 ShadowActivity.class, 117 ShadowActivityInfo.class, 118 ShadowActivityGroup.class, 119 ShadowActivityManager.class, 120 ShadowAdapterView.class, 121 ShadowAddress.class, 122 ShadowAlarmManager.class, 123 ShadowAlertDialog.class, 124 ShadowAlertDialog.ShadowBuilder.class, 125 ShadowAndroidHttpClient.class, 126 ShadowAnimation.class, 127 ShadowAnimationUtils.class, 128 ShadowApplication.class, 129 ShadowAppWidgetManager.class, 130 ShadowArrayAdapter.class, 131 ShadowAssetManager.class, 132 ShadowAsyncTask.class, 133 ShadowAudioManager.class, 134 ShadowBaseAdapter.class, 135 ShadowBitmap.class, 136 ShadowBitmapDrawable.class, 137 ShadowBitmapFactory.class, 138 ShadowBluetoothAdapter.class, 139 ShadowBluetoothDevice.class, 140 ShadowBundle.class, 141 ShadowButton.class, 142 ShadowCamera.class, 143 ShadowCameraParameters.class, 144 ShadowCameraSize.class, 145 ShadowCanvas.class, 146 ShadowClipboardManager.class, 147 ShadowColor.class, 148 ShadowColorDrawable.class, 149 ShadowColorMatrix.class, 150 ShadowColorMatrixColorFilter.class, 151 ShadowColorStateList.class, 152 ShadowComponentName.class, 153 ShadowCompoundButton.class, 154 ShadowConfiguration.class, 155 ShadowConnectivityManager.class, 156 ShadowContentProvider.class, 157 ShadowContentProviderOperation.class, 158 ShadowContentProviderOperationBuilder.class, 159 ShadowContentProviderResult.class, 160 ShadowContentResolver.class, 161 ShadowContentUris.class, 162 ShadowContentValues.class, 163 ShadowContext.class, 164 ShadowContextWrapper.class, 165 ShadowContextThemeWrapper.class, 166 ShadowCookieManager.class, 167 ShadowCookieSyncManager.class, 168 ShadowCriteria.class, 169 ShadowCountDownTimer.class, 170 ShadowCursorAdapter.class, 171 ShadowDatabaseUtils.class, 172 ShadowDateFormat.class, 173 ShadowDefaultRequestDirector.class, 174 ShadowDisplay.class, 175 ShadowDrawable.class, 176 ShadowDialog.class, 177 ShadowDialogFragment.class, 178 ShadowDialogPreference.class, 179 ShadowEditText.class, 180 ShadowEnvironment.class, 181 ShadowExpandableListView.class, 182 ShadowFilter.class, 183 ShadowFloatMath.class, 184 ShadowFragment.class, 185 ShadowFragmentActivity.class, 186 ShadowFrameLayout.class, 187 ShadowGallery.class, 188 ShadowGeocoder.class, 189 ShadowGeoPoint.class, 190 ShadowGridView.class, 191 ShadowHandler.class, 192 ShadowHandlerThread.class, 193 ShadowHtml.class, 194 ShadowImageView.class, 195 ShadowInputMethodManager.class, 196 ShadowIntent.class, 197 ShadowIntentFilter.class, 198 ShadowIntentFilterAuthorityEntry.class, 199 ShadowItemizedOverlay.class, 200 ShadowJsPromptResult.class, 201 ShadowJsResult.class, 202 ShadowKeyEvent.class, 203 ShadowKeyguardManager.class, 204 ShadowKeyGuardLock.class, 205 ShadowLayerDrawable.class, 206 ShadowLayoutInflater.class, 207 ShadowLayoutParams.class, 208 ShadowLinearLayout.class, 209 ShadowListActivity.class, 210 ShadowListPreference.class, 211 ShadowListView.class, 212 ShadowLocation.class, 213 ShadowLocationManager.class, 214 ShadowLog.class, 215 ShadowLooper.class, 216 ShadowMapController.class, 217 ShadowMapActivity.class, 218 ShadowMapView.class, 219 ShadowMarginLayoutParams.class, 220 ShadowMatrix.class, 221 ShadowMatrixCursor.class, 222 ShadowMediaPlayer.class, 223 ShadowMediaRecorder.class, 224 ShadowMediaStore.ShadowImages.ShadowMedia.class, 225 ShadowMenuInflater.class, 226 ShadowMessage.class, 227 ShadowMotionEvent.class, 228 ShadowNotification.class, 229 ShadowNdefMessage.class, 230 ShadowNdefRecord.class, 231 ShadowNfcAdapter.class, 232 ShadowNotificationManager.class, 233 ShadowNetworkInfo.class, 234 ShadowOverlayItem.class, 235 ShadowPaint.class, 236 ShadowPair.class, 237 ShadowParcel.class, 238 ShadowPasswordTransformationMethod.class, 239 ShadowPath.class, 240 ShadowPendingIntent.class, 241 ShadowPeriodicSync.class, 242 ShadowPhoneNumberUtils.class, 243 ShadowPoint.class, 244 ShadowPointF.class, 245 ShadowPowerManager.class, 246 ShadowPreference.class, 247 ShadowPreferenceActivity.class, 248 ShadowPreferenceCategory.class, 249 ShadowPreferenceGroup.class, 250 ShadowPreferenceManager.class, 251 ShadowPreferenceScreen.class, 252 ShadowProgressBar.class, 253 ShadowProgressDialog.class, 254 ShadowRadioButton.class, 255 ShadowRadioGroup.class, 256 ShadowRatingBar.class, 257 ShadowRect.class, 258 ShadowResolveInfo.class, 259 ShadowRemoteCallbackList.class, 260 ShadowRemoteViews.class, 261 ShadowResultReceiver.class, 262 ShadowResourceCursorAdapter.class, 263 ShadowResources.class, 264 ShadowResources.ShadowTheme.class, 265 ShadowScanResult.class, 266 ShadowScrollView.class, 267 ShadowSeekBar.class, 268 ShadowSensorManager.class, 269 ShadowService.class, 270 ShadowSettings.class, 271 ShadowSettings.ShadowSecure.class, 272 ShadowSettings.ShadowSystem.class, 273 ShadowSimpleCursorAdapter.class, 274 ShadowShapeDrawable.class, 275 ShadowSmsManager.class, 276 ShadowSpannableStringBuilder.class, 277 ShadowSparseArray.class, 278 ShadowSyncResult.class, 279 ShadowSyncResult.ShadowSyncStats.class, 280 ShadowSQLiteProgram.class, 281 ShadowSQLiteDatabase.class, 282 ShadowSQLiteCursor.class, 283 ShadowSQLiteOpenHelper.class, 284 ShadowSQLiteStatement.class, 285 ShadowSQLiteQueryBuilder.class, 286 ShadowSslErrorHandler.class, 287 ShadowStateListDrawable.class, 288 ShadowSurfaceView.class, 289 ShadowTabActivity.class, 290 ShadowTabHost.class, 291 ShadowTabSpec.class, 292 ShadowTelephonyManager.class, 293 ShadowTextPaint.class, 294 ShadowTextUtils.class, 295 ShadowTextView.class, 296 ShadowTime.class, 297 ShadowToast.class, 298 ShadowTranslateAnimation.class, 299 ShadowTypedArray.class, 300 ShadowTypedValue.class, 301 ShadowUriMatcher.class, 302 ShadowURLSpan.class, 303 ShadowVideoView.class, 304 ShadowView.class, 305 ShadowViewAnimator.class, 306 ShadowViewConfiguration.class, 307 ShadowViewGroup.class, 308 ShadowViewFlipper.class, 309 ShadowViewMeasureSpec.class, 310 ShadowViewStub.class, 311 ShadowViewTreeObserver.class, 312 ShadowWebSettings.class, 313 ShadowWebView.class, 314 ShadowWifiConfiguration.class, 315 ShadowWifiInfo.class, 316 ShadowWifiManager.class, 317 ShadowWindow.class, 318 ShadowZoomButtonsController.class 319 ); 320 } 321 322 public static void resetStaticState() { 323 ShadowWrangler.getInstance().silence(); 324 Robolectric.application = new Application(); 325 ShadowBitmapFactory.reset(); 326 ShadowDrawable.reset(); 327 ShadowMediaStore.reset(); 328 ShadowLog.reset(); 329 ShadowContext.clearFilesAndCache(); 330 ShadowLooper.resetThreadLoopers(); 331 ShadowDialog.reset(); 332 ShadowContentResolver.reset(); 333 } 334 335 public static <T> T directlyOn(T shadowedObject) { 336 return RobolectricInternals.directlyOn(shadowedObject); 337 } 338 339 public static ShadowAbsListView shadowOf(AbsListView instance) { 340 return (ShadowAbsListView) shadowOf_(instance); 341 } 342 343 public static ShadowAbsSeekBar shadowOf(AbsSeekBar instance) { 344 return (ShadowAbsSeekBar) shadowOf_(instance); 345 } 346 347 public static ShadowActivity shadowOf(Activity instance) { 348 return (ShadowActivity) shadowOf_(instance); 349 } 350 351 public static ShadowActivityGroup shadowOf(ActivityGroup instance) { 352 return (ShadowActivityGroup) shadowOf_(instance); 353 } 354 355 public static ShadowActivityManager shadowOf(ActivityManager instance) { 356 return (ShadowActivityManager) shadowOf_(instance); 357 } 358 359 public static ShadowAdapterView shadowOf(AdapterView instance) { 360 return (ShadowAdapterView) shadowOf_(instance); 361 } 362 363 public static ShadowAddress shadowOf(Address instance) { 364 return (ShadowAddress) shadowOf_(instance); 365 } 366 367 public static ShadowAlarmManager shadowOf(AlarmManager instance) { 368 return (ShadowAlarmManager) Robolectric.shadowOf_(instance); 369 } 370 371 public static ShadowAlertDialog shadowOf(AlertDialog instance) { 372 return (ShadowAlertDialog) shadowOf_(instance); 373 } 374 375 public static ShadowAnimation shadowOf(Animation instance) { 376 return (ShadowAnimation) shadowOf_(instance); 377 } 378 379 public static ShadowAnimationUtils shadowOf(AnimationUtils instance) { 380 return (ShadowAnimationUtils) shadowOf_(instance); 381 } 382 383 public static ShadowApplication shadowOf(Application instance) { 384 return (ShadowApplication) shadowOf_(instance); 385 } 386 387 public static ShadowAppWidgetManager shadowOf(AppWidgetManager instance) { 388 return (ShadowAppWidgetManager) shadowOf_(instance); 389 } 390 391 public static ShadowArrayAdapter shadowOf(ArrayAdapter instance) { 392 return (ShadowArrayAdapter) shadowOf_(instance); 393 } 394 395 public static ShadowAssetManager shadowOf(AssetManager instance) { 396 return (ShadowAssetManager) Robolectric.shadowOf_(instance); 397 } 398 399 public static ShadowAudioManager shadowOf(AudioManager instance) { 400 return (ShadowAudioManager) shadowOf_(instance); 401 } 402 403 public static ShadowBitmap shadowOf(Bitmap other) { 404 return (ShadowBitmap) Robolectric.shadowOf_(other); 405 } 406 407 public static ShadowBitmapDrawable shadowOf(BitmapDrawable instance) { 408 return (ShadowBitmapDrawable) shadowOf_(instance); 409 } 410 411 public static ShadowBluetoothAdapter shadowOf(BluetoothAdapter other) { 412 return (ShadowBluetoothAdapter) Robolectric.shadowOf_(other); 413 } 414 415 public static ShadowBluetoothDevice shadowOf(BluetoothDevice other) { 416 return (ShadowBluetoothDevice) Robolectric.shadowOf_(other); 417 } 418 419 public static ShadowBundle shadowOf(Bundle instance) { 420 return (ShadowBundle) shadowOf_(instance); 421 } 422 423 public static ShadowCamera shadowOf(Camera instance) { 424 return (ShadowCamera) shadowOf_(instance); 425 } 426 427 public static ShadowCameraParameters shadowOf(Camera.Parameters instance) { 428 return (ShadowCameraParameters) shadowOf_(instance); 429 } 430 431 public static ShadowCameraSize shadowOf(Camera.Size instance) { 432 return (ShadowCameraSize) shadowOf_(instance); 433 } 434 435 public static ShadowCanvas shadowOf(Canvas instance) { 436 return (ShadowCanvas) shadowOf_(instance); 437 } 438 439 public static ShadowClipboardManager shadowOf(ClipboardManager instance) { 440 return (ShadowClipboardManager) shadowOf_(instance); 441 } 442 443 public static ShadowColor shadowOf(Color instance) { 444 return (ShadowColor) shadowOf_(instance); 445 } 446 447 public static ShadowColorDrawable shadowOf(ColorDrawable instance) { 448 return (ShadowColorDrawable) shadowOf_(instance); 449 } 450 451 public static ShadowColorMatrix shadowOf(ColorMatrix instance) { 452 return (ShadowColorMatrix) shadowOf_(instance); 453 } 454 455 public static ShadowConfiguration shadowOf(Configuration instance) { 456 return (ShadowConfiguration) Robolectric.shadowOf_(instance); 457 } 458 459 public static ShadowConnectivityManager shadowOf(ConnectivityManager instance) { 460 return (ShadowConnectivityManager) shadowOf_(instance); 461 } 462 463 public static ShadowCookieManager shadowOf(CookieManager instance) { 464 return (ShadowCookieManager) shadowOf_(instance); 465 } 466 467 public static ShadowContentResolver shadowOf(ContentResolver instance) { 468 return (ShadowContentResolver) shadowOf_(instance); 469 } 470 471 public static ShadowContentProviderOperation shadowOf(ContentProviderOperation instance) { 472 return (ShadowContentProviderOperation) shadowOf_(instance); 473 } 474 475 public static ShadowContentProviderOperationBuilder shadowOf(ContentProviderOperation.Builder instance) { 476 return (ShadowContentProviderOperationBuilder) shadowOf_(instance); 477 } 478 479 public static ShadowContentProviderResult shadowOf(ContentProviderResult instance) { 480 return (ShadowContentProviderResult) shadowOf_(instance); 481 } 482 483 public static ShadowCookieSyncManager shadowOf(CookieSyncManager instance) { 484 return (ShadowCookieSyncManager) shadowOf_(instance); 485 } 486 487 public static ShadowContext shadowOf(Context instance) { 488 return (ShadowContext) shadowOf_(instance); 489 } 490 491 public static ShadowContentValues shadowOf(ContentValues other) { 492 return (ShadowContentValues) Robolectric.shadowOf_(other); 493 } 494 495 public static ShadowContextWrapper shadowOf(ContextWrapper instance) { 496 return (ShadowContextWrapper) shadowOf_(instance); 497 } 498 499 public static ShadowCountDownTimer shadowOf(CountDownTimer instance) { 500 return (ShadowCountDownTimer) Robolectric.shadowOf_(instance); 501 } 502 503 public static ShadowCursorAdapter shadowOf(CursorAdapter instance) { 504 return (ShadowCursorAdapter) shadowOf_(instance); 505 } 506 507 public static ShadowDateFormat shadowOf(DateFormat instance) { 508 return (ShadowDateFormat) shadowOf_(instance); 509 } 510 511 public static ShadowDefaultRequestDirector shadowOf(DefaultRequestDirector instance) { 512 return (ShadowDefaultRequestDirector) shadowOf_(instance); 513 } 514 515 public static ShadowDialog shadowOf(Dialog instance) { 516 return (ShadowDialog) shadowOf_(instance); 517 } 518 519 public static ShadowDialogFragment shadowOf(DialogFragment instance) { 520 return (ShadowDialogFragment) shadowOf_(instance); 521 } 522 523 public static ShadowDialogPreference shadowOf(DialogPreference instance) { 524 return (ShadowDialogPreference) shadowOf_(instance); 525 } 526 527 public static ShadowDrawable shadowOf(Drawable instance) { 528 return (ShadowDrawable) shadowOf_(instance); 529 } 530 531 public static ShadowDisplay shadowOf(Display instance) { 532 return (ShadowDisplay) shadowOf_(instance); 533 } 534 535 public static ShadowExpandableListView shadowOf(ExpandableListView instance) { 536 return (ShadowExpandableListView) shadowOf_(instance); 537 } 538 539 public static ShadowFilter shadowOf(Filter instance) { 540 return (ShadowFilter) shadowOf_(instance); 541 } 542 543 public static ShadowFragment shadowOf(Fragment instance) { 544 return (ShadowFragment) shadowOf_(instance); 545 } 546 547 public static ShadowFragmentActivity shadowOf(FragmentActivity instance) { 548 return (ShadowFragmentActivity) shadowOf_(instance); 549 } 550 551 public static ShadowFrameLayout shadowOf(FrameLayout instance) { 552 return (ShadowFrameLayout) shadowOf_(instance); 553 } 554 555 public static ShadowGallery shadowOf(Gallery instance) { 556 return (ShadowGallery) shadowOf_(instance); 557 } 558 559 public static ShadowGeocoder shadowOf(Geocoder instance) { 560 return (ShadowGeocoder) shadowOf_(instance); 561 } 562 563 public static ShadowGridView shadowOf(GridView instance) { 564 return (ShadowGridView) shadowOf_(instance); 565 } 566 567 public static ShadowHandler shadowOf(Handler instance) { 568 return (ShadowHandler) shadowOf_(instance); 569 } 570 571 public static ShadowHandlerThread shadowOf(HandlerThread instance) { 572 return (ShadowHandlerThread) shadowOf_(instance); 573 } 574 575 public static ShadowImageView shadowOf(ImageView instance) { 576 return (ShadowImageView) shadowOf_(instance); 577 } 578 579 public static ShadowInputMethodManager shadowOf(InputMethodManager instance) { 580 return (ShadowInputMethodManager) shadowOf_(instance); 581 } 582 583 public static ShadowIntent shadowOf(Intent instance) { 584 return (ShadowIntent) shadowOf_(instance); 585 } 586 587 public static ShadowJsPromptResult shadowOf(JsPromptResult instance) { 588 return (ShadowJsPromptResult) shadowOf_(instance); 589 } 590 591 public static ShadowJsResult shadowOf(JsResult instance) { 592 return (ShadowJsResult) shadowOf_(instance); 593 } 594 595 public static ShadowKeyguardManager shadowOf(KeyguardManager instance) { 596 return (ShadowKeyguardManager) shadowOf_(instance); 597 } 598 599 public static ShadowKeyGuardLock shadowOf(KeyguardManager.KeyguardLock instance) { 600 return (ShadowKeyGuardLock) shadowOf_(instance); 601 } 602 603 public static ShadowLayerDrawable shadowOf(LayerDrawable instance) { 604 return (ShadowLayerDrawable) shadowOf_(instance); 605 } 606 607 public static ShadowLayoutInflater shadowOf(LayoutInflater instance) { 608 return (ShadowLayoutInflater) shadowOf_(instance); 609 } 610 611 public static ShadowListActivity shadowOf(ListActivity instance) { 612 return (ShadowListActivity) shadowOf_(instance); 613 } 614 615 public static ShadowListPreference shadowOf(ListPreference instance) { 616 return (ShadowListPreference) shadowOf_(instance); 617 } 618 619 public static ShadowListView shadowOf(ListView instance) { 620 return (ShadowListView) shadowOf_(instance); 621 } 622 623 public static ShadowLocationManager shadowOf(LocationManager instance) { 624 return (ShadowLocationManager) shadowOf_(instance); 625 } 626 627 public static ShadowLooper shadowOf(Looper instance) { 628 return (ShadowLooper) shadowOf_(instance); 629 } 630 631 public static ShadowMatrix shadowOf(Matrix other) { 632 return (ShadowMatrix) Robolectric.shadowOf_(other); 633 } 634 635 public static ShadowMediaPlayer shadowOf(MediaPlayer instance) { 636 return (ShadowMediaPlayer) shadowOf_(instance); 637 } 638 639 public static ShadowMediaRecorder shadowOf(MediaRecorder instance) { 640 return (ShadowMediaRecorder) shadowOf_(instance); 641 } 642 643 public static ShadowMenuInflater shadowOf(MenuInflater instance) { 644 return (ShadowMenuInflater) shadowOf_(instance); 645 } 646 647 public static ShadowMotionEvent shadowOf(MotionEvent other) { 648 return (ShadowMotionEvent) Robolectric.shadowOf_(other); 649 } 650 651 public static ShadowNetworkInfo shadowOf(NetworkInfo instance) { 652 return (ShadowNetworkInfo) shadowOf_(instance); 653 } 654 655 public static ShadowNotification shadowOf(Notification other) { 656 return (ShadowNotification) Robolectric.shadowOf_(other); 657 } 658 659 public static ShadowNotificationManager shadowOf(NotificationManager other) { 660 return (ShadowNotificationManager) Robolectric.shadowOf_(other); 661 } 662 663 public static ShadowPaint shadowOf(Paint instance) { 664 return (ShadowPaint) shadowOf_(instance); 665 } 666 667 public static ShadowParcel shadowOf(Parcel instance) { 668 return (ShadowParcel) shadowOf_(instance); 669 } 670 671 public static ShadowPasswordTransformationMethod shadowOf(PasswordTransformationMethod instance) { 672 return (ShadowPasswordTransformationMethod) shadowOf_(instance); 673 } 674 675 public static ShadowPath shadowOf(Path instance) { 676 return (ShadowPath) shadowOf_(instance); 677 } 678 679 public static ShadowPendingIntent shadowOf(PendingIntent instance) { 680 return (ShadowPendingIntent) shadowOf_(instance); 681 } 682 683 public static ShadowPhoneNumberUtils shadowOf(PhoneNumberUtils instance) { 684 return (ShadowPhoneNumberUtils) shadowOf_(instance); 685 } 686 687 public static ShadowPowerManager shadowOf(PowerManager instance) { 688 return (ShadowPowerManager) shadowOf_(instance); 689 } 690 691 public static ShadowPreference shadowOf(Preference instance) { 692 return (ShadowPreference) shadowOf_(instance); 693 } 694 695 public static ShadowPreferenceActivity shadowOf(PreferenceActivity instance) { 696 return (ShadowPreferenceActivity) shadowOf_(instance); 697 } 698 699 public static ShadowPreferenceCategory shadowOf(PreferenceCategory instance) { 700 return (ShadowPreferenceCategory) shadowOf_(instance); 701 } 702 703 public static ShadowPreferenceGroup shadowOf(PreferenceGroup instance) { 704 return (ShadowPreferenceGroup) shadowOf_(instance); 705 } 706 707 public static ShadowPreferenceScreen shadowOf(PreferenceScreen instance) { 708 return (ShadowPreferenceScreen) shadowOf_(instance); 709 } 710 711 public static ShadowProgressBar shadowOf(ProgressBar instance) { 712 return (ShadowProgressBar) shadowOf_(instance); 713 } 714 715 public static ShadowProgressDialog shadowOf(ProgressDialog instance) { 716 return (ShadowProgressDialog) shadowOf_(instance); 717 } 718 719 public static ShadowRect shadowOf(Rect instance) { 720 return (ShadowRect) shadowOf_(instance); 721 } 722 723 public static ShadowRatingBar shadowOf(RatingBar instance) { 724 return (ShadowRatingBar) shadowOf_(instance); 725 } 726 727 public static ShadowRemoteViews shadowOf(RemoteViews instance) { 728 return (ShadowRemoteViews) shadowOf_(instance); 729 } 730 731 public static ShadowResolveInfo shadowOf(ResolveInfo instance) { 732 return (ShadowResolveInfo) shadowOf_(instance); 733 } 734 735 public static ShadowResourceCursorAdapter shadowOf(ResourceCursorAdapter instance) { 736 return (ShadowResourceCursorAdapter) shadowOf_(instance); 737 } 738 739 public static ShadowResources shadowOf(Resources instance) { 740 return (ShadowResources) shadowOf_(instance); 741 } 742 743 public static ShadowResultReceiver shadowOf(ResultReceiver instance) { 744 return (ShadowResultReceiver) shadowOf_(instance); 745 } 746 747 public static ShadowScanResult shadowOf(ScanResult instance) { 748 return (ShadowScanResult) shadowOf_(instance); 749 } 750 751 public static ShadowScrollView shadowOf(ScrollView instance) { 752 return (ShadowScrollView) shadowOf_(instance); 753 } 754 755 public static ShadowSeekBar shadowOf(SeekBar instance) { 756 return (ShadowSeekBar) shadowOf_(instance); 757 } 758 759 public static ShadowSensorManager shadowOf(SensorManager instance) { 760 return (ShadowSensorManager) shadowOf_(instance); 761 } 762 763 public static ShadowService shadowOf(Service instance) { 764 return (ShadowService) shadowOf_(instance); 765 } 766 767 public static ShadowShapeDrawable shadowOf(ShapeDrawable instance) { 768 return (ShadowShapeDrawable) shadowOf_(instance); 769 } 770 771 public static ShadowSimpleCursorAdapter shadowOf(SimpleCursorAdapter instance) { 772 return (ShadowSimpleCursorAdapter) shadowOf_(instance); 773 } 774 775 public static ShadowSmsManager shadowOf(SmsManager instance) { 776 return (ShadowSmsManager) shadowOf_(instance); 777 } 778 779 public static ShadowSQLiteCursor shadowOf(SQLiteCursor other) { 780 return (ShadowSQLiteCursor) Robolectric.shadowOf_(other); 781 } 782 783 public static ShadowSQLiteDatabase shadowOf(SQLiteDatabase other) { 784 return (ShadowSQLiteDatabase) Robolectric.shadowOf_(other); 785 } 786 787 public static ShadowSQLiteOpenHelper shadowOf(SQLiteOpenHelper other) { 788 return (ShadowSQLiteOpenHelper) Robolectric.shadowOf_(other); 789 } 790 791 public static ShadowSQLiteProgram shadowOf(SQLiteProgram other) { 792 return (ShadowSQLiteProgram) Robolectric.shadowOf_(other); 793 } 794 795 public static ShadowSQLiteQueryBuilder shadowOf(SQLiteQueryBuilder other) { 796 return (ShadowSQLiteQueryBuilder) Robolectric.shadowOf_(other); 797 } 798 799 public static ShadowSQLiteStatement shadowOf(SQLiteStatement other) { 800 return (ShadowSQLiteStatement) Robolectric.shadowOf_(other); 801 } 802 803 public static <E> ShadowSparseArray<E> shadowOf(SparseArray<E> other) { 804 //noinspection unchecked 805 return (ShadowSparseArray<E>) Robolectric.shadowOf_(other); 806 } 807 808 public static ShadowSslErrorHandler shadowOf(SslErrorHandler instance) { 809 return (ShadowSslErrorHandler) shadowOf_(instance); 810 } 811 812 public static ShadowStateListDrawable shadowOf(StateListDrawable instance) { 813 return (ShadowStateListDrawable) shadowOf_(instance); 814 } 815 816 public static ShadowTabHost shadowOf(TabHost instance) { 817 return (ShadowTabHost) shadowOf_(instance); 818 } 819 820 public static ShadowTabSpec shadowOf(TabHost.TabSpec instance) { 821 return (ShadowTabSpec) shadowOf_(instance); 822 } 823 824 public static ShadowTelephonyManager shadowOf(TelephonyManager instance) { 825 return (ShadowTelephonyManager) shadowOf_(instance); 826 } 827 828 public static ShadowTextPaint shadowOf(TextPaint instance) { 829 return (ShadowTextPaint) shadowOf_(instance); 830 } 831 832 public static ShadowTextView shadowOf(TextView instance) { 833 return (ShadowTextView) shadowOf_(instance); 834 } 835 836 public static ShadowToast shadowOf(Toast instance) { 837 return (ShadowToast) shadowOf_(instance); 838 } 839 840 public static ShadowTranslateAnimation shadowOf(TranslateAnimation instance) { 841 return (ShadowTranslateAnimation) shadowOf_(instance); 842 } 843 844 public static ShadowUriMatcher shadowOf(UriMatcher instance) { 845 return (ShadowUriMatcher) shadowOf_(instance); 846 } 847 848 public static ShadowView shadowOf(View instance) { 849 return (ShadowView) shadowOf_(instance); 850 } 851 852 public static ShadowViewAnimator shadowOf(ViewAnimator instance) { 853 return (ShadowViewAnimator) shadowOf_(instance); 854 } 855 856 public static ShadowViewConfiguration shadowOf(ViewConfiguration instance) { 857 return (ShadowViewConfiguration) shadowOf_(instance); 858 } 859 860 public static ShadowViewFlipper shadowOf(ViewFlipper instance) { 861 return (ShadowViewFlipper) shadowOf_(instance); 862 } 863 864 public static ShadowViewTreeObserver shadowOf(ViewTreeObserver instance) { 865 return (ShadowViewTreeObserver) shadowOf_(instance); 866 } 867 868 public static ShadowViewGroup shadowOf(ViewGroup instance) { 869 return (ShadowViewGroup) shadowOf_(instance); 870 } 871 872 public static ShadowVideoView shadowOf(VideoView instance) { 873 return (ShadowVideoView) shadowOf_(instance); 874 } 875 876 public static ShadowWebSettings shadowOf(WebSettings instance) { 877 return (ShadowWebSettings) shadowOf_(instance); 878 } 879 880 public static ShadowWebView shadowOf(WebView instance) { 881 return (ShadowWebView) shadowOf_(instance); 882 } 883 884 public static ShadowWifiConfiguration shadowOf(WifiConfiguration instance) { 885 return (ShadowWifiConfiguration) shadowOf_(instance); 886 } 887 888 public static ShadowWifiInfo shadowOf(WifiInfo instance) { 889 return (ShadowWifiInfo) shadowOf_(instance); 890 } 891 892 public static ShadowWifiManager shadowOf(WifiManager instance) { 893 return (ShadowWifiManager) shadowOf_(instance); 894 } 895 896 public static ShadowZoomButtonsController shadowOf(ZoomButtonsController instance) { 897 return (ShadowZoomButtonsController) shadowOf_(instance); 898 } 899 900 @SuppressWarnings({"unchecked"}) 901 public static <P, R> P shadowOf_(R instance) { 902 return (P) ShadowWrangler.getInstance().shadowOf(instance); 903 } 904 905 /** 906 * Runs any background tasks previously queued by {@link android.os.AsyncTask#execute(Object[])}. 907 * <p/> 908 * <p/> 909 * Note: calling this method does not pause or un-pause the scheduler. 910 */ 911 public static void runBackgroundTasks() { 912 getBackgroundScheduler().advanceBy(0); 913 } 914 915 /** 916 * Runs any immediately runnable tasks previously queued on the UI thread, 917 * e.g. by {@link Activity#runOnUiThread(Runnable)} or {@link android.os.AsyncTask#onPostExecute(Object)}. 918 * <p/> 919 * <p/> 920 * Note: calling this method does not pause or un-pause the scheduler. 921 */ 922 public static void runUiThreadTasks() { 923 getUiThreadScheduler().advanceBy(0); 924 } 925 926 public static void runUiThreadTasksIncludingDelayedTasks() { 927 getUiThreadScheduler().advanceToLastPostedRunnable(); 928 } 929 930 /** 931 * Sets up an HTTP response to be returned by calls to Apache's {@code HttpClient} implementers. 932 * 933 * @param statusCode the status code of the response 934 * @param responseBody the body of the response 935 * @param headers optional headers for the request 936 */ 937 public static void addPendingHttpResponse(int statusCode, String responseBody, Header... headers) { 938 getFakeHttpLayer().addPendingHttpResponse(statusCode, responseBody, headers); 939 } 940 941 /** 942 * Sets up an HTTP response to be returned by calls to Apache's {@code HttpClient} implementers. 943 * 944 * @param statusCode the status code of the response 945 * @param responseBody the body of the response 946 * @param contentType the contentType of the response 947 * @deprecated use {@link #addPendingHttpResponse(int, String, Header...)} instead 948 */ 949 public static void addPendingHttpResponseWithContentType(int statusCode, String responseBody, Header contentType) { 950 getFakeHttpLayer().addPendingHttpResponse(statusCode, responseBody, contentType); 951 } 952 953 /** 954 * Sets up an HTTP response to be returned by calls to Apache's {@code HttpClient} implementers. 955 * 956 * @param httpResponse the response 957 */ 958 public static void addPendingHttpResponse(HttpResponse httpResponse) { 959 getFakeHttpLayer().addPendingHttpResponse(httpResponse); 960 } 961 962 /** 963 * Accessor to obtain HTTP requests made during the current test in the order in which they were made. 964 * 965 * @param index index of the request to retrieve. 966 * @return the requested request. 967 */ 968 public static HttpRequest getSentHttpRequest(int index) { 969 return ShadowDefaultRequestDirector.getSentHttpRequest(index); 970 } 971 972 public static HttpRequest getLatestSentHttpRequest() { 973 return ShadowDefaultRequestDirector.getLatestSentHttpRequest(); 974 } 975 976 /** 977 * Accessor to find out if HTTP requests were made during the current test. 978 * 979 * @return whether a request was made. 980 */ 981 public static boolean httpRequestWasMade() { 982 return getShadowApplication().getFakeHttpLayer().hasRequestInfos(); 983 } 984 985 public static boolean httpRequestWasMade(String uri) { 986 return getShadowApplication().getFakeHttpLayer().hasRequestMatchingRule(new FakeHttpLayer.UriRequestMatcher(uri)); 987 } 988 989 /** 990 * Accessor to obtain metadata for an HTTP request made during the current test in the order in which they were made. 991 * 992 * @param index index of the request to retrieve. 993 * @return the requested request metadata. 994 */ 995 public static HttpRequestInfo getSentHttpRequestInfo(int index) { 996 return ShadowDefaultRequestDirector.getSentHttpRequestInfo(index); 997 } 998 999 /** 1000 * Adds an HTTP response rule. The response will be returned when the rule is matched. 1001 * 1002 * @param method method to match. 1003 * @param uri uri to match. 1004 * @param response response to return when a match is found. 1005 */ 1006 public static void addHttpResponseRule(String method, String uri, HttpResponse response) { 1007 getFakeHttpLayer().addHttpResponseRule(method, uri, response); 1008 } 1009 1010 /** 1011 * Adds an HTTP response rule with a default method of GET. The response will be returned when the rule is matched. 1012 * 1013 * @param uri uri to match. 1014 * @param response response to return when a match is found. 1015 */ 1016 public static void addHttpResponseRule(String uri, HttpResponse response) { 1017 getFakeHttpLayer().addHttpResponseRule(uri, response); 1018 } 1019 1020 /** 1021 * Adds an HTTP response rule. The response will be returned when the rule is matched. 1022 * 1023 * @param uri uri to match. 1024 * @param response response to return when a match is found. 1025 */ 1026 public static void addHttpResponseRule(String uri, String response) { 1027 getFakeHttpLayer().addHttpResponseRule(uri, response); 1028 } 1029 1030 /** 1031 * Adds an HTTP response rule. The response will be returned when the rule is matched. 1032 * 1033 * @param requestMatcher custom {@code RequestMatcher}. 1034 * @param response response to return when a match is found. 1035 */ 1036 public static void addHttpResponseRule(RequestMatcher requestMatcher, HttpResponse response) { 1037 getFakeHttpLayer().addHttpResponseRule(requestMatcher, response); 1038 } 1039 1040 /** 1041 * Adds an HTTP response rule. For each time the rule is matched, responses will be shifted 1042 * off the list and returned. When all responses have been given and the rule is matched again, 1043 * an exception will be thrown. 1044 * 1045 * @param requestMatcher custom {@code RequestMatcher}. 1046 * @param responses responses to return in order when a match is found. 1047 */ 1048 public static void addHttpResponseRule(RequestMatcher requestMatcher, List<? extends HttpResponse> responses) { 1049 getFakeHttpLayer().addHttpResponseRule(requestMatcher, responses); 1050 } 1051 1052 public static FakeHttpLayer getFakeHttpLayer() { 1053 return getShadowApplication().getFakeHttpLayer(); 1054 } 1055 1056 public static void setDefaultHttpResponse(int statusCode, String responseBody) { 1057 getFakeHttpLayer().setDefaultHttpResponse(statusCode, responseBody); 1058 } 1059 1060 public static void setDefaultHttpResponse(HttpResponse defaultHttpResponse) { 1061 getFakeHttpLayer().setDefaultHttpResponse(defaultHttpResponse); 1062 } 1063 1064 public static void clearHttpResponseRules() { 1065 getFakeHttpLayer().clearHttpResponseRules(); 1066 } 1067 1068 public static void clearPendingHttpResponses() { 1069 getFakeHttpLayer().clearPendingHttpResponses(); 1070 } 1071 1072 public static void pauseLooper(Looper looper) { 1073 ShadowLooper.pauseLooper(looper); 1074 } 1075 1076 public static void unPauseLooper(Looper looper) { 1077 ShadowLooper.unPauseLooper(looper); 1078 } 1079 1080 public static void pauseMainLooper() { 1081 ShadowLooper.pauseMainLooper(); 1082 } 1083 1084 public static void unPauseMainLooper() { 1085 ShadowLooper.unPauseMainLooper(); 1086 } 1087 1088 public static void idleMainLooper(long interval) { 1089 ShadowLooper.idleMainLooper(interval); 1090 } 1091 1092 public static Scheduler getUiThreadScheduler() { 1093 return shadowOf(Looper.getMainLooper()).getScheduler(); 1094 } 1095 1096 public static Scheduler getBackgroundScheduler() { 1097 return getShadowApplication().getBackgroundScheduler(); 1098 } 1099 1100 public static ShadowApplication getShadowApplication() { 1101 return shadowOf(Robolectric.application); 1102 } 1103 1104 public static void setDisplayMetricsDensity(float densityMultiplier) { 1105 shadowOf(getShadowApplication().getResources()).setDensity(densityMultiplier); 1106 } 1107 1108 public static void setDefaultDisplay(Display display) { 1109 shadowOf(getShadowApplication().getResources()).setDisplay(display); 1110 } 1111 1112 /** 1113 * Calls {@code performClick()} on a {@code View} after ensuring that it and its ancestors are visible and that it 1114 * is enabled. 1115 * 1116 * @param view the view to click on 1117 * @return true if {@code View.OnClickListener}s were found and fired, false otherwise. 1118 * @throws RuntimeException if the preconditions are not met. 1119 */ 1120 public static boolean clickOn(View view) { 1121 return shadowOf(view).checkedPerformClick(); 1122 } 1123 1124 public static String visualize(View view) { 1125 Canvas canvas = new Canvas(); 1126 view.draw(canvas); 1127 return shadowOf(canvas).getDescription(); 1128 } 1129 1130 public static String visualize(Canvas canvas) { 1131 return shadowOf(canvas).getDescription(); 1132 } 1133 1134 public static String visualize(Bitmap bitmap) { 1135 return shadowOf(bitmap).getDescription(); 1136 } 1137 1138 /** 1139 * Reflection helper methods. 1140 */ 1141 public static class Reflection { 1142 public static <T> T newInstanceOf(Class<T> clazz) { 1143 return Robolectric.newInstanceOf(clazz); 1144 } 1145 1146 public static Object newInstanceOf(String className) { 1147 return Robolectric.newInstanceOf(className); 1148 } 1149 1150 public static void setFinalStaticField(Class classWhichContainsField, String fieldName, Object newValue) { 1151 try { 1152 Field field = classWhichContainsField.getField(fieldName); 1153 field.setAccessible(true); 1154 1155 Field modifiersField = Field.class.getDeclaredField("modifiers"); 1156 modifiersField.setAccessible(true); 1157 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); 1158 1159 field.set(null, newValue); 1160 } catch (NoSuchFieldException e) { 1161 throw new RuntimeException(e); 1162 } catch (IllegalAccessException e) { 1163 throw new RuntimeException(e); 1164 } 1165 } 1166 } 1167 1168} 1169