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