Fix settings ui tests in abvt suite

Change-Id: I77a262c473b2dfe09c3dabf8e07525fa17815b35
Fixes: 72608407
Test: atest
This commit is contained in:
Fan Zhang
2018-01-29 09:53:26 -08:00
parent 499efd06ab
commit 7576e726f9
5 changed files with 875 additions and 824 deletions

View File

@@ -16,57 +16,55 @@
package com.android.settings.ui;
import static com.android.settings.ui.testutils.SettingsTestUtils.SETTINGS_PACKAGE;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import android.app.Instrumentation;
import android.content.Intent;
import android.os.RemoteException;
import android.provider.Settings;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.Direction;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.Until;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.Suppress;
import android.text.TextUtils;
import android.util.Log;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
/** Verifies basic functionality of the About Phone screen */
public class AboutPhoneSettingsTests extends InstrumentationTestCase {
private static final boolean LOCAL_LOGV = false;
private static final String TAG = "AboutPhoneSettingsTest";
@RunWith(AndroidJUnit4.class)
@SmallTest
public class AboutPhoneSettingsTests {
private static final int TIMEOUT = 2000;
private static final String SETTINGS_PACKAGE = "com.android.settings";
private UiDevice mDevice;
// TODO: retrieve using name/ids from com.android.settings package
private static final String[] sResourceTexts = {
"Status",
"Legal information",
"Regulatory labels",
"Model",
"Android version",
"Android security patch level",
"Baseband version",
"Kernel version",
"Build number"
"Phone number",
"SIM status",
"Model & hardware",
"MEID",
"Android version"
};
private static final String[] sClickableResourceTexts = {
"Status", "Legal information", "Regulatory labels",
};
private UiDevice mDevice;
private Instrumentation mInstrumentation;
@Override
@Before
public void setUp() throws Exception {
if (LOCAL_LOGV) {
Log.d(TAG, "-------");
}
super.setUp();
mDevice = UiDevice.getInstance(getInstrumentation());
mInstrumentation = InstrumentationRegistry.getInstrumentation();
mDevice = UiDevice.getInstance(mInstrumentation);
try {
mDevice.setOrientationNatural();
} catch (RemoteException e) {
@@ -82,84 +80,40 @@ public class AboutPhoneSettingsTests extends InstrumentationTestCase {
UiObject2 view =
mDevice.wait(
Until.findObject(By.res(SETTINGS_PACKAGE + ":id/main_content")), TIMEOUT);
assertNotNull("Could not find main About Phone screen", view);
assertThat(view).isNotNull();
view.scroll(Direction.UP, 1.0f);
}
@Override
protected void tearDown() throws Exception {
@After
public void tearDown() throws Exception {
// Adding an extra pressBack so we exit About Phone Settings
// and finish the test cleanly
mDevice.pressBack();
mDevice.pressHome(); // finish settings activity
mDevice.waitForIdle(TIMEOUT * 2); // give UI time to finish animating
super.tearDown();
}
@Test
public void testAllMenuEntriesExist() throws Exception {
searchForItemsAndTakeAction(mDevice, sResourceTexts);
}
private void launchAboutPhoneSettings(String aboutSetting) throws Exception {
Intent aboutIntent = new Intent(aboutSetting);
aboutIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getInstrumentation().getContext().startActivity(aboutIntent);
}
/**
* Callable actions that can be taken when a UIObject2 is found
*
* @param device The current UiDevice
* @param item The UiObject2 that was found and can be acted on
*
* @return {@code true} if the call was successful, and {@code false} otherwise
*/
public interface UIObject2Callback {
boolean call(UiDevice device, UiObject2 item) throws Exception;
}
/**
* Clicks the given item and then presses the Back button
*
* <p>Used to test whether a given UiObject2 can be successfully clicked.
* Presses Back to restore state to the previous screen.
*
* @param device The device that can be used to press Back
* @param item The item to click
*
* @return {@code true} if clicking the item succeeded, and {@code false} otherwise
*/
public class UiObject2Clicker implements UIObject2Callback {
public boolean call(UiDevice device, UiObject2 item) throws Exception {
item.click();
Thread.sleep(TIMEOUT * 2); // give UI time to finish animating
boolean pressWorked = device.pressBack();
Thread.sleep(TIMEOUT * 2);
return pressWorked;
}
InstrumentationRegistry.getTargetContext().startActivity(aboutIntent);
}
/**
* Removes items found in the view and optionally takes some action.
*
* @param device The current UiDevice
* @param itemsLeftToFind The items to search for in the current view
* @param action Action to call on each item that is found; pass {@code null} to take no action
*/
private void removeItemsAndTakeAction(
UiDevice device, ArrayList<String> itemsLeftToFind, UIObject2Callback action) throws Exception {
private void removeItemsAndTakeAction(UiDevice device, ArrayList<String> itemsLeftToFind)
throws Exception {
for (Iterator<String> iterator = itemsLeftToFind.iterator(); iterator.hasNext(); ) {
String itemText = iterator.next();
UiObject2 item = device.wait(Until.findObject(By.text(itemText)), TIMEOUT);
if (item != null) {
if (LOCAL_LOGV) {
Log.d(TAG, itemText + " is present");
}
iterator.remove();
if (action != null) {
boolean success = action.call(device, item);
assertTrue("Calling action after " + itemText + " did not work", success);
}
} else {
if (LOCAL_LOGV) {
Log.d(TAG, "Could not find " + itemText);
}
}
}
}
@@ -169,25 +123,18 @@ public class AboutPhoneSettingsTests extends InstrumentationTestCase {
*
* <p>Will scroll down the screen until it has found all elements or reached the bottom.
* This allows elements to be found and acted on even if they change order.
*
* @param device The current UiDevice
* @param itemsToFind The items to search for in the current view
* @param action Action to call on each item that is found; pass {@code null} to take no action
*/
public void searchForItemsAndTakeAction(UiDevice device, String[] itemsToFind, UIObject2Callback action)
private void searchForItemsAndTakeAction(UiDevice device, String[] itemsToFind)
throws Exception {
ArrayList<String> itemsLeftToFind = new ArrayList<String>(Arrays.asList(itemsToFind));
assertFalse(
"There must be at least one item to search for on the screen!",
itemsLeftToFind.isEmpty());
ArrayList<String> itemsLeftToFind = new ArrayList<>(Arrays.asList(itemsToFind));
assertWithMessage("There must be at least one item to search for on the screen!")
.that(itemsLeftToFind)
.isNotEmpty();
if (LOCAL_LOGV) {
Log.d(TAG, "items: " + TextUtils.join(", ", itemsLeftToFind));
}
boolean canScrollDown = true;
while (canScrollDown && !itemsLeftToFind.isEmpty()) {
removeItemsAndTakeAction(device, itemsLeftToFind, action);
removeItemsAndTakeAction(device, itemsLeftToFind);
// when we've finished searching the current view, scroll down
UiObject2 view =
@@ -201,24 +148,11 @@ public class AboutPhoneSettingsTests extends InstrumentationTestCase {
}
}
// check the last items once we have reached the bottom of the view
removeItemsAndTakeAction(device, itemsLeftToFind, action);
removeItemsAndTakeAction(device, itemsLeftToFind);
assertTrue(
"The following items were not found on the screen: "
+ TextUtils.join(", ", itemsLeftToFind),
itemsLeftToFind.isEmpty());
}
@MediumTest // UI interaction
public void testAllMenuEntriesExist() throws Exception {
searchForItemsAndTakeAction(mDevice, sResourceTexts, null);
}
// Suppressing this test as it might be causing other test failures
// Will verify that this test is the cause before proceeding with solution
@Suppress
@MediumTest // UI interaction
public void testClickableEntriesCanBeClicked() throws Exception {
searchForItemsAndTakeAction(mDevice, sClickableResourceTexts, new UiObject2Clicker());
assertWithMessage("The following items were not found on the screen: "
+ TextUtils.join(", ", itemsLeftToFind))
.that(itemsLeftToFind)
.isEmpty();
}
}