/*
 * Copyright 2016 Joe Rogers
 *
 * 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.espresso.contrib.activity;

import android.support.test.rule.ActivityTestRule;

import com.example.espresso.contrib.R;
import com.forkingcode.espresso.contrib.DescendantViewActions;

import org.junit.Rule;
import org.junit.Test;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.assertion.ViewAssertions.selectedDescendantsMatch;
import static android.support.test.espresso.contrib.RecyclerViewActions.actionOnItemAtPosition;
import static android.support.test.espresso.contrib.RecyclerViewActions.scrollToPosition;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

/**
 * Sample test case
 */
public class MainActivityTest {
    @Rule
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class, true, true);

    @Test
    public void testRecyclerAction() throws Exception {
        // By default you could only test direct actions on a recycler view. For example
        // you could "click" the view, navigate to another activity and verify conditions.

        // Chaining several RecyclerViewActions together.
        onView(withId(R.id.recyclerView)).perform(

                // First position the recycler view. Necessary to allow the layout
                // manager perform the scroll operation
                scrollToPosition(15),

                // Click the item to trigger navigation to detail view
                actionOnItemAtPosition(15, click())
        );

        // Check detail view
        onView(withId(R.id.favoriteStatus)).check(matches(withText(R.string.unfavorite)));

        // return to main activity
        pressBack();
    }

    @Test
    public void testFavoriteToggle() throws Exception {
        // More advanced test case testing toggling the favorite status on a particular row

        // Chaining several actions together on the recycler view
        onView(withId(R.id.recyclerView)).perform(

                // First position the recycler view
                scrollToPosition(25),

                // With the descendant actions provided, you can check the status of a descendant view using
                // a standard check
                actionOnItemAtPosition(25, DescendantViewActions.checkViewAction(
                        selectedDescendantsMatch(withId(R.id.favoriteButton),
                                withContentDescription(R.string.favorite)))),

                // Or perform an action on a descendant view.
                actionOnItemAtPosition(25,
                        DescendantViewActions.performDescendantAction(withId(R.id.favoriteButton), click())),

                // Then check to see the status change
                actionOnItemAtPosition(25, DescendantViewActions.checkViewAction(
                        selectedDescendantsMatch(withId(R.id.favoriteButton),
                                withContentDescription(R.string.unfavorite))))
        );
    }
}
@@ -1115,103 +1115,8 @@
 es;%0A
-import static android.support.test.espresso.assertion.ViewAssertions.selectedDescendantsMatch;%0A
 impo
@@ -1289,32 +1289,120 @@
 rollToPosition;%0A
+import static android.support.test.espresso.matcher.ViewMatchers.isCompletelyDisplayed;%0A
 import static an
@@ -3025,24 +3025,245 @@
 ition(25),%0A%0A
+                // Using the check view action, you can now test conditions of the view at position 25%0A                actionOnItemAtPosition(25, DescendantViewActions.checkViewAction(matches(isCompletelyDisplayed()))),%0A%0A
             
@@ -3394,16 +3394,120 @@
 rd check
+. Just provide way to find the descendant view and how you want to validate%0A                // the view.
 %0A       
@@ -3561,32 +3561,42 @@
 iewActions.check
+Descendant
 ViewAction(%0A    
@@ -3619,33 +3619,8 @@
     
-selectedDescendantsMatch(
 with
@@ -3647,41 +3647,17 @@
 on),
-%0A                                
+ matches(
 with
@@ -4032,32 +4032,42 @@
 iewActions.check
+Descendant
 ViewAction(%0A    
@@ -4090,33 +4090,8 @@
     
-selectedDescendantsMatch(
 with
@@ -4118,41 +4118,17 @@
 on),
-%0A                                
+ matches(
 with

Update test case sample