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;
18 
19 import android.test.AndroidTestCase;
20 import android.test.suitebuilder.annotation.SmallTest;
21 
22 import com.android.documentsui.DocumentsActivity.State;
23 import com.android.documentsui.model.RootInfo;
24 import com.google.android.collect.Lists;
25 
26 import java.util.List;
27 
28 @SmallTest
29 public class RootsCacheTest extends AndroidTestCase {
30 
buildForMimeTypes(String... mimeTypes)31     private static RootInfo buildForMimeTypes(String... mimeTypes) {
32         final RootInfo root = new RootInfo();
33         root.derivedMimeTypes = mimeTypes;
34         return root;
35     }
36 
37     private RootInfo mNull = new RootInfo();
38     private RootInfo mEmpty = buildForMimeTypes();
39     private RootInfo mWild = buildForMimeTypes("*/*");
40     private RootInfo mImages = buildForMimeTypes("image/*");
41     private RootInfo mAudio = buildForMimeTypes("audio/*", "application/ogg", "application/x-flac");
42     private RootInfo mDocs = buildForMimeTypes("application/msword", "application/vnd.ms-excel");
43     private RootInfo mMalformed1 = buildForMimeTypes("meow");
44     private RootInfo mMalformed2 = buildForMimeTypes("*/meow");
45 
46     private List<RootInfo> mRoots = Lists.newArrayList(
47             mNull, mWild, mEmpty, mImages, mAudio, mDocs, mMalformed1, mMalformed2);
48 
49     private State mState;
50 
51     @Override
setUp()52     protected void setUp() throws Exception {
53         super.setUp();
54 
55         mState = new State();
56         mState.action = State.ACTION_OPEN;
57         mState.showAdvanced = true;
58         mState.localOnly = false;
59     }
60 
testMatchingRootsEverything()61     public void testMatchingRootsEverything() throws Exception {
62         mState.acceptMimes = new String[] { "*/*" };
63         assertContainsExactly(
64                 Lists.newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
65                 RootsCache.getMatchingRoots(mRoots, mState));
66     }
67 
testMatchingRootsPngOrWild()68     public void testMatchingRootsPngOrWild() throws Exception {
69         mState.acceptMimes = new String[] { "image/png", "*/*" };
70         assertContainsExactly(
71                 Lists.newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
72                 RootsCache.getMatchingRoots(mRoots, mState));
73     }
74 
testMatchingRootsAudioWild()75     public void testMatchingRootsAudioWild() throws Exception {
76         mState.acceptMimes = new String[] { "audio/*" };
77         assertContainsExactly(
78                 Lists.newArrayList(mNull, mWild, mAudio),
79                 RootsCache.getMatchingRoots(mRoots, mState));
80     }
81 
testMatchingRootsAudioWildOrImageWild()82     public void testMatchingRootsAudioWildOrImageWild() throws Exception {
83         mState.acceptMimes = new String[] { "audio/*", "image/*" };
84         assertContainsExactly(
85                 Lists.newArrayList(mNull, mWild, mAudio, mImages),
86                 RootsCache.getMatchingRoots(mRoots, mState));
87     }
88 
testMatchingRootsAudioSpecific()89     public void testMatchingRootsAudioSpecific() throws Exception {
90         mState.acceptMimes = new String[] { "audio/mpeg" };
91         assertContainsExactly(
92                 Lists.newArrayList(mNull, mWild, mAudio),
93                 RootsCache.getMatchingRoots(mRoots, mState));
94     }
95 
testMatchingRootsDocument()96     public void testMatchingRootsDocument() throws Exception {
97         mState.acceptMimes = new String[] { "application/msword" };
98         assertContainsExactly(
99                 Lists.newArrayList(mNull, mWild, mDocs),
100                 RootsCache.getMatchingRoots(mRoots, mState));
101     }
102 
testMatchingRootsApplication()103     public void testMatchingRootsApplication() throws Exception {
104         mState.acceptMimes = new String[] { "application/*" };
105         assertContainsExactly(
106                 Lists.newArrayList(mNull, mWild, mAudio, mDocs),
107                 RootsCache.getMatchingRoots(mRoots, mState));
108     }
109 
testMatchingRootsFlacOrPng()110     public void testMatchingRootsFlacOrPng() throws Exception {
111         mState.acceptMimes = new String[] { "application/x-flac", "image/png" };
112         assertContainsExactly(
113                 Lists.newArrayList(mNull, mWild, mAudio, mImages),
114                 RootsCache.getMatchingRoots(mRoots, mState));
115     }
116 
assertContainsExactly(List<?> expected, List<?> actual)117     private static void assertContainsExactly(List<?> expected, List<?> actual) {
118         assertEquals(expected.size(), actual.size());
119         for (Object o : expected) {
120             assertTrue(actual.contains(o));
121         }
122     }
123 }
124