/*
 * Copyright (C) 2015 Karumi.
 *
 * 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.karumi.katasuperheroes;

import android.support.test.InstrumentationRegistry;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import com.karumi.katasuperheroes.di.MainComponent;
import com.karumi.katasuperheroes.di.MainModule;
import com.karumi.katasuperheroes.model.SuperHero;
import com.karumi.katasuperheroes.model.SuperHeroesRepository;
import com.karumi.katasuperheroes.ui.view.MainActivity;
import it.cosenonjaviste.daggermock.DaggerMockRule;
import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;

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;
import static org.mockito.Mockito.when;

@RunWith(AndroidJUnit4.class) @LargeTest public class MainActivityTest {

  @Rule public DaggerMockRule<MainComponent> daggerRule =
      new DaggerMockRule<>(MainComponent.class, new MainModule()).set(
          new DaggerMockRule.ComponentSetter<MainComponent>() {
            @Override public void setComponent(MainComponent component) {
              SuperHeroesApplication app =
                  (SuperHeroesApplication) InstrumentationRegistry.getInstrumentation()
                      .getTargetContext()
                      .getApplicationContext();
              app.setComponent(component);
            }
          });

  @Rule public ActivityTestRule<MainActivity> activityRule =
      new ActivityTestRule<>(MainActivity.class, false, false);

  @Mock SuperHeroesRepository repository;

  @Test public void showsEmptyCaseIfThereAreNoSuperHeroes() {
    when(repository.getAll()).thenReturn(Collections.<SuperHero>emptyList());

    activityRule.launchActivity(null);

    onView(withText("¯\\_(ツ)_/¯")).check(matches(isDisplayed()));
  }
}
@@ -1184,16 +1184,68 @@
 ctions;%0A
+import java.util.LinkedList;%0Aimport java.util.List;%0A
 import o
@@ -1748,16 +1748,76 @@
 Test %7B%0A%0A
+  private static final int ANY_NUMBER_OF_SUPER_HEROES = 3;%0A%0A
   @Rule 
@@ -2607,186 +2607,1278 @@
     
-when(repository.getAll()).thenReturn(Collections.%3CSuperHero%3EemptyList());%0A%0A    activityRule.launchActivity(null);%0A%0A    onView(withText(%22%C2%AF%5C%5C_(%E3%83%84)_/%C2%AF%22)).check(matches(isDisplayed())
+givenThereAreNoSuperHeroes();%0A%0A    startActivity();%0A%0A    onView(withText(%22%C2%AF%5C%5C_(%E3%83%84)_/%C2%AF%22)).check(matches(isDisplayed()));%0A  %7D%0A%0A  @Test public void showsSuperHeroesNameIfThereAreSuperHeroes() %7B%0A    List%3CSuperHero%3E superHeroes = givenThereAreSuperHeroes(ANY_NUMBER_OF_SUPER_HEROES);%0A%0A    startActivity();%0A%0A    for (SuperHero superHero : superHeroes) %7B%0A      onView(withText(superHero.getName())).check(matches(isDisplayed()));%0A    %7D%0A  %7D%0A%0A  private List%3CSuperHero%3E givenThereAreSuperHeroes(int numberOfSuperHeroes) %7B%0A    List%3CSuperHero%3E superHeroes = new LinkedList%3C%3E();%0A    for (int i = 0; i %3C numberOfSuperHeroes; i++) %7B%0A      String superHeroName = %22SuperHero - %22 + i;%0A      String superHeroPhoto = %22https://i.annihil.us/u/prod/marvel/i/mg/c/60/55b6a28ef24fa.jpg%22;%0A      boolean isAvenger = i %25 2 == 0;%0A      String superHeroDescription = %22Description Super Hero - %22 + i;%0A      superHeroes.add(%0A          new SuperHero(superHeroName, superHeroPhoto, isAvenger, superHeroDescription));%0A    %7D%0A    when(repository.getAll()).thenReturn(superHeroes);%0A    return superHeroes;%0A  %7D%0A%0A  private void givenThereAreNoSuperHeroes() %7B%0A    when(repository.getAll()).thenReturn(Collections.%3CSuperHero%3EemptyList());%0A  %7D%0A%0A  private void startActivity() %7B%0A    activityRule.launchActivity(null
 );%0A 

Test shows super heroes name inside the list view