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 android.content.ContentResolver;
     20 import android.content.ContentValues;
     21 import android.net.Uri;
     22 import android.provider.MediaStore;
     23 import android.provider.MediaStore.Audio.Media;
     24 
     25 import androidx.test.runner.AndroidJUnit4;
     26 
     27 import junit.framework.Assert;
     28 
     29 import org.junit.Test;
     30 import org.junit.runner.RunWith;
     31 
     32 import java.io.File;
     33 import java.io.IOException;
     34 
     35 /**
     36  * This class contains fake data and convenient methods for testing:
     37  * {@link MediaStore.Audio.Media}
     38  * {@link MediaStore.Audio.Genres}
     39  * {@link MediaStore.Audio.Genres.Members}
     40  * {@link MediaStore.Audio.Playlists}
     41  * {@link MediaStore.Audio.Playlists.Members}
     42  * {@link MediaStore.Audio.Albums}
     43  * {@link MediaStore.Audio.Artists}
     44  * {@link MediaStore.Audio.Artists.Albums}
     45  *
     46  * @see MediaStore_Audio_MediaTest
     47  * @see MediaStore_Audio_GenresTest
     48  * @see MediaStore_Audio_Genres_MembersTest
     49  * @see MediaStore_Audio_PlaylistsTest
     50  * @see MediaStore_Audio_Playlists_MembersTest
     51  * @see MediaStore_Audio_ArtistsTest
     52  * @see MediaStore_Audio_Artists_AlbumsTest
     53  * @see MediaStore_Audio_AlbumsTest
     54  */
     55 @RunWith(AndroidJUnit4.class)
     56 public class MediaStoreAudioTestHelper {
     57     public static abstract class MockAudioMediaInfo {
     58         public abstract ContentValues getContentValues(String volumeName);
     59 
     60         public Uri insert(ContentResolver contentResolver, String volumeName) {
     61             final Uri dirUri = MediaStore.Audio.Media.getContentUri(volumeName);
     62             final ContentValues values = getContentValues(volumeName);
     63             contentResolver.delete(dirUri, MediaStore.Audio.Media.DATA + "=?", new String[] {
     64                     values.getAsString(MediaStore.Audio.Media.DATA)
     65             });
     66 
     67             final Uri itemUri = contentResolver.insert(dirUri, values);
     68             Assert.assertNotNull(itemUri);
     69             return itemUri;
     70         }
     71 
     72         public int delete(ContentResolver contentResolver, Uri uri) {
     73             return contentResolver.delete(uri, null, null);
     74         }
     75     }
     76 
     77     public static class Audio1 extends MockAudioMediaInfo {
     78         private Audio1() {
     79         }
     80 
     81         private static Audio1 sInstance = new Audio1();
     82 
     83         public static Audio1 getInstance() {
     84             return sInstance;
     85         }
     86 
     87         public static final int IS_RINGTONE = 0;
     88         public static final int IS_NOTIFICATION = 0;
     89         public static final int IS_ALARM = 0;
     90         public static final int IS_MUSIC = 1;
     91         public static final int YEAR = 1992;
     92         public static final int TRACK = 1;
     93         public static final int DURATION = 340000;
     94         public static final String COMPOSER = "Bruce Swedien";
     95         public static final String ARTIST = "Michael Jackson";
     96         public static final String ALBUM = "Dangerous";
     97         public static final String TITLE = "Jam";
     98         public static final int SIZE = 2737870;
     99         public static final String MIME_TYPE = "audio/x-mpeg";
    100         public static final String FILE_NAME = "Jam.mp3";
    101         public static final String DISPLAY_NAME = FILE_NAME;
    102         public static final long DATE_MODIFIED = System.currentTimeMillis() / 1000;
    103         public static final String GENRE = "POP";
    104 
    105         @Override
    106         public ContentValues getContentValues(String volumeName) {
    107             ContentValues values = new ContentValues();
    108             try {
    109                 final File data;
    110                 if (MediaStore.VOLUME_INTERNAL.equals(volumeName)) {
    111                     data = new File("/data/data/android.provider.cts/files/", FILE_NAME);
    112                 } else {
    113                     data = new File(ProviderTestUtils.stageDir(volumeName), FILE_NAME);
    114                 }
    115                 values.put(Media.DATA, data.getAbsolutePath());
    116             } catch (IOException e) {
    117                 throw new RuntimeException(e);
    118             }
    119             values.put(Media.DATE_MODIFIED, DATE_MODIFIED);
    120             values.put(Media.DISPLAY_NAME, DISPLAY_NAME);
    121             values.put(Media.MIME_TYPE, MIME_TYPE);
    122             values.put(Media.SIZE, SIZE);
    123             values.put(Media.TITLE, TITLE);
    124             values.put(Media.ALBUM, ALBUM);
    125             values.put(Media.ARTIST, ARTIST);
    126             values.put(Media.COMPOSER, COMPOSER);
    127             values.put(Media.DURATION, DURATION);
    128             values.put(Media.TRACK, TRACK);
    129             values.put(Media.YEAR, YEAR);
    130             values.put(Media.IS_MUSIC, IS_MUSIC);
    131             values.put(Media.IS_ALARM, IS_ALARM);
    132             values.put(Media.IS_NOTIFICATION, IS_NOTIFICATION);
    133             values.put(Media.IS_RINGTONE, IS_RINGTONE);
    134             return values;
    135         }
    136     }
    137 
    138     public static class Audio2 extends MockAudioMediaInfo {
    139         private Audio2() {
    140         }
    141 
    142         private static Audio2 sInstance = new Audio2();
    143 
    144         public static Audio2 getInstance() {
    145             return sInstance;
    146         }
    147 
    148         public static final int IS_RINGTONE = 1;
    149         public static final int IS_NOTIFICATION = 0;
    150         public static final int IS_ALARM = 0;
    151         public static final int IS_MUSIC = 0;
    152         public static final int YEAR = 1992;
    153         public static final int TRACK = 1001;
    154         public static final int DURATION = 338000;
    155         public static final String COMPOSER = "Bruce Swedien";
    156         public static final String ARTIST =
    157             "Michael Jackson - Live And Dangerous - National Stadium Bucharest";
    158         public static final String ALBUM =
    159             "Michael Jackson - Live And Dangerous - National Stadium Bucharest";
    160         public static final String TITLE = "Jam";
    161         public static final int SIZE = 2737321;
    162         public static final String MIME_TYPE = "audio/x-mpeg";
    163         public static final String FILE_NAME = "Jam_live.mp3";
    164         public static final String DISPLAY_NAME = FILE_NAME;
    165         public static final long DATE_MODIFIED = System.currentTimeMillis() / 1000;
    166 
    167         @Override
    168         public ContentValues getContentValues(String volumeName) {
    169             ContentValues values = new ContentValues();
    170             try {
    171                 final File data;
    172                 if (MediaStore.VOLUME_INTERNAL.equals(volumeName)) {
    173                     data = new File("/data/data/android.provider.cts/files/", FILE_NAME);
    174                 } else {
    175                     data = new File(ProviderTestUtils.stageDir(volumeName), FILE_NAME);
    176                 }
    177                 values.put(Media.DATA, data.getAbsolutePath());
    178             } catch (IOException e) {
    179                 throw new RuntimeException(e);
    180             }
    181             values.put(Media.DATE_MODIFIED, DATE_MODIFIED);
    182             values.put(Media.DISPLAY_NAME, DISPLAY_NAME);
    183             values.put(Media.MIME_TYPE, MIME_TYPE);
    184             values.put(Media.SIZE, SIZE);
    185             values.put(Media.TITLE, TITLE);
    186             values.put(Media.ALBUM, ALBUM);
    187             values.put(Media.ARTIST, ARTIST);
    188             values.put(Media.COMPOSER, COMPOSER);
    189             values.put(Media.DURATION, DURATION);
    190             values.put(Media.TRACK, TRACK);
    191             values.put(Media.YEAR, YEAR);
    192             values.put(Media.IS_MUSIC, IS_MUSIC);
    193             values.put(Media.IS_ALARM, IS_ALARM);
    194             values.put(Media.IS_NOTIFICATION, IS_NOTIFICATION);
    195             values.put(Media.IS_RINGTONE, IS_RINGTONE);
    196             return values;
    197         }
    198     }
    199 
    200     public static class Audio3 extends Audio1 {
    201         private Audio3() {
    202         }
    203 
    204         private static Audio3 sInstance = new Audio3();
    205 
    206         public static Audio3 getInstance() {
    207             return sInstance;
    208         }
    209 
    210         @Override
    211         public ContentValues getContentValues(String volumeName) {
    212             ContentValues values = super.getContentValues(volumeName);
    213             values.put(Media.DATA, values.getAsString(Media.DATA) + "_3");
    214             return values;
    215         }
    216     }
    217 
    218     public static class Audio4 extends Audio1 {
    219         private Audio4() {
    220         }
    221 
    222         private static Audio4 sInstance = new Audio4();
    223 
    224         public static Audio4 getInstance() {
    225             return sInstance;
    226         }
    227 
    228         @Override
    229         public ContentValues getContentValues(String volumeName) {
    230             ContentValues values = super.getContentValues(volumeName);
    231             values.put(Media.DATA, values.getAsString(Media.DATA) + "_4");
    232             return values;
    233         }
    234     }
    235 
    236     public static class Audio5 extends Audio1 {
    237         private Audio5() {
    238         }
    239 
    240         private static Audio5 sInstance = new Audio5();
    241 
    242         public static Audio5 getInstance() {
    243             return sInstance;
    244         }
    245 
    246         @Override
    247         public ContentValues getContentValues(String volumeName) {
    248             ContentValues values = super.getContentValues(volumeName);
    249             values.put(Media.DATA, values.getAsString(Media.DATA) + "_5");
    250             return values;
    251         }
    252     }
    253 
    254     @Test
    255     public void testStub() {
    256         // No-op test here to keep atest happy
    257     }
    258 
    259     // These constants are not part of the public API
    260     public static final String EXTERNAL_VOLUME_NAME = "external";
    261     public static final String INTERNAL_VOLUME_NAME = "internal";
    262 }
    263