1 /*
2  * Copyright (C) 2016 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.clipping;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 
22 import android.content.SharedPreferences;
23 import android.net.Uri;
24 import android.os.AsyncTask;
25 import android.provider.DocumentsContract;
26 
27 import androidx.test.filters.MediumTest;
28 import androidx.test.platform.app.InstrumentationRegistry;
29 import androidx.test.runner.AndroidJUnit4;
30 
31 import com.android.documentsui.base.Shared;
32 import com.android.documentsui.testing.TestScheduledExecutorService;
33 
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.TemporaryFolder;
39 import org.junit.runner.RunWith;
40 
41 import java.util.ArrayList;
42 import java.util.Iterator;
43 import java.util.List;
44 
45 @RunWith(AndroidJUnit4.class)
46 @MediumTest
47 public class UrisSupplierTest {
48 
49     private static final String PREF_NAME = "pref";
50     private static final String AUTHORITY = "foo";
51     private static final List<Uri> SHORT_URI_LIST = createList(3);
52     private static final List<Uri> LONG_URI_LIST = createList(Shared.MAX_DOCS_IN_INTENT + 5);
53 
54     @Rule
55     public TemporaryFolder folder = new TemporaryFolder();
56 
57     private SharedPreferences mPref;
58     private TestScheduledExecutorService mExecutor;
59     private ClipStorage mStorage;
60 
61     @Before
setUp()62     public void setUp() {
63         mExecutor = new TestScheduledExecutorService();
64         AsyncTask.setDefaultExecutor(mExecutor);
65 
66         mPref = InstrumentationRegistry.getInstrumentation().getTargetContext()
67                 .getSharedPreferences(PREF_NAME, 0);
68         mStorage = new ClipStorage(folder.getRoot(), mPref);
69     }
70 
71     @AfterClass
tearDownOnce()72     public static void tearDownOnce() {
73         AsyncTask.setDefaultExecutor(AsyncTask.SERIAL_EXECUTOR);
74     }
75 
76     @Test
testItemCountEquals_shortList()77     public void testItemCountEquals_shortList() throws Exception {
78         UrisSupplier uris = createWithShortList();
79 
80         assertEquals(SHORT_URI_LIST.size(), uris.getItemCount());
81     }
82 
83     @Test
testItemCountEquals_longList()84     public void testItemCountEquals_longList() throws Exception {
85         UrisSupplier uris = createWithLongList();
86 
87         assertEquals(LONG_URI_LIST.size(), uris.getItemCount());
88     }
89 
90     @Test
testGetDocsEquals_shortList()91     public void testGetDocsEquals_shortList() throws Exception {
92         UrisSupplier uris = createWithShortList();
93 
94         assertIterableEquals(SHORT_URI_LIST, uris.getUris(mStorage));
95     }
96 
97     @Test
testGetDocsEquals_longList()98     public void testGetDocsEquals_longList() throws Exception {
99         UrisSupplier uris = createWithLongList();
100 
101         assertIterableEquals(LONG_URI_LIST, uris.getUris(mStorage));
102     }
103 
104     @Test
testDispose_shortList()105     public void testDispose_shortList() throws Exception {
106         UrisSupplier uris = createWithShortList();
107 
108         uris.dispose();
109     }
110 
111     @Test
testDispose_longList()112     public void testDispose_longList() throws Exception {
113         UrisSupplier uris = createWithLongList();
114 
115         uris.dispose();
116     }
117 
createWithShortList()118     private UrisSupplier createWithShortList() throws Exception {
119         return UrisSupplier.create(SHORT_URI_LIST, mStorage);
120     }
121 
createWithLongList()122     private UrisSupplier createWithLongList() throws Exception {
123         UrisSupplier uris =
124                 UrisSupplier.create(LONG_URI_LIST, mStorage);
125 
126         mExecutor.runAll();
127 
128         return uris;
129     }
130 
assertIterableEquals(Iterable<Uri> expected, Iterable<Uri> value)131     private void assertIterableEquals(Iterable<Uri> expected, Iterable<Uri> value) {
132         Iterator<Uri> expectedIter = expected.iterator();
133         Iterator<Uri> valueIter = value.iterator();
134 
135         while (expectedIter.hasNext() && valueIter.hasNext()) {
136             assertEquals(expectedIter.next(), valueIter.next());
137         }
138 
139         assertFalse(expectedIter.hasNext());
140         assertFalse(expectedIter.hasNext());
141     }
142 
createList(int count)143     private static List<Uri> createList(int count) {
144         List<Uri> uris = new ArrayList<>(count);
145 
146         for (int i = 0; i < count; ++i) {
147             uris.add(DocumentsContract.buildDocumentUri(AUTHORITY, Integer.toString(i)));
148         }
149 
150         return uris;
151     }
152 }
153