Merge "Merge lastest test case from master" into rvc-dev am: 09600dd8cc
Change-Id: I323cc1221d8182de32dba59ae57b476d82726e3c
This commit is contained in:
@@ -15,15 +15,15 @@
|
|||||||
-->
|
-->
|
||||||
|
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="com.android.settings.tests.perf">
|
package="com.android.settings.tests.perf">
|
||||||
|
|
||||||
<application>
|
<application>
|
||||||
<uses-library android:name="android.test.runner" />
|
<uses-library android:name="android.test.runner"/>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
<instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
|
<instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
|
||||||
android:targetPackage="com.android.settings"
|
android:targetPackage="com.android.settings.tests.perf"
|
||||||
android:label="Settings Performance Test Cases">
|
android:label="Settings Performance Test Cases">
|
||||||
</instrumentation>
|
</instrumentation>
|
||||||
|
|
||||||
</manifest>
|
</manifest>
|
||||||
|
@@ -15,8 +15,15 @@
|
|||||||
*/
|
*/
|
||||||
package com.android.settings.tests.perf;
|
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.app.Instrumentation;
|
||||||
import android.os.Bundle;
|
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.InstrumentationRegistry;
|
||||||
import androidx.test.runner.AndroidJUnit4;
|
import androidx.test.runner.AndroidJUnit4;
|
||||||
@@ -26,23 +33,119 @@ import org.junit.Before;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
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)
|
@RunWith(AndroidJUnit4.class)
|
||||||
public class LaunchSettingsTest {
|
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<String, ArrayList<Integer>> mResult;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
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
|
@After
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
|
putResultToBundle();
|
||||||
|
mInstrumentation.sendStatus(0, mBundle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReportMetrics() throws Exception {
|
public void settingsPerformanceTest() throws Exception {
|
||||||
Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
|
for (int i = 0; i < TEST_TIME; i++) {
|
||||||
final Bundle result = new Bundle();
|
for (Page page : PAGES) {
|
||||||
result.putString("LaunchSettingsTest_metric_key1", "1000");
|
executePreformanceTest(page.action, page.displayName, page.title);
|
||||||
result.putString("LaunchSettingsTest_metric_key2", "5000");
|
}
|
||||||
instrumentation.sendStatus(0, result);
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user