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.Root;
     19 import com.android.documentsui.InspectorProvider;
     20 import com.android.documentsui.base.Providers;
     21 import com.android.documentsui.base.RootInfo;
     22 import com.android.documentsui.base.State;
     23 import com.android.documentsui.roots.ProvidersAccess;
     24 
     25 import java.util.ArrayList;
     26 import java.util.Collection;
     27 import java.util.HashMap;
     28 import java.util.List;
     29 import java.util.Map;
     30 
     31 import javax.annotation.Nullable;
     32 
     33 public class TestProvidersAccess implements ProvidersAccess {
     34 
     35     public static final RootInfo DOWNLOADS;
     36     public static final RootInfo HOME;
     37     public static final RootInfo HAMMY;
     38     public static final RootInfo PICKLES;
     39     public static final RootInfo RECENTS;
     40     public static final RootInfo INSPECTOR;
     41 
     42     static {
     43         DOWNLOADS = new RootInfo() {{
     44             flags = Root.FLAG_SUPPORTS_CREATE;
     45         }};
     46         DOWNLOADS.authority = Providers.AUTHORITY_DOWNLOADS;
     47         DOWNLOADS.rootId = Providers.ROOT_ID_DOWNLOADS;
     48         DOWNLOADS.flags = Root.FLAG_LOCAL_ONLY
     49                 | Root.FLAG_SUPPORTS_CREATE
     50                 | Root.FLAG_SUPPORTS_IS_CHILD
     51                 | Root.FLAG_SUPPORTS_RECENTS;
     52 
     53         HOME = new RootInfo();
     54         HOME.authority = Providers.AUTHORITY_STORAGE;
     55         HOME.rootId = Providers.ROOT_ID_HOME;
     56         HOME.flags = Root.FLAG_LOCAL_ONLY
     57                 | Root.FLAG_SUPPORTS_CREATE
     58                 | Root.FLAG_SUPPORTS_IS_CHILD
     59                 | Root.FLAG_SUPPORTS_RECENTS;
     60 
     61         HAMMY = new RootInfo();
     62         HAMMY.authority = "yummies";
     63         HAMMY.rootId = "hamsandwich";
     64 
     65         PICKLES = new RootInfo();
     66         PICKLES.authority = "yummies";
     67         PICKLES.rootId = "pickles";
     68 
     69         RECENTS = new RootInfo() {{
     70             // Special root for recents
     71             derivedType = RootInfo.TYPE_RECENTS;
     72             flags = Root.FLAG_LOCAL_ONLY | Root.FLAG_SUPPORTS_IS_CHILD;
     73             availableBytes = -1;
     74         }};
     75 
     76         INSPECTOR = new RootInfo();
     77         INSPECTOR.authority = InspectorProvider.AUTHORITY;
     78         INSPECTOR.rootId = InspectorProvider.ROOT_ID;
     79         INSPECTOR.flags = Root.FLAG_LOCAL_ONLY
     80             | Root.FLAG_SUPPORTS_CREATE;
     81     }
     82 
     83     public final Map<String, Collection<RootInfo>> roots = new HashMap<>();
     84     private @Nullable RootInfo nextRoot;
     85 
     86     public TestProvidersAccess() {
     87         add(DOWNLOADS);
     88         add(HOME);
     89         add(HAMMY);
     90         add(PICKLES);
     91     }
     92 
     93     private void add(RootInfo root) {
     94         if (!roots.containsKey(root.authority)) {
     95             roots.put(root.authority, new ArrayList<>());
     96         }
     97         roots.get(root.authority).add(root);
     98     }
     99 
    100     public void configurePm(TestPackageManager pm) {
    101         pm.addStubContentProviderForRoot(TestProvidersAccess.DOWNLOADS);
    102         pm.addStubContentProviderForRoot(TestProvidersAccess.HOME);
    103         pm.addStubContentProviderForRoot(TestProvidersAccess.HAMMY);
    104         pm.addStubContentProviderForRoot(TestProvidersAccess.PICKLES);
    105     }
    106 
    107     @Override
    108     public RootInfo getRootOneshot(String authority, String rootId) {
    109         if (roots.containsKey(authority)) {
    110             for (RootInfo root : roots.get(authority)) {
    111                 if (rootId.equals(root.rootId)) {
    112                     return root;
    113                 }
    114             }
    115         }
    116         return null;
    117     }
    118 
    119     @Override
    120     public Collection<RootInfo> getMatchingRootsBlocking(State state) {
    121         List<RootInfo> allRoots = new ArrayList<>();
    122         for (String authority : roots.keySet()) {
    123             allRoots.addAll(roots.get(authority));
    124         }
    125         return ProvidersAccess.getMatchingRoots(allRoots, state);
    126     }
    127 
    128     @Override
    129     public Collection<RootInfo> getRootsForAuthorityBlocking(String authority) {
    130         return roots.get(authority);
    131     }
    132 
    133     @Override
    134     public Collection<RootInfo> getRootsBlocking() {
    135         List<RootInfo> result = new ArrayList<>();
    136         for (Collection<RootInfo> vals : roots.values()) {
    137             result.addAll(vals);
    138         }
    139         return result;
    140     }
    141 
    142     @Override
    143     public RootInfo getDefaultRootBlocking(State state) {
    144         return DOWNLOADS;
    145     }
    146 
    147     @Override
    148     public RootInfo getRecentsRoot() {
    149         return RECENTS;
    150     }
    151 
    152     @Override
    153     public String getApplicationName(String authority) {
    154         return "Test Application";
    155     }
    156 
    157     @Override
    158     public String getPackageName(String authority) {
    159         return "com.android.documentsui";
    160     }
    161 }
    162