9a9ace066538c2259796ce19719f8293b4738c0d
[vlc/bindings/jvlc.git] / core / src / test / java / org / videolan / jvlc / internal / LibVlcMediaListPlayerTest.java
1 /*****************************************************************************
2  * MediaListPlayerTest.java: VLC Java Bindings
3  *****************************************************************************
4  * Copyright (C) 1998-2008 the VideoLAN team
5  *
6  * Authors: Filippo Carone <filippo@carone.org>
7  *
8  *
9  * $Id $
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 package org.videolan.jvlc.internal;
27
28 import junit.framework.Assert;
29
30 import org.junit.After;
31 import org.junit.Test;
32 import org.videolan.jvlc.internal.LibVlc.LibVlcMedia;
33 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaPlayer;
34 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaList;
35 import org.videolan.jvlc.internal.LibVlc.LibVlcMediaListPlayer;
36 import org.videolan.jvlc.internal.LibVlc.libvlc_exception_t;
37
38
39 public class LibVlcMediaListPlayerTest extends AbstractVLCInternalTest
40 {
41
42     private LibVlcMediaListPlayer current;
43
44     @Test
45     public void mediaListPlayerNewTest()
46     {
47         
48         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
49         Assert.assertNotNull(mediaListPlayer);
50         libvlc.libvlc_media_list_player_release(mediaListPlayer);
51     }
52
53     @Test
54     public void mediaListPlayerSetMediaListTest()
55     {
56         
57         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
58         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance);
59         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList);
60         libvlc.libvlc_media_list_release(mediaList);
61         libvlc.libvlc_media_list_player_release(mediaListPlayer);
62     }
63
64     @Test
65     public void mediaListPlayerSetMediaListTest2()
66     {
67         
68         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
69         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance);
70         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new_location(libvlcInstance, mrl);
71         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor);
72         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList);
73         
74         libvlc.libvlc_media_release(mediaDescriptor);
75         libvlc.libvlc_media_list_release(mediaList);
76         libvlc.libvlc_media_list_player_release(mediaListPlayer);
77     }
78
79     @Test
80     public void mediaListPlayerIsNotPlayingTest()
81     {
82         
83         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
84         int result = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer);
85         Assert.assertEquals(LibVlcState.libvlc_NothingSpecial.ordinal(), result);
86         
87         libvlc.libvlc_media_list_player_release(mediaListPlayer);
88     }
89
90     @Test
91     public void mediaListPlayerPlayNoItemTest()
92     {
93         
94         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
95         libvlc.libvlc_media_list_player_play(mediaListPlayer);
96         libvlc.libvlc_media_list_player_release(mediaListPlayer);
97     }
98
99     @Test
100     public void mediaListPlayerPlay()
101     {
102         
103         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
104         current = mediaListPlayer;
105         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance);
106         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new_location(libvlcInstance, mrl);
107         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor);
108         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList);
109         libvlc.libvlc_media_list_player_play(mediaListPlayer);
110         
111         libvlc.libvlc_media_release(mediaDescriptor);
112         libvlc.libvlc_media_list_release(mediaList);
113     }
114
115     @Test
116     public void mediaListPlayerPlayItemAtIndex() throws Exception
117     {
118         
119         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
120         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance);
121         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new_location(libvlcInstance, mrl);
122         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor);
123         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList);
124         libvlc.libvlc_media_list_player_play_item_at_index(mediaListPlayer, 0);
125         while (true)
126         {
127             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer);
128             if (playing == 1)
129             {
130                 break;
131             }
132             Thread.sleep(150);
133         }
134         libvlc.libvlc_media_list_player_stop(mediaListPlayer);
135         while (libvlc.libvlc_media_list_player_get_state(mediaListPlayer) != LibVlcState.libvlc_Ended
136             .ordinal())
137         {
138             Thread.sleep(100);
139         }
140         libvlc.libvlc_media_release(mediaDescriptor);
141         libvlc.libvlc_media_list_release(mediaList);
142         libvlc.libvlc_media_list_player_release(mediaListPlayer);
143     }
144
145     @Test
146     public void mediaListPlayerPlayItem() throws Exception
147     {
148         
149         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
150         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance);
151         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new_location(libvlcInstance, mrl);
152         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor);
153         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList);
154         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor);
155         
156         while (true)
157         {
158             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer);
159             if (playing == 1)
160             {
161                 break;
162             }
163             Thread.sleep(150);
164         }
165         Thread.sleep(400);
166         libvlc.libvlc_media_list_player_stop(mediaListPlayer);
167         libvlc.libvlc_media_list_release(mediaList);
168         libvlc.libvlc_media_list_player_release(mediaListPlayer);
169     }
170
171     @Test
172     public void mediaListPlayerGetStateEnded()
173     {
174         
175         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
176         int state = libvlc.libvlc_media_list_player_get_state(mediaListPlayer);
177         Assert.assertEquals(LibVlcState.libvlc_Ended.ordinal(), state);
178         libvlc.libvlc_media_list_player_release(mediaListPlayer);
179     }
180
181     @Test
182     public void mediaLtistPlayerPause() throws Exception
183     {
184         
185         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
186         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance);
187         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new_location(libvlcInstance, mrl);
188         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor);
189         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList);
190         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor);
191         while (true)
192         {
193             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer);
194             if (playing == 1)
195             {
196                 break;
197             }
198             Thread.sleep(150);
199         }
200         libvlc.libvlc_media_list_player_pause(mediaListPlayer);
201
202         Thread.sleep(500);
203         
204         int state = libvlc.libvlc_media_list_player_get_state(mediaListPlayer);
205         
206         Thread.sleep(200L);
207         Assert.assertEquals(
208             "Expected state: " + LibVlcState.libvlc_Paused + ".\n",
209             LibVlcState.libvlc_Paused.ordinal(),
210             state);
211         libvlc.libvlc_media_list_player_stop(mediaListPlayer);
212         libvlc.libvlc_media_list_release(mediaList);
213         libvlc.libvlc_media_list_player_release(mediaListPlayer);
214     }
215
216     @Test
217     public void mediaListPlayerSetMediaInstance()
218     {
219         
220         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
221         LibVlcMedia md = libvlc.libvlc_media_new_location(libvlcInstance, mrl);
222         LibVlcMediaPlayer mi = libvlc.libvlc_media_player_new_from_media(md);
223         libvlc.libvlc_media_list_player_set_media_player(mediaListPlayer, mi);
224         
225     }
226
227     @Test
228     public void mediaListPlayerNextNoItems()
229     {
230         
231         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
232         libvlc.libvlc_media_list_player_next(mediaListPlayer);
233     }
234
235     /**
236      * fails, see https://trac.videolan.org/vlc/ticket/1535
237      */
238     // @Test
239     public void mediaListPlayerNext() throws Exception
240     {
241         
242         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
243         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance);
244         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new_location(libvlcInstance, mrl);
245         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor);
246         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor);
247         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList);
248         libvlc.libvlc_media_list_player_play_item_at_index(mediaListPlayer, 0);
249         Thread.sleep(150);
250         libvlc.libvlc_media_list_player_next(mediaListPlayer);
251         
252         libvlc.libvlc_media_list_release(mediaList);
253     }
254
255     @Test
256     public void mediaListPlayerIsPlaying() throws Exception
257     {
258         
259         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance);
260         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance);
261         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new_location(libvlcInstance, mrl);
262         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor);
263         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList);
264         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor);
265
266         while (true)
267         {
268             int playing = libvlc.libvlc_media_list_player_get_state(mediaListPlayer);
269             
270             if (playing == LibVlcState.libvlc_Playing.ordinal())
271             {
272                 break;
273             }
274             Thread.sleep(150);
275         }
276
277         libvlc.libvlc_media_list_player_stop(mediaListPlayer);
278         while (true)
279         {
280             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer);
281             
282             if (playing == 0)
283             {
284                 break;
285             }
286             Thread.sleep(150);
287         }
288         Assert.assertEquals(LibVlcState.libvlc_Ended.ordinal(), libvlc.libvlc_media_list_player_get_state(
289             mediaListPlayer));
290         libvlc.libvlc_media_list_release(mediaList);
291     }
292
293     @Override
294     @After
295     public void tearDown()
296     {
297         if (current != null)
298         {
299             libvlc.libvlc_media_list_player_stop(current);
300             while (libvlc.libvlc_media_list_player_get_state(current) != LibVlcState.libvlc_Ended.ordinal())
301             {
302                 try
303                 {
304                     Thread.sleep(100);
305                 }
306                 catch (InterruptedException e)
307                 {
308                     //
309                 }
310             }
311         }
312         current = null;
313         super.tearDown();
314     }
315
316 }