package com.movies.tobi.popularmovies.popularstream;

import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.contrib.RecyclerViewActions;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.movies.tobi.popularmovies.EspressoDependencies;
import com.movies.tobi.popularmovies.MovieApplication;
import com.movies.tobi.popularmovies.R;
import com.movies.tobi.popularmovies.backend.FakeBackend;
import com.movies.tobi.popularmovies.posterdetails.ApiMovieDetails;

import org.junit.Test;
import org.junit.runner.RunWith;

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.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
public class PopularMoviesActivityTest {

    private FakeBackend backend = new FakeBackend();

    public ActivityTestRule<PopularMoviesActivity> rule = new ActivityTestRule<PopularMoviesActivity>(PopularMoviesActivity.class) {
        @Override
        protected void beforeActivityLaunched() {
            MovieApplication movieApplication = (MovieApplication) InstrumentationRegistry.getTargetContext().getApplicationContext();
            movieApplication.setDependencies(new EspressoDependencies(backend));
        }
    };

    @Test
    public void shouldNavigateToMovieDetails() throws Exception {
        long movieId = 293660L;
        String posterPath = "/deadpool.jpg";
        String movieTitle = "Deadpool";
        String movieDescription = "Awesome movie";
        givenAddedStreamToBackend(movieId, posterPath);
        givenBackendReturnsMovieDetails(movieId, movieTitle, movieDescription, posterPath);
        rule.launchActivity(null);

        onView(withId(R.id.popularMovies_recycler))
                .perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));

        onView(withText(movieTitle)).check(matches(isDisplayed()));
        onView(withText(movieDescription)).check(matches(isDisplayed()));
    }

    private void givenAddedStreamToBackend(long movieId, String posterPath) {
        ApiMoviePoster poster = new ApiMoviePoster();
        poster.movieId = movieId;
        poster.posterPath = posterPath;

        backend.addToPopularStream(poster);
    }

    private void givenBackendReturnsMovieDetails(long movieId, String movieTitle, String movieOverview, String posterPath) {
        ApiMovieDetails apiMovieDetails = new ApiMovieDetails();
        apiMovieDetails.originalTitle = movieTitle;
        apiMovieDetails.movieId = movieId;
        apiMovieDetails.overview = movieOverview;
        apiMovieDetails.posterPath = posterPath;
        backend.addMovieDetails(movieId, apiMovieDetails);
    }
}

@@ -1130,60 +1130,254 @@
 ate 
-FakeBackend backend = new FakeBackend();%0A%0A    public
+static final long MOVIE_ID = 293660L;%0A    private static final String POSTER_PATH = %22/deadpool.jpg%22;%0A    private static final String MOVIE_TITLE = %22Deadpool%22;%0A    private static final String MOVIE_DESCRIPTION = %22Awesome movie%22;%0A%0A    private final
  Act
@@ -1797,16 +1797,76 @@
     %7D;%0A%0A
+    private final FakeBackend backend = new FakeBackend();%0A%0A
     @Tes
@@ -1945,313 +1945,162 @@
     
-long movieId = 293660L;%0A        String posterPath = %22/deadpool.jpg%22;%0A        String movieTitle = %22Deadpool%22;%0A        String movieDescription = %22Awesome movie%22;%0A        givenAddedStreamToBackend(movieId, posterPath);%0A        givenBackendReturnsMovieDetails(movieId, movieTitle, movieDescription, posterPath
+givenBackendReturnsPopularStreamWith(MOVIE_ID, POSTER_PATH);%0A        givenBackendReturnsMovieDetails(MOVIE_ID, MOVIE_TITLE, MOVIE_DESCRIPTION, POSTER_PATH
 );%0A 
@@ -2293,26 +2293,27 @@
 ithText(
-movieTitle
+MOVIE_TITLE
 )).check
@@ -2366,24 +2366,25 @@
 ext(
-movieDescription
+MOVIE_DESCRIPTION
 )).c
@@ -2446,28 +2446,39 @@
 iven
-AddedStreamToBackend
+BackendReturnsPopularStreamWith
 (lon
@@ -2637,17 +2637,16 @@
 erPath;%0A
-%0A
         

make the test pretty