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
17package com.android.music.tests.functional;
18
19import android.app.Activity;
20import android.content.*;
21import android.app.Instrumentation;
22import android.app.Instrumentation.ActivityMonitor;
23import android.content.Intent;
24import android.test.ActivityInstrumentationTestCase;
25import android.test.suitebuilder.annotation.LargeTest;
26import android.view.KeyEvent;
27import android.provider.MediaStore;
28import android.content.ContentResolver;
29import android.database.Cursor;
30
31import com.android.music.CreatePlaylist;
32import com.android.music.MusicUtils;
33import com.android.music.PlaylistBrowserActivity;
34import com.android.music.TrackBrowserActivity;
35
36import com.android.music.tests.MusicPlayerNames;
37import com.android.music.tests.functional.TestSongs;
38
39/**
40 * Junit / Instrumentation test case for the PlaylistBrowserActivity
41 * This test case need to run in the landscape mode and opened keyboard
42
43 */
44public class TestPlaylist extends ActivityInstrumentationTestCase <PlaylistBrowserActivity>{
45    private static String TAG = "musicplayertests";
46
47    public TestPlaylist() {
48        super("com.android.music",PlaylistBrowserActivity.class);
49    }
50
51    @Override
52    protected void setUp() throws Exception {
53        super.setUp();
54    }
55
56    @Override
57    protected void tearDown() throws Exception {
58        super.tearDown();
59    }
60
61
62    private void clearSearchString(int length){
63        Instrumentation inst = getInstrumentation();
64        for (int j=0; j< length; j++)
65            inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DEL);
66    }
67    /**
68     * Remove playlist
69     */
70    public void deletePlaylist(String playlistname) throws Exception{
71        Instrumentation inst = getInstrumentation();
72        inst.sendStringSync(playlistname);
73        Thread.sleep(MusicPlayerNames.WAIT_SHORT_TIME);
74        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
75        inst.invokeContextMenuAction(getActivity(), MusicUtils.Defs.CHILD_MENU_BASE + 1, 0);
76        Thread.sleep(MusicPlayerNames.WAIT_SHORT_TIME);
77        clearSearchString(playlistname.length());
78
79    }
80
81    /**
82     *  Start the trackBrowserActivity and add the new playlist
83     */
84    public void addNewPlaylist(String playListName) throws Exception{
85        Instrumentation inst = getInstrumentation();
86        Activity trackBrowserActivity;
87        ActivityMonitor trackBrowserMon = inst.addMonitor("com.android.music.TrackBrowserActivity",
88                null, false);
89        Intent intent = new Intent();
90        intent.setAction(Intent.ACTION_PICK);
91        intent.setClassName("com.android.music", "com.android.music.TrackBrowserActivity");
92        getActivity().startActivity(intent);
93        Thread.sleep(MusicPlayerNames.WAIT_LONG_TIME);
94        trackBrowserActivity = trackBrowserMon.waitForActivityWithTimeout(2000);
95        inst.invokeContextMenuAction(trackBrowserActivity, MusicUtils.Defs.NEW_PLAYLIST, 0);
96        Thread.sleep(MusicPlayerNames.WAIT_SHORT_TIME);
97        //Remove the default playlist name
98        clearSearchString(MusicPlayerNames.DEFAULT_PLAYLIST_LENGTH);
99        inst.sendStringSync(playListName);
100        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
101        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
102        Thread.sleep(MusicPlayerNames.WAIT_LONG_TIME);
103        trackBrowserActivity.finish();
104        clearSearchString(playListName.length());
105
106    }
107
108    /**
109     * Rename playlist
110     */
111    public void renamePlaylist(String oldPlaylistName, String newPlaylistName) throws Exception{
112        Instrumentation inst = getInstrumentation();
113        inst.sendStringSync(oldPlaylistName);
114        Thread.sleep(MusicPlayerNames.WAIT_SHORT_TIME);
115        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
116        inst.invokeContextMenuAction(getActivity(), MusicUtils.Defs.CHILD_MENU_BASE + 3, 0);
117        Thread.sleep(MusicPlayerNames.WAIT_SHORT_TIME);
118        //Remove the old playlist name
119        clearSearchString(oldPlaylistName.length());
120        inst.sendStringSync(newPlaylistName);
121        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_DOWN);
122        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_DPAD_CENTER);
123        Thread.sleep(MusicPlayerNames.WAIT_LONG_TIME);
124        clearSearchString(oldPlaylistName.length());
125    }
126
127    public boolean verifyPlaylist(String playlistname) throws Exception{
128        Cursor mCursor;
129        boolean isEmptyPlaylist = true;
130        String[] cols = new String[] {
131                MediaStore.Audio.Playlists.NAME
132        };
133        ContentResolver resolver = getActivity().getContentResolver();
134        if (resolver == null) {
135            System.out.println("resolver = null");
136            assertNull(TAG, resolver);
137        } else {
138            String whereclause = MediaStore.Audio.Playlists.NAME + " = '" + playlistname +"'";
139            mCursor = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
140                    cols, whereclause, null,
141                    MediaStore.Audio.Playlists.NAME);
142            isEmptyPlaylist = mCursor.moveToFirst();
143        }
144        return isEmptyPlaylist;
145    }
146
147    /**
148     * Test case 1: Add a playlist and delet the playlist just added.
149     * Verification: The mediastore playlist should be empty
150     */
151    @LargeTest
152    public void testDeletePlaylist() throws Exception{
153        boolean isEmptyPlaylist = true;
154        addNewPlaylist(MusicPlayerNames.DELETE_PLAYLIST_NAME);
155        deletePlaylist(MusicPlayerNames.DELETE_PLAYLIST_NAME);
156        isEmptyPlaylist = verifyPlaylist(MusicPlayerNames.DELETE_PLAYLIST_NAME);
157        assertFalse("testDeletePlaylist", isEmptyPlaylist);
158    }
159
160    /**
161     * Test case 2: Add playlist and rename the playlist just added.
162     * Verification: The mediastore playlist should contain the updated name.
163     */
164    @LargeTest
165    public void testRenamePlaylist() throws Exception{
166        boolean isEmptyPlaylist = true;
167        addNewPlaylist(MusicPlayerNames.ORIGINAL_PLAYLIST_NAME);
168        renamePlaylist(MusicPlayerNames.ORIGINAL_PLAYLIST_NAME, MusicPlayerNames.RENAMED_PLAYLIST_NAME);
169        isEmptyPlaylist = verifyPlaylist(MusicPlayerNames.RENAMED_PLAYLIST_NAME);
170        deletePlaylist(MusicPlayerNames.RENAMED_PLAYLIST_NAME);
171        assertTrue("testDeletePlaylist", isEmptyPlaylist);
172    }
173
174}
175