package com.github.ayltai.newspaper;

import android.app.Activity;
import android.support.test.InstrumentationRegistry;
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.contrib.RecyclerViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.WindowManager;

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

import com.github.ayltai.newspaper.util.MoreViewMatchers;
import com.github.ayltai.newspaper.util.SuppressFBWarnings;

@RunWith(AndroidJUnit4.class)
@LargeTest
public final class MainActivityTest {
    @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
    @Rule
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);

    @SuppressWarnings("deprecation")
    @Before
    public void setUp() {
        final Activity activity = this.activityRule.getActivity();

        // Makes sure the device is unlocked
        activity.runOnUiThread(() -> activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON));
    }

    @Test
    public void swipeThroughAllTabs() {
        final String[] shortCategories = InstrumentationRegistry.getTargetContext().getResources().getStringArray(R.array.pref_category_short_entries);
        final String[] longCategories  = InstrumentationRegistry.getTargetContext().getResources().getStringArray(R.array.pref_category_entries);

        for (int i = 0; i < shortCategories.length; i++) {
            final String longCategory = longCategories[i];

            Espresso.onView(ViewMatchers.withId(R.id.collapsingToolbarLayout))
                .check(ViewAssertions.matches(MoreViewMatchers.withTitle(longCategory)));

            Espresso.onView(Matchers.allOf(ViewMatchers.withText(shortCategories[i]), ViewMatchers.isDescendantOfA(ViewMatchers.withId(R.id.tabLayout))))
                .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));

            Espresso.onView(ViewMatchers.withId(R.id.navigate_next))
                .perform(ViewActions.click());
        }
    }

    @Test
    public void openFirstItem() {
        // Switches to "HK" tab
        Espresso.onView(ViewMatchers.withId(R.id.navigate_next))
            .perform(ViewActions.click());

        // Opens first item
        Espresso.onView(Matchers.allOf(ViewMatchers.withId(R.id.recyclerView), ViewMatchers.isDisplayed()))
            .perform(RecyclerViewActions.actionOnItemAtPosition(0, ViewActions.click()));

        // Asserts that the title is expected
        Espresso.onView(ViewMatchers.withId(R.id.title))
            .check(ViewAssertions.matches(ViewMatchers.withText("馬時亨冀3月就票價機制達協議 不覺得有驚天動地改變")));
    }
}

@@ -1536,17 +1536,34 @@
 ic void 
-s
+openFirstItemThenS
 wipeThro
@@ -1573,24 +1573,804 @@
 AllTabs() %7B%0A
+        // Switches to %22HK%22 tab%0A        Espresso.onView(ViewMatchers.withId(R.id.navigate_next))%0A            .perform(ViewActions.click());%0A%0A        // Opens first item%0A        Espresso.onView(Matchers.allOf(ViewMatchers.withId(R.id.recyclerView), ViewMatchers.isDisplayed()))%0A            .perform(RecyclerViewActions.actionOnItemAtPosition(0, ViewActions.click()));%0A%0A        // Asserts that the title is expected%0A        Espresso.onView(ViewMatchers.withId(R.id.title))%0A            .check(ViewAssertions.matches(ViewMatchers.withText(%22%E9%A6%AC%E6%99%82%E4%BA%A8%E5%86%803%E6%9C%88%E5%B0%B1%E7%A5%A8%E5%83%B9%E6%A9%9F%E5%88%B6%E9%81%94%E5%8D%94%E8%AD%B0 %E4%B8%8D%E8%A6%BA%E5%BE%97%E6%9C%89%E9%A9%9A%E5%A4%A9%E5%8B%95%E5%9C%B0%E6%94%B9%E8%AE%8A%22)));%0A%0A        Espresso.pressBack();%0A%0A        // Switches to %22Top%22 tab%0A        Espresso.onView(ViewMatchers.withId(R.id.navigate_previous))%0A            .perform(ViewActions.click());%0A%0A        // Swipes through all tabs%0A
         fina
@@ -3311,625 +3311,7 @@
    %7D
-%0A%0A    @Test%0A    public void openFirstItem() %7B%0A        // Switches to %22HK%22 tab%0A        Espresso.onView(ViewMatchers.withId(R.id.navigate_next))%0A            .perform(ViewActions.click());%0A%0A        // Opens first item%0A        Espresso.onView(Matchers.allOf(ViewMatchers.withId(R.id.recyclerView), ViewMatchers.isDisplayed()))%0A            .perform(RecyclerViewActions.actionOnItemAtPosition(0, ViewActions.click()));%0A%0A        // Asserts that the title is expected%0A        Espresso.onView(ViewMatchers.withId(R.id.title))%0A            .check(ViewAssertions.matches(ViewMatchers.withText(%22%E9%A6%AC%E6%99%82%E4%BA%A8%E5%86%803%E6%9C%88%E5%B0%B1%E7%A5%A8%E5%83%B9%E6%A9%9F%E5%88%B6%E9%81%94%E5%8D%94%E8%AD%B0 %E4%B8%8D%E8%A6%BA%E5%BE%97%E6%9C%89%E9%A9%9A%E5%A4%A9%E5%8B%95%E5%9C%B0%E6%94%B9%E8%AE%8A%22)));%0A    %7D
 %0A%7D%0A

Update instrumental tests to avoid using a closed Realm instance