Search in sources :

Example 31 with KeyCharacterMap

use of android.view.KeyCharacterMap in project double-espresso by JakeWharton.

the class EventInjectorTest method testInjectKeyEventUpWithNoDown.

@LargeTest
public void testInjectKeyEventUpWithNoDown() throws Exception {
    sendActivity = getActivity();
    getInstrumentation().runOnMainSync(new Runnable() {

        @Override
        public void run() {
            View view = sendActivity.findViewById(R.id.send_data_edit_text);
            assertTrue(view.requestFocus());
            latch.countDown();
        }
    });
    assertTrue("Timed out!", latch.await(10, TimeUnit.SECONDS));
    KeyCharacterMap keyCharacterMap = UiControllerImpl.getKeyCharacterMap();
    KeyEvent[] events = keyCharacterMap.getEvents("a".toCharArray());
    assertTrue(injector.injectKeyEvent(events[1]));
}
Also used : KeyEvent(android.view.KeyEvent) View(android.view.View) KeyCharacterMap(android.view.KeyCharacterMap) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 32 with KeyCharacterMap

use of android.view.KeyCharacterMap in project double-espresso by JakeWharton.

the class EventInjectorTest method testInjectStaleKeyEvent.

@LargeTest
public void testInjectStaleKeyEvent() throws Exception {
    sendActivity = getActivity();
    getInstrumentation().runOnMainSync(new Runnable() {

        @Override
        public void run() {
            View view = sendActivity.findViewById(R.id.send_data_edit_text);
            assertTrue(view.requestFocus());
            latch.countDown();
        }
    });
    assertTrue("Timed out!", latch.await(10, TimeUnit.SECONDS));
    assertFalse("SecurityException exception was thrown.", injectEventThrewSecurityException.get());
    KeyCharacterMap keyCharacterMap = UiControllerImpl.getKeyCharacterMap();
    KeyEvent[] events = keyCharacterMap.getEvents("a".toCharArray());
    KeyEvent event = KeyEvent.changeTimeRepeat(events[0], 1, 0);
    // Stale event does not fail for API < 13.
    if (Build.VERSION.SDK_INT < 13) {
        assertTrue(injector.injectKeyEvent(event));
    } else {
        assertFalse(injector.injectKeyEvent(event));
    }
}
Also used : KeyEvent(android.view.KeyEvent) View(android.view.View) KeyCharacterMap(android.view.KeyCharacterMap) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 33 with KeyCharacterMap

use of android.view.KeyCharacterMap 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 34 with KeyCharacterMap

use of android.view.KeyCharacterMap in project robolectric by robolectric.

the class LocalUiController method injectString.

// TODO: implementation copied from espresso's UIControllerImpl. Refactor code into common
// location
@Override
public boolean injectString(String str) throws InjectEventSecurityException {
    checkNotNull(str);
    checkState(Looper.myLooper() == Looper.getMainLooper(), "Expecting to be on main thread!");
    // No-op if string is empty.
    if (str.isEmpty()) {
        Log.w(TAG, "Supplied string is empty resulting in no-op (nothing is typed).");
        return true;
    }
    boolean eventInjected = false;
    KeyCharacterMap keyCharacterMap = getKeyCharacterMap();
    // TODO: Investigate why not use (as suggested in javadoc of keyCharacterMap.getEvents):
    // http://developer.android.com/reference/android/view/KeyEvent.html#KeyEvent(long,
    // java.lang.String, int, int)
    KeyEvent[] events = keyCharacterMap.getEvents(str.toCharArray());
    if (events == null) {
        throw new RuntimeException(String.format("Failed to get key events for string %s (i.e. current IME does not understand how to" + " translate the string into key events). As a workaround, you can use" + " replaceText action to set the text directly in the EditText field.", str));
    }
    Log.d(TAG, String.format("Injecting string: \"%s\"", str));
    for (KeyEvent event : events) {
        checkNotNull(event, String.format("Failed to get event for character (%c) with key code (%s)", event.getKeyCode(), event.getUnicodeChar()));
        eventInjected = false;
        for (int attempts = 0; !eventInjected && attempts < 4; attempts++) {
            // We have to change the time of an event before injecting it because
            // all KeyEvents returned by KeyCharacterMap.getEvents() have the same
            // time stamp and the system rejects too old events. Hence, it is
            // possible for an event to become stale before it is injected if it
            // takes too long to inject the preceding ones.
            event = KeyEvent.changeTimeRepeat(event, SystemClock.uptimeMillis(), 0);
            eventInjected = injectKeyEvent(event);
        }
        if (!eventInjected) {
            Log.e(TAG, String.format("Failed to inject event for character (%c) with key code (%s)", event.getUnicodeChar(), event.getKeyCode()));
            break;
        }
    }
    return eventInjected;
}
Also used : KeyEvent(android.view.KeyEvent) KeyCharacterMap(android.view.KeyCharacterMap) SuppressLint(android.annotation.SuppressLint)

