package com.tobi.movies.posterdetails;

import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.tobi.movies.EspressoDependencies;
import com.tobi.movies.MovieApplication;
import com.tobi.movies.backend.ConfigurableBackend;

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

import static android.support.test.espresso.Espresso.onView;
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;

@RunWith(AndroidJUnit4.class)
public class MovieDetailsActivityTest {

    private ConfigurableBackend backend = new ConfigurableBackend();

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

    @Test
    public void shouldShowMovieTitle() throws Exception {
        long movieId = 293660L;
        String movieTitle = "Deadpool";
        String movieDescription = "Such an awesome movie!";
        String posterPath = "deadpool.jpg";
        givenBackendReturnsMovieDetails(movieId, movieTitle, movieDescription, posterPath);

        Intent intent = MovieDetailsActivity.createTestIntentFor(movieId);
        rule.launchActivity(intent);

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

    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(apiMovieDetails);
    }
}

@@ -37,39 +37,8 @@
 s;%0A%0A
-import android.content.Intent;%0A
 impo
@@ -325,16 +325,57 @@
 Backend;
+%0Aimport com.tobi.movies.utils.MovieRobot;
 %0A%0Aimport
@@ -429,370 +429,326 @@
 h;%0A%0A
-import static android.support.test.espresso.Espresso.onView;%0Aimport static android.support.test.espresso.assertion.ViewAssertions.matches;%0Aimport static android.support.
+@RunWith(AndroidJUnit4.class)%0Apublic class MovieDetailsActivityTest %7B%0A%0A    private static final long MOVIE_ID = 293660L;%0A    priva
 te
+ 
 st
-.espresso.matcher.ViewMatchers.isDisplayed;%0Aimport static android.support.test.espresso.matcher.ViewMatchers.withText;%0A%0A@RunWith(AndroidJUnit4.class)%0Apublic class MovieDetailsActivityTest %7B
+atic final String MOVIE_TITLE = %22Deadpool%22;%0A    private static final String MOVIE_DESCRIPTION = %22Such an awesome movie!%22;%0A    private static final String POSTER_PATH = %22deadpool.jpg%22;
 %0A%0A  
@@ -820,21 +820,22 @@
 ;%0A%0A    p
-ublic
+rivate
  Activit
@@ -1328,567 +1328,458 @@
     
-long movieId = 293660L;%0A        String movieTitle = %22Deadpool%22;%0A        String movieDescription = %22Such an awesome movie!%22;%0A        String posterPath = %22deadpool.jpg%22;%0A        givenBackendReturnsMovieDetails(movieId, movieTitle, movieDescription, posterPath);%0A%0A        Intent intent = MovieDetailsActivity.createTestIntentFor(movieId);%0A        rule.launchActivity(intent);%0A%0A        onView(withText(movieTitle)).check(matches(isDisplayed()));%0A        onView(withText(movieDescription)).check(matches(isDisplayed()));%0A    %7D%0A%0A    private void givenBackendReturns
+ApiMovieDetails apiMovieDetails = createApiMovieDetails(MOVIE_ID, MOVIE_TITLE, MOVIE_DESCRIPTION, POSTER_PATH);%0A%0A        MovieRobot%0A                .createRobot(backend, rule)%0A                .addRemoteMovieDetails(apiMovieDetails)%0A                .launchDetailsScreen(MOVIE_ID)%0A                .checkMovieTitleIsDisplayed(MOVIE_TITLE)%0A                .checkMovieDescriptionIsDisplayed(MOVIE_DESCRIPTION);%0A    %7D%0A%0A    private ApiMovieDetails createApi
 Movi
@@ -2122,24 +2122,25 @@
 erPath;%0A
+%0A
         
 backend.
@@ -2135,32 +2135,15 @@
     
-backend.addMovieDetails(
+return 
 apiM
@@ -2153,17 +2153,16 @@
 eDetails
-)
 ;%0A    %7D%0A

Use MovieRobot in MovieDetailsActivityTest