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 import static junit.framework.Assert.fail; 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.files.LauncherActivity; 35 import com.android.documentsui.selection.ItemDetailsLookup.ItemDetails; 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.providers, 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 openItem(ItemDetails doc, @ViewType int type, @ViewType int fallback) { 81 throw new UnsupportedOperationException(); 82 } 83 84 @Override 85 public void initLocation(Intent intent) { 86 throw new UnsupportedOperationException(); 87 } 88 89 @Override 90 protected void launchToDefaultLocation() { 91 throw new UnsupportedOperationException(); 92 } 93 }; 94 } 95 96 @Test testOpenNewWindow()97 public void testOpenNewWindow() { 98 DocumentStack path = new DocumentStack(Roots.create("123")); 99 mHandler.openInNewWindow(path); 100 101 Intent expected = LauncherActivity.createLaunchIntent(mActivity); 102 expected.putExtra(Shared.EXTRA_STACK, (Parcelable) path); 103 Intent actual = mActivity.startActivity.getLastValue(); 104 assertEquals(expected.toString(), actual.toString()); 105 } 106 107 @Test testOpensContainerDocuments_OpenFolderInSearch_JumpsToNewLocation()108 public void testOpensContainerDocuments_OpenFolderInSearch_JumpsToNewLocation() 109 throws Exception { 110 if (!mEnv.features.isLaunchToDocumentEnabled()) { 111 return; 112 } 113 114 mEnv.populateStack(); 115 116 mEnv.searchViewManager.isSearching = true; 117 mEnv.docs.nextIsDocumentsUri = true; 118 mEnv.docs.nextPath = new Path( 119 TestProvidersAccess.HOME.rootId, 120 Arrays.asList(TestEnv.FOLDER_1.documentId, TestEnv.FOLDER_2.documentId)); 121 mEnv.docs.nextDocuments = Arrays.asList(TestEnv.FOLDER_1, TestEnv.FOLDER_2); 122 123 mHandler.openContainerDocument(TestEnv.FOLDER_2); 124 125 mEnv.beforeAsserts(); 126 127 assertEquals(mEnv.docs.nextPath.getPath().size(), mEnv.state.stack.size()); 128 assertEquals(TestEnv.FOLDER_2, mEnv.state.stack.pop()); 129 assertEquals(TestEnv.FOLDER_1, mEnv.state.stack.pop()); 130 } 131 132 133 @Test testOpensContainerDocuments_ClickFolderInSearch_PushToRootDoc_NoFindPathSupport()134 public void testOpensContainerDocuments_ClickFolderInSearch_PushToRootDoc_NoFindPathSupport() 135 throws Exception { 136 mEnv.populateStack(); 137 138 mEnv.searchViewManager.isSearching = true; 139 mEnv.docs.nextIsDocumentsUri = true; 140 mEnv.docs.nextDocuments = Arrays.asList(TestEnv.FOLDER_1, TestEnv.FOLDER_2); 141 142 mHandler.openContainerDocument(TestEnv.FOLDER_2); 143 144 mEnv.beforeAsserts(); 145 146 assertEquals(2, mEnv.state.stack.size()); 147 assertEquals(TestEnv.FOLDER_2, mEnv.state.stack.pop()); 148 assertEquals(TestEnv.FOLDER_0, mEnv.state.stack.pop()); 149 } 150 151 @Test testOpensContainerDocuments_ClickArchiveInSearch_opensArchiveInArchiveProvider()152 public void testOpensContainerDocuments_ClickArchiveInSearch_opensArchiveInArchiveProvider() 153 throws Exception { 154 if (!mEnv.features.isLaunchToDocumentEnabled()) { 155 return; 156 } 157 158 mEnv.populateStack(); 159 160 mEnv.searchViewManager.isSearching = true; 161 mEnv.docs.nextIsDocumentsUri = true; 162 mEnv.docs.nextPath = new Path( 163 TestProvidersAccess.HOME.rootId, 164 Arrays.asList(TestEnv.FOLDER_1.documentId, TestEnv.FOLDER_2.documentId, 165 TestEnv.FILE_ARCHIVE.documentId)); 166 mEnv.docs.nextDocuments = Arrays.asList( 167 TestEnv.FOLDER_1, TestEnv.FOLDER_2, TestEnv.FILE_ARCHIVE); 168 mEnv.docs.nextDocument = TestEnv.FILE_IN_ARCHIVE; 169 170 mHandler.openContainerDocument(TestEnv.FILE_ARCHIVE); 171 172 mEnv.beforeAsserts(); 173 174 assertEquals(mEnv.docs.nextPath.getPath().size(), mEnv.state.stack.size()); 175 assertEquals(TestEnv.FILE_IN_ARCHIVE, mEnv.state.stack.pop()); 176 assertEquals(TestEnv.FOLDER_2, mEnv.state.stack.pop()); 177 assertEquals(TestEnv.FOLDER_1, mEnv.state.stack.pop()); 178 } 179 180 @Test testOpensDocument_ExceptionIfAlreadyInStack()181 public void testOpensDocument_ExceptionIfAlreadyInStack() throws Exception { 182 mEnv.populateStack(); 183 try { 184 mEnv.state.stack.push(TestEnv.FOLDER_0); 185 fail("Should have thrown IllegalArgumentException."); 186 } catch (IllegalArgumentException expected) { 187 } 188 } 189 190 @Test testLaunchToDocuments()191 public void testLaunchToDocuments() throws Exception { 192 if (!mEnv.features.isLaunchToDocumentEnabled()) { 193 return; 194 } 195 196 mEnv.docs.nextIsDocumentsUri = true; 197 mEnv.docs.nextPath = new Path( 198 TestProvidersAccess.HOME.rootId, 199 Arrays.asList( 200 TestEnv.FOLDER_0.documentId, 201 TestEnv.FOLDER_1.documentId, 202 TestEnv.FILE_GIF.documentId)); 203 mEnv.docs.nextDocuments = 204 Arrays.asList(TestEnv.FOLDER_0, TestEnv.FOLDER_1, TestEnv.FILE_GIF); 205 206 mActivity.refreshCurrentRootAndDirectory.assertNotCalled(); 207 assertTrue(mHandler.launchToDocument(TestEnv.FILE_GIF.derivedUri)); 208 209 mEnv.beforeAsserts(); 210 211 DocumentStackAsserts.assertEqualsTo(mEnv.state.stack, TestProvidersAccess.HOME, 212 Arrays.asList(TestEnv.FOLDER_0, TestEnv.FOLDER_1)); 213 mActivity.refreshCurrentRootAndDirectory.assertCalled(); 214 } 215 216 @Test testLaunchToDocuments_convertsTreeUriToDocumentUri()217 public void testLaunchToDocuments_convertsTreeUriToDocumentUri() throws Exception { 218 if (!mEnv.features.isLaunchToDocumentEnabled()) { 219 return; 220 } 221 222 mEnv.docs.nextIsDocumentsUri = true; 223 mEnv.docs.nextPath = new Path( 224 TestProvidersAccess.HOME.rootId, 225 Arrays.asList( 226 TestEnv.FOLDER_0.documentId, 227 TestEnv.FOLDER_1.documentId, 228 TestEnv.FILE_GIF.documentId)); 229 mEnv.docs.nextDocuments = 230 Arrays.asList(TestEnv.FOLDER_0, TestEnv.FOLDER_1, TestEnv.FILE_GIF); 231 232 final Uri treeBaseUri = DocumentsContract.buildTreeDocumentUri( 233 TestProvidersAccess.HOME.authority, TestEnv.FOLDER_0.documentId); 234 final Uri treeDocUri = DocumentsContract.buildDocumentUriUsingTree( 235 treeBaseUri, TestEnv.FILE_GIF.documentId); 236 assertTrue(mHandler.launchToDocument(treeDocUri)); 237 238 mEnv.beforeAsserts(); 239 240 DocumentStackAsserts.assertEqualsTo(mEnv.state.stack, TestProvidersAccess.HOME, 241 Arrays.asList(TestEnv.FOLDER_0, TestEnv.FOLDER_1)); 242 mEnv.docs.lastUri.assertLastArgument(TestEnv.FILE_GIF.derivedUri); 243 mActivity.refreshCurrentRootAndDirectory.assertCalled(); 244 } 245 246 @Test testLoadChildrenDocuments()247 public void testLoadChildrenDocuments() throws Exception { 248 mEnv.state.stack.changeRoot(TestProvidersAccess.HOME); 249 mEnv.state.stack.push(TestEnv.FOLDER_0); 250 251 mEnv.state.sortModel.sortByUser( 252 SortModel.SORT_DIMENSION_ID_TITLE, SortDimension.SORT_DIRECTION_ASCENDING); 253 254 mEnv.mockProviders.get(TestProvidersAccess.HOME.authority) 255 .setNextChildDocumentsReturns(TestEnv.FILE_APK, TestEnv.FILE_GIF); 256 257 mHandler.loadDocumentsForCurrentStack(); 258 mActivity.loaderManager.runAsyncTaskLoader(AbstractActionHandler.LOADER_ID); 259 260 assertEquals(2, mEnv.model.getItemCount()); 261 String[] modelIds = mEnv.model.getModelIds(); 262 assertEquals(TestEnv.FILE_APK, mEnv.model.getDocument(modelIds[0])); 263 assertEquals(TestEnv.FILE_GIF, mEnv.model.getDocument(modelIds[1])); 264 } 265 266 @Test testLoadChildrenDocuments_failsWithNonRecentsAndEmptyStack()267 public void testLoadChildrenDocuments_failsWithNonRecentsAndEmptyStack() throws Exception { 268 mEnv.state.stack.changeRoot(TestProvidersAccess.HOME); 269 270 mEnv.mockProviders.get(TestProvidersAccess.HOME.authority) 271 .setNextChildDocumentsReturns(TestEnv.FILE_APK, TestEnv.FILE_GIF); 272 273 TestEventHandler<Model.Update> listener = new TestEventHandler<>(); 274 mEnv.model.addUpdateListener(listener::accept); 275 276 mHandler.loadDocumentsForCurrentStack(); 277 278 assertTrue(listener.getLastValue().hasException()); 279 } 280 } 281