• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric;
2 
3 import static org.robolectric.shadows.ShadowAssetManager.useLegacy;
4 
5 import android.annotation.IdRes;
6 import android.app.Activity;
7 import android.app.Fragment;
8 import android.app.IntentService;
9 import android.app.Service;
10 import android.app.backup.BackupAgent;
11 import android.content.ContentProvider;
12 import android.content.Intent;
13 import android.os.Bundle;
14 import android.util.AttributeSet;
15 import android.view.View;
16 import org.robolectric.android.AttributeSetBuilderImpl;
17 import org.robolectric.android.AttributeSetBuilderImpl.ArscResourceResolver;
18 import org.robolectric.android.AttributeSetBuilderImpl.LegacyResourceResolver;
19 import org.robolectric.android.controller.ActivityController;
20 import org.robolectric.android.controller.BackupAgentController;
21 import org.robolectric.android.controller.ContentProviderController;
22 import org.robolectric.android.controller.FragmentController;
23 import org.robolectric.android.controller.IntentServiceController;
24 import org.robolectric.android.controller.ServiceController;
25 import org.robolectric.shadows.ShadowApplication;
26 import org.robolectric.util.ReflectionHelpers;
27 import org.robolectric.util.Scheduler;
28 
29 public class Robolectric {
30 
buildService(Class<T> serviceClass)31   public static <T extends Service> ServiceController<T> buildService(Class<T> serviceClass) {
32     return buildService(serviceClass, null);
33   }
34 
buildService(Class<T> serviceClass, Intent intent)35   public static <T extends Service> ServiceController<T> buildService(Class<T> serviceClass, Intent intent) {
36     return ServiceController.of(ReflectionHelpers.callConstructor(serviceClass), intent);
37   }
38 
setupService(Class<T> serviceClass)39   public static <T extends Service> T setupService(Class<T> serviceClass) {
40     return buildService(serviceClass).create().get();
41   }
42 
buildIntentService(Class<T> serviceClass)43   public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass) {
44     return buildIntentService(serviceClass, null);
45   }
46 
buildIntentService(Class<T> serviceClass, Intent intent)47   public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass, Intent intent) {
48     return IntentServiceController.of(ReflectionHelpers.callConstructor(serviceClass), intent);
49   }
50 
setupIntentService(Class<T> serviceClass)51   public static <T extends IntentService> T setupIntentService(Class<T> serviceClass) {
52     return buildIntentService(serviceClass).create().get();
53   }
54 
buildContentProvider(Class<T> contentProviderClass)55   public static <T extends ContentProvider> ContentProviderController<T> buildContentProvider(Class<T> contentProviderClass) {
56     return ContentProviderController.of(ReflectionHelpers.callConstructor(contentProviderClass));
57   }
58 
setupContentProvider(Class<T> contentProviderClass)59   public static <T extends ContentProvider> T setupContentProvider(Class<T> contentProviderClass) {
60     return buildContentProvider(contentProviderClass).create().get();
61   }
62 
setupContentProvider(Class<T> contentProviderClass, String authority)63   public static <T extends ContentProvider> T setupContentProvider(Class<T> contentProviderClass, String authority) {
64     return buildContentProvider(contentProviderClass).create(authority).get();
65   }
66 
buildActivity(Class<T> activityClass)67   public static <T extends Activity> ActivityController<T> buildActivity(Class<T> activityClass) {
68     return buildActivity(activityClass, null);
69   }
70 
buildActivity(Class<T> activityClass, Intent intent)71   public static <T extends Activity> ActivityController<T> buildActivity(Class<T> activityClass, Intent intent) {
72     return ActivityController.of(ReflectionHelpers.callConstructor(activityClass), intent);
73   }
74 
setupActivity(Class<T> activityClass)75   public static <T extends Activity> T setupActivity(Class<T> activityClass) {
76     return buildActivity(activityClass).setup().get();
77   }
78 
buildFragment(Class<T> fragmentClass)79   public static <T extends Fragment> FragmentController<T> buildFragment(Class<T> fragmentClass) {
80     return FragmentController.of(ReflectionHelpers.callConstructor(fragmentClass));
81   }
82 
buildFragment(Class<T> fragmentClass, Bundle arguments)83   public static <T extends Fragment> FragmentController<T> buildFragment(Class<T> fragmentClass,
84                                                                          Bundle arguments) {
85     return FragmentController.of(ReflectionHelpers.callConstructor(fragmentClass), arguments);
86   }
87 
buildFragment(Class<T> fragmentClass, Class<? extends Activity> activityClass)88   public static <T extends Fragment> FragmentController<T> buildFragment(Class<T> fragmentClass,
89                                                                          Class<? extends Activity> activityClass) {
90     return FragmentController.of(ReflectionHelpers.callConstructor(fragmentClass), activityClass);
91   }
92 
buildFragment(Class<T> fragmentClass, Intent intent)93   public static <T extends Fragment> FragmentController<T> buildFragment(Class<T> fragmentClass, Intent intent) {
94     return FragmentController.of(ReflectionHelpers.callConstructor(fragmentClass), intent);
95   }
96 
buildFragment(Class<T> fragmentClass, Intent intent, Bundle arguments)97   public static <T extends Fragment> FragmentController<T> buildFragment(Class<T> fragmentClass,
98                                                                          Intent intent,
99                                                                          Bundle arguments) {
100     return FragmentController.of(ReflectionHelpers.callConstructor(fragmentClass), intent, arguments);
101   }
102 
buildFragment(Class<T> fragmentClass, Class<? extends Activity> activityClass, Intent intent)103   public static <T extends Fragment> FragmentController<T> buildFragment(Class<T> fragmentClass,
104                                                                          Class<? extends Activity> activityClass,
105                                                                          Intent intent) {
106     return FragmentController.of(ReflectionHelpers.callConstructor(fragmentClass), activityClass, intent);
107   }
108 
buildFragment(Class<T> fragmentClass, Class<? extends Activity> activityClass, Bundle arguments)109   public static <T extends Fragment> FragmentController<T> buildFragment(Class<T> fragmentClass,
110                                                                          Class<? extends Activity> activityClass,
111                                                                          Bundle arguments) {
112     return FragmentController.of(ReflectionHelpers.callConstructor(fragmentClass), activityClass, arguments);
113   }
114 
buildFragment(Class<T> fragmentClass, Class<? extends Activity> activityClass, Intent intent, Bundle arguments)115   public static <T extends Fragment> FragmentController<T> buildFragment(Class<T> fragmentClass,
116                                                                          Class<? extends Activity> activityClass,
117                                                                          Intent intent,
118                                                                          Bundle arguments) {
119     return FragmentController.of(ReflectionHelpers.callConstructor(fragmentClass), activityClass, intent, arguments);
120   }
121 
buildBackupAgent(Class<T> backupAgentClass)122   public static <T extends BackupAgent> BackupAgentController<T> buildBackupAgent(Class<T> backupAgentClass) {
123     return BackupAgentController.of(ReflectionHelpers.callConstructor(backupAgentClass));
124   }
125 
setupBackupAgent(Class<T> backupAgentClass)126   public static <T extends BackupAgent> T setupBackupAgent(Class<T> backupAgentClass) {
127     return buildBackupAgent(backupAgentClass).create().get();
128   }
129 
130   /**
131    * Allows for the programmatic creation of an {@link AttributeSet}.
132    *
133    * Useful for testing {@link View} classes without the need for creating XML snippets.
134    */
buildAttributeSet()135   public static org.robolectric.android.AttributeSetBuilder buildAttributeSet() {
136     if (useLegacy()) {
137       return new AttributeSetBuilderImpl(
138           new LegacyResourceResolver(RuntimeEnvironment.application,
139               RuntimeEnvironment.getCompileTimeResourceTable())) {};
140     } else {
141       return new AttributeSetBuilderImpl(
142           new ArscResourceResolver(RuntimeEnvironment.application)) {};
143     }
144   }
145 
146   /**
147    * Builder of {@link AttributeSet}s.
148    *
149    * @deprecated Use {@link org.robolectric.android.AttributeSetBuilder} instead.
150    */
151   @Deprecated
152   public interface AttributeSetBuilder {
153     /**
154      * Set an attribute to the given value.
155      *
156      * The value will be interpreted according to the attribute's format.
157      *
158      * @param resId The attribute resource id to set.
159      * @param value The value to set.
160      * @return This {@link org.robolectric.android.AttributeSetBuilder}.
161      */
addAttribute(@dRes int resId, String value)162     AttributeSetBuilder addAttribute(@IdRes int resId, String value);
163 
164     /**
165      * Set the style attribute to the given value.
166      *
167      * The value will be interpreted as a resource reference.
168      *
169      * @param value The value for the specified attribute in this {@link AttributeSet}.
170      * @return This {@link org.robolectric.android.AttributeSetBuilder}.
171      */
setStyleAttribute(String value)172     AttributeSetBuilder setStyleAttribute(String value);
173 
174     /**
175      * Build an {@link AttributeSet} with the antecedent attributes.
176      *
177      * @return A new {@link AttributeSet}.
178      */
build()179     AttributeSet build();
180   }
181 
182   /**
183    * Return the foreground scheduler (e.g. the UI thread scheduler).
184    *
185    * @return Foreground scheduler.
186    */
getForegroundThreadScheduler()187   public static Scheduler getForegroundThreadScheduler() {
188     return ShadowApplication.getInstance().getForegroundThreadScheduler();
189   }
190 
191   /**
192    * Execute all runnables that have been enqueued on the foreground scheduler.
193    */
flushForegroundThreadScheduler()194   public static void flushForegroundThreadScheduler() {
195     getForegroundThreadScheduler().advanceToLastPostedRunnable();
196   }
197 
198   /**
199    * Return the background scheduler.
200    *
201    * @return Background scheduler.
202    */
getBackgroundThreadScheduler()203   public static Scheduler getBackgroundThreadScheduler() {
204     return ShadowApplication.getInstance().getBackgroundThreadScheduler();
205   }
206 
207   /**
208    * Execute all runnables that have been enqueued on the background scheduler.
209    */
flushBackgroundThreadScheduler()210   public static void flushBackgroundThreadScheduler() {
211     getBackgroundThreadScheduler().advanceToLastPostedRunnable();
212   }
213 }
214