From 7993f1481ed8d5503c4a7e16c27987e49b633a19 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Tue, 22 Mar 2016 17:04:42 +0000 Subject: [PATCH] Added partial test suite for GStreamerPlayer. --- src/main/java/musicplayer/model/Song.java | 21 +++++ .../musicplayer/player/GStreamerPlayer.java | 10 ++- .../player/GStreamerPlayerTest.java | 83 +++++++++++++++++++ 3 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 src/test/java/musicplayer/player/GStreamerPlayerTest.java diff --git a/src/main/java/musicplayer/model/Song.java b/src/main/java/musicplayer/model/Song.java index 4f492e1..8810ccf 100644 --- a/src/main/java/musicplayer/model/Song.java +++ b/src/main/java/musicplayer/model/Song.java @@ -40,6 +40,27 @@ public class Song implements Comparable, IDBType { protected Song() { } + /** + * Create a song from ExtractedMetadata. + * This will always create new model objects (Album, Artist, etc). Use the verbose constructor if you need + * proper relationship behaviour between Songs and other model classes. + * @param metadata Song metadata. + */ + public Song(ExtractedMetadata metadata){ + this(metadata.getTrackNumber(), metadata.getDiscNumber(), metadata.getTitle(), new Artist(metadata.getArtist()), + new Album(metadata.getAlbum()), metadata.getGenre(), metadata.getSongFile()); + } + + /** + * Representation of a music file. + * @param trackNumber Track number. + * @param discNumber Disc number. + * @param title Song title. + * @param artist Song artist. + * @param album Song album. + * @param genre Song genre. + * @param songFile String representation of file path. + */ public Song(String trackNumber, String discNumber, String title, Artist artist, Album album, String genre, String songFile) { updateData(trackNumber, discNumber, title, artist, album, genre, songFile); } diff --git a/src/main/java/musicplayer/player/GStreamerPlayer.java b/src/main/java/musicplayer/player/GStreamerPlayer.java index df9d145..8968091 100644 --- a/src/main/java/musicplayer/player/GStreamerPlayer.java +++ b/src/main/java/musicplayer/player/GStreamerPlayer.java @@ -28,15 +28,21 @@ public class GStreamerPlayer implements IPlayer{ /** * Manages GStreamer based playback operations. * @param callbackInterface Interface on which UI updates can be called. + * @param playlist Playlist containing music to play. */ @Inject - public GStreamerPlayer(PlayerCallbackInterface callbackInterface, IPlaylist playlist) { + public GStreamerPlayer(PlayerCallbackInterface callbackInterface, IPlaylist playlist){ + this(callbackInterface, playlist, false); + } + + public GStreamerPlayer(PlayerCallbackInterface callbackInterface, IPlaylist playlist, boolean testMode) { this.callbackInterface = callbackInterface; this.playlist = playlist; Gst.init(); playBin = new PlayBin2("BusMessages"); playBin.setVideoSink(ElementFactory.make("fakesink", "videosink")); - + if(testMode) // Null output for testing + playBin.setAudioSink(ElementFactory.make("fakesink", "audiosink")); playBin.connect((PlayBin2.ABOUT_TO_FINISH) playBin2 -> { Thread callbackThing = new Thread(() -> { try { diff --git a/src/test/java/musicplayer/player/GStreamerPlayerTest.java b/src/test/java/musicplayer/player/GStreamerPlayerTest.java new file mode 100644 index 0000000..396c433 --- /dev/null +++ b/src/test/java/musicplayer/player/GStreamerPlayerTest.java @@ -0,0 +1,83 @@ +package musicplayer.player; + +import musicplayer.model.Song; +import musicplayer.playlist.IPlaylist; +import musicplayer.playlist.JTablePlaylist; +import musicplayer.util.LibraryUtils; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.util.Optional; + +import static org.junit.Assert.*; + +public class GStreamerPlayerTest { + + GStreamerPlayer player; + IPlaylist playlist; + Song sampleSong = new Song(LibraryUtils.autoParse( + new File(GStreamerPlayerTest.class.getResource("/sample.mp3").getFile()).toPath()).get()); + + @Before + public void setUp() throws Exception { + playlist = new JTablePlaylist(); + player = new GStreamerPlayer(null, playlist, true); + } + + @Test + public void testPlayEmptyPlaylist() throws Exception { + player.play(); + assertFalse(player.isPlaying()); + } + + @Test + public void testPlay() throws Exception { + playlist.addSong(sampleSong); + player.play(); + assertTrue(player.isPlaying()); + } + + @Test + public void testPlaySong() throws Exception { + player.playSong(Optional.of(sampleSong)); + assertTrue(player.isPlaying()); + assertEquals(sampleSong, player.getCurrentSong()); + } + + @Test + public void testStop() throws Exception { + player.playSong(Optional.of(sampleSong)); + assertTrue(player.isPlaying()); + player.stop(); + assertFalse(player.isPlaying()); + } + + @Test + public void testResume() throws Exception { + } + + @Test + public void testPause() throws Exception { + player.playSong(Optional.of(sampleSong)); + assertTrue(player.isPlaying()); + player.pause(); + assertFalse(player.isPlaying()); + } + + @Test + public void testSetVolume() throws Exception { + assertEquals(100, player.getVolume()); + int newVolume = 25; + player.setVolume(25); + assertEquals(newVolume, player.getVolume()); + } + + @Test + public void testSeek() throws Exception { + player.playSong(Optional.of(sampleSong)); + assertTrue(player.isPlaying()); + player.seek(100); + assertTrue(player.currentSongPosition() >= 100); + } +} \ No newline at end of file