]> git.sesse.net Git - vlc/blob - bindings/java/core/src/test/java/org/videolan/jvlc/internal/LibVlcMediaListPlayerTest.java
jvlc: honour asynchronousness of the libvlc_media_list_player_pause native method
[vlc] / bindings / java / 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         libvlc_exception_t exception = new libvlc_exception_t();
48         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
49         Assert.assertNotNull(mediaListPlayer);
50         Assert.assertEquals(0, exception.b_raised);
51         libvlc.libvlc_media_list_player_release(mediaListPlayer);
52     }
53
54     @Test
55     public void mediaListPlayerSetMediaListTest()
56     {
57         libvlc_exception_t exception = new libvlc_exception_t();
58         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
59         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
60         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
61         Assert.assertEquals(0, exception.b_raised);
62         libvlc.libvlc_media_list_release(mediaList);
63         libvlc.libvlc_media_list_player_release(mediaListPlayer);
64     }
65
66     @Test
67     public void mediaListPlayerSetMediaListTest2()
68     {
69         libvlc_exception_t exception = new libvlc_exception_t();
70         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
71         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
72         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
73         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
74         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
75         Assert.assertEquals(0, exception.b_raised);
76         libvlc.libvlc_media_list_release(mediaList);
77         libvlc.libvlc_media_list_player_release(mediaListPlayer);
78     }
79
80     @Test
81     public void mediaListPlayerIsNotPlayingTest()
82     {
83         libvlc_exception_t exception = new libvlc_exception_t();
84         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
85         int result = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
86         Assert.assertEquals(0, result);
87         Assert.assertEquals(0, exception.b_raised);
88         libvlc.libvlc_media_list_player_release(mediaListPlayer);
89     }
90
91     @Test
92     public void mediaListPlayerPlayNoItemTest()
93     {
94         libvlc_exception_t exception = new libvlc_exception_t();
95         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
96         libvlc.libvlc_media_list_player_play(mediaListPlayer, exception);
97         Assert.assertEquals(1, exception.b_raised);
98         libvlc.libvlc_media_list_player_release(mediaListPlayer);
99     }
100
101     @Test
102     public void mediaListPlayerPlay()
103     {
104         libvlc_exception_t exception = new libvlc_exception_t();
105         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
106         current = mediaListPlayer;
107         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
108         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
109         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
110         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
111         libvlc.libvlc_media_list_player_play(mediaListPlayer, exception);
112         Assert.assertEquals(0, exception.b_raised);
113         libvlc.libvlc_media_release(mediaDescriptor);
114         libvlc.libvlc_media_list_release(mediaList);
115     }
116
117     @Test
118     public void mediaListPlayerPlayItemAtIndex() throws Exception
119     {
120         libvlc_exception_t exception = new libvlc_exception_t();
121         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
122         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
123         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
124         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
125         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
126         libvlc.libvlc_media_list_player_play_item_at_index(mediaListPlayer, 0, exception);
127         while (true)
128         {
129             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
130             if (exception.b_raised == 1)
131             {
132                 throw new RuntimeException("Native exception thrown");
133             }
134             if (playing == 1)
135             {
136                 break;
137             }
138             Thread.sleep(150);
139         }
140         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
141         while (libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception) != LibVlcState.libvlc_Ended
142             .ordinal())
143         {
144             Thread.sleep(100);
145         }
146         libvlc.libvlc_media_release(mediaDescriptor);
147         libvlc.libvlc_media_list_release(mediaList);
148         libvlc.libvlc_media_list_player_release(mediaListPlayer);
149     }
150
151     @Test
152     public void mediaListPlayerPlayItem() throws Exception
153     {
154         libvlc_exception_t exception = new libvlc_exception_t();
155         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
156         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
157         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
158         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
159         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
160         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
161         Assert.assertEquals(0, exception.b_raised);
162         while (true)
163         {
164             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
165             if (exception.b_raised == 1)
166             {
167                 throw new RuntimeException("Native exception thrown");
168             }
169             if (playing == 1)
170             {
171                 break;
172             }
173             Thread.sleep(150);
174         }
175         Thread.sleep(400);
176         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
177         libvlc.libvlc_media_list_release(mediaList);
178         libvlc.libvlc_media_list_player_release(mediaListPlayer);
179     }
180
181     @Test
182     public void mediaListPlayerGetStateEnded()
183     {
184         libvlc_exception_t exception = new libvlc_exception_t();
185         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
186         int state = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
187         Assert.assertEquals(LibVlcState.libvlc_Ended.ordinal(), state);
188         libvlc.libvlc_media_list_player_release(mediaListPlayer);
189     }
190
191     @Test
192     public void mediaLtistPlayerPause() throws Exception
193     {
194         libvlc_exception_t exception = new libvlc_exception_t();
195         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
196         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
197         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
198         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
199         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
200         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
201         while (true)
202         {
203             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
204             if (exception.b_raised == 1)
205             {
206                 throw new RuntimeException("Native exception thrown");
207             }
208             if (playing == 1)
209             {
210                 break;
211             }
212             Thread.sleep(150);
213         }
214         libvlc.libvlc_media_list_player_pause(mediaListPlayer, exception);
215
216         Thread.sleep(500);
217         
218         int state = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
219         Assert.assertEquals(0, exception.b_raised);
220         Thread.sleep(200L);
221         Assert.assertEquals(
222             "Expected state: " + LibVlcState.libvlc_Paused + ".\n",
223             LibVlcState.libvlc_Paused.ordinal(),
224             state);
225         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
226         libvlc.libvlc_media_list_release(mediaList);
227         libvlc.libvlc_media_list_player_release(mediaListPlayer);
228     }
229
230     @Test
231     public void mediaListPlayerSetMediaInstance()
232     {
233         libvlc_exception_t exception = new libvlc_exception_t();
234         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
235         LibVlcMedia md = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
236         LibVlcMediaPlayer mi = libvlc.libvlc_media_player_new_from_media(md, exception);
237         libvlc.libvlc_media_list_player_set_media_player(mediaListPlayer, mi, exception);
238         Assert.assertEquals(0, exception.b_raised);
239     }
240
241     @Test
242     public void mediaListPlayerNextNoItems()
243     {
244         libvlc_exception_t exception = new libvlc_exception_t();
245         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
246         libvlc.libvlc_media_list_player_next(mediaListPlayer, exception);
247         Assert.assertEquals(1, exception.b_raised);
248     }
249
250     /**
251      * fails, see https://trac.videolan.org/vlc/ticket/1535
252      */
253     // @Test
254     public void mediaListPlayerNext() throws Exception
255     {
256         libvlc_exception_t exception = new libvlc_exception_t();
257         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
258         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
259         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
260         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
261         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
262         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
263         libvlc.libvlc_media_list_player_play_item_at_index(mediaListPlayer, 0, exception);
264         Thread.sleep(150);
265         libvlc.libvlc_media_list_player_next(mediaListPlayer, exception);
266         Assert.assertEquals(0, exception.b_raised);
267         libvlc.libvlc_media_list_release(mediaList);
268     }
269
270     @Test
271     public void mediaListPlayerIsPlaying() throws Exception
272     {
273         libvlc_exception_t exception = new libvlc_exception_t();
274         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
275         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
276         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
277         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
278         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
279         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
280
281         while (true)
282         {
283             int playing = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
284             Assert.assertEquals(0, exception.b_raised);
285             if (playing == LibVlcState.libvlc_Playing.ordinal())
286             {
287                 break;
288             }
289             Thread.sleep(150);
290         }
291
292         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
293         while (true)
294         {
295             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
296             Assert.assertEquals(0, exception.b_raised);
297             if (playing == 0)
298             {
299                 break;
300             }
301             Thread.sleep(150);
302         }
303         Assert.assertEquals(LibVlcState.libvlc_Ended.ordinal(), libvlc.libvlc_media_list_player_get_state(
304             mediaListPlayer,
305             exception));
306         libvlc.libvlc_media_list_release(mediaList);
307     }
308
309     @Override
310     @After
311     public void tearDown()
312     {
313         if (current != null)
314         {
315             libvlc.libvlc_media_list_player_stop(current, exception);
316             while (libvlc.libvlc_media_list_player_get_state(current, exception) != LibVlcState.libvlc_Ended.ordinal())
317             {
318                 try
319                 {
320                     Thread.sleep(100);
321                 }
322                 catch (InterruptedException e)
323                 {
324                     //
325                 }
326             }
327         }
328         current = null;
329         super.tearDown();
330     }
331
332 }