1 /* 2 * Copyright (C) 2019 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.server.display; 18 19 import android.hardware.Sensor; 20 import android.hardware.SensorEvent; 21 import android.hardware.input.InputSensorInfo; 22 import android.os.Parcel; 23 import android.os.SystemClock; 24 import android.view.DisplayAddress; 25 26 import java.lang.reflect.Constructor; 27 import java.lang.reflect.Field; 28 import java.lang.reflect.Method; 29 30 public final class TestUtils { 31 createSensorEvent(Sensor sensor, int value)32 public static SensorEvent createSensorEvent(Sensor sensor, int value) throws Exception { 33 final Constructor<SensorEvent> constructor = 34 SensorEvent.class.getDeclaredConstructor(int.class); 35 constructor.setAccessible(true); 36 final SensorEvent event = constructor.newInstance(1); 37 event.sensor = sensor; 38 event.values[0] = value; 39 event.timestamp = SystemClock.elapsedRealtimeNanos(); 40 return event; 41 } 42 43 setSensorType(Sensor sensor, int type, String strType)44 public static void setSensorType(Sensor sensor, int type, String strType) throws Exception { 45 Method setter = Sensor.class.getDeclaredMethod("setType", Integer.TYPE); 46 setter.setAccessible(true); 47 setter.invoke(sensor, type); 48 if (strType != null) { 49 Field f = sensor.getClass().getDeclaredField("mStringType"); 50 f.setAccessible(true); 51 f.set(sensor, strType); 52 } 53 } 54 setMaximumRange(Sensor sensor, float maximumRange)55 public static void setMaximumRange(Sensor sensor, float maximumRange) throws Exception { 56 Method setter = Sensor.class.getDeclaredMethod("setRange", Float.TYPE, Float.TYPE); 57 setter.setAccessible(true); 58 setter.invoke(sensor, maximumRange, 1); 59 } 60 createSensor(int type, String strType)61 public static Sensor createSensor(int type, String strType) throws Exception { 62 Constructor<Sensor> constr = Sensor.class.getDeclaredConstructor(); 63 constr.setAccessible(true); 64 Sensor sensor = constr.newInstance(); 65 setSensorType(sensor, type, strType); 66 return sensor; 67 } 68 createSensor(int type, String strType, float maximumRange)69 public static Sensor createSensor(int type, String strType, float maximumRange) 70 throws Exception { 71 Constructor<Sensor> constr = Sensor.class.getDeclaredConstructor(); 72 constr.setAccessible(true); 73 Sensor sensor = constr.newInstance(); 74 setSensorType(sensor, type, strType); 75 setMaximumRange(sensor, maximumRange); 76 return sensor; 77 } 78 createSensor(String type, String name)79 public static Sensor createSensor(String type, String name) { 80 return new Sensor(new InputSensorInfo( 81 name, "vendor", 0, 0, 0, 1f, 1f, 1, 1, 1, 1, 82 type, "", 0, 0, 0)); 83 } 84 85 /** 86 * Create a custom {@link DisplayAddress} to ensure we're not relying on any specific 87 * display-address implementation in our code. Intentionally uses default object (reference) 88 * equality rules. 89 */ 90 public static class TestDisplayAddress extends DisplayAddress { 91 @Override writeToParcel(Parcel out, int flags)92 public void writeToParcel(Parcel out, int flags) { } 93 } 94 } 95