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.net.Uri;
20 
21 import androidx.test.filters.LargeTest;
22 
23 import com.android.documentsui.files.FilesActivity;
24 import com.android.documentsui.sorting.SortDimension;
25 import com.android.documentsui.sorting.SortModel;
26 
27 @LargeTest
28 public class SortDocumentUiTest extends ActivityTest<FilesActivity> {
29 
30     private static final String DIR_1 = "folder_1";
31     private static final String DIR_2 = "dir_2";
32 
33     private static final String FILE_1 = "file_1";
34     private static final String FILE_2 = "doc_2";
35     private static final String FILE_3 = "image_3";
36 
37     private static final String MIME_1 = "text/plain"; // Plain text
38     private static final String MIME_2 = "text/html"; // HTML document
39     private static final String MIME_3 = "image/jpeg"; // JPG image
40 
41     private static final String[] FILES = { FILE_1, FILE_3, FILE_2 };
42     private static final String[] MIMES = { MIME_1, MIME_3, MIME_2 };
43     private static final String[] DIRS = { DIR_1, DIR_2 };
44 
45     private static final String[] DIRS_IN_NAME_ASC = { DIR_2, DIR_1 };
46     private static final String[] DIRS_IN_NAME_DESC = reverse(DIRS_IN_NAME_ASC);
47     private static final String[] FILES_IN_NAME_ASC = { FILE_2, FILE_1, FILE_3 };
48     private static final String[] FILES_IN_NAME_DESC = reverse(FILES_IN_NAME_ASC);
49 
50     private static final String[] FILES_IN_SIZE_ASC = { FILE_2, FILE_1, FILE_3 };
51     private static final String[] FILES_IN_SIZE_DESC = reverse(FILES_IN_SIZE_ASC);
52 
53     private static final String[] DIRS_IN_MODIFIED_DESC = reverse(DIRS);
54     private static final String[] FILES_IN_MODIFIED_DESC = reverse(FILES);
55 
56     private static final String[] FILES_IN_TYPE_ASC = { FILE_2, FILE_3, FILE_1 };
57     private static final String[] FILES_IN_TYPE_DESC = reverse(FILES_IN_TYPE_ASC);
58 
SortDocumentUiTest()59     public SortDocumentUiTest() {
60         super(FilesActivity.class);
61     }
62 
63     @Override
setUp()64     public void setUp() throws Exception {
65         super.setUp();
66         bots.roots.closeDrawer();
67     }
68 
initFiles()69     private void initFiles() throws Exception {
70         initFiles(0);
71     }
72 
73     /**
74      * Initiate test files. It allows waiting between creations of files, so that we can assure
75      * the modified date of each document is different.
76      * @param sleep time to sleep in ms
77      */
initFiles(long sleep)78     private void initFiles(long sleep) throws Exception {
79         for (int i = 0; i < FILES.length; ++i) {
80             Uri uri = mDocsHelper.createDocument(rootDir0, MIMES[i], FILES[i]);
81             mDocsHelper.writeDocument(uri, FILES[i].getBytes());
82 
83             Thread.sleep(sleep);
84         }
85 
86         for (String dir : DIRS) {
87             mDocsHelper.createFolder(rootDir0, dir);
88 
89             Thread.sleep(sleep);
90         }
91     }
92 
testDefaultSortByNameAscending()93     public void testDefaultSortByNameAscending() throws Exception {
94         initFiles();
95         bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_NAME_ASC);
96     }
97 
testSortByName_Descending_listMode()98     public void testSortByName_Descending_listMode() throws Exception {
99         initFiles();
100 
101         bots.main.switchToListMode();
102 
103         bots.sort.sortBy(
104                 SortModel.SORT_DIMENSION_ID_TITLE, SortDimension.SORT_DIRECTION_DESCENDING);
105         bots.directory.assertOrder(DIRS_IN_NAME_DESC, FILES_IN_NAME_DESC);
106     }
107 
testSortBySize_Ascending_listMode()108     public void testSortBySize_Ascending_listMode() throws Exception {
109         initFiles();
110 
111         bots.main.switchToListMode();
112 
113         bots.sort.sortBy(
114                 SortModel.SORT_DIMENSION_ID_SIZE, SortDimension.SORT_DIRECTION_ASCENDING);
115         bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_SIZE_ASC);
116     }
117 
testSortBySize_Descending_listMode()118     public void testSortBySize_Descending_listMode() throws Exception {
119         initFiles();
120 
121         bots.main.switchToListMode();
122 
123         bots.sort.sortBy(
124                 SortModel.SORT_DIMENSION_ID_SIZE, SortDimension.SORT_DIRECTION_DESCENDING);
125         bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_SIZE_DESC);
126     }
127 
testSortByModified_Ascending_listMode()128     public void testSortByModified_Ascending_listMode() throws Exception {
129         initFiles(1000);
130 
131         bots.main.switchToListMode();
132 
133         bots.sort.sortBy(
134                 SortModel.SORT_DIMENSION_ID_DATE, SortDimension.SORT_DIRECTION_ASCENDING);
135         bots.directory.assertOrder(DIRS, FILES);
136     }
137 
testSortByModified_Descending_listMode()138     public void testSortByModified_Descending_listMode() throws Exception {
139         initFiles(1000);
140 
141         bots.main.switchToListMode();
142 
143         bots.sort.sortBy(
144                 SortModel.SORT_DIMENSION_ID_DATE, SortDimension.SORT_DIRECTION_DESCENDING);
145         bots.directory.assertOrder(DIRS_IN_MODIFIED_DESC, FILES_IN_MODIFIED_DESC);
146     }
147 
testSortByType_Ascending_listMode()148     public void testSortByType_Ascending_listMode() throws Exception {
149         initFiles();
150 
151         bots.main.switchToListMode();
152 
153         bots.sort.sortBy(
154                 SortModel.SORT_DIMENSION_ID_FILE_TYPE, SortDimension.SORT_DIRECTION_ASCENDING);
155         bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_TYPE_ASC);
156     }
157 
testSortByType_Descending_listMode()158     public void testSortByType_Descending_listMode() throws Exception {
159         initFiles();
160 
161         bots.main.switchToListMode();
162 
163         bots.sort.sortBy(
164                 SortModel.SORT_DIMENSION_ID_FILE_TYPE, SortDimension.SORT_DIRECTION_DESCENDING);
165         bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_TYPE_DESC);
166     }
167 
testSortByName_Descending_gridMode()168     public void testSortByName_Descending_gridMode() throws Exception {
169         initFiles();
170 
171         bots.main.switchToGridMode();
172 
173         bots.sort.sortBy(
174                 SortModel.SORT_DIMENSION_ID_TITLE, SortDimension.SORT_DIRECTION_DESCENDING);
175         bots.directory.assertOrder(DIRS_IN_NAME_DESC, FILES_IN_NAME_DESC);
176     }
177 
testSortBySize_Ascending_gridMode()178     public void testSortBySize_Ascending_gridMode() throws Exception {
179         initFiles();
180 
181         bots.main.switchToGridMode();
182 
183         bots.sort.sortBy(
184                 SortModel.SORT_DIMENSION_ID_SIZE, SortDimension.SORT_DIRECTION_ASCENDING);
185         bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_SIZE_ASC);
186     }
187 
testSortBySize_Descending_gridMode()188     public void testSortBySize_Descending_gridMode() throws Exception {
189         initFiles();
190 
191         bots.main.switchToGridMode();
192 
193         bots.sort.sortBy(
194                 SortModel.SORT_DIMENSION_ID_SIZE, SortDimension.SORT_DIRECTION_DESCENDING);
195         bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_SIZE_DESC);
196     }
197 
testSortByModified_Ascending_gridMode()198     public void testSortByModified_Ascending_gridMode() throws Exception {
199         initFiles(1000);
200 
201         bots.main.switchToGridMode();
202 
203         bots.sort.sortBy(
204                 SortModel.SORT_DIMENSION_ID_DATE, SortDimension.SORT_DIRECTION_ASCENDING);
205         bots.directory.assertOrder(DIRS, FILES);
206     }
207 
testSortByModified_Descending_gridMode()208     public void testSortByModified_Descending_gridMode() throws Exception {
209         initFiles(1000);
210 
211         bots.main.switchToGridMode();
212 
213         bots.sort.sortBy(
214                 SortModel.SORT_DIMENSION_ID_DATE, SortDimension.SORT_DIRECTION_DESCENDING);
215         bots.directory.assertOrder(DIRS_IN_MODIFIED_DESC, FILES_IN_MODIFIED_DESC);
216     }
217 
testSortByType_Ascending_gridMode()218     public void testSortByType_Ascending_gridMode() throws Exception {
219         initFiles();
220 
221         bots.main.switchToGridMode();
222 
223         bots.sort.sortBy(
224                 SortModel.SORT_DIMENSION_ID_FILE_TYPE, SortDimension.SORT_DIRECTION_ASCENDING);
225         bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_TYPE_ASC);
226     }
227 
testSortByType_Descending_gridMode()228     public void testSortByType_Descending_gridMode() throws Exception {
229         initFiles();
230 
231         bots.main.switchToGridMode();
232 
233         bots.sort.sortBy(
234                 SortModel.SORT_DIMENSION_ID_FILE_TYPE, SortDimension.SORT_DIRECTION_DESCENDING);
235         bots.directory.assertOrder(DIRS_IN_NAME_ASC, FILES_IN_TYPE_DESC);
236     }
237 
reverse(String[] array)238     private static String[] reverse(String[] array) {
239         String[] ret = new String[array.length];
240 
241         for (int i = 0; i < array.length; ++i) {
242             ret[ret.length - i - 1] = array[i];
243         }
244 
245         return ret;
246     }
247 }
248