package de.fojadrachi.playtube import org.json.JSONObject import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Test class PcRemoteTest { @Test fun parsesDiscoveryReply() { val device = PcRemote.parseDiscoveryReply( """{"service": "playtube", "protocol": 1, "name": "MEIN-PC", "port": 47811}""", "192.168.1.20", ) assertEquals(PcDevice("MEIN-PC", "192.168.1.20", 47811), device) } @Test fun ignoresForeignOrBrokenDiscoveryReplies() { assertNull(PcRemote.parseDiscoveryReply("""{"service": "other", "port": 1}""", "1.2.3.4")) assertNull(PcRemote.parseDiscoveryReply("""{"service": "playtube", "port": 70000}""", "1.2.3.4")) assertNull(PcRemote.parseDiscoveryReply("kein json", "1.2.3.4")) } @Test fun usesHostWhenNameIsMissing() { val device = PcRemote.parseDiscoveryReply("""{"service": "playtube"}""", "10.0.0.2") assertEquals(PcDevice("10.0.0.2", "10.0.0.2", PcRemote.DEFAULT_PORT), device) } @Test fun parsesStateOfTheActiveTab() { val reply = JSONObject( """ {"id": 3, "ok": true, "state": { "protocol": 1, "visibleTab": "youtube", "activeTab": "music", "tabs": { "youtube": null, "music": {"title": "Song", "subtitle": "Artist", "thumbnail": "https://i.ytimg.com/x.jpg", "playing": true, "currentTime": 61.5, "duration": 200, "volume": 42, "likeStatus": "LIKE"} }}} """.trimIndent(), ) val state = PcRemote.parseState(reply)!! assertEquals("music", state.activeTab) assertEquals("youtube", state.visibleTab) assertEquals("Song", state.title) assertEquals("Artist", state.subtitle) assertEquals("https://i.ytimg.com/x.jpg", state.thumbnail) assertTrue(state.playing) assertEquals(61.5, state.position, 0.001) assertEquals(200.0, state.duration, 0.001) assertEquals(42, state.volume) assertTrue(state.liked) } @Test fun handlesEmptyTabsAndNulls() { val reply = JSONObject( """{"ok": true, "state": {"activeTab": "youtube", "tabs": {"youtube": {"title": null, "thumbnail": "http://unsicher", "currentTime": null}, "music": null}}}""", ) val state = PcRemote.parseState(reply)!! assertEquals("", state.title) assertEquals("", state.thumbnail) assertEquals(0.0, state.position, 0.001) assertFalse(state.playing) assertNull(PcRemote.parseState(JSONObject("""{"ok": true}"""))) } @Test fun formatsTimes() { assertEquals("0:00", PcRemote.formatTime(-5.0)) assertEquals("1:05", PcRemote.formatTime(65.9)) assertEquals("1:01:01", PcRemote.formatTime(3661.0)) } }