| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 70 |
private void assertInputDeviceParcelUnparcel(KeyCharacterMap keyCharacterMap) { |
| 71 |
final InputDevice device = |
| 72 |
new InputDevice(DEVICE_ID, 0 , 0 , "name", |
| 73 |
0 , 0 , "descriptor", true , |
| 74 |
0 , 0 , keyCharacterMap, |
| 75 |
false , false , false , |
| 76 |
true , false ); |
| 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 |
| 87 |
public void testParcelUnparcelInputDevice_VirtualCharacterMap() { |
| 88 |
final KeyCharacterMap keyCharacterMap = |
| 89 |
KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); |
| 90 |
assertInputDeviceParcelUnparcel(keyCharacterMap); |
| 91 |
} |
| 92 |
|
| 93 |
@Test |
| 94 |
public void testParcelUnparcelInputDevice_EmptyCharacterMap() { |
| 95 |
final KeyCharacterMap keyCharacterMap = KeyCharacterMap.obtainEmptyMap(DEVICE_ID); |
| 96 |
assertInputDeviceParcelUnparcel(keyCharacterMap); |
| 97 |
} |
| 98 |
} |