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