androidx.test.espresso.Root Java Examples

The following examples show how to use androidx.test.espresso.Root. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RootViewPicker.java    From android-test with Apache License 2.0 6 votes vote down vote up
/**
 * Waits for a root to be ready. Ready here means the UI is no longer in flux if layout of the
 * root view is not being requested and the root view has window focus or is focusable.
 */
private Root waitForRootToBeReady(Root pickedRoot) {
  long timeout = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10) /* 10 seconds */;
  BackOff rootReadyBackoff = new RootReadyBackoff();
  while (System.currentTimeMillis() <= timeout) {
    if (pickedRoot.isReady()) {
      return pickedRoot;
    } else {
      controlledLooper.simulateWindowFocus(pickedRoot.getDecorView());
      uiController.loopMainThreadForAtLeast(rootReadyBackoff.getNextBackoffInMillis());
    }
  }

  throw new RootViewWithoutFocusException(
      String.format(
          Locale.ROOT,
          "Waited for the root of the view hierarchy to have "
              + "window focus and not request layout for 10 seconds. If you specified a non "
              + "default root matcher, it may be picking a root that never takes focus. "
              + "Root:\n%s",
          pickedRoot));
}
 
Example #2
Source File: RootMatchers.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matchesSafely(Root root) {
  int type = root.getWindowLayoutParams().get().type;
  // System-specific window types live between FIRST_SYSTEM_WINDOW and LAST_SYSTEM_WINDOW
  if ((WindowManager.LayoutParams.FIRST_SYSTEM_WINDOW < type
      && WindowManager.LayoutParams.LAST_SYSTEM_WINDOW > type)) {
    IBinder windowToken = root.getDecorView().getWindowToken();
    IBinder appToken = root.getDecorView().getApplicationWindowToken();
    if (windowToken == appToken) {
      // windowToken == appToken means this window isn't contained by any other windows.
      // if it was a window for an activity, it would have TYPE_BASE_APPLICATION.
      // therefore it must be a dialog box.
      return true;
    }
  }
  return false;
}
 
Example #3
Source File: RootMatchers.java    From android-test with Apache License 2.0 6 votes vote down vote up
@Override
public boolean matchesSafely(Root root) {
  int type = root.getWindowLayoutParams().get().type;
  if ((type != WindowManager.LayoutParams.TYPE_BASE_APPLICATION
      && type < WindowManager.LayoutParams.LAST_APPLICATION_WINDOW)) {
    IBinder windowToken = root.getDecorView().getWindowToken();
    IBinder appToken = root.getDecorView().getApplicationWindowToken();
    if (windowToken == appToken) {
      // windowToken == appToken means this window isn't contained by any other windows.
      // if it was a window for an activity, it would have TYPE_BASE_APPLICATION.
      // therefore it must be a dialog box.
      return true;
    }
  }
  return false;
}
 
Example #4
Source File: ToastMatcher.java    From samples-android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matchesSafely(Root root) {
    int type = root.getWindowLayoutParams().get().type;
    if ((type == WindowManager.LayoutParams.TYPE_TOAST)) {
        IBinder windowToken = root.getDecorView().getWindowToken();
        IBinder appToken = root.getDecorView().getApplicationWindowToken();
        if (windowToken == appToken) {
           return true;
        }
    }
    return false;
}
 
Example #5
Source File: InteractionRequest.java    From android-test with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the root matcher for this {@link InteractionRequest}
 *
 * @see Root
 * @param rootMatcher the root matcher to set
 * @return fluent interface
 */
public Builder setRootMatcher(@NonNull Matcher<Root> rootMatcher) {
  this.rootMatcher = checkNotNull(rootMatcher);
  checkArgument(
      remoteDescriptorRegistry.hasArgForInstanceType(rootMatcher.getClass()),
      "No RemoteDescriptor registered for ViewMatcher: %s",
      rootMatcher);
  return this;
}
 
Example #6
Source File: InteractionRequest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
InteractionRequest(
    @Nullable Matcher<Root> rootMatcher,
    @Nullable Matcher<View> viewMatcher,
    @Nullable ViewAction viewAction,
    @Nullable ViewAssertion viewAssertion) {
  this.rootMatcher = rootMatcher;
  this.viewMatcher = viewMatcher;
  this.viewAction = viewAction;
  this.viewAssertion = viewAssertion;
}
 
