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
17d9caa6ab53aa784acaf241c0ded3c4ae2d342bf8Steve McKaypackage com.android.documentsui.roots;
1806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
19af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKayimport static com.google.common.collect.Lists.newArrayList;
20af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay
2106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkeyimport android.test.AndroidTestCase;
2206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkeyimport android.test.suitebuilder.annotation.SmallTest;
2306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
248659cbccc6fbab79fb6855abe9cb31ee8171b37eSteve McKayimport com.android.documentsui.base.Providers;
25d080506e3aa8547605cd4783eb660775d7d2b8eeSteve McKayimport com.android.documentsui.base.RootInfo;
26d9caa6ab53aa784acaf241c0ded3c4ae2d342bf8Steve McKayimport com.android.documentsui.base.State;
27fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKay
28fefcd700d6b4cf1c4402af74c50fb0e762472901Steve McKayimport com.google.common.collect.Lists;
2906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
3006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkeyimport java.util.List;
3106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
3206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey@SmallTest
339bd4099ee27f8302a06fc66980079f29127318f8Jon Mannpublic class ProvidersCacheTest extends AndroidTestCase {
3406c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
35af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mNull = new RootInfo();
36af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mEmpty = buildForMimeTypes();
37af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mWild = buildForMimeTypes("*/*");
38af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mImages = buildForMimeTypes("image/*");
39af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mAudio = buildForMimeTypes(
40af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay            "audio/*", "application/ogg", "application/x-flac");
41af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mDocs = buildForMimeTypes(
42af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay            "application/msword", "application/vnd.ms-excel");
43af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mMalformed1 = buildForMimeTypes("meow");
44af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo mMalformed2 = buildForMimeTypes("*/meow");
4506c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
46af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private List<RootInfo> mRoots;
4706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
4806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    private State mState;
4906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
5006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    @Override
5106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    protected void setUp() throws Exception {
5206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        super.setUp();
5306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
54af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        mRoots = Lists.newArrayList(
55af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                mNull, mWild, mEmpty, mImages, mAudio, mDocs, mMalformed1, mMalformed2);
56af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay
5706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState = new State();
5806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.action = State.ACTION_OPEN;
59ceb990eae3c5eed7c662d544f3d7cfc29f137c9bAga Wronska        mState.showAdvanced = true;
6006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.localOnly = false;
6106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
6206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
63af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_Everything() throws Exception {
64af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        mState.acceptMimes = new String[] { "*/*" };
65af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        assertContainsExactly(
66af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
679bd4099ee27f8302a06fc66980079f29127318f8Jon Mann                ProvidersAccess.getMatchingRoots(mRoots, mState));
68af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    }
69af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay
70af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_DirectoryCopy() throws Exception {
71af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        RootInfo downloads = buildForMimeTypes("*/*");
728659cbccc6fbab79fb6855abe9cb31ee8171b37eSteve McKay        downloads.authority = Providers.AUTHORITY_DOWNLOADS;
73af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        mRoots.add(downloads);
74af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay
7506c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "*/*" };
76af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        mState.directoryCopy = true;
77af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay
78af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        // basically we're asserting that the results don't contain downloads
7906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
80af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
819bd4099ee27f8302a06fc66980079f29127318f8Jon Mann                ProvidersAccess.getMatchingRoots(mRoots, mState));
8206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
8306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
84af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_PngOrWild() throws Exception {
8506c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "image/png", "*/*" };
8606c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
87af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mImages, mAudio, mDocs, mMalformed1, mMalformed2),
889bd4099ee27f8302a06fc66980079f29127318f8Jon Mann                ProvidersAccess.getMatchingRoots(mRoots, mState));
8906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
9006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
91af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_AudioWild() throws Exception {
9206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "audio/*" };
9306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
94af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mAudio),
959bd4099ee27f8302a06fc66980079f29127318f8Jon Mann                ProvidersAccess.getMatchingRoots(mRoots, mState));
9606c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
9706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
98af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_AudioWildOrImageWild() throws Exception {
9906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "audio/*", "image/*" };
10006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
101af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mAudio, mImages),
1029bd4099ee27f8302a06fc66980079f29127318f8Jon Mann                ProvidersAccess.getMatchingRoots(mRoots, mState));
10306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
10406c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
105af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_AudioSpecific() throws Exception {
10606c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "audio/mpeg" };
10706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
108af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mAudio),
1099bd4099ee27f8302a06fc66980079f29127318f8Jon Mann                ProvidersAccess.getMatchingRoots(mRoots, mState));
11006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
11106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
112af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_Document() throws Exception {
11306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "application/msword" };
11406c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
115af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mDocs),
1169bd4099ee27f8302a06fc66980079f29127318f8Jon Mann                ProvidersAccess.getMatchingRoots(mRoots, mState));
11706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
11806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
119af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_Application() throws Exception {
12006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "application/*" };
12106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
122af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mAudio, mDocs),
1239bd4099ee27f8302a06fc66980079f29127318f8Jon Mann                ProvidersAccess.getMatchingRoots(mRoots, mState));
12406c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
12506c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
126af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    public void testMatchingRoots_FlacOrPng() throws Exception {
12706c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        mState.acceptMimes = new String[] { "application/x-flac", "image/png" };
12806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertContainsExactly(
129af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay                newArrayList(mNull, mWild, mAudio, mImages),
1309bd4099ee27f8302a06fc66980079f29127318f8Jon Mann                ProvidersAccess.getMatchingRoots(mRoots, mState));
13106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
13206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey
1330bcdec3400df914b0359d82456009a9e70eb029dBen Kwa    public void testExcludedAuthorities() throws Exception {
134af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        final List<RootInfo> roots = newArrayList();
1350bcdec3400df914b0359d82456009a9e70eb029dBen Kwa
1360bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        // Set up some roots
1370bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        for (int i = 0; i < 5; ++i) {
1380bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            RootInfo root = new RootInfo();
1390bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            root.authority = "authority" + i;
1400bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            roots.add(root);
1410bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        }
1420bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        // Make some allowed authorities
143af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        List<RootInfo> allowedRoots = newArrayList(
1440bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            roots.get(0), roots.get(2), roots.get(4));
1450bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        // Set up the excluded authority list
1460bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        for (RootInfo root: roots) {
1470bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            if (!allowedRoots.contains(root)) {
1480bcdec3400df914b0359d82456009a9e70eb029dBen Kwa                mState.excludedAuthorities.add(root.authority);
1490bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            }
1500bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        }
1510bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        mState.acceptMimes = new String[] { "*/*" };
1520bcdec3400df914b0359d82456009a9e70eb029dBen Kwa
1530bcdec3400df914b0359d82456009a9e70eb029dBen Kwa        assertContainsExactly(
1540bcdec3400df914b0359d82456009a9e70eb029dBen Kwa            allowedRoots,
1559bd4099ee27f8302a06fc66980079f29127318f8Jon Mann            ProvidersAccess.getMatchingRoots(roots, mState));
1560bcdec3400df914b0359d82456009a9e70eb029dBen Kwa    }
1570bcdec3400df914b0359d82456009a9e70eb029dBen Kwa
15806c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    private static void assertContainsExactly(List<?> expected, List<?> actual) {
15906c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        assertEquals(expected.size(), actual.size());
16006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        for (Object o : expected) {
16106c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey            assertTrue(actual.contains(o));
16206c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey        }
16306c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey    }
164af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay
165af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    private static RootInfo buildForMimeTypes(String... mimeTypes) {
166af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        final RootInfo root = new RootInfo();
167af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        root.derivedMimeTypes = mimeTypes;
168af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay        return root;
169af2033a730edc52c6ca342a3479d9951dfd67f79Steve McKay    }
17006c4187080497dffbcdb438aa45b262eb37706b1Jeff Sharkey}
171