Skip to content

Commit 8b63904

Browse files
Unit tests for all or most public methods in StreamPlayer.
The test are written mainly as documentation of existing behavior, to give usage examples, and maybe to reveal a bug or two. Most tests are just placeholders, not fully implemented. All current tests focus on the happy path, there is no test of how the methods fail in the expected way.
1 parent ea4b7d9 commit 8b63904

File tree

1 file changed

+349
-0
lines changed

1 file changed

+349
-0
lines changed
Lines changed: 349 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,349 @@
1+
package com.goxr3plus.streamplayer.stream;
2+
3+
import com.goxr3plus.streamplayer.enums.Status;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import javax.sound.sampled.SourceDataLine;
8+
import java.io.File;
9+
import java.util.logging.Logger;
10+
11+
import static org.junit.jupiter.api.Assertions.*;
12+
import static org.mockito.Mockito.mock;
13+
14+
/**
15+
* Tests of all or most of the public methods of StreamPlayer.
16+
* These unit tests are written primarily as documentation of the behavior and as example use case,
17+
* not as a part of test driven development.
18+
*/
19+
public class StreamPlayerMethodsTest {
20+
21+
StreamPlayer player;
22+
private File audioFile;
23+
24+
@BeforeEach
25+
void setup() {
26+
final Logger logger = mock(Logger.class);
27+
player = new StreamPlayer(logger);
28+
audioFile = new File("Logic - Ballin [Bass Boosted].mp3");
29+
}
30+
31+
@Test
32+
void balance() throws StreamPlayerException {
33+
// Setup
34+
final float wantedBalance = 0.5f;
35+
36+
//Exercise
37+
player.open(audioFile);
38+
player.play(); // Necessary to be able to set the balance
39+
40+
final float initialBalance = player.getBalance();
41+
player.setBalance(wantedBalance);
42+
player.stop(); // Probably not needed, but cleanup is good.
43+
final float actualBalance = player.getBalance(); // Can be made before or after stop()
44+
45+
// Verify
46+
assertEquals(0, initialBalance);
47+
assertEquals(wantedBalance, actualBalance);
48+
}
49+
50+
@Test
51+
void status() throws StreamPlayerException {
52+
// Setup
53+
final File audioFile = new File("Logic - Ballin [Bass Boosted].mp3");
54+
55+
// Exercise
56+
final Status initialStatus = player.getStatus();
57+
58+
player.open(audioFile);
59+
final Status statusAfterOpen = player.getStatus();
60+
61+
player.stop();
62+
final Status statusAfterFirstStop = player.getStatus();
63+
64+
player.play();
65+
final Status statusAfterPlay = player.getStatus();
66+
67+
player.pause();
68+
final Status statusAfterPause = player.getStatus();
69+
70+
player.seekTo(40);
71+
final Status statusAfterSeeking = player.getStatus();
72+
73+
player.stop();
74+
final Status statusAfterSecondStop = player.getStatus();
75+
76+
// Verify
77+
assertEquals(Status.NOT_SPECIFIED, initialStatus);
78+
assertEquals(Status.OPENED, statusAfterOpen);
79+
assertEquals(Status.STOPPED, statusAfterFirstStop);
80+
assertEquals(Status.PLAYING, statusAfterPlay);
81+
assertEquals(Status.PAUSED, statusAfterPause);
82+
assertEquals(Status.PAUSED, statusAfterSeeking); // Still paused (or paused again)
83+
assertEquals(Status.STOPPED, statusAfterSecondStop);
84+
}
85+
86+
@Test
87+
void gain() throws StreamPlayerException, InterruptedException {
88+
// Setup
89+
final double gain1 = 0.99;
90+
final double gain2 = 0.2;
91+
final double delta = 0.05;
92+
final boolean listen = false;
93+
94+
// Exercise
95+
final float initialGain = player.getGainValue();
96+
player.open(audioFile);
97+
player.seekTo(30);
98+
player.play();
99+
player.setGain(gain1);
100+
final float actualGain1First = player.getGainValue();
101+
if (listen) Thread.sleep(2000);
102+
final float actualGain1 = player.getGainValue();
103+
104+
player.setGain(gain2);
105+
if (listen) Thread.sleep(2000);
106+
final float actualGain2 = player.getGainValue();
107+
108+
player.setGain(gain1);
109+
if (listen) Thread.sleep(2000);
110+
111+
player.stop();
112+
113+
// Verify
114+
assertEquals(0, initialGain);
115+
assertEquals(actualGain1First, actualGain1);
116+
assertEquals(gain1, actualGain1, delta); // TODO: Investigate probable bug.
117+
// fail("Test not done");
118+
}
119+
120+
@Test
121+
void totalBytes() {
122+
player.getTotalBytes();
123+
124+
fail("Test not done");
125+
}
126+
127+
@Test
128+
void stopped() {
129+
player.isStopped();
130+
131+
fail("Test not done");
132+
}
133+
134+
@Test
135+
void sourceDataLine() {
136+
final SourceDataLine sourceDataLine = player.getSourceDataLine();
137+
138+
assertNotNull(sourceDataLine);
139+
140+
fail("Test not done");
141+
}
142+
143+
@Test
144+
void playing() {
145+
final boolean playing = player.isPlaying();
146+
147+
assertFalse(playing);
148+
149+
fail("Test not done");
150+
}
151+
152+
@Test
153+
void pausedOrPlaying() {
154+
player.isPausedOrPlaying();
155+
156+
fail("Test not done");
157+
}
158+
159+
@Test
160+
void paused() {
161+
player.isPaused();
162+
163+
fail("Test not done");
164+
}
165+
166+
@Test
167+
void addStreamPlayerListener_dontAcceptNull() {
168+
assertThrows(Exception.class, () -> player.addStreamPlayerListener(null));
169+
170+
fail("Test not done");
171+
}
172+
173+
@Test
174+
void addStreamPlayerListener() {
175+
final StreamPlayerListener listener = mock(StreamPlayerListener.class);
176+
player.addStreamPlayerListener(listener);
177+
178+
fail("Test not done"); // TODO: CHeck that the listener is actually added
179+
}
180+
181+
@Test
182+
void mute() {
183+
player.getMute();
184+
player.setMute(false);
185+
186+
fail("Test not done");
187+
}
188+
189+
@Test
190+
void speedFactor() {
191+
player.getSpeedFactor();
192+
player.setSpeedFactor(1000);
193+
194+
fail("Test not done");
195+
}
196+
197+
@Test
198+
void equalizer() {
199+
player.setEqualizer(null, 0);
200+
201+
fail("Test not done");
202+
}
203+
204+
@Test
205+
void play() throws StreamPlayerException {
206+
player.play();
207+
208+
fail("Test not done");
209+
}
210+
211+
@Test
212+
void resume() {
213+
player.resume();
214+
215+
fail("Test not done");
216+
}
217+
218+
@Test
219+
void pause() {
220+
player.pause();
221+
222+
fail("Test not done");
223+
}
224+
225+
@Test
226+
void stop() {
227+
player.stop();
228+
229+
fail("Test not done");
230+
}
231+
232+
@Test
233+
void pan() {
234+
player.getPan();
235+
player.setPan(1000);
236+
237+
fail("Test not done");
238+
}
239+
240+
@Test
241+
void unknown() {
242+
player.isUnknown();
243+
244+
fail("Test not done");
245+
}
246+
247+
@Test
248+
void open() throws StreamPlayerException {
249+
player.open(null);
250+
251+
fail("Test not done");
252+
}
253+
254+
@Test
255+
void mixers() {
256+
player.getMixers();
257+
258+
fail("Test not done");
259+
}
260+
261+
@Test
262+
void seekBytes() throws StreamPlayerException {
263+
player.seekBytes(0);
264+
265+
fail("Test not done");
266+
}
267+
268+
269+
// The methods tested below aren't used otherwhere in this project, nor in XR3Player
270+
271+
@Test
272+
void lineBufferSize() {
273+
player.getLineBufferSize();
274+
player.setLineBufferSize(0);
275+
fail("Test not done");
276+
}
277+
278+
@Test
279+
void lineCurrentBufferSize() {
280+
player.getLineCurrentBufferSize();
281+
282+
fail("Test not done");
283+
}
284+
285+
@Test
286+
void maximumGain() {
287+
player.getMaximumGain();
288+
289+
fail("Test not done");
290+
}
291+
292+
@Test
293+
void minimumGain() {
294+
player.getMinimumGain();
295+
296+
fail("Test not done");
297+
}
298+
299+
@Test
300+
void positionByte() {
301+
player.getPositionByte();
302+
303+
fail("Test not done");
304+
}
305+
306+
@Test
307+
void precision() {
308+
player.getPrecision();
309+
310+
fail("Test not done");
311+
}
312+
313+
@Test
314+
void opened() {
315+
player.isOpened();
316+
317+
fail("Test not done");
318+
}
319+
320+
@Test
321+
void seeking() {
322+
player.isSeeking();
323+
324+
fail("Test not done");
325+
}
326+
327+
@Test
328+
void removeStreamPlayerListener() {
329+
player.removeStreamPlayerListener(null);
330+
331+
fail("Test not done");
332+
}
333+
334+
@Test
335+
void seekTo() throws StreamPlayerException {
336+
player.seekTo(1000);
337+
338+
fail("Test not done");
339+
}
340+
341+
@Test
342+
void equalizerKey() {
343+
player.setEqualizerKey(0, 0);
344+
345+
fail("Test not done");
346+
}
347+
348+
349+
}

0 commit comments

Comments
 (0)