android.support.test.espresso.ViewAssertion Java Examples

The following examples show how to use android.support.test.espresso.ViewAssertion. 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: TestElectionChoice.java    From polling-station-app with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
    public void clickOnElection() throws Exception {
        final Election e = electionActivity.getAdapter().getItem(1);

        onData(instanceOf(Election.class)) // We are using the position so don't need to specify a data matcher
                .inAdapterView(withId(R.id.election_list)) // Specify the explicit id of the ListView
                .atPosition(1) // Explicitly specify the adapter item to use
                .perform(click());
//        intended(hasComponent(MainActivity.class.getName()));
        onView(withId(R.id.app_bar)).check(new ViewAssertion() {
            @Override
            public void check(View view, NoMatchingViewException noViewFoundException) {
                assertEquals(((Toolbar) view).getTitle(), e.getKind());
                assertEquals(((Toolbar) view).getSubtitle(), e.getPlace());
            }
        });
    }
 
Example #2
Source File: CameraViewTest.java    From cameraview with Apache License 2.0 6 votes vote down vote up
private static ViewAssertion showingPreview() {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException noViewFoundException) {
            CameraView cameraView = (CameraView) view;
            TextureView textureView = cameraView.findViewById(R.id.texture_view);
            Bitmap bitmap = textureView.getBitmap();
            int topLeft = bitmap.getPixel(0, 0);
            int center = bitmap.getPixel(bitmap.getWidth() / 2, bitmap.getHeight() / 2);
            int bottomRight = bitmap.getPixel(
                    bitmap.getWidth() - 1, bitmap.getHeight() - 1);
            assertFalse("Preview possibly blank: " + Integer.toHexString(topLeft),
                    topLeft == center && center == bottomRight);
        }
    };
}
 
Example #3
Source File: CameraViewTest.java    From cameraview with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoFocus() {
    onView(withId(R.id.camera))
            .check(new ViewAssertion() {
                @Override
                public void check(View view, NoMatchingViewException noViewFoundException) {
                    CameraView cameraView = (CameraView) view;
                    // This can fail on devices without auto-focus support
                    assertThat(cameraView.getAutoFocus(), is(true));
                    cameraView.setAutoFocus(false);
                    assertThat(cameraView.getAutoFocus(), is(false));
                    cameraView.setAutoFocus(true);
                    assertThat(cameraView.getAutoFocus(), is(true));
                }
            });
}
 
Example #4
Source File: ViewAssertions.java    From zulip-android with Apache License 2.0 6 votes vote down vote up
public static ViewAssertion checkMessagesOnlyFromToday() {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException e) {
            if (!(view instanceof RecyclerView)) {
                throw e;
            }
            RecyclerView rv = (RecyclerView) view;
            RecyclerMessageAdapter recyclerMessageAdapter = (RecyclerMessageAdapter) rv.getAdapter();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("OfDate-" + (new Date()).toString() + " { ");
            for (int index = 0; index < recyclerMessageAdapter.getItemCount(); index++) {
                if (recyclerMessageAdapter.getItem(index) instanceof Message) {
                    Message message = (Message) recyclerMessageAdapter.getItem(index);
                    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.US);
                    stringBuilder.append(message.getID() + "-" + sdf.format(message.getTimestamp()) + " , ");
                    assertTrue("This message is not of today ID=" + message.getID() + ":" + message.getIdForHolder() + "\ncontent=" + message.getContent(), DateUtils.isToday(message.getTimestamp().getTime()));
                }
            }
            stringBuilder.append(" }");
            printLogInPartsIfExceeded(stringBuilder, "checkMessagesOnlyFromToday");
        }
    };
}
 
Example #5
Source File: EspRecyclerViewItem.java    From espresso-macchiato with MIT License 6 votes vote down vote up
/**
 * Check that this item exist.
 *
 * Is true when adapter has matching item ignores the display state.
 *
 * @since Espresso Macchiato 0.6
 */

public void assertExist() {
    findRecyclerView().check(new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException noViewFoundException) {
            if (noViewFoundException != null) {
                throw noViewFoundException;
            }

            RecyclerView recyclerView = (RecyclerView) view;
            if (index >= recyclerView.getAdapter().getItemCount()) {
                throw new AssertionFailedError("Requested item should exist.");
            }
        }
    });
}
 
