Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 97cb1e36 authored by Fiona Campbell's avatar Fiona Campbell Committed by Android (Google) Code Review
Browse files

Merge "Tidy up our tests" into main

parents e9f30dae c264bafd
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -141,7 +141,7 @@ import java.util.concurrent.atomic.AtomicReference;
 * Tests for {@link com.android.server.power.PowerManagerService}.
 *
 * Build/Install/Run:
 *  atest FrameworksServicesTests:PowerManagerServiceTest
 *  atest PowerServiceTests:PowerManagerServiceTest
 */
@SuppressWarnings("GuardedBy")
@RunWith(TestParameterInjector.class)
+0 −1
Original line number Diff line number Diff line
@@ -134,7 +134,6 @@ java_library {
    name: "servicestests-core-utils",
    srcs: [
        "src/com/android/server/am/DeviceConfigSession.java",
        "src/com/android/server/display/TestUtils.java",
        "src/com/android/server/pm/PackageSettingBuilder.java",
        "src/com/android/server/pm/parsing/TestPackageParser2.kt",
    ],
+0 −94
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.display;

import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.input.InputSensorInfo;
import android.os.Parcel;
import android.os.SystemClock;
import android.view.DisplayAddress;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public final class TestUtils {

    public static SensorEvent createSensorEvent(Sensor sensor, int value) throws Exception {
        final Constructor<SensorEvent> constructor =
                SensorEvent.class.getDeclaredConstructor(int.class);
        constructor.setAccessible(true);
        final SensorEvent event = constructor.newInstance(1);
        event.sensor = sensor;
        event.values[0] = value;
        event.timestamp = SystemClock.elapsedRealtimeNanos();
        return event;
    }


    public static void setSensorType(Sensor sensor, int type, String strType) throws Exception {
        Method setter = Sensor.class.getDeclaredMethod("setType", Integer.TYPE);
        setter.setAccessible(true);
        setter.invoke(sensor, type);
        if (strType != null) {
            Field f = sensor.getClass().getDeclaredField("mStringType");
            f.setAccessible(true);
            f.set(sensor, strType);
        }
    }

    public static void setMaximumRange(Sensor sensor, float maximumRange) throws Exception {
        Method setter = Sensor.class.getDeclaredMethod("setRange", Float.TYPE, Float.TYPE);
        setter.setAccessible(true);
        setter.invoke(sensor, maximumRange, 1);
    }

    public static Sensor createSensor(int type, String strType) throws Exception {
        Constructor<Sensor> constr = Sensor.class.getDeclaredConstructor();
        constr.setAccessible(true);
        Sensor sensor = constr.newInstance();
        setSensorType(sensor, type, strType);
        return sensor;
    }

    public static Sensor createSensor(int type, String strType, float maximumRange)
            throws Exception {
        Constructor<Sensor> constr = Sensor.class.getDeclaredConstructor();
        constr.setAccessible(true);
        Sensor sensor = constr.newInstance();
        setSensorType(sensor, type, strType);
        setMaximumRange(sensor, maximumRange);
        return sensor;
    }

    public static Sensor createSensor(String type, String name) {
        return new Sensor(new InputSensorInfo(
                name, "vendor", 0, 0, 0, 1f, 1f, 1, 1, 1, 1,
                type, "", 0, 0, 0));
    }

    /**
     * Create a custom {@link DisplayAddress} to ensure we're not relying on any specific
     * display-address implementation in our code. Intentionally uses default object (reference)
     * equality rules.
     */
    public static class TestDisplayAddress extends DisplayAddress {
        @Override
        public void writeToParcel(Parcel out, int flags) { }
    }
}