package com.example.profbola.bakingtime;

import android.support.test.espresso.ViewAssertion;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.support.test.espresso.matcher.BoundedMatcher;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.example.profbola.bakingtime.ui.MainActivity;
import com.example.profbola.bakingtime.utils.RecipeAdapter;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onData;
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.doesNotExist;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.not;

/**
 * Created by prof.BOLA on 7/6/2017.
 */

@RunWith(AndroidJUnit4.class)
public class RecipeActivityTest {

    public final String CAKE_NAME = "Nutella Pie";

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule
            = new ActivityTestRule<>(MainActivity.class);

    private Matcher<RecyclerView.ViewHolder> recipeViewHolderBoundedMatcher (final String text) {

        return new BoundedMatcher<RecyclerView.ViewHolder, RecipeAdapter.RecipeViewHolder>(RecipeAdapter.RecipeViewHolder.class) {
            @Override
            protected boolean matchesSafely(RecipeAdapter.RecipeViewHolder item) {
                TextView titleView = (TextView) item.itemView.findViewById(R.id.recipe_list);
                return titleView == null ? false : titleView.getText().toString().contains(text);
            }

            @Override
            public void describeTo(Description description) {
                description.appendText("No ViewHolder found with text: " + text);
            }
        };
    }

    @Test
    public void CakeName_AppearInListing() {
        onView(withId(R.id.recipe_list))
                .perform(RecyclerViewActions
                        .scrollToHolder(recipeViewHolderBoundedMatcher(CAKE_NAME)));
    }

    @Test
    public void clickRecipeItem_OpensDetailActivity() {
        onView(withId(R.id.recipe_list))
                .perform(RecyclerViewActions.actionOnItem(withId(R.id.recipe_list), click()));

        onView(withId(R.id.ingredients_listing))
                .perform(RecyclerViewActions.scrollTo(withId(R.id.ingredient_name)))
                .check((ViewAssertion) not(doesNotExist()));
    }

}

@@ -1007,16 +1007,96 @@
 atches;%0A
+import static android.support.test.espresso.matcher.ViewMatchers.hasDescendant;%0A
 import s
@@ -1482,21 +1482,22 @@
 %7B%0A%0A    p
-ublic
+rivate
  final S
@@ -1528,16 +1528,80 @@
 la Pie%22;
+%0A    private final String INGREDIENT_NAME = %22heavy cream(cold)%22;
 %0A%0A    @R
@@ -2551,32 +2551,103 @@
 d.recipe_list))%0A
+                .check(matches(hasDescendant(withText(CAKE_NAME))));%0A//
                 
@@ -2675,16 +2675,18 @@
 Actions%0A
+//
         
@@ -2905,79 +2905,226 @@
 orm(
-RecyclerViewActions.actionOnItem(withId(R.id.recipe_list), click(
+click())%0A                .perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));%0A%0A        onView(withId(R.id.ingredients_listing))%0A                .check(matches(hasDescendant(withText(INGREDIENT_NAME)
 )));%0A
-%0A
+//
     
@@ -3160,32 +3160,34 @@
 ients_listing))%0A
+//
                 
@@ -3255,16 +3255,18 @@
 name)))%0A
+//
         

Fix all failing tests