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

Commit f21e0c5f authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Adding perf testing for brightness and display state changes"

parents 5737941c 231d8697
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ android_test {
        "apct-perftests-resources-manager-apps",
        "apct-perftests-utils",
        "collector-device-lib",
        "compatibility-device-util-axt",
        "core-tests-support",
        "guava",
    ],
+3 −1
Original line number Diff line number Diff line
@@ -13,7 +13,9 @@
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <uses-permission android:name="android.permission.DEVICE_POWER" />
    <uses-permission android:name="android.permission.CONTROL_DISPLAY_BRIGHTNESS" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <application>
        <uses-library android:name="android.test.runner" />
        <profileable android:shell="true" />
+84 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 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 android.os;

import android.content.Context;
import android.hardware.display.DisplayManager;
import android.provider.Settings;
import android.view.Display;

import androidx.benchmark.BenchmarkState;
import androidx.benchmark.junit4.BenchmarkRule;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import com.android.compatibility.common.util.PollingCheck;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public final class DisplayPerfTest {
    private static final float DELTA = 0.001f;

    @Rule
    public final BenchmarkRule mBenchmarkRule = new BenchmarkRule();

    private DisplayManager mDisplayManager;
    private Context mContext;

    @Before
    public void setUp() {
        mContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        mDisplayManager = mContext.getSystemService(DisplayManager.class);
    }

    @Test
    public void testBrightnessChanges() throws Exception {
        final BenchmarkState state = mBenchmarkRule.getState();
        Settings.System.putInt(mContext.getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE,
                Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
        SystemClock.sleep(20);
        float brightness = 0.3f;
        while (state.keepRunning()) {
            setAndWaitToChangeBrightness(brightness);
            brightness = toggleBrightness(brightness);
        }
    }

    private float toggleBrightness(float oldBrightness) {
        float[] brightnesses = new float[]{0.3f, 0.5f};
        if (oldBrightness == brightnesses[0]) {
            return brightnesses[1];
        }
        return brightnesses[0];
    }

    private void setAndWaitToChangeBrightness(float brightness) throws Exception {
        mDisplayManager.setBrightness(0, brightness);
        PollingCheck.check("Brightness is not set to the expected value", 500,
                () -> isInRange(mDisplayManager.getBrightness(Display.DEFAULT_DISPLAY), brightness,
                        DELTA));
    }

    private boolean isInRange(float value, float target, float delta) {
        return target - delta <= value && target + delta >= value;
    }
}