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 
     20 import android.content.ContentResolver;
     21 import android.content.ContentValues;
     22 import android.database.Cursor;
     23 import android.net.Uri;
     24 import android.provider.MediaStore;
     25 import android.provider.MediaStore.Audio.Media;
     26 import android.provider.MediaStore.Audio.Artists.Albums;
     27 import android.provider.cts.MediaStoreAudioTestHelper.Audio1;
     28 import android.provider.cts.MediaStoreAudioTestHelper.Audio2;
     29 import android.test.InstrumentationTestCase;
     30 
     31 public class MediaStore_Audio_Artists_AlbumsTest extends InstrumentationTestCase {
     32     private ContentResolver mContentResolver;
     33 
     34     @Override
     35     protected void setUp() throws Exception {
     36         super.setUp();
     37 
     38         mContentResolver = getInstrumentation().getContext().getContentResolver();
     39     }
     40 
     41     public void testGetContentUri() {
     42         Cursor c = null;
     43         Uri contentUri = MediaStore.Audio.Artists.Albums.getContentUri(
     44                 MediaStoreAudioTestHelper.INTERNAL_VOLUME_NAME, 1);
     45         assertNotNull(c = mContentResolver.query(contentUri, null, null, null, null));
     46         c.close();
     47 
     48         contentUri = MediaStore.Audio.Artists.Albums.getContentUri(
     49                 MediaStoreAudioTestHelper.EXTERNAL_VOLUME_NAME, 1);
     50         assertNotNull(c = mContentResolver.query(contentUri, null, null, null, null));
     51         c.close();
     52 
     53         // can not accept any other volume names
     54         String volume = "fakeVolume";
     55         assertNull(mContentResolver.query(MediaStore.Audio.Artists.Albums.getContentUri(volume, 1),
     56                 null, null, null, null));
     57     }
     58 
     59     public void testStoreAudioArtistsAlbumsInternal() {
     60         testStoreAudioArtistsAlbums(true);
     61     }
     62 
     63     public void testStoreAudioArtistsAlbumsExternal() {
     64         testStoreAudioArtistsAlbums(false);
     65     }
     66 
     67     private void testStoreAudioArtistsAlbums(boolean isInternal) {
     68         // the album item is inserted when inserting audio media
     69         Uri audioMediaUri = isInternal ? Audio1.getInstance().insertToInternal(mContentResolver)
     70                 : Audio1.getInstance().insertToExternal(mContentResolver);
     71         // get artist id
     72         Cursor c = mContentResolver.query(audioMediaUri, new String[] { Media.ARTIST_ID }, null,
     73                 null, null);
     74         c.moveToFirst();
     75         Long artistId = c.getLong(c.getColumnIndex(Media.ARTIST_ID));
     76         c.close();
     77         Uri artistsAlbumsUri = MediaStore.Audio.Artists.Albums.getContentUri(isInternal ?
     78                 MediaStoreAudioTestHelper.INTERNAL_VOLUME_NAME :
     79                     MediaStoreAudioTestHelper.EXTERNAL_VOLUME_NAME, artistId);
     80         // do not support insert operation of the albums
     81         try {
     82             mContentResolver.insert(artistsAlbumsUri, new ContentValues());
     83             fail("Should throw UnsupportedOperationException!");
     84         } catch (UnsupportedOperationException e) {
     85             // expected
     86         }
     87 
     88         try {
     89             // query
     90             c = mContentResolver.query(artistsAlbumsUri, null, null, null, null);
     91             assertEquals(1, c.getCount());
     92             c.moveToFirst();
     93 
     94             assertEquals(Audio1.ALBUM, c.getString(c.getColumnIndex(Albums.ALBUM)));
     95             assertNull(c.getString(c.getColumnIndex(Albums.ALBUM_ART)));
     96             assertNotNull(c.getString(c.getColumnIndex(Albums.ALBUM_KEY)));
     97             assertEquals(Audio1.ARTIST, c.getString(c.getColumnIndex(Albums.ARTIST)));
     98             assertEquals(Audio1.YEAR, c.getInt(c.getColumnIndex(Albums.FIRST_YEAR)));
     99             assertEquals(Audio1.YEAR, c.getInt(c.getColumnIndex(Albums.LAST_YEAR)));
    100             assertEquals(1, c.getInt(c.getColumnIndex(Albums.NUMBER_OF_SONGS)));
    101             assertEquals(1, c.getInt(c.getColumnIndex(Albums.NUMBER_OF_SONGS_FOR_ARTIST)));
    102             // the ALBUM_ID column does not exist
    103             try {
    104                 c.getColumnIndexOrThrow(Albums.ALBUM_ID);
    105                 fail("Should throw IllegalArgumentException because there is no column with name"
    106                         + " \"Albums.ALBUM_ID\" in the table");
    107             } catch (IllegalArgumentException e) {
    108                 // expected
    109             }
    110             c.close();
    111 
    112             // do not support update operation of the albums
    113             ContentValues albumValues = new ContentValues();
    114             albumValues.put(Albums.ALBUM, Audio2.ALBUM);
    115             try {
    116                 mContentResolver.update(artistsAlbumsUri, albumValues, null, null);
    117                 fail("Should throw UnsupportedOperationException!");
    118             } catch (UnsupportedOperationException e) {
    119                 // expected
    120             }
    121 
    122             // do not support delete operation of the albums
    123             try {
    124                 mContentResolver.delete(artistsAlbumsUri, null, null);
    125                 fail("Should throw UnsupportedOperationException!");
    126             } catch (UnsupportedOperationException e) {
    127                 // expected
    128             }
    129         } finally {
    130             mContentResolver.delete(audioMediaUri, null, null);
    131         }
    132         // the album items are deleted when deleting the audio media which belongs to the album
    133         c = mContentResolver.query(artistsAlbumsUri, null, null, null, null);
    134         assertEquals(0, c.getCount());
    135         c.close();
    136     }
    137 }
    138