Use BySelector to search for text in search results.
Verify the text after it's collected. I ran the test 20 times and couldn't find the stale object exception being thrown. Bug: 340341450 Bug: 339737008 Test: Manual Flag: com.google.android.apps.nexuslauncher.enable_inject_private_space_tile Change-Id: Ib153205db36cdd8b5fba4734b8b68930857659ff
This commit is contained in:
@@ -17,10 +17,12 @@ package com.android.launcher3.tapl;
|
||||
|
||||
import static com.android.launcher3.testing.shared.TestProtocol.NORMAL_STATE_ORDINAL;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.graphics.Rect;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.test.uiautomator.By;
|
||||
import androidx.test.uiautomator.BySelector;
|
||||
import androidx.test.uiautomator.Direction;
|
||||
import androidx.test.uiautomator.UiObject2;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -34,10 +36,12 @@ public class SearchResultFromQsb implements SearchInputSource {
|
||||
// This particular ID change should happen with caution
|
||||
private static final String SEARCH_CONTAINER_RES_ID = "search_results_list_view";
|
||||
protected final LauncherInstrumentation mLauncher;
|
||||
private final UiObject2 mSearchContainer;
|
||||
|
||||
SearchResultFromQsb(LauncherInstrumentation launcher) {
|
||||
mLauncher = launcher;
|
||||
mLauncher.waitForLauncherObject("search_container_all_apps");
|
||||
mSearchContainer = mLauncher.waitForSystemLauncherObject(SEARCH_CONTAINER_RES_ID);
|
||||
}
|
||||
|
||||
/** Find the app from search results with app name. */
|
||||
@@ -52,18 +56,9 @@ public class SearchResultFromQsb implements SearchInputSource {
|
||||
|
||||
/** Find the web suggestion from search suggestion's title text */
|
||||
public SearchWebSuggestion findWebSuggestion(String text) {
|
||||
ArrayList<UiObject2> webSuggestions =
|
||||
new ArrayList<>(mLauncher.waitForObjectsInContainer(
|
||||
mLauncher.waitForSystemLauncherObject(SEARCH_CONTAINER_RES_ID),
|
||||
By.clazz(TextView.class)));
|
||||
for (UiObject2 uiObject: webSuggestions) {
|
||||
String currentString = uiObject.getText();
|
||||
if (currentString.equals(text)) {
|
||||
return createWebSuggestion(uiObject);
|
||||
}
|
||||
}
|
||||
mLauncher.fail("Web suggestion title: " + text + " not found");
|
||||
return null;
|
||||
UiObject2 webSuggestion = mLauncher.waitForObjectInContainer(mSearchContainer,
|
||||
getSearchResultSelector(text));
|
||||
return createWebSuggestion(webSuggestion);
|
||||
}
|
||||
|
||||
protected SearchWebSuggestion createWebSuggestion(UiObject2 webSuggestion) {
|
||||
@@ -73,12 +68,24 @@ public class SearchResultFromQsb implements SearchInputSource {
|
||||
/** Find the total amount of views being displayed and return the size */
|
||||
public int getSearchResultItemSize() {
|
||||
ArrayList<UiObject2> searchResultItems =
|
||||
new ArrayList<>(mLauncher.waitForObjectsInContainer(
|
||||
mLauncher.waitForSystemLauncherObject(SEARCH_CONTAINER_RES_ID),
|
||||
new ArrayList<>(mLauncher.waitForObjectsInContainer(mSearchContainer,
|
||||
By.clazz(TextView.class)));
|
||||
return searchResultItems.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Scroll down to make next page search results rendered.
|
||||
*/
|
||||
public void getNextPageSearchResults() {
|
||||
final int searchContainerHeight = mLauncher.getVisibleBounds(mSearchContainer).height();
|
||||
// Start scrolling from center of the screen to top of the screen.
|
||||
mLauncher.scroll(mSearchContainer,
|
||||
Direction.DOWN,
|
||||
new Rect(0, 0, 0, searchContainerHeight / 2),
|
||||
/* steps= */ 10,
|
||||
/*slowDown= */ false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Taps outside bottom sheet to dismiss and return to workspace. Available on tablets only.
|
||||
* @param tapRight Tap on the right of bottom sheet if true, or left otherwise.
|
||||
@@ -121,25 +128,13 @@ public class SearchResultFromQsb implements SearchInputSource {
|
||||
|
||||
/** Verify a tile is present by checking its title and subtitle. */
|
||||
public void verifyTileIsPresent(String title, String subtitle) {
|
||||
ArrayList<UiObject2> searchResults =
|
||||
new ArrayList<>(mLauncher.waitForObjectsInContainer(
|
||||
mLauncher.waitForSystemLauncherObject(SEARCH_CONTAINER_RES_ID),
|
||||
By.clazz(TextView.class)));
|
||||
boolean foundTitle = false;
|
||||
boolean foundSubtitle = false;
|
||||
for (UiObject2 uiObject: searchResults) {
|
||||
String currentString = uiObject.getText();
|
||||
if (TextUtils.equals(currentString, title)) {
|
||||
foundTitle = true;
|
||||
} else if (TextUtils.equals(currentString, subtitle)) {
|
||||
foundSubtitle = true;
|
||||
}
|
||||
}
|
||||
if (!foundTitle) {
|
||||
mLauncher.fail("Tile not found for title: " + title);
|
||||
}
|
||||
if (!foundSubtitle) {
|
||||
mLauncher.fail("Tile not found for subtitle: " + subtitle);
|
||||
}
|
||||
mLauncher.waitForObjectInContainer(mSearchContainer,
|
||||
getSearchResultSelector(title));
|
||||
mLauncher.waitForObjectInContainer(mSearchContainer,
|
||||
getSearchResultSelector(subtitle));
|
||||
}
|
||||
|
||||
private BySelector getSearchResultSelector(String text) {
|
||||
return By.clazz(TextView.class).text(text);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user