Merge lastest test case from master

Bug: 152716522
Test: atest com.android.settings.tests.perf
Change-Id: If5913440b4a4b58662c195e8cb637804fd6f2449
Merged-In: Ia504dd1185de1d33a21fec344871d4b0fabae953
This commit is contained in:
Raff Tsai
2020-04-14 09:48:00 +08:00
parent 2c9d412ba2
commit 7baddeebeb
3 changed files with 115 additions and 12 deletions

View File

@@ -19,4 +19,4 @@ android_test {
test_suites: ["device-tests"],
instrumentation_for: "Settings",
}
}

View File

@@ -15,15 +15,15 @@
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.settings.tests.perf">
package="com.android.settings.tests.perf">
<application>
<uses-library android:name="android.test.runner" />
<uses-library android:name="android.test.runner"/>
</application>
<instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="com.android.settings"
android:label="Settings Performance Test Cases">
android:targetPackage="com.android.settings.tests.perf"
android:label="Settings Performance Test Cases">
</instrumentation>
</manifest>

View File

@@ -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 WiFi", "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<String, ArrayList<Integer>> 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<Integer>());
}
}
@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<Integer> launchResult) {
return String.format("%s", launchResult.isEmpty() ? "null" : Collections.max(launchResult));
}
private String getMin(ArrayList<Integer> launchResult) {
return String.format("%s", launchResult.isEmpty() ? "null" : Collections.min(launchResult));
}
private String getAvg(ArrayList<Integer> launchResult) {
return String.valueOf((int) launchResult.stream().mapToInt(i -> i).average().orElse(0));
}
}