Example 35 with KeyCharacterMap

use of android.view.KeyCharacterMap in project SmartAndroidSource by jaychou2012.

the class ActionBarSherlockCompat method preparePanel.

// /////////////////////////////////////////////////////////////////////////
// Menu callback lifecycle and creation
// /////////////////////////////////////////////////////////////////////////
private boolean preparePanel() {
    // Already prepared (isPrepared will be reset to false later)
    if (mMenuIsPrepared) {
        return true;
    }
    // Init the panel state's menu--return false if init failed
    if (mMenu == null || mMenuRefreshContent) {
        if (mMenu == null) {
            if (!initializePanelMenu() || (mMenu == null)) {
                return false;
            }
        }
        if (wActionBar != null) {
            wActionBar.setMenu(mMenu, this);
        }
        // Call callback, and return if it doesn't want to display menu.
        // Creating the panel menu will involve a lot of manipulation;
        // don't dispatch change events to presenters until we're done.
        mMenu.stopDispatchingItemsChanged();
        if (!callbackCreateOptionsMenu(mMenu)) {
            // Ditch the menu created above
            mMenu = null;
            if (wActionBar != null) {
                // Don't show it in the action bar either
                wActionBar.setMenu(null, this);
            }
            return false;
        }
        mMenuRefreshContent = false;
    }
    // Callback and return if the callback does not want to show the menu
    // Preparing the panel menu can involve a lot of manipulation;
    // don't dispatch change events to presenters until we're done.
    mMenu.stopDispatchingItemsChanged();
    // an opportunity to override frozen/restored state in onPrepare.
    if (mMenuFrozenActionViewState != null) {
        mMenu.restoreActionViewStates(mMenuFrozenActionViewState);
        mMenuFrozenActionViewState = null;
    }
    if (!callbackPrepareOptionsMenu(mMenu)) {
        if (wActionBar != null) {
            // The app didn't want to show the menu for now but it still
            // exists.
            // Clear it out of the action bar.
            wActionBar.setMenu(null, this);
        }
        mMenu.startDispatchingItemsChanged();
        return false;
    }
    // Set the proper keymap
    KeyCharacterMap kmap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
    mMenu.setQwertyMode(kmap.getKeyboardType() != KeyCharacterMap.NUMERIC);
    mMenu.startDispatchingItemsChanged();
    // Set other state
    mMenuIsPrepared = true;
    return true;
}
Also used : KeyCharacterMap(android.view.KeyCharacterMap)

Aggregations

KeyCharacterMap (android.view.KeyCharacterMap)71 KeyEvent (android.view.KeyEvent)39 Instrumentation (android.app.Instrumentation)6 ActivityNotFoundException (android.content.ActivityNotFoundException)5 Intent (android.content.Intent)5 RemoteException (android.os.RemoteException)5 IWindowManager (android.view.IWindowManager)5 FallbackAction (android.view.KeyCharacterMap.FallbackAction)5 Menu (android.view.Menu)5 WindowManager (android.view.WindowManager)5 LayoutParams (android.view.WindowManager.LayoutParams)4 IStatusBarService (com.android.internal.statusbar.IStatusBarService)4 IDeviceIdleController (android.os.IDeviceIdleController)3 RecognizerIntent (android.speech.RecognizerIntent)3 LargeTest (android.test.suitebuilder.annotation.LargeTest)3 IShortcutService (com.android.internal.policy.IShortcutService)3 SuppressLint (android.annotation.SuppressLint)2 ContentResolver (android.content.ContentResolver)2 View (android.view.View)2 ITelephony (com.android.internal.telephony.ITelephony)2