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