Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
RecyclerViewSampleTest.java
/* * Copyright 2016, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.testing.espresso.RecyclerViewSample; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.action.ViewActions.click; import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.withText; import android.support.test.espresso.contrib.RecyclerViewActions; import android.support.test.espresso.matcher.ViewMatchers; import android.support.test.filters.LargeTest; import android.support.test.rule.ActivityTestRule; import android.support.test.runner.AndroidJUnit4; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeMatcher; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; /** * Test class showcasing some {@link RecyclerViewActions} from Espresso. */ @RunWith(AndroidJUnit4.class) @LargeTest public class RecyclerViewSampleTest { private static final int ITEM_BELOW_THE_FOLD = 40; @Rule public ActivityTestRule
mActivityRule = new ActivityTestRule<>( MainActivity.class); @Test public void scrollToItemBelowFold_checkItsText() { // First scroll to the position that needs to be matched and click on it. onView(ViewMatchers.withId(R.id.recyclerView)) .perform(RecyclerViewActions.actionOnItemAtPosition(ITEM_BELOW_THE_FOLD, click())); // Match the text in an item below the fold and check that it's displayed. String itemElementText = mActivityRule.getActivity().getResources().getString( R.string.item_element_text) + String.valueOf(ITEM_BELOW_THE_FOLD); onView(withText(itemElementText)).check(matches(isDisplayed())); } @Test public void itemInMiddleOfList_hasSpecialText() { // First, scroll to the view holder using the isInTheMiddle matcher. onView(ViewMatchers.withId(R.id.recyclerView)) .perform(RecyclerViewActions.scrollToHolder(isInTheMiddle())); // Check that the item has the special text. String middleElementText = mActivityRule.getActivity().getResources().getString(R.string.middle); onView(withText(middleElementText)).check(matches(isDisplayed())); } /** * Matches the {@link CustomAdapter.ViewHolder}s in the middle of the list. */ private static Matcher
isInTheMiddle() { return new TypeSafeMatcher
() { @Override protected boolean matchesSafely(CustomAdapter.ViewHolder customHolder) { return customHolder.getIsInTheMiddle(); } @Override public void describeTo(Description description) { description.appendText("item in the middle"); } }; } }