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