Search in sources :

Example 6 with InjectEventSecurityException

use of com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException in project double-espresso by JakeWharton.

the class EventInjectorTest method testInjectMotionEvent_upEventFailure.

@LargeTest
public void testInjectMotionEvent_upEventFailure() throws InterruptedException {
    final CountDownLatch activityStarted = new CountDownLatch(1);
    ActivityLifecycleCallback callback = new ActivityLifecycleCallback() {

        @Override
        public void onActivityLifecycleChanged(Activity activity, Stage stage) {
            if (Stage.RESUMED == stage && activity instanceof SendActivity) {
                activityStarted.countDown();
            }
        }
    };
    ActivityLifecycleMonitorRegistry.getInstance().addLifecycleCallback(callback);
    try {
        getActivity();
        assertTrue(activityStarted.await(20, TimeUnit.SECONDS));
        final int[] xy = UiControllerImplIntegrationTest.getCoordinatesInMiddleOfSendButton(getActivity(), getInstrumentation());
        getInstrumentation().runOnMainSync(new Runnable() {

            @Override
            public void run() {
                MotionEvent up = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, xy[0], xy[1], 0);
                try {
                    injectEventWorked.set(injector.injectMotionEvent(up));
                } catch (InjectEventSecurityException e) {
                    Log.e(TAG, "injectEvent threw a SecurityException");
                }
                up.recycle();
                latch.countDown();
            }
        });
        latch.await(10, TimeUnit.SECONDS);
        assertFalse(injectEventWorked.get());
    } finally {
        ActivityLifecycleMonitorRegistry.getInstance().removeLifecycleCallback(callback);
    }
}
Also used : SendActivity(com.google.android.apps.common.testing.ui.testapp.SendActivity) SendActivity(com.google.android.apps.common.testing.ui.testapp.SendActivity) Activity(android.app.Activity) Stage(com.google.android.apps.common.testing.testrunner.Stage) CountDownLatch(java.util.concurrent.CountDownLatch) InjectEventSecurityException(com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException) ActivityLifecycleCallback(com.google.android.apps.common.testing.testrunner.ActivityLifecycleCallback) MotionEvent(android.view.MotionEvent) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 7 with InjectEventSecurityException

use of com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException in project double-espresso by JakeWharton.

the class UiControllerImplIntegrationTest method testInjectLargeString.

