package uk.co.tekkies.readings.test.contentLocationActivity;

import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.net.Uri;
import android.support.test.espresso.intent.rule.IntentsTestRule;
import android.support.test.runner.AndroidJUnit4;

import junit.framework.Assert;

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

import java.io.File;

import uk.co.tekkies.readings.R;
import uk.co.tekkies.readings.activity.ContentLocationActivity;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.intent.Intents.intending;
import static android.support.test.espresso.intent.matcher.IntentMatchers.toPackage;
import static android.support.test.espresso.matcher.ViewMatchers.withId;

@RunWith(AndroidJUnit4.class)
public class ContentLocationActivityTest{


    @Rule
    public IntentsTestRule<ContentLocationActivity> mActivityRule = new IntentsTestRule<>(
            ContentLocationActivity.class);

    @Before
    public void stubAllExternalIntents() {
        Intent resultData = new Intent();
        Uri uri = Uri.fromFile(new File("/storage/emulated/0/readings-samples"));
        resultData.setData(uri);
        Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, resultData);
        intending(toPackage("com.estrongs.android.pop")).respondWith(result);
    }

    @Test
    public void testContentIsFound() {
        onView(withId(R.id.button_browse)).perform(click());


    }

}

@@ -170,15 +170,102 @@
 oid.
-net.Uri
+content.SharedPreferences;%0Aimport android.net.Uri;%0Aimport android.preference.PreferenceManager
 ;%0Aim
@@ -377,16 +377,42 @@
 dJUnit4;
+%0Aimport android.view.View;
 %0A%0Aimport
@@ -437,16 +437,45 @@
 ssert;%0A%0A
+import org.hamcrest.Matcher;%0A
 import o
@@ -687,24 +687,103 @@
 nActivity;%0A%0A
+import static android.support.test.InstrumentationRegistry.getInstrumentation;%0A
 import stati
@@ -901,16 +901,94 @@
 .click;%0A
+import static android.support.test.espresso.assertion.ViewAssertions.matches;%0A
 import s
@@ -1199,22 +1199,175 @@
 atchers.
-withId
+isDisplayed;%0Aimport static android.support.test.espresso.matcher.ViewMatchers.withId;%0Aimport static android.support.test.espresso.matcher.ViewMatchers.withText
 ;%0A%0A@RunW
@@ -1435,16 +1435,18 @@
 Test%7B%0A%0A%0A
+%0A%0A
     @Rul
@@ -1535,16 +1535,39 @@
 estRule%3C
+ContentLocationActivity
 %3E(%0A     
@@ -1607,66 +1607,365 @@
 ass)
-;%0A%0A    @Before%0A    public void stubAllExternalIntents
+ %7B%0A        @Override%0A        protected void beforeActivityLaunched() %7B%0A            super.beforeActivityLaunched();%0A%0A            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getInstrumentation().getTargetContext());%0A            preferences.edit().clear().commit();%0A%0A        %7D%0A    %7D;%0A%0A    @Before%0A    public void setup
 () %7B%0A
+%0A%0A%0A
     
@@ -2429,18 +2429,132 @@
 ck());%0A%0A
+        onView(withText(%22Found: /storage/emulated/0/readings-samples/KjvScourby2%22)).check(matches(isDisplayed()));
 %0A    %7D%0A%0A
 %7D%0A
@@ -2551,10 +2551,12 @@
 %0A    %7D%0A%0A
+%0A%0A
 %7D%0A

Clear prefs then confirm content found