Example #7
Source File: InteractionRequest.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public InteractionRequest fromProto(InteractionRequestProto interactionRequestProto) {
  Builder interactionRequestBuilder = new Builder();
  interactionRequestBuilder
      .setRootMatcher(
          TypeProtoConverters.<Matcher<Root>>anyToType(
              interactionRequestProto.getRootMatcher()))
      .setViewMatcher(
          TypeProtoConverters.<Matcher<View>>anyToType(
              interactionRequestProto.getViewMatcher()));

  int actionOrAssertionCaseNumber =
      interactionRequestProto.getActionOrAssertionCase().getNumber();

  if (ActionOrAssertionCase.VIEW_ACTION.getNumber() == actionOrAssertionCaseNumber) {
    interactionRequestBuilder.setViewAction(
        TypeProtoConverters.<ViewAction>anyToType(
            interactionRequestProto.getViewAction()));
  }

  if (ActionOrAssertionCase.VIEW_ASSERTION.getNumber() == actionOrAssertionCaseNumber) {
    interactionRequestBuilder.setViewAssertion(
        TypeProtoConverters.<ViewAssertion>anyToType(
            interactionRequestProto.getViewAssertion()));
  }
  return interactionRequestBuilder.build();
}
 
Example #8
Source File: EspressoRemote.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized Callable<Void> createRemotePerformCallable(
    final Matcher<Root> rootMatcher,
    final Matcher<View> viewMatcher,
    final Map<String, IBinder> iBinders,
    final ViewAction... viewActions) {
  return createRemoteInteraction(
      new Runnable() {
        @Override
        public void run() {
          for (ViewAction viewAction : viewActions) {
            Log.i(
                TAG,
                String.format(
                    Locale.ROOT,
                    "Attempting to run perform interaction on a remote "
                        + "processes for ViewAction: %s",
                    viewAction));
            // TODO(b/32948667): This will create a request for every action.
            InteractionRequest interactionRequest =
                new InteractionRequest.Builder()
                    .setRootMatcher(rootMatcher)
                    .setViewMatcher(viewMatcher)
                    .setViewAction(viewAction)
                    .build();

            // Send remote interaction request to other Espresso instances
            initiateRemoteCall(interactionRequest.toProto().toByteArray(), iBinders);
          }
        }
      });
}
 
Example #9
Source File: EspressoRemote.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized Callable<Void> createRemoteCheckCallable(
    final Matcher<Root> rootMatcher,
    final Matcher<View> viewMatcher,
    final Map<String, IBinder> iBinders,
    final ViewAssertion viewAssertion) {

  return createRemoteInteraction(
      new Runnable() {
        @Override
        public void run() {
          Log.i(
              TAG,
              String.format(
                  Locale.ROOT,
                  "Attempting to run check interaction on a remote process "
                      + "for ViewAssertion: %s",
                  viewAssertion));
          InteractionRequest interactionRequest =
              new InteractionRequest.Builder()
                  .setRootMatcher(rootMatcher)
                  .setViewMatcher(viewMatcher)
                  .setViewAssertion(viewAssertion)
                  .build();

          // Send remote interaction request to other Espresso instances
          initiateRemoteCall(interactionRequest.toProto().toByteArray(), iBinders);
        }
      });
}
 
Example #10
Source File: NoopRemoteInteraction.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public Callable<Void> createRemotePerformCallable(
    Matcher<Root> rootMatcher,
    Matcher<View> viewMatcher,
    Map<String, IBinder> iBinders,
    ViewAction... viewActions) {
  return new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      throw new NoRemoteEspressoInstanceException("No remote instances available");
    }
  };
}
 
Example #11
Source File: NoopRemoteInteraction.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public Callable<Void> createRemoteCheckCallable(
    Matcher<Root> rootMatcher,
    Matcher<View> viewMatcher,
    Map<String, IBinder> iBinders,
    ViewAssertion viewAssertion) {
  return new Callable<Void>() {
    @Override
    public Void call() throws Exception {
      throw new NoRemoteEspressoInstanceException("No remote instances available");
    }
  };
}
 
