blob: 859dfe3c3fa4da9aef856df08737eea0aff9c3e9 [file] [log] [blame]
Santos Cordon64a86272019-11-28 11:24:21 +00001/*
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
17package com.android.server.display;
18
19import android.hardware.Sensor;
20import android.hardware.SensorEvent;
21import android.os.SystemClock;
22
23import java.lang.reflect.Constructor;
24import java.lang.reflect.Field;
25import java.lang.reflect.Method;
26
27public final class TestUtils {
28
29 public static SensorEvent createSensorEvent(Sensor sensor, int lux) throws Exception {
30 final Constructor<SensorEvent> constructor =
31 SensorEvent.class.getDeclaredConstructor(int.class);
32 constructor.setAccessible(true);
33 final SensorEvent event = constructor.newInstance(1);
34 event.sensor = sensor;
35 event.values[0] = lux;
36 event.timestamp = SystemClock.elapsedRealtimeNanos();
37 return event;
38 }
39
40
41 public static void setSensorType(Sensor sensor, int type, String strType) throws Exception {
42 Method setter = Sensor.class.getDeclaredMethod("setType", Integer.TYPE);
43 setter.setAccessible(true);
44 setter.invoke(sensor, type);
45 if (strType != null) {
46 Field f = sensor.getClass().getDeclaredField("mStringType");
47 f.setAccessible(true);
48 f.set(sensor, strType);
49 }
50 }
51
52 public static Sensor createSensor(int type, String strType) throws Exception {
53 Constructor<Sensor> constr = Sensor.class.getDeclaredConstructor();
54 constr.setAccessible(true);
55 Sensor sensor = constr.newInstance();
56 setSensorType(sensor, type, strType);
57 return sensor;
58 }
59
60}