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