package com.github.ayltai.newspaper;

import android.app.Instrumentation;
import android.content.Intent;
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.intent.Intents;
import android.support.test.espresso.intent.matcher.IntentMatchers;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.filters.SmallTest;
import android.support.test.runner.AndroidJUnit4;

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

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

@SmallTest
@RunWith(AndroidJUnit4.class)
public final class DetailsTest extends BaseTest {
    @Test
    public void detailsTest() {
        Espresso.onView(MoreTestUtils.first(Matchers.allOf(
            ViewMatchers.withClassName(Matchers.is("com.github.ayltai.newspaper.widget.SmartRecyclerView")),
            ViewMatchers.withId(R.id.recyclerView))))
            .perform(RecyclerViewActions.actionOnItemAtPosition(2, ViewActions.click()));

        MoreTestUtils.sleep(MoreTestUtils.DURATION_SHORT);

        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.avatar),
            ViewMatchers.isDisplayed()))
            .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));

        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.source),
            ViewMatchers.isDisplayed()))
            .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));

        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.action_bookmark),
            ViewMatchers.withContentDescription("Bookmark"),
            ViewMatchers.isDisplayed()))
            .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));

        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.action_share),
            ViewMatchers.withContentDescription("Share"),
            ViewMatchers.isDisplayed()))
            .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));

        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.title),
            ViewMatchers.withParent(Matchers.allOf(
                ViewMatchers.withClassName(Matchers.is("android.widget.LinearLayout")),
                ViewMatchers.withParent(ViewMatchers.withId(R.id.container))
            )),
            ViewMatchers.isDisplayed()))
            .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));

        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.description),
            ViewMatchers.isDisplayed()))
            .check(ViewAssertions.matches(ViewMatchers.isDisplayed()));

        final Matcher<Intent> viewOnWebIntent = Matchers.allOf(
            IntentMatchers.hasAction(Intent.ACTION_CHOOSER),
            IntentMatchers.hasExtra(Matchers.is(Intent.EXTRA_INTENT), IntentMatchers.hasAction(Intent.ACTION_VIEW)));

        Intents.init();
        Intents.intending(viewOnWebIntent).respondWith(new Instrumentation.ActivityResult(0, null));

        // Clicks Share button
        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.action_view_on_web),
            ViewMatchers.withContentDescription("View on web"),
            ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        // Checks that the fired Intent is correct
        Intents.intended(viewOnWebIntent);
        Intents.release();

        final Matcher<Intent> shareIntent = Matchers.allOf(
            IntentMatchers.hasAction(Intent.ACTION_CHOOSER),
            IntentMatchers.hasExtra(Matchers.is(Intent.EXTRA_INTENT), Matchers.allOf(IntentMatchers.hasAction(Intent.ACTION_SEND), IntentMatchers.hasExtraWithKey(Intent.EXTRA_TEXT), IntentMatchers.hasType("text/plain"))));

        Intents.init();
        Intents.intending(shareIntent).respondWith(new Instrumentation.ActivityResult(0, null));

        // Clicks Share button
        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.action_share),
            ViewMatchers.withContentDescription("Share"),
            ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());

        // Checks that the fired Intent is correct
        Intents.intended(shareIntent);
        Intents.release();

        // Clicks Text-to-Speech button
        Espresso.onView(Matchers.allOf(
            ViewMatchers.withId(R.id.action_text_to_speech),
            ViewMatchers.withContentDescription("Text to speech"),
            ViewMatchers.isDisplayed()))
            .perform(ViewActions.click());
    }
}

@@ -2863,778 +2863,8 @@
 );%0A%0A
-        final Matcher%3CIntent%3E viewOnWebIntent = Matchers.allOf(%0A            IntentMatchers.hasAction(Intent.ACTION_CHOOSER),%0A            IntentMatchers.hasExtra(Matchers.is(Intent.EXTRA_INTENT), IntentMatchers.hasAction(Intent.ACTION_VIEW)));%0A%0A        Intents.init();%0A        Intents.intending(viewOnWebIntent).respondWith(new Instrumentation.ActivityResult(0, null));%0A%0A        // Clicks Share button%0A        Espresso.onView(Matchers.allOf(%0A            ViewMatchers.withId(R.id.action_view_on_web),%0A            ViewMatchers.withContentDescription(%22View on web%22),%0A            ViewMatchers.isDisplayed()))%0A            .perform(ViewActions.click());%0A%0A        // Checks that the fired Intent is correct%0A        Intents.intended(viewOnWebIntent);%0A        Intents.release();%0A%0A
     

Remove test for "View on web" to avoid conflict with "Share"