1 /* 2 * Copyright (C) 2021 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; 18 19 import static org.junit.Assert.assertEquals; 20 21 import android.hardware.input.HostUsiVersion; 22 import android.os.Parcel; 23 24 import androidx.test.ext.junit.runners.AndroidJUnit4; 25 import androidx.test.filters.SmallTest; 26 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 @SmallTest 31 @RunWith(AndroidJUnit4.class) 32 public class InputDeviceTest { 33 private static final float DELTA = 0.01f; 34 private static final int DEVICE_ID = 1000; 35 assertMotionRangeEquals(InputDevice.MotionRange range, InputDevice.MotionRange outRange)36 private void assertMotionRangeEquals(InputDevice.MotionRange range, 37 InputDevice.MotionRange outRange) { 38 assertEquals(range.getAxis(), outRange.getAxis()); 39 assertEquals(range.getSource(), outRange.getSource()); 40 assertEquals(range.getMin(), outRange.getMin(), DELTA); 41 assertEquals(range.getMax(), outRange.getMax(), DELTA); 42 assertEquals(range.getFlat(), outRange.getFlat(), DELTA); 43 assertEquals(range.getFuzz(), outRange.getFuzz(), DELTA); 44 assertEquals(range.getResolution(), outRange.getResolution(), DELTA); 45 } 46 assertDeviceEquals(InputDevice device, InputDevice outDevice)47 private void assertDeviceEquals(InputDevice device, InputDevice outDevice) { 48 assertEquals(device.getId(), outDevice.getId()); 49 assertEquals(device.getGeneration(), outDevice.getGeneration()); 50 assertEquals(device.getControllerNumber(), outDevice.getControllerNumber()); 51 assertEquals(device.getName(), outDevice.getName()); 52 assertEquals(device.getVendorId(), outDevice.getVendorId()); 53 assertEquals(device.getProductId(), outDevice.getProductId()); 54 assertEquals(device.getDeviceBus(), outDevice.getDeviceBus()); 55 assertEquals(device.getDescriptor(), outDevice.getDescriptor()); 56 assertEquals(device.isExternal(), outDevice.isExternal()); 57 assertEquals(device.getSources(), outDevice.getSources()); 58 assertEquals(device.getKeyboardType(), outDevice.getKeyboardType()); 59 assertEquals(device.getKeyboardLanguageTag(), outDevice.getKeyboardLanguageTag()); 60 assertEquals(device.getKeyboardLayoutType(), outDevice.getKeyboardLayoutType()); 61 assertEquals(device.getMotionRanges().size(), outDevice.getMotionRanges().size()); 62 assertEquals(device.getHostUsiVersion(), outDevice.getHostUsiVersion()); 63 assertEquals(device.getAssociatedDisplayId(), outDevice.getAssociatedDisplayId()); 64 assertEquals(device.isEnabled(), outDevice.isEnabled()); 65 66 KeyCharacterMap keyCharacterMap = device.getKeyCharacterMap(); 67 KeyCharacterMap outKeyCharacterMap = outDevice.getKeyCharacterMap(); 68 assertEquals("keyCharacterMap not equal", keyCharacterMap, outKeyCharacterMap); 69 70 for (int j = 0; j < device.getMotionRanges().size(); j++) { 71 InputDevice.MotionRange motionRange = device.getMotionRanges().get(j); 72 assertMotionRangeEquals(motionRange, outDevice.getMotionRanges().get(j)); 73 74 int axis = motionRange.getAxis(); 75 int source = motionRange.getSource(); 76 assertEquals( 77 device.getViewBehavior().shouldSmoothScroll(axis, source), 78 outDevice.getViewBehavior().shouldSmoothScroll(axis, source)); 79 } 80 } 81 assertInputDeviceParcelUnparcel(KeyCharacterMap keyCharacterMap)82 private void assertInputDeviceParcelUnparcel(KeyCharacterMap keyCharacterMap) { 83 final InputDevice.Builder deviceBuilder = new InputDevice.Builder() 84 .setId(DEVICE_ID) 85 .setGeneration(42) 86 .setControllerNumber(43) 87 .setName("Test Device " + DEVICE_ID) 88 .setVendorId(44) 89 .setProductId(45) 90 .setDeviceBus(3) 91 .setDescriptor("descriptor") 92 .setExternal(true) 93 .setSources(InputDevice.SOURCE_HDMI) 94 .setKeyboardType(InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC) 95 .setKeyCharacterMap(keyCharacterMap) 96 .setHasVibrator(true) 97 .setHasMicrophone(true) 98 .setHasButtonUnderPad(true) 99 .setHasSensor(true) 100 .setHasBattery(true) 101 .setKeyboardLanguageTag("en-US") 102 .setKeyboardLayoutType("qwerty") 103 .setUsiVersion(new HostUsiVersion(2, 0)) 104 .setShouldSmoothScroll(true) 105 .setAssociatedDisplayId(Display.DEFAULT_DISPLAY) 106 .setEnabled(false); 107 108 for (int i = 0; i < 30; i++) { 109 deviceBuilder.addMotionRange( 110 MotionEvent.AXIS_GENERIC_1, 111 InputDevice.SOURCE_UNKNOWN, 112 i, 113 i + 1, 114 i + 2, 115 i + 3, 116 i + 4); 117 } 118 119 final InputDevice device = deviceBuilder.build(); 120 121 Parcel parcel = Parcel.obtain(); 122 device.writeToParcel(parcel, 0); 123 parcel.setDataPosition(0); 124 125 InputDevice outDevice = InputDevice.CREATOR.createFromParcel(parcel); 126 assertDeviceEquals(device, outDevice); 127 } 128 129 @Test testParcelUnparcelInputDevice_VirtualCharacterMap()130 public void testParcelUnparcelInputDevice_VirtualCharacterMap() { 131 final KeyCharacterMap keyCharacterMap = 132 KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); 133 assertInputDeviceParcelUnparcel(keyCharacterMap); 134 } 135 136 @Test testParcelUnparcelInputDevice_EmptyCharacterMap()137 public void testParcelUnparcelInputDevice_EmptyCharacterMap() { 138 final KeyCharacterMap keyCharacterMap = KeyCharacterMap.obtainEmptyMap(DEVICE_ID); 139 assertInputDeviceParcelUnparcel(keyCharacterMap); 140 } 141 } 142