]> git.sesse.net Git - vlc/blob - bindings/java/core/src/test/java/org/videolan/jvlc/internal/LibVlcMediaListPlayerTest.java
31dec733ef9b912ce843401d5bf906674a1f4d58
[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.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.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.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.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.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("Exception message: " + exception.message + ".\n", 0, exception.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.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.raised);
162         while (true)
163         {
164             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
165             if (exception.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.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         int state = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
217         Assert.assertEquals(exception.message, 0, exception.raised);
218         Thread.sleep(200L);
219         Assert.assertEquals(
220             "Expected state: " + LibVlcState.libvlc_Paused + ".\n",
221             LibVlcState.libvlc_Paused.ordinal(),
222             state);
223         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
224         libvlc.libvlc_media_list_release(mediaList);
225         libvlc.libvlc_media_list_player_release(mediaListPlayer);
226     }
227
228     @Test
229     public void mediaListPlayerSetMediaInstance()
230     {
231         libvlc_exception_t exception = new libvlc_exception_t();
232         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
233         LibVlcMedia md = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
234         LibVlcMediaPlayer mi = libvlc.libvlc_media_player_new_from_media(md, exception);
235         libvlc.libvlc_media_list_player_set_media_player(mediaListPlayer, mi, exception);
236         Assert.assertEquals(0, exception.raised);
237     }
238
239     @Test
240     public void mediaListPlayerNextNoItems()
241     {
242         libvlc_exception_t exception = new libvlc_exception_t();
243         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
244         libvlc.libvlc_media_list_player_next(mediaListPlayer, exception);
245         Assert.assertEquals(1, exception.raised);
246     }
247
248     /**
249      * fails, see https://trac.videolan.org/vlc/ticket/1535
250      */
251     // @Test
252     public void mediaListPlayerNext() throws Exception
253     {
254         libvlc_exception_t exception = new libvlc_exception_t();
255         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
256         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
257         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
258         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
259         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
260         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
261         libvlc.libvlc_media_list_player_play_item_at_index(mediaListPlayer, 0, exception);
262         Thread.sleep(150);
263         libvlc.libvlc_media_list_player_next(mediaListPlayer, exception);
264         Assert.assertEquals(0, exception.raised);
265         libvlc.libvlc_media_list_release(mediaList);
266     }
267
268     @Test
269     public void mediaListPlayerIsPlaying() throws Exception
270     {
271         libvlc_exception_t exception = new libvlc_exception_t();
272         LibVlcMediaListPlayer mediaListPlayer = libvlc.libvlc_media_list_player_new(libvlcInstance, exception);
273         LibVlcMediaList mediaList = libvlc.libvlc_media_list_new(libvlcInstance, exception);
274         LibVlcMedia mediaDescriptor = libvlc.libvlc_media_new(libvlcInstance, mrl, exception);
275         libvlc.libvlc_media_list_add_media(mediaList, mediaDescriptor, exception);
276         libvlc.libvlc_media_list_player_set_media_list(mediaListPlayer, mediaList, exception);
277         libvlc.libvlc_media_list_player_play_item(mediaListPlayer, mediaDescriptor, exception);
278
279         while (true)
280         {
281             int playing = libvlc.libvlc_media_list_player_get_state(mediaListPlayer, exception);
282             Assert.assertEquals(0, exception.raised);
283             if (playing == LibVlcState.libvlc_Playing.ordinal())
284             {
285                 break;
286             }
287             Thread.sleep(150);
288         }
289
290         libvlc.libvlc_media_list_player_stop(mediaListPlayer, exception);
291         while (true)
292         {
293             int playing = libvlc.libvlc_media_list_player_is_playing(mediaListPlayer, exception);
294             Assert.assertEquals(0, exception.raised);
295             if (playing == 0)
296             {
297                 break;
298             }
299             Thread.sleep(150);
300         }
301         Assert.assertEquals(LibVlcState.libvlc_Ended.ordinal(), libvlc.libvlc_media_list_player_get_state(
302             mediaListPlayer,
303             exception));
304         libvlc.libvlc_media_list_release(mediaList);
305     }
306
307     @Override
308     @After
309     public void tearDown()
310     {
311         if (current != null)
312         {
313             libvlc.libvlc_media_list_player_stop(current, exception);
314             while (libvlc.libvlc_media_list_player_get_state(current, exception) != LibVlcState.libvlc_Ended.ordinal())
315             {
316                 try
317                 {
318                     Thread.sleep(100);
319                 }
320                 catch (InterruptedException e)
321                 {
322                     //
323                 }
324             }
325         }
326         current = null;
327         super.tearDown();
328     }
329
330 }