package com.github.ayltai.newspaper;

import android.support.test.espresso.Espresso;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.assertion.ViewAssertions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.filters.MediumTest;
import android.support.test.runner.AndroidJUnit4;

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

import com.google.android.libraries.cloudtesting.screenshots.ScreenShotter;

import com.github.ayltai.newspaper.util.MoreTestUtils;

@MediumTest
@RunWith(AndroidJUnit4.class)
public final class ViewStyleTest extends BaseTest {
    @Test
    public void viewStyleTest() {
        // Clicks More button
        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.action_more),
            ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        MoreTestUtils.sleep(MoreTestUtils.DURATION_SHORT);

        // Clicks Settings button
        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.action_settings),
            ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        MoreTestUtils.sleep(MoreTestUtils.DURATION_SHORT);

        // Clicks Settings tab
        Espresso.onView(Matchers.allOf(
            ViewMatchers.withText("Settings"),
            ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        MoreTestUtils.sleep(MoreTestUtils.DURATION_SHORT);

        ScreenShotter.takeScreenshot(this.getClass().getSimpleName() + ".settings.cozy", this.testRule.getActivity());

        // Checks that Cozy view style is checked
        Espresso.onView(Matchers.allOf(
            MoreTestUtils.childAtPosition(Matchers.allOf(
                ViewMatchers.withId(R.id.linearLayout),
                MoreTestUtils.childAtPosition(ViewMatchers.withParent(Matchers.allOf(
                    ViewMatchers.withId(R.id.viewPager),
                    MoreTestUtils.childAtPosition(MoreTestUtils.childAtPosition(MoreTestUtils.childAtPosition(Matchers.allOf(
                        ViewMatchers.withId(R.id.design_bottom_sheet),
                        MoreTestUtils.childAtPosition(ViewMatchers.withId(R.id.coordinator), 1)), 0), 0), 1))), 0)), 0),
            ViewMatchers.isDisplayed()))
            .check(ViewAssertions.matches(MoreTestUtils.isChecked()));

        // De-selects Cozy view style
        Espresso.onView(Matchers.allOf(
            ViewMatchers.withText("Cozy layout"),
            ViewMatchers.withParent(ViewMatchers.withId(R.id.linearLayout)),
            ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        // Clicks Apply Changes
        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.action_ok),
            ViewMatchers.withText("Apply changes"),
            ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        MoreTestUtils.sleep(MoreTestUtils.DURATION_MEDIUM);

        // Clicks More button
        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.action_more),
            ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        MoreTestUtils.sleep(MoreTestUtils.DURATION_SHORT);

        // Clicks Settings button
        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.action_settings),
            ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        MoreTestUtils.sleep(MoreTestUtils.DURATION_SHORT);

        // Clicks Settings tab
        Espresso.onView(Matchers.allOf(
            ViewMatchers.withText("Settings"),
            ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        MoreTestUtils.sleep(MoreTestUtils.DURATION_SHORT);

        ScreenShotter.takeScreenshot(this.getClass().getSimpleName() + ".settings.compact", this.testRule.getActivity());

        // Checks that Cozy view style is unchecked
        Espresso.onView(Matchers.allOf(
            MoreTestUtils.childAtPosition(Matchers.allOf(
                ViewMatchers.withId(R.id.linearLayout),
                MoreTestUtils.childAtPosition(ViewMatchers.withParent(Matchers.allOf(
                    ViewMatchers.withId(R.id.viewPager),
                    MoreTestUtils.childAtPosition(MoreTestUtils.childAtPosition(MoreTestUtils.childAtPosition(Matchers.allOf(
                        ViewMatchers.withId(R.id.design_bottom_sheet),
                        MoreTestUtils.childAtPosition(ViewMatchers.withId(R.id.coordinator), 1)), 0), 0), 1))), 0)), 0),
            ViewMatchers.isDisplayed()))
            .check(ViewAssertions.matches(MoreTestUtils.isNotChecked()));
    }
}

@@ -4731,16 +4731,307 @@
 ked()));
+%0A%0A        // Restores to Cozy view style%0A        Espresso.onView(Matchers.allOf(%0A            ViewMatchers.withText(%22Cozy layout%22),%0A            ViewMatchers.withParent(ViewMatchers.withId(R.id.linearLayout)),%0A            ViewMatchers.isDisplayed()))%0A            .perform(ViewActions.click());
 %0A    %7D%0A%7D

Fix instrumented tests