package com.wbrawner.simplemarkdown.view.activity;


import android.Manifest;
import android.support.test.espresso.DataInteraction;
import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.rule.GrantPermissionRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import com.wbrawner.simplemarkdown.R;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.InstrumentationRegistry.getInstrumentation;
import static android.support.test.espresso.Espresso.onData;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.Espresso.openActionBarOverflowOrOptionsMenu;
import static android.support.test.espresso.Espresso.pressBack;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
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.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.is;

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

    @Rule
    public ActivityTestRule<SplashActivity> mActivityTestRule = new ActivityTestRule<>(SplashActivity.class);

    @Rule
    public GrantPermissionRule permissionRule = GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE);

    @Test
    public void autosaveTest() {
        String dummyFileName = "dummy-autosave.md";
        String realFileName = "test-autosave.md";
        String testText = "This should be automatically saved";

        // Create a dummy file that we'll later use to provoke the autosave
        saveFile(dummyFileName);

        // Then create our actual file that we expect to be automatically saved.
        saveFile(realFileName);

        ViewInteraction appCompatEditText3 = onView(
                allOf(withId(R.id.markdown_edit),
                        childAtPosition(
                                withParent(withId(R.id.pager)),
                                0),
                        isDisplayed()));
        appCompatEditText3.perform(click());

        ViewInteraction appCompatEditText4 = onView(
                allOf(withId(R.id.markdown_edit),
                        childAtPosition(
                                withParent(withId(R.id.pager)),
                                0),
                        isDisplayed()));
        appCompatEditText4.perform(replaceText(testText), closeSoftKeyboard());

        // Jump back to the dummy file. This should provoke the autosave
        openFile(dummyFileName);

        ViewInteraction editText = onView(
                allOf(withId(
                        R.id.markdown_edit),
                        childAtPosition(
                                withParent(withId(R.id.pager)), 0),
                        isDisplayed())
        );

        // Assert that the text is empty
        editText.check(matches(withText("")));

        // Then re-open the actual file
        openFile(realFileName);

        // And assert that we have our expected text (a newline is appended upon reading the file
        // so we'll need to account for that here as well)
        editText.check(matches(withText(testText + "\n")));
    }

    private void saveFile(String fileName) {
        openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());

        ViewInteraction appCompatTextView = onView(
                allOf(withId(R.id.title), withText("Save"),
                        childAtPosition(
                                childAtPosition(
                                        withClassName(is("android.support.v7.view.menu.ListMenuItemView")),
                                        0),
                                0),
                        isDisplayed()));
        appCompatTextView.perform(click());

        ViewInteraction appCompatEditText = onView(
                allOf(withId(R.id.file_name),
                        childAtPosition(
                                childAtPosition(
                                        withId(android.R.id.content),
                                        0),
                                3),
                        isDisplayed()));
        appCompatEditText.perform(replaceText(fileName));

        appCompatEditText.perform(closeSoftKeyboard());

        ViewInteraction appCompatButton = onView(
                allOf(withId(R.id.button_save),
                        childAtPosition(
                                childAtPosition(
                                        withId(android.R.id.content),
                                        0),
                                4),
                        isDisplayed()));
        appCompatButton.perform(click());
    }

    private void openFile(String fileName) {
        openActionBarOverflowOrOptionsMenu(getInstrumentation().getTargetContext());

        ViewInteraction openMenuItem = onView(
                allOf(withId(R.id.title), withText("Open"),
                        childAtPosition(
                                childAtPosition(
                                        withClassName(is("android.support.v7.view.menu.ListMenuItemView")),
                                        0),
                                0),
                        isDisplayed()));
        openMenuItem.perform(click());

        onView(withText(fileName))
                .perform(click());
    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}

@@ -1978,22 +1978,20 @@
 estRule%3C
-Splash
+Main
 Activity
@@ -2039,14 +2039,12 @@
 e%3C%3E(
-Splash
+Main
 Acti

Use correct activity in AutosaveTest