1 /* 2 * Copyright (C) 2008 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.view.cts; 18 19 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.os.SystemClock; 23 import android.test.AndroidTestCase; 24 import android.text.method.MetaKeyKeyListener; 25 import android.view.KeyEvent; 26 import android.view.KeyCharacterMap.KeyData; 27 import android.view.KeyEvent.Callback; 28 29 import junit.framework.Assert; 30 31 /** 32 * Test {@link KeyEvent}. 33 */ 34 public class KeyEventTest extends AndroidTestCase { 35 private KeyEvent mKeyEvent; 36 private long mDownTime; 37 private long mEventTime; 38 39 @Override setUp()40 protected void setUp() throws Exception { 41 super.setUp(); 42 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 43 44 mDownTime = SystemClock.uptimeMillis(); 45 mEventTime = SystemClock.uptimeMillis(); 46 } 47 testConstructor()48 public void testConstructor() { 49 new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 50 51 new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0, 5); 52 53 new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0, 5, 54 KeyEvent.META_SHIFT_ON); 55 56 new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0, 5, 57 KeyEvent.META_SHIFT_ON, 1, 1); 58 59 new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0, 5, 60 KeyEvent.META_SHIFT_ON, 1, 1, KeyEvent.FLAG_SOFT_KEYBOARD); 61 62 KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 63 new KeyEvent(keyEvent); 64 new KeyEvent(keyEvent, mEventTime, 1); 65 66 new KeyEvent(mDownTime, "test", 0, KeyEvent.FLAG_SOFT_KEYBOARD); 67 } 68 testGetCharacters()69 public void testGetCharacters() { 70 String characters = "android_test"; 71 mKeyEvent = new KeyEvent(mDownTime, characters, 0, KeyEvent.FLAG_SOFT_KEYBOARD); 72 assertEquals(KeyEvent.ACTION_MULTIPLE, mKeyEvent.getAction()); 73 assertEquals(KeyEvent.KEYCODE_UNKNOWN, mKeyEvent.getKeyCode()); 74 assertEquals(characters, mKeyEvent.getCharacters()); 75 76 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 77 assertNull(mKeyEvent.getCharacters()); 78 } 79 testGetMaxKeyCode()80 public void testGetMaxKeyCode() { 81 assertTrue(KeyEvent.getMaxKeyCode() > 0); 82 } 83 testIsShiftPressed()84 public void testIsShiftPressed() { 85 assertFalse(mKeyEvent.isShiftPressed()); 86 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0, 5, 87 KeyEvent.META_SHIFT_ON); 88 assertTrue(mKeyEvent.isShiftPressed()); 89 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0, 5, 90 KeyEvent.META_ALT_ON); 91 assertFalse(mKeyEvent.isShiftPressed()); 92 } 93 testGetDeadChar()94 public void testGetDeadChar() { 95 // decimal number of è is 232. 96 assertEquals(232, KeyEvent.getDeadChar('`', 'e')); 97 } 98 testGetKeyData()99 public void testGetKeyData() { 100 KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_Z); 101 KeyData keyData = new KeyData(); 102 assertTrue(keyEvent.getKeyData(keyData)); 103 104 assertEquals('Z', keyData.displayLabel); 105 assertEquals(0, keyData.number); 106 assertEquals('z', keyData.meta[0]); 107 assertEquals('Z', keyData.meta[1]); 108 assertEquals(0, keyData.meta[3]); 109 } 110 testDispatch()111 public void testDispatch() { 112 MockCallback callback = new MockCallback(); 113 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 114 callback.reset(); 115 assertFalse(callback.isKeyDown()); 116 assertTrue(mKeyEvent.dispatch(callback)); 117 assertTrue(callback.isKeyDown()); 118 assertEquals(KeyEvent.KEYCODE_0, callback.getKeyCode()); 119 assertSame(mKeyEvent, callback.getKeyEvent()); 120 121 mKeyEvent = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_0); 122 callback.reset(); 123 assertFalse(callback.isKeyUp()); 124 assertTrue(mKeyEvent.dispatch(callback)); 125 assertTrue(callback.isKeyUp()); 126 assertEquals(KeyEvent.KEYCODE_0, callback.getKeyCode()); 127 assertSame(mKeyEvent, callback.getKeyEvent()); 128 129 callback.reset(); 130 int count = 2; 131 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_MULTIPLE, 132 KeyEvent.KEYCODE_0, count); 133 assertFalse(callback.isKeyMultiple()); 134 assertTrue(mKeyEvent.dispatch(callback)); 135 assertTrue(callback.isKeyMultiple()); 136 assertEquals(KeyEvent.KEYCODE_0, callback.getKeyCode()); 137 assertSame(mKeyEvent, callback.getKeyEvent()); 138 assertEquals(count, callback.getCount()); 139 140 callback.reset(); 141 count = 0; 142 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_MULTIPLE, 143 KeyEvent.KEYCODE_0, count); 144 assertTrue(mKeyEvent.dispatch(callback)); 145 assertTrue(callback.isKeyMultiple()); 146 assertTrue(callback.isKeyDown()); 147 assertTrue(callback.isKeyUp()); 148 assertEquals(count, callback.getCount()); 149 assertEquals(KeyEvent.KEYCODE_0, callback.getKeyCode()); 150 } 151 testGetMetaState()152 public void testGetMetaState() { 153 int metaState = KeyEvent.META_ALT_ON; 154 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_MULTIPLE, 155 KeyEvent.KEYCODE_1, 1, metaState); 156 assertEquals(metaState, mKeyEvent.getMetaState()); 157 } 158 testGetEventTime()159 public void testGetEventTime() { 160 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, 161 KeyEvent.KEYCODE_0, 5); 162 assertEquals(mEventTime, mKeyEvent.getEventTime()); 163 } 164 testGetDownTime()165 public void testGetDownTime() { 166 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, 167 KeyEvent.KEYCODE_0, 5); 168 assertEquals(mDownTime, mKeyEvent.getDownTime()); 169 } 170 testGetUnicodeChar1()171 public void testGetUnicodeChar1() { 172 // 48 is Unicode character of '0' 173 assertEquals(48, mKeyEvent.getUnicodeChar()); 174 175 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, 176 KeyEvent.KEYCODE_9, 5, 0); 177 // 57 is Unicode character of '9' 178 assertEquals(57, mKeyEvent.getUnicodeChar()); 179 180 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, 181 KeyEvent.KEYCODE_ALT_LEFT, 5, KeyEvent.META_SHIFT_ON); 182 // 'ALT' key is not a type Unicode character. 183 assertEquals(0, mKeyEvent.getUnicodeChar()); 184 } 185 testGetUnicodeChar2()186 public void testGetUnicodeChar2() { 187 // 48 is Unicode character of '0' 188 assertEquals(48, mKeyEvent.getUnicodeChar(MetaKeyKeyListener.META_CAP_LOCKED)); 189 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, 190 KeyEvent.KEYCODE_9, 5, 0); 191 192 // 57 is Unicode character of '9' 193 assertEquals(57, mKeyEvent.getUnicodeChar(0)); 194 195 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, 196 KeyEvent.KEYCODE_ALT_LEFT, 5, KeyEvent.META_SHIFT_ON); 197 // 'ALT' key is not a type Unicode character. 198 assertEquals(0, mKeyEvent.getUnicodeChar(0)); 199 } 200 testGetNumber()201 public void testGetNumber() { 202 // 48 is associated with key '0' 203 assertEquals(48, mKeyEvent.getNumber()); 204 205 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_3); 206 // 51 is associated with key '3' 207 assertEquals(51, mKeyEvent.getNumber()); 208 } 209 testIsSymPressed()210 public void testIsSymPressed() { 211 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0, 5, 212 KeyEvent.META_SYM_ON); 213 assertTrue(mKeyEvent.isSymPressed()); 214 215 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0, 5, 216 KeyEvent.META_SHIFT_ON); 217 assertFalse(mKeyEvent.isSymPressed()); 218 } 219 testGetDeviceId()220 public void testGetDeviceId() { 221 int deviceId = 1; 222 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0, 5, 223 KeyEvent.META_SHIFT_ON, deviceId, 1); 224 assertEquals(deviceId, mKeyEvent.getDeviceId()); 225 } 226 testToString()227 public void testToString() { 228 // make sure it does not throw any exception. 229 mKeyEvent.toString(); 230 } 231 testIsAltPressed()232 public void testIsAltPressed() { 233 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0, 5, 234 KeyEvent.META_ALT_ON); 235 assertTrue(mKeyEvent.isAltPressed()); 236 237 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0, 5, 238 KeyEvent.META_SHIFT_ON); 239 assertFalse(mKeyEvent.isAltPressed()); 240 } 241 testGetModifierMetaStateMask()242 public void testGetModifierMetaStateMask() { 243 int mask = KeyEvent.getModifierMetaStateMask(); 244 assertTrue((mask & KeyEvent.META_SHIFT_ON) != 0); 245 assertTrue((mask & KeyEvent.META_SHIFT_LEFT_ON) != 0); 246 assertTrue((mask & KeyEvent.META_SHIFT_RIGHT_ON) != 0); 247 assertTrue((mask & KeyEvent.META_ALT_ON) != 0); 248 assertTrue((mask & KeyEvent.META_ALT_LEFT_ON) != 0); 249 assertTrue((mask & KeyEvent.META_ALT_RIGHT_ON) != 0); 250 assertTrue((mask & KeyEvent.META_CTRL_ON) != 0); 251 assertTrue((mask & KeyEvent.META_CTRL_LEFT_ON) != 0); 252 assertTrue((mask & KeyEvent.META_CTRL_RIGHT_ON) != 0); 253 assertTrue((mask & KeyEvent.META_META_ON) != 0); 254 assertTrue((mask & KeyEvent.META_META_LEFT_ON) != 0); 255 assertTrue((mask & KeyEvent.META_META_RIGHT_ON) != 0); 256 assertTrue((mask & KeyEvent.META_SYM_ON) != 0); 257 assertTrue((mask & KeyEvent.META_FUNCTION_ON) != 0); 258 259 assertFalse((mask & KeyEvent.META_CAPS_LOCK_ON) != 0); 260 assertFalse((mask & KeyEvent.META_NUM_LOCK_ON) != 0); 261 assertFalse((mask & KeyEvent.META_SCROLL_LOCK_ON) != 0); 262 } 263 testIsModifierKey()264 public void testIsModifierKey() { 265 assertTrue(KeyEvent.isModifierKey(KeyEvent.KEYCODE_SHIFT_LEFT)); 266 assertTrue(KeyEvent.isModifierKey(KeyEvent.KEYCODE_SHIFT_RIGHT)); 267 assertTrue(KeyEvent.isModifierKey(KeyEvent.KEYCODE_ALT_LEFT)); 268 assertTrue(KeyEvent.isModifierKey(KeyEvent.KEYCODE_ALT_RIGHT)); 269 assertTrue(KeyEvent.isModifierKey(KeyEvent.KEYCODE_CTRL_LEFT)); 270 assertTrue(KeyEvent.isModifierKey(KeyEvent.KEYCODE_CTRL_RIGHT)); 271 assertTrue(KeyEvent.isModifierKey(KeyEvent.KEYCODE_META_LEFT)); 272 assertTrue(KeyEvent.isModifierKey(KeyEvent.KEYCODE_META_RIGHT)); 273 assertTrue(KeyEvent.isModifierKey(KeyEvent.KEYCODE_SYM)); 274 assertTrue(KeyEvent.isModifierKey(KeyEvent.KEYCODE_NUM)); 275 assertTrue(KeyEvent.isModifierKey(KeyEvent.KEYCODE_FUNCTION)); 276 277 assertFalse(KeyEvent.isModifierKey(KeyEvent.KEYCODE_0)); 278 } 279 280 private static final int UNDEFINED_META_STATE = 0x80000000; 281 testNormalizeMetaState()282 public void testNormalizeMetaState() { 283 // Already normalized values. 284 assertEquals(0, KeyEvent.normalizeMetaState(0)); 285 assertEquals(KeyEvent.getModifierMetaStateMask(), 286 KeyEvent.normalizeMetaState(KeyEvent.getModifierMetaStateMask())); 287 288 // Values that require normalization. 289 assertEquals(KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_ON, 290 KeyEvent.normalizeMetaState(KeyEvent.META_SHIFT_LEFT_ON)); 291 assertEquals(KeyEvent.META_SHIFT_RIGHT_ON | KeyEvent.META_SHIFT_ON, 292 KeyEvent.normalizeMetaState(KeyEvent.META_SHIFT_RIGHT_ON)); 293 assertEquals(KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_ALT_ON, 294 KeyEvent.normalizeMetaState(KeyEvent.META_ALT_LEFT_ON)); 295 assertEquals(KeyEvent.META_ALT_RIGHT_ON | KeyEvent.META_ALT_ON, 296 KeyEvent.normalizeMetaState(KeyEvent.META_ALT_RIGHT_ON)); 297 assertEquals(KeyEvent.META_CTRL_LEFT_ON | KeyEvent.META_CTRL_ON, 298 KeyEvent.normalizeMetaState(KeyEvent.META_CTRL_LEFT_ON)); 299 assertEquals(KeyEvent.META_CTRL_RIGHT_ON | KeyEvent.META_CTRL_ON, 300 KeyEvent.normalizeMetaState(KeyEvent.META_CTRL_RIGHT_ON)); 301 assertEquals(KeyEvent.META_META_LEFT_ON | KeyEvent.META_META_ON, 302 KeyEvent.normalizeMetaState(KeyEvent.META_META_LEFT_ON)); 303 assertEquals(KeyEvent.META_META_RIGHT_ON | KeyEvent.META_META_ON, 304 KeyEvent.normalizeMetaState(KeyEvent.META_META_RIGHT_ON)); 305 assertEquals(KeyEvent.META_CAPS_LOCK_ON, 306 KeyEvent.normalizeMetaState(MetaKeyKeyListener.META_CAP_LOCKED)); 307 assertEquals(KeyEvent.META_ALT_ON, 308 KeyEvent.normalizeMetaState(MetaKeyKeyListener.META_ALT_LOCKED)); 309 assertEquals(KeyEvent.META_SYM_ON, 310 KeyEvent.normalizeMetaState(MetaKeyKeyListener.META_SYM_LOCKED)); 311 assertEquals(KeyEvent.META_SHIFT_ON, 312 KeyEvent.normalizeMetaState(KeyEvent.META_SHIFT_ON | UNDEFINED_META_STATE)); 313 } 314 testMetaStateHasNoModifiers()315 public void testMetaStateHasNoModifiers() { 316 assertTrue(KeyEvent.metaStateHasNoModifiers(0)); 317 assertTrue(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_CAPS_LOCK_ON)); 318 assertTrue(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_NUM_LOCK_ON)); 319 assertTrue(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_SCROLL_LOCK_ON)); 320 321 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_SHIFT_ON)); 322 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_SHIFT_LEFT_ON)); 323 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_SHIFT_RIGHT_ON)); 324 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_ALT_ON)); 325 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_ALT_LEFT_ON)); 326 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_ALT_RIGHT_ON)); 327 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_CTRL_ON)); 328 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_CTRL_LEFT_ON)); 329 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_CTRL_RIGHT_ON)); 330 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_META_ON)); 331 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_META_LEFT_ON)); 332 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_META_RIGHT_ON)); 333 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_SYM_ON)); 334 assertFalse(KeyEvent.metaStateHasNoModifiers(KeyEvent.META_FUNCTION_ON)); 335 } 336 testMetaStateHasModifiers()337 public void testMetaStateHasModifiers() { 338 assertTrue(KeyEvent.metaStateHasModifiers(0, 0)); 339 assertTrue(KeyEvent.metaStateHasModifiers( 340 KeyEvent.META_NUM_LOCK_ON | KeyEvent.META_CAPS_LOCK_ON 341 | KeyEvent.META_SCROLL_LOCK_ON, 0)); 342 assertTrue(KeyEvent.metaStateHasModifiers( 343 KeyEvent.META_SHIFT_ON | KeyEvent.META_SHIFT_LEFT_ON, 344 KeyEvent.META_SHIFT_LEFT_ON)); 345 assertTrue(KeyEvent.metaStateHasModifiers( 346 KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON, 347 KeyEvent.META_SHIFT_LEFT_ON | KeyEvent.META_SHIFT_RIGHT_ON)); 348 assertTrue(KeyEvent.metaStateHasModifiers( 349 KeyEvent.META_SHIFT_LEFT_ON, 350 KeyEvent.META_SHIFT_LEFT_ON)); 351 assertTrue(KeyEvent.metaStateHasModifiers( 352 KeyEvent.META_NUM_LOCK_ON | KeyEvent.META_CAPS_LOCK_ON 353 | KeyEvent.META_SCROLL_LOCK_ON | KeyEvent.META_SHIFT_LEFT_ON, 354 KeyEvent.META_SHIFT_LEFT_ON)); 355 assertTrue(KeyEvent.metaStateHasModifiers( 356 KeyEvent.META_SHIFT_ON | KeyEvent.META_SHIFT_LEFT_ON, 357 KeyEvent.META_SHIFT_ON)); 358 assertTrue(KeyEvent.metaStateHasModifiers( 359 KeyEvent.META_ALT_ON | KeyEvent.META_ALT_RIGHT_ON, 360 KeyEvent.META_ALT_ON)); 361 assertTrue(KeyEvent.metaStateHasModifiers( 362 KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_SHIFT_LEFT_ON, 363 KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_ON)); 364 assertTrue(KeyEvent.metaStateHasModifiers( 365 KeyEvent.META_CTRL_RIGHT_ON | KeyEvent.META_META_LEFT_ON, 366 KeyEvent.META_CTRL_RIGHT_ON | KeyEvent.META_META_ON)); 367 assertTrue(KeyEvent.metaStateHasModifiers( 368 KeyEvent.META_SYM_ON | KeyEvent.META_FUNCTION_ON | KeyEvent.META_CAPS_LOCK_ON, 369 KeyEvent.META_SYM_ON | KeyEvent.META_FUNCTION_ON)); 370 371 assertFalse(KeyEvent.metaStateHasModifiers(0, KeyEvent.META_SHIFT_ON)); 372 assertFalse(KeyEvent.metaStateHasModifiers( 373 KeyEvent.META_ALT_ON | KeyEvent.META_SHIFT_LEFT_ON, 374 KeyEvent.META_SHIFT_ON)); 375 assertFalse(KeyEvent.metaStateHasModifiers( 376 KeyEvent.META_ALT_LEFT_ON | KeyEvent.META_SHIFT_LEFT_ON, 377 KeyEvent.META_SHIFT_ON)); 378 assertFalse(KeyEvent.metaStateHasModifiers( 379 KeyEvent.META_ALT_LEFT_ON, 380 KeyEvent.META_ALT_RIGHT_ON)); 381 assertFalse(KeyEvent.metaStateHasModifiers( 382 KeyEvent.META_ALT_LEFT_ON, 383 KeyEvent.META_CTRL_LEFT_ON)); 384 385 final int[] invalidModifiers = new int[] { 386 KeyEvent.META_CAPS_LOCK_ON, 387 KeyEvent.META_NUM_LOCK_ON, 388 KeyEvent.META_SCROLL_LOCK_ON, 389 MetaKeyKeyListener.META_CAP_LOCKED, 390 MetaKeyKeyListener.META_ALT_LOCKED, 391 MetaKeyKeyListener.META_SYM_LOCKED, 392 KeyEvent.META_SHIFT_ON | KeyEvent.META_SHIFT_LEFT_ON, 393 KeyEvent.META_SHIFT_ON | KeyEvent.META_SHIFT_RIGHT_ON, 394 KeyEvent.META_SHIFT_ON | KeyEvent.META_SHIFT_LEFT_ON| KeyEvent.META_SHIFT_RIGHT_ON, 395 KeyEvent.META_ALT_ON | KeyEvent.META_ALT_LEFT_ON, 396 KeyEvent.META_ALT_ON | KeyEvent.META_ALT_RIGHT_ON, 397 KeyEvent.META_ALT_ON | KeyEvent.META_ALT_LEFT_ON| KeyEvent.META_ALT_RIGHT_ON, 398 KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON, 399 KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_RIGHT_ON, 400 KeyEvent.META_CTRL_ON | KeyEvent.META_CTRL_LEFT_ON| KeyEvent.META_CTRL_RIGHT_ON, 401 KeyEvent.META_META_ON | KeyEvent.META_META_LEFT_ON, 402 KeyEvent.META_META_ON | KeyEvent.META_META_RIGHT_ON, 403 KeyEvent.META_META_ON | KeyEvent.META_META_LEFT_ON| KeyEvent.META_META_RIGHT_ON, 404 }; 405 for (int modifiers : invalidModifiers) { 406 try { 407 KeyEvent.metaStateHasModifiers(0, modifiers); 408 Assert.fail("Expected IllegalArgumentException"); 409 } catch (IllegalArgumentException ex) { 410 } 411 } 412 413 assertFalse(KeyEvent.metaStateHasModifiers(0, UNDEFINED_META_STATE)); 414 } 415 testHasNoModifiers()416 public void testHasNoModifiers() { 417 KeyEvent ev = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, 418 KeyEvent.KEYCODE_A, 0, KeyEvent.META_CAPS_LOCK_ON); 419 assertTrue(ev.hasNoModifiers()); 420 421 ev = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, 422 KeyEvent.KEYCODE_A, 0, KeyEvent.META_CAPS_LOCK_ON | KeyEvent.META_SHIFT_ON); 423 assertFalse(ev.hasNoModifiers()); 424 } 425 testHasModifiers()426 public void testHasModifiers() { 427 KeyEvent ev = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, 428 KeyEvent.KEYCODE_A, 0, KeyEvent.META_CAPS_LOCK_ON); 429 assertTrue(ev.hasModifiers(0)); 430 431 ev = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, 432 KeyEvent.KEYCODE_A, 0, KeyEvent.META_CAPS_LOCK_ON | KeyEvent.META_SHIFT_ON); 433 assertTrue(ev.hasModifiers(KeyEvent.META_SHIFT_ON)); 434 435 ev = new KeyEvent(0, 0, KeyEvent.ACTION_DOWN, 436 KeyEvent.KEYCODE_A, 0, 437 KeyEvent.META_CAPS_LOCK_ON | KeyEvent.META_SHIFT_ON | KeyEvent.META_SHIFT_RIGHT_ON); 438 assertFalse(ev.hasModifiers(KeyEvent.META_SHIFT_LEFT_ON)); 439 } 440 testGetDisplayLabel()441 public void testGetDisplayLabel() { 442 assertTrue(mKeyEvent.getDisplayLabel() > 0); 443 } 444 testIsSystem()445 public void testIsSystem() { 446 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_MENU); 447 assertTrue(mKeyEvent.isSystem()); 448 449 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SOFT_RIGHT); 450 assertTrue(mKeyEvent.isSystem()); 451 452 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HOME); 453 assertTrue(mKeyEvent.isSystem()); 454 455 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK); 456 assertTrue(mKeyEvent.isSystem()); 457 458 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_CALL); 459 assertTrue(mKeyEvent.isSystem()); 460 461 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENDCALL); 462 assertTrue(mKeyEvent.isSystem()); 463 464 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_VOLUME_UP); 465 assertTrue(mKeyEvent.isSystem()); 466 467 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_VOLUME_DOWN); 468 assertTrue(mKeyEvent.isSystem()); 469 470 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER); 471 assertTrue(mKeyEvent.isSystem()); 472 473 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SEARCH); 474 assertTrue(mKeyEvent.isSystem()); 475 476 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK); 477 assertTrue(mKeyEvent.isSystem()); 478 479 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_CAMERA); 480 assertTrue(mKeyEvent.isSystem()); 481 482 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_FOCUS); 483 assertTrue(mKeyEvent.isSystem()); 484 485 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 486 assertFalse(mKeyEvent.isSystem()); 487 } 488 testIsPrintingKey()489 public void testIsPrintingKey() { 490 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, Character.SPACE_SEPARATOR); 491 assertTrue(mKeyEvent.isPrintingKey()); 492 493 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, Character.LINE_SEPARATOR); 494 assertTrue(mKeyEvent.isPrintingKey()); 495 496 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, Character.PARAGRAPH_SEPARATOR); 497 assertTrue(mKeyEvent.isPrintingKey()); 498 499 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, Character.CONTROL); 500 assertTrue(mKeyEvent.isPrintingKey()); 501 502 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, Character.FORMAT); 503 assertTrue(mKeyEvent.isPrintingKey()); 504 505 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_0); 506 assertTrue(mKeyEvent.isPrintingKey()); 507 } 508 testGetMatch1()509 public void testGetMatch1() { 510 char[] codes1 = new char[] { '0', '1', '2' }; 511 assertEquals('0', mKeyEvent.getMatch(codes1)); 512 513 char[] codes2 = new char[] { 'A', 'B', 'C' }; 514 assertEquals('\0', mKeyEvent.getMatch(codes2)); 515 516 char[] codes3 = { '2', 'S' }; 517 mKeyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_S); 518 assertEquals('S', mKeyEvent.getMatch(codes3)); 519 } 520 testGetAction()521 public void testGetAction() { 522 assertEquals(KeyEvent.ACTION_DOWN, mKeyEvent.getAction()); 523 } 524 testGetRepeatCount()525 public void testGetRepeatCount() { 526 int repeatCount = 1; 527 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_MULTIPLE, 528 KeyEvent.KEYCODE_0, repeatCount); 529 assertEquals(repeatCount, mKeyEvent.getRepeatCount()); 530 } 531 testWriteToParcel()532 public void testWriteToParcel() { 533 Parcel parcel = Parcel.obtain(); 534 mKeyEvent.writeToParcel(parcel, Parcelable.PARCELABLE_WRITE_RETURN_VALUE); 535 parcel.setDataPosition(0); 536 537 KeyEvent keyEvent = KeyEvent.CREATOR.createFromParcel(parcel); 538 parcel.recycle(); 539 540 assertEquals(mKeyEvent.getAction(), keyEvent.getAction()); 541 assertEquals(mKeyEvent.getKeyCode(), keyEvent.getKeyCode()); 542 assertEquals(mKeyEvent.getRepeatCount(), keyEvent.getRepeatCount()); 543 assertEquals(mKeyEvent.getMetaState(), keyEvent.getMetaState()); 544 assertEquals(mKeyEvent.getDeviceId(), keyEvent.getDeviceId()); 545 assertEquals(mKeyEvent.getScanCode(), keyEvent.getScanCode()); 546 assertEquals(mKeyEvent.getFlags(), keyEvent.getFlags()); 547 assertEquals(mKeyEvent.getDownTime(), keyEvent.getDownTime()); 548 assertEquals(mKeyEvent.getEventTime(), keyEvent.getEventTime()); 549 } 550 testDescribeContents()551 public void testDescribeContents() { 552 // make sure it never shrow any exception. 553 mKeyEvent.describeContents(); 554 } 555 testGetKeyCode()556 public void testGetKeyCode() { 557 assertEquals(KeyEvent.KEYCODE_0, mKeyEvent.getKeyCode()); 558 } 559 testGetFlags()560 public void testGetFlags() { 561 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, 562 KeyEvent.KEYCODE_0, 5, KeyEvent.META_SHIFT_ON, 1, 1, KeyEvent.FLAG_WOKE_HERE); 563 assertEquals(KeyEvent.FLAG_WOKE_HERE, mKeyEvent.getFlags()); 564 565 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, 566 KeyEvent.KEYCODE_0, 5, KeyEvent.META_SHIFT_ON, 1, 1, KeyEvent.FLAG_SOFT_KEYBOARD); 567 assertEquals(KeyEvent.FLAG_SOFT_KEYBOARD, mKeyEvent.getFlags()); 568 } 569 testGetScanCode()570 public void testGetScanCode() { 571 int scanCode = 1; 572 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, 573 KeyEvent.KEYCODE_0, 5, KeyEvent.META_SHIFT_ON, 1, scanCode); 574 assertEquals(scanCode, mKeyEvent.getScanCode()); 575 } 576 testChangeAction()577 public void testChangeAction() { 578 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, 579 KeyEvent.KEYCODE_0, 5, KeyEvent.META_SHIFT_ON, 1, 1, KeyEvent.FLAG_WOKE_HERE); 580 581 KeyEvent newEvent = KeyEvent.changeAction(mKeyEvent, KeyEvent.ACTION_UP); 582 assertEquals(KeyEvent.ACTION_UP, newEvent.getAction()); 583 assertEquals(mKeyEvent.getFlags(), newEvent.getFlags()); 584 assertEquals(mKeyEvent.getCharacters(), newEvent.getCharacters()); 585 assertEquals(mKeyEvent.getDisplayLabel(), newEvent.getDisplayLabel()); 586 assertEquals(mKeyEvent.getDeviceId(), newEvent.getDeviceId()); 587 assertEquals(mKeyEvent.getDownTime(), newEvent.getDownTime()); 588 assertEquals(mKeyEvent.getEventTime(), newEvent.getEventTime()); 589 assertEquals(mKeyEvent.getKeyCode(), newEvent.getKeyCode()); 590 assertEquals(mKeyEvent.getRepeatCount(), newEvent.getRepeatCount()); 591 } 592 testChangeFlags()593 public void testChangeFlags() { 594 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, 595 KeyEvent.KEYCODE_0, 5, KeyEvent.META_SHIFT_ON, 1, 1, KeyEvent.FLAG_WOKE_HERE); 596 597 KeyEvent newEvent = KeyEvent.changeFlags(mKeyEvent, KeyEvent.FLAG_FROM_SYSTEM); 598 assertEquals(KeyEvent.FLAG_FROM_SYSTEM, newEvent.getFlags()); 599 assertEquals(mKeyEvent.getAction(), newEvent.getAction()); 600 assertEquals(mKeyEvent.getCharacters(), newEvent.getCharacters()); 601 assertEquals(mKeyEvent.getDisplayLabel(), newEvent.getDisplayLabel()); 602 assertEquals(mKeyEvent.getDeviceId(), newEvent.getDeviceId()); 603 assertEquals(mKeyEvent.getDownTime(), newEvent.getDownTime()); 604 assertEquals(mKeyEvent.getEventTime(), newEvent.getEventTime()); 605 assertEquals(mKeyEvent.getKeyCode(), newEvent.getKeyCode()); 606 assertEquals(mKeyEvent.getRepeatCount(), newEvent.getRepeatCount()); 607 } 608 testChangeTimeRepeat()609 public void testChangeTimeRepeat() { 610 mKeyEvent = new KeyEvent(mDownTime, mEventTime, KeyEvent.ACTION_DOWN, 611 KeyEvent.KEYCODE_0, 5, KeyEvent.META_SHIFT_ON, 1, 1, KeyEvent.FLAG_WOKE_HERE); 612 613 long newEventTime = SystemClock.uptimeMillis(); 614 int newRepeat = mKeyEvent.getRepeatCount() + 1; 615 KeyEvent newEvent = KeyEvent.changeTimeRepeat(mKeyEvent, newEventTime, newRepeat); 616 assertEquals(newEventTime, newEvent.getEventTime()); 617 assertEquals(newRepeat, newEvent.getRepeatCount()); 618 assertEquals(mKeyEvent.getFlags(), newEvent.getFlags()); 619 assertEquals(mKeyEvent.getAction(), newEvent.getAction()); 620 assertEquals(mKeyEvent.getCharacters(), newEvent.getCharacters()); 621 assertEquals(mKeyEvent.getDisplayLabel(), newEvent.getDisplayLabel()); 622 assertEquals(mKeyEvent.getDeviceId(), newEvent.getDeviceId()); 623 assertEquals(mKeyEvent.getDownTime(), newEvent.getDownTime()); 624 assertEquals(mKeyEvent.getKeyCode(), newEvent.getKeyCode()); 625 } 626 627 private class MockCallback implements Callback { 628 private boolean mIsKeyDown; 629 private boolean mIsKeyUp; 630 private boolean mIsMultiple; 631 private int mKeyCode; 632 private KeyEvent mKeyEvent; 633 private int mCount; 634 isKeyDown()635 public boolean isKeyDown() { 636 return mIsKeyDown; 637 } 638 isKeyUp()639 public boolean isKeyUp() { 640 return mIsKeyUp; 641 } 642 isKeyMultiple()643 public boolean isKeyMultiple() { 644 return mIsMultiple; 645 } 646 getKeyCode()647 public int getKeyCode() { 648 return mKeyCode; 649 } 650 getKeyEvent()651 public KeyEvent getKeyEvent() { 652 return mKeyEvent; 653 } 654 getCount()655 public int getCount() { 656 return mCount; 657 } 658 reset()659 public void reset() { 660 mIsKeyDown = false; 661 mIsKeyUp = false; 662 mIsMultiple = false; 663 } 664 onKeyDown(int keyCode, KeyEvent event)665 public boolean onKeyDown(int keyCode, KeyEvent event) { 666 mIsKeyDown = true; 667 mKeyCode = keyCode; 668 mKeyEvent = event; 669 return true; 670 } 671 onKeyLongPress(int keyCode, KeyEvent event)672 public boolean onKeyLongPress(int keyCode, KeyEvent event) { 673 return false; 674 } 675 onKeyMultiple(int keyCode, int count, KeyEvent event)676 public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) { 677 mIsMultiple = true; 678 mKeyCode = keyCode; 679 mKeyEvent = event; 680 mCount = count; 681 if (count < 1) { 682 return false; // this key event never repeat. 683 } 684 return true; 685 } 686 onKeyUp(int keyCode, KeyEvent event)687 public boolean onKeyUp(int keyCode, KeyEvent event) { 688 mIsKeyUp = true; 689 mKeyCode = keyCode; 690 mKeyEvent = event; 691 return true; 692 } 693 } 694 } 695