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

Commit f5c02746 authored by Aga Wronska's avatar Aga Wronska Committed by Android (Google) Code Review
Browse files

Merge "Add tests for rename documents feature - Add helper class for the...

Merge "Add tests for rename documents feature     - Add helper class for the Ui tests. It creates basic test environment.       It is intermediate layer between UiBot/DocumentsProviderHelper and UiTest classes.     - Refactor SearchView tests     - Add renaming support in the test DocumentsProvider     - Add renaming tests"
parents b4ec8aaa 0950df19
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -105,6 +105,19 @@ public class DocumentsProviderHelper {
        return createDocument(root.documentId, mimeType, name);
    }

    public Uri createDocumentWithFlags(String documentId, String mimeType, String name, int flags)
            throws RemoteException {
        Bundle in = new Bundle();
        in.putInt(StubProvider.EXTRA_FLAGS, flags);
        in.putString(StubProvider.EXTRA_PARENT_ID, documentId);
        in.putString(Document.COLUMN_MIME_TYPE, mimeType);
        in.putString(Document.COLUMN_DISPLAY_NAME, name);

        Bundle out = mClient.call("createDocumentWithFlags", null, in);
        Uri uri = out.getParcelable(DocumentsContract.EXTRA_URI);
        return uri;
    }

    public Uri createFolder(Uri parentUri, String name) {
        return createDocument(parentUri, Document.MIME_TYPE_DIR, name);
    }
@@ -286,4 +299,5 @@ public class DocumentsProviderHelper {

        return DocumentsContract.buildDocumentUri(mAuthority, documentId);
    }

}
+1 −1
Original line number Diff line number Diff line
@@ -85,7 +85,7 @@ public class DownloadsActivityUiTest extends InstrumentationTestCase {
        mDevice.wait(Until.hasObject(By.pkg(TARGET_PKG).depth(0)), TIMEOUT);
        mDevice.waitForIdle();

        mBot = new UiBot(mDevice, TIMEOUT);
        mBot = new UiBot(mDevice, mContext, TIMEOUT);

        resetStorage();  // Just in case a test failed and tearDown didn't happen.
    }
+1 −1
Original line number Diff line number Diff line
@@ -82,7 +82,7 @@ public class FilesActivityUiTest extends InstrumentationTestCase {
        mDevice.wait(Until.hasObject(By.pkg(TARGET_PKG).depth(0)), TIMEOUT);
        mDevice.waitForIdle();

        mBot = new UiBot(mDevice, TIMEOUT);
        mBot = new UiBot(mDevice, mContext, TIMEOUT);

        resetStorage();  // Just incase a test failed and tearDown didn't happen.
    }
+166 −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;

import static com.android.documentsui.StubProvider.ROOT_0_ID;
import static com.android.documentsui.UiTestEnvironment.TIMEOUT;

import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.LargeTest;

@LargeTest
public class RenameDocumentUiTest extends InstrumentationTestCase {

    private static final String TAG = "RenamDocumentUiTest";

    private final String newName = "kitties.log";

    private UiTestEnvironment mHelper;

    @Override
    public void setUp() throws Exception {
        super.setUp();
        mHelper = new UiTestEnvironment(getInstrumentation());
        mHelper.launch();
        mHelper.initTestFiles();
        mHelper.bot().openRoot(ROOT_0_ID);
    }

    public void testRenameEnabled_SingleSelection() throws Exception {
        mHelper.bot().selectDocument(UiTestEnvironment.fileName1);
        mHelper.bot().openOverflowMenu();
        mHelper.bot().assertMenuEnabled(R.string.menu_rename, true);

        // Dismiss more options window
        mHelper.device().pressBack();
    }

    public void testNoRenameSupport_SingleSelection() throws Exception {
        mHelper.bot().selectDocument(UiTestEnvironment.fileNameNoRename);
        mHelper.bot().openOverflowMenu();
        mHelper.bot().assertMenuEnabled(R.string.menu_rename, false);

        // Dismiss more options window
        mHelper.device().pressBack();
    }

