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