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

Commit 7b68b868 authored by Tomasz Mikolajewski's avatar Tomasz Mikolajewski Committed by Android (Google) Code Review
Browse files

Merge "Migrate all tests to ActivityTest." into nyc-dev

parents cd502cf7 c7b83220
Loading
Loading
Loading
Loading
+136 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ 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.Activity;
import android.app.Instrumentation;
import android.content.ContentProviderClient;
import android.content.ContentResolver;
@@ -32,6 +33,7 @@ import android.support.test.uiautomator.Configurator;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.Until;
import android.test.ActivityInstrumentationTestCase2;
import android.view.MotionEvent;

import com.android.documentsui.model.RootInfo;
@@ -42,136 +44,93 @@ import com.android.documentsui.model.RootInfo;
 * - Creates and gives access to test root directories and test files
 * - Cleans up the test environment
 */
class UiTestEnvironment {
public abstract class ActivityTest<T extends Activity> extends ActivityInstrumentationTestCase2<T> {

    public static final int TIMEOUT = 5000;
    public static final String NO_RENAME = "NO_RENAME";
    static final int TIMEOUT = 5000;

    // Testing files. For custom ones, override initTestFiles().
    public static final String dirName1 = "Dir1";
    public static final String fileName1 = "file1.log";
    public static final String fileName2 = "file12.png";
    public static final String fileName3 = "anotherFile0.log";
    public static final String fileName4 = "poodles.text";
    public static final String fileNameNoRename = NO_RENAME + "file.txt";
    public static final String fileNameNoRename = "NO_RENAMEfile.txt";

    private static final String TARGET_PKG = "com.android.documentsui";
    private static final String LAUNCHER_PKG = "com.android.launcher";
    public UiBot bot;
    public UiDevice device;
    public Context context;

    private final UiBot mBot;
    private final UiDevice mDevice;
    private final Context mContext;
    public RootInfo rootDir0;
    public RootInfo rootDir1;

    private  RootInfo mRootDir0;
    private  RootInfo mRootDir1;
    private int mDocsCountDir0;
    private int mDocsCountDir1;
    ContentResolver mResolver;
    DocumentsProviderHelper mDocsHelper;
    ContentProviderClient mClient;

    private ContentResolver mResolver;
    private DocumentsProviderHelper mDocsHelper;
    private ContentProviderClient mClient;

    private final Instrumentation mInstrumentation;

