Modify close app function.

Test: atest com.android.settings.tests.perf
Change-Id: Ia504dd1185de1d33a21fec344871d4b0fabae953
This commit is contained in:
youtengliang
2020-03-20 18:10:31 +08:00
committed by Raff Tsai
parent c305df9450
commit aa4f582931
2 changed files with 40 additions and 22 deletions

View File

@@ -22,7 +22,7 @@
</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>

View File

@@ -44,12 +44,33 @@ 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 TIME_OUT = 5000;
private static final int TEST_TIME = 10; private static final int TEST_TIME = 10;
private static final Pattern PATTERN = Pattern.compile("TotalTime:\\s[0-9]*"); private static final Pattern PATTERN = Pattern.compile("TotalTime:\\s[0-9]*");
private static final String[] PAGES = private static final Page[] PAGES;
{"Settings", "Wi-Fi", "BlueTooth", "Application", "Battery"};
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 Bundle mBundle;
private UiDevice mDevice; private UiDevice mDevice;
private Instrumentation mInstrumentation; private Instrumentation mInstrumentation;
@@ -64,53 +85,50 @@ public class LaunchSettingsTest {
mDevice.pressHome(); mDevice.pressHome();
mDevice.waitForIdle(TIME_OUT); mDevice.waitForIdle(TIME_OUT);
for (String string : PAGES) { for (Page page : PAGES) {
mResult.put(string, new ArrayList<Integer>()); mResult.put(page.title, new ArrayList<Integer>());
} }
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
sendResult(); putResultToBundle();
mInstrumentation.sendStatus(0, mBundle); mInstrumentation.sendStatus(0, mBundle);
} }
@Test @Test
public void settingsPerformanceTest() throws Exception { public void settingsPerformanceTest() throws Exception {
for (int i = 0; i < TEST_TIME; i++) { for (int i = 0; i < TEST_TIME; i++) {
executePreformanceTest("android.settings.SETTINGS", "Search settings", 0); for (Page page : PAGES) {
executePreformanceTest("android.settings.WIFI_SETTINGS", "Use WiFi", 1); executePreformanceTest(page.action, page.displayName, page.title);
executePreformanceTest("android.settings.BLUETOOTH_SETTINGS", "Connected devices", 2); }
executePreformanceTest("android.settings.APPLICATION_SETTINGS", "App info", 3);
executePreformanceTest("android.intent.action.POWER_USAGE_SUMMARY", "Battery", 4);
} }
} }
private void executePreformanceTest(String activity, String text, int page) throws Exception { private void executePreformanceTest(String action, String displayName, String title)
final String mString = mDevice.executeShellCommand("am start -W -a" + activity); throws Exception {
mDevice.wait(Until.findObject(By.text(text)), TIME_OUT); final String mString = mDevice.executeShellCommand("am start -W -a" + action);
handleLaunchResult(page, mString); mDevice.wait(Until.findObject(By.text(displayName)), TIME_OUT);
handleLaunchResult(title, mString);
closeApp(); closeApp();
mDevice.waitForIdle(TIME_OUT); mDevice.waitForIdle(TIME_OUT);
} }
private void handleLaunchResult(int page, String s) { private void handleLaunchResult(String title, String s) {
Matcher mMatcher = PATTERN.matcher(s); Matcher mMatcher = PATTERN.matcher(s);
if (mMatcher.find()) { if (mMatcher.find()) {
mResult.get(PAGES[page]).add(Integer.valueOf(mMatcher.group().split("\\s")[1])); mResult.get(title).add(Integer.valueOf(mMatcher.group().split("\\s")[1]));
} else { } else {
fail("Some pages can't be found"); fail("Some pages can't be found");
} }
} }
private void closeApp() throws Exception { private void closeApp() throws Exception {
mDevice.pressRecentApps(); mDevice.executeShellCommand("am force-stop com.android.settings");
mDevice.findObject(new UiSelector().resourceId("com.android.launcher3:id/snapshot")) Thread.sleep(1000);
.swipeUp(10);
} }
private void sendResult() { private void putResultToBundle() {
for (String string : mResult.keySet()) { for (String string : mResult.keySet()) {
mBundle.putString(String.format("LaunchSettingsTest_%s_%s", string, "max"), mBundle.putString(String.format("LaunchSettingsTest_%s_%s", string, "max"),
getMax(mResult.get(string))); getMax(mResult.get(string)));