diff --git a/app/src/androidTest/java/com/wbrawner/simplemarkdown/view/activity/ExplorerActivityTest.java b/app/src/androidTest/java/com/wbrawner/simplemarkdown/view/activity/ExplorerActivityTest.java new file mode 100644 index 0000000..3970862 --- /dev/null +++ b/app/src/androidTest/java/com/wbrawner/simplemarkdown/view/activity/ExplorerActivityTest.java @@ -0,0 +1,79 @@ +package com.wbrawner.simplemarkdown.view.activity; + +import android.Manifest; +import android.content.Intent; +import android.support.test.rule.ActivityTestRule; +import android.support.test.rule.GrantPermissionRule; +import android.widget.TextView; + +import com.wbrawner.simplemarkdown.R; +import com.wbrawner.simplemarkdown.utility.Constants; + +import org.junit.Rule; +import org.junit.Test; + +import java.util.HashMap; + +import static android.support.test.espresso.Espresso.onData; +import static android.support.test.espresso.Espresso.onView; +import static android.support.test.espresso.action.ViewActions.click; +import static android.support.test.espresso.assertion.ViewAssertions.matches; +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.instanceOf; +import static org.hamcrest.Matchers.is; + +public class ExplorerActivityTest { + @Rule + public ActivityTestRule explorerActivityActivityTestRule + = new ActivityTestRule<>(ExplorerActivity.class, false, false); + @Rule + public GrantPermissionRule readFilesPermissionRule = + GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE); + @Rule + public GrantPermissionRule writeFilesPermissionRule = + GrantPermissionRule.grant(Manifest.permission.WRITE_EXTERNAL_STORAGE); + + @Test + public void firstItemInDirectoryIsGoUpTest() { + Intent startIntent = new Intent(); + startIntent.putExtra(Constants.EXTRA_REQUEST_CODE, Constants.REQUEST_OPEN_FILE); + explorerActivityActivityTestRule.launchActivity(startIntent); + // We should start in the Documents directory + onView(allOf(instanceOf(TextView.class), withParent(withId(R.id.toolbar)))) + .check(matches(withText("Documents"))); + + // Check that the first item in the list is our navigate up a directory item and click it + onData(is(instanceOf(HashMap.class))) + .atPosition(0) + .check(matches(withText( + explorerActivityActivityTestRule.getActivity().getString(R.string.directory_up) + ))) + .perform(click()); + + // Check that we're in the parent of the Documents directory + onView(allOf(instanceOf(TextView.class), withParent(withId(R.id.toolbar)))) + .check(matches(withText("0"))); + + // Enter the Android/data/data directory, which should almost certainly have other + // directories, and check that each directory shows the "Go up" option as the first option + // in the list + onView(withText("Android")) + .perform(click()); + onData(is(instanceOf(HashMap.class))) + .atPosition(0) + .check(matches(withText( + explorerActivityActivityTestRule.getActivity().getString(R.string.directory_up) + ))); + + onView(withText("data")) + .perform(click()); + onData(is(instanceOf(HashMap.class))) + .atPosition(0) + .check(matches(withText( + explorerActivityActivityTestRule.getActivity().getString(R.string.directory_up) + ))); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/ExplorerActivity.java b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/ExplorerActivity.java index 504b230..59b2906 100644 --- a/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/ExplorerActivity.java +++ b/app/src/main/java/com/wbrawner/simplemarkdown/view/activity/ExplorerActivity.java @@ -172,13 +172,6 @@ public class ExplorerActivity extends AppCompatActivity { TreeSet> dirs = new TreeSet>((o1, o2) -> ((String) o1.get("name")).compareToIgnoreCase((String) o2.get("name"))) { }; - if (docsDir.getParentFile() != null && docsDir.getParentFile().canRead()) { - HashMap fileHashMap = new HashMap<>(); - fileHashMap.put("name", getString(R.string.directory_up)); - fileHashMap.put("file", docsDir.getParentFile()); - - dirs.add(fileHashMap); - } if (docsDir.listFiles() != null) { for (File file : docsDir.listFiles()) { @@ -202,7 +195,16 @@ public class ExplorerActivity extends AppCompatActivity { } } - List> sortedFiles = new ArrayList<>(dirs); + List> sortedFiles = new ArrayList<>(); + if (docsDir.getParentFile() != null && docsDir.getParentFile().canRead()) { + HashMap fileHashMap = new HashMap<>(); + fileHashMap.put("name", getString(R.string.directory_up)); + fileHashMap.put("file", docsDir.getParentFile()); + + sortedFiles.add(fileHashMap); + } + + sortedFiles.addAll(dirs); sortedFiles.addAll(files); return sortedFiles;