Loading tests/src/com/android/documentsui/FilesActivityUiTest.java +15 −5 Original line number Diff line number Diff line Loading @@ -73,7 +73,7 @@ public class FilesActivityUiTest extends ActivityTest<FilesActivity> { ROOT_1_ID); // Separate logic for "Documents" root, which presence depends on the config setting if (Shared.shouldShowDocumentsRoot(context, new Intent(DocumentsContract.ACTION_BROWSE))) { if (docsRootEnabled()) { bots.roots.assertRootsPresent("Documents"); } else { bots.roots.assertRootsAbsent("Documents"); Loading @@ -87,18 +87,24 @@ public class FilesActivityUiTest extends ActivityTest<FilesActivity> { bots.directory.assertDocumentsPresent("file0.log", "file1.png", "file2.csv"); } public void testLoadsDownloadsDirectoryByDefault() throws Exception { public void testLoadsDefaultDirectory() throws Exception { initTestFiles(); device.waitForIdle(); // Separate logic for "Documents" root, which presence depends on the config setting if (docsRootEnabled()) { bots.main.assertWindowTitle("Documents"); } else { bots.main.assertWindowTitle("Downloads"); } } public void testRootClickSetsWindowTitle() throws Exception { initTestFiles(); bots.roots.openRoot("Downloads"); bots.main.assertWindowTitle("Downloads"); bots.roots.openRoot("Images"); bots.main.assertWindowTitle("Images"); } public void testFilesList_LiveUpdate() throws Exception { Loading Loading @@ -289,4 +295,8 @@ public class FilesActivityUiTest extends ActivityTest<FilesActivity> { device.pressBack(); // to clear the dialog. } private boolean docsRootEnabled() { return Shared.shouldShowDocumentsRoot(context, new Intent(DocumentsContract.ACTION_BROWSE)); } } tests/src/com/android/documentsui/bots/Matchers.java 0 → 100644 +48 −0 Original line number Diff line number Diff line /* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.documentsui.bots; import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 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.withResourceName; import static org.hamcrest.CoreMatchers.allOf; import android.view.View; import android.widget.ImageView; import android.widget.Spinner; import android.widget.Toolbar; import com.android.documentsui.R; import com.android.internal.view.menu.ActionMenuItemView; import org.hamcrest.Matcher; /** * Handy matchers useful for finding stuff in the UI. Use with Espresso testing. */ final class Matchers { static final Matcher<View> TOOLBAR = allOf(isAssignableFrom(Toolbar.class), withId(R.id.toolbar)); static final Matcher<View> SEARCH_MENU = allOf(withId(R.id.menu_search), isDisplayed()); static final Matcher<View> SEARCH_BUTTON = allOf(isAssignableFrom(ImageView.class), withResourceName("search_button")); static final Matcher<View> BREADCRUMB = allOf(isAssignableFrom(Spinner.class), withId(R.id.breadcrumb)); static final Matcher<View> MENU_SEARCH = allOf(isAssignableFrom(ActionMenuItemView.class), withResourceName("menu_search")); } tests/src/com/android/documentsui/bots/UiBot.java +68 −66 Original line number Diff line number Diff line Loading @@ -24,12 +24,10 @@ import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 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.withResourceName; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.is; import android.content.Context; Loading @@ -41,16 +39,11 @@ import android.support.test.uiautomator.UiObject; import android.support.test.uiautomator.UiObject2; import android.support.test.uiautomator.UiObjectNotFoundException; import android.support.test.uiautomator.UiSelector; import android.widget.ImageView; import android.widget.Spinner; import android.widget.Toolbar; import com.android.documentsui.R; import com.android.documentsui.model.DocumentInfo; import com.android.internal.view.menu.ActionMenuItemView; import junit.framework.Assert; import junit.framework.AssertionFailedError; import org.hamcrest.Description; import org.hamcrest.Matcher; Loading @@ -60,29 +53,31 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; import junit.framework.Assert; import junit.framework.AssertionFailedError; /** * A test helper class that provides support for controlling DocumentsUI activities * programmatically, and making assertions against the state of the UI. * * <p>Support for working directly with Roots and Directory view can be found * in the respective bots. * <p> * Support for working directly with Roots and Directory view can be found in the respective bots. */ public class UiBot extends BaseBot { public static final String TARGET_PKG = "com.android.documentsui"; private static final String LAUNCHER_PKG = "com.android.launcher"; public UiBot(UiDevice device, Context context, int timeout) { super(device, context, timeout); } public void assertWindowTitle(String expected) { onView(allOf(isAssignableFrom(Toolbar.class), withId(R.id.toolbar))) onView(Matchers.TOOLBAR) .check(matches(withToolbarTitle(is(expected)))); } public void assertBreadcrumbTitle(String expected) { if (!isTablet()) { onView(allOf(isAssignableFrom(Spinner.class), withId(R.id.breadcrumb))) onView(Matchers.BREADCRUMB) .check(matches(withBreadcrumbTitle(is(expected)))); } } Loading @@ -106,8 +101,8 @@ public class UiBot extends BaseBot { } } public void assertSearchTextFiledAndIcon(boolean searchTextFieldExists, boolean searchIconExists) { public void assertSearchTextFiledAndIcon( boolean searchTextFieldExists, boolean searchIconExists) { assertEquals(searchTextFieldExists, findSearchViewTextField().exists()); boolean searchIconVisible = isSearchIconVisible(); assertEquals(searchIconExists, searchIconVisible); Loading @@ -125,7 +120,7 @@ public class UiBot extends BaseBot { } public void setSearchQuery(String query) throws UiObjectNotFoundException { onView(allOf(withId(R.id.menu_search), isDisplayed())).perform(typeText(query)); onView(Matchers.SEARCH_MENU).perform(typeText(query)); } public UiObject openOverflowMenu() throws UiObjectNotFoundException { Loading Loading @@ -159,11 +154,12 @@ public class UiBot extends BaseBot { try { if (isTablet) { // Tablets use ImageView for its search icon, and has search_button as its res name onView(allOf(isAssignableFrom(ImageView.class), withResourceName("search_button"))) onView(Matchers.SEARCH_BUTTON) .check(matches(isDisplayed())); } else { // Phones use ActionMenuItemView for its search icon, and has menu_search as its res name onView(allOf(isAssignableFrom(ActionMenuItemView.class), withResourceName("menu_search"))) // Phones use ActionMenuItemView for its search icon, and has menu_search as its res // name onView(Matchers.MENU_SEARCH) .check(matches(isDisplayed())); } } catch (Exception | AssertionFailedError e) { Loading Loading @@ -314,10 +310,13 @@ public class UiBot extends BaseBot { private static Matcher<Object> withToolbarTitle( final Matcher<CharSequence> textMatcher) { return new BoundedMatcher<Object, Toolbar>(Toolbar.class) { @Override public boolean matchesSafely(Toolbar toolbar) { @Override public boolean matchesSafely(Toolbar toolbar) { return textMatcher.matches(toolbar.getTitle()); } @Override public void describeTo(Description description) { @Override public void describeTo(Description description) { description.appendText("with toolbar title: "); textMatcher.describeTo(description); } Loading @@ -327,11 +326,14 @@ public class UiBot extends BaseBot { private static Matcher<Object> withBreadcrumbTitle( final Matcher<CharSequence> textMatcher) { return new BoundedMatcher<Object, Spinner>(Spinner.class) { @Override public boolean matchesSafely(Spinner breadcrumb) { @Override public boolean matchesSafely(Spinner breadcrumb) { DocumentInfo selectedDoc = (DocumentInfo) breadcrumb.getSelectedItem(); return textMatcher.matches(selectedDoc.displayName); } @Override public void describeTo(Description description) { @Override public void describeTo(Description description) { description.appendText("with breadcrumb title: "); textMatcher.describeTo(description); } Loading Loading
tests/src/com/android/documentsui/FilesActivityUiTest.java +15 −5 Original line number Diff line number Diff line Loading @@ -73,7 +73,7 @@ public class FilesActivityUiTest extends ActivityTest<FilesActivity> { ROOT_1_ID); // Separate logic for "Documents" root, which presence depends on the config setting if (Shared.shouldShowDocumentsRoot(context, new Intent(DocumentsContract.ACTION_BROWSE))) { if (docsRootEnabled()) { bots.roots.assertRootsPresent("Documents"); } else { bots.roots.assertRootsAbsent("Documents"); Loading @@ -87,18 +87,24 @@ public class FilesActivityUiTest extends ActivityTest<FilesActivity> { bots.directory.assertDocumentsPresent("file0.log", "file1.png", "file2.csv"); } public void testLoadsDownloadsDirectoryByDefault() throws Exception { public void testLoadsDefaultDirectory() throws Exception { initTestFiles(); device.waitForIdle(); // Separate logic for "Documents" root, which presence depends on the config setting if (docsRootEnabled()) { bots.main.assertWindowTitle("Documents"); } else { bots.main.assertWindowTitle("Downloads"); } } public void testRootClickSetsWindowTitle() throws Exception { initTestFiles(); bots.roots.openRoot("Downloads"); bots.main.assertWindowTitle("Downloads"); bots.roots.openRoot("Images"); bots.main.assertWindowTitle("Images"); } public void testFilesList_LiveUpdate() throws Exception { Loading Loading @@ -289,4 +295,8 @@ public class FilesActivityUiTest extends ActivityTest<FilesActivity> { device.pressBack(); // to clear the dialog. } private boolean docsRootEnabled() { return Shared.shouldShowDocumentsRoot(context, new Intent(DocumentsContract.ACTION_BROWSE)); } }
tests/src/com/android/documentsui/bots/Matchers.java 0 → 100644 +48 −0 Original line number Diff line number Diff line /* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.documentsui.bots; import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 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.withResourceName; import static org.hamcrest.CoreMatchers.allOf; import android.view.View; import android.widget.ImageView; import android.widget.Spinner; import android.widget.Toolbar; import com.android.documentsui.R; import com.android.internal.view.menu.ActionMenuItemView; import org.hamcrest.Matcher; /** * Handy matchers useful for finding stuff in the UI. Use with Espresso testing. */ final class Matchers { static final Matcher<View> TOOLBAR = allOf(isAssignableFrom(Toolbar.class), withId(R.id.toolbar)); static final Matcher<View> SEARCH_MENU = allOf(withId(R.id.menu_search), isDisplayed()); static final Matcher<View> SEARCH_BUTTON = allOf(isAssignableFrom(ImageView.class), withResourceName("search_button")); static final Matcher<View> BREADCRUMB = allOf(isAssignableFrom(Spinner.class), withId(R.id.breadcrumb)); static final Matcher<View> MENU_SEARCH = allOf(isAssignableFrom(ActionMenuItemView.class), withResourceName("menu_search")); }
tests/src/com/android/documentsui/bots/UiBot.java +68 −66 Original line number Diff line number Diff line Loading @@ -24,12 +24,10 @@ import static android.support.test.espresso.assertion.ViewAssertions.matches; import static android.support.test.espresso.matcher.ViewMatchers.isAssignableFrom; 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.withResourceName; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; import static org.hamcrest.CoreMatchers.allOf; import static org.hamcrest.CoreMatchers.is; import android.content.Context; Loading @@ -41,16 +39,11 @@ import android.support.test.uiautomator.UiObject; import android.support.test.uiautomator.UiObject2; import android.support.test.uiautomator.UiObjectNotFoundException; import android.support.test.uiautomator.UiSelector; import android.widget.ImageView; import android.widget.Spinner; import android.widget.Toolbar; import com.android.documentsui.R; import com.android.documentsui.model.DocumentInfo; import com.android.internal.view.menu.ActionMenuItemView; import junit.framework.Assert; import junit.framework.AssertionFailedError; import org.hamcrest.Description; import org.hamcrest.Matcher; Loading @@ -60,29 +53,31 @@ import java.util.Arrays; import java.util.Iterator; import java.util.List; import junit.framework.Assert; import junit.framework.AssertionFailedError; /** * A test helper class that provides support for controlling DocumentsUI activities * programmatically, and making assertions against the state of the UI. * * <p>Support for working directly with Roots and Directory view can be found * in the respective bots. * <p> * Support for working directly with Roots and Directory view can be found in the respective bots. */ public class UiBot extends BaseBot { public static final String TARGET_PKG = "com.android.documentsui"; private static final String LAUNCHER_PKG = "com.android.launcher"; public UiBot(UiDevice device, Context context, int timeout) { super(device, context, timeout); } public void assertWindowTitle(String expected) { onView(allOf(isAssignableFrom(Toolbar.class), withId(R.id.toolbar))) onView(Matchers.TOOLBAR) .check(matches(withToolbarTitle(is(expected)))); } public void assertBreadcrumbTitle(String expected) { if (!isTablet()) { onView(allOf(isAssignableFrom(Spinner.class), withId(R.id.breadcrumb))) onView(Matchers.BREADCRUMB) .check(matches(withBreadcrumbTitle(is(expected)))); } } Loading @@ -106,8 +101,8 @@ public class UiBot extends BaseBot { } } public void assertSearchTextFiledAndIcon(boolean searchTextFieldExists, boolean searchIconExists) { public void assertSearchTextFiledAndIcon( boolean searchTextFieldExists, boolean searchIconExists) { assertEquals(searchTextFieldExists, findSearchViewTextField().exists()); boolean searchIconVisible = isSearchIconVisible(); assertEquals(searchIconExists, searchIconVisible); Loading @@ -125,7 +120,7 @@ public class UiBot extends BaseBot { } public void setSearchQuery(String query) throws UiObjectNotFoundException { onView(allOf(withId(R.id.menu_search), isDisplayed())).perform(typeText(query)); onView(Matchers.SEARCH_MENU).perform(typeText(query)); } public UiObject openOverflowMenu() throws UiObjectNotFoundException { Loading Loading @@ -159,11 +154,12 @@ public class UiBot extends BaseBot { try { if (isTablet) { // Tablets use ImageView for its search icon, and has search_button as its res name onView(allOf(isAssignableFrom(ImageView.class), withResourceName("search_button"))) onView(Matchers.SEARCH_BUTTON) .check(matches(isDisplayed())); } else { // Phones use ActionMenuItemView for its search icon, and has menu_search as its res name onView(allOf(isAssignableFrom(ActionMenuItemView.class), withResourceName("menu_search"))) // Phones use ActionMenuItemView for its search icon, and has menu_search as its res // name onView(Matchers.MENU_SEARCH) .check(matches(isDisplayed())); } } catch (Exception | AssertionFailedError e) { Loading Loading @@ -314,10 +310,13 @@ public class UiBot extends BaseBot { private static Matcher<Object> withToolbarTitle( final Matcher<CharSequence> textMatcher) { return new BoundedMatcher<Object, Toolbar>(Toolbar.class) { @Override public boolean matchesSafely(Toolbar toolbar) { @Override public boolean matchesSafely(Toolbar toolbar) { return textMatcher.matches(toolbar.getTitle()); } @Override public void describeTo(Description description) { @Override public void describeTo(Description description) { description.appendText("with toolbar title: "); textMatcher.describeTo(description); } Loading @@ -327,11 +326,14 @@ public class UiBot extends BaseBot { private static Matcher<Object> withBreadcrumbTitle( final Matcher<CharSequence> textMatcher) { return new BoundedMatcher<Object, Spinner>(Spinner.class) { @Override public boolean matchesSafely(Spinner breadcrumb) { @Override public boolean matchesSafely(Spinner breadcrumb) { DocumentInfo selectedDoc = (DocumentInfo) breadcrumb.getSelectedItem(); return textMatcher.matches(selectedDoc.displayName); } @Override public void describeTo(Description description) { @Override public void describeTo(Description description) { description.appendText("with breadcrumb title: "); textMatcher.describeTo(description); } Loading