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 static junit.framework.Assert.assertTrue;
20 
21 import static org.junit.Assert.assertEquals;
22 
23 import android.content.Intent;
24 import android.net.Uri;
25 import android.os.Parcelable;
26 import android.provider.DocumentsContract;
27 import android.provider.DocumentsContract.Path;
28 import android.support.test.filters.MediumTest;
29 import android.support.test.runner.AndroidJUnit4;
30 
31 import com.android.documentsui.base.DocumentStack;
32 import com.android.documentsui.base.RootInfo;
33 import com.android.documentsui.base.Shared;
34 import com.android.documentsui.dirlist.DocumentDetails;
35 import com.android.documentsui.files.LauncherActivity;
36 import com.android.documentsui.sorting.SortDimension;
37 import com.android.documentsui.sorting.SortModel;
38 import com.android.documentsui.testing.DocumentStackAsserts;
39 import com.android.documentsui.testing.Roots;
40 import com.android.documentsui.testing.TestEnv;
41 import com.android.documentsui.testing.TestEventHandler;
42 import com.android.documentsui.testing.TestProvidersAccess;
43 
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 
48 import java.util.Arrays;
49 
50 /**
51  * A unit test *for* AbstractActionHandler, not an abstract test baseclass.
52  */
53 @RunWith(AndroidJUnit4.class)
54 @MediumTest
55 public class AbstractActionHandlerTest {
56 
57     private TestActivity mActivity;
58     private TestEnv mEnv;
59     private AbstractActionHandler<TestActivity> mHandler;
60 
61     @Before
setUp()62     public void setUp() {
63         mEnv = TestEnv.create();
64         mActivity = TestActivity.create(mEnv);
65         mHandler = new AbstractActionHandler<TestActivity>(
66                 mActivity,
67                 mEnv.state,
68                 mEnv.roots,
69                 mEnv.docs,
70                 mEnv.searchViewManager,
71                 mEnv::lookupExecutor,
72                 mEnv.injector) {
73 
74             @Override
75             public void openRoot(RootInfo root) {
76                 throw new UnsupportedOperationException();
77             }
78 
79             @Override
80             public boolean openDocument(DocumentDetails doc, @ViewType int type,
81                     @ViewType int fallback) {
82                 throw new UnsupportedOperationException();
83             }
84 
85             @Override
86             public void initLocation(Intent intent) {
87                 throw new UnsupportedOperationException();
88             }
89 
90             @Override
91             protected void launchToDefaultLocation() {
92                 throw new UnsupportedOperationException();
93             }
94         };
95     }
96 
97     @Test
testOpenNewWindow()98     public void testOpenNewWindow() {
99         DocumentStack path = new DocumentStack(Roots.create("123"));
100         mHandler.openInNewWindow(path);
101 
102         Intent expected = LauncherActivity.createLaunchIntent(mActivity);
103         expected.putExtra(Shared.EXTRA_STACK, (Parcelable) path);
104         Intent actual = mActivity.startActivity.getLastValue();
105         assertEquals(expected.toString(), actual.toString());
106     }
107 
108     @Test
testOpensContainerDocuments_jumpToNewLocation()109     public void testOpensContainerDocuments_jumpToNewLocation() throws Exception {
110         mEnv.populateStack();
111 
112         mEnv.searchViewManager.isSearching = true;
113         mEnv.docs.nextPath = new Path(
114                 TestProvidersAccess.HOME.rootId,
115                 Arrays.asList(TestEnv.FOLDER_1.documentId, TestEnv.FOLDER_2.documentId));
116         mEnv.docs.nextDocuments = Arrays.asList(TestEnv.FOLDER_1, TestEnv.FOLDER_2);
117 
118         mHandler.openContainerDocument(TestEnv.FOLDER_2);
119 
120         mEnv.beforeAsserts();
121 
122         assertEquals(mEnv.docs.nextPath.getPath().size(), mEnv.state.stack.size());
123         assertEquals(TestEnv.FOLDER_2, mEnv.state.stack.peek());
124     }
125 
126 
127     @Test
testOpensContainerDocuments_pushToRootDoc_NoFindPathSupport()128     public void testOpensContainerDocuments_pushToRootDoc_NoFindPathSupport() throws Exception {
129         mEnv.populateStack();
130 
131         mEnv.searchViewManager.isSearching = true;
132         mEnv.docs.nextDocuments = Arrays.asList(TestEnv.FOLDER_1, TestEnv.FOLDER_2);
133 
134         mHandler.openContainerDocument(TestEnv.FOLDER_2);
135 
136         mEnv.beforeAsserts();
137 
138         assertEquals(2, mEnv.state.stack.size());
139         assertEquals(TestEnv.FOLDER_2, mEnv.state.stack.pop());
140         assertEquals(TestEnv.FOLDER_0, mEnv.state.stack.pop());
141     }
142 
143     @Test
testOpensDocument_AssertionErrorIfAlreadyInStack()144     public void testOpensDocument_AssertionErrorIfAlreadyInStack() throws Exception {
145         mEnv.populateStack();
146         boolean threw = false;
147         try {
148             mEnv.state.stack.push(TestEnv.FOLDER_0);
149         } catch (AssertionError e) {
150             threw = true;
151         }
152         assertTrue(threw);
153     }
154 
155     @Test
testLaunchToDocuments()156     public void testLaunchToDocuments() throws Exception {
157         mEnv.docs.nextIsDocumentsUri = true;
158         mEnv.docs.nextPath = new Path(
159                 TestProvidersAccess.HOME.rootId,
160                 Arrays.asList(
161                         TestEnv.FOLDER_0.documentId,
162                         TestEnv.FOLDER_1.documentId,
163                         TestEnv.FILE_GIF.documentId));
164         mEnv.docs.nextDocuments =
165                 Arrays.asList(TestEnv.FOLDER_0, TestEnv.FOLDER_1, TestEnv.FILE_GIF);
166 
167         mActivity.refreshCurrentRootAndDirectory.assertNotCalled();
168         assertTrue(mHandler.launchToDocument(TestEnv.FILE_GIF.derivedUri));
169 
170         mEnv.beforeAsserts();
171 
172         DocumentStackAsserts.assertEqualsTo(mEnv.state.stack, TestProvidersAccess.HOME,
173                 Arrays.asList(TestEnv.FOLDER_0, TestEnv.FOLDER_1));
174         mActivity.refreshCurrentRootAndDirectory.assertCalled();
175     }
176 
177     @Test
testLaunchToDocuments_convertsTreeUriToDocumentUri()178     public void testLaunchToDocuments_convertsTreeUriToDocumentUri() throws Exception {
179         mEnv.docs.nextIsDocumentsUri = true;
180         mEnv.docs.nextPath = new Path(
181                 TestProvidersAccess.HOME.rootId,
182                 Arrays.asList(
183                         TestEnv.FOLDER_0.documentId,
184                         TestEnv.FOLDER_1.documentId,
185                         TestEnv.FILE_GIF.documentId));
186         mEnv.docs.nextDocuments =
187                 Arrays.asList(TestEnv.FOLDER_0, TestEnv.FOLDER_1, TestEnv.FILE_GIF);
188 
189         final Uri treeBaseUri = DocumentsContract.buildTreeDocumentUri(
190                 TestProvidersAccess.HOME.authority, TestEnv.FOLDER_0.documentId);
191         final Uri treeDocUri = DocumentsContract.buildDocumentUriUsingTree(
192                 treeBaseUri, TestEnv.FILE_GIF.documentId);
193         assertTrue(mHandler.launchToDocument(treeDocUri));
194 
195         mEnv.beforeAsserts();
196 
197         DocumentStackAsserts.assertEqualsTo(mEnv.state.stack, TestProvidersAccess.HOME,
198                 Arrays.asList(TestEnv.FOLDER_0, TestEnv.FOLDER_1));
199         mEnv.docs.lastUri.assertLastArgument(TestEnv.FILE_GIF.derivedUri);
200         mActivity.refreshCurrentRootAndDirectory.assertCalled();
201     }
202 
203     @Test
testLoadChildrenDocuments()204     public void testLoadChildrenDocuments() throws Exception {
205         mEnv.state.stack.changeRoot(TestProvidersAccess.HOME);
206         mEnv.state.stack.push(TestEnv.FOLDER_0);
207 
208         mEnv.state.sortModel.sortByUser(
209                 SortModel.SORT_DIMENSION_ID_TITLE, SortDimension.SORT_DIRECTION_ASCENDING);
210 
211         mEnv.providers.get(TestProvidersAccess.HOME.authority)
212                 .setNextChildDocumentsReturns(TestEnv.FILE_APK, TestEnv.FILE_GIF);
213 
214         mHandler.loadDocumentsForCurrentStack();
215         mActivity.loaderManager.runAsyncTaskLoader(AbstractActionHandler.LOADER_ID);
216 
217         assertEquals(2, mEnv.model.getItemCount());
218         String[] modelIds = mEnv.model.getModelIds();
219         assertEquals(TestEnv.FILE_APK, mEnv.model.getDocument(modelIds[0]));
220         assertEquals(TestEnv.FILE_GIF, mEnv.model.getDocument(modelIds[1]));
221     }
222 
223     @Test
testLoadChildrenDocuments_failsWithNonRecentsAndEmptyStack()224     public void testLoadChildrenDocuments_failsWithNonRecentsAndEmptyStack() throws Exception {
225         mEnv.state.stack.changeRoot(TestProvidersAccess.HOME);
226 
227         mEnv.providers.get(TestProvidersAccess.HOME.authority)
228                 .setNextChildDocumentsReturns(TestEnv.FILE_APK, TestEnv.FILE_GIF);
229 
230         TestEventHandler<Model.Update> listener = new TestEventHandler<>();
231         mEnv.model.addUpdateListener(listener::accept);
232 
233         mHandler.loadDocumentsForCurrentStack();
234 
235         assertTrue(listener.getLastValue().hasException());
236     }
237 }
238