diff options
author | 2016-09-14 17:57:25 -0700 | |
---|---|---|
committer | 2016-09-16 16:08:19 -0700 | |
commit | fa1e1360999a7ca6d78eb1a2fd560196fe0b4dbb (patch) | |
tree | a930bb6283e1dc34dba4f6d76bcf3c11aa2d1558 | |
parent | ae84a18a1fa5dc020fd25a963cb0955c8c31bfef (diff) |
Functional Test for context menus.
Change-Id: Ibc3f9e0234444a6a2469218a475ca49c205bc93b
10 files changed, 254 insertions, 11 deletions
diff --git a/perf-tests/src/com/android/documentsui/FilesJankPerfTest.java b/perf-tests/src/com/android/documentsui/FilesJankPerfTest.java index 0d1d143d2..43c42dbdf 100644 --- a/perf-tests/src/com/android/documentsui/FilesJankPerfTest.java +++ b/perf-tests/src/com/android/documentsui/FilesJankPerfTest.java @@ -24,6 +24,7 @@ import android.app.Activity; import android.os.RemoteException; import android.test.suitebuilder.annotation.LargeTest; +import android.app.UiAutomation; import android.content.Intent; import android.content.Context; import android.support.test.jank.JankTest; @@ -50,8 +51,9 @@ public class FilesJankPerfTest extends JankTestBase { public void setUpInLoop() { final UiDevice device = UiDevice.getInstance(getInstrumentation()); final Context context = getInstrumentation().getTargetContext(); + final UiAutomation automation = getInstrumentation().getUiAutomation(); mRootsListBot = new SidebarBot(device, context, BOT_TIMEOUT); - mDirListBot = new DirectoryListBot(device, context, BOT_TIMEOUT); + mDirListBot = new DirectoryListBot(device, automation, context, BOT_TIMEOUT); final Intent intent = new Intent(context, ManageActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); diff --git a/tests/common/com/android/documentsui/bots/Bots.java b/tests/common/com/android/documentsui/bots/Bots.java index bd6be3b88..bfa7743cf 100644 --- a/tests/common/com/android/documentsui/bots/Bots.java +++ b/tests/common/com/android/documentsui/bots/Bots.java @@ -18,6 +18,7 @@ package com.android.documentsui.bots; import static junit.framework.Assert.assertNotNull; +import android.app.UiAutomation; import android.content.Context; import android.support.test.uiautomator.By; import android.support.test.uiautomator.BySelector; @@ -41,17 +42,19 @@ public final class Bots { public final SidebarBot roots; public final SearchBot search; public final GestureBot gesture; + public final MenuBot menu; public final UiBot main; - public Bots(UiDevice device, Context context, int timeout) { + public Bots(UiDevice device, UiAutomation automation, Context context, int timeout) { main = new UiBot(device, context, TIMEOUT); breadcrumb = new BreadBot(device, context, TIMEOUT, main); roots = new SidebarBot(device, context, TIMEOUT); - directory = new DirectoryListBot(device, context, TIMEOUT); + directory = new DirectoryListBot(device, automation, context, TIMEOUT); sortHeader = new SortHeaderBot(device, context, TIMEOUT); keyboard = new KeyboardBot(device, context, TIMEOUT); search = new SearchBot(device, context, TIMEOUT); gesture = new GestureBot(device, context, TIMEOUT); + menu = new MenuBot(device, context, TIMEOUT); } /** diff --git a/tests/common/com/android/documentsui/bots/BreadBot.java b/tests/common/com/android/documentsui/bots/BreadBot.java index 92653d467..7ec64e28e 100644 --- a/tests/common/com/android/documentsui/bots/BreadBot.java +++ b/tests/common/com/android/documentsui/bots/BreadBot.java @@ -38,6 +38,8 @@ import com.android.documentsui.DropdownBreadcrumb; import com.android.documentsui.R; import com.android.documentsui.base.DocumentInfo; +import junit.framework.Assert; + import org.hamcrest.Description; import org.hamcrest.Matcher; @@ -46,8 +48,6 @@ import java.util.Arrays; import java.util.List; import java.util.function.Predicate; -import junit.framework.Assert; - /** * A test helper class that provides support for controlling the UI Breadcrumb * programmatically, and making assertions against the state of the UI. @@ -71,7 +71,8 @@ public class BreadBot extends Bots.BaseBot { private UiBot mMain; - public BreadBot(UiDevice device, Context context, int timeout, UiBot main) { + public BreadBot( + UiDevice device, Context context, int timeout, UiBot main) { super(device, context, timeout); mMain = main; } diff --git a/tests/common/com/android/documentsui/bots/DirectoryListBot.java b/tests/common/com/android/documentsui/bots/DirectoryListBot.java index d985edc13..5bddac1b0 100644 --- a/tests/common/com/android/documentsui/bots/DirectoryListBot.java +++ b/tests/common/com/android/documentsui/bots/DirectoryListBot.java @@ -21,9 +21,11 @@ import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; +import android.app.UiAutomation; import android.content.Context; import android.graphics.Point; import android.graphics.Rect; +import android.os.SystemClock; import android.support.test.uiautomator.By; import android.support.test.uiautomator.BySelector; import android.support.test.uiautomator.Configurator; @@ -33,6 +35,7 @@ import android.support.test.uiautomator.UiObject2; import android.support.test.uiautomator.UiObjectNotFoundException; import android.support.test.uiautomator.UiSelector; import android.support.test.uiautomator.Until; +import android.view.InputDevice; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; @@ -53,9 +56,12 @@ public class DirectoryListBot extends Bots.BaseBot { private static final BySelector SNACK_DELETE = By.desc(Pattern.compile("^Deleting [0-9]+ file.+")); + private UiAutomation mAutomation; - public DirectoryListBot(UiDevice device, Context context, int timeout) { + public DirectoryListBot( + UiDevice device, UiAutomation automation, Context context, int timeout) { super(device, context, timeout); + mAutomation = automation; } public void assertDocumentsCount(int count) throws UiObjectNotFoundException { @@ -241,6 +247,67 @@ public class DirectoryListBot extends Bots.BaseBot { } } + public void rightClickDocument(String label) throws UiObjectNotFoundException { + Rect startCoord = findDocument(label).getBounds(); + rightClickDocument(new Point(startCoord.centerX(), startCoord.centerY())); + } + + public void rightClickDocument(Point point) throws UiObjectNotFoundException { + //TODO: Use Espresso instead of doing the events mock ourselves + MotionEvent motionDown = getTestMotionEvent( + MotionEvent.ACTION_DOWN, + MotionEvent.BUTTON_SECONDARY, + MotionEvent.TOOL_TYPE_MOUSE, + InputDevice.SOURCE_MOUSE, + point.x, + point.y); + mAutomation.injectInputEvent(motionDown, true); + SystemClock.sleep(100); + + MotionEvent motionUp = getTestMotionEvent( + MotionEvent.ACTION_UP, + MotionEvent.BUTTON_SECONDARY, + MotionEvent.TOOL_TYPE_MOUSE, + InputDevice.SOURCE_MOUSE, + point.x, + point.y); + + mAutomation.injectInputEvent(motionUp, true); + } + + private MotionEvent getTestMotionEvent( + int action, int buttonState, int toolType, int source, int x, int y) { + long eventTime = SystemClock.uptimeMillis(); + + MotionEvent.PointerProperties[] pp = {new MotionEvent.PointerProperties()}; + pp[0].clear(); + pp[0].id = 0; + pp[0].toolType = toolType; + + MotionEvent.PointerCoords[] pointerCoords = {new MotionEvent.PointerCoords()}; + pointerCoords[0].clear(); + pointerCoords[0].x = x; + pointerCoords[0].y = y; + pointerCoords[0].pressure = 0; + pointerCoords[0].size = 1; + + return MotionEvent.obtain( + eventTime, + eventTime, + action, + 1, + pp, + pointerCoords, + 0, + buttonState, + 1f, + 1f, + 0, + 0, + source, + 0); + } + private void assertOrder(String first, String second) throws UiObjectNotFoundException { final UiObject firstObj = findDocument(first); diff --git a/tests/common/com/android/documentsui/bots/MenuBot.java b/tests/common/com/android/documentsui/bots/MenuBot.java new file mode 100644 index 000000000..06e505415 --- /dev/null +++ b/tests/common/com/android/documentsui/bots/MenuBot.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2015 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 junit.framework.Assert.assertTrue; +import static junit.framework.Assert.assertFalse; + +import android.content.Context; +import android.support.test.uiautomator.By; +import android.support.test.uiautomator.UiDevice; +import android.support.test.uiautomator.UiObjectNotFoundException; + +import java.util.Map; + +/** + * A test helper class that provides support for controlling menu items. + */ +public class MenuBot extends Bots.BaseBot { + + public MenuBot( + UiDevice device, Context context, int timeout) { + super(device, context, timeout); + } + + public boolean hasMenuItem(String menuLabel) throws UiObjectNotFoundException { + return mDevice.findObject(By.text(menuLabel)) != null; + } + + public void assertPresentMenuItems(Map<String, Boolean> menuStates) throws Exception { + for (String key : menuStates.keySet()) { + if (menuStates.get(key)) { + assertTrue(key + " expected to be shown", hasMenuItem(key)); + } else { + assertFalse(key + " expected not to be shown", hasMenuItem(key)); + } + } + } +} diff --git a/tests/common/com/android/documentsui/bots/SidebarBot.java b/tests/common/com/android/documentsui/bots/SidebarBot.java index ae899d3c9..9d95a93a0 100644 --- a/tests/common/com/android/documentsui/bots/SidebarBot.java +++ b/tests/common/com/android/documentsui/bots/SidebarBot.java @@ -32,12 +32,12 @@ import android.view.View; import com.android.documentsui.R; +import junit.framework.Assert; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import junit.framework.Assert; - /** * A test helper class that provides support for controlling and asserting against * the roots list drawer. diff --git a/tests/common/com/android/documentsui/bots/SortHeaderBot.java b/tests/common/com/android/documentsui/bots/SortHeaderBot.java index 24bdc70c7..ff6a839b1 100644 --- a/tests/common/com/android/documentsui/bots/SortHeaderBot.java +++ b/tests/common/com/android/documentsui/bots/SortHeaderBot.java @@ -29,6 +29,7 @@ import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; import static org.hamcrest.Matchers.allOf; +import android.app.UiAutomation; import android.content.Context; import android.support.annotation.StringRes; import android.support.test.uiautomator.UiDevice; diff --git a/tests/functional/com/android/documentsui/ActivityTest.java b/tests/functional/com/android/documentsui/ActivityTest.java index 4aa2f33f1..f68b708b8 100644 --- a/tests/functional/com/android/documentsui/ActivityTest.java +++ b/tests/functional/com/android/documentsui/ActivityTest.java @@ -17,6 +17,7 @@ package com.android.documentsui; import android.app.Activity; +import android.app.UiAutomation; import android.content.ContentProviderClient; import android.content.ContentResolver; import android.content.Context; @@ -58,6 +59,7 @@ public abstract class ActivityTest<T extends Activity> extends ActivityInstrumen public Bots bots; public UiDevice device; public Context context; + public UiAutomation automation; public RootInfo rootDir0; public RootInfo rootDir1; @@ -101,8 +103,9 @@ public abstract class ActivityTest<T extends Activity> extends ActivityInstrumen device = UiDevice.getInstance(getInstrumentation()); // NOTE: Must be the "target" context, else security checks in content provider will fail. context = getInstrumentation().getTargetContext(); + automation = getInstrumentation().getUiAutomation(); - bots = new Bots(device, context, TIMEOUT); + bots = new Bots(device, automation, context, TIMEOUT); Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_MOUSE); diff --git a/tests/functional/com/android/documentsui/ContextMenuUiTest.java b/tests/functional/com/android/documentsui/ContextMenuUiTest.java new file mode 100644 index 000000000..a81f3a77c --- /dev/null +++ b/tests/functional/com/android/documentsui/ContextMenuUiTest.java @@ -0,0 +1,115 @@ +/* + * 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; + +import android.graphics.Point; +import android.graphics.Rect; +import android.net.Uri; +import android.os.RemoteException; +import android.support.test.filters.LargeTest; + +import com.android.documentsui.manager.ManageActivity; + +import java.util.HashMap; +import java.util.Map; + +@LargeTest +public class ContextMenuUiTest extends ActivityTest<ManageActivity> { + + private Map<String, Boolean> menuItems; + + public ContextMenuUiTest() { + super(ManageActivity.class); + } + + @Override + public void setUp() throws Exception { + super.setUp(); + initTestFiles(); + menuItems = new HashMap<>(); + + menuItems.put("Share", false); + menuItems.put("Open", false); + menuItems.put("Open with", false); + menuItems.put("Cut", false); + menuItems.put("Copy", false); + menuItems.put("Rename", false); + menuItems.put("Delete", false); + menuItems.put("Open in new window", false); + menuItems.put("Paste into folder", false); + menuItems.put("Select all", false); + menuItems.put("New folder", false); + } + + @Override + public void initTestFiles() throws RemoteException { + Uri uri = mDocsHelper.createFolder(rootDir0, dirName1); + mDocsHelper.createFolder(uri, childDir1); + + mDocsHelper.createDocument(rootDir0, "text/plain", "file0.log"); + mDocsHelper.createDocument(rootDir0, "image/png", "file1.png"); + mDocsHelper.createDocument(rootDir0, "text/csv", "file2.csv"); + + mDocsHelper.createDocument(rootDir1, "text/plain", "anotherFile0.log"); + mDocsHelper.createDocument(rootDir1, "text/plain", "poodles.text"); + } + + public void testContextMenu_onFile() throws Exception { + menuItems.put("Share", true); + menuItems.put("Open", true); + menuItems.put("Open with", true); + menuItems.put("Cut", true); + menuItems.put("Copy", true); + menuItems.put("Rename", true); + menuItems.put("Delete", true); + + bots.directory.rightClickDocument("file1.png"); + bots.menu.assertPresentMenuItems(menuItems); + } + + public void testContextMenu_onDir() throws Exception { + menuItems.put("Cut", true); + menuItems.put("Copy", true); + menuItems.put("Paste into folder", true); + menuItems.put("Open in new window", true); + menuItems.put("Delete", true); + menuItems.put("Rename", true); + bots.directory.rightClickDocument("Dir1"); + bots.menu.assertPresentMenuItems(menuItems); + } + + public void testContextMenu_onMixedFileDir() throws Exception { + menuItems.put("Cut", true); + menuItems.put("Copy", true); + menuItems.put("Delete", true); + bots.directory.selectDocument("file1.png"); + bots.directory.selectDocument("Dir1"); + bots.directory.assertSelection(2); //wait until selection is fully done + bots.directory.rightClickDocument("Dir1"); + bots.menu.assertPresentMenuItems(menuItems); + } + + public void testContextMenu_onEmptyArea() throws Exception { + menuItems.put("Select all", true); + menuItems.put("Paste", true); + menuItems.put("New folder", true); + Rect dirListBounds = bots.directory.findDocumentsList().getBounds(); + bots.directory.rightClickDocument( + new Point(dirListBounds.right - 1, dirListBounds.bottom - 1)); //bottom right corner + bots.menu.assertPresentMenuItems(menuItems); + } +} diff --git a/tests/functional/com/android/documentsui/FileManagementUiTest.java b/tests/functional/com/android/documentsui/FileManagementUiTest.java index f9a47217a..e7b7cf8d6 100644 --- a/tests/functional/com/android/documentsui/FileManagementUiTest.java +++ b/tests/functional/com/android/documentsui/FileManagementUiTest.java @@ -26,7 +26,6 @@ import android.test.suitebuilder.annotation.LargeTest; import android.view.KeyEvent; import com.android.documentsui.R; -import com.android.documentsui.R.id; import com.android.documentsui.manager.ManageActivity; @LargeTest |