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

Commit 1273913f authored by Ben Lin's avatar Ben Lin
Browse files

GestureSelector and BandSelection Functional Tests (basic).

Change-Id: Ib03aa03eff23713530471f9212314f5b024a3bf7
parent d080506e
Loading
Loading
Loading
Loading
+2 −0
Original line number Original line Diff line number Diff line
@@ -40,6 +40,7 @@ public final class Bots {
    public final KeyboardBot keyboard;
    public final KeyboardBot keyboard;
    public final SidebarBot roots;
    public final SidebarBot roots;
    public final SearchBot search;
    public final SearchBot search;
    public final GestureBot gesture;
    public final UiBot main;
    public final UiBot main;


    public Bots(UiDevice device, Context context, int timeout) {
    public Bots(UiDevice device, Context context, int timeout) {
@@ -50,6 +51,7 @@ public final class Bots {
        sortHeader = new SortHeaderBot(device, context, TIMEOUT);
        sortHeader = new SortHeaderBot(device, context, TIMEOUT);
        keyboard = new KeyboardBot(device, context, TIMEOUT);
        keyboard = new KeyboardBot(device, context, TIMEOUT);
        search = new SearchBot(device, context, TIMEOUT);
        search = new SearchBot(device, context, TIMEOUT);
        gesture = new GestureBot(device, context, TIMEOUT);
    }
    }


    /**
    /**
+8 −0
Original line number Original line Diff line number Diff line
@@ -22,6 +22,7 @@ import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import static junit.framework.Assert.fail;


import android.content.Context;
import android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.Rect;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.BySelector;
@@ -219,6 +220,13 @@ public class DirectoryListBot extends Bots.BaseBot {
        assertHasFocus(DIR_LIST_ID);
        assertHasFocus(DIR_LIST_ID);
    }
    }


    public void assertSelection(int numSelected) {
        String assertSelectionText = numSelected + " selected";
        UiObject2 selectionText = mDevice.wait(
                Until.findObject(By.text(assertSelectionText)), mTimeout);
        assertTrue(selectionText != null);
    }

    public void assertOrder(String[] dirs, String[] files) throws UiObjectNotFoundException {
    public void assertOrder(String[] dirs, String[] files) throws UiObjectNotFoundException {
        for (int i = 0; i < dirs.length - 1; ++i) {
        for (int i = 0; i < dirs.length - 1; ++i) {
            assertOrder(dirs[i], dirs[i + 1]);
            assertOrder(dirs[i], dirs[i + 1]);
+90 −0
Original line number Original line 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 android.content.Context;
import android.graphics.Point;
import android.graphics.Rect;
import android.support.test.uiautomator.Configurator;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;
import android.view.MotionEvent;

/**
 * A test helper class that provides support for controlling directory list
 * and making assertions against the state of it.
 */
public class GestureBot extends Bots.BaseBot {
    private static final String DIR_CONTAINER_ID = "com.android.documentsui:id/container_directory";
    private static final String DIR_LIST_ID = "com.android.documentsui:id/dir_list";
    private static final int LONGPRESS_STEPS = 60;
    private static final int TRAVELING_STEPS = 20;
    private static final int BAND_SELECTION_DEFAULT_STEPS = 100;
    private static final int STEPS_INBETWEEN_POINTS = 2;

    public GestureBot(UiDevice device, Context context, int timeout) {
        super(device, context, timeout);
    }

    public void gestureSelectFiles(String startLabel, String endLabel) throws Exception {
        int toolType = Configurator.getInstance().getToolType();
        Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_FINGER);
        Rect startCoord = findDocument(startLabel).getBounds();
        Rect endCoord = findDocument(endLabel).getBounds();
        double diffX = endCoord.centerX() - startCoord.centerX();
        double diffY = endCoord.centerY() - startCoord.centerY();
        Point[] points = new Point[LONGPRESS_STEPS + TRAVELING_STEPS];

        // First simulate long-press by having a bunch of MOVE events in the same coordinate
        for (int i = 0; i < LONGPRESS_STEPS; i++) {
            points[i] = new Point(startCoord.centerX(), startCoord.centerY());
        }

        // Next put the actual drag/move events
        for (int i = 0; i < TRAVELING_STEPS; i++) {
            int newX = startCoord.centerX() + (int) (diffX / TRAVELING_STEPS * i);
            int newY = startCoord.centerY() + (int) (diffY / TRAVELING_STEPS * i);
            points[i + LONGPRESS_STEPS] = new Point(newX, newY);
        }
        mDevice.swipe(points, STEPS_INBETWEEN_POINTS);
        Configurator.getInstance().setToolType(toolType);
    }

    public void bandSelection(Point start, Point end) throws Exception {
        bandSelection(start, end, BAND_SELECTION_DEFAULT_STEPS);
    }

    public void bandSelection(Point start, Point end, int steps) throws Exception {
        int toolType = Configurator.getInstance().getToolType();
        Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_MOUSE);
        mDevice.swipe(start.x, start.y, end.x, end.y, steps);
        Configurator.getInstance().setToolType(toolType);
    }

    public UiObject findDocument(String label) throws UiObjectNotFoundException {
        final UiSelector docList = new UiSelector().resourceId(
                DIR_CONTAINER_ID).childSelector(
                        new UiSelector().resourceId(DIR_LIST_ID));

        // Wait for the first list item to appear
        new UiObject(docList.childSelector(new UiSelector())).waitForExists(mTimeout);

        return mDevice.findObject(docList.childSelector(new UiSelector().text(label)));
    }
}
+60 −0
Original line number Original line 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.functional;

