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 android.app.Activity; 20 import android.app.UiAutomation; 21 import android.app.UiModeManager; 22 import android.content.ContentProviderClient; 23 import android.content.ContentResolver; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.res.Configuration; 27 import android.os.Bundle; 28 import android.os.RemoteException; 29 import android.provider.DocumentsContract; 30 import android.provider.DocumentsContract.Document; 31 import android.support.test.uiautomator.Configurator; 32 import android.support.test.uiautomator.UiDevice; 33 import android.support.test.uiautomator.UiObjectNotFoundException; 34 import android.test.ActivityInstrumentationTestCase2; 35 import android.view.MotionEvent; 36 37 import com.android.documentsui.base.Features; 38 import com.android.documentsui.base.RootInfo; 39 import com.android.documentsui.bots.Bots; 40 import com.android.documentsui.files.FilesActivity; 41 42 import javax.annotation.Nullable; 43 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; 53 static final int NIGHT_MODE_CHANGE_WAIT_TIME = 1000; 54 55 // Testing files. For custom ones, override initTestFiles(). 56 public static final String dirName1 = "Dir1"; 57 public static final String childDir1 = "ChildDir1"; 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 64 public Bots bots; 65 public UiDevice device; 66 public Context context; 67 public UiAutomation automation; 68 69 public Features features; 70 public RootInfo rootDir0; 71 public RootInfo rootDir1; 72 protected ContentResolver mResolver; 73 protected DocumentsProviderHelper mDocsHelper; 74 protected ContentProviderClient mClient; 75 protected UiModeManager mUiModeManager; 76 ActivityTest(Class<T> activityClass)77 public ActivityTest(Class<T> activityClass) { 78 super(activityClass); 79 } 80 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 */ getInitialRoot()87 protected @Nullable RootInfo getInitialRoot() { 88 return rootDir0; 89 } 90 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 */ getTestingProviderAuthority()96 protected String getTestingProviderAuthority() { 97 return StubProvider.DEFAULT_AUTHORITY; 98 } 99 100 /** 101 * Resolves testing roots. 102 */ setupTestingRoots()103 protected void setupTestingRoots() throws RemoteException { 104 rootDir0 = mDocsHelper.getRoot(StubProvider.ROOT_0_ID); 105 rootDir1 = mDocsHelper.getRoot(StubProvider.ROOT_1_ID); 106 } 107 108 @Override setUp()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(); 113 automation = getInstrumentation().getUiAutomation(); 114 features = new Features.RuntimeFeatures(context.getResources(), null); 115 116 bots = new Bots(device, automation, context, TIMEOUT); 117 118 Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_MOUSE); 119 120 mResolver = context.getContentResolver(); 121 mClient = mResolver.acquireUnstableContentProviderClient(getTestingProviderAuthority()); 122 mDocsHelper = new DocumentsProviderHelper(getTestingProviderAuthority(), mClient); 123 124 device.setOrientationNatural(); 125 setupTestingRoots(); 126 127 launchActivity(); 128 resetStorage(); 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(); 135 136 // Configure the provider back to default. 137 mDocsHelper.configure(null, Bundle.EMPTY); 138 } 139 140 @Override tearDown()141 public void tearDown() throws Exception { 142 device.unfreezeRotation(); 143 mClient.release(); 144 super.tearDown(); 145 } 146 launchActivity()147 protected void launchActivity() { 148 final Intent intent = new Intent(context, FilesActivity.class); 149 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 150 if (getInitialRoot() != null) { 151 intent.setAction(Intent.ACTION_VIEW); 152 intent.setDataAndType(getInitialRoot().getUri(), DocumentsContract.Root.MIME_TYPE_ITEM); 153 } 154 setActivityIntent(intent); 155 getActivity(); // Launch the activity. 156 } 157 resetStorage()158 protected void resetStorage() throws RemoteException { 159 mClient.call("clear", null, null); 160 device.waitForIdle(); 161 } 162 initTestFiles()163 protected void initTestFiles() throws RemoteException { 164 mDocsHelper.createFolder(rootDir0, dirName1); 165 mDocsHelper.createDocument(rootDir0, "text/plain", fileName1); 166 mDocsHelper.createDocument(rootDir0, "image/png", fileName2); 167 mDocsHelper.createDocumentWithFlags(rootDir0.documentId, "text/plain", fileNameNoRename, 168 Document.FLAG_SUPPORTS_WRITE); 169 170 mDocsHelper.createDocument(rootDir1, "text/plain", fileName3); 171 mDocsHelper.createDocument(rootDir1, "text/plain", fileName4); 172 } 173 assertDefaultContentOfTestDir0()174 void assertDefaultContentOfTestDir0() throws UiObjectNotFoundException { 175 bots.directory.waitForDocument(fileName1); 176 bots.directory.waitForDocument(fileName2); 177 bots.directory.waitForDocument(dirName1); 178 bots.directory.waitForDocument(fileNameNoRename); 179 bots.directory.assertDocumentsCount(4); 180 } 181 assertDefaultContentOfTestDir1()182 void assertDefaultContentOfTestDir1() throws UiObjectNotFoundException { 183 bots.directory.waitForDocument(fileName3); 184 bots.directory.waitForDocument(fileName4); 185 bots.directory.assertDocumentsCount(2); 186 } 187 188 /** 189 * Setup test Activity UI Mode YES or not(AUTO/YES/NO) before start to testing 190 * @param uiModeNight Constant for {@link #setNightMode(int)} 191 * 0 - MODE_NIGHT_AUTO 192 * 1 - MODE_NIGHT_NO 193 * 2 - MODE_NIGHT_YES 194 */ setSystemUiModeNight(int uiModeNight)195 protected void setSystemUiModeNight(int uiModeNight) { 196 int systemUiMode = getActivity().getResources().getConfiguration().uiMode 197 & Configuration.UI_MODE_NIGHT_MASK; 198 if(uiModeNight != systemUiMode) { 199 /* TODO since ag/4947691 enable config_lockDayNightMode to block app setNightMode() 200 create b/115315612 to handle the UiModeManager permission deny problem */ 201 mUiModeManager = (UiModeManager) getActivity() 202 .getSystemService(Context.UI_MODE_SERVICE); 203 mUiModeManager.setNightMode(uiModeNight); 204 device.waitForIdle(NIGHT_MODE_CHANGE_WAIT_TIME); 205 } 206 } 207 } 208