Example #6
Source File: Util.java    From DebugRank with Apache License 2.0 6 votes vote down vote up
public static ViewAssertion flexGridHasCode(final List<CodeLine> expectedCodeLines)
{
    return new ViewAssertion()
    {
        @Override
        public void check(View view, NoMatchingViewException noViewException)
        {
            FlexGrid flexGrid = (FlexGrid) view;

            List<CodeLine> actualCodeLines = (List<CodeLine>) flexGrid.getItemsSource();

            for (int i = 0; i < expectedCodeLines.size(); i++)
            {
                CodeLine expected = expectedCodeLines.get(i);
                CodeLine actual = actualCodeLines.get(i);

                assertEquals(expected.getCodeText(), actual.getCodeText());
            }
        }
    };
}
 
Example #7
Source File: EspressoTools.java    From android-step-by-step with Apache License 2.0 6 votes vote down vote up
public static ViewAssertion hasViewWithTextAtPosition(final int index, final CharSequence text) {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException e) {
            if (!(view instanceof RecyclerView)) {
                throw e;
            }
            RecyclerView rv = (RecyclerView) view;
            ArrayList<View> outviews = new ArrayList<>();
            rv.findViewHolderForAdapterPosition(index).itemView.findViewsWithText(outviews, text,
                    FIND_VIEWS_WITH_TEXT);
            Truth.assert_().withFailureMessage("There's no view at index " + index + " of recyclerview that has text : " + text)
                    .that(outviews).isNotEmpty();
        }
    };
}
 
Example #8
Source File: EspressoTools.java    From android-step-by-step with Apache License 2.0 6 votes vote down vote up
public static ViewAssertion doesntHaveViewWithText(final String text) {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException e) {
            if (!(view instanceof RecyclerView)) {
                throw e;
            }
            RecyclerView rv = (RecyclerView) view;
            ArrayList<View> outviews = new ArrayList<>();
            for (int index = 0; index < rv.getAdapter().getItemCount(); index++) {
                rv.findViewHolderForAdapterPosition(index).itemView.findViewsWithText(outviews, text,
                        FIND_VIEWS_WITH_TEXT);
                if (outviews.size() > 0) break;
            }
            Truth.assertThat(outviews).isEmpty();
        }
    };
}
 
Example #9
Source File: TestElectionChoice.java    From polling-station-app with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
    public void searchCityAndClick() throws Exception {
        List<Election> unfilteredList = electionActivity.getAdapter().getList();

        onView(withId(R.id.search)).perform(click());

        final Election toClick = unfilteredList.get(0);
        onView(withId(android.support.design.R.id.search_src_text)).perform(typeText(toClick.getPlace()));

        onView (withId (R.id.election_list)).check (ViewAssertions.matches (new Matchers().withListSize (1)));

        onData(instanceOf(Election.class))
                .inAdapterView(withId(R.id.election_list))
                .atPosition(0)
                .perform(click());
//        intended(hasComponent(MainActivity.class.getName()));
        onView(withId(R.id.app_bar)).check(new ViewAssertion() {
            @Override
            public void check(View view, NoMatchingViewException noViewFoundException) {
                assertEquals(((Toolbar) view).getTitle(), toClick.getKind());
                assertEquals(((Toolbar) view).getSubtitle(), toClick.getPlace());
            }
        });
    }
 
Example #10
Source File: TestUtilities.java    From test-samples with Apache License 2.0 5 votes vote down vote up
static Activity getCurrentActivity() {

        final Activity[] activity = new Activity[1];
        onView(isRoot()).check(new ViewAssertion() {
            @Override
            public void check(View view, NoMatchingViewException noViewFoundException) {
                activity[0] = (Activity) view.findViewById(android.R.id.content).getContext();
            }
        });
        return activity[0];
    }
 