    public void testOneHasRenameSupport_MultipleSelection() throws Exception {
        mHelper.bot().selectDocument(UiTestEnvironment.fileName1);
        mHelper.bot().selectDocument(UiTestEnvironment.fileNameNoRename);
        mHelper.bot().openOverflowMenu();
        mHelper.bot().assertMenuEnabled(R.string.menu_rename, false);

        // Dismiss more options window
        mHelper.device().pressBack();
    }

    public void testRenameDisabled_MultipleSelection() throws Exception {
        mHelper.bot().selectDocument(UiTestEnvironment.fileName1);
        mHelper.bot().selectDocument(UiTestEnvironment.fileName2);
        mHelper.bot().openOverflowMenu();
        mHelper.bot().assertMenuEnabled(R.string.menu_rename, false);

        // Dismiss more options window
        mHelper.device().pressBack();
    }

    public void testRenameFile_OkButton() throws Exception {
        mHelper.bot().selectDocument(UiTestEnvironment.fileName1);
        mHelper.bot().openOverflowMenu();
        mHelper.bot().openDialog(R.string.menu_rename);
        mHelper.bot().setDialogText(newName);
        mHelper.bot().dismissKeyboardIfPresent();

        mHelper.device().waitForIdle(TIMEOUT);
        mHelper.bot().findRenameDialogOkButton().click();
        mHelper.device().waitForIdle(TIMEOUT);

        mHelper.bot().assertDocument(UiTestEnvironment.fileName1, false);
        mHelper.bot().assertDocument(newName, true);
        mHelper.bot().assertDocumentsCount(mHelper.getDocumentsCountDir0());
    }

    public void testRenameFile_Enter() throws Exception {
        mHelper.bot().selectDocument(UiTestEnvironment.fileName1);
        mHelper.bot().openOverflowMenu();
        mHelper.bot().openDialog(R.string.menu_rename);
        mHelper.bot().setDialogText(newName);

        pressEnter();

        mHelper.bot().assertDocument(UiTestEnvironment.fileName1, false);
        mHelper.bot().assertDocument(newName, true);
        mHelper.bot().assertDocumentsCount(mHelper.getDocumentsCountDir0());
    }

    public void testRenameFile_Cancel() throws Exception {
        mHelper.bot().selectDocument(UiTestEnvironment.fileName1);
        mHelper.bot().openOverflowMenu();
        mHelper.bot().openDialog(R.string.menu_rename);
        mHelper.bot().setDialogText(newName);
        mHelper.bot().dismissKeyboardIfPresent();

        mHelper.device().waitForIdle(TIMEOUT);
        mHelper.bot().findRenameDialogCancelButton().click();
        mHelper.device().waitForIdle(TIMEOUT);

        mHelper.bot().assertDocument(UiTestEnvironment.fileName1, true);
        mHelper.bot().assertDocument(newName, false);
        mHelper.bot().assertDocumentsCount(mHelper.getDocumentsCountDir0());
    }

    public void testRenameDir() throws Exception {
        String oldName = "Dir1";
        String newName = "Dir123";

        mHelper.bot().selectDocument(oldName);
        mHelper.bot().openOverflowMenu();
        mHelper.bot().openDialog(R.string.menu_rename);
        mHelper.bot().setDialogText(newName);

        pressEnter();

        mHelper.bot().assertDocument(oldName, false);
        mHelper.bot().assertDocument(newName, true);
        mHelper.bot().assertDocumentsCount(mHelper.getDocumentsCountDir0());
    }

    public void testRename_NameExists() throws Exception {
        // Check that document with the new name exists
        mHelper.bot().assertDocument(UiTestEnvironment.fileName2, true);
        mHelper.bot().selectDocument(UiTestEnvironment.fileName1);
        mHelper.bot().openOverflowMenu();
        mHelper.bot().openDialog(R.string.menu_rename);
        mHelper.bot().setDialogText(UiTestEnvironment.fileName2);

        pressEnter();

        mHelper.bot().assertSnackbar(R.string.rename_error);
        mHelper.bot().assertDocument(UiTestEnvironment.fileName1, true);
        mHelper.bot().assertDocument(UiTestEnvironment.fileName2, true);
        mHelper.bot().assertDocumentsCount(mHelper.getDocumentsCountDir0());
    }

