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
17package com.android.mediaframeworktest.unit;
18
19import static org.mockito.Matchers.any;
20import static org.mockito.Matchers.argThat;
21import static org.mockito.Matchers.eq;
22import static org.mockito.Mockito.never;
23import static org.mockito.Mockito.times;
24import static org.mockito.Mockito.verify;
25import static org.mockito.Mockito.when;
26
27import android.content.ContentProviderClient;
28import android.content.ContentValues;
29import android.content.IContentProvider;
30import android.media.MediaInserter;
31import android.net.Uri;
32import android.provider.MediaStore.Audio;
33import android.provider.MediaStore.Files;
34import android.provider.MediaStore.Images;
35import android.provider.MediaStore.Video;
36import android.test.InstrumentationTestCase;
37import android.test.suitebuilder.annotation.SmallTest;
38
39import org.mockito.ArgumentMatcher;
40import org.mockito.Mock;
41import org.mockito.MockitoAnnotations;
42
43public class MediaInserterTest extends InstrumentationTestCase {
44
45    private MediaInserter mMediaInserter;
46    private static final int TEST_BUFFER_SIZE = 10;
47    private @Mock IContentProvider mMockProvider;
48    private String mPackageName;
49
50    private int mFilesCounter;
51    private int mAudioCounter;
52    private int mVideoCounter;
53    private int mImagesCounter;
54
55    private static final String sVolumeName = "external";
56    private static final Uri sAudioUri = Audio.Media.getContentUri(sVolumeName);
57    private static final Uri sVideoUri = Video.Media.getContentUri(sVolumeName);
58    private static final Uri sImagesUri = Images.Media.getContentUri(sVolumeName);
59    private static final Uri sFilesUri = Files.getContentUri(sVolumeName);
60
61    private static class MediaUriMatcher implements ArgumentMatcher<Uri> {
62        private final Uri mUri;
63
64        private MediaUriMatcher(Uri uri) {
65            mUri = uri;
66        }
67
68        @Override
69        public boolean matches(Uri actualUri) {
70            return actualUri == mUri;
71        }
72
73        @Override
74        public String toString() {
75            return "expected a TableUri '" + mUri.toString() + "'";
76        }
77    }
78
79    private static Uri eqUri(Uri in) {
80        return argThat(new MediaUriMatcher(in));
81    }
82
83    @Override
84    protected void setUp() throws Exception {
85        super.setUp();
86        MockitoAnnotations.initMocks(this);
87
88        final ContentProviderClient client = new ContentProviderClient(
89                getInstrumentation().getContext().getContentResolver(), mMockProvider, true);
90        mMediaInserter = new MediaInserter(client, TEST_BUFFER_SIZE);
91        mPackageName = getInstrumentation().getContext().getPackageName();
92        mFilesCounter = 0;
93        mAudioCounter = 0;
94        mVideoCounter = 0;
95        mImagesCounter = 0;
96    }
97
98    private ContentValues createFileContent() {
99        ContentValues values = new ContentValues();
100        values.put("_data", "/mnt/sdcard/file" + ++mFilesCounter);
101        return values;
102    }
103
104    private ContentValues createAudioContent() {
105        ContentValues values = new ContentValues();
106        values.put("_data", "/mnt/sdcard/audio" + ++mAudioCounter);
107        return values;
108    }
109
110    private ContentValues createVideoContent() {
111        ContentValues values = new ContentValues();
112        values.put("_data", "/mnt/sdcard/video" + ++mVideoCounter);
113        return values;
114    }
115
116    private ContentValues createImageContent() {
117        ContentValues values = new ContentValues();
118        values.put("_data", "/mnt/sdcard/image" + ++mImagesCounter);
119        return values;
120    }
121
122    private ContentValues createContent(Uri uri) {
123        if (uri == sFilesUri) return createFileContent();
124        else if (uri == sAudioUri) return createAudioContent();
125        else if (uri == sVideoUri) return createVideoContent();
126        else if (uri == sImagesUri) return createImageContent();
127        else throw new IllegalArgumentException("Unknown URL: " + uri.toString());
128    }
129
130    private void fillBuffer(Uri uri, int numberOfFiles) throws Exception {
131        ContentValues values;
132        for (int i = 0; i < numberOfFiles; ++i) {
133            values = createContent(uri);
134            mMediaInserter.insert(uri, values);
135        }
136    }
137
138    @SmallTest
139    public void testInsertContentsLessThanBufferSize() throws Exception {
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        verify(mMockProvider, never()).bulkInsert(eq(mPackageName), any(), any());
146    }
147
148    @SmallTest
149    public void testInsertContentsEqualToBufferSize() throws Exception {
150        when(mMockProvider.bulkInsert(eq(mPackageName), any(), any())).thenReturn(1);
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        verify(mMockProvider, times(4)).bulkInsert(eq(mPackageName), any(), any());
158    }
159
160    @SmallTest
161    public void testInsertContentsMoreThanBufferSize() throws Exception {
162        when(mMockProvider.bulkInsert(eq(mPackageName), any(), any())).thenReturn(1);
163
164        fillBuffer(sFilesUri, TEST_BUFFER_SIZE + 1);
165        fillBuffer(sAudioUri, TEST_BUFFER_SIZE + 2);
166        fillBuffer(sVideoUri, TEST_BUFFER_SIZE + 3);
167        fillBuffer(sImagesUri, TEST_BUFFER_SIZE + 4);
168
169        verify(mMockProvider, times(4)).bulkInsert(eq(mPackageName), any(), any());
170    }
171
172    @SmallTest
173    public void testFlushAllWithEmptyContents() throws Exception {
174        mMediaInserter.flushAll();
175    }
176
177    @SmallTest
178    public void testFlushAllWithSomeContents() throws Exception {
179        when(mMockProvider.bulkInsert(eq(mPackageName), any(), any())).thenReturn(1);
180
181        fillBuffer(sFilesUri, TEST_BUFFER_SIZE - 4);
182        fillBuffer(sAudioUri, TEST_BUFFER_SIZE - 3);
183        fillBuffer(sVideoUri, TEST_BUFFER_SIZE - 2);
184        fillBuffer(sImagesUri, TEST_BUFFER_SIZE - 1);
185        mMediaInserter.flushAll();
186
187        verify(mMockProvider, times(4)).bulkInsert(eq(mPackageName), any(), any());
188    }
189
190    @SmallTest
191    public void testInsertContentsAfterFlushAll() throws Exception {
192        when(mMockProvider.bulkInsert(eq(mPackageName), any(), any())).thenReturn(1);
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        fillBuffer(sFilesUri, TEST_BUFFER_SIZE + 1);
201        fillBuffer(sAudioUri, TEST_BUFFER_SIZE + 2);
202        fillBuffer(sVideoUri, TEST_BUFFER_SIZE + 3);
203        fillBuffer(sImagesUri, TEST_BUFFER_SIZE + 4);
204
205        verify(mMockProvider, times(8)).bulkInsert(eq(mPackageName), any(), any());
206    }
207
208    @SmallTest
209    public void testInsertContentsWithDifferentSizePerContentType() throws Exception {
210        when(mMockProvider.bulkInsert(eq(mPackageName), eqUri(sFilesUri), any())).thenReturn(1);
211        when(mMockProvider.bulkInsert(eq(mPackageName), eqUri(sAudioUri), any())).thenReturn(1);
212        when(mMockProvider.bulkInsert(eq(mPackageName), eqUri(sVideoUri), any())).thenReturn(1);
213        when(mMockProvider.bulkInsert(eq(mPackageName), eqUri(sImagesUri), any())).thenReturn(1);
214
215        for (int i = 0; i < TEST_BUFFER_SIZE; ++i) {
216            fillBuffer(sFilesUri, 1);
217            fillBuffer(sAudioUri, 2);
218            fillBuffer(sVideoUri, 3);
219            fillBuffer(sImagesUri, 4);
220        }
221
222        verify(mMockProvider, times(1)).bulkInsert(eq(mPackageName), eqUri(sFilesUri), any());
223        verify(mMockProvider, times(2)).bulkInsert(eq(mPackageName), eqUri(sAudioUri), any());
224        verify(mMockProvider, times(3)).bulkInsert(eq(mPackageName), eqUri(sVideoUri), any());
225        verify(mMockProvider, times(4)).bulkInsert(eq(mPackageName), eqUri(sImagesUri), any());
226    }
227}
228