Search in sources :

Example 11 with ViewAssertion

use of android.support.test.espresso.ViewAssertion 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)));
        }
    });
}
Also used : CameraViewMatchers.hasAspectRatio(com.google.android.cameraview.CameraViewMatchers.hasAspectRatio) CameraViewActions.setAspectRatio(com.google.android.cameraview.CameraViewActions.setAspectRatio) ViewAssertion(android.support.test.espresso.ViewAssertion) ViewGroup(android.view.ViewGroup) UiController(android.support.test.espresso.UiController) NoMatchingViewException(android.support.test.espresso.NoMatchingViewException) View(android.view.View) TextureView(android.view.TextureView) Espresso.onView(android.support.test.espresso.Espresso.onView) FlakyTest(android.support.test.filters.FlakyTest) Test(org.junit.Test)

Example 12 with ViewAssertion

use of android.support.test.espresso.ViewAssertion in project material-components-android by material-components.

the class TabLayoutTest method testSetScrollPosition.

private void testSetScrollPosition(final boolean isLtr) throws Throwable {
    activityTestRule.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            activityTestRule.getActivity().setContentView(R.layout.design_tabs_fixed_width);
        }
    });
    final TabLayout tabs = (TabLayout) activityTestRule.getActivity().findViewById(R.id.tabs);
    assertEquals(TabLayout.MODE_SCROLLABLE, tabs.getTabMode());
    final TabLayoutScrollIdlingResource idler = new TabLayoutScrollIdlingResource(tabs);
    Espresso.registerIdlingResources(idler);
    // We're going to call setScrollPosition() incrementally, as if scrolling between one tab
    // and the next. Use the middle tab for best results. The positionOffsets should be in the
    // range [0, 1), so the final call will wrap to 0 but use the next tab's position.
    final int middleTab = tabs.getTabCount() / 2;
    final int[] positions = { middleTab, middleTab, middleTab, middleTab, middleTab + 1 };
    final float[] positionOffsets = { 0f, .25f, .5f, .75f, 0f };
    // Set layout direction
    onView(withId(R.id.tabs)).perform(setLayoutDirection(isLtr ? ViewCompat.LAYOUT_DIRECTION_LTR : ViewCompat.LAYOUT_DIRECTION_RTL));
    // Make sure it's scrolled all the way to the start
    onView(withId(R.id.tabs)).perform(selectTab(0));
    // Perform a series of setScrollPosition() calls
    final AtomicInteger lastScrollX = new AtomicInteger(tabs.getScrollX());
    for (int i = 0; i < positions.length; i++) {
        onView(withId(R.id.tabs)).perform(setScrollPosition(positions[i], positionOffsets[i])).check(new ViewAssertion() {

            @Override
            public void check(View view, NoMatchingViewException notFoundException) {
                if (view == null) {
                    throw notFoundException;
                }
                // Verify increasing or decreasing scroll X values
                int sx = view.getScrollX();
                assertTrue(isLtr ? sx > lastScrollX.get() : sx < lastScrollX.get());
                lastScrollX.set(sx);
            }
        });
    }
    Espresso.unregisterIdlingResources(idler);
}
Also used : ViewAssertion(android.support.test.espresso.ViewAssertion) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NoMatchingViewException(android.support.test.espresso.NoMatchingViewException) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView)

Example 13 with ViewAssertion

use of android.support.test.espresso.ViewAssertion in project material-components-android by material-components.

the class BottomSheetBehaviorTest method testInvisibleThenVisible.