    private void pressEnter() {
        mHelper.device().waitForIdle(TIMEOUT);
        mHelper.device().pressEnter();
        mHelper.device().waitForIdle(TIMEOUT);
    }

}
+74 −213
Original line number Diff line number Diff line
@@ -16,278 +16,139 @@

package com.android.documentsui;

import static com.android.documentsui.StubProvider.DEFAULT_AUTHORITY;
import static com.android.documentsui.StubProvider.ROOT_0_ID;
import static com.android.documentsui.StubProvider.ROOT_1_ID;

import android.app.Instrumentation;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.support.test.uiautomator.By;
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.Until;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
import android.view.MotionEvent;

import com.android.documentsui.model.RootInfo;

@LargeTest
public class SearchViewUiTest extends InstrumentationTestCase {

    private static final int TIMEOUT = 5000;
    private static final String TAG = "SearchViewUiTest";
    private static final String TARGET_PKG = "com.android.documentsui";
    private static final String LAUNCHER_PKG = "com.android.launcher";

    private UiBot mBot;
    private UiDevice mDevice;
    private Context mContext;
    private ContentResolver mResolver;
    private DocumentsProviderHelper mDocsHelper;
    private ContentProviderClient mClient;
    private RootInfo mRoot_0;
    private RootInfo mRoot_1;

    private UiObject mSearchView;
    private UiObject mSearchTextField;

    private UiTestEnvironment mEnv;

    private UiObject mDocsList;
    private UiObject mMessageTextView;
    private UiObject mSearchIcon;

    @Override
    public void setUp() throws Exception {
        // Initialize UiDevice instance.
        Instrumentation instrumentation = getInstrumentation();

        mDevice = UiDevice.getInstance(instrumentation);

        Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_MOUSE);

        // Start from the home screen.
        mDevice.pressHome();
        mDevice.wait(Until.hasObject(By.pkg(LAUNCHER_PKG).depth(0)), TIMEOUT);
        super.setUp();
        mEnv = new UiTestEnvironment(getInstrumentation());
        mEnv.launch();

        // NOTE: Must be the "target" context, else security checks in content provider will fail.
        mContext = instrumentation.getTargetContext();
        mResolver = mContext.getContentResolver();

        mClient = mResolver.acquireUnstableContentProviderClient(DEFAULT_AUTHORITY);
        mDocsHelper = new DocumentsProviderHelper(DEFAULT_AUTHORITY, mClient);

        // Launch app.
        Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(TARGET_PKG);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        mContext.startActivity(intent);
        // Wait for the app to appear.
        mDevice.wait(Until.hasObject(By.pkg(TARGET_PKG).depth(0)), TIMEOUT);
        mDevice.waitForIdle();

        mBot = new UiBot(mDevice, TIMEOUT);

        resetStorage(); // Just incase a test failed and tearDown didn't happen.

