/** * Waits for and returns a View. * * @param index the index of the view * @param classToFilterby the class to filter * @return the specified View */ public <T extends View> T waitForAndGetView(int index, Class<T> classToFilterBy) { long endTime = SystemClock.uptimeMillis() + Timeout.getSmallTimeout(); while (SystemClock.uptimeMillis() <= endTime && !waitForView(classToFilterBy, index, true, true)) ; int numberOfUniqueViews = searcher.getNumberOfUniqueViews(); ArrayList<T> views = RobotiumUtils.removeInvisibleViews(viewFetcher.getCurrentViews(classToFilterBy)); if (views.size() < numberOfUniqueViews) { int newIndex = index - (numberOfUniqueViews - views.size()); if (newIndex >= 0) index = newIndex; } T view = null; try { view = views.get(index); } catch (IndexOutOfBoundsException exception) { int match = index + 1; if (match > 1) { Assert.fail(match + " " + classToFilterBy.getSimpleName() + "s" + " are not found!"); } else { Assert.fail(classToFilterBy.getSimpleName() + " is not found!"); } } views = null; return view; }
public View[] getViews(String id, boolean scroll, View parent) { Context targetContext = instrumentation.getTargetContext(); String packageName = targetContext.getPackageName(); int viewId = targetContext.getResources().getIdentifier(id, "id", packageName); // Log.i("AAAAAA","id:"+viewId); Set<View> uniqueViewsMatchingId = new LinkedHashSet<View>(); long endTime = SystemClock.uptimeMillis() + Timeout.getSmallTimeout(); while (SystemClock.uptimeMillis() <= endTime) { sleeper.sleep(); List<View> list = null; if (parent == null) list = viewFetcher.getAllViews(true); else list = viewFetcher.getViews(parent, true); // LogEx.i("list in getViews: "+Arrays.deepToString(list.toArray())); for (View view : list) { Integer idOfView = Integer.valueOf(view.getId()); if (idOfView.equals(viewId) && view.isShown() && view.getWidth() != 0 && view.getHeight() != 0) { uniqueViewsMatchingId.add(view); } } if (scroll && scroller.scrollDown()) continue; break; } return uniqueViewsMatchingId.toArray(new View[0]); }
/** * Returns a {@code View} that shows a given text, from the list of current {@code View}s of the * specified type. * * @param classToFilterBy which {@code View}s to choose from * @param text the text that the view shows * @param onlyVisible {@code true} if only visible texts on the screen should be returned * @return a {@code View} showing a given text, from the list of current {@code View}s of the * specified type */ public <T extends TextView> T getView( Class<T> classToFilterBy, String text, boolean onlyVisible) { T viewToReturn = (T) waiter.waitForText( classToFilterBy, text, 0, Timeout.getSmallTimeout(), false, onlyVisible, false); if (viewToReturn == null) Assert.fail(classToFilterBy.getSimpleName() + " with text: '" + text + "' is not found!"); return viewToReturn; }
/** Gets the proper view to use for a screenshot. */ private View getScreenshotView() { View decorView = viewFetcher.getRecentDecorView(viewFetcher.getWindowDecorViews()); final long endTime = SystemClock.uptimeMillis() + Timeout.getSmallTimeout(); while (decorView == null) { final boolean timedOut = SystemClock.uptimeMillis() > endTime; if (timedOut) { return null; } sleeper.sleepMini(); decorView = viewFetcher.getRecentDecorView(viewFetcher.getWindowDecorViews()); } wrapAllGLViews(decorView); return decorView; }
public View[] getViews( Method method, String value, View parent, boolean scroll, long timeout, View scroller) { if (timeout <= 0) timeout = Timeout.getSmallTimeout(); long endTime = SystemClock.uptimeMillis() + timeout; Set<View> uniqueViewsMatchingId = new LinkedHashSet<View>(); Pattern targetTextPattern = null; int targetId = 0; if (Method.REGEX_TEXT == method) targetTextPattern = Pattern.compile(value); else if (Method.ID == method) { Context targetContext = instrumentation.getTargetContext(); String packageName = targetContext.getPackageName(); targetId = targetContext.getResources().getIdentifier(value, "id", packageName); } while (SystemClock.uptimeMillis() <= endTime) { sleeper.sleep(); List<View> list = null; if (parent == null) list = viewFetcher.getAllViews(true); else list = viewFetcher.getViews(parent, true); for (View view : list) { if (!view.isShown() || view.getWidth() == 0 || view.getHeight() == 0) continue; if (method == Method.REGEX_TEXT && (view instanceof TextView) && targetTextPattern.matcher(((TextView) view).getText()).find()) uniqueViewsMatchingId.add(view); else if (method == Method.PLAIN_TEXT && (view instanceof TextView) && ((TextView) view).getText().toString().contains(value)) uniqueViewsMatchingId.add(view); else if (method == Method.CLASS && view.getClass().getSimpleName().matches(value)) uniqueViewsMatchingId.add(view); else if (method == Method.ID && view.getId() == targetId) uniqueViewsMatchingId.add(view); } if (scroll && scrollEx(Scroller.DOWN, false, scroller)) continue; break; } return uniqueViewsMatchingId.toArray(new View[0]); }
/** * Waits for two views to be shown. * * @param scrollMethod {@code true} if it's a method used for scrolling * @param classes the classes to wait for * @return {@code true} if any of the views are shown and {@code false} if none of the views are * shown before the timeout */ public <T extends View> boolean waitForViews( boolean scrollMethod, Class<? extends T>... classes) { final long endTime = SystemClock.uptimeMillis() + Timeout.getSmallTimeout(); while (SystemClock.uptimeMillis() < endTime) { for (Class<? extends T> classToWaitFor : classes) { if (waitForView(classToWaitFor, 0, false, false)) { return true; } } if (scrollMethod) { scroller.scroll(Scroller.DOWN); } else { scroller.scrollDown(); } sleeper.sleep(); } return false; }
/** * Waits for the given {@link Activity}. * * @param activityClass the class of the {@code Activity} to wait for * @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it * does not */ public boolean waitForActivity(Class<? extends Activity> activityClass) { return waitForActivity(activityClass, Timeout.getSmallTimeout()); }
/** * Waits for the given {@link Activity}. * * @param name the name of the {@code Activity} to wait for e.g. {@code "MyActivity"} * @return {@code true} if {@code Activity} appears before the timeout and {@code false} if it * does not */ public boolean waitForActivity(String name) { return waitForActivity(name, Timeout.getSmallTimeout()); }
/** * Waits for a certain view. * * @param view the id of the view to wait for * @param index the index of the {@link View}. {@code 0} if only one is available * @param timeout the timeout in milliseconds * @return the specified View */ public View waitForView(int id, int index, int timeout) { if (timeout == 0) { timeout = Timeout.getSmallTimeout(); } return waitForView(id, index, timeout, false); }
public View getView(Method method, String value, View parent, View scroller) { return getView(method, value, parent, true, Timeout.getSmallTimeout(), scroller); }
public View getView(Method method, String value, View parent, boolean scroll) { return getView(method, value, parent, scroll, Timeout.getSmallTimeout(), null); }
public View[] getViews(View parent, String text, boolean scroll) { text = ".*" + text + ".*"; Set<View> uniqueViewsMatchingId = new LinkedHashSet<View>(); long endTime = SystemClock.uptimeMillis() + Timeout.getSmallTimeout(); while (SystemClock.uptimeMillis() <= endTime) { sleeper.sleep(); List<View> list = null; if (parent == null) list = viewFetcher.getAllViews(true); else list = viewFetcher.getViews(parent, true); for (View view : list) { if ((view instanceof TextView) && view.isShown() && ((TextView) view).getText().toString().matches(text) && view.getWidth() != 0 && view.getHeight() != 0) { uniqueViewsMatchingId.add(view); } } if (scroll && scroller.scrollDown()) continue; break; } return uniqueViewsMatchingId.toArray(new View[0]); }