Robolectric.java revision 31af8607e2c7da6ba8708f57c4b67b6b4aaad921
1package com.xtremelabs.robolectric;
2
3import android.app.*;
4import android.appwidget.AppWidgetManager;
5import android.content.ContentValues;
6import android.content.Context;
7import android.content.ContextWrapper;
8import android.content.Intent;
9import android.content.res.Resources;
10import android.database.sqlite.SQLiteCursor;
11import android.database.sqlite.SQLiteDatabase;
12import android.database.sqlite.SQLiteOpenHelper;
13import android.database.sqlite.SQLiteQueryBuilder;
14import android.graphics.Canvas;
15import android.graphics.Paint;
16import android.graphics.Path;
17import android.graphics.drawable.BitmapDrawable;
18import android.graphics.drawable.Drawable;
19import android.hardware.Camera;
20import android.location.Geocoder;
21import android.location.LocationManager;
22import android.media.AudioManager;
23import android.media.MediaRecorder;
24import android.net.ConnectivityManager;
25import android.net.NetworkInfo;
26import android.os.Handler;
27import android.os.Looper;
28import android.view.Display;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.webkit.WebView;
33import android.widget.*;
34import com.google.android.maps.GeoPoint;
35import com.google.android.maps.ItemizedOverlay;
36import com.google.android.maps.MapController;
37import com.google.android.maps.MapView;
38import com.xtremelabs.robolectric.shadows.*;
39import com.xtremelabs.robolectric.util.Implements;
40import com.xtremelabs.robolectric.util.Scheduler;
41import com.xtremelabs.robolectric.view.TestSharedPreferences;
42
43import java.lang.reflect.Constructor;
44import java.lang.reflect.InvocationTargetException;
45import java.util.Arrays;
46import java.util.List;
47
48public class Robolectric {
49    public static Application application;
50    public static Scheduler backgroundScheduler;
51    public static Scheduler uiThreadScheduler;
52
53    public static <T> T newInstanceOf(Class<T> clazz) {
54        try {
55            Constructor<T> defaultConstructor = clazz.getDeclaredConstructor();
56            defaultConstructor.setAccessible(true);
57            return defaultConstructor.newInstance();
58        } catch (InstantiationException e) {
59            throw new RuntimeException(e);
60        } catch (IllegalAccessException e) {
61            throw new RuntimeException(e);
62        } catch (NoSuchMethodException e) {
63            throw new RuntimeException(e);
64        } catch (InvocationTargetException e) {
65            throw new RuntimeException(e);
66        }
67    }
68
69    public static void bindShadowClass(Class<?> realClass, Class<?> shadowClass) {
70        ShadowWrangler.getInstance().bindShadowClass(realClass, shadowClass);
71    }
72
73    public static void bindShadowClass(Class<?> shadowClass) {
74        Implements realClass = shadowClass.getAnnotation(Implements.class);
75        if (realClass == null) {
76            throw new IllegalArgumentException(shadowClass + " is not annotated with @Implements");
77        }
78
79        try {
80            bindShadowClass(realClass.value(), shadowClass);
81        } catch (TypeNotPresentException ignored) {
82            //this allows users of the robolectric.jar file to use the non-Google APIs version of the api
83        }
84    }
85
86    public static void bindDefaultShadowClasses() {
87        bindShadowClasses(getDefaultShadowClasses());
88    }
89
90    public static void bindShadowClasses(List<Class<?>> shadowClasses) {
91        for (Class<?> shadowClass : shadowClasses) {
92            bindShadowClass(shadowClass);
93        }
94    }
95
96    /**
97     * Invoke this utility method in tests to reveal which Android api classes and methods are being invoked
98     * for which there are no shadows or shadow methods. This helps expose which methods are being invoked
99     * either by a third party library or application code which need new shadow methods to be written. Generates
100     * output for the current test only.
101     */
102    public static void logMissingInvokedShadowMethods() {
103        ShadowWrangler.getInstance().logMissingInvokedShadowMethods();
104    }
105
106    /**
107     * Calls {@code performClick()} on a {@code View} after ensuring that it and its ancestors are visible and that it
108     * is enabled.
109     *
110     * @param view the view to click on
111     * @return true if {@code View.OnClickListener}s were found and fired, false otherwise.
112     * @throws RuntimeException if the preconditions are not met.
113     */
114    public static boolean clickOn(View view) {
115        return shadowOf(view).checkedPerformClick();
116    }
117
118    public static List<Class<?>> getDefaultShadowClasses() {
119        return Arrays.asList(
120                ShadowAbsoluteLayout.class,
121                ShadowAbsSpinner.class,
122                ShadowAbstractCursor.class,
123                ShadowActivity.class,
124                ShadowAdapterView.class,
125                ShadowAddress.class,
126                ShadowAlertDialog.class,
127                ShadowAlertDialog.ShadowBuilder.class,
128                ShadowApplication.class,
129                ShadowAppWidgetManager.class,
130                ShadowAsyncTask.class,
131                ShadowAudioManager.class,
132                ShadowBaseAdapter.class,
133                ShadowBitmapDrawable.class,
134                ShadowBundle.class,
135                ShadowCamera.class,
136                ShadowCameraParameters.class,
137                ShadowCanvas.class,
138                ShadowCompoundButton.class,
139                ShadowComponentName.class,
140                ShadowConnectivityManager.class,
141                ShadowContentValues.class,
142                ShadowContext.class,
143                ShadowContextWrapper.class,
144                ShadowContextThemeWrapper.class,
145                ShadowDisplay.class,
146                ShadowDrawable.class,
147                ShadowDialog.class,
148                ShadowEditText.class,
149                ShadowGeocoder.class,
150                ShadowGeoPoint.class,
151                ShadowHandler.class,
152                ShadowImageView.class,
153                ShadowIntent.class,
154                ShadowIntentFilter.class,
155                ShadowItemizedOverlay.class,
156                ShadowLayoutInflater.class,
157                ShadowLayoutParams.class,
158                ShadowListActivity.class,
159                ShadowListView.class,
160                ShadowLocation.class,
161                ShadowLocationManager.class,
162                ShadowLooper.class,
163                ShadowMapController.class,
164                ShadowMapActivity.class,
165                ShadowMapView.class,
166                ShadowMediaRecorder.class,
167                ShadowMotionEvent.class,
168                ShadowNetworkInfo.class,
169                ShadowOverlayItem.class,
170                ShadowPaint.class,
171                ShadowPath.class,
172                ShadowPendingIntent.class,
173                ShadowPoint.class,
174                ShadowPreferenceManager.class,
175                ShadowRect.class,
176                ShadowRemoteViews.class,
177                ShadowResources.class,
178                ShadowService.class,
179                ShadowSettings.class,
180                ShadowSettings.ShadowSecure.class,
181                ShadowSettings.ShadowSystem.class,
182                ShadowSpannableStringBuilder.class,
183                ShadowSQLiteDatabase.class,
184                ShadowSQLiteCursor.class,
185                ShadowSQLiteOpenHelper.class,
186                ShadowSQLiteQueryBuilder.class,
187                ShadowTextUtils.class,
188                ShadowTextView.class,
189                ShadowToast.class,
190                ShadowTypedValue.class,
191                ShadowView.class,
192                ShadowViewGroup.class,
193                ShadowWebView.class,
194                ShadowWifiManager.class,
195                ShadowZoomButtonsController.class
196        );
197    }
198
199    public static void resetStaticState() {
200        ShadowWrangler.getInstance().silence();
201        Robolectric.application = new Application();
202        Robolectric.backgroundScheduler = new Scheduler();
203        Robolectric.uiThreadScheduler = new Scheduler();
204        TestSharedPreferences.reset();
205        ShadowToast.reset();
206        ShadowAlertDialog.reset();
207        ShadowDialog.reset();
208        ShadowLooper.resetAll();
209    }
210
211    public static <T> T directlyOn(T shadowedObject) {
212        return RobolectricInternals.directlyOn(shadowedObject);
213    }
214
215    public static ShadowDrawable shadowOf(Drawable instance) {
216        return (ShadowDrawable) shadowOf_(instance);
217    }
218
219    public static ShadowToast shadowOf(Toast instance) {
220        return (ShadowToast) shadowOf_(instance);
221    }
222
223    public static ShadowNetworkInfo shadowOf(NetworkInfo instance) {
224        return (ShadowNetworkInfo) shadowOf_(instance);
225    }
226
227    public static ShadowConnectivityManager shadowOf(ConnectivityManager instance) {
228        return (ShadowConnectivityManager) shadowOf_(instance);
229    }
230
231    public static ShadowBitmapDrawable shadowOf(BitmapDrawable instance) {
232        return (ShadowBitmapDrawable) shadowOf_(instance);
233    }
234
235    public static ShadowZoomButtonsController shadowOf(ZoomButtonsController instance) {
236        return (ShadowZoomButtonsController) shadowOf_(instance);
237    }
238
239    public static ShadowGeoPoint shadowOf(GeoPoint instance) {
240        return (ShadowGeoPoint) shadowOf_(instance);
241    }
242
243    public static ShadowMapView shadowOf(MapView instance) {
244        return (ShadowMapView) shadowOf_(instance);
245    }
246
247    public static ShadowMapController shadowOf(MapController instance) {
248        return (ShadowMapController) shadowOf_(instance);
249    }
250
251    public static ShadowItemizedOverlay shadowOf(ItemizedOverlay instance) {
252        return (ShadowItemizedOverlay) shadowOf_(instance);
253    }
254
255    public static ShadowListView shadowOf(ListView instance) {
256        return (ShadowListView) shadowOf_(instance);
257    }
258
259    public static ShadowActivity shadowOf(Activity instance) {
260        return (ShadowActivity) shadowOf_(instance);
261    }
262
263    public static ShadowContextWrapper shadowOf(ContextWrapper instance) {
264        return (ShadowContextWrapper) shadowOf_(instance);
265    }
266
267    public static ShadowContextWrapper shadowOf(Context instance) {
268        return (ShadowContextWrapper) shadowOf_(instance);
269    }
270
271    public static ShadowPaint shadowOf(Paint instance) {
272        return (ShadowPaint) shadowOf_(instance);
273    }
274
275    public static ShadowPath shadowOf(Path instance) {
276        return (ShadowPath) shadowOf_(instance);
277    }
278
279    public static ShadowListActivity shadowOf(ListActivity instance) {
280        return (ShadowListActivity) shadowOf_(instance);
281    }
282
283    public static ShadowHandler shadowOf(Handler instance) {
284        return (ShadowHandler) shadowOf_(instance);
285    }
286
287    public static ShadowIntent shadowOf(Intent instance) {
288        return (ShadowIntent) shadowOf_(instance);
289    }
290
291    public static ShadowView shadowOf(View instance) {
292        return (ShadowView) shadowOf_(instance);
293    }
294
295    public static ShadowViewGroup shadowOf(ViewGroup instance) {
296        return (ShadowViewGroup) shadowOf_(instance);
297    }
298
299    public static ShadowWebView shadowOf(WebView instance) {
300        return (ShadowWebView) shadowOf_(instance);
301    }
302
303    public static ShadowAdapterView shadowOf(AdapterView instance) {
304        return (ShadowAdapterView) shadowOf_(instance);
305    }
306
307    public static ShadowTextView shadowOf(TextView instance) {
308        return (ShadowTextView) shadowOf_(instance);
309    }
310
311    public static ShadowImageView shadowOf(ImageView instance) {
312        return (ShadowImageView) shadowOf_(instance);
313    }
314
315    public static ShadowRemoteViews shadowOf(RemoteViews instance) {
316        return (ShadowRemoteViews) shadowOf_(instance);
317    }
318
319    public static ShadowDialog shadowOf(Dialog instance) {
320        return (ShadowDialog) shadowOf_(instance);
321    }
322
323    public static ShadowAlertDialog shadowOf(AlertDialog instance) {
324        return (ShadowAlertDialog) shadowOf_(instance);
325    }
326
327    public static ShadowLooper shadowOf(Looper instance) {
328        return (ShadowLooper) shadowOf_(instance);
329    }
330
331    public static ShadowCanvas shadowOf(Canvas instance) {
332        return (ShadowCanvas) shadowOf_(instance);
333    }
334
335    public static ShadowLocationManager shadowOf(LocationManager instance) {
336        return (ShadowLocationManager) shadowOf_(instance);
337    }
338
339    public static ShadowAppWidgetManager shadowOf(AppWidgetManager instance) {
340        return (ShadowAppWidgetManager) shadowOf_(instance);
341    }
342
343    public static ShadowResources shadowOf(Resources instance) {
344        return (ShadowResources) shadowOf_(instance);
345    }
346
347    public static ShadowLayoutInflater shadowOf(LayoutInflater instance) {
348        return (ShadowLayoutInflater) shadowOf_(instance);
349    }
350
351    public static ShadowDisplay shadowOf(Display instance) {
352        return (ShadowDisplay) shadowOf_(instance);
353    }
354
355    public static ShadowAudioManager shadowOf(AudioManager instance) {
356        return (ShadowAudioManager) shadowOf_(instance);
357    }
358
359    public static ShadowGeocoder shadowOf(Geocoder instance) {
360        return (ShadowGeocoder) shadowOf_(instance);
361    }
362
363    public static ShadowSQLiteDatabase shadowOf(SQLiteDatabase other) {
364        return (ShadowSQLiteDatabase) Robolectric.shadowOf_(other);
365    }
366
367    public static ShadowSQLiteCursor shadowOf(SQLiteCursor other) {
368        return (ShadowSQLiteCursor) Robolectric.shadowOf_(other);
369    }
370
371    public static ShadowSQLiteOpenHelper shadowOf(SQLiteOpenHelper other) {
372        return (ShadowSQLiteOpenHelper) Robolectric.shadowOf_(other);
373    }
374
375    public static ShadowSQLiteQueryBuilder shadowOf(SQLiteQueryBuilder other) {
376        return (ShadowSQLiteQueryBuilder) Robolectric.shadowOf_(other);
377    }
378
379    public static ShadowContentValues shadowOf(ContentValues other) {
380        return (ShadowContentValues) Robolectric.shadowOf_(other);
381    }
382
383    public static ShadowCamera shadowOf(Camera instance) {
384        return (ShadowCamera) shadowOf_(instance);
385    }
386
387    public static ShadowCameraParameters shadowOf(Camera.Parameters instance) {
388        return (ShadowCameraParameters) shadowOf_(instance);
389    }
390
391    public static ShadowMediaRecorder shadowOf(MediaRecorder instance) {
392        return (ShadowMediaRecorder) shadowOf_(instance);
393    }
394
395    @SuppressWarnings({"unchecked"})
396    public static <P, R> P shadowOf_(R instance) {
397        return (P) ShadowWrangler.getInstance().shadowOf(instance);
398    }
399
400    public static void runBackgroundTasks() {
401        backgroundScheduler.tick(0);
402    }
403
404    public static void runUiThreadTasks() {
405        uiThreadScheduler.tick(0);
406    }
407}
408