Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 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 | |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.RootsCache.getMatchingRoots; |
| 20 | import static com.google.common.collect.Lists.newArrayList; |
| 21 | |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 22 | import android.test.AndroidTestCase; |
| 23 | import android.test.suitebuilder.annotation.SmallTest; |
| 24 | |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 25 | import com.android.documentsui.model.RootInfo; |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 26 | |
| 27 | import com.google.common.collect.Lists; |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 28 | |
| 29 | import java.util.List; |
| 30 | |
| 31 | @SmallTest |
| 32 | public class RootsCacheTest extends AndroidTestCase { |
| 33 | |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 34 | 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 Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 44 | |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 45 | private List<RootInfo> mRoots; |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 46 | |
| 47 | private State mState; |
| 48 | |
| 49 | @Override |
| 50 | protected void setUp() throws Exception { |
| 51 | super.setUp(); |
| 52 | |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 53 | mRoots = Lists.newArrayList( |
| 54 | mNull, mWild, mEmpty, mImages, mAudio, mDocs, mMalformed1, mMalformed2); |
| 55 | |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 56 | mState = new State(); |
| 57 | mState.action = State.ACTION_OPEN; |
Aga Wronska | 774cc93 | 2016-03-30 18:07:59 -0700 | [diff] [blame] | 58 | mState.showAdvanced = true; |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 59 | mState.localOnly = false; |
| 60 | } |
| 61 | |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 62 | public void testMatchingRoots_Everything() throws Exception { |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 63 | mState.acceptMimes = new String[] { "*/*" }; |
| 64 | assertContainsExactly( |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 65 | newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2), |
| 66 | getMatchingRoots(mRoots, mState)); |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 69 | 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 Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 84 | mState.acceptMimes = new String[] { "image/png", "*/*" }; |
| 85 | assertContainsExactly( |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 86 | newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2), |
| 87 | getMatchingRoots(mRoots, mState)); |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 88 | } |
| 89 | |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 90 | public void testMatchingRoots_AudioWild() throws Exception { |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 91 | mState.acceptMimes = new String[] { "audio/*" }; |
| 92 | assertContainsExactly( |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 93 | newArrayList(mNull, mWild, mAudio), |
| 94 | getMatchingRoots(mRoots, mState)); |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 97 | public void testMatchingRoots_AudioWildOrImageWild() throws Exception { |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 98 | mState.acceptMimes = new String[] { "audio/*", "image/*" }; |
| 99 | assertContainsExactly( |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 100 | newArrayList(mNull, mWild, mAudio, mImages), |
| 101 | getMatchingRoots(mRoots, mState)); |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 104 | public void testMatchingRoots_AudioSpecific() throws Exception { |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 105 | mState.acceptMimes = new String[] { "audio/mpeg" }; |
| 106 | assertContainsExactly( |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 107 | newArrayList(mNull, mWild, mAudio), |
| 108 | getMatchingRoots(mRoots, mState)); |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 111 | public void testMatchingRoots_Document() throws Exception { |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 112 | mState.acceptMimes = new String[] { "application/msword" }; |
| 113 | assertContainsExactly( |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 114 | newArrayList(mNull, mWild, mDocs), |
| 115 | getMatchingRoots(mRoots, mState)); |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 118 | public void testMatchingRoots_Application() throws Exception { |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 119 | mState.acceptMimes = new String[] { "application/*" }; |
| 120 | assertContainsExactly( |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 121 | newArrayList(mNull, mWild, mAudio, mDocs), |
| 122 | getMatchingRoots(mRoots, mState)); |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 125 | public void testMatchingRoots_FlacOrPng() throws Exception { |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 126 | mState.acceptMimes = new String[] { "application/x-flac", "image/png" }; |
| 127 | assertContainsExactly( |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 128 | newArrayList(mNull, mWild, mAudio, mImages), |
| 129 | getMatchingRoots(mRoots, mState)); |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Ben Kwa | 7779740 | 2015-05-29 15:40:31 -0700 | [diff] [blame] | 132 | public void testExcludedAuthorities() throws Exception { |
Steve McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 133 | final List<RootInfo> roots = newArrayList(); |
Ben Kwa | 7779740 | 2015-05-29 15:40:31 -0700 | [diff] [blame] | 134 | |
| 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 McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 142 | List<RootInfo> allowedRoots = newArrayList( |
Ben Kwa | 7779740 | 2015-05-29 15:40:31 -0700 | [diff] [blame] | 143 | 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 McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 154 | getMatchingRoots(roots, mState)); |
Ben Kwa | 7779740 | 2015-05-29 15:40:31 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 157 | 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 McKay | ea9ec29 | 2016-03-01 15:11:56 -0800 | [diff] [blame] | 163 | |
| 164 | private static RootInfo buildForMimeTypes(String... mimeTypes) { |
| 165 | final RootInfo root = new RootInfo(); |
| 166 | root.derivedMimeTypes = mimeTypes; |
| 167 | return root; |
| 168 | } |
Jeff Sharkey | 6d97d3c | 2013-09-06 10:43:45 -0700 | [diff] [blame] | 169 | } |