1 /* 2 * Copyright (C) 2008 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 com.android.music.tests.functional; 18 19 import android.app.Activity; 20 import android.content.*; 21 import android.app.Instrumentation; 22 import android.content.Intent; 23 import android.test.ActivityInstrumentationTestCase; 24 import android.test.suitebuilder.annotation.LargeTest; 25 import android.util.Log; 26 import android.view.KeyEvent; 27 import android.net.Uri; 28 import android.os.Environment; 29 import android.provider.MediaStore; 30 import android.content.ContentResolver; 31 import android.content.pm.ActivityInfo; 32 import android.database.Cursor; 33 import android.content.Intent; 34 import android.content.BroadcastReceiver; 35 import android.content.IntentFilter; 36 37 import com.android.music.TrackBrowserActivity; 38 import com.android.music.MusicUtils; 39 40 import com.android.music.tests.MusicPlayerNames; 41 42 import java.io.*; 43 44 /** 45 * Junit / Instrumentation test case for the TrackBrowserActivity 46 */ 47 public class TestSongs extends ActivityInstrumentationTestCase<TrackBrowserActivity> { 48 private static String TAG = "musicplayertests"; 49 TestSongs()50 public TestSongs() { 51 super("com.android.music", TrackBrowserActivity.class); 52 } 53 54 @Override setUp()55 protected void setUp() throws Exception { 56 super.setUp(); 57 } 58 59 @Override tearDown()60 protected void tearDown() throws Exception { 61 super.tearDown(); 62 } 63 64 /** 65 * Add 10 new playlists with unsorted title order 66 */ addNewPlaylist()67 public void addNewPlaylist() throws Exception { 68 Instrumentation inst = getInstrumentation(); 69 for (int i = 0; i < MusicPlayerNames.NO_OF_PLAYLIST; i++) { 70 inst.invokeContextMenuAction(getActivity(), 0, 0); 71 Thread.sleep(MusicPlayerNames.WAIT_SHORT_TIME); 72 // Remove the default playlist name 73 for (int j = 0; j < MusicPlayerNames.DEFAULT_PLAYLIST_LENGTH; j++) { 74 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DEL); 75 } 76 inst.sendStringSync(MusicPlayerNames.unsortedPlaylistTitle[i]); 77 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN); 78 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); 79 Thread.sleep(MusicPlayerNames.WAIT_LONG_TIME); 80 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN); 81 Thread.sleep(MusicPlayerNames.WAIT_LONG_TIME); 82 } 83 } 84 copy(File src, File dst)85 private void copy(File src, File dst) throws IOException { 86 InputStream in = new FileInputStream(src); 87 OutputStream out = new FileOutputStream(dst); 88 89 // Transfer bytes from in to out 90 byte[] buf = new byte[1024]; 91 int len; 92 while ((len = in.read(buf)) > 0) { 93 out.write(buf, 0, len); 94 } 95 in.close(); 96 out.close(); 97 Log.v(TAG, "Copy file"); 98 } 99 100 // Rescan the sdcard after copy the file rescanSdcard()101 private void rescanSdcard() throws Exception { 102 Intent scanIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED, 103 Uri.parse("file://" + Environment.getExternalStorageDirectory())); 104 Log.v(TAG, "start the intent"); 105 IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED); 106 intentFilter.addDataScheme("file"); 107 getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 108 Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 109 Thread.sleep(MusicPlayerNames.WAIT_VERY_LONG_TIME); 110 } 111 112 /** 113 * Test case 1: tests the new playlist added with sorted order. 114 * Verification: The new playlist title should be sorted in alphabetical order 115 */ 116 @LargeTest testAddPlaylist()117 public void testAddPlaylist() throws Exception { 118 Cursor mCursor; 119 addNewPlaylist(); 120 121 // Verify the new playlist is created, check the playlist table 122 String[] cols = new String[] {MediaStore.Audio.Playlists.NAME}; 123 ContentResolver resolver = getActivity().getContentResolver(); 124 if (resolver == null) { 125 System.out.println("resolver = null"); 126 } else { 127 String whereclause = MediaStore.Audio.Playlists.NAME + " != ''"; 128 mCursor = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, cols, 129 whereclause, null, MediaStore.Audio.Playlists.NAME); 130 // Check the new playlist 131 mCursor.moveToFirst(); 132 133 for (int j = 0; j < 10; j++) { 134 assertEquals("New sorted Playlist title:", 135 MusicPlayerNames.expectedPlaylistTitle[j], mCursor.getString(0)); 136 mCursor.moveToNext(); 137 } 138 } 139 } 140 141 /** 142 * Test case 2: Set a song as ringtone 143 * Test case precondition: The testing device should wipe data before 144 * run the test case. 145 * Verification: The count of audio.media.is_ringtone equal to 1. 146 */ 147 @LargeTest testSetRingtone()148 public void testSetRingtone() throws Exception { 149 Cursor mCursor; 150 Instrumentation inst = getInstrumentation(); 151 inst.invokeContextMenuAction(getActivity(), 0, 0); 152 // This only check if there only 1 ringtone set in music player 153 ContentResolver resolver = getActivity().getContentResolver(); 154 if (resolver == null) { 155 System.out.println("resolver = null"); 156 } else { 157 String whereclause = MediaStore.Audio.Media.IS_RINGTONE + " = 1"; 158 mCursor = resolver.query( 159 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, whereclause, null, null); 160 // Check the new playlist 161 mCursor.moveToFirst(); 162 int isRingtoneSet = mCursor.getCount(); 163 assertEquals(TAG, MusicPlayerNames.EXPECTED_NO_RINGTONE, isRingtoneSet); 164 } 165 } 166 167 /** 168 * Test case 3: Delete a song 169 * Test case precondition: Copy a song and rescan the sdcard 170 * Verification: The song is deleted from the sdcard and mediastore 171 */ 172 @LargeTest testDeleteSong()173 public void testDeleteSong() throws Exception { 174 Instrumentation inst = getInstrumentation(); 175 Cursor mCursor; 176 177 // Copy a song from the golden directory 178 Log.v(TAG, "Copy a temp file to the sdcard"); 179 File goldenfile = new File(MusicPlayerNames.GOLDENSONG); 180 File toBeDeleteSong = new File(MusicPlayerNames.DELETESONG); 181 copy(goldenfile, toBeDeleteSong); 182 rescanSdcard(); 183 184 // Delete the file from music player 185 Thread.sleep(MusicPlayerNames.WAIT_LONG_TIME); 186 inst.sendStringSync(MusicPlayerNames.TOBEDELETESONGNAME); 187 Thread.sleep(MusicPlayerNames.WAIT_LONG_TIME); 188 inst.invokeContextMenuAction(getActivity(), 0, 0); 189 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN); 190 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER); 191 Thread.sleep(MusicPlayerNames.WAIT_LONG_TIME); 192 193 // Clear the search string 194 for (int j = 0; j < MusicPlayerNames.TOBEDELETESONGNAME.length(); j++) { 195 inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DEL); 196 } 197 198 // Verfiy the item is removed from sdcard 199 File checkDeletedFile = new File(MusicPlayerNames.DELETESONG); 200 assertFalse(TAG, checkDeletedFile.exists()); 201 202 ContentResolver resolver = getActivity().getContentResolver(); 203 if (resolver == null) { 204 System.out.println("resolver = null"); 205 } else { 206 String whereclause = MediaStore.Audio.Media.DISPLAY_NAME + " = '" 207 + MusicPlayerNames.TOBEDELETESONGNAME + "'"; 208 mCursor = resolver.query( 209 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, whereclause, null, null); 210 boolean isEmptyCursor = mCursor.moveToFirst(); 211 assertFalse(TAG, isEmptyCursor); 212 } 213 } 214 } 215