@Test
@MediumTest
public void testInvisibleThenVisible() throws Throwable {
    activityTestRule.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            // The bottom sheet is initially invisible
            getBottomSheet().setVisibility(View.INVISIBLE);
            // Then it becomes visible when the CoL is touched
            getCoordinatorLayout().setOnTouchListener(new View.OnTouchListener() {

                @Override
                public boolean onTouch(View view, MotionEvent e) {
                    if (e.getAction() == MotionEvent.ACTION_DOWN) {
                        getBottomSheet().setVisibility(View.VISIBLE);
                        return true;
                    }
                    return false;
                }
            });
            assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_COLLAPSED));
        }
    });
    // Drag over the CoL
    Espresso.onView(ViewMatchers.withId(R.id.coordinator)).perform(new DragAction(GeneralLocation.BOTTOM_CENTER, GeneralLocation.TOP_CENTER, Press.FINGER)).check(new ViewAssertion() {

        @Override
        public void check(View view, NoMatchingViewException e) {
            // The bottom sheet should not react to the touch events
            assertThat(getBottomSheet(), is(ViewMatchers.isDisplayed()));
            assertThat(getBehavior().getState(), is(BottomSheetBehavior.STATE_COLLAPSED));
        }
    });
}
Also used : ViewAssertion(android.support.test.espresso.ViewAssertion) NoMatchingViewException(android.support.test.espresso.NoMatchingViewException) View(android.view.View) NestedScrollView(android.support.v4.widget.NestedScrollView) TextView(android.widget.TextView) MotionEvent(android.view.MotionEvent) SmallTest(android.support.test.filters.SmallTest) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test) MediumTest(android.support.test.filters.MediumTest)

Example 14 with ViewAssertion

use of android.support.test.espresso.ViewAssertion in project material-components-android by material-components.

the class BottomSheetBehaviorTouchTest method testTouchCoordinatorLayout.

@Test
public void testTouchCoordinatorLayout() {
    final CoordinatorLayoutActivity activity = activityTestRule.getActivity();
    mDown = false;
    Espresso.onView(sameInstance((View) activity.mCoordinatorLayout)).perform(// Click outside the bottom sheet
    ViewActions.click()).check(new ViewAssertion() {

        @Override
        public void check(View view, NoMatchingViewException e) {
            assertThat(e, is(nullValue()));
            assertThat(view, is(notNullValue()));
            // Check that the touch event fell through to the container
            assertThat(mDown, is(true));
        }
    });
}
Also used : ViewAssertion(android.support.test.espresso.ViewAssertion) CoordinatorLayoutActivity(android.support.design.testapp.CoordinatorLayoutActivity) NoMatchingViewException(android.support.test.espresso.NoMatchingViewException) View(android.view.View) MediumTest(android.support.test.filters.MediumTest) Test(org.junit.Test)

Example 15 with ViewAssertion

use of android.support.test.espresso.ViewAssertion in project android_frameworks_base by AOSPA.

the class SuggestionsPopupWindowTest method testTextAppearanceInSuggestionsPopup.

