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.database.SQLException;
     24 import android.net.Uri;
     25 import android.os.Environment;
     26 import android.provider.MediaStore.Audio.Playlists;
     27 import android.test.InstrumentationTestCase;
     28 
     29 import java.util.regex.Pattern;
     30 
     31 public class MediaStore_Audio_PlaylistsTest 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         assertNotNull(c = mContentResolver.query(
     44                 Playlists.getContentUri(MediaStoreAudioTestHelper.EXTERNAL_VOLUME_NAME), null, null,
     45                 null, null));
     46         c.close();
     47 
     48         // can not accept any other volume names
     49         try {
     50             assertNotNull(c = mContentResolver.query(
     51                     Playlists.getContentUri(MediaStoreAudioTestHelper.INTERNAL_VOLUME_NAME), null,
     52                     null, null, null));
     53             c.close();
     54             fail("Should throw SQLException as the internal datatbase has no playlist");
     55         } catch (SQLException e) {
     56             // expected
     57         }
     58 
     59         String volume = "fakeVolume";
     60         assertNull(mContentResolver.query(Playlists.getContentUri(volume), null, null, null,
     61                 null));
     62     }
     63 
     64     public void testStoreAudioPlaylistsExternal() {
     65         final String externalPlaylistPath = Environment.getExternalStorageDirectory().getPath() +
     66             "/my_favorites.pl";
     67         ContentValues values = new ContentValues();
     68         values.put(Playlists.NAME, "My favourites");
     69         values.put(Playlists.DATA, externalPlaylistPath);
     70         long dateAdded = System.currentTimeMillis() / 1000;
     71         long dateModified = System.currentTimeMillis() / 1000;
     72         values.put(Playlists.DATE_MODIFIED, dateModified);
     73         // insert
     74         Uri uri = mContentResolver.insert(Playlists.EXTERNAL_CONTENT_URI, values);
     75         assertNotNull(uri);
     76 
     77         try {
     78             // query
     79             Cursor c = mContentResolver.query(uri, null, null, null, null);
     80             assertEquals(1, c.getCount());
     81             c.moveToFirst();
     82             assertEquals("My favourites", c.getString(c.getColumnIndex(Playlists.NAME)));
     83             assertEquals(externalPlaylistPath,
     84                     c.getString(c.getColumnIndex(Playlists.DATA)));
     85 
     86             long realDateAdded = c.getLong(c.getColumnIndex(Playlists.DATE_ADDED));
     87             assertTrue(realDateAdded >= dateAdded);
     88             assertEquals(dateModified, c.getLong(c.getColumnIndex(Playlists.DATE_MODIFIED)));
     89             assertTrue(c.getLong(c.getColumnIndex(Playlists._ID)) > 0);
     90             c.close();
     91 
     92             // update
     93             values.clear();
     94             values.put(Playlists.NAME, "xxx");
     95             dateModified = System.currentTimeMillis();
     96             values.put(Playlists.DATE_MODIFIED, dateModified);
     97             assertEquals(1, mContentResolver.update(uri, values, null, null));
     98             c = mContentResolver.query(uri, null, null, null, null);
     99             c.moveToFirst();
    100             assertEquals("xxx", c.getString(c.getColumnIndex(Playlists.NAME)));
    101             assertEquals(externalPlaylistPath,
    102                     c.getString(c.getColumnIndex(Playlists.DATA)));
    103 
    104             assertEquals(realDateAdded, c.getLong(c.getColumnIndex(Playlists.DATE_ADDED)));
    105             assertEquals(dateModified, c.getLong(c.getColumnIndex(Playlists.DATE_MODIFIED)));
    106             c.close();
    107         } finally {
    108             assertEquals(1, mContentResolver.delete(uri, null, null));
    109         }
    110     }
    111 
    112     public void testStoreAudioPlaylistsInternal() {
    113         ContentValues values = new ContentValues();
    114         values.put(Playlists.NAME, "My favourites");
    115         values.put(Playlists.DATA, "/data/data/android.provider.cts/files/my_favorites.pl");
    116         long dateAdded = System.currentTimeMillis();
    117         values.put(Playlists.DATE_ADDED, dateAdded);
    118         long dateModified = System.currentTimeMillis();
    119         values.put(Playlists.DATE_MODIFIED, dateModified);
    120         // insert
    121         Uri uri = mContentResolver.insert(Playlists.INTERNAL_CONTENT_URI, values);
    122         assertNotNull(uri);
    123 
    124         try {
    125             assertTrue(Pattern.matches("content://media/internal/audio/playlists/\\d+",
    126                     uri.toString()));
    127         } finally {
    128             // delete the playlists
    129             assertEquals(1, mContentResolver.delete(uri, null, null));
    130         }
    131     }
    132 }
    133