1 /*
2  * Copyright (C) 2013 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.roots;
18 
19 import static com.google.common.collect.Lists.newArrayList;
20 
21 import android.test.AndroidTestCase;
22 import android.test.suitebuilder.annotation.SmallTest;
23 
24 import com.android.documentsui.base.Providers;
25 import com.android.documentsui.base.RootInfo;
26 import com.android.documentsui.base.State;
27 
28 import com.google.common.collect.Lists;
29 
30 import java.util.List;
31 
32 @SmallTest
33 public class ProvidersCacheTest extends AndroidTestCase {
34 
35     private static RootInfo mNull = new RootInfo();
36     private static RootInfo mEmpty = buildForMimeTypes();
37     private static RootInfo mWild = buildForMimeTypes("*/*");
38     private static RootInfo mImages = buildForMimeTypes("image/*");
39     private static RootInfo mAudio = buildForMimeTypes(
40             "audio/*", "application/ogg", "application/x-flac");
41     private static RootInfo mDocs = buildForMimeTypes(
42             "application/msword", "application/vnd.ms-excel");
43     private static RootInfo mMalformed1 = buildForMimeTypes("meow");
44     private static RootInfo mMalformed2 = buildForMimeTypes("*/meow");
45 
46     private List<RootInfo> mRoots;
47 
48     private State mState;
49 
50     @Override
setUp()51     protected void setUp() throws Exception {
52         super.setUp();
53 
54         mRoots = Lists.newArrayList(
55                 mNull, mWild, mEmpty, mImages, mAudio, mDocs, mMalformed1, mMalformed2);
56 
57         mState = new State();
58         mState.action = State.ACTION_OPEN;
59         mState.showAdvanced = true;
60         mState.localOnly = false;
61     }
62 
testMatchingRoots_Everything()63     public void testMatchingRoots_Everything() throws Exception {
64         mState.acceptMimes = new String[] { "*/*" };
65         assertContainsExactly(
66                 newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
67                 ProvidersAccess.getMatchingRoots(mRoots, mState));
68     }
69 
testMatchingRoots_DirectoryCopy()70     public void testMatchingRoots_DirectoryCopy() throws Exception {
71         RootInfo downloads = buildForMimeTypes("*/*");
72         downloads.authority = Providers.AUTHORITY_DOWNLOADS;
73         mRoots.add(downloads);
74 
75         mState.acceptMimes = new String[] { "*/*" };
76         mState.directoryCopy = true;
77 
78         // basically we're asserting that the results don't contain downloads
79         assertContainsExactly(
80                 newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
81                 ProvidersAccess.getMatchingRoots(mRoots, mState));
82     }
83 
testMatchingRoots_PngOrWild()84     public void testMatchingRoots_PngOrWild() throws Exception {
85         mState.acceptMimes = new String[] { "image/png", "*/*" };
86         assertContainsExactly(
87                 newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
88                 ProvidersAccess.getMatchingRoots(mRoots, mState));
89     }
90 
testMatchingRoots_AudioWild()91     public void testMatchingRoots_AudioWild() throws Exception {
92         mState.acceptMimes = new String[] { "audio/*" };
93         assertContainsExactly(
94                 newArrayList(mNull, mWild, mAudio),
95                 ProvidersAccess.getMatchingRoots(mRoots, mState));
96     }
97 
testMatchingRoots_AudioWildOrImageWild()98     public void testMatchingRoots_AudioWildOrImageWild() throws Exception {
99         mState.acceptMimes = new String[] { "audio/*", "image/*" };
100         assertContainsExactly(
101                 newArrayList(mNull, mWild, mAudio, mImages),
102                 ProvidersAccess.getMatchingRoots(mRoots, mState));
103     }
104 
testMatchingRoots_AudioSpecific()105     public void testMatchingRoots_AudioSpecific() throws Exception {
106         mState.acceptMimes = new String[] { "audio/mpeg" };
107         assertContainsExactly(
108                 newArrayList(mNull, mWild, mAudio),
109                 ProvidersAccess.getMatchingRoots(mRoots, mState));
110     }
111 
testMatchingRoots_Document()112     public void testMatchingRoots_Document() throws Exception {
113         mState.acceptMimes = new String[] { "application/msword" };
114         assertContainsExactly(
115                 newArrayList(mNull, mWild, mDocs),
116                 ProvidersAccess.getMatchingRoots(mRoots, mState));
117     }
118 
testMatchingRoots_Application()119     public void testMatchingRoots_Application() throws Exception {
120         mState.acceptMimes = new String[] { "application/*" };
121         assertContainsExactly(
122                 newArrayList(mNull, mWild, mAudio, mDocs),
123                 ProvidersAccess.getMatchingRoots(mRoots, mState));
124     }
125 
testMatchingRoots_FlacOrPng()126     public void testMatchingRoots_FlacOrPng() throws Exception {
127         mState.acceptMimes = new String[] { "application/x-flac", "image/png" };
128         assertContainsExactly(
129                 newArrayList(mNull, mWild, mAudio, mImages),
130                 ProvidersAccess.getMatchingRoots(mRoots, mState));
131     }
132 
testExcludedAuthorities()133     public void testExcludedAuthorities() throws Exception {
134         final List<RootInfo> roots = newArrayList();
135 
136         // Set up some roots
137         for (int i = 0; i < 5; ++i) {
138             RootInfo root = new RootInfo();
139             root.authority = "authority" + i;
140             roots.add(root);
141         }
142         // Make some allowed authorities
143         List<RootInfo> allowedRoots = newArrayList(
144             roots.get(0), roots.get(2), roots.get(4));
145         // Set up the excluded authority list
146         for (RootInfo root: roots) {
147             if (!allowedRoots.contains(root)) {
148                 mState.excludedAuthorities.add(root.authority);
149             }
150         }
151         mState.acceptMimes = new String[] { "*/*" };
152 
153         assertContainsExactly(
154             allowedRoots,
155             ProvidersAccess.getMatchingRoots(roots, mState));
156     }
157 
assertContainsExactly(List<?> expected, List<?> actual)158     private static void assertContainsExactly(List<?> expected, List<?> actual) {
159         assertEquals(expected.size(), actual.size());
160         for (Object o : expected) {
161             assertTrue(actual.contains(o));
162         }
163     }
164 
buildForMimeTypes(String... mimeTypes)165     private static RootInfo buildForMimeTypes(String... mimeTypes) {
166         final RootInfo root = new RootInfo();
167         root.derivedMimeTypes = mimeTypes;
168         return root;
169     }
170 }
171