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