blob: adcfef333defc8bbdf8e50e6fcc7e385e14964a6 [file] [log] [blame]
Tomasz Mikolajewskic7b83222016-02-04 17:46:35 +09001/*
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
17package com.android.documentsui;
18
19import static com.android.documentsui.StubProvider.DEFAULT_AUTHORITY;
20import static com.android.documentsui.StubProvider.ROOT_0_ID;
21import static com.android.documentsui.StubProvider.ROOT_1_ID;
22
Aga Wronskab9eeee12016-02-24 12:57:21 -080023import android.annotation.Nullable;
Tomasz Mikolajewskic7b83222016-02-04 17:46:35 +090024import android.app.Activity;
Tomasz Mikolajewskic7b83222016-02-04 17:46:35 +090025import android.content.ContentProviderClient;
26import android.content.ContentResolver;
27import android.content.Context;
28import android.content.Intent;
29import android.os.RemoteException;
30import android.provider.DocumentsContract.Document;
Tomasz Mikolajewskic7b83222016-02-04 17:46:35 +090031import android.support.test.uiautomator.Configurator;
32import android.support.test.uiautomator.UiDevice;
33import android.support.test.uiautomator.UiObjectNotFoundException;
Tomasz Mikolajewskic7b83222016-02-04 17:46:35 +090034import android.test.ActivityInstrumentationTestCase2;
35import android.view.MotionEvent;
36
Steve McKayb9a20d12016-02-19 12:57:05 -080037import com.android.documentsui.bots.DirectoryListBot;
38import com.android.documentsui.bots.KeyboardBot;
39import com.android.documentsui.bots.RootsListBot;
40import com.android.documentsui.bots.UiBot;
Tomasz Mikolajewskic7b83222016-02-04 17:46:35 +090041import 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 */
49public 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 McKayb9a20d12016-02-19 12:57:05 -080061 public Bots bots;
Tomasz Mikolajewskic7b83222016-02-04 17:46:35 +090062 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 Wronskab9eeee12016-02-24 12:57:21 -080076 /*
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 Mikolajewskic7b83222016-02-04 17:46:35 +090087 @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 McKayb9a20d12016-02-19 12:57:05 -080092
93 bots = new Bots(device, context, TIMEOUT);
Tomasz Mikolajewskic7b83222016-02-04 17:46:35 +090094
95 Configurator.getInstance().setToolType(MotionEvent.TOOL_TYPE_MOUSE);
Steve McKayb9a20d12016-02-19 12:57:05 -080096 bots.main.revealLauncher();
Tomasz Mikolajewskic7b83222016-02-04 17:46:35 +090097
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 McKayb9a20d12016-02-19 12:57:05 -0800107 bots.main.revealApp();
Tomasz Mikolajewskic7b83222016-02-04 17:46:35 +0900108 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 Wronskab9eeee12016-02-24 12:57:21 -0800121 if (getInitialRoot() != null) {
122 intent.setData(getInitialRoot().getUri());
123 }
Tomasz Mikolajewskic7b83222016-02-04 17:46:35 +0900124 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 McKayb9a20d12016-02-19 12:57:05 -0800145 bots.directory.assertDocumentsCount(4);
146 bots.directory.assertDocumentsPresent(fileName1, fileName2, dirName1, fileNameNoRename);
Tomasz Mikolajewskic7b83222016-02-04 17:46:35 +0900147 }
148
149 void assertDefaultContentOfTestDir1() throws UiObjectNotFoundException {
Steve McKayb9a20d12016-02-19 12:57:05 -0800150 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 Mikolajewskic7b83222016-02-04 17:46:35 +0900169 }
170}