private void testStoreAudioArtists(boolean isInternal) {
    Uri artistsUri = isInternal ? Artists.INTERNAL_CONTENT_URI : Artists.EXTERNAL_CONTENT_URI;
    // do not support insert operation of the artists
    try {
      mContentResolver.insert(artistsUri, new ContentValues());
      fail("Should throw UnsupportedOperationException!");
    } catch (UnsupportedOperationException e) {
      // expected
    }
    // the artist items are inserted when inserting audio media
    Uri uri =
        isInternal
            ? Audio1.getInstance().insertToInternal(mContentResolver)
            : Audio1.getInstance().insertToExternal(mContentResolver);

    String selection = Artists.ARTIST + "=?";
    String[] selectionArgs = new String[] {Audio1.ARTIST};
    try {
      // query
      Cursor c = mContentResolver.query(artistsUri, null, selection, selectionArgs, null);
      assertEquals(1, c.getCount());
      c.moveToFirst();

      assertEquals(Audio1.ARTIST, c.getString(c.getColumnIndex(Artists.ARTIST)));
      assertTrue(c.getLong(c.getColumnIndex(Artists._ID)) > 0);
      assertNotNull(c.getString(c.getColumnIndex(Artists.ARTIST_KEY)));
      assertEquals(1, c.getInt(c.getColumnIndex(Artists.NUMBER_OF_ALBUMS)));
      assertEquals(1, c.getInt(c.getColumnIndex(Artists.NUMBER_OF_TRACKS)));
      c.close();

      // do not support update operation of the artists
      ContentValues artistValues = new ContentValues();
      artistValues.put(Artists.ARTIST, Audio2.ALBUM);
      try {
        mContentResolver.update(artistsUri, artistValues, selection, selectionArgs);
        fail("Should throw UnsupportedOperationException!");
      } catch (UnsupportedOperationException e) {
        // expected
      }

      // do not support delete operation of the artists
      try {
        mContentResolver.delete(artistsUri, selection, selectionArgs);
        fail("Should throw UnsupportedOperationException!");
      } catch (UnsupportedOperationException e) {
        // expected
      }
    } finally {
      mContentResolver.delete(uri, null, null);
    }
    // the artist items are deleted when deleting the audio media which belongs to the album
    Cursor c = mContentResolver.query(artistsUri, null, selection, selectionArgs, null);
    assertEquals(0, c.getCount());
    c.close();
  }
Esempio n. 2
0
  private void testStoreAudioArtistsAlbums(boolean isInternal) {
    // the album item is inserted when inserting audio media
    Uri audioMediaUri =
        isInternal
            ? Audio1.getInstance().insertToInternal(mContentResolver)
            : Audio1.getInstance().insertToExternal(mContentResolver);
    // get artist id
    Cursor c =
        mContentResolver.query(audioMediaUri, new String[] {Media.ARTIST_ID}, null, null, null);
    c.moveToFirst();
    Long artistId = c.getLong(c.getColumnIndex(Media.ARTIST_ID));
    c.close();
    Uri artistsAlbumsUri =
        MediaStore.Audio.Artists.Albums.getContentUri(
            isInternal
                ? MediaStoreAudioTestHelper.INTERNAL_VOLUME_NAME
                : MediaStoreAudioTestHelper.EXTERNAL_VOLUME_NAME,
            artistId);
    // do not support insert operation of the albums
    try {
      mContentResolver.insert(artistsAlbumsUri, new ContentValues());
      fail("Should throw UnsupportedOperationException!");
    } catch (UnsupportedOperationException e) {
      // expected
    }

    try {
      // query
      c = mContentResolver.query(artistsAlbumsUri, null, null, null, null);
      assertEquals(1, c.getCount());
      c.moveToFirst();

      assertEquals(Audio1.ALBUM, c.getString(c.getColumnIndex(Albums.ALBUM)));
      assertNull(c.getString(c.getColumnIndex(Albums.ALBUM_ART)));
      assertNotNull(c.getString(c.getColumnIndex(Albums.ALBUM_KEY)));
      assertEquals(Audio1.ARTIST, c.getString(c.getColumnIndex(Albums.ARTIST)));
      assertEquals(Audio1.YEAR, c.getInt(c.getColumnIndex(Albums.FIRST_YEAR)));
      assertEquals(Audio1.YEAR, c.getInt(c.getColumnIndex(Albums.LAST_YEAR)));
      assertEquals(1, c.getInt(c.getColumnIndex(Albums.NUMBER_OF_SONGS)));
      assertEquals(1, c.getInt(c.getColumnIndex(Albums.NUMBER_OF_SONGS_FOR_ARTIST)));
      // the ALBUM_ID column does not exist
      try {
        c.getColumnIndexOrThrow(Albums.ALBUM_ID);
        fail(
            "Should throw IllegalArgumentException because there is no column with name"
                + " \"Albums.ALBUM_ID\" in the table");
      } catch (IllegalArgumentException e) {
        // expected
      }
      c.close();

      // do not support update operation of the albums
      ContentValues albumValues = new ContentValues();
      albumValues.put(Albums.ALBUM, Audio2.ALBUM);
      try {
        mContentResolver.update(artistsAlbumsUri, albumValues, null, null);
        fail("Should throw UnsupportedOperationException!");
      } catch (UnsupportedOperationException e) {
        // expected
      }

      // do not support delete operation of the albums
      try {
        mContentResolver.delete(artistsAlbumsUri, null, null);
        fail("Should throw UnsupportedOperationException!");
      } catch (UnsupportedOperationException e) {
        // expected
      }
    } finally {
      mContentResolver.delete(audioMediaUri, null, null);
    }
    // the album items are deleted when deleting the audio media which belongs to the album
    c = mContentResolver.query(artistsAlbumsUri, null, null, null, null);
    assertEquals(0, c.getCount());
    c.close();
  }