package org.stepic.droid.ui.activities;


import android.support.test.espresso.ViewInteraction;
import android.support.test.espresso.contrib.DrawerActions;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

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 org.stepic.droid.R;

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.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.pressImeActionButton;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.action.ViewActions.scrollTo;
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.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;

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

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

    @Test
    public void loginActivityTest() {

        onView(withId(R.id.sign_in_with_email))
                .perform(scrollTo(), click());

        onView(withId(R.id.email_et))
                .perform(scrollTo(), replaceText("test@stepik.org"), closeSoftKeyboard());

        onView(allOf(withId(R.id.email_et), withText("test@stepik.org")))
                .perform(pressImeActionButton());

        onView(withId(R.id.password_et))
                .perform(scrollTo(), replaceText("qwerty123"), closeSoftKeyboard());

        onView(withId(R.id.login_button_layout))
                .perform(scrollTo(), click());

        onView(withId(R.id.drawer))
                .perform(DrawerActions.open());

        ViewInteraction checkedTextView = onView(
                allOf(withId(R.id.design_menu_item_text),
                        childAtPosition(
                                childAtPosition(withId(R.id.design_navigation_view), 1),
                                0)));
        checkedTextView.check(matches(isDisplayed()));

    }

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

@@ -1668,18 +1668,15 @@
 sign
-_in_with_e
+InWithE
 mail
@@ -1753,23 +1753,24 @@
 Id(R.id.
-email_e
+loginTex
 t))%0A    
@@ -1894,15 +1894,16 @@
 .id.
-email_e
+loginTex
 t), 
@@ -2022,10 +2022,15 @@
 word
-_e
+EditTex
 t))%0A
@@ -2151,22 +2151,14 @@
 ogin
-_b
+B
 utton
-_layout
 ))%0A 

fix naming of id in LoginActivityTest