Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
Jun Ono | 9c53921 | 2018-01-19 18:37:08 +0900 | [diff] [blame] | 19 | import static com.android.documentsui.StubProvider.EXTRA_SIZE; |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 20 | import static com.android.documentsui.StubProvider.ROOT_0_ID; |
| 21 | import static com.android.documentsui.StubProvider.ROOT_1_ID; |
| 22 | |
Takamasa Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
Takamasa Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 24 | import android.content.Context; |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 27 | import android.net.Uri; |
Jun Ono | 9c53921 | 2018-01-19 18:37:08 +0900 | [diff] [blame] | 28 | import android.os.Bundle; |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 29 | import android.os.RemoteException; |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 30 | import android.util.Log; |
| 31 | |
Brett Chabot | 2027ca0 | 2018-12-13 19:06:31 -0800 | [diff] [blame] | 32 | import androidx.test.filters.LargeTest; |
| 33 | |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 34 | import com.android.documentsui.files.FilesActivity; |
Felka Chang | c181d52 | 2019-02-21 14:36:08 +0800 | [diff] [blame] | 35 | import com.android.documentsui.filters.HugeLongTest; |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 36 | import com.android.documentsui.services.TestNotificationService; |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 37 | |
| 38 | import java.util.concurrent.CountDownLatch; |
| 39 | import 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 |
| 47 | public class CancelFromNotificationUiTest extends ActivityTest<FilesActivity> { |
Takamasa Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 48 | private static final String TAG = "CancelFromNotificationUiTest"; |
| 49 | |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 50 | private static final String TARGET_FILE = "dummy.data"; |
| 51 | |
| 52 | private static final int BUFFER_SIZE = 10 * 1024 * 1024; |
| 53 | |
Takamasa Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 54 | private static final int WAIT_TIME_SECONDS = 60; |
| 55 | |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 56 | 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 Ono | 9c53921 | 2018-01-19 18:37:08 +0900 | [diff] [blame] | 85 | |
| 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 Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 92 | try { |
Takamasa Kuramitsu | 3bdd3f5 | 2018-06-05 12:21:07 +0900 | [diff] [blame] | 93 | bots.notifications.setNotificationAccess(getActivity(), true); |
Takamasa Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 94 | } catch (Exception e) { |
| 95 | Log.d(TAG, "Cannot set notification access. ", e); |
| 96 | } |
| 97 | |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 98 | 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 Kuramitsu | 3bdd3f5 | 2018-06-05 12:21:07 +0900 | [diff] [blame] | 118 | bots.notifications.setNotificationAccess(getActivity(), false); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 119 | } catch (Exception e) { |
Takamasa Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 120 | Log.d(TAG, "Cannot set notification access. ", e); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 121 | } |
| 122 | super.tearDown(); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public void initTestFiles() throws RemoteException { |
| 127 | try { |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 128 | 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 Huang | 3e3f4d9 | 2019-11-12 17:05:59 +0800 | [diff] [blame] | 141 | mDocsHelper.writeAppendDocument(uri, dummyByte, dummyByte.length); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Felka Chang | c181d52 | 2019-02-21 14:36:08 +0800 | [diff] [blame] | 145 | @HugeLongTest |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 146 | 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 Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 152 | bots.directory.selectDocument(TARGET_FILE, 1); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 153 | device.waitForIdle(); |
| 154 | |
Takamasa Kuramitsu | 3bdd3f5 | 2018-06-05 12:21:07 +0900 | [diff] [blame] | 155 | bots.main.clickToolbarOverflowItem(context.getResources().getString(R.string.menu_copy)); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 156 | device.waitForIdle(); |
| 157 | |
| 158 | bots.main.clickDialogCancelButton(); |
| 159 | device.waitForIdle(); |
| 160 | |
| 161 | bots.directory.waitForDocument(TARGET_FILE); |
| 162 | } |
| 163 | |
Felka Chang | c181d52 | 2019-02-21 14:36:08 +0800 | [diff] [blame] | 164 | @HugeLongTest |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 165 | public void testCopyDocument_CancelFromNotification() throws Exception { |
| 166 | bots.roots.openRoot(ROOT_0_ID); |
| 167 | bots.directory.findDocument(TARGET_FILE); |
| 168 | device.waitForIdle(); |
| 169 | |
Takamasa Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 170 | bots.directory.selectDocument(TARGET_FILE, 1); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 171 | device.waitForIdle(); |
| 172 | |
Takamasa Kuramitsu | 3bdd3f5 | 2018-06-05 12:21:07 +0900 | [diff] [blame] | 173 | bots.main.clickToolbarOverflowItem(context.getResources().getString(R.string.menu_copy)); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 174 | device.waitForIdle(); |
| 175 | |
| 176 | bots.roots.openRoot(ROOT_1_ID); |
| 177 | bots.main.clickDialogOkButton(); |
| 178 | device.waitForIdle(); |
| 179 | |
| 180 | try { |
Takamasa Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 181 | mCountDownLatch.await(WAIT_TIME_SECONDS, TimeUnit.SECONDS); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 182 | } 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 Chang | c181d52 | 2019-02-21 14:36:08 +0800 | [diff] [blame] | 197 | @HugeLongTest |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 198 | 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 Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 204 | bots.directory.selectDocument(TARGET_FILE, 1); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 205 | device.waitForIdle(); |
| 206 | |
Takamasa Kuramitsu | 3bdd3f5 | 2018-06-05 12:21:07 +0900 | [diff] [blame] | 207 | bots.main.clickToolbarOverflowItem(context.getResources().getString(R.string.menu_move)); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 208 | device.waitForIdle(); |
| 209 | |
| 210 | bots.main.clickDialogCancelButton(); |
| 211 | device.waitForIdle(); |
| 212 | |
| 213 | bots.directory.waitForDocument(TARGET_FILE); |
| 214 | } |
| 215 | |
Felka Chang | c181d52 | 2019-02-21 14:36:08 +0800 | [diff] [blame] | 216 | @HugeLongTest |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 217 | public void testMoveDocument_CancelFromNotification() throws Exception { |
| 218 | bots.roots.openRoot(ROOT_0_ID); |
| 219 | bots.directory.findDocument(TARGET_FILE); |
| 220 | device.waitForIdle(); |
| 221 | |
Takamasa Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 222 | bots.directory.selectDocument(TARGET_FILE, 1); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 223 | device.waitForIdle(); |
| 224 | |
Takamasa Kuramitsu | 3bdd3f5 | 2018-06-05 12:21:07 +0900 | [diff] [blame] | 225 | bots.main.clickToolbarOverflowItem(context.getResources().getString(R.string.menu_move)); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 226 | device.waitForIdle(); |
| 227 | |
| 228 | bots.roots.openRoot(ROOT_1_ID); |
| 229 | bots.main.clickDialogOkButton(); |
| 230 | device.waitForIdle(); |
| 231 | |
| 232 | try { |
Takamasa Kuramitsu | ff23d0b | 2018-05-24 11:44:26 +0900 | [diff] [blame] | 233 | mCountDownLatch.await(WAIT_TIME_SECONDS, TimeUnit.SECONDS); |
Jun Ono | 418e5a3 | 2017-11-22 10:29:30 +0900 | [diff] [blame] | 234 | } 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 | } |