blob: 1c125c9d3c60df4b8cb959cc45f8b806dbb939de [file] [log] [blame]
Jun Ono418e5a32017-11-22 10:29:30 +09001/*
2 * Copyright (C) 2017 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
Jun Ono9c539212018-01-19 18:37:08 +090019import static com.android.documentsui.StubProvider.EXTRA_SIZE;
Jun Ono418e5a32017-11-22 10:29:30 +090020import static com.android.documentsui.StubProvider.ROOT_0_ID;
21import static com.android.documentsui.StubProvider.ROOT_1_ID;
22
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +090023import android.content.BroadcastReceiver;
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +090024import android.content.Context;
Jun Ono418e5a32017-11-22 10:29:30 +090025import android.content.Intent;
26import android.content.IntentFilter;
Jun Ono418e5a32017-11-22 10:29:30 +090027import android.net.Uri;
Jun Ono9c539212018-01-19 18:37:08 +090028import android.os.Bundle;
Jun Ono418e5a32017-11-22 10:29:30 +090029import android.os.RemoteException;
Jun Ono418e5a32017-11-22 10:29:30 +090030import android.util.Log;
31
Brett Chabot2027ca02018-12-13 19:06:31 -080032import androidx.test.filters.LargeTest;
33
Jun Ono418e5a32017-11-22 10:29:30 +090034import com.android.documentsui.files.FilesActivity;
Felka Changc181d522019-02-21 14:36:08 +080035import com.android.documentsui.filters.HugeLongTest;
Jun Ono418e5a32017-11-22 10:29:30 +090036import com.android.documentsui.services.TestNotificationService;
Jun Ono418e5a32017-11-22 10:29:30 +090037
38import java.util.concurrent.CountDownLatch;
39import java.util.concurrent.TimeUnit;
40
41/**
42* This class tests the below points.
43* - Cancel copying or moving file before starting it.
44* - Cancel during copying or moving file.
45*/
46@LargeTest
47public class CancelFromNotificationUiTest extends ActivityTest<FilesActivity> {
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +090048 private static final String TAG = "CancelFromNotificationUiTest";
49
Jun Ono418e5a32017-11-22 10:29:30 +090050 private static final String TARGET_FILE = "dummy.data";
51
52 private static final int BUFFER_SIZE = 10 * 1024 * 1024;
53
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +090054 private static final int WAIT_TIME_SECONDS = 60;
55
Jun Ono418e5a32017-11-22 10:29:30 +090056 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
57 @Override
58 public void onReceive(Context context, Intent intent) {
59 String action = intent.getAction();
60 if (TestNotificationService.ACTION_OPERATION_RESULT.equals(action)) {
61 mOperationExecuted = intent.getBooleanExtra(
62 TestNotificationService.EXTRA_RESULT, false);
63 if (!mOperationExecuted) {
64 mErrorReason = intent.getStringExtra(
65 TestNotificationService.EXTRA_ERROR_REASON);
66 }
67 mCountDownLatch.countDown();
68 }
69 }
70 };
71
72 private CountDownLatch mCountDownLatch;
73
74 private boolean mOperationExecuted;
75
76 private String mErrorReason;
77
78 public CancelFromNotificationUiTest() {
79 super(FilesActivity.class);
80 }
81
82 @Override
83 public void setUp() throws Exception {
84 super.setUp();
Jun Ono9c539212018-01-19 18:37:08 +090085
86 // super.setUp() method will change the storage size to 100MB.
87 // So, reset the storage size again to 500MB.
88 Bundle bundle = new Bundle();
89 bundle.putLong(EXTRA_SIZE, 500L);
90 mDocsHelper.configure(null, bundle);
91
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +090092 try {
Takamasa Kuramitsu3bdd3f52018-06-05 12:21:07 +090093 bots.notifications.setNotificationAccess(getActivity(), true);
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +090094 } catch (Exception e) {
95 Log.d(TAG, "Cannot set notification access. ", e);
96 }
97
Jun Ono418e5a32017-11-22 10:29:30 +090098 initTestFiles();
99
100 IntentFilter filter = new IntentFilter();
101 filter.addAction(TestNotificationService.ACTION_OPERATION_RESULT);
102 context.registerReceiver(mReceiver, filter);
103 context.sendBroadcast(new Intent(
104 TestNotificationService.ACTION_CHANGE_CANCEL_MODE));
105
106 mOperationExecuted = false;
107 mErrorReason = "No response from Notification";
108 mCountDownLatch = new CountDownLatch(1);
109 }
110
111 @Override
112 public void tearDown() throws Exception {
113 mCountDownLatch.countDown();
114 mCountDownLatch = null;
115
116 context.unregisterReceiver(mReceiver);
117 try {
Takamasa Kuramitsu3bdd3f52018-06-05 12:21:07 +0900118 bots.notifications.setNotificationAccess(getActivity(), false);
Jun Ono418e5a32017-11-22 10:29:30 +0900119 } catch (Exception e) {
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +0900120 Log.d(TAG, "Cannot set notification access. ", e);
Jun Ono418e5a32017-11-22 10:29:30 +0900121 }
122 super.tearDown();
123 }
124
125 @Override
126 public void initTestFiles() throws RemoteException {
127 try {
Jun Ono418e5a32017-11-22 10:29:30 +0900128 createDummyFile();
129 } catch (Exception e) {
130 fail("Initialization failed. " + e.toString());
131 }
132 }
133
134 private void createDummyFile() throws Exception {
135 Uri uri = mDocsHelper.createDocument(rootDir0, "*/*", TARGET_FILE);
136 byte[] dummyByte = new byte[BUFFER_SIZE];
137 mDocsHelper.writeDocument(uri, dummyByte);
138 for (int i = 0; i < 49; i++) {
139 dummyByte = null;
140 dummyByte = new byte[BUFFER_SIZE];
Tony Huang3e3f4d92019-11-12 17:05:59 +0800141 mDocsHelper.writeAppendDocument(uri, dummyByte, dummyByte.length);
Jun Ono418e5a32017-11-22 10:29:30 +0900142 }
143 }
144
Felka Changc181d522019-02-21 14:36:08 +0800145 @HugeLongTest
Jun Ono418e5a32017-11-22 10:29:30 +0900146 public void testCopyDocument_Cancel() throws Exception {
147 bots.roots.openRoot(ROOT_0_ID);
148
149 bots.directory.findDocument(TARGET_FILE);
150 device.waitForIdle();
151
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +0900152 bots.directory.selectDocument(TARGET_FILE, 1);
Jun Ono418e5a32017-11-22 10:29:30 +0900153 device.waitForIdle();
154
Takamasa Kuramitsu3bdd3f52018-06-05 12:21:07 +0900155 bots.main.clickToolbarOverflowItem(context.getResources().getString(R.string.menu_copy));
Jun Ono418e5a32017-11-22 10:29:30 +0900156 device.waitForIdle();
157
158 bots.main.clickDialogCancelButton();
159 device.waitForIdle();
160
161 bots.directory.waitForDocument(TARGET_FILE);
162 }
163
Felka Changc181d522019-02-21 14:36:08 +0800164 @HugeLongTest
Jun Ono418e5a32017-11-22 10:29:30 +0900165 public void testCopyDocument_CancelFromNotification() throws Exception {
166 bots.roots.openRoot(ROOT_0_ID);
167 bots.directory.findDocument(TARGET_FILE);
168 device.waitForIdle();
169
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +0900170 bots.directory.selectDocument(TARGET_FILE, 1);
Jun Ono418e5a32017-11-22 10:29:30 +0900171 device.waitForIdle();
172
Takamasa Kuramitsu3bdd3f52018-06-05 12:21:07 +0900173 bots.main.clickToolbarOverflowItem(context.getResources().getString(R.string.menu_copy));
Jun Ono418e5a32017-11-22 10:29:30 +0900174 device.waitForIdle();
175
176 bots.roots.openRoot(ROOT_1_ID);
177 bots.main.clickDialogOkButton();
178 device.waitForIdle();
179
180 try {
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +0900181 mCountDownLatch.await(WAIT_TIME_SECONDS, TimeUnit.SECONDS);
Jun Ono418e5a32017-11-22 10:29:30 +0900182 } catch (Exception e) {
183 fail("Cannot wait because of error." + e.toString());
184 }
185
186 assertTrue(mErrorReason, mOperationExecuted);
187
188 bots.roots.openRoot(ROOT_1_ID);
189 device.waitForIdle();
190 assertFalse(bots.directory.hasDocuments(TARGET_FILE));
191
192 bots.roots.openRoot(ROOT_0_ID);
193 device.waitForIdle();
194 assertTrue(bots.directory.hasDocuments(TARGET_FILE));
195 }
196
Felka Changc181d522019-02-21 14:36:08 +0800197 @HugeLongTest
Jun Ono418e5a32017-11-22 10:29:30 +0900198 public void testMoveDocument_Cancel() throws Exception {
199 bots.roots.openRoot(ROOT_0_ID);
200
201 bots.directory.findDocument(TARGET_FILE);
202 device.waitForIdle();
203
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +0900204 bots.directory.selectDocument(TARGET_FILE, 1);
Jun Ono418e5a32017-11-22 10:29:30 +0900205 device.waitForIdle();
206
Takamasa Kuramitsu3bdd3f52018-06-05 12:21:07 +0900207 bots.main.clickToolbarOverflowItem(context.getResources().getString(R.string.menu_move));
Jun Ono418e5a32017-11-22 10:29:30 +0900208 device.waitForIdle();
209
210 bots.main.clickDialogCancelButton();
211 device.waitForIdle();
212
213 bots.directory.waitForDocument(TARGET_FILE);
214 }
215
Felka Changc181d522019-02-21 14:36:08 +0800216 @HugeLongTest
Jun Ono418e5a32017-11-22 10:29:30 +0900217 public void testMoveDocument_CancelFromNotification() throws Exception {
218 bots.roots.openRoot(ROOT_0_ID);
219 bots.directory.findDocument(TARGET_FILE);
220 device.waitForIdle();
221
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +0900222 bots.directory.selectDocument(TARGET_FILE, 1);
Jun Ono418e5a32017-11-22 10:29:30 +0900223 device.waitForIdle();
224
Takamasa Kuramitsu3bdd3f52018-06-05 12:21:07 +0900225 bots.main.clickToolbarOverflowItem(context.getResources().getString(R.string.menu_move));
Jun Ono418e5a32017-11-22 10:29:30 +0900226 device.waitForIdle();
227
228 bots.roots.openRoot(ROOT_1_ID);
229 bots.main.clickDialogOkButton();
230 device.waitForIdle();
231
232 try {
Takamasa Kuramitsuff23d0b2018-05-24 11:44:26 +0900233 mCountDownLatch.await(WAIT_TIME_SECONDS, TimeUnit.SECONDS);
Jun Ono418e5a32017-11-22 10:29:30 +0900234 } catch (Exception e) {
235 fail("Cannot wait because of error." + e.toString());
236 }
237
238 assertTrue(mErrorReason, mOperationExecuted);
239
240 bots.roots.openRoot(ROOT_1_ID);
241 device.waitForIdle();
242 assertFalse(bots.directory.hasDocuments(TARGET_FILE));
243
244 bots.roots.openRoot(ROOT_0_ID);
245 device.waitForIdle();
246 assertTrue(bots.directory.hasDocuments(TARGET_FILE));
247 }
248}