类android.app.Instrumentation.ActivityMonitor源码实例Demo

下面列出了怎么用android.app.Instrumentation.ActivityMonitor的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: react-native-GPay   文件: ShareTestCase.java
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());

}
 
源代码2 项目: AndroidRipper   文件: Waiter.java
/**
 * 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;
}
 
源代码3 项目: AndroidRipper   文件: Waiter.java
/**
 * 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));
}
 
源代码5 项目: Inside_Android_Testing   文件: MainActivityTest.java
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();
}
 
源代码6 项目: android_9.0.0_r45   文件: TestUtils.java
@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;
}
 
源代码7 项目: AndroidRipper   文件: Waiter.java
/**
 * Removes the AcitivityMonitor
 * 
 * @param activityMonitor the ActivityMonitor to remove
 */

private void removeMonitor(ActivityMonitor activityMonitor){
	try{
		instrumentation.removeMonitor(activityMonitor);	
	}catch (Exception ignored) {}
}
 
源代码8 项目: AndroidRipper   文件: Solo.java
/**
 * 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);
}
 
源代码10 项目: androidtestdebug   文件: SampleTest.java
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);
}
 
源代码11 项目: AndroidRipper   文件: Waiter.java
/**
 * 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;
}
 
源代码12 项目: AtlasForAndroid   文件: InstrumentationHook.java
public void addMonitor(ActivityMonitor activityMonitor) {
    this.mBase.addMonitor(activityMonitor);
}
 
源代码13 项目: AtlasForAndroid   文件: InstrumentationHook.java
public ActivityMonitor addMonitor(IntentFilter intentFilter, ActivityResult activityResult, boolean z) {
    return this.mBase.addMonitor(intentFilter, activityResult, z);
}
 
源代码14 项目: AtlasForAndroid   文件: InstrumentationHook.java
public ActivityMonitor addMonitor(String str, ActivityResult activityResult, boolean z) {
    return this.mBase.addMonitor(str, activityResult, z);
}
 
源代码15 项目: AtlasForAndroid   文件: InstrumentationHook.java
public boolean checkMonitorHit(ActivityMonitor activityMonitor, int i) {
    return this.mBase.checkMonitorHit(activityMonitor, i);
}
 
源代码16 项目: AtlasForAndroid   文件: InstrumentationHook.java
public Activity waitForMonitor(ActivityMonitor activityMonitor) {
    return this.mBase.waitForMonitor(activityMonitor);
}
 
源代码17 项目: AtlasForAndroid   文件: InstrumentationHook.java
public Activity waitForMonitorWithTimeout(ActivityMonitor activityMonitor, long j) {
    return this.mBase.waitForMonitorWithTimeout(activityMonitor, j);
}
 
源代码18 项目: AtlasForAndroid   文件: InstrumentationHook.java
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);
}
 
源代码20 项目: AndroidRipper   文件: ActivityUtils.java
/**
 * Returns the ActivityMonitor used by Robotium.
 * 
 * @return the ActivityMonitor used by Robotium
 */

public ActivityMonitor getActivityMonitor(){
	return activityMonitor;
}
 
 类所在包
 类方法
 同包方法