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 com.android.documentsui.clipping.ClipStorage.NUM_OF_SLOTS; 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertFalse; 22 import static org.junit.Assert.assertTrue; 23 24 import android.content.SharedPreferences; 25 import android.net.Uri; 26 import android.os.AsyncTask; 27 import android.support.test.InstrumentationRegistry; 28 import android.support.test.filters.SmallTest; 29 import android.support.test.runner.AndroidJUnit4; 30 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.io.File; 41 import java.util.ArrayList; 42 import java.util.Iterator; 43 import java.util.List; 44 45 @RunWith(AndroidJUnit4.class) 46 @SmallTest 47 public class ClipStorageTest { 48 private static final String PREF_NAME = "pref"; 49 private static final List<Uri> TEST_URIS = createList( 50 "content://ham/fancy", 51 "content://poodle/monkey/giraffe"); 52 53 @Rule 54 public TemporaryFolder folder = new TemporaryFolder(); 55 56 private SharedPreferences mPref; 57 private TestScheduledExecutorService mExecutor; 58 private ClipStorage mStorage; 59 60 private int mSlot; 61 62 @Before setUp()63 public void setUp() { 64 mPref = InstrumentationRegistry.getContext().getSharedPreferences(PREF_NAME, 0); 65 File clipDir = ClipStorage.prepareStorage(folder.getRoot()); 66 mStorage = new ClipStorage(clipDir, mPref); 67 68 mExecutor = new TestScheduledExecutorService(); 69 AsyncTask.setDefaultExecutor(mExecutor); 70 71 mSlot = mStorage.claimStorageSlot(); 72 } 73 74 @AfterClass tearDownOnce()75 public static void tearDownOnce() { 76 AsyncTask.setDefaultExecutor(AsyncTask.SERIAL_EXECUTOR); 77 } 78 79 @Test testWrite()80 public void testWrite() throws Exception { 81 writeAll(mSlot, TEST_URIS); 82 } 83 84 @Test testRead()85 public void testRead() throws Exception { 86 writeAll(mSlot, TEST_URIS); 87 List<Uri> uris = new ArrayList<>(); 88 89 File copy = mStorage.getFile(mSlot); 90 try(ClipStorageReader provider = mStorage.createReader(copy)) { 91 for (Uri uri : provider) { 92 uris.add(uri); 93 } 94 } 95 assertEquals(TEST_URIS, uris); 96 } 97 98 @Test testClaimStorageSlot_NoAvailableSlot()99 public void testClaimStorageSlot_NoAvailableSlot() throws Exception { 100 int firstSlot = mStorage.claimStorageSlot(); 101 writeAll(firstSlot, TEST_URIS); 102 mStorage.getFile(firstSlot); 103 for (int i = 0; i < NUM_OF_SLOTS - 1; ++i) { 104 int slot = mStorage.claimStorageSlot(); 105 writeAll(slot, TEST_URIS); 106 mStorage.getFile(slot); 107 } 108 109 assertEquals(firstSlot, mStorage.claimStorageSlot()); 110 } 111 112 @Test testReadConcurrently()113 public void testReadConcurrently() throws Exception { 114 writeAll(mSlot, TEST_URIS); 115 List<Uri> uris = new ArrayList<>(); 116 List<Uri> uris2 = new ArrayList<>(); 117 118 File copy = mStorage.getFile(mSlot); 119 File copy2 = mStorage.getFile(mSlot); 120 try(ClipStorageReader reader = mStorage.createReader(copy)) { 121 try(ClipStorageReader reader2 = mStorage.createReader(copy2)){ 122 Iterator<Uri> iter = reader.iterator(); 123 Iterator<Uri> iter2 = reader2.iterator(); 124 125 while (iter.hasNext() && iter2.hasNext()) { 126 uris.add(iter.next()); 127 uris2.add(iter2.next()); 128 } 129 130 assertFalse(iter.hasNext()); 131 assertFalse(iter2.hasNext()); 132 } 133 } 134 assertEquals(TEST_URIS, uris); 135 assertEquals(TEST_URIS, uris2); 136 } 137 138 @Test testPrepareStorage_CreatesDir()139 public void testPrepareStorage_CreatesDir() throws Exception { 140 File clipDir = ClipStorage.prepareStorage(folder.getRoot()); 141 assertTrue(clipDir.exists()); 142 assertTrue(clipDir.isDirectory()); 143 assertFalse(clipDir.equals(folder.getRoot())); 144 } 145 writeAll(int slot, List<Uri> uris)146 private void writeAll(int slot, List<Uri> uris) { 147 mStorage.persistUris(uris, slot); 148 mExecutor.runAll(); 149 } 150 createList(String... values)151 private static List<Uri> createList(String... values) { 152 List<Uri> uris = new ArrayList<>(values.length); 153 for (int i = 0; i < values.length; i++) { 154 uris.add(i, Uri.parse(values[i])); 155 } 156 return uris; 157 } 158 } 159