• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.bots.UiBot;
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 = context.getPackageManager().getLaunchIntentForPackage(
149                 UiBot.targetPackageName);
150         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
151         if (getInitialRoot() != null) {
152             intent.setAction(Intent.ACTION_VIEW);
153             intent.setDataAndType(getInitialRoot().getUri(), DocumentsContract.Root.MIME_TYPE_ITEM);
154         }
155         setActivityIntent(intent);
156         getActivity();  // Launch the activity.
157     }
158 
resetStorage()159     protected void resetStorage() throws RemoteException {
160         mClient.call("clear", null, null);
161         device.waitForIdle();
162     }
163 
initTestFiles()164     protected void initTestFiles() throws RemoteException {
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 
assertDefaultContentOfTestDir0()175     void assertDefaultContentOfTestDir0() throws UiObjectNotFoundException {
176         bots.directory.waitForDocument(fileName1);
177         bots.directory.waitForDocument(fileName2);
178         bots.directory.waitForDocument(dirName1);
179         bots.directory.waitForDocument(fileNameNoRename);
180         bots.directory.assertDocumentsCount(4);
181     }
182 
assertDefaultContentOfTestDir1()183     void assertDefaultContentOfTestDir1() throws UiObjectNotFoundException {
184         bots.directory.waitForDocument(fileName3);
185         bots.directory.waitForDocument(fileName4);
186         bots.directory.assertDocumentsCount(2);
187     }
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      */
setSystemUiModeNight(int uiModeNight)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     }
208 }
209