Tomasz Mikolajewski | a2ad8df | 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 | |
Steve McKay | ae84a18 | 2016-09-16 09:36:50 -0700 | [diff] [blame] | 17 | package com.android.documentsui; |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 18 | |
| 19 | import android.app.Activity; |
Ben Lin | fa1e136 | 2016-09-14 17:57:25 -0700 | [diff] [blame] | 20 | import android.app.UiAutomation; |
Bill Lin | 6b77172 | 2018-08-16 19:56:56 +0800 | [diff] [blame] | 21 | import android.app.UiModeManager; |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 22 | import android.content.ContentProviderClient; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
Bill Lin | 6b77172 | 2018-08-16 19:56:56 +0800 | [diff] [blame] | 26 | import android.content.res.Configuration; |
Garfield Tan | ceedd1f | 2017-05-17 14:10:12 -0700 | [diff] [blame] | 27 | import android.os.Bundle; |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 28 | import android.os.RemoteException; |
Steve McKay | 09404b3 | 2016-09-30 13:18:36 -0700 | [diff] [blame] | 29 | import android.provider.DocumentsContract; |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 30 | import android.provider.DocumentsContract.Document; |
Tomasz Mikolajewski | a2ad8df | 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 | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 34 | import android.test.ActivityInstrumentationTestCase2; |
| 35 | import android.view.MotionEvent; |
Steve McKay | 09404b3 | 2016-09-30 13:18:36 -0700 | [diff] [blame] | 36 | |
Ben Lin | b18a526 | 2017-05-17 13:56:07 -0700 | [diff] [blame] | 37 | import com.android.documentsui.base.Features; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 38 | import com.android.documentsui.base.RootInfo; |
Steve McKay | 85ec0d6 | 2016-06-24 15:05:08 -0700 | [diff] [blame] | 39 | import com.android.documentsui.bots.Bots; |
Steve McKay | 5e1acc9 | 2016-02-19 12:57:05 -0800 | [diff] [blame] | 40 | import com.android.documentsui.bots.UiBot; |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 41 | |
Steve McKay | 85ec0d6 | 2016-06-24 15:05:08 -0700 | [diff] [blame] | 42 | import javax.annotation.Nullable; |
| 43 | |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 44 | /** |
| 45 | * Provides basic test environment for UI tests: |
| 46 | * - Launches activity |
| 47 | * - Creates and gives access to test root directories and test files |
| 48 | * - Cleans up the test environment |
| 49 | */ |
| 50 | public abstract class ActivityTest<T extends Activity> extends ActivityInstrumentationTestCase2<T> { |
| 51 | |
| 52 | static final int TIMEOUT = 5000; |
Bill Lin | 6b77172 | 2018-08-16 19:56:56 +0800 | [diff] [blame] | 53 | static final int NIGHT_MODE_CHANGE_WAIT_TIME = 1000; |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 54 | |
| 55 | // Testing files. For custom ones, override initTestFiles(). |
| 56 | public static final String dirName1 = "Dir1"; |
Steve McKay | 85ec0d6 | 2016-06-24 15:05:08 -0700 | [diff] [blame] | 57 | public static final String childDir1 = "ChildDir1"; |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 58 | public static final String fileName1 = "file1.log"; |
| 59 | public static final String fileName2 = "file12.png"; |
| 60 | public static final String fileName3 = "anotherFile0.log"; |
| 61 | public static final String fileName4 = "poodles.text"; |
| 62 | public static final String fileNameNoRename = "NO_RENAMEfile.txt"; |
| 63 | |
Steve McKay | 5e1acc9 | 2016-02-19 12:57:05 -0800 | [diff] [blame] | 64 | public Bots bots; |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 65 | public UiDevice device; |
| 66 | public Context context; |
Ben Lin | fa1e136 | 2016-09-14 17:57:25 -0700 | [diff] [blame] | 67 | public UiAutomation automation; |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 68 | |
Ben Lin | b18a526 | 2017-05-17 13:56:07 -0700 | [diff] [blame] | 69 | public Features features; |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 70 | public RootInfo rootDir0; |
| 71 | public RootInfo rootDir1; |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 72 | protected ContentResolver mResolver; |
| 73 | protected DocumentsProviderHelper mDocsHelper; |
| 74 | protected ContentProviderClient mClient; |
Bill Lin | 6b77172 | 2018-08-16 19:56:56 +0800 | [diff] [blame] | 75 | protected UiModeManager mUiModeManager; |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 76 | |
| 77 | public ActivityTest(Class<T> activityClass) { |
| 78 | super(activityClass); |
| 79 | } |
| 80 | |
Aga Wronska | ec401b7 | 2016-02-24 12:57:21 -0800 | [diff] [blame] | 81 | /* |
| 82 | * Returns the root that will be opened within the activity. |
| 83 | * By default tests are started with one of the test roots. |
| 84 | * Override the method if you want to open different root on start. |
| 85 | * @return Root that will be opened. Return null if you want to open activity's default root. |
| 86 | */ |
Steve McKay | 85ec0d6 | 2016-06-24 15:05:08 -0700 | [diff] [blame] | 87 | protected @Nullable RootInfo getInitialRoot() { |
Aga Wronska | ec401b7 | 2016-02-24 12:57:21 -0800 | [diff] [blame] | 88 | return rootDir0; |
| 89 | } |
| 90 | |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 91 | /** |
| 92 | * Returns the authority of the testing provider begin used. |
| 93 | * By default it's StubProvider's authority. |
| 94 | * @return Authority of the provider. |
| 95 | */ |
| 96 | protected String getTestingProviderAuthority() { |
Steve McKay | ae84a18 | 2016-09-16 09:36:50 -0700 | [diff] [blame] | 97 | return StubProvider.DEFAULT_AUTHORITY; |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Resolves testing roots. |
| 102 | */ |
| 103 | protected void setupTestingRoots() throws RemoteException { |
Steve McKay | ae84a18 | 2016-09-16 09:36:50 -0700 | [diff] [blame] | 104 | rootDir0 = mDocsHelper.getRoot(StubProvider.ROOT_0_ID); |
| 105 | rootDir1 = mDocsHelper.getRoot(StubProvider.ROOT_1_ID); |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 106 | } |
| 107 | |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 108 | @Override |
| 109 | public void setUp() throws Exception { |
| 110 | device = UiDevice.getInstance(getInstrumentation()); |
| 111 | // NOTE: Must be the "target" context, else security checks in content provider will fail. |
| 112 | context = getInstrumentation().getTargetContext(); |
Ben Lin | fa1e136 | 2016-09-14 17:57:25 -0700 | [diff] [blame] | 113 | automation = getInstrumentation().getUiAutomation(); |
Ben Lin | b18a526 | 2017-05-17 13:56:07 -0700 | [diff] [blame] | 114 | features = new Features.RuntimeFeatures(context.getResources(), null); |
Steve McKay | 5e1acc9 | 2016-02-19 12:57:05 -0800 | [diff] [blame] | 115 | |
Ben Lin | fa1e136 | 2016-09-14 17:57:25 -0700 | [diff] [blame] | 116 | bots = new Bots(device, automation, context, TIMEOUT); |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 117 | |
| 118 | Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_MOUSE); |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 119 | |
| 120 | mResolver = context.getContentResolver(); |
Tomasz Mikolajewski | e29e341 | 2016-02-24 12:53:44 +0900 | [diff] [blame] | 121 | mClient = mResolver.acquireUnstableContentProviderClient(getTestingProviderAuthority()); |
| 122 | mDocsHelper = new DocumentsProviderHelper(getTestingProviderAuthority(), mClient); |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 123 | |
Ben Lin | ebee3de | 2016-07-22 18:31:36 -0700 | [diff] [blame] | 124 | device.setOrientationNatural(); |
Tomasz Mikolajewski | 2fca4cc | 2016-03-02 14:38:23 +0900 | [diff] [blame] | 125 | setupTestingRoots(); |
| 126 | |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 127 | launchActivity(); |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 128 | resetStorage(); |
Ben Lin | ebee3de | 2016-07-22 18:31:36 -0700 | [diff] [blame] | 129 | |
| 130 | // Since at the launch of activity, ROOT_0 and ROOT_1 have no files, drawer will |
| 131 | // automatically open for phone devices. Espresso register click() as (x, y) MotionEvents, |
| 132 | // so if a drawer is on top of a file we want to select, it will actually click the drawer. |
| 133 | // Thus to start a clean state, we always try to close first. |
| 134 | bots.roots.closeDrawer(); |
Garfield Tan | ceedd1f | 2017-05-17 14:10:12 -0700 | [diff] [blame] | 135 | |
| 136 | // Configure the provider back to default. |
| 137 | mDocsHelper.configure(null, Bundle.EMPTY); |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void tearDown() throws Exception { |
Ben Lin | ebee3de | 2016-07-22 18:31:36 -0700 | [diff] [blame] | 142 | device.unfreezeRotation(); |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 143 | mClient.release(); |
| 144 | super.tearDown(); |
| 145 | } |
| 146 | |
Julian Mancini | db28d84 | 2017-06-19 10:14:57 -0700 | [diff] [blame] | 147 | protected void launchActivity() { |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 148 | final Intent intent = context.getPackageManager().getLaunchIntentForPackage( |
Bill Lin | 3432919 | 2019-03-05 22:22:20 +0800 | [diff] [blame] | 149 | UiBot.targetPackageName); |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 150 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); |
Aga Wronska | ec401b7 | 2016-02-24 12:57:21 -0800 | [diff] [blame] | 151 | if (getInitialRoot() != null) { |
Garfield Tan | c5efea0 | 2017-02-22 12:58:29 -0800 | [diff] [blame] | 152 | intent.setAction(Intent.ACTION_VIEW); |
| 153 | intent.setDataAndType(getInitialRoot().getUri(), DocumentsContract.Root.MIME_TYPE_ITEM); |
Aga Wronska | ec401b7 | 2016-02-24 12:57:21 -0800 | [diff] [blame] | 154 | } |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 155 | setActivityIntent(intent); |
| 156 | getActivity(); // Launch the activity. |
| 157 | } |
| 158 | |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 159 | protected void resetStorage() throws RemoteException { |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 160 | mClient.call("clear", null, null); |
| 161 | device.waitForIdle(); |
| 162 | } |
| 163 | |
Steve McKay | d080506 | 2016-09-15 14:30:38 -0700 | [diff] [blame] | 164 | protected void initTestFiles() throws RemoteException { |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 165 | mDocsHelper.createFolder(rootDir0, dirName1); |
| 166 | mDocsHelper.createDocument(rootDir0, "text/plain", fileName1); |
| 167 | mDocsHelper.createDocument(rootDir0, "image/png", fileName2); |
| 168 | mDocsHelper.createDocumentWithFlags(rootDir0.documentId, "text/plain", fileNameNoRename, |
| 169 | Document.FLAG_SUPPORTS_WRITE); |
| 170 | |
| 171 | mDocsHelper.createDocument(rootDir1, "text/plain", fileName3); |
| 172 | mDocsHelper.createDocument(rootDir1, "text/plain", fileName4); |
| 173 | } |
| 174 | |
| 175 | void assertDefaultContentOfTestDir0() throws UiObjectNotFoundException { |
Steve McKay | 85ec0d6 | 2016-06-24 15:05:08 -0700 | [diff] [blame] | 176 | bots.directory.waitForDocument(fileName1); |
| 177 | bots.directory.waitForDocument(fileName2); |
| 178 | bots.directory.waitForDocument(dirName1); |
| 179 | bots.directory.waitForDocument(fileNameNoRename); |
Steve McKay | 5e1acc9 | 2016-02-19 12:57:05 -0800 | [diff] [blame] | 180 | bots.directory.assertDocumentsCount(4); |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | void assertDefaultContentOfTestDir1() throws UiObjectNotFoundException { |
Steve McKay | 85ec0d6 | 2016-06-24 15:05:08 -0700 | [diff] [blame] | 184 | bots.directory.waitForDocument(fileName3); |
| 185 | bots.directory.waitForDocument(fileName4); |
Steve McKay | 5e1acc9 | 2016-02-19 12:57:05 -0800 | [diff] [blame] | 186 | bots.directory.assertDocumentsCount(2); |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 187 | } |
Bill Lin | 6b77172 | 2018-08-16 19:56:56 +0800 | [diff] [blame] | 188 | |
| 189 | /** |
| 190 | * Setup test Activity UI Mode YES or not(AUTO/YES/NO) before start to testing |
| 191 | * @param uiModeNight Constant for {@link #setNightMode(int)} |
| 192 | * 0 - MODE_NIGHT_AUTO |
| 193 | * 1 - MODE_NIGHT_NO |
| 194 | * 2 - MODE_NIGHT_YES |
| 195 | */ |
| 196 | protected void setSystemUiModeNight(int uiModeNight) { |
| 197 | int systemUiMode = getActivity().getResources().getConfiguration().uiMode |
| 198 | & Configuration.UI_MODE_NIGHT_MASK; |
| 199 | if(uiModeNight != systemUiMode) { |
| 200 | /* TODO since ag/4947691 enable config_lockDayNightMode to block app setNightMode() |
| 201 | create b/115315612 to handle the UiModeManager permission deny problem */ |
| 202 | mUiModeManager = (UiModeManager) getActivity() |
| 203 | .getSystemService(Context.UI_MODE_SERVICE); |
| 204 | mUiModeManager.setNightMode(uiModeNight); |
| 205 | device.waitForIdle(NIGHT_MODE_CHANGE_WAIT_TIME); |
| 206 | } |
| 207 | } |
Tomasz Mikolajewski | a2ad8df | 2016-02-04 17:46:35 +0900 | [diff] [blame] | 208 | } |