        initUiObjects();
    }

    @Override
    protected void tearDown() throws Exception {
        mEnv.device().pressBack();
        super.tearDown();
        mDevice.pressBack();
        resetStorage();
        mClient.release();
    }

    private void resetStorage() throws RemoteException {
        mClient.call("clear", null, null);
        // TODO: Would be nice to have an event to wait on here.
        mDevice.waitForIdle();
    public void testSearchView_ExpandsOnClick() throws Exception {
        mEnv.bot().openSearchView();
        mEnv.bot().assertSearchTextFiledAndIcon(true, false);
    }

    private void initTestFiles() throws RemoteException {
        mRoot_0 = mDocsHelper.getRoot(ROOT_0_ID);
        mRoot_1 = mDocsHelper.getRoot(ROOT_1_ID);
    public void testSearchView_CollapsesOnBack() throws Exception {
        mEnv.bot().openSearchView();

        mDocsHelper.createDocument(mRoot_0, "text/plain", "file10.log");
        mDocsHelper.createDocument(mRoot_0, "image/png", "file1.png");
        mDocsHelper.createDocument(mRoot_0, "text/csv", "file2.csv");
        mEnv.device().pressBack();

        mDocsHelper.createDocument(mRoot_1, "text/plain", "anotherFile0.log");
        mDocsHelper.createDocument(mRoot_1, "text/plain", "poodles.text");
        mEnv.bot().assertSearchTextFiledAndIcon(false, true);
    }

    private void initUiObjects() {
        mSearchView = mBot.findSearchView();
        mSearchTextField = mBot.findSearchViewTextField();
        mDocsList = mBot.findDocumentsList();
        mMessageTextView = mBot.findMessageTextView();
        mSearchIcon = mBot.findSearchViewIcon();
    }

    public void testSearchViewExpandsOnClick() throws Exception {
        assertTrue(mSearchIcon.exists());
        assertFalse(mSearchTextField.exists());

        mSearchView.click();

        assertTrue(mSearchTextField.exists());
        assertTrue(mSearchTextField.isFocused());
        assertFalse(mSearchIcon.exists());
    }

    public void testSearchViewCollapsesOnBack() throws Exception {
        assertTrue(mSearchIcon.exists());
        assertFalse(mSearchTextField.exists());

        mSearchView.click();

        mDevice.pressBack();

        assertTrue(mSearchIcon.exists());
        assertFalse(mSearchTextField.exists());
    }

    public void testSearchViewClearsTextOnBack() throws Exception {
        assertTrue(mSearchIcon.exists());
        assertFalse(mSearchTextField.exists());

    public void testSearchView_ClearsTextOnBack() throws Exception {
        String query = "file2";
        mSearchView.click();
        mSearchTextField.setText(query);

        assertSearchTextField(true, query);
        mEnv.bot().openSearchView();
        mEnv.bot().setSearchQuery(query);

        mDevice.pressBack();
        mEnv.device().pressBack();

        assertTrue(mSearchIcon.exists());
        assertFalse(mSearchTextField.exists());
        mEnv.bot().assertSearchTextFiledAndIcon(false, true);
    }

    public void testSearchFound() throws Exception {
        initTestFiles();

        mBot.openRoot(ROOT_0_ID);

        assertDefaultTestDir0();
    public void testSearch_ResultsFound() throws Exception {
        mEnv.initTestFiles();
        mEnv.bot().openRoot(ROOT_0_ID);
        mEnv.assertDefaultContentOfTestDir0();

        String query = "file1";
        mSearchView.click();
        mSearchTextField.setText(query);

        assertTrue(mDocsList.exists());
        assertSearchTextField(true, query);
        mEnv.bot().openSearchView();
        mEnv.bot().setSearchQuery(query);
        mEnv.bot().assertSearchTextField(true, query);

        mDevice.pressEnter();
        mEnv.device().pressEnter();

        assertTrue(mDocsList.exists());
        assertEquals(2, mDocsList.getChildCount());
        mBot.assertHasDocuments("file1.png", "file10.log");
        assertSearchTextField(false, query);
        mEnv.bot().assertDocumentsCountOnList(true, 2);
        mEnv.bot().assertHasDocuments(UiTestEnvironment.fileName1, UiTestEnvironment.fileName2);
        mEnv.bot().assertSearchTextField(false, query);
    }

    public void testSearchFoundClearsOnBack() throws Exception {
        initTestFiles();

        mBot.openRoot(ROOT_0_ID);

        assertDefaultTestDir0();

        String query = "file1";
        mSearchView.click();
        mSearchTextField.setText(query);
    public void testSearchResultsFound_ClearsOnBack() throws Exception {
        mEnv.initTestFiles();
        mEnv.bot().openRoot(ROOT_0_ID);
        mEnv.assertDefaultContentOfTestDir0();

        mDevice.pressEnter();
        mDevice.pressBack();
        String query = UiTestEnvironment.fileName1;
        mEnv.bot().openSearchView();
        mEnv.bot().setSearchQuery(query);

        assertDefaultTestDir0();
        mEnv.device().pressEnter();
        mEnv.device().pressBack();
        mEnv.assertDefaultContentOfTestDir0();
    }

    public void testSearchNoResults() throws Exception {
        initTestFiles();

        mBot.openRoot(ROOT_0_ID);

        assertDefaultTestDir0();
    public void testSearch_NoResults() throws Exception {
        mEnv.initTestFiles();
        mEnv.bot().openRoot(ROOT_0_ID);
        mEnv.assertDefaultContentOfTestDir0();

        String query = "chocolate";
        mSearchView.click();
        mSearchTextField.setText(query);
        mEnv.bot().openSearchView();
        mEnv.bot().setSearchQuery(query);

        mDevice.pressEnter();
        mEnv.device().pressEnter();

        assertFalse(mDocsList.exists());
        assertTrue(mMessageTextView.exists());
        String msg = String.valueOf(mContext.getString(R.string.no_results));
        assertEquals(String.format(msg, "TEST_ROOT_0"), mMessageTextView.getText());
        assertSearchTextField(false, query);
    }

    public void testSearchNoResultsClearsOnBack() throws Exception {
        initTestFiles();
        mEnv.bot().assertDocumentsCountOnList(false, 0);

        mBot.openRoot(ROOT_0_ID);
        String msg = String.valueOf(mEnv.context().getString(R.string.no_results));
        mEnv.bot().assertMessageTextView(String.format(msg, "TEST_ROOT_0"));
        mEnv.bot().assertSearchTextField(false, query);
    }

        assertDefaultTestDir0();
    public void testSearchNoResults_ClearsOnBack() throws Exception {
        mEnv.initTestFiles();
        mEnv.bot().openRoot(ROOT_0_ID);
        mEnv.assertDefaultContentOfTestDir0();

        String query = "chocolate";
        mSearchView.click();
        mSearchTextField.setText(query);
        mEnv.bot().openSearchView();
        mEnv.bot().setSearchQuery(query);

        mDevice.pressEnter();
        mDevice.pressBack();

        assertDefaultTestDir0();
        mEnv.device().pressEnter();
        mEnv.device().pressBack();
        mEnv.assertDefaultContentOfTestDir0();
    }

    public void testSearchFoundClearsDirectoryChange() throws Exception {
        initTestFiles();

        mBot.openRoot(ROOT_0_ID);

        assertDefaultTestDir0();

        String query = "file1";
        mSearchView.click();
        mSearchTextField.setText(query);

        mDevice.pressEnter();
    public void testSearchResultsFound_ClearsOnDirectoryChange() throws Exception {
        mEnv.initTestFiles();
        mEnv.bot().openRoot(ROOT_0_ID);
        mEnv.assertDefaultContentOfTestDir0();

        mBot.openRoot(ROOT_1_ID);
        String query = UiTestEnvironment.fileName1;
        mEnv.bot().openSearchView();
        mEnv.bot().setSearchQuery(query);

         assertDefaultTestDir1();
        mEnv.device().pressEnter();

         mBot.openRoot(ROOT_0_ID);

         assertDefaultTestDir0();
    }

    private void assertDefaultTestDir0() throws UiObjectNotFoundException {
        assertTrue(mSearchIcon.exists());
        assertTrue(mDocsList.exists());
        assertFalse(mSearchTextField.exists());
        assertEquals(3, mDocsList.getChildCount());
        mBot.assertHasDocuments("file2.csv", "file1.png", "file10.log");
    }

    private void assertDefaultTestDir1() throws UiObjectNotFoundException {
        assertTrue(mSearchIcon.exists());
        assertFalse(mSearchTextField.exists());
        assertTrue(mDocsList.exists());
        assertEquals(2, mDocsList.getChildCount());
        mBot.assertHasDocuments("anotherFile0.log", "poodles.text");
    }
        mEnv.bot().openRoot(ROOT_1_ID);
        mEnv.assertDefaultContentOfTestDir1();

    private void assertSearchTextField(boolean isFocused, String query)
            throws UiObjectNotFoundException {
        assertFalse(mSearchIcon.exists());
        assertTrue(mSearchTextField.exists());
        assertEquals(isFocused, mSearchTextField.isFocused());
        assertEquals(query, mSearchTextField.getText());
        mEnv.bot().openRoot(ROOT_0_ID);
        mEnv.assertDefaultContentOfTestDir0();
    }
}
Loading