@SmallTest
public void testTextAppearanceInSuggestionsPopup() {
    final String text = "abc def ghi";
    final String[] singleWordCandidates = { "DEF", "Def" };
    final SuggestionSpan suggestionSpan = new SuggestionSpan(getActivity(), singleWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
    final String[] multiWordCandidates = { "ABC DEF GHI", "Abc Def Ghi" };
    final SuggestionSpan multiWordSuggestionSpan = new SuggestionSpan(getActivity(), multiWordCandidates, SuggestionSpan.FLAG_MISSPELLED);
    final TypedArray array = getActivity().obtainStyledAttributes(com.android.internal.R.styleable.Theme);
    final int id = array.getResourceId(com.android.internal.R.styleable.Theme_textEditSuggestionHighlightStyle, 0);
    array.recycle();
    final TextAppearanceSpan expectedSpan = new TextAppearanceSpan(getActivity(), id);
    final TextPaint tmpTp = new TextPaint();
    expectedSpan.updateDrawState(tmpTp);
    final int expectedHighlightTextColor = tmpTp.getColor();
    final float expectedHighlightTextSize = tmpTp.getTextSize();
    final TextView textView = (TextView) getActivity().findViewById(R.id.textview);
    // *XX* means that XX is highlighted.
    for (int i = 0; i < 2; i++) {
        onView(withId(R.id.textview)).perform(click());
        onView(withId(R.id.textview)).perform(replaceText(text));
        setSuggestionSpan(suggestionSpan, text.indexOf('d'), text.indexOf('f') + 1);
        setSuggestionSpan(multiWordSuggestionSpan, 0, text.length());
        showSuggestionsPopup();
        assertSuggestionsPopupIsDisplayed();
        assertSuggestionsPopupContainsItem("abc DEF ghi");
        assertSuggestionsPopupContainsItem("abc Def ghi");
        assertSuggestionsPopupContainsItem("ABC DEF GHI");
        assertSuggestionsPopupContainsItem("Abc Def Ghi");
        assertSuggestionsPopupContainsItem(getActivity().getString(com.android.internal.R.string.delete));
        onSuggestionsPopup().check(new ViewAssertion() {

            @Override
            public void check(View view, NoMatchingViewException e) {
                final ListView listView = (ListView) view.findViewById(com.android.internal.R.id.suggestionContainer);
                assertNotNull(listView);
                final int childNum = listView.getChildCount();
                assertEquals(singleWordCandidates.length + multiWordCandidates.length, childNum);
                for (int j = 0; j < childNum; j++) {
                    final TextView suggestion = (TextView) listView.getChildAt(j);
                    assertNotNull(suggestion);
                    final Spanned spanned = (Spanned) suggestion.getText();
                    assertNotNull(spanned);
                    // Check that the suggestion item order is kept.
                    final String expectedText;
                    if (j < singleWordCandidates.length) {
                        expectedText = "abc " + singleWordCandidates[j] + " ghi";
                    } else {
                        expectedText = multiWordCandidates[j - singleWordCandidates.length];
                    }
                    assertEquals(expectedText, spanned.toString());
                    // Check that the text is highlighted with correct color and text size.
                    final TextAppearanceSpan[] taSpan = spanned.getSpans(text.indexOf('d'), text.indexOf('f') + 1, TextAppearanceSpan.class);
                    assertEquals(1, taSpan.length);
                    TextPaint tp = new TextPaint();
                    taSpan[0].updateDrawState(tp);
                    assertEquals(expectedHighlightTextColor, tp.getColor());
                    assertEquals(expectedHighlightTextSize, tp.getTextSize());
                    // Check the correct part of the text is highlighted.
                    final int expectedStart;
                    final int expectedEnd;
                    if (j < singleWordCandidates.length) {
                        expectedStart = text.indexOf('d');
                        expectedEnd = text.indexOf('f') + 1;
                    } else {
                        expectedStart = 0;
                        expectedEnd = text.length();
                    }
                    assertEquals(expectedStart, spanned.getSpanStart(taSpan[0]));
                    assertEquals(expectedEnd, spanned.getSpanEnd(taSpan[0]));
                }
            }
        });
        pressBack();
        onView(withId(R.id.textview)).inRoot(withDecorView(is(getActivity().getWindow().getDecorView()))).perform(clearText());
    }
}
Also used : TextAppearanceSpan(android.text.style.TextAppearanceSpan) NoMatchingViewException(android.support.test.espresso.NoMatchingViewException) View(android.view.View) Espresso.onView(android.support.test.espresso.Espresso.onView) RootMatchers.withDecorView(android.support.test.espresso.matcher.RootMatchers.withDecorView) DragHandleUtils.onHandleView(android.widget.espresso.DragHandleUtils.onHandleView) Spanned(android.text.Spanned) TextPaint(android.text.TextPaint) TextPaint(android.text.TextPaint) ViewAssertion(android.support.test.espresso.ViewAssertion) TypedArray(android.content.res.TypedArray) SuggestionSpan(android.text.style.SuggestionSpan) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Aggregations

NoMatchingViewException (android.support.test.espresso.NoMatchingViewException)19 ViewAssertion (android.support.test.espresso.ViewAssertion)19 View (android.view.View)19 Espresso.onView (android.support.test.espresso.Espresso.onView)12 TextView (android.widget.TextView)7 AssertionFailedError (junit.framework.AssertionFailedError)7 RootMatchers.withDecorView (android.support.test.espresso.matcher.RootMatchers.withDecorView)6 Test (org.junit.Test)6 TypedArray (android.content.res.TypedArray)5 SmallTest (android.test.suitebuilder.annotation.SmallTest)5 Spanned (android.text.Spanned)5 TextPaint (android.text.TextPaint)5 SuggestionSpan (android.text.style.SuggestionSpan)5 TextAppearanceSpan (android.text.style.TextAppearanceSpan)5 DragHandleUtils.onHandleView (android.widget.espresso.DragHandleUtils.onHandleView)5 ViewActions.typeTextIntoFocusedView (android.support.test.espresso.action.ViewActions.typeTextIntoFocusedView)2 MediumTest (android.support.test.filters.MediumTest)2 TextureView (android.view.TextureView)2 FragmentTest (com.waz.zclient.testutils.FragmentTest)2 Bitmap (android.graphics.Bitmap)1