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