1 /*
2  * Copyright (C) 2018 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 androidx.test.filters.LargeTest;
20 
21 import com.android.documentsui.base.Providers;
22 import com.android.documentsui.base.RootInfo;
23 import com.android.documentsui.files.FilesActivity;
24 import com.android.documentsui.filters.HugeLongTest;
25 
26 /**
27  * A Ui test will do tests in the internal storage root. It is implemented because some operations
28  * is failed and its result will different from the test on the StubProvider. b/115304092 is a
29  * example which only happen on root from ExternalStorageProvidrer.
30  */
31 @LargeTest
32 public class InternalStorageUiTest extends ActivityTest<FilesActivity> {
33 
34     private static final String fileName = "!Test3345678";
35     private static final String newFileName = "!9527Test";
36     private RootInfo rootPrimary;
37 
InternalStorageUiTest()38     public InternalStorageUiTest() {
39         super(FilesActivity.class);
40     }
41 
42     @Override
setUp()43     public void setUp() throws Exception {
44         super.setUp();
45 
46         mDocsHelper = new DocumentsProviderHelper(userId, Providers.AUTHORITY_STORAGE, context,
47                 Providers.AUTHORITY_STORAGE);
48         rootPrimary = mDocsHelper.getRoot(Providers.ROOT_ID_DEVICE);
49 
50         bots.roots.openRoot(rootPrimary.title);
51         deleteTestFiles();
52     }
53 
54     @Override
tearDown()55     public void tearDown() throws Exception {
56         deleteTestFiles();
57         super.tearDown();
58     }
59 
60     @HugeLongTest
testRenameFile()61     public void testRenameFile() throws Exception {
62         createTestFiles();
63 
64         bots.directory.selectDocument(fileName);
65         device.waitForIdle();
66 
67         bots.main.clickRename();
68 
69         bots.main.setDialogText(newFileName);
70         device.waitForIdle();
71 
72         bots.keyboard.pressEnter();
73 
74         bots.directory.assertDocumentsAbsent(fileName);
75         bots.directory.assertDocumentsPresent(newFileName);
76         // Snackbar will not show if no exception.
77         assertNull(bots.directory.getSnackbar(context.getString(R.string.rename_error)));
78     }
79 
createTestFiles()80     private void createTestFiles() {
81         mDocsHelper.createFolder(rootPrimary, fileName);
82     }
83 
deleteTestFiles()84     private void deleteTestFiles() throws Exception {
85         boolean selected = false;
86         // Delete the added file for not affect user and also avoid error on next test.
87         if (bots.directory.hasDocuments(fileName)) {
88             bots.directory.selectDocument(fileName);
89             device.waitForIdle();
90             selected = true;
91         }
92         if (bots.directory.hasDocuments(newFileName)) {
93             bots.directory.selectDocument(newFileName);
94             device.waitForIdle();
95             selected = true;
96         }
97         if (selected) {
98             bots.main.clickDelete();
99             device.waitForIdle();
100             bots.main.clickDialogOkButton();
101         }
102     }
103 }
104