From f29be6beeecdbf69a0c51827589194b023c114d1 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Tue, 22 Mar 2016 14:47:43 +0000 Subject: [PATCH] Fixed behaviour when stopping playback but the song is no longer in the playlist. --- .../java/musicplayer/playlist/JTablePlaylist.java | 6 +++--- .../musicplayer/playlist/JTablePlaylistTest.java | 12 +++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/musicplayer/playlist/JTablePlaylist.java b/src/main/java/musicplayer/playlist/JTablePlaylist.java index cf03fb4..3f87cf4 100644 --- a/src/main/java/musicplayer/playlist/JTablePlaylist.java +++ b/src/main/java/musicplayer/playlist/JTablePlaylist.java @@ -116,8 +116,8 @@ public class JTablePlaylist extends JScrollPane implements IPlaylist { public void setPlayingSong(Song song){ final int index = getIndex(song); playlistTableModel.setPlayingRow(index); - if(index >= 0) - SwingUtilities.invokeLater(() -> playList.setRowSelectionInterval(index, index)); + if(index >= 0 && index < playList.getRowCount()) + playList.setRowSelectionInterval(index, index); } @Override @@ -125,7 +125,7 @@ public class JTablePlaylist extends JScrollPane implements IPlaylist { if(getActive().isPresent()) { int index = getIndex(getActive().get()); playlistTableModel.setPlayingRow(-1); - SwingUtilities.invokeLater(() -> playList.setRowSelectionInterval(index, index)); + playList.setRowSelectionInterval(index, index); } else playlistTableModel.setPlayingRow(-1); diff --git a/src/test/java/musicplayer/playlist/JTablePlaylistTest.java b/src/test/java/musicplayer/playlist/JTablePlaylistTest.java index d9cfab3..ec61154 100644 --- a/src/test/java/musicplayer/playlist/JTablePlaylistTest.java +++ b/src/test/java/musicplayer/playlist/JTablePlaylistTest.java @@ -117,7 +117,6 @@ public class JTablePlaylistTest { playlist.addSong(testSong); playlist.setPlayingSong(testSong); assertEquals(testSong, playlist.getActive().get()); - Thread.sleep(10); // Wait for Swing thread update propagation assertEquals(playlist.getIndex(testSong), innerTable.getSelectedRow()); } @@ -139,6 +138,17 @@ public class JTablePlaylistTest { assertEquals(playlist.getIndex(testSong), innerTable.getSelectedRow()); } + @Test + public void testSetStoppedButSongRemovedFromPlaylist() throws Exception { + Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", ""); + playlist.addSong(testSong); + playlist.setPlayingSong(testSong); + playlist.delete(testSong); + playlist.setStopped(); + assertEquals(0, playlist.getSongList().size()); + assertEquals(-1, innerTable.getSelectedRow()); + } + @Test public void testGetSongList() throws Exception { Song testSong = new Song("1", "1", "test 1", new Artist("test artist"), new Album("test album"), "test genre", "");