Search in sources :

Example 1 with InjectEventSecurityException

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

the class UiControllerImplIntegrationTest method testInjectString.

@LargeTest
public void testInjectString() 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() && view.hasWindowFocus());
            Log.i("TEST-post", HumanReadables.describe(view));
            focusLatch.countDown();
        }
    });
    getInstrumentation().waitForIdleSync();
    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("Hello! \n&*$$$"));
                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 2 with InjectEventSecurityException

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

the class UiControllerImplIntegrationTest method testInjectEmptyString.

@LargeTest
public void testInjectEmptyString() 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);
            requestFocusSucceded.set(view.requestFocus());
            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(""));
                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 3 with InjectEventSecurityException

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

the class EventInjectorTest method testInjectKeyEvent_securityException.

@LargeTest
public void testInjectKeyEvent_securityException() {
    KeyCharacterMap keyCharacterMap = UiControllerImpl.getKeyCharacterMap();
    KeyEvent[] events = keyCharacterMap.getEvents("a".toCharArray());
    try {
        injector.injectKeyEvent(events[0]);
        fail("Should have thrown a security exception!");
    } catch (InjectEventSecurityException expected) {
    }
}
Also used : KeyEvent(android.view.KeyEvent) InjectEventSecurityException(com.google.android.apps.common.testing.ui.espresso.InjectEventSecurityException) KeyCharacterMap(android.view.KeyCharacterMap) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 4 with InjectEventSecurityException

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

the class TypeTextActionTest method testTypeTextActionPerformInjectEventSecurityException.

public void testTypeTextActionPerformInjectEventSecurityException() throws InjectEventSecurityException {
    String stringToBeTyped = "Hello!";
    typeTextAction = new TypeTextAction(stringToBeTyped);
    when(mockUiController.injectMotionEvent(isA(MotionEvent.class))).thenReturn(true);
    when(mockUiController.injectString(stringToBeTyped)).thenThrow(new InjectEventSecurityException(""));
    try {
        typeTextAction.perform(mockUiController, mockView);
        fail("Should have thrown PerformException");
    } catch (PerformException e) {
        if (!(e.getCause() instanceof InjectEventSecurityException)) {
            fail("Exception cause should be of type InjectEventSecurityException");
        }
    }
}
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 5 with InjectEventSecurityException

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

the class MotionEvents method sendMovement.

static boolean sendMovement(UiController uiController, MotionEvent downEvent, float[] coordinates) {
    checkNotNull(uiController);
    checkNotNull(downEvent);
    checkNotNull(coordinates);
    MotionEvent motionEvent = null;
    try {
        motionEvent = MotionEvent.obtain(downEvent.getDownTime(), SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, coordinates[0], coordinates[1], 0);
        boolean injectEventSucceeded = uiController.injectMotionEvent(motionEvent);
        if (!injectEventSucceeded) {
            Log.e(TAG, String.format("Injection of motion event failed (corresponding down event: %s)", downEvent.toString()));
            return false;
        }
    } catch (InjectEventSecurityException e) {
        throw new PerformException.Builder().withActionDescription(String.format("inject motion 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