@LargeTest
public void testInjectLargeString() throws InterruptedException {
    sendActivity = getActivity();
    getInstrumentation().waitForIdleSync();
    final AtomicBoolean requestFocusSucceded = new AtomicBoolean(false);
    getInstrumentation().runOnMainSync(new Runnable() {

        @Override
        public void run() {
            final View view = sendActivity.findViewById(R.id.send_data_to_call_edit_text);
            Log.i("TEST", HumanReadables.describe(view));
            requestFocusSucceded.set(view.requestFocus());
            Log.i("TEST-post", HumanReadables.describe(view));
            focusLatch.countDown();
        }
    });
    assertTrue("requestFocus timed out!", focusLatch.await(2, TimeUnit.SECONDS));
    assertTrue("requestFocus failed.", requestFocusSucceded.get());
    getInstrumentation().runOnMainSync(new Runnable() {

        @Override
        public void run() {
            try {
                injectEventWorked.set(uiController.injectString("This is a string with 32 chars!!"));
                latch.countDown();
            } catch (InjectEventSecurityException e) {
                injectEventThrewSecurityException.set(true);
            }
        }
    });
    assertFalse("SecurityException exception was thrown.", injectEventThrewSecurityException.get());
    assertTrue("Timed out!", latch.await(20, TimeUnit.SECONDS));
    assertTrue(injectEventWorked.get());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) View(android.view.View) InjectEventSecurityException(com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 8 with InjectEventSecurityException

use of com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException in project double-espresso by JakeWharton.

the class MotionEvents method sendCancel.

static void sendCancel(UiController uiController, MotionEvent downEvent) {
    checkNotNull(uiController);
    checkNotNull(downEvent);
    MotionEvent motionEvent = null;
    try {
        // Up press.
        motionEvent = MotionEvent.obtain(downEvent.getDownTime(), SystemClock.uptimeMillis(), MotionEvent.ACTION_CANCEL, downEvent.getX(), downEvent.getY(), 0);
        boolean injectEventSucceeded = uiController.injectMotionEvent(motionEvent);
        if (!injectEventSucceeded) {
            throw new PerformException.Builder().withActionDescription(String.format("inject cancel event (corresponding down event: %s)", downEvent.toString())).withViewDescription(// likely to be replaced by FailureHandler
            "unknown").build();
        }
    } catch (InjectEventSecurityException e) {
        throw new PerformException.Builder().withActionDescription(String.format("inject cancel event (corresponding down event: %s)", downEvent.toString())).withViewDescription(// likely to be replaced by FailureHandler
        "unknown").withCause(e).build();
    } finally {
        if (null != motionEvent) {
            motionEvent.recycle();
            motionEvent = null;
        }
    }
}
Also used : PerformException(com.google.android.apps.common.testing.ui.espresso.PerformException) InjectEventSecurityException(com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException) MotionEvent(android.view.MotionEvent)

Example 9 with InjectEventSecurityException

use of com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException in project double-espresso by JakeWharton.

the class MotionEvents method sendDown.

static DownResultHolder sendDown(UiController uiController, float[] coordinates, float[] precision) {
    checkNotNull(uiController);
    checkNotNull(coordinates);
    checkNotNull(precision);
    for (int retry = 0; retry < MAX_CLICK_ATTEMPTS; retry++) {
        MotionEvent motionEvent = null;
        try {
            // Algorithm of sending click event adopted from android.test.TouchUtils.
            // When the click event was first initiated. Needs to be same for both down and up press
            // events.
            long downTime = SystemClock.uptimeMillis();
            // Down press.
            motionEvent = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, coordinates[0], coordinates[1], // pressure
            0, // size
            1, // metaState
            0, // xPrecision
            precision[0], // yPrecision
            precision[1], // deviceId
            0, // edgeFlags
            0);
            // The down event should be considered a tap if it is long enough to be detected
            // but short enough not to be a long-press. Assume that TapTimeout is set at least
            // twice the detection time for a tap (no need to sleep for the whole TapTimeout since
            // we aren't concerned about scrolling here).
            long isTapAt = downTime + (ViewConfiguration.getTapTimeout() / 2);
            boolean injectEventSucceeded = uiController.injectMotionEvent(motionEvent);
            while (true) {
                long delayToBeTap = isTapAt - SystemClock.uptimeMillis();
                if (delayToBeTap <= 10) {
                    break;
                }
                // Sleep only a fraction of the time, since there may be other events in the UI queue
                // that could cause us to start sleeping late, and then oversleep.
                uiController.loopMainThreadForAtLeast(delayToBeTap / 4);
            }
            boolean longPress = false;
            if (SystemClock.uptimeMillis() > (downTime + ViewConfiguration.getLongPressTimeout())) {
                longPress = true;
                Log.e(TAG, "Overslept and turned a tap into a long press");
                UsageTrackerRegistry.getInstance().trackUsage("Espresso.Tap.Error.tapToLongPress");
            }
            if (!injectEventSucceeded) {
                motionEvent.recycle();
                motionEvent = null;
                continue;
            }
            return new DownResultHolder(motionEvent, longPress);
        } catch (InjectEventSecurityException e) {
            throw new PerformException.Builder().withActionDescription("Send down montion event").withViewDescription(// likely to be replaced by FailureHandler
            "unknown").withCause(e).build();
        }
    }
    throw new PerformException.Builder().withActionDescription(String.format("click (after %s attempts)", MAX_CLICK_ATTEMPTS)).withViewDescription(// likely to be replaced by FailureHandler
    "unknown").build();
}
Also used : PerformException(com.google.android.apps.common.testing.ui.espresso.PerformException) InjectEventSecurityException(com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException) MotionEvent(android.view.MotionEvent)

Example 10 with InjectEventSecurityException

use of com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException in project double-espresso by JakeWharton.

the class MotionEvents method sendUp.

static boolean sendUp(UiController uiController, MotionEvent downEvent, float[] coordinates) {
    checkNotNull(uiController);
    checkNotNull(downEvent);
    checkNotNull(coordinates);
    MotionEvent motionEvent = null;
    try {
        // Up press.
        motionEvent = MotionEvent.obtain(downEvent.getDownTime(), SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, coordinates[0], coordinates[1], 0);
        boolean injectEventSucceeded = uiController.injectMotionEvent(motionEvent);
        if (!injectEventSucceeded) {
            Log.e(TAG, String.format("Injection of up event failed (corresponding down event: %s)", downEvent.toString()));
            return false;
        }
    } catch (InjectEventSecurityException e) {
        throw new PerformException.Builder().withActionDescription(String.format("inject up event (corresponding down event: %s)", downEvent.toString())).withViewDescription(// likely to be replaced by FailureHandler
        "unknown").withCause(e).build();
    } finally {
        if (null != motionEvent) {
            motionEvent.recycle();
            motionEvent = null;
        }
    }
    return true;
}
Also used : PerformException(com.google.android.apps.common.testing.ui.espresso.PerformException) InjectEventSecurityException(com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException) MotionEvent(android.view.MotionEvent)

Aggregations

InjectEventSecurityException (com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException)10 MotionEvent (android.view.MotionEvent)6 LargeTest (android.test.suitebuilder.annotation.LargeTest)5 PerformException (com.google.android.apps.common.testing.ui.espresso.PerformException)5 View (android.view.View)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 Activity (android.app.Activity)1 KeyCharacterMap (android.view.KeyCharacterMap)1 KeyEvent (android.view.KeyEvent)1 ActivityLifecycleCallback (com.google.android.apps.common.testing.testrunner.ActivityLifecycleCallback)1 Stage (com.google.android.apps.common.testing.testrunner.Stage)1 SendActivity (com.google.android.apps.common.testing.ui.testapp.SendActivity)1 CountDownLatch (java.util.concurrent.CountDownLatch)1