use of android.support.test.espresso.UiController in project iosched by google.
the class MatchersHelper method getNumberOfDescendantsForViewGroupDescendant.
/**
* This returns the number of descendants of the {@code parentViewWithMultipleChildren} that
* have an id of {@code idOfDescendantViewToGetTextFor}}. The parent view is expected to be a
* {@link ViewGroup}. This is used when there is a certain randomness in the elements shown in a
* collection view, even with mock data, but we want to check the number of elements inside the
* parent view.
*
* @see com.google.samples.apps.iosched.explore.ExploreIOActivityTest
*/
public static int getNumberOfDescendantsForViewGroupDescendant(final Matcher<View> parentViewWithMultipleChildren, final int idOfDescendantViewToGetTextFor) {
/**
* We cannot use a int directly as we need to make it final to access it inside the
* inner method but we cannot reassign a value to a final int.
*/
final int[] intHolder = { 0 };
onView(parentViewWithMultipleChildren).perform(new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(ViewGroup.class);
}
@Override
public String getDescription() {
return "getting text from a TextView with a known id and that is a descendant of " + "the nth child of a viewgroup";
}
@Override
public void perform(UiController uiController, View view) {
ViewGroup vg = (ViewGroup) view;
int descendants = vg.getChildCount();
for (int i = 0; i < descendants; i++) {
View ithDescendant = vg.getChildAt(i);
View matchedView = ithDescendant.findViewById(idOfDescendantViewToGetTextFor);
if (matchedView != null) {
intHolder[0] += 1;
}
}
}
});
return intHolder[0];
}
use of android.support.test.espresso.UiController in project iosched by google.
the class MatchersHelper method getText.
/**
* This returns the text for the view that has been matched with {@code matcher}. The matched
* view is expected to be a {@link TextView}. This is different from matching a view by text,
* this is intended to be used when matching a view by another mean (for example, by id) but
* when we need to know the text of that view, for use later in a test.
* <p/>
* In general, this isn't good practice as tests should be written using mock data (so we always
* know what the data is) but this enables us to write tests that are written using real data
* (so we don't always know what the data is but we want to verify something later in a test).
* This is enables us to write UI tests before refactoring a feature to make it easier to mock
* the data.
*/
public static String getText(final Matcher<View> matcher) {
/**
* We cannot use a String directly as we need to make it final to access it inside the
* inner method but we cannot reassign a value to a final String.
*/
final String[] stringHolder = { null };
onView(matcher).perform(new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isAssignableFrom(TextView.class);
}
@Override
public String getDescription() {
return "getting text from a TextView";
}
@Override
public void perform(UiController uiController, View view) {
TextView tv = (TextView) view;
stringHolder[0] = tv.getText().toString();
}
});
return stringHolder[0];
}
use of android.support.test.espresso.UiController in project iosched by google.
the class MatchersHelper method moveViewPagerToPage.
/**
* Moves <code>ViewPager</code> to specific page.
*/
public static ViewAction moveViewPagerToPage(final int page) {
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return isDisplayingAtLeast(90);
}
@Override
public String getDescription() {
return "ViewPager move to a specific page";
}
@Override
public void perform(UiController uiController, View view) {
uiController.loopMainThreadUntilIdle();
ViewPager viewPager = (ViewPager) view;
viewPager.setCurrentItem(page, false);
uiController.loopMainThreadUntilIdle();
}
};
}
use of android.support.test.espresso.UiController in project cameraview by google.
the class CameraViewTest method testAdjustViewBounds.
@Test
public void testAdjustViewBounds() {
onView(withId(R.id.camera)).check(new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
CameraView cameraView = (CameraView) view;
assertThat(cameraView.getAdjustViewBounds(), is(false));
cameraView.setAdjustViewBounds(true);
assertThat(cameraView.getAdjustViewBounds(), is(true));
}
}).perform(new AnythingAction("layout") {
@Override
public void perform(UiController uiController, View view) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
view.setLayoutParams(params);
}
}).check(new ViewAssertion() {
@Override
public void check(View view, NoMatchingViewException noViewFoundException) {
CameraView cameraView = (CameraView) view;
AspectRatio cameraRatio = cameraView.getAspectRatio();
AspectRatio viewRatio = AspectRatio.of(view.getWidth(), view.getHeight());
assertThat(cameraRatio, is(closeToOrInverse(viewRatio)));
}
});
}
Aggregations