blob: 7d3498e4e07952776519de803315ce461ecc9b4d [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
19import android.test.AndroidTestCase;
20import android.test.suitebuilder.annotation.SmallTest;
21
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070022import com.android.documentsui.model.RootInfo;
Steve McKay58efce32015-08-20 16:19:38 +000023
24import com.google.common.collect.Lists;
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070025
26import java.util.List;
27
28@SmallTest
29public class RootsCacheTest extends AndroidTestCase {
30
31 private static RootInfo buildForMimeTypes(String... mimeTypes) {
32 final RootInfo root = new RootInfo();
Jeff Sharkey3f4c2052013-09-09 16:51:06 -070033 root.derivedMimeTypes = mimeTypes;
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -070034 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
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
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
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
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
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
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
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
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
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
Ben Kwa77797402015-05-29 15:40:31 -0700117 public void testExcludedAuthorities() throws Exception {
118 final List<RootInfo> roots = Lists.newArrayList();
119
120 // Set up some roots
121 for (int i = 0; i < 5; ++i) {
122 RootInfo root = new RootInfo();
123 root.authority = "authority" + i;
124 roots.add(root);
125 }
126 // Make some allowed authorities
127 List<RootInfo> allowedRoots = Lists.newArrayList(
128 roots.get(0), roots.get(2), roots.get(4));
129 // Set up the excluded authority list
130 for (RootInfo root: roots) {
131 if (!allowedRoots.contains(root)) {
132 mState.excludedAuthorities.add(root.authority);
133 }
134 }
135 mState.acceptMimes = new String[] { "*/*" };
136
137 assertContainsExactly(
138 allowedRoots,
139 RootsCache.getMatchingRoots(roots, mState));
140 }
141
Jeff Sharkey6d97d3c2013-09-06 10:43:45 -0700142 private static void assertContainsExactly(List<?> expected, List<?> actual) {
143 assertEquals(expected.size(), actual.size());
144 for (Object o : expected) {
145 assertTrue(actual.contains(o));
146 }
147 }
148}