def test_error(self): self.current = self.p.song = self.s1 self.p._error('Test error') run_gtk_loop() self.assertEquals(self.s1["~#playcount"], 0) self.assertEquals(self.s1["~#skipcount"], 0) self.failUnless(self.s1["~#lastplayed"], 10)
def test_restore(self): config.set("browsers", "query_text", "foo") self.bar.restore() self.failUnlessEqual(self.bar._get_text(), "foo") self.bar.finalize(True) run_gtk_loop() self.failUnlessEqual(self.emit_count, 0)
def test_main(self): cancel = Cancellable() data = [] def func(): data.append(threading.current_thread().name) def callback(result): data.append(threading.current_thread().name) call_async(func, cancel, callback) Gtk.main_iteration() run_gtk_loop() call_async_background(func, cancel, callback) Gtk.main_iteration() run_gtk_loop() main_name = threading.current_thread().name self.assertEqual(len(data), 4) self.assertNotEqual(data[0], main_name) self.assertEqual(data[1], main_name) self.assertNotEqual(data[2], main_name) self.assertEqual(data[3], main_name)
def test_search(self): manager = CoverManager(use_built_in=False) handler = manager.plugin_handler for source in dummy_sources: handler.plugin_handle(source) handler.plugin_enable(source) source.cls.cover_call = False source.cls.fetch_call = False song = AudioFile({ "~filename": os.path.join("/tmp/asong.ogg"), "album": "Abbey Road", "artist": "The Beatles" }) songs = [song] results = [] def done(manager, provider, result): self.failUnless(result, msg="Shouldn't succeed with no results") results.append(result) def finished(manager, songs): print("Finished!") manager.connect('covers-found', done) manager.search_cover(Cancellable(), songs) manager.connect('searches-complete', finished) run_gtk_loop() self.failUnlessEqual(len(results), 1)
def setUp(self): self.mod = self.modules["mpd_server"] config.init() init_fake_app() MPDServerPlugin = self.mod.MPDServerPlugin MPDConnection = self.mod.main.MPDConnection MPDService = self.mod.main.MPDService class Server: def __init__(self) -> None: super().__init__() self.service = MPDService(app, MPDServerPlugin()) def _remove_connection(self, conn): pass server = Server() s, c = socket.socketpair() self.s = s c.setblocking(False) s.settimeout(1) self.conn = MPDConnection(server, c) self.conn.handle_init(server) run_gtk_loop() self.s.recv(9999)
def test_toggle(self): config.set("memory", "bar", "on") c = ConfigCheckMenuItem("dummy", "memory", "bar") c.set_active(True) self.failUnless(config.getboolean("memory", "bar") and c.get_active()) c.set_active(False) run_gtk_loop() self.failIf(config.getboolean("memory", "bar") or c.get_active())
def test_restore_entry_text(self): self.bar.filter_text("foobar") self.bar.save() self.bar._set_text("nope") self.bar.restore() self.failUnlessEqual(self.bar._get_text(), "foobar") run_gtk_loop() self.failUnlessEqual(self.emit_count, 1)
def test_changed(self): self.lib1.add(self.Frange(12)) self.lib2.add(self.Frange(12, 24)) self.librarian.changed(self.Frange(6, 18)) run_gtk_loop() self.failUnlessEqual(sorted(self.changed), self.Frange(6, 18)) self.failUnlessEqual(self.changed_1, self.Frange(6, 12)) self.failUnlessEqual(self.changed_2, self.Frange(12, 18))
def test_current_song_changed(self): self.p.song = self.song self.song["title"] = "new!" self.lib.changed([self.song]) run_gtk_loop() with open(self.filename, "rb") as h: contents = h.read() assert b"title=new!\n" in contents
def test_elapsed(self): self.p.seek(123456) self.p.emit('song-started', AudioFile({"~#length": 10})) run_gtk_loop() with open(self.filename, "rb") as h: contents = h.read() assert b"~#elapsed=123.456" in contents assert b"~elapsed=2:03\n" in contents
def test_rename(self): song = self.Fake(10) self.library.add([song]) self.library.rename(song, 20) run_gtk_loop() self.failUnless(song in self.changed) self.failUnless(song in self.library) self.failUnless(song.key in self.library) self.failUnlessEqual(song.key, 20)
def test_restore_selection(self): self.bar.activate() self.bar.filter("artist", [u"piman"]) self.bar.save() self.bar.unfilter() self.bar.restore() self.bar.activate() run_gtk_loop() for song in self.last: self.assertTrue(u"piman" in song.list("artist"))
def test_filter_text(self): self.bar.activate() self.bar.filter_text("artist=nope") run_gtk_loop() self.failUnlessEqual(set(self.last), set()) self.bar.filter_text("artist=!boris") run_gtk_loop() self.failUnlessEqual(set(self.last), set(SONGS[1:]))
def test_populate(self): # Assert that active state works config.set("memory", "bar", "on") c = ConfigCheckMenuItem("dummy", "memory", "bar", populate=True) run_gtk_loop() self.failUnless(c.get_active()) # ...and inactive config.set("memory", "bar", "off") c = ConfigCheckMenuItem("dummy", "memory", "bar", populate=True) run_gtk_loop() self.failIf(c.get_active())
def test_watched_adding(self): with temp_filename(dir=self.temp_path, suffix=".mp3", as_path=True) as path: shutil.copy(Path(get_data_path("silence-44-s.mp3")), path) assert self.temp_path in path.parents, "Copied test file incorrectly" watch_dirs = self.library._monitors.keys() assert path.parent in watch_dirs, "Not monitoring directory of new file" run_gtk_loop() assert self.library, f"Nothing in library despite watches on {watch_dirs}" assert str(path) in self.library, (f"{path!s} should have been added to " f"library [{self.fns}]") assert str(path) in {af("~filename") for af in self.added}
def retry_queue(self): """Allows GTK looks, flushing of disk buffers etc""" queue = None start = time() while time() - start < 2: run_gtk_loop() queue = self.load_queue() if queue: break sleep(0.1) return queue
def test_cancel(self): def func(): assert 0 def callback(result): assert 0 cancel = Cancellable() cancel.cancel() call_async(func, cancel, callback) Gtk.main_iteration() run_gtk_loop()
def test_remove_songs(self): song = AudioFile({"~filename": "/dev/null"}) song.sanitize() self.lib.add([song]) assert song in self.lib, "Broken library?" self.songlist.add_songs([song]) assert set(self.songlist.get_songs()) == {song} self.lib.remove([song]) assert not list(self.lib), "Didn't get removed" run_gtk_loop() assert self.songs_removed == [{song} ], f"Signal not emitted: {self.__sigs}"
def test_play(self): import time # Allow at least 2 second to elapse to simulate playing self.p.song = self.s1 self.p.paused = False time.sleep(2) run_gtk_loop() self.p.emit('song-ended', self.s1, False) run_gtk_loop() t = time.time() self.assertEquals(self.s1["~#playcount"], 1) self.assertEquals(self.s1["~#skipcount"], 0) self.failUnless(t - self.s1["~#lastplayed"] <= 1)
def setUp(self): self.plugin = self.plugins["mpris"].cls config.init() init_fake_app() run_gtk_loop() app.window.songlist.set_songs([A1, A2]) app.player.go_to(None) self.m = self.plugin() self.m.enabled() self._replies = []
def test_download_tags(self): self.received = [] # TODO: parameterise this, spin up a local HTTP server, inject this. url = STATION_LIST_URL def cb(data): assert data self.received += data ret = list(download_taglist(cb, None)) run_gtk_loop() assert all(ret) assert self.received, "No stations received from %s" % url assert len(self.received) > 100
def test_rename(self): new = self.Fake(10) new.key = 30 self.lib1.add([new]) self.lib2.add([new]) self.librarian.rename(new, 20) run_gtk_loop() self.failUnlessEqual(new.key, 20) self.failUnless(new in self.lib1) self.failUnless(new in self.lib2) self.failUnless(new.key in self.lib1) self.failUnless(new.key in self.lib2) self.failUnlessEqual(self.changed_1, [new]) self.failUnlessEqual(self.changed_2, [new]) self.failUnless(new in self.changed)
def test_move(self, temp_dir: Path): monitor = BasicMonitor(temp_dir) with temp_filename(dir=temp_dir, suffix=".txt", as_path=True) as path: path.write_text("test\n") sleep(SLEEP_SECS) run_gtk_loop() assert monitor.changed, "No events after creation" monitor.changed.clear() new_name = f"new-{time()}.txt" path.rename(path.parent / new_name) sleep(SLEEP_SECS) run_gtk_loop() assert monitor.changed assert monitor.event_types >= {Event.RENAMED }, f"Got {monitor.changed}"
def test_watched_adding_removing(self): with temp_filename(dir=self.temp_path, suffix=".mp3", as_path=True) as path: shutil.copy(Path(get_data_path("silence-44-s.mp3")), path) sleep(0.5) run_gtk_loop() assert path.exists() assert str(path) in self.library, f"{path} should be in [{self.fns}] now" assert not path.exists(), "Failed to delete test file" sleep(0.5) # Deletion now run_gtk_loop() assert self.removed, "Nothing was automatically removed" assert self.added, "Nothing was automatically added" assert {Path(af("~filename")) for af in self.added} == {path} assert {Path(af("~filename")) for af in self.removed} == {path} assert str(path) not in self.library, f"{path} shouldn't be in the library now"
def test_create_delete(self, temp_dir: Path): path = temp_dir monitor = BasicMonitor(path) some_file = (path / "foo.txt") some_file.write_text("test") sleep(SLEEP_SECS) run_gtk_loop() assert monitor.changed, "No events after creation" # assert monitor.event_types >= {EventType.CHANGED, EventType.CREATED} assert monitor.event_types >= {Event.CREATED} monitor.changed.clear() some_file.unlink() sleep(SLEEP_SECS) run_gtk_loop() assert monitor.changed, "No events after deletion" assert monitor.event_types >= {Event.DELETED}
def test_acquire_cover_calls(self): # * fetch_cover shouldn't get called if source provides the cover # synchronously # * First cover source should fail providing the cover both # synchronously and asynchronously and only then the next source # should be used manager = CoverManager(use_built_in=False) handler = manager.plugin_handler found = [] result = [] for source in dummy_sources: handler.plugin_handle(source) handler.plugin_enable(source) source.cls.cover_call = False source.cls.fetch_call = False def done(_found, _result): found.append(_found) result.append(_result) manager.acquire_cover(done, None, None) run_gtk_loop() self.assertTrue(found[0]) self.assertIs(result[0], DUMMY_COVER) self.assertTrue(dummy_sources[0].cls.cover_call) self.assertTrue(dummy_sources[1].cls.cover_call) self.assertFalse(dummy_sources[2].cls.cover_call) self.assertFalse(dummy_sources[0].cls.fetch_call) self.assertFalse(dummy_sources[1].cls.fetch_call) self.assertFalse(dummy_sources[2].cls.fetch_call) for source in dummy_sources: source.cls.cover_call = False source.cls.fetch_call = False handler.plugin_disable(dummy_sources[1]) manager.acquire_cover(done, None, None) run_gtk_loop() self.assertTrue(found[1]) self.assertIs(result[1], DUMMY_COVER) self.assertTrue(dummy_sources[0].cls.cover_call) self.assertFalse(dummy_sources[1].cls.cover_call) self.assertTrue(dummy_sources[2].cls.cover_call) self.assertFalse(dummy_sources[0].cls.fetch_call) self.assertFalse(dummy_sources[1].cls.fetch_call) self.assertTrue(dummy_sources[2].cls.fetch_call)
def test_watched_moving_dir(self): temp_dir = self.temp_path / "old" temp_dir.mkdir(exist_ok=False) sleep(0.2) run_gtk_loop() assert temp_dir in self.library._monitors with temp_filename(dir=temp_dir, suffix=".flac", as_path=True) as path: shutil.copy(Path(get_data_path("silence-44-s.flac")), path) sleep(0.2) assert path.exists() run_gtk_loop() assert str(path) in self.library, f"New path {path!s} didn't get added" assert len(self.added) == 1 self.added.clear() assert self.library # Now move the directory... new_dir = path.parent.parent / "new" temp_dir.rename(new_dir) assert new_dir.is_dir(), "test should have moved to new dir" sleep(0.2) run_gtk_loop() new_path = new_dir / path.name assert new_path.is_file() msg = f"New path {new_path} not in library [{self.fns}]. Did move_root run?" assert str(new_path) in self.library, msg assert not self.removed, "A file was removed"
def test_acquire_cover(self): manager = CoverManager(use_built_in=False) handler = manager.plugin_handler for source in dummy_sources: handler.plugin_handle(source) handler.plugin_enable(dummy_sources[0]) found = [] result = [] def done(_found, _result): found.append(_found) result.append(_result) manager.acquire_cover(done, None, None) run_gtk_loop() self.assertFalse(found[0]) handler.plugin_enable(dummy_sources[1]) manager.acquire_cover(done, None, None) run_gtk_loop() self.assertTrue(found[1]) self.assertIs(result[1], DUMMY_COVER) handler.plugin_disable(dummy_sources[1]) handler.plugin_enable(dummy_sources[2]) manager.acquire_cover(done, None, None) run_gtk_loop() self.assertTrue(found[2]) self.assertIs(result[2], DUMMY_COVER)
def test_watched_moving_song(self): with temp_filename(dir=self.temp_path, suffix=".flac", as_path=True) as path: shutil.copy(Path(get_data_path("silence-44-s.flac")), path) sleep(0.2) assert path.exists() run_gtk_loop() assert str(path) in self.library, f"New path {path!s} didn't get added" assert len(self.added) == 1 assert self.added[0]("~basename") == path.name self.added.clear() # Now move it... new_path = path.parent / f"moved-{path.name}" path.rename(new_path) sleep(0.2) assert not path.exists(), "test should have removed old file" assert new_path.exists(), "test should have renamed file" print_d(f"New test file at {new_path}") run_gtk_loop() p = normalize_path(str(new_path), True) assert p in self.library, f"New path {new_path} not in library [{self.fns}]" msg = "Inconsistent events: should be (added and removed) or nothing at all" assert not (bool(self.added) ^ bool(self.removed)), msg
def test_restart(self): self.current = self.s1 self.p.emit('song-ended', self.s1, True) run_gtk_loop() self.assertEquals(self.s1["~#playcount"], 0) self.assertEquals(self.s1["~#skipcount"], 0)