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]));
}
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));
}
}
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) {
}
}
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;
}
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;
}
Aggregations