blob: e73dd8cdfec256a613598a89829e3d05cad62b68 [file] [log] [blame]
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -07001/*
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
17package com.android.documentsui;
18
Steve McKayea9ec292016-03-01 15:11:56 -080019import static com.android.documentsui.RootsCache.getMatchingRoots;
20import static com.google.common.collect.Lists.newArrayList;
21
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070022import android.test.AndroidTestCase;
23import android.test.suitebuilder.annotation.SmallTest;
24
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070025import com.android.documentsui.model.RootInfo;
Steve McKay58efce32015-08-20 16:19:38 +000026
27import com.google.common.collect.Lists;
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070028
29import java.util.List;
30
31@SmallTest
32public class RootsCacheTest extends AndroidTestCase {
33
Steve McKayea9ec292016-03-01 15:11:56 -080034 private static RootInfo mNull = new RootInfo();
35 private static RootInfo mEmpty = buildForMimeTypes();
36 private static RootInfo mWild = buildForMimeTypes("*/*");
37 private static RootInfo mImages = buildForMimeTypes("image/*");
38 private static RootInfo mAudio = buildForMimeTypes(
39 "audio/*", "application/ogg", "application/x-flac");
40 private static RootInfo mDocs = buildForMimeTypes(
41 "application/msword", "application/vnd.ms-excel");
42 private static RootInfo mMalformed1 = buildForMimeTypes("meow");
43 private static RootInfo mMalformed2 = buildForMimeTypes("*/meow");
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070044
Steve McKayea9ec292016-03-01 15:11:56 -080045 private List<RootInfo> mRoots;
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070046
47 private State mState;
48
49 @Override
50 protected void setUp() throws Exception {
51 super.setUp();
52
Steve McKayea9ec292016-03-01 15:11:56 -080053 mRoots = Lists.newArrayList(
54 mNull, mWild, mEmpty, mImages, mAudio, mDocs, mMalformed1, mMalformed2);
55
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070056 mState = new State();
57 mState.action = State.ACTION_OPEN;
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070058 mState.localOnly = false;
59 }
60
Steve McKayea9ec292016-03-01 15:11:56 -080061 public void testMatchingRoots_Everything() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070062 mState.acceptMimes = new String[] { "*/*" };
63 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -080064 newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
65 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070066 }
67
Steve McKayea9ec292016-03-01 15:11:56 -080068 public void testMatchingRoots_DirectoryCopy() throws Exception {
69 RootInfo downloads = buildForMimeTypes("*/*");
70 downloads.authority = "com.android.providers.downloads.documents";
71 mRoots.add(downloads);
72
73 mState.acceptMimes = new String[] { "*/*" };
74 mState.directoryCopy = true;
75
76 // basically we're asserting that the results don't contain downloads
77 assertContainsExactly(
78 newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
79 getMatchingRoots(mRoots, mState));
80 }
81
82 public void testMatchingRoots_PngOrWild() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070083 mState.acceptMimes = new String[] { "image/png", "*/*" };
84 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -080085 newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
86 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070087 }
88
Steve McKayea9ec292016-03-01 15:11:56 -080089 public void testMatchingRoots_AudioWild() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070090 mState.acceptMimes = new String[] { "audio/*" };
91 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -080092 newArrayList(mNull, mWild, mAudio),
93 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070094 }
95
Steve McKayea9ec292016-03-01 15:11:56 -080096 public void testMatchingRoots_AudioWildOrImageWild() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070097 mState.acceptMimes = new String[] { "audio/*", "image/*" };
98 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -080099 newArrayList(mNull, mWild, mAudio, mImages),
100 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700101 }
102
Steve McKayea9ec292016-03-01 15:11:56 -0800103 public void testMatchingRoots_AudioSpecific() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700104 mState.acceptMimes = new String[] { "audio/mpeg" };
105 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -0800106 newArrayList(mNull, mWild, mAudio),
107 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700108 }
109
Steve McKayea9ec292016-03-01 15:11:56 -0800110 public void testMatchingRoots_Document() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700111 mState.acceptMimes = new String[] { "application/msword" };
112 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -0800113 newArrayList(mNull, mWild, mDocs),
114 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700115 }
116
Steve McKayea9ec292016-03-01 15:11:56 -0800117 public void testMatchingRoots_Application() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700118 mState.acceptMimes = new String[] { "application/*" };
119 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -0800120 newArrayList(mNull, mWild, mAudio, mDocs),
121 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700122 }
123
Steve McKayea9ec292016-03-01 15:11:56 -0800124 public void testMatchingRoots_FlacOrPng() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700125 mState.acceptMimes = new String[] { "application/x-flac", "image/png" };
126 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -0800127 newArrayList(mNull, mWild, mAudio, mImages),
128 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700129 }
130
Ben Kwa77797402015-05-29 15:40:31 -0700131 public void testExcludedAuthorities() throws Exception {
Steve McKayea9ec292016-03-01 15:11:56 -0800132 final List<RootInfo> roots = newArrayList();
Ben Kwa77797402015-05-29 15:40:31 -0700133
134 // Set up some roots
135 for (int i = 0; i < 5; ++i) {
136 RootInfo root = new RootInfo();
137 root.authority = "authority" + i;
138 roots.add(root);
139 }
140 // Make some allowed authorities
Steve McKayea9ec292016-03-01 15:11:56 -0800141 List<RootInfo> allowedRoots = newArrayList(
Ben Kwa77797402015-05-29 15:40:31 -0700142 roots.get(0), roots.get(2), roots.get(4));
143 // Set up the excluded authority list
144 for (RootInfo root: roots) {
145 if (!allowedRoots.contains(root)) {
146 mState.excludedAuthorities.add(root.authority);
147 }
148 }
149 mState.acceptMimes = new String[] { "*/*" };
150
151 assertContainsExactly(
152 allowedRoots,
Steve McKayea9ec292016-03-01 15:11:56 -0800153 getMatchingRoots(roots, mState));
Ben Kwa77797402015-05-29 15:40:31 -0700154 }
155
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700156 private static void assertContainsExactly(List<?> expected, List<?> actual) {
157 assertEquals(expected.size(), actual.size());
158 for (Object o : expected) {
159 assertTrue(actual.contains(o));
160 }
161 }
Steve McKayea9ec292016-03-01 15:11:56 -0800162
163 private static RootInfo buildForMimeTypes(String... mimeTypes) {
164 final RootInfo root = new RootInfo();
165 root.derivedMimeTypes = mimeTypes;
166 return root;
167 }
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700168}