aboutsummaryrefslogtreecommitdiff
path: root/robolectric/src/main/java/org/robolectric/android/internal/RoboMonitoringInstrumentation.java
blob: a9665b7069faacae8a8dcd62319873984bd0110f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package org.robolectric.android.internal;

import static org.robolectric.Shadows.shadowOf;
import static org.robolectric.shadow.api.Shadow.extract;

import android.app.Activity;
import android.app.Application;
import android.app.Instrumentation;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.UserHandle;
import android.util.DisplayMetrics;
import android.util.Log;
import androidx.test.internal.runner.intent.IntentMonitorImpl;
import androidx.test.internal.runner.lifecycle.ActivityLifecycleMonitorImpl;
import androidx.test.internal.runner.lifecycle.ApplicationLifecycleMonitorImpl;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.intent.IntentMonitorRegistry;
import androidx.test.runner.intent.IntentStubberRegistry;
import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
import androidx.test.runner.lifecycle.ApplicationLifecycleMonitorRegistry;
import androidx.test.runner.lifecycle.ApplicationStage;
import androidx.test.runner.lifecycle.Stage;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.LooperMode;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowActivity;
import org.robolectric.shadows.ShadowInstrumentation;
import org.robolectric.shadows.ShadowLooper;
import org.robolectric.shadows.ShadowPausedLooper;

/**
 * A Robolectric instrumentation that acts like a slimmed down {@link
 * androidx.test.runner.MonitoringInstrumentation} with only the parts needed for Robolectric.
 */
@SuppressWarnings("RestrictTo")
public class RoboMonitoringInstrumentation extends Instrumentation {

  private static final String TAG = "RoboInstrumentation";

  private final ActivityLifecycleMonitorImpl lifecycleMonitor = new ActivityLifecycleMonitorImpl();
  private final ApplicationLifecycleMonitorImpl applicationMonitor =
      new ApplicationLifecycleMonitorImpl();
  private final IntentMonitorImpl intentMonitor = new IntentMonitorImpl();
  private final List<ActivityController<?>> createdActivities = new ArrayList<>();

  private final AtomicBoolean attachedConfigListener = new AtomicBoolean();

  /**
   * Sets up lifecycle monitoring, and argument registry.
   *
   * <p>Subclasses must call up to onCreate(). This onCreate method does not call start() it is the
   * subclasses responsibility to call start if it desires.
   */
  @Override
  public void onCreate(Bundle arguments) {
    InstrumentationRegistry.registerInstance(this, arguments);
    ActivityLifecycleMonitorRegistry.registerInstance(lifecycleMonitor);
    ApplicationLifecycleMonitorRegistry.registerInstance(applicationMonitor);
    IntentMonitorRegistry.registerInstance(intentMonitor);
    super.onCreate(arguments);
  }

  @Override
  public void waitForIdleSync() {
    shadowOf(Looper.getMainLooper()).idle();
  }

  @Override
  public Activity startActivitySync(final Intent intent) {
    return startActivitySyncInternal(intent).get();
  }

  public ActivityController<? extends Activity> startActivitySyncInternal(Intent intent) {
    return startActivitySyncInternal(intent, /* activityOptions= */ null);
  }

  public ActivityController<? extends Activity> startActivitySyncInternal(
      Intent intent, @Nullable Bundle activityOptions) {
    ActivityInfo ai = intent.resolveActivityInfo(getTargetContext().getPackageManager(), 0);
    if (ai == null) {
      throw new RuntimeException(
          "Unable to resolve activity for "
              + intent
              + " -- see https://github.com/robolectric/robolectric/pull/4736 for details");
    }

    Class<? extends Activity> activityClass;
    String activityClassName = ai.targetActivity != null ? ai.targetActivity : ai.name;
    try {
      activityClass = Class.forName(activityClassName).asSubclass(Activity.class);
    } catch (ClassNotFoundException e) {
      throw new RuntimeException("Could not load activity " + ai.name, e);
    }

    if (attachedConfigListener.compareAndSet(false, true)
        && !Boolean.getBoolean("robolectric.createActivityContexts")) {
      // To avoid infinite recursion listen to the system resources, this will be updated before
      // the application resources but because activities use the application resources they will
      // get updated by the first activity (via updateConfiguration).
      shadowOf(Resources.getSystem()).addConfigurationChangeListener(this::updateConfiguration);
    }

    AtomicReference<ActivityController<? extends Activity>> activityControllerReference =
        new AtomicReference<>();
    ShadowInstrumentation.runOnMainSyncNoIdle(
        () -> {
          ActivityController<? extends Activity> controller =
              Robolectric.buildActivity(activityClass, intent, activityOptions);
          activityControllerReference.set(controller);
          controller.create();
          if (controller.get().isFinishing()) {
            controller.destroy();
          } else {
            createdActivities.add(controller);
            controller
                .start()
                .postCreate(null)
                .resume()
                .visible()
                .windowFocusChanged(true)
                .topActivityResumed(true);
          }
        });
    return activityControllerReference.get();
  }