import android.graphics.Point;
import android.graphics.Rect;
import android.test.suitebuilder.annotation.LargeTest;
import com.android.documentsui.manager.ManageActivity;

@LargeTest
public class BandSelectionUiTest extends ActivityTest<ManageActivity> {

    public BandSelectionUiTest() {
        super(ManageActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        initTestFiles();
        bots.roots.closeDrawer();
    }

    public void testBandSelection_allFiles() throws Exception {
        bots.main.switchToGridMode();
        Rect dirListBounds = bots.directory.findDocumentsList().getBounds();
        Point start = new Point(dirListBounds.right - 1, dirListBounds.bottom - 1);
        Point end = new Point(dirListBounds.left + 1, dirListBounds.top + 1);
        bots.gesture.bandSelection(start, end);

        bots.directory.assertSelection(4);
    }

    public void testBandSelection_someFiles() throws Exception {
        bots.main.switchToGridMode();
        Rect dirListBounds = bots.directory.findDocumentsList().getBounds();
        Rect startDoc = bots.directory.findDocument(fileName2).getBounds();
        // 100 pixels below bottom of file2
        Point start = new Point(startDoc.centerX(), startDoc.bottom + 100);
        // Top left corner
        Point end = new Point(dirListBounds.left + 1, dirListBounds.top + 1);
        bots.gesture.bandSelection(start, end);

        bots.directory.assertSelection(3);
    }
}
+66 −0
Original line number Original line 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.functional;

import android.test.suitebuilder.annotation.LargeTest;
import com.android.documentsui.manager.ManageActivity;

@LargeTest
public class GestureSelectionUiTest extends ActivityTest<ManageActivity> {

    public GestureSelectionUiTest() {
        super(ManageActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        initTestFiles();
        bots.roots.closeDrawer();
    }

    public void testGridGestureSelect_twoFiles() throws Exception {
        bots.main.switchToGridMode();
        bots.gesture.gestureSelectFiles(fileName1, fileName2);

        bots.directory.assertSelection(2);
    }

    public void testGridGestureSelect_multipleFiles() throws Exception {
        bots.main.switchToGridMode();
        bots.gesture.gestureSelectFiles(fileName2, dirName1);

        bots.directory.assertSelection(3);

    }

    public void testListGestureSelect_twoFiles() throws Exception {
        bots.main.switchToListMode();
        bots.gesture.gestureSelectFiles(fileName1, fileName2);

        bots.directory.assertSelection(2);

    }

    public void testListGestureSelect_multipleFiles() throws Exception {
        bots.main.switchToListMode();
        bots.gesture.gestureSelectFiles(fileName2, dirName1);

        bots.directory.assertSelection(3);

    }
}