1 /* 2 * Copyright (C) 2009 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 android.provider.cts; 18 19 20 import android.content.ContentResolver; 21 import android.content.ContentValues; 22 import android.database.Cursor; 23 import android.net.Uri; 24 import android.os.Environment; 25 import android.provider.MediaStore.Audio.Media; 26 import android.provider.cts.MediaStoreAudioTestHelper.Audio1; 27 import android.provider.cts.MediaStoreAudioTestHelper.Audio2; 28 import android.test.InstrumentationTestCase; 29 30 public class MediaStore_Audio_MediaTest extends InstrumentationTestCase { 31 private ContentResolver mContentResolver; 32 33 @Override 34 protected void setUp() throws Exception { 35 super.setUp(); 36 37 mContentResolver = getInstrumentation().getContext().getContentResolver(); 38 } 39 40 public void testGetContentUri() { 41 Cursor c = null; 42 assertNotNull(c = mContentResolver.query( 43 Media.getContentUri(MediaStoreAudioTestHelper.INTERNAL_VOLUME_NAME), null, null, 44 null, null)); 45 c.close(); 46 assertNotNull(c = mContentResolver.query( 47 Media.getContentUri(MediaStoreAudioTestHelper.EXTERNAL_VOLUME_NAME), null, null, 48 null, null)); 49 c.close(); 50 51 // can not accept any other volume names 52 String volume = "faveVolume"; 53 assertNull(mContentResolver.query(Media.getContentUri(volume), null, null, null, null)); 54 } 55 56 public void testGetContentUriForPath() { 57 Cursor c = null; 58 String externalPath = Environment.getExternalStorageDirectory().getPath(); 59 assertNotNull(c = mContentResolver.query(Media.getContentUriForPath(externalPath), null, null, 60 null, null)); 61 c.close(); 62 63 String internalPath = 64 getInstrumentation().getTargetContext().getFilesDir().getAbsolutePath(); 65 assertNotNull(c = mContentResolver.query(Media.getContentUriForPath(internalPath), null, null, 66 null, null)); 67 c.close(); 68 } 69 70 public void testStoreAudioMediaInternal() { 71 testStoreAudioMedia(true); 72 } 73 74 public void testStoreAudioMediaExternal() { 75 testStoreAudioMedia(false); 76 } 77 78 private void testStoreAudioMedia(boolean isInternal) { 79 Audio1 audio1 = Audio1.getInstance(); 80 ContentValues values = audio1.getContentValues(isInternal); 81 //insert 82 Uri mediaUri = isInternal ? Media.INTERNAL_CONTENT_URI : Media.EXTERNAL_CONTENT_URI; 83 Uri uri = mContentResolver.insert(mediaUri, values); 84 assertNotNull(uri); 85 86 try { 87 // query 88 // the following columns in the table are generated automatically when inserting: 89 // _ID, DATE_ADDED, ALBUM_ID, ALBUM_KEY, ARTIST_ID, ARTIST_KEY, TITLE_KEY 90 // the column DISPLAY_NAME will be ignored when inserting 91 Cursor c = mContentResolver.query(uri, null, null, null, null); 92 assertEquals(1, c.getCount()); 93 c.moveToFirst(); 94 long id = c.getLong(c.getColumnIndex(Media._ID)); 95 assertTrue(id > 0); 96 String expected = isInternal ? Audio1.INTERNAL_DATA : Audio1.EXTERNAL_DATA; 97 assertEquals(expected, c.getString(c.getColumnIndex(Media.DATA))); 98 assertTrue(c.getLong(c.getColumnIndex(Media.DATE_ADDED)) > 0); 99 assertEquals(Audio1.DATE_MODIFIED, c.getLong(c.getColumnIndex(Media.DATE_MODIFIED))); 100 assertEquals(Audio1.IS_DRM, c.getInt(c.getColumnIndex(Media.IS_DRM))); 101 assertEquals(Audio1.FILE_NAME, c.getString(c.getColumnIndex(Media.DISPLAY_NAME))); 102 assertEquals(Audio1.MIME_TYPE, c.getString(c.getColumnIndex(Media.MIME_TYPE))); 103 assertEquals(Audio1.SIZE, c.getInt(c.getColumnIndex(Media.SIZE))); 104 assertEquals(Audio1.TITLE, c.getString(c.getColumnIndex(Media.TITLE))); 105 assertEquals(Audio1.ALBUM, c.getString(c.getColumnIndex(Media.ALBUM))); 106 String albumKey = c.getString(c.getColumnIndex(Media.ALBUM_KEY)); 107 assertNotNull(albumKey); 108 long albumId = c.getLong(c.getColumnIndex(Media.ALBUM_ID)); 109 assertTrue(albumId > 0); 110 assertEquals(Audio1.ARTIST, c.getString(c.getColumnIndex(Media.ARTIST))); 111 String artistKey = c.getString(c.getColumnIndex(Media.ARTIST_KEY)); 112 assertNotNull(artistKey); 113 long artistId = c.getLong(c.getColumnIndex(Media.ARTIST_ID)); 114 assertTrue(artistId > 0); 115 assertEquals(Audio1.COMPOSER, c.getString(c.getColumnIndex(Media.COMPOSER))); 116 assertEquals(Audio1.DURATION, c.getLong(c.getColumnIndex(Media.DURATION))); 117 assertEquals(Audio1.IS_ALARM, c.getInt(c.getColumnIndex(Media.IS_ALARM))); 118 assertEquals(Audio1.IS_MUSIC, c.getInt(c.getColumnIndex(Media.IS_MUSIC))); 119 assertEquals(Audio1.IS_NOTIFICATION, c.getInt(c.getColumnIndex(Media.IS_NOTIFICATION))); 120 assertEquals(Audio1.IS_RINGTONE, c.getInt(c.getColumnIndex(Media.IS_RINGTONE))); 121 assertEquals(Audio1.TRACK, c.getInt(c.getColumnIndex(Media.TRACK))); 122 assertEquals(Audio1.YEAR, c.getInt(c.getColumnIndex(Media.YEAR))); 123 String titleKey = c.getString(c.getColumnIndex(Media.TITLE_KEY)); 124 assertNotNull(titleKey); 125 c.close(); 126 127 // update 128 // the column DISPLAY_NAME will not be ignored when updating 129 Audio2 audio2 = Audio2.getInstance(); 130 values = audio2.getContentValues(isInternal); 131 132 int result = mContentResolver.update(uri, values, null, null); 133 assertEquals(1, result); 134 c = mContentResolver.query(uri, null, null, null, null); 135 assertEquals(1, c.getCount()); 136 c.moveToFirst(); 137 long id2 = c.getLong(c.getColumnIndex(Media._ID)); 138 assertTrue(id == id2); 139 expected = isInternal ? Audio2.INTERNAL_DATA : Audio2.EXTERNAL_DATA; 140 assertEquals(expected, c.getString(c.getColumnIndex(Media.DATA))); 141 assertEquals(Audio2.DATE_MODIFIED, c.getLong(c.getColumnIndex(Media.DATE_MODIFIED))); 142 assertEquals(Audio2.IS_DRM, c.getInt(c.getColumnIndex(Media.IS_DRM))); 143 assertEquals(Audio2.DISPLAY_NAME, c.getString(c.getColumnIndex(Media.DISPLAY_NAME))); 144 assertEquals(Audio2.MIME_TYPE, c.getString(c.getColumnIndex(Media.MIME_TYPE))); 145 assertEquals(Audio2.SIZE, c.getInt(c.getColumnIndex(Media.SIZE))); 146 assertEquals(Audio2.TITLE, c.getString(c.getColumnIndex(Media.TITLE))); 147 assertEquals(Audio2.ALBUM, c.getString(c.getColumnIndex(Media.ALBUM))); 148 assertFalse(albumKey.equals(c.getString(c.getColumnIndex(Media.ALBUM_KEY)))); 149 assertTrue(albumId != c.getLong(c.getColumnIndex(Media.ALBUM_ID))); 150 assertEquals(Audio2.ARTIST, c.getString(c.getColumnIndex(Media.ARTIST))); 151 assertFalse(artistKey.equals(c.getString(c.getColumnIndex(Media.ARTIST_KEY)))); 152 assertTrue(artistId != c.getLong(c.getColumnIndex(Media.ARTIST_ID))); 153 assertEquals(Audio2.COMPOSER, c.getString(c.getColumnIndex(Media.COMPOSER))); 154 assertEquals(Audio2.DURATION, c.getLong(c.getColumnIndex(Media.DURATION))); 155 assertEquals(Audio2.IS_ALARM, c.getInt(c.getColumnIndex(Media.IS_ALARM))); 156 assertEquals(Audio2.IS_MUSIC, c.getInt(c.getColumnIndex(Media.IS_MUSIC))); 157 assertEquals(Audio2.IS_NOTIFICATION, 158 c.getInt(c.getColumnIndex(Media.IS_NOTIFICATION))); 159 assertEquals(Audio2.IS_RINGTONE, c.getInt(c.getColumnIndex(Media.IS_RINGTONE))); 160 assertEquals(Audio2.TRACK, c.getInt(c.getColumnIndex(Media.TRACK))); 161 assertEquals(Audio2.YEAR, c.getInt(c.getColumnIndex(Media.YEAR))); 162 assertTrue(titleKey.equals(c.getString(c.getColumnIndex(Media.TITLE_KEY)))); 163 c.close(); 164 165 // test filtering 166 Uri baseUri = isInternal ? Media.INTERNAL_CONTENT_URI : Media.EXTERNAL_CONTENT_URI; 167 Uri filterUri = baseUri.buildUpon() 168 .appendQueryParameter("filter", Audio2.ARTIST).build(); 169 c = mContentResolver.query(filterUri, null, null, null, null); 170 assertEquals(1, c.getCount()); 171 c.moveToFirst(); 172 long fid = c.getLong(c.getColumnIndex(Media._ID)); 173 assertTrue(id == fid); 174 c.close(); 175 176 filterUri = baseUri.buildUpon().appendQueryParameter("filter", "xyzfoo").build(); 177 c = mContentResolver.query(filterUri, null, null, null, null); 178 assertEquals(0, c.getCount()); 179 c.close(); 180 } finally { 181 // delete 182 int result = mContentResolver.delete(uri, null, null); 183 assertEquals(1, result); 184 } 185 } 186 } 187