1 /* 2 * Copyright (C) 2011 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.mediaframeworktest.unit; 18 19 import android.content.ContentValues; 20 import android.content.IContentProvider; 21 import android.media.MediaInserter; 22 import android.net.Uri; 23 import android.provider.MediaStore.Audio; 24 import android.provider.MediaStore.Files; 25 import android.provider.MediaStore.Images; 26 import android.provider.MediaStore.Video; 27 import android.test.InstrumentationTestCase; 28 import android.test.suitebuilder.annotation.SmallTest; 29 30 import dalvik.annotation.TestTargetClass; 31 32 import org.easymock.EasyMock; 33 import org.easymock.IArgumentMatcher; 34 35 @TestTargetClass(MediaInserter.class) 36 public class MediaInserterTest extends InstrumentationTestCase { 37 38 private MediaInserter mMediaInserter; 39 private static final int TEST_BUFFER_SIZE = 10; 40 private IContentProvider mMockProvider; 41 private String mPackageName; 42 43 private int mFilesCounter; 44 private int mAudioCounter; 45 private int mVideoCounter; 46 private int mImagesCounter; 47 48 private static final String sVolumeName = "external"; 49 private static final Uri sAudioUri = Audio.Media.getContentUri(sVolumeName); 50 private static final Uri sVideoUri = Video.Media.getContentUri(sVolumeName); 51 private static final Uri sImagesUri = Images.Media.getContentUri(sVolumeName); 52 private static final Uri sFilesUri = Files.getContentUri(sVolumeName); 53 54 private static class MediaUriMatcher implements IArgumentMatcher { 55 private Uri mUri; 56 57 private MediaUriMatcher(Uri uri) { 58 mUri = uri; 59 } 60 61 @Override 62 public boolean matches(Object argument) { 63 if (!(argument instanceof Uri)) { 64 return false; 65 } 66 67 Uri actualUri = (Uri) argument; 68 if (actualUri == mUri) return true; 69 return false; 70 } 71 72 @Override 73 public void appendTo(StringBuffer buffer) { 74 buffer.append("expected a TableUri '").append(mUri).append("'"); 75 } 76 77 private static Uri expectMediaUri(Uri in) { 78 EasyMock.reportMatcher(new MediaUriMatcher(in)); 79 return null; 80 } 81 } 82 83 @Override 84 protected void setUp() throws Exception { 85 super.setUp(); 86 mMockProvider = EasyMock.createMock(IContentProvider.class); 87 mMediaInserter = new MediaInserter(mMockProvider, 88 mPackageName, TEST_BUFFER_SIZE); 89 mPackageName = getInstrumentation().getContext().getPackageName(); 90 mFilesCounter = 0; 91 mAudioCounter = 0; 92 mVideoCounter = 0; 93 mImagesCounter = 0; 94 } 95 96 private ContentValues createFileContent() { 97 ContentValues values = new ContentValues(); 98 values.put("_data", "/mnt/sdcard/file" + ++mFilesCounter); 99 return values; 100 } 101 102 private ContentValues createAudioContent() { 103 ContentValues values = new ContentValues(); 104 values.put("_data", "/mnt/sdcard/audio" + ++mAudioCounter); 105 return values; 106 } 107 108 private ContentValues createVideoContent() { 109 ContentValues values = new ContentValues(); 110 values.put("_data", "/mnt/sdcard/video" + ++mVideoCounter); 111 return values; 112 } 113 114 private ContentValues createImageContent() { 115 ContentValues values = new ContentValues(); 116 values.put("_data", "/mnt/sdcard/image" + ++mImagesCounter); 117 return values; 118 } 119 120 private ContentValues createContent(Uri uri) { 121 if (uri == sFilesUri) return createFileContent(); 122 else if (uri == sAudioUri) return createAudioContent(); 123 else if (uri == sVideoUri) return createVideoContent(); 124 else if (uri == sImagesUri) return createImageContent(); 125 else throw new IllegalArgumentException("Unknown URL: " + uri.toString()); 126 } 127 128 private void fillBuffer(Uri uri, int numberOfFiles) throws Exception { 129 ContentValues values; 130 for (int i = 0; i < numberOfFiles; ++i) { 131 values = createContent(uri); 132 mMediaInserter.insert(uri, values); 133 } 134 } 135 136 @SmallTest 137 public void testInsertContentsLessThanBufferSize() throws Exception { 138 EasyMock.replay(mMockProvider); 139 140 fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4); 141 fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3); 142 fillBuffer(sVideoUri, TEST_BUFFER_SIZE - 2); 143 fillBuffer(sImagesUri, TEST_BUFFER_SIZE - 1); 144 145 EasyMock.verify(mMockProvider); 146 } 147 148 @SmallTest 149 public void testInsertContentsEqualToBufferSize() throws Exception { 150 EasyMock.expect(mMockProvider.bulkInsert(mPackageName, 151 (Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1); 152 EasyMock.expectLastCall().times(4); 153 EasyMock.replay(mMockProvider); 154 155 fillBuffer(sFilesUri, TEST_BUFFER_SIZE); 156 fillBuffer(sAudioUri, TEST_BUFFER_SIZE); 157 fillBuffer(sVideoUri, TEST_BUFFER_SIZE); 158 fillBuffer(sImagesUri, TEST_BUFFER_SIZE); 159 160 EasyMock.verify(mMockProvider); 161 } 162 163 @SmallTest 164 public void testInsertContentsMoreThanBufferSize() throws Exception { 165 EasyMock.expect(mMockProvider.bulkInsert(mPackageName, 166 (Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1); 167 EasyMock.expectLastCall().times(4); 168 EasyMock.replay(mMockProvider); 169 170 fillBuffer(sFilesUri, TEST_BUFFER_SIZE + 1); 171 fillBuffer(sAudioUri, TEST_BUFFER_SIZE + 2); 172 fillBuffer(sVideoUri, TEST_BUFFER_SIZE + 3); 173 fillBuffer(sImagesUri, TEST_BUFFER_SIZE + 4); 174 175 EasyMock.verify(mMockProvider); 176 } 177 178 @SmallTest 179 public void testFlushAllWithEmptyContents() throws Exception { 180 EasyMock.replay(mMockProvider); 181 182 mMediaInserter.flushAll(); 183 184 EasyMock.verify(mMockProvider); 185 } 186 187 @SmallTest 188 public void testFlushAllWithSomeContents() throws Exception { 189 EasyMock.expect(mMockProvider.bulkInsert(mPackageName, 190 (Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1); 191 EasyMock.expectLastCall().times(4); 192 EasyMock.replay(mMockProvider); 193 194 fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4); 195 fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3); 196 fillBuffer(sVideoUri, TEST_BUFFER_SIZE - 2); 197 fillBuffer(sImagesUri, TEST_BUFFER_SIZE - 1); 198 mMediaInserter.flushAll(); 199 200 EasyMock.verify(mMockProvider); 201 } 202 203 @SmallTest 204 public void testInsertContentsAfterFlushAll() throws Exception { 205 EasyMock.expect(mMockProvider.bulkInsert(mPackageName, 206 (Uri) EasyMock.anyObject(), (ContentValues[]) EasyMock.anyObject())).andReturn(1); 207 EasyMock.expectLastCall().times(8); 208 EasyMock.replay(mMockProvider); 209 210 fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4); 211 fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3); 212 fillBuffer(sVideoUri, TEST_BUFFER_SIZE - 2); 213 fillBuffer(sImagesUri, TEST_BUFFER_SIZE - 1); 214 mMediaInserter.flushAll(); 215 216 fillBuffer(sFilesUri, TEST_BUFFER_SIZE + 1); 217 fillBuffer(sAudioUri, TEST_BUFFER_SIZE + 2); 218 fillBuffer(sVideoUri, TEST_BUFFER_SIZE + 3); 219 fillBuffer(sImagesUri, TEST_BUFFER_SIZE + 4); 220 221 EasyMock.verify(mMockProvider); 222 } 223 224 @SmallTest 225 public void testInsertContentsWithDifferentSizePerContentType() throws Exception { 226 EasyMock.expect(mMockProvider.bulkInsert(mPackageName, 227 MediaUriMatcher.expectMediaUri(sFilesUri), 228 (ContentValues[]) EasyMock.anyObject())).andReturn(1); 229 EasyMock.expectLastCall().times(1); 230 EasyMock.expect(mMockProvider.bulkInsert(mPackageName, 231 MediaUriMatcher.expectMediaUri(sAudioUri), 232 (ContentValues[]) EasyMock.anyObject())).andReturn(1); 233 EasyMock.expectLastCall().times(2); 234 EasyMock.expect(mMockProvider.bulkInsert(mPackageName, 235 MediaUriMatcher.expectMediaUri(sVideoUri), 236 (ContentValues[]) EasyMock.anyObject())).andReturn(1); 237 EasyMock.expectLastCall().times(3); 238 EasyMock.expect(mMockProvider.bulkInsert(mPackageName, 239 MediaUriMatcher.expectMediaUri(sImagesUri), 240 (ContentValues[]) EasyMock.anyObject())).andReturn(1); 241 EasyMock.expectLastCall().times(4); 242 EasyMock.replay(mMockProvider); 243 244 for (int i = 0; i < TEST_BUFFER_SIZE; ++i) { 245 fillBuffer(sFilesUri, 1); 246 fillBuffer(sAudioUri, 2); 247 fillBuffer(sVideoUri, 3); 248 fillBuffer(sImagesUri, 4); 249 } 250 251 EasyMock.verify(mMockProvider); 252 } 253 } 254