下面列出了怎么用android.app.Instrumentation.ActivityMonitor的API类实例代码及写法,或者点击链接到github查看源代码。
public void testShowBasicShareDialog() {
final WritableMap content = new WritableNativeMap();
content.putString("message", "Hello, ReactNative!");
final WritableMap options = new WritableNativeMap();
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CHOOSER);
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
ActivityMonitor monitor = getInstrumentation().addMonitor(intentFilter, null, true);
getTestModule().showShareDialog(content, options);
waitForBridgeAndUIIdle();
getInstrumentation().waitForIdleSync();
assertEquals(1, monitor.getHits());
assertEquals(1, mRecordingModule.getOpened());
assertEquals(0, mRecordingModule.getErrors());
}
/**
* Waits for the given {@link Activity}.
*
* @param name the name of the {@code Activity} to wait for e.g. {@code "MyActivity"}
* @param timeout the amount of time in milliseconds to wait
* @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it does not
*
*/
public boolean waitForActivity(String name, int timeout){
if(isActivityMatching(activityUtils.getCurrentActivity(false, false), name)){
return true;
}
boolean foundActivity = false;
ActivityMonitor activityMonitor = getActivityMonitor();
long currentTime = SystemClock.uptimeMillis();
final long endTime = currentTime + timeout;
while(currentTime < endTime){
Activity currentActivity = activityMonitor.waitForActivityWithTimeout(endTime - currentTime);
if(isActivityMatching(currentActivity, name)){
foundActivity = true;
break;
}
currentTime = SystemClock.uptimeMillis();
}
removeMonitor(activityMonitor);
return foundActivity;
}
/**
* Waits for the given {@link Activity}.
*
* @param activityClass the class of the {@code Activity} to wait for
* @param timeout the amount of time in milliseconds to wait
* @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it does not
*
*/
public boolean waitForActivity(Class<? extends Activity> activityClass, int timeout){
if(isActivityMatching(activityClass, activityUtils.getCurrentActivity(false, false))){
return true;
}
boolean foundActivity = false;
ActivityMonitor activityMonitor = getActivityMonitor();
long currentTime = SystemClock.uptimeMillis();
final long endTime = currentTime + timeout;
while(currentTime < endTime){
Activity currentActivity = activityMonitor.waitForActivityWithTimeout(endTime - currentTime);
if(currentActivity != null && currentActivity.getClass().equals(activityClass)) {
foundActivity = true;
break;
}
currentTime = SystemClock.uptimeMillis();
}
removeMonitor(activityMonitor);
return foundActivity;
}
@Test
public void testShow() {
ActivityMonitor monitor = new ActivityMonitor(DefaultWelcomeActivity.class.getName(), null, false);
instrumentation.addMonitor(monitor);
String key = WelcomeUtils.getKey(DefaultWelcomeActivity.class);
WelcomeSharedPreferencesHelper.storeWelcomeCompleted(activity, key);
assertFalse(helper.show(null));
assertFalse(helper.show(new Bundle()));
WelcomeSharedPreferencesHelper.removeWelcomeCompleted(activity, key);
assertTrue(helper.show(null));
assertFalse(helper.show(null));
Activity welcomeActivity = instrumentation.waitForMonitor(monitor);
assertNotNull(welcomeActivity);
WelcomeSharedPreferencesHelper.removeWelcomeCompleted(activity, key);
Bundle state = new Bundle();
helper.onSaveInstanceState(state);
assertFalse(helper.show(state));
}
public void testLaunchActivity() {
//register next activity that need to be monitored
ActivityMonitor activityMonitor = getInstrumentation().addMonitor(OtherActivity.class.getName(), null, false);
//open current activity
final Button button = (Button)mainActivity.findViewById(R.id.bt_launch);
mainActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
button.performClick();
}
});
// let's wait to open the activity
OtherActivity otherActivity = (OtherActivity)getInstrumentation().waitForMonitor(activityMonitor);
// next activity is opened and captured.
assertNotNull(otherActivity);
otherActivity.finish();
}
@SuppressWarnings("unchecked")
static <T extends Activity> T recreateActivity(final T activity, ActivityTestRule rule)
throws Throwable {
ActivityMonitor monitor = new ActivityMonitor(
activity.getClass().getCanonicalName(), null, false);
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
instrumentation.addMonitor(monitor);
rule.runOnUiThread(activity::recreate);
T result;
// this guarantee that we will reinstall monitor between notifications about onDestroy
// and onCreate
//noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (monitor) {
do {
// the documetation says "Block until an Activity is created
// that matches this monitor." This statement is true, but there are some other
// true statements like: "Block until an Activity is destoyed" or
// "Block until an Activity is resumed"...
// this call will release synchronization monitor's monitor
result = (T) monitor.waitForActivityWithTimeout(TIMEOUT_MS);
if (result == null) {
throw new RuntimeException("Timeout. Failed to recreate an activity");
}
} while (result == activity);
}
return result;
}
/**
* Removes the AcitivityMonitor
*
* @param activityMonitor the ActivityMonitor to remove
*/
private void removeMonitor(ActivityMonitor activityMonitor){
try{
instrumentation.removeMonitor(activityMonitor);
}catch (Exception ignored) {}
}
/**
* Returns the ActivityMonitor used by Robotium.
*
* @return the ActivityMonitor used by Robotium
*/
public ActivityMonitor getActivityMonitor(){
if(config.commandLogging){
Log.d(config.commandLoggingTag, "getActivityMonitor()");
}
return activityUtils.getActivityMonitor();
}
@Test
public void testForceShow() {
ActivityMonitor monitor = new ActivityMonitor(DefaultWelcomeActivity.class.getName(), null, false);
instrumentation.addMonitor(monitor);
helper.forceShow();
Activity welcomeActivity = instrumentation.waitForMonitor(monitor);
assertNotNull(welcomeActivity);
}
public void test点击链接() {
final Instrumentation inst = getInstrumentation();
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_VIEW);
intentFilter.addDataScheme("http");
intentFilter.addCategory(Intent.CATEGORY_BROWSABLE);
View link = this.getActivity().findViewById(R.id.link);
ActivityMonitor monitor = inst.addMonitor(
intentFilter, null, false);
assertEquals(0, monitor.getHits());
TouchUtils.clickView(this, link);
monitor.waitForActivityWithTimeout(5000);
assertEquals(1, monitor.getHits());
inst.removeMonitor(monitor);
}
/**
* Creates a new ActivityMonitor and returns it
*
* @return an ActivityMonitor
*/
private ActivityMonitor getActivityMonitor(){
IntentFilter filter = null;
ActivityMonitor activityMonitor = instrumentation.addMonitor(filter, null, false);
return activityMonitor;
}
public void addMonitor(ActivityMonitor activityMonitor) {
this.mBase.addMonitor(activityMonitor);
}
public ActivityMonitor addMonitor(IntentFilter intentFilter, ActivityResult activityResult, boolean z) {
return this.mBase.addMonitor(intentFilter, activityResult, z);
}
public ActivityMonitor addMonitor(String str, ActivityResult activityResult, boolean z) {
return this.mBase.addMonitor(str, activityResult, z);
}
public boolean checkMonitorHit(ActivityMonitor activityMonitor, int i) {
return this.mBase.checkMonitorHit(activityMonitor, i);
}
public Activity waitForMonitor(ActivityMonitor activityMonitor) {
return this.mBase.waitForMonitor(activityMonitor);
}
public Activity waitForMonitorWithTimeout(ActivityMonitor activityMonitor, long j) {
return this.mBase.waitForMonitorWithTimeout(activityMonitor, j);
}
public void removeMonitor(ActivityMonitor activityMonitor) {
this.mBase.removeMonitor(activityMonitor);
}
public void testStartSecondActivity() throws Exception {
final EditText editText = (EditText) activity
.findViewById(R.id.editText1);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
editText.setText(INPUT);
}
});
// Add monitor to check for the second activity
ActivityMonitor monitor = getInstrumentation().addMonitor(
SecondActivity.class.getName(), null, false);
// Find button and click it
Button view = (Button) activity.findViewById(R.id.button1);
TouchUtils.clickView(this, view);
// To click on a click, e.g. in a listview
// listView.getChildAt(0);
// Wait 2 seconds for the start of the activity
SecondActivity startedActivity = (SecondActivity) monitor
.waitForActivityWithTimeout(2000);
assertNotNull(startedActivity);
// Search for the textView
TextView textView = (TextView) startedActivity
.findViewById(R.id.resultText);
// Check that the TextView is on the screen
ViewAsserts.assertOnScreen(startedActivity.getWindow().getDecorView(),
textView);
// Validate the text on the TextView
assertEquals("Text incorrect", INPUT, textView.getText().toString());
// Press back and click again
this.sendKeys(KeyEvent.KEYCODE_BACK);
TouchUtils.clickView(this, view);
}
/**
* Returns the ActivityMonitor used by Robotium.
*
* @return the ActivityMonitor used by Robotium
*/
public ActivityMonitor getActivityMonitor(){
return activityMonitor;
}