Example #11
Source File: CameraViewTest.java    From cameraview with Apache License 2.0 5 votes vote down vote up
@Test
public void testFlash() {
    onView(withId(R.id.camera))
            .check(new ViewAssertion() {
                @Override
                public void check(View view, NoMatchingViewException noViewFoundException) {
                    CameraView cameraView = (CameraView) view;
                    assertThat(cameraView.getFlash(), is(CameraView.FLASH_AUTO));
                    cameraView.setFlash(CameraView.FLASH_TORCH);
                    assertThat(cameraView.getFlash(), is(CameraView.FLASH_TORCH));
                }
            });
}
 
Example #12
Source File: CameraViewTest.java    From cameraview with Apache License 2.0 5 votes vote down vote up
@Test
public void testFacing() {
    onView(withId(R.id.camera))
            .check(new ViewAssertion() {
                @Override
                public void check(View view, NoMatchingViewException noViewFoundException) {
                    CameraView cameraView = (CameraView) view;
                    assertThat(cameraView.getFacing(), is(CameraView.FACING_BACK));
                    cameraView.setFacing(CameraView.FACING_FRONT);
                    assertThat(cameraView.getFacing(), is(CameraView.FACING_FRONT));
                }
            })
            .perform(waitFor(1000))
            .check(showingPreview());
}
 
Example #13
Source File: NumberPadTimePickerDialogTest.java    From NumberPadTimePicker with Apache License 2.0 5 votes vote down vote up
/**
 * @param enabled Whether the view should be matched to be enabled or not.
 * @return A {@link ViewAssertion} that asserts that a view should be matched
 *         to be enabled or disabled.
 */
private static ViewAssertion matchesIsEnabled(boolean enabled) {
    // TODO: When we're comfortable with the APIs, we can statically import them and
    // make direct calls to these methods and cut down on the verbosity, instead of
    // writing helper methods that wrap these APIs.
    return ViewAssertions.matches(enabled ? ViewMatchers.isEnabled() : Matchers.not(ViewMatchers.isEnabled()));
}
 
Example #14
Source File: ViewAssertions.java    From zulip-android with Apache License 2.0 5 votes vote down vote up
public static ViewAssertion checkOrderOfMessages(final ZulipActivity zulipActivity) {
    //ZulipActivity is needed for generating detailed info about position if doesn't matches
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException noMatchingViewException) {
            if (!(view instanceof RecyclerView)) {
                throw noMatchingViewException;
            }
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append('{');
            RecyclerMessageAdapter recyclerMessageAdapter = (RecyclerMessageAdapter) ((RecyclerView) view).getAdapter();
            for (int i = 0; i < recyclerMessageAdapter.getItemCount() - 1; i++) {
                Object object = recyclerMessageAdapter.getItem(i);
                if (object instanceof Message) {
                    if (recyclerMessageAdapter.getItem(i + 1) instanceof Message) {
                        boolean checkOrder = (((Message) object).getID() < ((Message) recyclerMessageAdapter.getItem(i + 1)).getID());
                        assertTrue(generateDetailedInfoForOrder(((Message) object), ((Message) recyclerMessageAdapter.getItem(i + 1)), zulipActivity, i), checkOrder);
                    }
                    stringBuilder.append(((Message) object).getID() + ",");
                } else if (object instanceof MessageHeaderParent) {
                    stringBuilder.append("} | { ");
                }
            }
            stringBuilder.append('}');
            printLogInPartsIfExceeded(stringBuilder, "checkOrderOfMessages");
        }
    };
}
 
Example #15
Source File: AppBarLayoutAssertion.java    From espresso-macchiato with MIT License 5 votes vote down vote up
public static ViewAssertion assertIsExpanded() {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException noViewFoundException) {
            AppBarLayout appBarLayout = (AppBarLayout) view;
            boolean isFullExpanded = appBarLayout.getHeight() - appBarLayout.getBottom() == 0;
            ViewMatchers.assertThat("is full expanded", isFullExpanded, is(true));
        }
    };
}
 
