diff --git a/tests/perftests/Android.bp b/tests/perftests/Android.bp index 1ba5d9c432a..7708bbe2569 100644 --- a/tests/perftests/Android.bp +++ b/tests/perftests/Android.bp @@ -19,4 +19,4 @@ android_test { test_suites: ["device-tests"], instrumentation_for: "Settings", -} +} \ No newline at end of file diff --git a/tests/perftests/AndroidManifest.xml b/tests/perftests/AndroidManifest.xml index 4ce6a54048c..972467f5fe6 100644 --- a/tests/perftests/AndroidManifest.xml +++ b/tests/perftests/AndroidManifest.xml @@ -15,15 +15,15 @@ --> + package="com.android.settings.tests.perf"> - + + android:targetPackage="com.android.settings.tests.perf" + android:label="Settings Performance Test Cases"> diff --git a/tests/perftests/src/com/android/settings/tests/perf/LaunchSettingsTest.java b/tests/perftests/src/com/android/settings/tests/perf/LaunchSettingsTest.java index 5140be2db91..f55598cb998 100644 --- a/tests/perftests/src/com/android/settings/tests/perf/LaunchSettingsTest.java +++ b/tests/perftests/src/com/android/settings/tests/perf/LaunchSettingsTest.java @@ -15,8 +15,15 @@ */ package com.android.settings.tests.perf; +import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; + +import static junit.framework.TestCase.fail; + import android.app.Instrumentation; import android.os.Bundle; +import android.support.test.uiautomator.By; +import android.support.test.uiautomator.UiDevice; +import android.support.test.uiautomator.Until; import androidx.test.InstrumentationRegistry; import androidx.test.runner.AndroidJUnit4; @@ -26,23 +33,119 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + @RunWith(AndroidJUnit4.class) public class LaunchSettingsTest { + private static class Page { + String action; + String displayName; + String title; + + Page(String action, String displayName, String title) { + this.action = action; + this.displayName = displayName; + this.title = title; + } + } + + private static final int TIME_OUT = 5000; + private static final int TEST_TIME = 10; + private static final Pattern PATTERN = Pattern.compile("TotalTime:\\s[0-9]*"); + private static final Page[] PAGES; + + static { + PAGES = new Page[]{ + new Page("android.settings.SETTINGS", "Search settings", "Settings"), + new Page("android.settings.WIFI_SETTINGS", "Use Wi‑Fi", "Wi-Fi"), + new Page("android.settings.BLUETOOTH_SETTINGS", "Connected devices", "BlueTooth"), + new Page("android.settings.APPLICATION_SETTINGS", "App info", "Application"), + new Page("android.intent.action.POWER_USAGE_SUMMARY", "Battery", "Battery") + }; + } + + private Bundle mBundle; + private UiDevice mDevice; + private Instrumentation mInstrumentation; + private Map> mResult; @Before public void setUp() throws Exception { + mBundle = new Bundle(); + mDevice = UiDevice.getInstance(getInstrumentation()); + mInstrumentation = InstrumentationRegistry.getInstrumentation(); + mResult = new LinkedHashMap<>(); + mDevice.pressHome(); + mDevice.waitForIdle(TIME_OUT); + + for (Page page : PAGES) { + mResult.put(page.title, new ArrayList()); + } } @After public void tearDown() throws Exception { + putResultToBundle(); + mInstrumentation.sendStatus(0, mBundle); } @Test - public void testReportMetrics() throws Exception { - Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); - final Bundle result = new Bundle(); - result.putString("LaunchSettingsTest_metric_key1", "1000"); - result.putString("LaunchSettingsTest_metric_key2", "5000"); - instrumentation.sendStatus(0, result); + public void settingsPerformanceTest() throws Exception { + for (int i = 0; i < TEST_TIME; i++) { + for (Page page : PAGES) { + executePreformanceTest(page.action, page.displayName, page.title); + } + } } -} + + private void executePreformanceTest(String action, String displayName, String title) + throws Exception { + final String mString = mDevice.executeShellCommand("am start -W -a" + action); + mDevice.wait(Until.findObject(By.text(displayName)), TIME_OUT); + handleLaunchResult(title, mString); + closeApp(); + mDevice.waitForIdle(TIME_OUT); + } + + private void handleLaunchResult(String title, String s) { + Matcher mMatcher = PATTERN.matcher(s); + if (mMatcher.find()) { + mResult.get(title).add(Integer.valueOf(mMatcher.group().split("\\s")[1])); + } else { + fail("Some pages can't be found"); + } + } + + private void closeApp() throws Exception { + mDevice.executeShellCommand("am force-stop com.android.settings"); + Thread.sleep(1000); + } + + private void putResultToBundle() { + for (String string : mResult.keySet()) { + mBundle.putString(String.format("LaunchSettingsTest_%s_%s", string, "max"), + getMax(mResult.get(string))); + mBundle.putString(String.format("LaunchSettingsTest_%s_%s", string, "min"), + getMin(mResult.get(string))); + mBundle.putString(String.format("LaunchSettingsTest_%s_%s", string, "avg"), + getAvg(mResult.get(string))); + } + } + + private String getMax(ArrayList launchResult) { + return String.format("%s", launchResult.isEmpty() ? "null" : Collections.max(launchResult)); + } + + private String getMin(ArrayList launchResult) { + return String.format("%s", launchResult.isEmpty() ? "null" : Collections.min(launchResult)); + } + + private String getAvg(ArrayList launchResult) { + return String.valueOf((int) launchResult.stream().mapToInt(i -> i).average().orElse(0)); + } +} \ No newline at end of file