package org.gdg.frisbee.android.activity;

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

import org.gdg.frisbee.android.R;
import org.gdg.frisbee.android.api.model.Chapter;
import org.gdg.frisbee.android.chapter.MainActivity;
import org.gdg.frisbee.android.utils.PrefUtils;
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.action.ViewActions.swipeLeft;
import static android.support.test.espresso.action.ViewActions.swipeRight;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isRoot;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withSpinnerText;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.gdg.frisbee.android.activity.util.OrientationChangeAction.orientationLandscape;
import static org.gdg.frisbee.android.activity.util.OrientationChangeAction.orientationPortrait;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
    public static final Chapter CHAPTER_ISTANBUL = new Chapter("Istanbul", "100514812580249787371");
    public static final Chapter CHAPTER_BRUSSELS = new Chapter("Brussels", "105068877693379070381");

    @Rule
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<MainActivity>(MainActivity.class) {
        @Override
        protected void beforeActivityLaunched() {
            PrefUtils.setInitialSettings(InstrumentationRegistry.getTargetContext(), false);
            PrefUtils.setHomeChapter(InstrumentationRegistry.getTargetContext(), CHAPTER_BRUSSELS);
        }
    };

    @Test
    public void supportsChapterSwapping() {

        onView(withId(R.id.actionbar_spinner)).perform(click());
        onData(allOf(is(instanceOf(Chapter.class)), is(CHAPTER_ISTANBUL)))
            .perform(click());
        onView(withId(R.id.actionbar_spinner))
            .check(matches(withSpinnerText(CHAPTER_ISTANBUL.toString())));

        onView(withId(R.id.pager)).perform(swipeRight());
        onView(withId(R.id.tagline)).check(matches(withText(containsString(CHAPTER_ISTANBUL.toString()))));
        onView(withId(R.id.about)).check(matches(withText(containsString(CHAPTER_ISTANBUL.toString()))));
        onView(withId(R.id.pager)).perform(swipeLeft());
    }


    @Test
    public void keepsChapterOnOrientationChange() {

        onView(isRoot()).perform(orientationPortrait());

        onView(withId(R.id.actionbar_spinner)).perform(click());
        onData(allOf(is(instanceOf(Chapter.class)), is(CHAPTER_ISTANBUL)))
            .perform(click());
        onView(withId(R.id.actionbar_spinner))
            .check(matches(withSpinnerText(CHAPTER_ISTANBUL.toString())));


        onView(isRoot()).perform(orientationLandscape());

        onView(withId(R.id.actionbar_spinner))
            .check(matches(withSpinnerText(CHAPTER_ISTANBUL.toString())));
    }
}

@@ -89,16 +89,70 @@
 gistry;%0A
+import android.support.test.espresso.ViewInteraction;%0A
 import a
@@ -243,16 +243,58 @@
 dJUnit4;
+%0Aimport android.support.v7.widget.Toolbar;
 %0A%0Aimport
@@ -971,16 +971,181 @@
 atches;%0A
+import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom;%0Aimport static android.support.test.espresso.matcher.ViewMatchers.isDescendantOfA;%0A
 import s
@@ -1286,90 +1286,8 @@
 Id;%0A
-import static android.support.test.espresso.matcher.ViewMatchers.withSpinnerText;%0A
 impo
@@ -2477,39 +2477,24 @@
 View
-(withId(R.id.actionbar_spinner)
+ChapterSwitcher(
 ).pe
@@ -2633,39 +2633,24 @@
 View
-(withId(R.id.actionbar_spinner)
+ChapterSwitcher(
 )%0A  
@@ -2670,39 +2670,32 @@
 eck(matches(with
-Spinner
 Text(CHAPTER_IST
@@ -3111,25 +3111,24 @@
 nChange() %7B%0A
-%0A
         onVi
@@ -3191,39 +3191,24 @@
 View
-(withId(R.id.actionbar_spinner)
+ChapterSwitcher(
 ).pe
@@ -3347,53 +3347,25 @@
 View
-(withId(R.id.actionbar_spinner))%0A            
+ChapterSwitcher()
 .che
@@ -3371,39 +3371,32 @@
 eck(matches(with
-Spinner
 Text(CHAPTER_IST
@@ -3417,17 +3417,16 @@
 ())));%0A%0A
-%0A
         
@@ -3494,53 +3494,25 @@
 View
-(withId(R.id.actionbar_spinner))%0A            
+ChapterSwitcher()
 .che
@@ -3530,15 +3530,8 @@
 with
-Spinner
 Text
@@ -3568,11 +3568,187 @@
 );%0A    %7D
+%0A%0A    private static ViewInteraction onViewChapterSwitcher() %7B%0A        return onView(allOf(withId(android.R.id.text1), isDescendantOfA(isAssignableFrom(Toolbar.class))));%0A    %7D
 %0A%7D%0A

Fix UI tests in MainActivity