1 /* 2 * Copyright (C) 2017 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 com.android.settings.testutils.shadow; 18 19 import android.view.InputDevice; 20 21 import org.robolectric.annotation.Implementation; 22 import org.robolectric.annotation.Implements; 23 import org.robolectric.annotation.Resetter; 24 import org.robolectric.shadow.api.Shadow; 25 26 import java.util.HashMap; 27 import java.util.Map; 28 29 @Implements(InputDevice.class) 30 public class ShadowInputDevice extends org.robolectric.shadows.ShadowInputDevice { 31 32 public static int[] sDeviceIds; 33 34 private static Map<Integer, InputDevice> sDeviceMap = new HashMap<>(); 35 36 private int mDeviceId; 37 38 private int mSources; 39 40 private boolean mIsFullKeyboard; 41 42 @Implementation getDeviceIds()43 protected static int[] getDeviceIds() { 44 return sDeviceIds; 45 } 46 47 @Implementation getDevice(int id)48 protected static InputDevice getDevice(int id) { 49 return sDeviceMap.get(id); 50 } 51 addDevice(int id, InputDevice device)52 public static void addDevice(int id, InputDevice device) { 53 sDeviceMap.put(id, device); 54 } 55 56 @Resetter reset()57 public static void reset() { 58 sDeviceIds = null; 59 sDeviceMap.clear(); 60 } 61 62 @Implementation getId()63 protected int getId() { 64 return mDeviceId; 65 } 66 setId(int id)67 public void setId(int id) { 68 mDeviceId = id; 69 } 70 71 @Implementation getSources()72 public int getSources() { 73 return mSources; 74 } 75 setSources(int sources)76 public void setSources(int sources) { 77 mSources = sources; 78 } 79 80 @Implementation isFullKeyboard()81 public boolean isFullKeyboard() { 82 return mIsFullKeyboard; 83 } 84 setFullKeyboard(boolean isFullKeyboard)85 public void setFullKeyboard(boolean isFullKeyboard) { 86 mIsFullKeyboard = isFullKeyboard; 87 } 88 makeInputDevicebyId(int id)89 public static InputDevice makeInputDevicebyId(int id) { 90 final InputDevice inputDevice = Shadow.newInstanceOf(InputDevice.class); 91 final ShadowInputDevice shadowInputDevice = Shadow.extract(inputDevice); 92 shadowInputDevice.setId(id); 93 return inputDevice; 94 } 95 makeInputDevicebyIdWithSources(int id, int sources)96 public static InputDevice makeInputDevicebyIdWithSources(int id, int sources) { 97 final InputDevice inputDevice = Shadow.newInstanceOf(InputDevice.class); 98 final ShadowInputDevice shadowInputDevice = Shadow.extract(inputDevice); 99 shadowInputDevice.setId(id); 100 shadowInputDevice.setSources(sources); 101 return inputDevice; 102 } 103 104 /** 105 * Create a full keyboard input device shadow. 106 * @param id The ID to use. If the ID is < 1, the device is considered virtual. 107 * @return The shadow InputDevice 108 */ makeFullKeyboardInputDevicebyId(int id)109 public static InputDevice makeFullKeyboardInputDevicebyId(int id) { 110 final InputDevice inputDevice = Shadow.newInstanceOf(InputDevice.class); 111 final ShadowInputDevice shadowInputDevice = Shadow.extract(inputDevice); 112 shadowInputDevice.setId(id); 113 shadowInputDevice.setFullKeyboard(true); 114 shadowInputDevice.setSources(InputDevice.SOURCE_KEYBOARD); 115 return inputDevice; 116 } 117 } 118