Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b502273e authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Functional Test for context menus." into nyc-andromeda-dev

parents 239ab977 fa1e1360
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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);
+5 −2
Original line number Diff line number Diff line
@@ -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);
    }

    /**
+4 −3
Original line number Diff line number Diff line
@@ -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;
    }
+68 −1
Original line number Diff line number Diff line
@@ -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);
+52 −0
Original line number Diff line number Diff line
/*
 * 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));
            }
        }
    }
}
Loading