android.app.Instrumentation.ActivityMonitor Java Examples

The following examples show how to use android.app.Instrumentation.ActivityMonitor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ShareTestCase.java    From react-native-GPay with MIT License 6 votes vote down vote up
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());

}
 
Example #2
Source File: Waiter.java    From AndroidRipper with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #3
Source File: Waiter.java    From AndroidRipper with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: WelcomeScreenHelperAndroidTest.java    From welcome-android with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #5
Source File: MainActivityTest.java    From Inside_Android_Testing with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #6
Source File: TestUtils.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #7
Source File: SampleTest.java    From androidtestdebug with MIT License 5 votes vote down vote up
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);
}
 
Example #8
Source File: Waiter.java    From AndroidRipper with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Removes the AcitivityMonitor
 * 
 * @param activityMonitor the ActivityMonitor to remove
 */

private void removeMonitor(ActivityMonitor activityMonitor){
	try{
		instrumentation.removeMonitor(activityMonitor);	
	}catch (Exception ignored) {}
}
 
Example #9
Source File: Solo.java    From AndroidRipper with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * 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();
}
 
Example #10
Source File: WelcomeScreenHelperAndroidTest.java    From welcome-android with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #11
Source File: MainActivityFunctionalTest.java    From codeexamples-android with Eclipse Public License 1.0 4 votes vote down vote up
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);
}
 
Example #12
Source File: InstrumentationHook.java    From AtlasForAndroid with MIT License 4 votes vote down vote up
public void removeMonitor(ActivityMonitor activityMonitor) {
    this.mBase.removeMonitor(activityMonitor);
}
 
Example #13
Source File: InstrumentationHook.java    From AtlasForAndroid with MIT License 4 votes vote down vote up
public Activity waitForMonitorWithTimeout(ActivityMonitor activityMonitor, long j) {
    return this.mBase.waitForMonitorWithTimeout(activityMonitor, j);
}
 
Example #14
Source File: InstrumentationHook.java    From AtlasForAndroid with MIT License 4 votes vote down vote up
public Activity waitForMonitor(ActivityMonitor activityMonitor) {
    return this.mBase.waitForMonitor(activityMonitor);
}
 
Example #15
Source File: InstrumentationHook.java    From AtlasForAndroid with MIT License 4 votes vote down vote up
public boolean checkMonitorHit(ActivityMonitor activityMonitor, int i) {
    return this.mBase.checkMonitorHit(activityMonitor, i);
}
 
Example #16
Source File: InstrumentationHook.java    From AtlasForAndroid with MIT License 4 votes vote down vote up
public ActivityMonitor addMonitor(String str, ActivityResult activityResult, boolean z) {
    return this.mBase.addMonitor(str, activityResult, z);
}
 
Example #17
Source File: InstrumentationHook.java    From AtlasForAndroid with MIT License 4 votes vote down vote up
public ActivityMonitor addMonitor(IntentFilter intentFilter, ActivityResult activityResult, boolean z) {
    return this.mBase.addMonitor(intentFilter, activityResult, z);
}
 
Example #18
Source File: InstrumentationHook.java    From AtlasForAndroid with MIT License 4 votes vote down vote up
public void addMonitor(ActivityMonitor activityMonitor) {
    this.mBase.addMonitor(activityMonitor);
}
 
Example #19
Source File: Waiter.java    From AndroidRipper with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * 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;
}
 
Example #20
Source File: ActivityUtils.java    From AndroidRipper with GNU Affero General Public License v3.0 2 votes vote down vote up
/**
 * Returns the ActivityMonitor used by Robotium.
 * 
 * @return the ActivityMonitor used by Robotium
 */

public ActivityMonitor getActivityMonitor(){
	return activityMonitor;
}