blob: 2e81545e8e54dd5e50754d15593dd4fa719b27a1 [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;
Aga Wronska774cc932016-03-30 18:07:59 -070058 mState.showAdvanced = true;
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070059 mState.localOnly = false;
60 }
61
Steve McKayea9ec292016-03-01 15:11:56 -080062 public void testMatchingRoots_Everything() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070063 mState.acceptMimes = new String[] { "*/*" };
64 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -080065 newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
66 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070067 }
68
Steve McKayea9ec292016-03-01 15:11:56 -080069 public void testMatchingRoots_DirectoryCopy() throws Exception {
70 RootInfo downloads = buildForMimeTypes("*/*");
71 downloads.authority = "com.android.providers.downloads.documents";
72 mRoots.add(downloads);
73
74 mState.acceptMimes = new String[] { "*/*" };
75 mState.directoryCopy = true;
76
77 // basically we're asserting that the results don't contain downloads
78 assertContainsExactly(
79 newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
80 getMatchingRoots(mRoots, mState));
81 }
82
83 public void testMatchingRoots_PngOrWild() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070084 mState.acceptMimes = new String[] { "image/png", "*/*" };
85 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -080086 newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
87 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070088 }
89
Steve McKayea9ec292016-03-01 15:11:56 -080090 public void testMatchingRoots_AudioWild() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070091 mState.acceptMimes = new String[] { "audio/*" };
92 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -080093 newArrayList(mNull, mWild, mAudio),
94 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070095 }
96
Steve McKayea9ec292016-03-01 15:11:56 -080097 public void testMatchingRoots_AudioWildOrImageWild() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070098 mState.acceptMimes = new String[] { "audio/*", "image/*" };
99 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -0800100 newArrayList(mNull, mWild, mAudio, mImages),
101 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700102 }
103
Steve McKayea9ec292016-03-01 15:11:56 -0800104 public void testMatchingRoots_AudioSpecific() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700105 mState.acceptMimes = new String[] { "audio/mpeg" };
106 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -0800107 newArrayList(mNull, mWild, mAudio),
108 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700109 }
110
Steve McKayea9ec292016-03-01 15:11:56 -0800111 public void testMatchingRoots_Document() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700112 mState.acceptMimes = new String[] { "application/msword" };
113 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -0800114 newArrayList(mNull, mWild, mDocs),
115 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700116 }
117
Steve McKayea9ec292016-03-01 15:11:56 -0800118 public void testMatchingRoots_Application() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700119 mState.acceptMimes = new String[] { "application/*" };
120 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -0800121 newArrayList(mNull, mWild, mAudio, mDocs),
122 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700123 }
124
Steve McKayea9ec292016-03-01 15:11:56 -0800125 public void testMatchingRoots_FlacOrPng() throws Exception {
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700126 mState.acceptMimes = new String[] { "application/x-flac", "image/png" };
127 assertContainsExactly(
Steve McKayea9ec292016-03-01 15:11:56 -0800128 newArrayList(mNull, mWild, mAudio, mImages),
129 getMatchingRoots(mRoots, mState));
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700130 }
131
Ben Kwa77797402015-05-29 15:40:31 -0700132 public void testExcludedAuthorities() throws Exception {
Steve McKayea9ec292016-03-01 15:11:56 -0800133 final List<RootInfo> roots = newArrayList();
Ben Kwa77797402015-05-29 15:40:31 -0700134
135 // Set up some roots
136 for (int i = 0; i < 5; ++i) {
137 RootInfo root = new RootInfo();
138 root.authority = "authority" + i;
139 roots.add(root);
140 }
141 // Make some allowed authorities
Steve McKayea9ec292016-03-01 15:11:56 -0800142 List<RootInfo> allowedRoots = newArrayList(
Ben Kwa77797402015-05-29 15:40:31 -0700143 roots.get(0), roots.get(2), roots.get(4));
144 // Set up the excluded authority list
145 for (RootInfo root: roots) {
146 if (!allowedRoots.contains(root)) {
147 mState.excludedAuthorities.add(root.authority);
148 }
149 }
150 mState.acceptMimes = new String[] { "*/*" };
151
152 assertContainsExactly(
153 allowedRoots,
Steve McKayea9ec292016-03-01 15:11:56 -0800154 getMatchingRoots(roots, mState));
Ben Kwa77797402015-05-29 15:40:31 -0700155 }
156
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700157 private static void assertContainsExactly(List<?> expected, List<?> actual) {
158 assertEquals(expected.size(), actual.size());
159 for (Object o : expected) {
160 assertTrue(actual.contains(o));
161 }
162 }
Steve McKayea9ec292016-03-01 15:11:56 -0800163
164 private static RootInfo buildForMimeTypes(String... mimeTypes) {
165 final RootInfo root = new RootInfo();
166 root.derivedMimeTypes = mimeTypes;
167 return root;
168 }
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700169}