1 /* 2 * Copyright (C) 2016 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 package android.view.cts; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.assertNotNull; 20 21 import android.os.Parcel; 22 import android.support.test.filters.SmallTest; 23 import android.support.test.runner.AndroidJUnit4; 24 import android.view.KeyEvent; 25 import android.view.KeyboardShortcutInfo; 26 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 /** 31 * Tests for {@link android.view.KeyboardShortcutInfo}. 32 */ 33 @SmallTest 34 @RunWith(AndroidJUnit4.class) 35 public class KeyboardShortcutInfoTest { 36 private static final CharSequence TEST_LABEL = "Test Label"; 37 private static final char TEST_BASE_CHARACTER = 't'; 38 private static final int TEST_KEYCODE = KeyEvent.KEYCODE_T; 39 private static final int TEST_MODIFIERS = KeyEvent.META_ALT_ON | KeyEvent.META_CTRL_ON; 40 41 @Test 42 public void testCharacterConstructor() { 43 KeyboardShortcutInfo info = new KeyboardShortcutInfo( 44 TEST_LABEL, TEST_BASE_CHARACTER, TEST_MODIFIERS); 45 assertNotNull(info); 46 assertEquals(TEST_LABEL, info.getLabel()); 47 assertEquals(TEST_BASE_CHARACTER, info.getBaseCharacter()); 48 assertEquals(KeyEvent.KEYCODE_UNKNOWN, info.getKeycode()); 49 assertEquals(TEST_MODIFIERS, info.getModifiers()); 50 assertEquals(0, info.describeContents()); 51 } 52 53 @Test 54 public void testKeycodeConstructor() { 55 KeyboardShortcutInfo info = new KeyboardShortcutInfo( 56 TEST_LABEL, TEST_KEYCODE, TEST_MODIFIERS); 57 assertNotNull(info); 58 assertEquals(TEST_LABEL, info.getLabel()); 59 assertEquals(Character.MIN_VALUE, info.getBaseCharacter()); 60 assertEquals(TEST_KEYCODE, info.getKeycode()); 61 assertEquals(TEST_MODIFIERS, info.getModifiers()); 62 assertEquals(0, info.describeContents()); 63 } 64 65 @Test(expected=IllegalArgumentException.class) 66 public void testConstructorChecksBaseCharacter() { 67 new KeyboardShortcutInfo(TEST_LABEL, Character.MIN_VALUE, TEST_MODIFIERS); 68 } 69 70 @Test(expected=IllegalArgumentException.class) 71 public void testConstructorChecksKeycode() { 72 new KeyboardShortcutInfo(TEST_LABEL, KeyEvent.KEYCODE_UNKNOWN - 1, TEST_MODIFIERS); 73 } 74 75 @Test 76 public void testWriteToParcelAndReadCharacter() { 77 Parcel dest = Parcel.obtain(); 78 KeyboardShortcutInfo info = new KeyboardShortcutInfo( 79 TEST_LABEL, TEST_BASE_CHARACTER, TEST_MODIFIERS); 80 info.writeToParcel(dest, 0); 81 82 dest.setDataPosition(0); 83 KeyboardShortcutInfo result = KeyboardShortcutInfo.CREATOR.createFromParcel(dest); 84 85 assertEquals(TEST_LABEL, result.getLabel()); 86 assertEquals(TEST_BASE_CHARACTER, result.getBaseCharacter()); 87 assertEquals(KeyEvent.KEYCODE_UNKNOWN, result.getKeycode()); 88 assertEquals(TEST_MODIFIERS, result.getModifiers()); 89 } 90 91 @Test 92 public void testWriteToParcelAndReadKeycode() { 93 Parcel dest = Parcel.obtain(); 94 KeyboardShortcutInfo info = new KeyboardShortcutInfo( 95 TEST_LABEL, TEST_KEYCODE, TEST_MODIFIERS); 96 info.writeToParcel(dest, 0); 97 98 dest.setDataPosition(0); 99 KeyboardShortcutInfo result = KeyboardShortcutInfo.CREATOR.createFromParcel(dest); 100 101 assertEquals(TEST_LABEL, result.getLabel()); 102 assertEquals(Character.MIN_VALUE, result.getBaseCharacter()); 103 assertEquals(TEST_KEYCODE, result.getKeycode()); 104 assertEquals(TEST_MODIFIERS, result.getModifiers()); 105 } 106 } 107