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