Example #12
Source File: RootMatchers.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matchesSafely(Root item) {
  String popupClassName = "android.widget.PopupWindow$PopupViewContainer";
  if (Build.VERSION.SDK_INT >= 23) {
    popupClassName = "android.widget.PopupWindow$PopupDecorView";
  }
  return withDecorView(withClassName(is(popupClassName))).matches(item);
}
 
Example #13
Source File: RootMatchers.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matchesSafely(Root root) {
  int flags = root.getWindowLayoutParams().get().flags;
  // return true if FLAG_NOT_TOUCHABLE flag is not set
  return (flags & WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE)
      != WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
}
 
Example #14
Source File: RootMatchers.java    From android-test with Apache License 2.0 5 votes vote down vote up
@Override
public boolean matchesSafely(Root root) {
  int flags = root.getWindowLayoutParams().get().flags;
  // return true if FLAG_NOT_FOCUSABLE flag is not set
  return (flags & WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
      != WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
}
 
Example #15
Source File: ToastMatcher.java    From adamant-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean matchesSafely(Root root) {
    int type = root.getWindowLayoutParams().get().type;
    if ((type == WindowManager.LayoutParams.TYPE_TOAST)) {
        IBinder windowToken = root.getDecorView().getWindowToken();
        IBinder appToken = root.getDecorView().getApplicationWindowToken();
        if (windowToken == appToken) {
            return true;
        }
    }
    return false;
}
 
Example #16
Source File: RootViewPicker.java    From android-test with Apache License 2.0 5 votes vote down vote up
private Root pickARoot() {
  long timeout = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(60) /* 60 seconds */;
  RootResults rootResults = rootResultFetcher.fetch();
  BackOff noActiveRootsBackoff = new NoActiveRootsBackoff();
  BackOff noMatchingRootBackoff = new NoMatchingRootBackoff();
  while (System.currentTimeMillis() <= timeout) {
    switch (rootResults.getState()) {
      case ROOTS_PICKED:
        return rootResults.getPickedRoot();
      case NO_ROOTS_PRESENT:
        // no active roots yet, but should appear soon.
        uiController.loopMainThreadForAtLeast(noActiveRootsBackoff.getNextBackoffInMillis());
        break;
      case NO_ROOTS_PICKED:
        // a root which satisfies the matcher should show up eventually.
        uiController.loopMainThreadForAtLeast(noMatchingRootBackoff.getNextBackoffInMillis());
        break;
    }
    rootResults = rootResultFetcher.fetch();
  }

  if (RootResults.State.ROOTS_PICKED == rootResults.getState()) {
    return rootResults.getPickedRoot();
  }

  throw NoMatchingRootException.create(rootResults.rootSelector, rootResults.allRoots);
}
 
Example #17
Source File: RootViewPicker.java    From android-test with Apache License 2.0 5 votes vote down vote up
public RootResults fetch() {
  List<Root> allRoots = activeRootLister.listActiveRoots();
  List<Root> pickedRoots = Lists.newArrayList();

  for (Root root : allRoots) {
    if (selector.matches(root)) {
      pickedRoots.add(root);
    }
  }
  return new RootResults(allRoots, pickedRoots, selector);
}
 
Example #18
Source File: RootViewPicker.java    From android-test with Apache License 2.0 5 votes vote down vote up
public Root getPickedRoot() {
  if (pickedRoots.size() > 1) {
    LogUtil.logDebugWithProcess(TAG, "Multiple root windows detected: %s", pickedRoots);
    return getRootFromMultipleRoots();
  }
  return pickedRoots.get(0);
}
 
Example #19
Source File: RootMatchers.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matchesSafely(Root root) {
  return getResumedActivityTokens().contains(root.getDecorView().getApplicationWindowToken());
}
 
Example #20
Source File: StubRootMatcher.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matchesSafely(Root root) {
  return true;
}
 
Example #21
Source File: RootsOracle.java    From android-test with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public List<Root> listActiveRoots() {
  checkState(mainLooper.equals(Looper.myLooper()), "must be called on main thread.");

  if (!initialized) {
    initialize();
  }

  if (null == windowManagerObj) {
    Log.w(TAG, "No reflective access to windowmanager object.");
    return Lists.newArrayList();
  }

  if (null == viewsField) {
    Log.w(TAG, "No reflective access to mViews");
    return Lists.newArrayList();
  }
  if (null == paramsField) {
    Log.w(TAG, "No reflective access to mParams");
    return Lists.newArrayList();
  }

  List<View> views = null;
  List<LayoutParams> params = null;

  try {
    if (Build.VERSION.SDK_INT < 19) {
      views = Arrays.asList((View[]) viewsField.get(windowManagerObj));
      params = Arrays.asList((LayoutParams[]) paramsField.get(windowManagerObj));
    } else {
      views = (List<View>) viewsField.get(windowManagerObj);
      params = (List<LayoutParams>) paramsField.get(windowManagerObj);
    }
  } catch (RuntimeException re) {
    Log.w(
        TAG,
        String.format(
            Locale.ROOT,
            "Reflective access to %s or %s on %s failed.",
            viewsField,
            paramsField,
            windowManagerObj),
        re);
    return Lists.newArrayList();
  } catch (IllegalAccessException iae) {
    Log.w(
        TAG,
        String.format(
            Locale.ROOT,
            "Reflective access to %s or %s on %s failed.",
            viewsField,
            paramsField,
            windowManagerObj),
        iae);
    return Lists.newArrayList();
  }

  List<Root> roots = Lists.newArrayList();
  for (int i = views.size() - 1; i > -1; i--) {
    roots.add(
        new Root.Builder()
            .withDecorView(views.get(i))
            .withWindowLayoutParams(params.get(i))
            .build());
  }

  return roots;
}
 
Example #22
Source File: EspressoRemote.java    From android-test with Apache License 2.0 4 votes vote down vote up
public OnViewCheckStrategy(
    Matcher<Root> rootMatcher, Matcher<View> viewMatcher, ViewAssertion viewAssertion) {
  this.rootMatcher = rootMatcher;
  this.viewMatcher = viewMatcher;
  this.viewAssertion = viewAssertion;
}
 
Example #23
Source File: EspressoRemote.java    From android-test with Apache License 2.0 4 votes vote down vote up
public OnViewPerformStrategy(
    Matcher<Root> rootMatcher, Matcher<View> viewMatcher, ViewAction viewAction) {
  this.rootMatcher = rootMatcher;
  this.viewMatcher = viewMatcher;
  this.viewAction = viewAction;
}
 
Example #24
Source File: RootViewPicker.java    From android-test with Apache License 2.0 4 votes vote down vote up
private RootResults(List<Root> allRoots, List<Root> pickedRoots, Matcher<Root> rootSelector) {
  this.allRoots = allRoots;
  this.pickedRoots = pickedRoots;
  this.rootSelector = rootSelector;
}
 
Example #25
Source File: RootViewPicker.java    From android-test with Apache License 2.0 4 votes vote down vote up
private static boolean isTopmostRoot(Root topMostRoot, Root root) {
  return root.getWindowLayoutParams().get().type
      > topMostRoot.getWindowLayoutParams().get().type;
}
 
Example #26
Source File: RootMatchers.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matchesSafely(Root root) {
  return root.getWindowLayoutParams().isPresent();
}
 
Example #27
Source File: RootMatchers.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matchesSafely(Root root) {
  return decorViewMatcher.matches(root.getDecorView());
}
 
Example #28
Source File: RootViewPicker.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Inject
public RootResultFetcher(
    ActiveRootLister activeRootLister, AtomicReference<Matcher<Root>> rootMatcherRef) {
  this.activeRootLister = activeRootLister;
  this.selector = rootMatcherRef.get();
}
 
Example #29
Source File: RootMatchers.java    From android-test with Apache License 2.0 4 votes vote down vote up
/** Matches {@link Root}s that can take window focus. */
public static Matcher<Root> isFocusable() {
  return new IsFocusable();
}
 
Example #30
Source File: RootMatchers.java    From android-test with Apache License 2.0 4 votes vote down vote up
/** Matches {@link Root}s that can receive touch events. */
public static Matcher<Root> isTouchable() {
  return new IsTouchable();
}