    public UiTestEnvironment(Instrumentation instrumentation) {
        mInstrumentation = instrumentation;
        mDevice = UiDevice.getInstance(mInstrumentation);
        // NOTE: Must be the "target" context, else security checks in content provider will fail.
        mContext = mInstrumentation.getTargetContext();
        mBot = new UiBot(mDevice, mContext, TIMEOUT);
    public ActivityTest(Class<T> activityClass) {
        super(activityClass);
    }

/**
 * Launches default activity and waits for the application to appear.
 * @throws Exception
 */
    public void launch() throws Exception {
        Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(TARGET_PKG);
        launch(intent);
    }
    @Override
    public void setUp() throws Exception {
        device = UiDevice.getInstance(getInstrumentation());
        // NOTE: Must be the "target" context, else security checks in content provider will fail.
        context = getInstrumentation().getTargetContext();
        bot = new UiBot(device, context, TIMEOUT);

    /**
     * Launches activity specified by intent and waits for the application to appear.
     * @param intent Intent describing activity to launch.
     * @throws Exception
     */
    public void launch(Intent intent) throws Exception {
        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);
        bot.revealLauncher();

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

        // Launch app.
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
        // Wait for the app to appear.
        mDevice.wait(Until.hasObject(By.pkg(TARGET_PKG).depth(0)), TIMEOUT);
        mDevice.waitForIdle();
        rootDir0 = mDocsHelper.getRoot(ROOT_0_ID);
        rootDir1 = mDocsHelper.getRoot(ROOT_1_ID);

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

    public void cleanUp() throws Exception {
        bot.revealApp();
        resetStorage();
        mClient.release();
    }

    public void resetStorage() throws RemoteException {
        mClient.call("clear", null, null);
        mDevice.waitForIdle();
    }

    public void initTestFiles() throws RemoteException {
        mRootDir0 = mDocsHelper.getRoot(ROOT_0_ID);
        mRootDir1 = mDocsHelper.getRoot(ROOT_1_ID);

        mDocsHelper.createFolder(mRootDir0, dirName1);
        mDocsHelper.createDocument(mRootDir0, "text/plain", fileName1);
        mDocsHelper.createDocument(mRootDir0, "image/png", fileName2);
        mDocsHelper.createDocumentWithFlags(mRootDir0.documentId, "text/plain", fileNameNoRename,
                Document.FLAG_SUPPORTS_WRITE);
        mDocsCountDir0 = 4;

        mDocsHelper.createDocument(mRootDir1, "text/plain", fileName3);
        mDocsHelper.createDocument(mRootDir1, "text/plain", fileName4);
        mDocsCountDir1 = 2;
    }

    public void assertDefaultContentOfTestDir0() throws UiObjectNotFoundException {
        bot().assertDocumentsCount(ROOT_0_ID, getDocumentsCountDir0());
        bot().assertHasDocuments(UiTestEnvironment.fileName1, UiTestEnvironment.fileName2,
                UiTestEnvironment.dirName1, UiTestEnvironment.fileNameNoRename);
    }

    public void assertDefaultContentOfTestDir1() throws UiObjectNotFoundException {
        bot().assertDocumentsCount(ROOT_1_ID, getDocumentsCountDir1());
        bot().assertHasDocuments(UiTestEnvironment.fileName3, UiTestEnvironment.fileName4);
    @Override
    public void tearDown() throws Exception {
        mClient.release();
        super.tearDown();
    }

    public UiBot bot() {
        return mBot;
    void launchActivity() {
        final Intent intent = context.getPackageManager().getLaunchIntentForPackage(
                UiBot.TARGET_PKG);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        setActivityIntent(intent);
        getActivity();  // Launch the activity.
    }

    public UiDevice device() {
        return mDevice;
    void resetStorage() throws RemoteException {
        mClient.call("clear", null, null);
        device.waitForIdle();
    }

    public Context context() {
        return mContext;
    }
    void initTestFiles() throws RemoteException {
        mDocsHelper.createFolder(rootDir0, dirName1);
        mDocsHelper.createDocument(rootDir0, "text/plain", fileName1);
        mDocsHelper.createDocument(rootDir0, "image/png", fileName2);
        mDocsHelper.createDocumentWithFlags(rootDir0.documentId, "text/plain", fileNameNoRename,
                Document.FLAG_SUPPORTS_WRITE);

    public RootInfo getRootDir0() {
        return mRootDir0;
        mDocsHelper.createDocument(rootDir1, "text/plain", fileName3);
        mDocsHelper.createDocument(rootDir1, "text/plain", fileName4);
    }

    public int getDocumentsCountDir0() {
        return mDocsCountDir0;
    void assertDefaultContentOfTestDir0() throws UiObjectNotFoundException {
        bot.assertDocumentsCount(ROOT_0_ID, 4);
        bot.assertHasDocuments(fileName1, fileName2, dirName1, fileNameNoRename);
    }

    public int getDocumentsCountDir1() {
        return mDocsCountDir1;
    void assertDefaultContentOfTestDir1() throws UiObjectNotFoundException {
        bot.assertDocumentsCount(ROOT_1_ID, 2);
        bot.assertHasDocuments(fileName3, fileName4);
    }
}
+2 −1
Original line number Diff line number Diff line
@@ -92,7 +92,8 @@ public class DocumentsProviderHelper {
            Uri uri = DocumentsContract.createDocument(mClient, parentUri, mimeType, name);
            return uri;
        } catch (RemoteException e) {
            throw new RuntimeException("Couldn't create document: " + name + " with mimetype " + mimeType, e);
            throw new RuntimeException("Couldn't create document: " + name + " with mimetype "
                    + mimeType, e);
        }
    }

+26 −74
Original line number Diff line number Diff line
@@ -38,122 +38,74 @@ import android.view.MotionEvent;
import com.android.documentsui.model.RootInfo;

@LargeTest
public class DownloadsActivityUiTest extends ActivityInstrumentationTestCase2<DownloadsActivity> {
public class DownloadsActivityUiTest extends ActivityTest<DownloadsActivity> {

    private static final int TIMEOUT = 5000;
    private static final String TAG = "DownloadsActivityUiTest";
    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;

    public DownloadsActivityUiTest() {
        super(DownloadsActivity.class);
    }

    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);

        // 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);

        mRoot = mDocsHelper.getRoot(ROOT_0_ID);

        // Open the Downloads activity on our stub provider root.
        Intent intent = new Intent(DocumentsContract.ACTION_MANAGE_ROOT);
        intent.setDataAndType(mRoot.getUri(), DocumentsContract.Root.MIME_TYPE_ITEM);
    @Override
    void launchActivity() {
        final Intent intent = new Intent(DocumentsContract.ACTION_MANAGE_ROOT);
        intent.setDataAndType(rootDir0.getUri(), DocumentsContract.Root.MIME_TYPE_ITEM);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        setActivityIntent(intent);
        getActivity();  // Start the activity.

        // Wait for the app to appear.
        mDevice.wait(Until.hasObject(By.pkg(TARGET_PKG).depth(0)), TIMEOUT);
        mDevice.waitForIdle();

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

        resetStorage();  // Just in case a test failed and tearDown didn't happen.
        getActivity();  // Launch the activity.
    }

    @Override
    protected void tearDown() throws Exception {
        Log.d(TAG, "Resetting storage from setUp");
        resetStorage();
        mClient.release();

        super.tearDown();
    }

    private void resetStorage() throws RemoteException {
        mClient.call("clear", null, null);
        // TODO: Would be nice to have an event to wait on here.
        mDevice.waitForIdle();
    }

    private void initTestFiles() throws RemoteException {
        mDocsHelper.createDocument(mRoot, "text/plain", "file0.log");
        mDocsHelper.createDocument(mRoot, "image/png", "file1.png");
        mDocsHelper.createDocument(mRoot, "text/csv", "file2.csv");
    void initTestFiles() throws RemoteException {
        mDocsHelper.createDocument(rootDir0, "text/plain", "file0.log");
        mDocsHelper.createDocument(rootDir0, "image/png", "file1.png");
        mDocsHelper.createDocument(rootDir0, "text/csv", "file2.csv");
    }

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

        mBot.assertWindowTitle(ROOT_0_ID);
        bot.assertWindowTitle(ROOT_0_ID);
    }

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

        mBot.assertHasDocuments("file0.log", "file1.png", "file2.csv");
        bot.assertHasDocuments("file0.log", "file1.png", "file2.csv");
    }

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

        mDocsHelper.createDocument(mRoot, "yummers/sandwich", "Ham & Cheese.sandwich");
        mBot.assertHasDocuments("file0.log", "file1.png", "file2.csv", "Ham & Cheese.sandwich");
        mDocsHelper.createDocument(rootDir0, "yummers/sandwich", "Ham & Cheese.sandwich");

        device.waitForIdle();
        bot.assertHasDocuments("file0.log", "file1.png", "file2.csv", "Ham & Cheese.sandwich");
    }

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

        mBot.clickDocument("file1.png");
        mDevice.waitForIdle();
        mBot.menuDelete().click();
        bot.clickDocument("file1.png");
        device.waitForIdle();
        bot.menuDelete().click();

        mBot.waitForDeleteSnackbar();
        assertFalse(mBot.hasDocuments("file1.png"));
        bot.waitForDeleteSnackbar();
        assertFalse(bot.hasDocuments("file1.png"));

        mBot.waitForDeleteSnackbarGone();
        assertFalse(mBot.hasDocuments("file1.png"));
        bot.waitForDeleteSnackbarGone();
        assertFalse(bot.hasDocuments("file1.png"));
    }

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

        mBot.clickDocument("file1.png");
        mDevice.waitForIdle();
        assertNotNull(mBot.menuShare());
        bot.clickDocument("file1.png");
        device.waitForIdle();
        assertNotNull(bot.menuShare());
    }
}
+35 −97
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ import android.support.test.uiautomator.By;
import android.support.test.uiautomator.Configurator;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.Until;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;
import android.util.Log;
import android.view.MotionEvent;
@@ -38,96 +37,35 @@ import android.view.MotionEvent;
import com.android.documentsui.model.RootInfo;

@LargeTest
public class FilesActivityUiTest extends ActivityInstrumentationTestCase2<FilesActivity> {
public class FilesActivityUiTest extends ActivityTest<FilesActivity> {

    private static final int TIMEOUT = 5000;
    private static final String TAG = "FilesActivityUiTest";
    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;

    public FilesActivityUiTest() {
        super(FilesActivity.class);
    }

    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);

        // 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);
        assertNotNull("Failed to acquire ContentProviderClient.", mClient);
        mDocsHelper = new DocumentsProviderHelper(DEFAULT_AUTHORITY, mClient);

        // Launch app.
        Intent intent = mContext.getPackageManager().getLaunchIntentForPackage(TARGET_PKG);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        setActivityIntent(intent);
        getActivity();  // Start the activity.

        // Wait for the app to appear.
        mDevice.wait(Until.hasObject(By.pkg(TARGET_PKG).depth(0)), TIMEOUT);
        mDevice.waitForIdle();

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

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

    @Override
    protected void tearDown() throws Exception {
        Log.d(TAG, "Resetting storage from setUp");
        resetStorage();
        mClient.release();

        super.tearDown();
    }

    private void resetStorage() throws RemoteException {
        mClient.call("clear", null, null);
        // TODO: Would be nice to have an event to wait on here.
        mDevice.waitForIdle();
    }

    private void initTestFiles() throws RemoteException {
        mRoot_0 = mDocsHelper.getRoot(ROOT_0_ID);
        mRoot_1 = mDocsHelper.getRoot(ROOT_1_ID);

        mDocsHelper.createDocument(mRoot_0, "text/plain", "file0.log");
        mDocsHelper.createDocument(mRoot_0, "image/png", "file1.png");
        mDocsHelper.createDocument(mRoot_0, "text/csv", "file2.csv");
    public void initTestFiles() throws RemoteException {
        mDocsHelper.createDocument(rootDir0, "text/plain", "file0.log");
        mDocsHelper.createDocument(rootDir0, "image/png", "file1.png");
        mDocsHelper.createDocument(rootDir0, "text/csv", "file2.csv");

        mDocsHelper.createDocument(mRoot_1, "text/plain", "anotherFile0.log");
        mDocsHelper.createDocument(mRoot_1, "text/plain", "poodles.text");
        mDocsHelper.createDocument(rootDir1, "text/plain", "anotherFile0.log");
        mDocsHelper.createDocument(rootDir1, "text/plain", "poodles.text");
    }

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

        mBot.openRoot(ROOT_0_ID);
        bot.openRoot(ROOT_0_ID);

        // Should also have Drive, but that requires pre-configuration of devices
        // We omit for now.
        mBot.assertHasRoots(
        bot.assertHasRoots(
                "Images",
                "Videos",
                "Audio",
@@ -140,60 +78,60 @@ public class FilesActivityUiTest extends ActivityInstrumentationTestCase2<FilesA
    public void testFilesListed() throws Exception {
        initTestFiles();

        mBot.openRoot(ROOT_0_ID);
        mBot.assertHasDocuments("file0.log", "file1.png", "file2.csv");
        bot.openRoot(ROOT_0_ID);
        bot.assertHasDocuments("file0.log", "file1.png", "file2.csv");
    }

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

        mDevice.waitForIdle();
        mBot.assertWindowTitle("Home");
        device.waitForIdle();
        bot.assertWindowTitle("Home");
    }

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

        mBot.openRoot("Downloads");
        mBot.assertWindowTitle("Downloads");
        bot.openRoot("Downloads");
        bot.assertWindowTitle("Downloads");
    }

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

        mBot.openRoot(ROOT_0_ID);
        mDocsHelper.createDocument(mRoot_0, "yummers/sandwich", "Ham & Cheese.sandwich");
        bot.openRoot(ROOT_0_ID);
        mDocsHelper.createDocument(rootDir0, "yummers/sandwich", "Ham & Cheese.sandwich");

        mDevice.waitForIdle();
        mBot.assertHasDocuments("file0.log", "file1.png", "file2.csv", "Ham & Cheese.sandwich");
        device.waitForIdle();
        bot.assertHasDocuments("file0.log", "file1.png", "file2.csv", "Ham & Cheese.sandwich");
    }

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

        mBot.openRoot(ROOT_0_ID);
        bot.openRoot(ROOT_0_ID);

        mBot.clickDocument("file1.png");
        mDevice.waitForIdle();
        mBot.menuDelete().click();
        bot.clickDocument("file1.png");
        device.waitForIdle();
        bot.menuDelete().click();

        mBot.waitForDeleteSnackbar();
        assertFalse(mBot.hasDocuments("file1.png"));
        bot.waitForDeleteSnackbar();
        assertFalse(bot.hasDocuments("file1.png"));

        mBot.waitForDeleteSnackbarGone();
        assertFalse(mBot.hasDocuments("file1.png"));
        bot.waitForDeleteSnackbarGone();
        assertFalse(bot.hasDocuments("file1.png"));

        // Now delete from another root.
        mBot.openRoot(ROOT_1_ID);
        bot.openRoot(ROOT_1_ID);

        mBot.clickDocument("poodles.text");
        mDevice.waitForIdle();
        mBot.menuDelete().click();
        bot.clickDocument("poodles.text");
        device.waitForIdle();
        bot.menuDelete().click();

        mBot.waitForDeleteSnackbar();
        assertFalse(mBot.hasDocuments("poodles.text"));
        bot.waitForDeleteSnackbar();
        assertFalse(bot.hasDocuments("poodles.text"));

        mBot.waitForDeleteSnackbarGone();
        assertFalse(mBot.hasDocuments("poodles.text"));
        bot.waitForDeleteSnackbarGone();
        assertFalse(bot.hasDocuments("poodles.text"));
    }
}
+76 −78

File changed.

Preview size limit exceeded, changes collapsed.

Loading