Example #16
Source File: ViewAssertions.java    From zulip-android with Apache License 2.0 5 votes vote down vote up
public static ViewAssertion checkIfMessagesMatchTheHeaderParent(final ZulipActivity zulipActivity) {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException noMatchingViewException) {
            if (!(view instanceof RecyclerView)) {
                throw noMatchingViewException;
            }

            RecyclerMessageAdapter recyclerMessageAdapter = (RecyclerMessageAdapter) ((RecyclerView) view).getAdapter();
            String lastHolderId = null;
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < recyclerMessageAdapter.getItemCount() - 1; i++) {
                Object object = recyclerMessageAdapter.getItem(i);
                if (object instanceof MessageHeaderParent) {
                    lastHolderId = ((MessageHeaderParent) object).getId();
                    stringBuilder.append("} \'MHP\'-\'" + lastHolderId + " {");
                } else if (object instanceof Message) {
                    Message message = (Message) object;
                    boolean checkOrder = (lastHolderId.equals(message.getIdForHolder()));
                    assertTrue(generateDetailDebugInfoForCorrectHeaderParent(message, zulipActivity, lastHolderId, i), checkOrder);
                    stringBuilder.append(message.getIdForHolder() + "-" + message.getID() + " , ");
                }
            }
            stringBuilder.append('}');
            printLogInPartsIfExceeded(stringBuilder, "checkIfMessagesMatchTheHeaderParent");
        }
    };
}
 
Example #17
Source File: ViewAssertions.java    From zulip-android with Apache License 2.0 5 votes vote down vote up
public static ViewAssertion checkIfBelongToSameNarrow(final ZulipActivity zulipActivity) {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException noMatchingViewException) {
            if (!(view instanceof RecyclerView)) {
                throw noMatchingViewException;
            }
            RecyclerMessageAdapter recyclerMessageAdapter = (RecyclerMessageAdapter) ((RecyclerView) view).getAdapter();
            String lastHolderId = null;
            int headerParentsCount = 0;
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < recyclerMessageAdapter.getItemCount() - 1; i++) {
                Object object = recyclerMessageAdapter.getItem(i);
                if (object instanceof MessageHeaderParent) {
                    headerParentsCount++;
                    assertTrue(generateDetailInfoSameNarrow((MessageHeaderParent) object, i, zulipActivity, lastHolderId), headerParentsCount <= 1);
                    lastHolderId = ((MessageHeaderParent) object).getId();
                    stringBuilder.append(lastHolderId + " { ");
                } else if (object instanceof Message) {
                    boolean checkId = ((Message) object).getIdForHolder().equals(lastHolderId);
                    assertTrue(generateDetailInfoSameNarrow(((Message) object), zulipActivity, lastHolderId, i), checkId);
                    stringBuilder.append(((Message) object).getIdForHolder() + " , ");
                }
            }
            stringBuilder.append(" }");
            printLogInPartsIfExceeded(stringBuilder, "checkIfBelongToSameNarrow");
        }
    };
}
 
Example #18
Source File: TestElectionChoice.java    From polling-station-app with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void atElectionActivity() throws Exception {
    onView(withId(R.id.app_bar)).check(new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException noViewFoundException) {
            assertEquals(((Toolbar) view).getTitle(), "Choose election");
        }
    });
}
 
Example #19
Source File: ViewAssertions.java    From android with Apache License 2.0 5 votes vote down vote up
public static ViewAssertion haveItemCount(final int count) {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException noViewFoundException) {
            StringDescription description = new StringDescription();
            description.appendText("'");

            if (noViewFoundException != null) {
                description.appendText(String.format(
                        "' check could not be performed because view '%s' was not found.\n",
                        noViewFoundException.getViewMatcherDescription()));
                Log.e(TAG, description.toString());
                throw noViewFoundException;
            } else {
                int actualCount = -1;

                if (view instanceof RecyclerView) {
                    actualCount = getItemCount((RecyclerView) view);
                }

                if (actualCount == -1) {
                    throw new AssertionError("Cannot get view item count.");
                } else if (actualCount != count) {
                    throw new AssertionError("View has " + actualCount +
                            "items while expected " + count);
                }
            }
        }
    };
}
 
