package device.radioPlayer; import org.assertj.core.util.Strings; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; import java.util.stream.Stream; import static org.assertj.core.api.Assertions.assertThat; class RadioPlayerTest { @ParameterizedTest @MethodSource("quieterLouderOptions") void quieterLouderTest(String testName, RadioPlayer testRp, int expectedResult) { int volume = testRp.getVolume(); assertThat(volume).describedAs(testName).isEqualTo(expectedResult); } static Stream quieterLouderOptions() { RadioPlayer rp1 = new RadioPlayer(); RadioPlayer rp2 = new RadioPlayer(); RadioPlayer rp3 = new RadioPlayer(); RadioPlayer rp4 = new RadioPlayer(); rp1.setLautstaerke(0); rp1.louder(); rp2.setLautstaerke(100); rp2.louder(); rp3.setLautstaerke(1); rp3.quieter(); rp4.setLautstaerke(0); rp4.quieter(); return Stream.of( Arguments.of("Test for one step louder", rp1, 1), Arguments.of("Test for max louder", rp2, 100), Arguments.of("Test for one step quieter", rp3, 0), Arguments.of("Test for min quieter", rp4, 0) ); } @ParameterizedTest @MethodSource("changeRegionOptions") void changeRegionTest(String testName, RadioPlayer testRp, String expectedResult) { String station = testRp.playedStation; assertThat(station).describedAs(testName).isEqualTo(expectedResult); } static Stream changeRegionOptions() { RadioPlayer rp1 = new RadioPlayer(); RadioPlayer rp2 = new RadioPlayer(); RadioPlayer rp3 = new RadioPlayer(); rp1.changeRegion("BY"); rp2.changeRegion("HE"); rp3.changeRegion("BW"); return Stream.of( Arguments.of("Test change Region to Bayern", rp1, "Antenne Bayern"), Arguments.of("Test change Region to Hessen", rp2, "Hit Radio FFH"), Arguments.of("Test change Region to Baden-Wuerttemberg", rp3, "DASDING") ); } @ParameterizedTest @MethodSource("prevNextOptions") void prevNextTest(String testName, RadioPlayer testRp, String expectedResult) { String station = testRp.playedStation; assertThat(station).describedAs(testName).isEqualTo(expectedResult); } static Stream prevNextOptions() { RadioPlayer rp1 = new RadioPlayer(); RadioPlayer rp2 = new RadioPlayer(); RadioPlayer rp3 = new RadioPlayer(); RadioPlayer rp4 = new RadioPlayer(); RadioPlayer rp5 = new RadioPlayer(); RadioPlayer rp6 = new RadioPlayer(); rp1.next(); rp2.next(); rp2.prev(); rp3.prev(); rp4.changeRegion("BY"); rp4.next(); rp5.changeRegion("BY"); rp5.prev(); rp6.changeRegion("BY"); rp6.saveStation(); rp6.next(); return Stream.of( Arguments.of("Next Station Test for saved Stations", rp1, "Teddy"), Arguments.of("Prev Station Test for saved Stations", rp2, "YouFM"), Arguments.of("Prev works on first Station for saved Stations", rp3, "MegaHits"), Arguments.of("changed Region and going to next station", rp4, "Bayern 1"), Arguments.of("changed Region and going to prev station", rp5, "Hit Radio N1"), Arguments.of("changed Region, saved station and going to next station", rp6, "YouFM") ); } @Test void YouFMInfoByTimeTest() { RadioPlayer rp = new RadioPlayer(); assertThat(rp.getYouFMInfoByTime(8)).describedAs("YouFM info by Time at 8.00").isEqualTo("YOUFM Good Morning Show"); } @Test void YouFMInfoByTime2Test() { RadioPlayer rp = new RadioPlayer(); assertThat(rp.getYouFMInfoByTime(3)).describedAs("YouFM info by Time at 3.00").isEqualTo("YOUFM Junge Nacht der ARD"); } @Test void AntenneBYInfoByTimeTest() { RadioPlayer rp = new RadioPlayer(); assertThat(rp.getAntenneBYInfoByTime(8)).describedAs("AntenneBY info by Time at 8.00").isEqualTo("ANTENNE BAYERN Guten Morgen Bayern"); } @Test void AntenneBYInfoByTime2Test() { RadioPlayer rp = new RadioPlayer(); assertThat(rp.getAntenneBYInfoByTime(3)).describedAs("AntenneBY info by Time at 3.00").isEqualTo("ANTENNE BAYERN Hit-Nacht"); } @Test void BR3InfoByTimeTest() { RadioPlayer rp = new RadioPlayer(); assertThat(rp.getBR3InfoByTime(5)).describedAs("BR3 info by Time at 5.00").isEqualTo("Sebastian Winkler und die Frühaufdreher"); } @Test void BR3InfoByTime2Test() { RadioPlayer rp = new RadioPlayer(); assertThat(rp.getBR3InfoByTime(21)).describedAs("BR3 info by Time at 21.00").isEqualTo("Matuschke - der etwas andere Abend"); } @ParameterizedTest @MethodSource("showStationInfoOptions") void showStationInfo(String testName, RadioPlayer testRp, String expectedResult) { String info = testRp.showStationInfo(); assertThat(info).describedAs(testName).isEqualTo(expectedResult); } static Stream showStationInfoOptions() { RadioPlayer rp1 = new RadioPlayer(); RadioPlayer rp2 = new RadioPlayer(); rp2.changeRegion("BY"); RadioPlayer rp3 = new RadioPlayer(); rp3.changeRegion("BY"); rp3.next(); rp3.next(); RadioPlayer rp4 = new RadioPlayer(); rp4.next(); return Stream.of( Arguments.of("Show Station Info for first saved Radio", rp1, rp1.getYouFMInfoByTime(rp1.hour)), Arguments.of("Show Station Info for regional Station", rp2, rp2.getAntenneBYInfoByTime(rp2.hour)), Arguments.of("Test for getting info text after station switching ", rp3, rp1.getBR3InfoByTime(rp3.hour)), Arguments.of("Show info text for a station without ", rp4, "") ); } /* @Test void getInfoText() { } @Test void getOptions() { } */ @ParameterizedTest @MethodSource("chooseItemOptions") void testChooseItem(String testName, RadioPlayer testRp, String expectedResult) { String playedStation = testRp.playedStation; assertThat(playedStation).describedAs(testName).isEqualTo(expectedResult); } static Stream chooseItemOptions() { RadioPlayer rp = new RadioPlayer(); rp.chooseItem(2); RadioPlayer rp1 = new RadioPlayer(); rp1.chooseItem(5); RadioPlayer rp2 = new RadioPlayer(); rp2.chooseItem(-1); RadioPlayer rp3 = new RadioPlayer(); rp3.changeRegion("BY"); rp3.chooseItem(2); RadioPlayer rp4 = new RadioPlayer(); rp4.changeRegion("BY"); rp4.chooseItem(5); RadioPlayer rp5 = new RadioPlayer(); rp5.changeRegion("BY"); rp5.chooseItem(-2); return Stream.of( Arguments.of("Test select station in saved playlist to play with nr isn`t bigger than playlist size", rp, "Teddy"), Arguments.of("Test select station in saved playlist to play with nr is bigger than playlist size. Last station in playlist gets played", rp1, "MegaHits"), Arguments.of("Test select station in saved playlist to play with nr is lower than 0. First station in playlist gets played", rp2, "YouFM"), Arguments.of("Test select station in regional playlist to play with nr isn`t bigger than playlist size", rp3, "Bayern 1"), Arguments.of("Test select station in regional playlist to play with nr is bigger than playlist size. Last station in playlist gets played", rp4, "Hit Radio N1"), Arguments.of("Test select station in regional playlist to play with nr is lower than 0. Last station in playlist gets played", rp5, "Antenne Bayern") ); } @ParameterizedTest @MethodSource("testPlayOptions") void testPlay(String testName, RadioPlayer testRp, String expectedResult) { String playedStation = testRp.play(); assertThat(playedStation).describedAs(testName).isEqualTo(expectedResult); } static Stream testPlayOptions() { RadioPlayer rp1 = new RadioPlayer(); RadioPlayer rp2 = new RadioPlayer(); rp2.changeRegion("BY"); return Stream.of( Arguments.of("Test for playing first station on saved playlist", rp1, "Radio is playing station: 01 YouFM from saved playlist"), Arguments.of("Test for playing first station on regional playlist", rp2, "Radio is playing station: 01 Antenne Bayern from regional playlist") ); } @ParameterizedTest @MethodSource("saveStationOptions") void testSaveStation(String testName, RadioPlayer testRp, String testTyp, String expectedResult) { String savedStation; if (testTyp.equals("normal")) { savedStation = testRp.savedPlaylist.get(testRp.savedPlaylist.size() - 1); } else savedStation = Boolean.toString(testRp.regionPlaylist.isEmpty()); assertThat(savedStation).describedAs(testName).isEqualTo(expectedResult); } static Stream saveStationOptions() { RadioPlayer rp1 = new RadioPlayer(); RadioPlayer rp2 = new RadioPlayer(); RadioPlayer rp3 = new RadioPlayer(); rp1.changeRegion("BY"); rp1.saveStation(); rp2.saveStation(); rp3.changeRegion("BY"); rp3.saveStation(); return Stream.of( Arguments.of("Test for saving station in saved playlist", rp1, "normal", "Antenne Bayern"), Arguments.of("Test for not saving station which is already in saved playlist", rp2, "normal", "MegaHits"), Arguments.of("Test for after saving station jump back to saved playlist", rp3, "jump", "true") ); } @ParameterizedTest @MethodSource("deleteStationOptions") void testDeleteStation(String testName, RadioPlayer testRp, String testTyp, Boolean expectedResult) { Boolean savedStation; if (testTyp.equals("delete")) { savedStation = testRp.savedPlaylist.contains("YouFM"); } else savedStation = testRp.savedPlaylist.contains("MegaHits"); assertThat(savedStation).describedAs(testName).isEqualTo(expectedResult); } static Stream deleteStationOptions() { RadioPlayer rp = new RadioPlayer(); rp.deleteStation(); RadioPlayer rp1 = new RadioPlayer(); rp1.deleteStation(); rp1.deleteStation(); rp1.deleteStation(); return Stream.of( Arguments.of("Test for delete station in saved playlist", rp, "delete", false), Arguments.of("Test for not delete station in saved playlist if this is the last station", rp1, "noDelete", true) ); } @ParameterizedTest @MethodSource("changeToSavedPlaylistOptions") void testChangeToSavedPlaylist(String testName, RadioPlayer testRp, String expectedResult) { String savedStation = testRp.playedStation; assertThat(savedStation).describedAs(testName).isEqualTo(expectedResult); } static Stream changeToSavedPlaylistOptions() { RadioPlayer rp = new RadioPlayer(); rp.changeToSavedPlaylist(); RadioPlayer rp1 = new RadioPlayer(); rp1.changeRegion("BY"); rp1.changeToSavedPlaylist(); return Stream.of( Arguments.of("Test for change to saved playlist when you already in saved playlist", rp, "YouFM"), Arguments.of("Test for change to saved playlist when in regional playlist", rp1, "YouFM") ); } @ParameterizedTest @MethodSource("changeOrderInSavedPlaylistOptions") void testChangeOrderInSavedPlaylist(String testName, RadioPlayer testRp, int nr, String expectedResult) { String savedStation = testRp.savedPlaylist.get(nr); assertThat(savedStation).describedAs(testName).isEqualTo(expectedResult); } static Stream changeOrderInSavedPlaylistOptions() { RadioPlayer rp = new RadioPlayer(); rp.next(); rp.changeOrderInSavedPlaylist(1); RadioPlayer rp1 = new RadioPlayer(); rp1.changeOrderInSavedPlaylist(5); RadioPlayer rp2 = new RadioPlayer(); rp2.next(); rp2.changeOrderInSavedPlaylist(-1); return Stream.of( Arguments.of("Test for change order in saved playlist with nr isn`t bigger than playlist size", rp, 0, "Teddy"), Arguments.of("Test for change order in saved playlist with nr is than bigger playlist size put at the end of playlist", rp1, 2, "YouFM"), Arguments.of("Test for change order in saved playlist with nr is than smaller than 0 at front of playlist", rp2, 0, "Teddy") ); } @ParameterizedTest @MethodSource("getItemListOptions") void testGetItemList(String testName, RadioPlayer testRp, String testTyp, String[] expectedResult) { if (testTyp.equals("region")) { testRp.changeRegion("BY"); } String[] playList = testRp.getItemList(); assertThat(playList).describedAs(testName).isEqualTo(expectedResult); } static Stream getItemListOptions() { RadioPlayer rp = new RadioPlayer(); RadioPlayer rp1 = new RadioPlayer(); rp1.changeRegion("BY"); return Stream.of( Arguments.of("Test for return saved playlist", rp, "saved", rp.savedPlaylist.toArray(new String[0])), Arguments.of("Test for return regional playlist", rp1, "region", rp1.regionPlaylist.toArray(new String[0])) ); } @ParameterizedTest @MethodSource("muteOptions") void testMute(String testName, String testTyp, RadioPlayer testRp, int expectedResult) { int volume; if (testTyp.equals("actual")) volume = testRp.getVolume(); else volume = testRp.savedVolume; assertThat(volume).describedAs(testName).isEqualTo(expectedResult); } static Stream muteOptions() { RadioPlayer rp = new RadioPlayer(); rp.setLautstaerke(25); rp.mute(); RadioPlayer rp1 = new RadioPlayer(); rp1.setLautstaerke(30); rp1.mute(); return Stream.of( Arguments.of("Test for mute RadioPlayer if volume is actually 0", "actual", rp, 0), Arguments.of("Test for mute RadioPlayer if volume is saved before muting", "saved", rp1, 30) ); } @Test void TestUnmute() { RadioPlayer rp = new RadioPlayer(); rp.setLautstaerke(40); rp.mute(); rp.unmute(); assertThat(rp.getVolume()).describedAs("Test if unmute is setting the saved volume").isEqualTo(rp.savedVolume); } @ParameterizedTest @MethodSource("getInfoTextOptions") void testGetInfoText(String testName, String testTyp, RadioPlayer testRp, boolean expectedResult) { Boolean bool; if(testTyp.equals("null")) bool = Strings.isNullOrEmpty(testRp.getInfoText()); else if(testTyp.contains("station")) bool = testRp.getInfoText().contains(testRp.playedStation); else if(testTyp.contains("region")) bool = testRp.getInfoText().contains("Regional playlist"); else if(testTyp.contains("saved")) bool = testRp.getInfoText().contains("Saved playlist"); else if(testTyp.contains("savedNum")) bool = testRp.getInfoText().contains(("0") + testRp.savedPlaylist.indexOf(testRp.playedStation) + 1); else if(testTyp.contains("regionNum")) bool = testRp.getInfoText().contains(("0") + testRp.regionPlaylist.indexOf(testRp.playedStation) + 1); else if(testTyp.contains("stationInfo")) bool = testRp.getInfoText().contains("Now playing"); else bool = testRp.getInfoText().contains("Now playing"); assertThat(bool).describedAs(testName).isEqualTo(expectedResult); } static Stream getInfoTextOptions() { RadioPlayer rp = new RadioPlayer(); RadioPlayer rp1 = new RadioPlayer(); RadioPlayer rp2 = new RadioPlayer(); rp2.changeRegion("BY"); RadioPlayer rp3 = new RadioPlayer(); RadioPlayer rp4 = new RadioPlayer(); rp4.next(); RadioPlayer rp5 = new RadioPlayer(); rp5.changeRegion("HE"); rp5.next(); RadioPlayer rp6 = new RadioPlayer(); rp6.changeRegion("BY"); RadioPlayer rp7 = new RadioPlayer(); rp7.next(); return Stream.of( Arguments.of("Test for getInfoText is not empty or null", "null", rp, false), Arguments.of("Test for getInfoText contains played station", "station", rp1, true), Arguments.of("Test for getInfoText contains regional playlist if played station is in it", "region", rp2, true), Arguments.of("Test for getInfoText contains saved playlist if played station is in it", "saved", rp3, true), Arguments.of("Test for getInfoText contains station number in saved playlist", "savedNum", rp4, true), Arguments.of("Test for getInfoText contains station number in regional playlist", "regionNum", rp5, true), Arguments.of("Test for getInfoText contains station info if station has one", "stationInfo", rp6, true), Arguments.of("Test for getInfoText not contains station info if station not has one", "noStationInfo", rp7, false) ); } }