Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.documentsui; |
| 18 | |
| 19 | import static com.android.documentsui.StubProvider.DEFAULT_AUTHORITY; |
| 20 | import static com.android.documentsui.StubProvider.ROOT_0_ID; |
| 21 | import static com.android.documentsui.StubProvider.ROOT_1_ID; |
| 22 | |
Aga Wronska | b9eeee1 | 2016-02-24 12:57:21 -0800 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 24 | import android.app.Activity; |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 25 | import android.content.ContentProviderClient; |
| 26 | import android.content.ContentResolver; |
| 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
| 29 | import android.os.RemoteException; |
| 30 | import android.provider.DocumentsContract.Document; |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 31 | import android.support.test.uiautomator.Configurator; |
| 32 | import android.support.test.uiautomator.UiDevice; |
| 33 | import android.support.test.uiautomator.UiObjectNotFoundException; |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 34 | import android.test.ActivityInstrumentationTestCase2; |
| 35 | import android.view.MotionEvent; |
| 36 | |
Steve McKay | b9a20d1 | 2016-02-19 12:57:05 -0800 | [diff] [blame] | 37 | import com.android.documentsui.bots.DirectoryListBot; |
| 38 | import com.android.documentsui.bots.KeyboardBot; |
| 39 | import com.android.documentsui.bots.RootsListBot; |
| 40 | import com.android.documentsui.bots.UiBot; |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 41 | import com.android.documentsui.model.RootInfo; |
| 42 | |
| 43 | /** |
| 44 | * Provides basic test environment for UI tests: |
| 45 | * - Launches activity |
| 46 | * - Creates and gives access to test root directories and test files |
| 47 | * - Cleans up the test environment |
| 48 | */ |
| 49 | public abstract class ActivityTest<T extends Activity> extends ActivityInstrumentationTestCase2<T> { |
| 50 | |
| 51 | static final int TIMEOUT = 5000; |
| 52 | |
| 53 | // Testing files. For custom ones, override initTestFiles(). |
| 54 | public static final String dirName1 = "Dir1"; |
| 55 | public static final String fileName1 = "file1.log"; |
| 56 | public static final String fileName2 = "file12.png"; |
| 57 | public static final String fileName3 = "anotherFile0.log"; |
| 58 | public static final String fileName4 = "poodles.text"; |
| 59 | public static final String fileNameNoRename = "NO_RENAMEfile.txt"; |
| 60 | |
Steve McKay | b9a20d1 | 2016-02-19 12:57:05 -0800 | [diff] [blame] | 61 | public Bots bots; |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 62 | public UiDevice device; |
| 63 | public Context context; |
| 64 | |
| 65 | public RootInfo rootDir0; |
| 66 | public RootInfo rootDir1; |
| 67 | |
| 68 | ContentResolver mResolver; |
| 69 | DocumentsProviderHelper mDocsHelper; |
| 70 | ContentProviderClient mClient; |
| 71 | |
| 72 | public ActivityTest(Class<T> activityClass) { |
| 73 | super(activityClass); |
| 74 | } |
| 75 | |
Aga Wronska | b9eeee1 | 2016-02-24 12:57:21 -0800 | [diff] [blame] | 76 | /* |
| 77 | * Returns the root that will be opened within the activity. |
| 78 | * By default tests are started with one of the test roots. |
| 79 | * Override the method if you want to open different root on start. |
| 80 | * @return Root that will be opened. Return null if you want to open activity's default root. |
| 81 | */ |
| 82 | @Nullable |
| 83 | protected RootInfo getInitialRoot() { |
| 84 | return rootDir0; |
| 85 | } |
| 86 | |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 87 | @Override |
| 88 | public void setUp() throws Exception { |
| 89 | device = UiDevice.getInstance(getInstrumentation()); |
| 90 | // NOTE: Must be the "target" context, else security checks in content provider will fail. |
| 91 | context = getInstrumentation().getTargetContext(); |
Steve McKay | b9a20d1 | 2016-02-19 12:57:05 -0800 | [diff] [blame] | 92 | |
| 93 | bots = new Bots(device, context, TIMEOUT); |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 94 | |
| 95 | Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_MOUSE); |
Steve McKay | b9a20d1 | 2016-02-19 12:57:05 -0800 | [diff] [blame] | 96 | bots.main.revealLauncher(); |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 97 | |
| 98 | mResolver = context.getContentResolver(); |
| 99 | mClient = mResolver.acquireUnstableContentProviderClient(DEFAULT_AUTHORITY); |
| 100 | mDocsHelper = new DocumentsProviderHelper(DEFAULT_AUTHORITY, mClient); |
| 101 | |
| 102 | rootDir0 = mDocsHelper.getRoot(ROOT_0_ID); |
| 103 | rootDir1 = mDocsHelper.getRoot(ROOT_1_ID); |
| 104 | |
| 105 | launchActivity(); |
| 106 | |
Steve McKay | b9a20d1 | 2016-02-19 12:57:05 -0800 | [diff] [blame] | 107 | bots.main.revealApp(); |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 108 | resetStorage(); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void tearDown() throws Exception { |
| 113 | mClient.release(); |
| 114 | super.tearDown(); |
| 115 | } |
| 116 | |
| 117 | void launchActivity() { |
| 118 | final Intent intent = context.getPackageManager().getLaunchIntentForPackage( |
| 119 | UiBot.TARGET_PKG); |
| 120 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); |
Aga Wronska | b9eeee1 | 2016-02-24 12:57:21 -0800 | [diff] [blame] | 121 | if (getInitialRoot() != null) { |
| 122 | intent.setData(getInitialRoot().getUri()); |
| 123 | } |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 124 | setActivityIntent(intent); |
| 125 | getActivity(); // Launch the activity. |
| 126 | } |
| 127 | |
| 128 | void resetStorage() throws RemoteException { |
| 129 | mClient.call("clear", null, null); |
| 130 | device.waitForIdle(); |
| 131 | } |
| 132 | |
| 133 | void initTestFiles() throws RemoteException { |
| 134 | mDocsHelper.createFolder(rootDir0, dirName1); |
| 135 | mDocsHelper.createDocument(rootDir0, "text/plain", fileName1); |
| 136 | mDocsHelper.createDocument(rootDir0, "image/png", fileName2); |
| 137 | mDocsHelper.createDocumentWithFlags(rootDir0.documentId, "text/plain", fileNameNoRename, |
| 138 | Document.FLAG_SUPPORTS_WRITE); |
| 139 | |
| 140 | mDocsHelper.createDocument(rootDir1, "text/plain", fileName3); |
| 141 | mDocsHelper.createDocument(rootDir1, "text/plain", fileName4); |
| 142 | } |
| 143 | |
| 144 | void assertDefaultContentOfTestDir0() throws UiObjectNotFoundException { |
Steve McKay | b9a20d1 | 2016-02-19 12:57:05 -0800 | [diff] [blame] | 145 | bots.directory.assertDocumentsCount(4); |
| 146 | bots.directory.assertDocumentsPresent(fileName1, fileName2, dirName1, fileNameNoRename); |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void assertDefaultContentOfTestDir1() throws UiObjectNotFoundException { |
Steve McKay | b9a20d1 | 2016-02-19 12:57:05 -0800 | [diff] [blame] | 150 | bots.directory.assertDocumentsCount(2); |
| 151 | bots.directory.assertDocumentsPresent(fileName3, fileName4); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Handy collection of bots for working with Files app. |
| 156 | */ |
| 157 | public static final class Bots { |
| 158 | public final UiBot main; |
| 159 | public final RootsListBot roots; |
| 160 | public final DirectoryListBot directory; |
| 161 | public final KeyboardBot keyboard; |
| 162 | |
| 163 | private Bots(UiDevice device, Context context, int timeout) { |
| 164 | this.main = new UiBot(device, context, TIMEOUT); |
| 165 | this.roots = new RootsListBot(device, context, TIMEOUT); |
| 166 | this.directory = new DirectoryListBot(device, context, TIMEOUT); |
| 167 | this.keyboard = new KeyboardBot(device, context, TIMEOUT); |
| 168 | } |
Tomasz Mikolajewski | c7b8322 | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 169 | } |
| 170 | } |