Example #20
Source File: ViewAssertions.java    From zulip-android with Apache License 2.0 5 votes vote down vote up
public static ViewAssertion hasItemsCount() {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException e) {
            if (!(view instanceof RecyclerView)) {
                throw e;
            }
            RecyclerView rv = (RecyclerView) view;
            assertThat("Items less than 2, which means no E-Mails!", rv.getAdapter().getItemCount(), org.hamcrest.Matchers.greaterThan(2));
        }
    };
}
 
Example #21
Source File: EspressoTools.java    From android-step-by-step with Apache License 2.0 5 votes vote down vote up
public static ViewAssertion hasItemsCount(final int count) {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException e) {
            if (!(view instanceof RecyclerView)) {
                throw e;
            }
            RecyclerView rv = (RecyclerView) view;
            if (rv.getAdapter() == null && count == 0) {
                return;
            }
            Truth.assertThat(rv.getAdapter().getItemCount()).isEqualTo(count);
        }
    };
}
 
Example #22
Source File: EspressoTools.java    From android-step-by-step with Apache License 2.0 5 votes vote down vote up
public static ViewAssertion hasHolderItemAtPosition(final int index,
                                                    final Matcher<RecyclerView.ViewHolder> viewHolderMatcher) {
    return new ViewAssertion() {
        @Override
        public void check(View view, NoMatchingViewException e) {
            if (!(view instanceof RecyclerView)) {
                throw e;
            }
            RecyclerView rv = (RecyclerView) view;
            Assert.assertThat(rv.findViewHolderForAdapterPosition(index), viewHolderMatcher);
        }
    };
}
 
Example #23
Source File: Util.java    From DebugRank with Apache License 2.0 5 votes vote down vote up
public static ViewAssertion withDrawable(@DrawableRes final int expectedResourceId)
{
    return new ViewAssertion()
    {
        @Override
        public void check(View view, NoMatchingViewException noViewException)
        {
            @DrawableRes int tag = (int) ((MockDrawable) ((ImageView) view).getDrawable()).getTag();

            assertEquals(expectedResourceId, tag);
        }
    };
}
 
Example #24
Source File: Util.java    From DebugRank with Apache License 2.0 5 votes vote down vote up
public static ViewAssertion mockDrawable(final String expectedTag)
{
    return new ViewAssertion()
    {
        @Override
        public void check(View view, NoMatchingViewException noViewException)
        {
            String tag = (String) ((MockDrawable) ((ImageView) view).getDrawable()).getTag();

            assertEquals(expectedTag, tag);
        }
    };
}
 
Example #25
Source File: Util.java    From DebugRank with Apache License 2.0 5 votes vote down vote up
public static ViewAssertion flexGridReadOnly(final boolean expected)
{
    return new ViewAssertion()
    {
        @Override
        public void check(View view, NoMatchingViewException noViewException)
        {
            FlexGrid flexGrid = (FlexGrid) view;

            assertEquals(expected, flexGrid.isReadOnly());
        }
    };
}
 
Example #26
Source File: FlowViewAssertions.java    From flow with Apache License 2.0 4 votes vote down vote up
static ViewAssertion doesNotHaveFlowService(final String serviceName) {
  return new FlowServiceIsNull(serviceName, true);
}
 
Example #27
Source File: FlowViewAssertions.java    From flow with Apache License 2.0 4 votes vote down vote up
static ViewAssertion hasFlowService(final String serviceName) {
  return new FlowServiceIsNull(serviceName, false);
}
 
Example #28
Source File: OrientationAssertion.java    From espresso-macchiato with MIT License 4 votes vote down vote up
public static ViewAssertion isOrientationLandscape() {
    return new OrientationAssertion(Configuration.ORIENTATION_LANDSCAPE);
}
 
Example #29
Source File: OrientationAssertion.java    From espresso-macchiato with MIT License 4 votes vote down vote up
public static ViewAssertion isOrientationPortrait() {
    return new OrientationAssertion(Configuration.ORIENTATION_PORTRAIT);
}
 
Example #30
Source File: LayoutManagerTypeAssertion.java    From espresso-macchiato with MIT License 4 votes vote down vote up
public static ViewAssertion isLinearLayoutManager() {
    return new LayoutManagerTypeAssertion(LinearLayoutManager.class);
}