  @Override
  public void callApplicationOnCreate(Application app) {
    if (Boolean.getBoolean("robolectric.createActivityContexts")) {
      shadowOf(app.getResources()).addConfigurationChangeListener(this::updateConfiguration);
    }
    applicationMonitor.signalLifecycleChange(app, ApplicationStage.PRE_ON_CREATE);
    super.callApplicationOnCreate(app);
    applicationMonitor.signalLifecycleChange(app, ApplicationStage.CREATED);
  }

  /**
   * Executes a runnable on the main thread, blocking until it is complete.
   *
   * <p>When in INSTUMENTATION_TEST Looper mode, the runnable is posted to the main handler and the
   * caller's thread blocks until that runnable has finished. When a Throwable is thrown in the
   * runnable, the exception is propagated back to the caller's thread. If it is an unchecked
   * throwable, it will be rethrown as is. If it is a checked exception, it will be rethrown as a
   * {@link RuntimeException}.
   *
   * <p>For other Looper modes, the main looper is idled and then the runnable is executed in the
   * caller's thread.
   *
   * @param runnable a runnable to be executed on the main thread
   */
  @Override
  public void runOnMainSync(Runnable runnable) {
    if (ShadowLooper.looperMode() == LooperMode.Mode.INSTRUMENTATION_TEST) {
      FutureTask<Void> wrapped = new FutureTask<>(runnable, null);
      Shadow.<ShadowPausedLooper>extract(Looper.getMainLooper()).postSync(wrapped);
      try {
        wrapped.get();
      } catch (InterruptedException e) {
        throw new RuntimeException(e);
      } catch (ExecutionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof RuntimeException) {
          throw (RuntimeException) cause;
        } else if (cause instanceof Error) {
          throw (Error) cause;
        }
        throw new RuntimeException(cause);
      }
    } else {
      // TODO: Use ShadowPausedLooper#postSync for PAUSED looper mode which provides more realistic
      //  behavior (i.e. it only runs to the runnable, it doesn't completely idle).
      waitForIdleSync();
      runnable.run();
    }
  }

  /** {@inheritDoc} */
  @Override
  public ActivityResult execStartActivity(
      Context who,
      IBinder contextThread,
      IBinder token,
      Activity target,
      Intent intent,
      int requestCode,
      Bundle options) {
    intentMonitor.signalIntent(intent);
    ActivityResult ar = stubResultFor(intent);
    if (ar != null) {
      Log.i(TAG, String.format("Stubbing intent %s", intent));
    } else {
      ar = super.execStartActivity(who, contextThread, token, target, intent, requestCode, options);
    }
    if (ar != null && target != null) {
      ShadowActivity shadowActivity = extract(target);
      postDispatchActivityResult(shadowActivity, null, requestCode, ar);
    }
    return null;
  }

  /** This API was added in Android API 23 (M) */
  @Override
  public ActivityResult execStartActivity(
      Context who,
      IBinder contextThread,
      IBinder token,
      String target,
      Intent intent,
      int requestCode,
      Bundle options) {
    intentMonitor.signalIntent(intent);
    ActivityResult ar = stubResultFor(intent);
    if (ar != null) {
      Log.i(TAG, String.format("Stubbing intent %s", intent));
    } else {
      ar = super.execStartActivity(who, contextThread, token, target, intent, requestCode, options);
    }
    if (ar != null && who instanceof Activity) {
      ShadowActivity shadowActivity = extract(who);
      postDispatchActivityResult(shadowActivity, target, requestCode, ar);
    }
    return null;
  }

  /** This API was added in Android API 17 (JELLY_BEAN_MR1) */
  @Override
  public ActivityResult execStartActivity(
      Context who,
      IBinder contextThread,
      IBinder token,
      String target,
      Intent intent,
      int requestCode,
      Bundle options,
      UserHandle user) {
    ActivityResult ar = stubResultFor(intent);
    if (ar != null) {
      Log.i(TAG, String.format("Stubbing intent %s", intent));
    } else {
      ar =
          super.execStartActivity(
              who, contextThread, token, target, intent, requestCode, options, user);
    }
    if (ar != null && target != null) {
      ShadowActivity shadowActivity = extract(target);
      postDispatchActivityResult(shadowActivity, null, requestCode, ar);
    }
    return null;
  }

  private void postDispatchActivityResult(
      ShadowActivity shadowActivity, String target, int requestCode, ActivityResult ar) {
    new Handler(Looper.getMainLooper())
        .post(
            new Runnable() {
              @Override
              public void run() {
                shadowActivity.internalCallDispatchActivityResult(
                    target, requestCode, ar.getResultCode(), ar.getResultData());
              }
            });
  }

  private ActivityResult stubResultFor(Intent intent) {
    if (!IntentStubberRegistry.isLoaded()) {
      return null;
    }

    FutureTask<ActivityResult> task =
        new FutureTask<ActivityResult>(
            new Callable<ActivityResult>() {
              @Override
              public ActivityResult call() throws Exception {
                return IntentStubberRegistry.getInstance().getActivityResultForIntent(intent);
              }
            });
    ShadowInstrumentation.runOnMainSyncNoIdle(task);

    try {
      return task.get();
    } catch (ExecutionException e) {
      String msg = String.format("Could not retrieve stub result for intent %s", intent);
      // Preserve original exception
      if (e.getCause() instanceof RuntimeException) {
        Log.w(TAG, msg, e);
        throw (RuntimeException) e.getCause();
      } else if (e.getCause() != null) {
        throw new RuntimeException(msg, e.getCause());
      } else {
        throw new RuntimeException(msg, e);
      }
    } catch (InterruptedException e) {
      throw new RuntimeException(e);
    }
  }

  /** {@inheritDoc} */
  @Override
  public void execStartActivities(
      Context who,
      IBinder contextThread,
      IBinder token,
      Activity target,
      Intent[] intents,
      Bundle options) {
    Log.d(TAG, "execStartActivities(context, ibinder, ibinder, activity, intent[], bundle)");
    // For requestCode < 0, the caller doesn't expect any result and
    // in this case we are not expecting any result so selecting
    // a value < 0.
    int requestCode = -1;
    for (Intent intent : intents) {
      execStartActivity(who, contextThread, token, target, intent, requestCode, options);
    }
  }

  @Override
  public boolean onException(Object obj, Throwable e) {
    String error =
        String.format(
            "Exception encountered by: %s. Dumping thread state to "
                + "outputs and pining for the fjords.",
            obj);
    Log.e(TAG, error, e);
    Log.e("THREAD_STATE", getThreadState());
    Log.e(TAG, "Dying now...");
    return super.onException(obj, e);
  }

  protected String getThreadState() {
    Set<Map.Entry<Thread, StackTraceElement[]>> threads = Thread.getAllStackTraces().entrySet();
    StringBuilder threadState = new StringBuilder();
    for (Map.Entry<Thread, StackTraceElement[]> threadAndStack : threads) {
      StringBuilder threadMessage = new StringBuilder("  ").append(threadAndStack.getKey());
      threadMessage.append("\n");
      for (StackTraceElement ste : threadAndStack.getValue()) {
        threadMessage.append(String.format("    %s%n", ste));
      }
      threadMessage.append("\n");
      threadState.append(threadMessage);
    }
    return threadState.toString();
  }

  @Override
  public void callActivityOnDestroy(Activity activity) {
    if (activity.isFinishing()) {
      createdActivities.removeIf(controller -> controller.get() == activity);
    }
    super.callActivityOnDestroy(activity);
    lifecycleMonitor.signalLifecycleChange(Stage.DESTROYED, activity);
  }

  @Override
  public void callActivityOnRestart(Activity activity) {
    super.callActivityOnRestart(activity);
    lifecycleMonitor.signalLifecycleChange(Stage.RESTARTED, activity);
  }

  @Override
  public void callActivityOnCreate(Activity activity, Bundle bundle) {
    lifecycleMonitor.signalLifecycleChange(Stage.PRE_ON_CREATE, activity);
    super.callActivityOnCreate(activity, bundle);
    lifecycleMonitor.signalLifecycleChange(Stage.CREATED, activity);
  }

  @Override
  public void callActivityOnStart(Activity activity) {
    super.callActivityOnStart(activity);
    lifecycleMonitor.signalLifecycleChange(Stage.STARTED, activity);
  }

  @Override
  public void callActivityOnStop(Activity activity) {
    super.callActivityOnStop(activity);
    lifecycleMonitor.signalLifecycleChange(Stage.STOPPED, activity);
  }

  @Override
  public void callActivityOnResume(Activity activity) {
    super.callActivityOnResume(activity);
    lifecycleMonitor.signalLifecycleChange(Stage.RESUMED, activity);
  }

  @Override
  public void callActivityOnPause(Activity activity) {
    super.callActivityOnPause(activity);
    lifecycleMonitor.signalLifecycleChange(Stage.PAUSED, activity);
  }

  @Override
  public void finish(int resultCode, Bundle bundle) {}

  @Override
  public Context getTargetContext() {
    return RuntimeEnvironment.getApplication();
  }

  @Override
  public Context getContext() {
    return RuntimeEnvironment.getApplication();
  }

  private void updateConfiguration(
      Configuration oldConfig, Configuration newConfig, DisplayMetrics newMetrics) {
    int changedConfig = oldConfig.diff(newConfig);
    List<ActivityController<?>> controllers = new ArrayList<>(createdActivities);
    for (ActivityController<?> controller : controllers) {
      if (createdActivities.contains(controller)) {
        Activity activity = controller.get();
        controller.configurationChange(newConfig, newMetrics, changedConfig);
        // If the activity is recreated then make the new activity visible, this should be done by
        // configurationChange but there's a pre-existing TODO to address this and it will require
        // more work to make it function correctly.
        if (controller.get() != activity) {
          controller.visible();
        }
      }
    }
  }
}