• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.android.fakes;
2 
3 import static org.robolectric.Shadows.shadowOf;
4 import static org.robolectric.shadow.api.Shadow.extract;
5 
6 import android.app.Activity;
7 import android.content.Context;
8 import android.content.Intent;
9 import android.content.pm.ActivityInfo;
10 import android.os.Bundle;
11 import android.os.IBinder;
12 import android.os.Looper;
13 import android.os.UserHandle;
14 import androidx.test.runner.MonitoringInstrumentation;
15 import org.robolectric.Robolectric;
16 import org.robolectric.shadows.ShadowActivity;
17 
18 public class RoboMonitoringInstrumentation extends MonitoringInstrumentation {
19 
20   @Override
specifyDexMakerCacheProperty()21   protected void specifyDexMakerCacheProperty() {
22     // ignore, unnecessary for robolectric
23   }
24 
25   @Override
installMultidex()26   protected void installMultidex() {
27     // ignore, unnecessary for robolectric
28   }
29 
30   @Override
setInTouchMode(boolean inTouch)31   public void setInTouchMode(boolean inTouch) {
32     // ignore
33   }
34 
35   @Override
waitForIdleSync()36   public void waitForIdleSync() {
37     shadowOf(Looper.getMainLooper()).idle();
38   }
39 
40   @Override
startActivitySync(final Intent intent)41   public Activity startActivitySync(final Intent intent) {
42     ActivityInfo ai = intent.resolveActivityInfo(getTargetContext().getPackageManager(), 0);
43     try {
44       Class<? extends Activity> activityClass = Class.forName(ai.name).asSubclass(Activity.class);
45       return Robolectric.buildActivity(activityClass, intent)
46           .create()
47           .postCreate(null)
48           .start()
49           .resume()
50           .postResume()
51           .visible()
52           .windowFocusChanged(true)
53           .get();
54     } catch (ClassNotFoundException e) {
55       throw new RuntimeException("Could not load activity " + ai.name, e);
56     }
57   }
58 
59   @Override
runOnMainSync(Runnable runner)60   public void runOnMainSync(Runnable runner) {
61     runner.run();
62   }
63 
64   @Override
execStartActivity( Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode)65   public ActivityResult execStartActivity(
66       Context who,
67       IBinder contextThread,
68       IBinder token,
69       Activity target,
70       Intent intent,
71       int requestCode) {
72 
73     ActivityResult ar =
74         super.execStartActivity(who, contextThread, token, target, intent, requestCode);
75     if (ar != null) {
76       ShadowActivity shadowActivity = extract(target);
77       shadowActivity.callOnActivityResult(requestCode, ar.getResultCode(), ar.getResultData());
78     }
79     return ar;
80   }
81 
82   /** {@inheritDoc} */
83   @Override
execStartActivity( Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode, Bundle options)84   public ActivityResult execStartActivity(
85       Context who,
86       IBinder contextThread,
87       IBinder token,
88       Activity target,
89       Intent intent,
90       int requestCode,
91       Bundle options) {
92     ActivityResult ar =
93         super.execStartActivity(who, contextThread, token, target, intent, requestCode, options);
94     if (ar != null) {
95       ShadowActivity shadowActivity = extract(target);
96       shadowActivity.callOnActivityResult(requestCode, ar.getResultCode(), ar.getResultData());
97     }
98     return ar;
99   }
100 
101   /** This API was added in Android API 23 (M) */
102   @Override
execStartActivity( Context who, IBinder contextThread, IBinder token, String target, Intent intent, int requestCode, Bundle options)103   public ActivityResult execStartActivity(
104       Context who,
105       IBinder contextThread,
106       IBinder token,
107       String target,
108       Intent intent,
109       int requestCode,
110       Bundle options) {
111 
112     ActivityResult ar =
113         super.execStartActivity(who, contextThread, token, target, intent, requestCode, options);
114     if (ar != null) {
115       ShadowActivity shadowActivity = extract(target);
116       shadowActivity.callOnActivityResult(requestCode, ar.getResultCode(), ar.getResultData());
117     }
118     return ar;
119   }
120 
121   /** This API was added in Android API 17 (JELLY_BEAN_MR1) */
122   @Override
execStartActivity( Context who, IBinder contextThread, IBinder token, Activity target, Intent intent, int requestCode, Bundle options, UserHandle user)123   public ActivityResult execStartActivity(
124       Context who,
125       IBinder contextThread,
126       IBinder token,
127       Activity target,
128       Intent intent,
129       int requestCode,
130       Bundle options,
131       UserHandle user) {
132     ActivityResult ar =
133         super.execStartActivity(
134             who, contextThread, token, target, intent, requestCode, options, user);
135     if (ar != null) {
136       ShadowActivity shadowActivity = extract(target);
137       shadowActivity.callOnActivityResult(requestCode, ar.getResultCode(), ar.getResultData());
138     }
139     return ar;
140   }
141 }
142