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 package com.android.documentsui.testing; 17 18 import android.provider.DocumentsContract.Document; 19 import android.support.test.InstrumentationRegistry; 20 import android.test.mock.MockContentResolver; 21 22 import com.android.documentsui.FocusManager; 23 import com.android.documentsui.Injector; 24 import com.android.documentsui.archives.ArchivesProvider; 25 import com.android.documentsui.base.DocumentInfo; 26 import com.android.documentsui.base.Features; 27 import com.android.documentsui.base.RootInfo; 28 import com.android.documentsui.base.State; 29 import com.android.documentsui.dirlist.TestFocusHandler; 30 import com.android.documentsui.selection.SelectionManager; 31 import com.android.documentsui.sorting.SortModel; 32 import com.android.documentsui.ui.TestDialogController; 33 34 import junit.framework.Assert; 35 36 import java.util.ArrayList; 37 import java.util.HashMap; 38 import java.util.List; 39 import java.util.Map; 40 import java.util.concurrent.Executor; 41 42 public class TestEnv { 43 44 public static DocumentInfo FOLDER_0; 45 public static DocumentInfo FOLDER_1; 46 public static DocumentInfo FOLDER_2; 47 public static DocumentInfo FILE_TXT; 48 public static DocumentInfo FILE_PNG; 49 public static DocumentInfo FILE_JPG; 50 public static DocumentInfo FILE_GIF; 51 public static DocumentInfo FILE_PDF; 52 public static DocumentInfo FILE_APK; 53 public static DocumentInfo FILE_PARTIAL; 54 public static DocumentInfo FILE_ARCHIVE; 55 public static DocumentInfo FILE_IN_ARCHIVE; 56 public static DocumentInfo FILE_VIRTUAL; 57 public static DocumentInfo FILE_READ_ONLY; 58 59 public final TestScheduledExecutorService mExecutor; 60 public final State state = new State(); 61 public final TestProvidersAccess providers = new TestProvidersAccess(); 62 public final TestDocumentsAccess docs = new TestDocumentsAccess(); 63 public final TestFocusHandler focusHandler = new TestFocusHandler(); 64 public final TestDialogController dialogs = new TestDialogController(); 65 public final TestModel model; 66 public final TestModel archiveModel; 67 public final SelectionManager selectionMgr; 68 public final TestSearchViewManager searchViewManager; 69 public final Injector injector; 70 public final Features features; 71 72 public final MockContentResolver contentResolver; 73 public final Map<String, TestDocumentsProvider> mockProviders; 74 TestEnv(String authority)75 private TestEnv(String authority) { 76 state.sortModel = SortModel.createModel(); 77 mExecutor = new TestScheduledExecutorService(); 78 features = new Features.RuntimeFeatures( 79 InstrumentationRegistry.getInstrumentation().getTargetContext().getResources(), 80 null); 81 model = new TestModel(authority, features); 82 archiveModel = new TestModel(ArchivesProvider.AUTHORITY, features); 83 selectionMgr = SelectionManagers.createTestInstance(); 84 searchViewManager = new TestSearchViewManager(); 85 injector = new Injector( 86 features, 87 new TestActivityConfig(), 88 null, //ScopedPreferences are not required for tests 89 null, //a MessageBuilder is not required for tests 90 dialogs, 91 new TestFileTypeLookup(), 92 (roots) -> {}, // not sure why, but java gets angry when I declare roots type. 93 model); 94 95 injector.selectionMgr = selectionMgr; 96 injector.focusManager = new FocusManager(features, selectionMgr, null, null, 0); 97 injector.searchManager = searchViewManager; 98 99 contentResolver = new MockContentResolver(); 100 mockProviders = new HashMap<>(providers.getRootsBlocking().size()); 101 registerProviders(); 102 } 103 registerProviders()104 private void registerProviders() { 105 for (RootInfo root : providers.getRootsBlocking()) { 106 if (!mockProviders.containsKey(root.authority)) { 107 TestDocumentsProvider provider = new TestDocumentsProvider(root.authority); 108 contentResolver.addProvider(root.authority, provider); 109 mockProviders.put(root.authority, provider); 110 } 111 } 112 } 113 create()114 public static TestEnv create() { 115 return create(TestProvidersAccess.HOME.authority); 116 } 117 create(String authority)118 public static TestEnv create(String authority) { 119 TestEnv env = new TestEnv(authority); 120 env.reset(); 121 return env; 122 } 123 clear()124 public void clear() { 125 model.reset(); 126 model.update(); 127 } 128 reset()129 public void reset() { 130 model.reset(); 131 FOLDER_0 = model.createFolder("folder 0"); 132 FOLDER_1 = model.createFolder("folder 1"); 133 FOLDER_2 = model.createFolder("folder 2"); 134 FILE_TXT = model.createFile("woowoo.txt"); 135 FILE_PNG = model.createFile("peppey.png"); 136 FILE_JPG = model.createFile("jiffy.jpg"); 137 FILE_GIF = model.createFile("glibby.gif"); 138 FILE_PDF = model.createFile("busy.pdf"); 139 FILE_APK = model.createFile("becareful.apk"); 140 FILE_PARTIAL = model.createFile( 141 "UbuntuFlappyBird.iso", 142 Document.FLAG_SUPPORTS_DELETE 143 | Document.FLAG_PARTIAL); 144 FILE_READ_ONLY = model.createFile("topsecretsystemfile.bin", 0); 145 FILE_ARCHIVE = model.createFile("whatsinthere.zip"); 146 FILE_IN_ARCHIVE = archiveModel.createFile("whatsinthere.png", 0); 147 FILE_VIRTUAL = model.createDocument( 148 "virtualdoc.vnd", 149 "application/vnd.google-apps.document", 150 Document.FLAG_VIRTUAL_DOCUMENT 151 | Document.FLAG_SUPPORTS_DELETE 152 | Document.FLAG_SUPPORTS_RENAME); 153 154 archiveModel.update(); 155 model.update(); 156 } 157 populateStack()158 public void populateStack() { 159 DocumentInfo rootDoc = model.getDocument("1"); 160 Assert.assertNotNull(rootDoc); 161 Assert.assertEquals(rootDoc.displayName, FOLDER_0.displayName); 162 163 state.stack.changeRoot(TestProvidersAccess.HOME); 164 state.stack.push(rootDoc); 165 } 166 beforeAsserts()167 public void beforeAsserts() throws Exception { 168 mExecutor.waitForTasks(30000); // 30 secs 169 } 170 lookupExecutor(String authority)171 public Executor lookupExecutor(String authority) { 172 return mExecutor; 173 } 174 selectDocument(DocumentInfo info)175 public void selectDocument(DocumentInfo info) { 176 List<String> ids = new ArrayList<>(1); 177 ids.add(info.documentId); 178 selectionMgr.setItemsSelected(ids, true); 179 } 180 } 181