1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertFalse;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 |
|
---|
7 | import java.io.File;
|
---|
8 | import java.net.MalformedURLException;
|
---|
9 | import java.net.URL;
|
---|
10 |
|
---|
11 | import org.junit.BeforeClass;
|
---|
12 | import org.junit.Test;
|
---|
13 | import org.openstreetmap.josm.JOSMFixture;
|
---|
14 | import org.openstreetmap.josm.TestUtils;
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Unit tests of {@link AudioPlayer} class.
|
---|
18 | */
|
---|
19 | public class AudioPlayerTest {
|
---|
20 |
|
---|
21 | // We play wav files of about 4 seconds + pause, so define timeout at 12 seconds
|
---|
22 | private static final long MAX_DURATION = 12000;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Setup test.
|
---|
26 | */
|
---|
27 | @BeforeClass
|
---|
28 | public static void setUp() {
|
---|
29 | JOSMFixture.createUnitTestFixture().init();
|
---|
30 | }
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Test method for {@code AudioPlayer#play(URL)}
|
---|
34 | * @throws Exception audio fault exception, e.g. can't open stream, unhandleable audio format
|
---|
35 | * @throws MalformedURLException wrong URL
|
---|
36 | */
|
---|
37 | @Test(timeout = 4*MAX_DURATION)
|
---|
38 | public void testPlay() throws MalformedURLException, Exception {
|
---|
39 | File wav1 = new File(TestUtils.getRegressionDataFile(6851, "20111003_121226.wav"));
|
---|
40 | File wav2 = new File(TestUtils.getRegressionDataFile(6851, "20111003_121557.wav"));
|
---|
41 |
|
---|
42 | for (File w : new File[] {wav1, wav2}) {
|
---|
43 | System.out.println("Playing " + w.toPath());
|
---|
44 | URL url = w.toURI().toURL();
|
---|
45 | long start = System.currentTimeMillis();
|
---|
46 | AudioPlayer.play(url);
|
---|
47 | assertTrue(AudioPlayer.playing());
|
---|
48 | assertFalse(AudioPlayer.paused());
|
---|
49 | AudioPlayer.pause();
|
---|
50 | assertFalse(AudioPlayer.playing());
|
---|
51 | assertTrue(AudioPlayer.paused());
|
---|
52 | AudioPlayer.play(url, AudioPlayer.position());
|
---|
53 | while (AudioPlayer.playing() && (System.currentTimeMillis() - start) < MAX_DURATION) {
|
---|
54 | Thread.sleep(500);
|
---|
55 | }
|
---|
56 | long duration = System.currentTimeMillis() - start;
|
---|
57 | System.out.println("Play finished after " + Utils.getDurationString(duration));
|
---|
58 | assertTrue(duration < MAX_DURATION);
|
---|
59 | AudioPlayer.reset();
|
---|
60 | }
|
---|
61 | }
|
---|
62 | }
|
---|