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 23 import android.annotation.Nullable; 24 import android.app.Activity; 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; 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.util.Log; 36 import android.view.MotionEvent; 37 38 import com.android.documentsui.BaseActivity; 39 import com.android.documentsui.EventListener; 40 import com.android.documentsui.bots.DirectoryListBot; 41 import com.android.documentsui.bots.KeyboardBot; 42 import com.android.documentsui.bots.RootsListBot; 43 import com.android.documentsui.bots.UiBot; 44 import com.android.documentsui.model.RootInfo; 45 46 /** 47 * Provides basic test environment for UI tests: 48 * - Launches activity 49 * - Creates and gives access to test root directories and test files 50 * - Cleans up the test environment 51 */ 52 public abstract class ActivityTest<T extends Activity> extends ActivityInstrumentationTestCase2<T> { 53 54 static final int TIMEOUT = 5000; 55 56 // Testing files. For custom ones, override initTestFiles(). 57 public static final String dirName1 = "Dir1"; 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 68 public RootInfo rootDir0; 69 public RootInfo rootDir1; 70 ContentResolver mResolver; 71 DocumentsProviderHelper mDocsHelper; 72 ContentProviderClient mClient; 73 ActivityTest(Class<T> activityClass)74 public ActivityTest(Class<T> activityClass) { 75 super(activityClass); 76 } 77 78 /* 79 * Returns the root that will be opened within the activity. 80 * By default tests are started with one of the test roots. 81 * Override the method if you want to open different root on start. 82 * @return Root that will be opened. Return null if you want to open activity's default root. 83 */ 84 @Nullable getInitialRoot()85 protected RootInfo getInitialRoot() { 86 return rootDir0; 87 } 88 89 /** 90 * Returns the authority of the testing provider begin used. 91 * By default it's StubProvider's authority. 92 * @return Authority of the provider. 93 */ getTestingProviderAuthority()94 protected String getTestingProviderAuthority() { 95 return DEFAULT_AUTHORITY; 96 } 97 98 /** 99 * Resolves testing roots. 100 */ setupTestingRoots()101 protected void setupTestingRoots() throws RemoteException { 102 rootDir0 = mDocsHelper.getRoot(ROOT_0_ID); 103 rootDir1 = mDocsHelper.getRoot(ROOT_1_ID); 104 } 105 106 @Override setUp()107 public void setUp() throws Exception { 108 device = UiDevice.getInstance(getInstrumentation()); 109 // NOTE: Must be the "target" context, else security checks in content provider will fail. 110 context = getInstrumentation().getTargetContext(); 111 112 bots = new Bots(device, context, TIMEOUT); 113 114 Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_MOUSE); 115 116 mResolver = context.getContentResolver(); 117 mClient = mResolver.acquireUnstableContentProviderClient(getTestingProviderAuthority()); 118 mDocsHelper = new DocumentsProviderHelper(getTestingProviderAuthority(), mClient); 119 120 setupTestingRoots(); 121 122 launchActivity(); 123 resetStorage(); 124 } 125 126 @Override tearDown()127 public void tearDown() throws Exception { 128 mClient.release(); 129 super.tearDown(); 130 } 131 launchActivity()132 void launchActivity() { 133 final Intent intent = context.getPackageManager().getLaunchIntentForPackage( 134 UiBot.TARGET_PKG); 135 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); 136 if (getInitialRoot() != null) { 137 intent.setData(getInitialRoot().getUri()); 138 } 139 setActivityIntent(intent); 140 getActivity(); // Launch the activity. 141 } 142 resetStorage()143 void resetStorage() throws RemoteException { 144 mClient.call("clear", null, null); 145 device.waitForIdle(); 146 } 147 initTestFiles()148 void initTestFiles() throws RemoteException { 149 mDocsHelper.createFolder(rootDir0, dirName1); 150 mDocsHelper.createDocument(rootDir0, "text/plain", fileName1); 151 mDocsHelper.createDocument(rootDir0, "image/png", fileName2); 152 mDocsHelper.createDocumentWithFlags(rootDir0.documentId, "text/plain", fileNameNoRename, 153 Document.FLAG_SUPPORTS_WRITE); 154 155 mDocsHelper.createDocument(rootDir1, "text/plain", fileName3); 156 mDocsHelper.createDocument(rootDir1, "text/plain", fileName4); 157 } 158 assertDefaultContentOfTestDir0()159 void assertDefaultContentOfTestDir0() throws UiObjectNotFoundException { 160 bots.directory.assertDocumentsCount(4); 161 bots.directory.assertDocumentsPresent(fileName1, fileName2, dirName1, fileNameNoRename); 162 } 163 assertDefaultContentOfTestDir1()164 void assertDefaultContentOfTestDir1() throws UiObjectNotFoundException { 165 bots.directory.assertDocumentsCount(2); 166 bots.directory.assertDocumentsPresent(fileName3, fileName4); 167 } 168 169 /** 170 * Handy collection of bots for working with Files app. 171 */ 172 public static final class Bots { 173 public final UiBot main; 174 public final RootsListBot roots; 175 public final DirectoryListBot directory; 176 public final KeyboardBot keyboard; 177 Bots(UiDevice device, Context context, int timeout)178 private Bots(UiDevice device, Context context, int timeout) { 179 this.main = new UiBot(device, context, TIMEOUT); 180 this.roots = new RootsListBot(device, context, TIMEOUT); 181 this.directory = new DirectoryListBot(device, context, TIMEOUT); 182 this.keyboard = new KeyboardBot(device, context, TIMEOUT); 183 } 184 } 185 } 186