ProvidersCacheTest.java revision d080506e3aa8547605cd4783eb660775d7d2b8ee
106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey/*
206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey * Copyright (C) 2013 The Android Open Source Project
306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey *
406c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
506c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey * you may not use this file except in compliance with the License.
606c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey * You may obtain a copy of the License at
706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey *
806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey *
1006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey * Unless required by applicable law or agreed to in writing, software
1106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
1206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey * See the License for the specific language governing permissions and
1406c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey * limitations under the License.
1506c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey */
1606c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
1706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkeypackage com.android.documentsui;
1806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
19af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKayimport static com.android.documentsui.RootsCache.getMatchingRoots;
20af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKayimport static com.google.common.collect.Lists.newArrayList;
21af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay
2206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkeyimport android.test.AndroidTestCase;
2306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkeyimport android.test.suitebuilder.annotation.SmallTest;
2406c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
25d080506e3aa8547605cd4783eb660775d7d2b8eeSteve McKayimport com.android.documentsui.base.RootInfo;
26fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay
27fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKayimport com.google.common.collect.Lists;
2806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
2906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkeyimport java.util.List;
3006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
3106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey@SmallTest
3206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkeypublic class RootsCacheTest extends AndroidTestCase {
3306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
34af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mNull = new RootInfo();
35af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mEmpty = buildForMimeTypes();
36af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mWild = buildForMimeTypes("*/*");
37af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mImages = buildForMimeTypes("image/*");
38af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mAudio = buildForMimeTypes(
39af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay            "audio/*", "application/ogg", "application/x-flac");
40af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mDocs = buildForMimeTypes(
41af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay            "application/msword", "application/vnd.ms-excel");
42af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mMalformed1 = buildForMimeTypes("meow");
43af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mMalformed2 = buildForMimeTypes("*/meow");
4406c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
45af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private List<RootInfo> mRoots;
4606c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
4706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    private State mState;
4806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
4906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    @Override
5006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    protected void setUp() throws Exception {
5106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        super.setUp();
5206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
53af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        mRoots = Lists.newArrayList(
54af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                mNull, mWild, mEmpty, mImages, mAudio, mDocs, mMalformed1, mMalformed2);
55af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay
5606c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState = new State();
5706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.action = State.ACTION_OPEN;
58ceb990eae3c5eed7c662d544f3d7cfc29f137c9bAga Wronska        mState.showAdvanced = true;
5906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.localOnly = false;
6006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
6106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
62af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_Everything() throws Exception {
63af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        mState.acceptMimes = new String[] { "*/*" };
64af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        assertContainsExactly(
65af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
66af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                getMatchingRoots(mRoots, mState));
67af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    }
68af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay
69af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_DirectoryCopy() throws Exception {
70af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        RootInfo downloads = buildForMimeTypes("*/*");
71af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        downloads.authority = "com.android.providers.downloads.documents";
72af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        mRoots.add(downloads);
73af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay
7406c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "*/*" };
75af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        mState.directoryCopy = true;
76af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay
77af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        // basically we're asserting that the results don't contain downloads
7806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
79af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
80af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                getMatchingRoots(mRoots, mState));
8106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
8206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
83af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_PngOrWild() throws Exception {
8406c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "image/png", "*/*" };
8506c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
86af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
87af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                getMatchingRoots(mRoots, mState));
8806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
8906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
90af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_AudioWild() throws Exception {
9106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "audio/*" };
9206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
93af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mAudio),
94af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                getMatchingRoots(mRoots, mState));
9506c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
9606c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
97af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_AudioWildOrImageWild() throws Exception {
9806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "audio/*", "image/*" };
9906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
100af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mAudio, mImages),
101af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                getMatchingRoots(mRoots, mState));
10206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
10306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
104af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_AudioSpecific() throws Exception {
10506c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "audio/mpeg" };
10606c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
107af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mAudio),
108af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                getMatchingRoots(mRoots, mState));
10906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
11006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
111af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_Document() throws Exception {
11206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "application/msword" };
11306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
114af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mDocs),
115af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                getMatchingRoots(mRoots, mState));
11606c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
11706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
118af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_Application() throws Exception {
11906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "application/*" };
12006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
121af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mAudio, mDocs),
122af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                getMatchingRoots(mRoots, mState));
12306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
12406c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
125af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_FlacOrPng() throws Exception {
12606c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "application/x-flac", "image/png" };
12706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
128af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mAudio, mImages),
129af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                getMatchingRoots(mRoots, mState));
13006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
13106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
1320bcdec3400df914b0359d82456009a9e70eb029dBen Kwa    public void testExcludedAuthorities() throws Exception {
133af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        final List<RootInfo> roots = newArrayList();
1340bcdec3400df914b0359d82456009a9e70eb029dBen Kwa
1350bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        // Set up some roots
1360bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        for (int i = 0; i < 5; ++i) {
1370bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            RootInfo root = new RootInfo();
1380bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            root.authority = "authority" + i;
1390bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            roots.add(root);
1400bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        }
1410bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        // Make some allowed authorities
142af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        List<RootInfo> allowedRoots = newArrayList(
1430bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            roots.get(0), roots.get(2), roots.get(4));
1440bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        // Set up the excluded authority list
1450bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        for (RootInfo root: roots) {
1460bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            if (!allowedRoots.contains(root)) {
1470bcdec3400df914b0359d82456009a9e70eb029dBen Kwa                mState.excludedAuthorities.add(root.authority);
1480bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            }
1490bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        }
1500bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        mState.acceptMimes = new String[] { "*/*" };
1510bcdec3400df914b0359d82456009a9e70eb029dBen Kwa
1520bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        assertContainsExactly(
1530bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            allowedRoots,
154af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay            getMatchingRoots(roots, mState));
1550bcdec3400df914b0359d82456009a9e70eb029dBen Kwa    }
1560bcdec3400df914b0359d82456009a9e70eb029dBen Kwa
15706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    private static void assertContainsExactly(List<?> expected, List<?> actual) {
15806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertEquals(expected.size(), actual.size());
15906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        for (Object o : expected) {
16006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey            assertTrue(actual.contains(o));
16106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        }
16206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
163af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay
164af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo buildForMimeTypes(String... mimeTypes) {
165af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        final RootInfo root = new RootInfo();
166af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        root.derivedMimeTypes = mimeTypes;
167af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        return root;
168af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    }
16906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey}
170