• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static org.junit.Assert.assertTrue;
21 
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.getDescriptor(), outDevice.getDescriptor());
55         assertEquals(device.isExternal(), outDevice.isExternal());
56         assertEquals(device.getSources(), outDevice.getSources());
57         assertEquals(device.getKeyboardType(), outDevice.getKeyboardType());
58         assertEquals(device.getMotionRanges().size(), outDevice.getMotionRanges().size());
59 
60         KeyCharacterMap keyCharacterMap = device.getKeyCharacterMap();
61         KeyCharacterMap outKeyCharacterMap = outDevice.getKeyCharacterMap();
62         assertTrue("keyCharacterMap not equal", keyCharacterMap.equals(outKeyCharacterMap));
63 
64         for (int j = 0; j < device.getMotionRanges().size(); j++) {
65             assertMotionRangeEquals(device.getMotionRanges().get(j),
66                     outDevice.getMotionRanges().get(j));
67         }
68     }
69 
assertInputDeviceParcelUnparcel(KeyCharacterMap keyCharacterMap)70     private void assertInputDeviceParcelUnparcel(KeyCharacterMap keyCharacterMap) {
71         final InputDevice device =
72                 new InputDevice(DEVICE_ID, 0 /* generation */, 0 /* controllerNumber */, "name",
73                 0 /* vendorId */, 0 /* productId */, "descriptor", true /* isExternal */,
74                 0 /* sources */, 0 /* keyboardType */, keyCharacterMap,
75                 false /* hasVibrator */, false /* hasMicrophone */, false /* hasButtonUnderpad */,
76                 true /* hasSensor */, false /* hasBattery */);
77 
78         Parcel parcel = Parcel.obtain();
79         device.writeToParcel(parcel, 0);
80         parcel.setDataPosition(0);
81 
82         InputDevice outDevice = InputDevice.CREATOR.createFromParcel(parcel);
83         assertDeviceEquals(device, outDevice);
84     }
85 
86     @Test
testParcelUnparcelInputDevice_VirtualCharacterMap()87     public void testParcelUnparcelInputDevice_VirtualCharacterMap() {
88         final KeyCharacterMap keyCharacterMap =
89                 KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
90         assertInputDeviceParcelUnparcel(keyCharacterMap);
91     }
92 
93     @Test
testParcelUnparcelInputDevice_EmptyCharacterMap()94     public void testParcelUnparcelInputDevice_EmptyCharacterMap() {
95         final KeyCharacterMap keyCharacterMap = KeyCharacterMap.obtainEmptyMap(DEVICE_ID);
96         assertInputDeviceParcelUnparcel(keyCharacterMap);
97     }
98 }
99