1# This program is free software; you can redistribute it and/or modify 2# it under the terms of the GNU General Public License as published by 3# the Free Software Foundation; either version 2 of the License, or 4# (at your option) any later version. 5 6import os 7import shutil 8 9from tests import TestCase, mkdtemp 10 11from gi.repository import Gtk 12 13from quodlibet import config 14from quodlibet.formats import AudioFile 15from quodlibet.player.nullbe import NullPlayer 16from quodlibet.qltk.tracker import SongTracker, FSInterface 17from quodlibet.library import SongLibrary 18 19 20class TSongTracker(TestCase): 21 def setUp(self): 22 config.init() 23 self.p = NullPlayer() 24 self.w = SongLibrary() 25 self.s1 = AudioFile( 26 {"~#playcount": 0, "~#skipcount": 0, "~#lastplayed": 10, 27 "~filename": "foo", "~#length": 1.5}) 28 self.s2 = AudioFile( 29 {"~#playcount": 0, "~#skipcount": 0, "~#lastplayed": 10, 30 "~filename": "foo", "~#length": 1.5}) 31 self.cm = SongTracker(self.w, self.p, self) 32 self.current = None 33 34 def do(self): 35 while Gtk.events_pending(): 36 Gtk.main_iteration() 37 38 def test_destroy(self): 39 self.cm.destroy() 40 41 def test_play(self): 42 import time 43 # Allow at least 2 second to elapse to simulate playing 44 self.p.song = self.s1 45 self.p.paused = False 46 time.sleep(2) 47 self.do() 48 self.p.emit('song-ended', self.s1, False) 49 self.do() 50 t = time.time() 51 self.assertEquals(self.s1["~#playcount"], 1) 52 self.assertEquals(self.s1["~#skipcount"], 0) 53 self.failUnless(t - self.s1["~#lastplayed"] <= 1) 54 55 def test_skip(self): 56 self.p.emit('song-ended', self.s1, True) 57 self.do() 58 self.assertEquals(self.s1["~#playcount"], 0) 59 self.assertEquals(self.s1["~#skipcount"], 1) 60 self.failUnless(self.s1["~#lastplayed"], 10) 61 62 def test_error(self): 63 self.current = self.p.song = self.s1 64 self.p._error('Test error') 65 self.do() 66 self.assertEquals(self.s1["~#playcount"], 0) 67 self.assertEquals(self.s1["~#skipcount"], 0) 68 self.failUnless(self.s1["~#lastplayed"], 10) 69 70 def test_restart(self): 71 self.current = self.s1 72 self.p.emit('song-ended', self.s1, True) 73 self.do() 74 self.assertEquals(self.s1["~#playcount"], 0) 75 self.assertEquals(self.s1["~#skipcount"], 0) 76 77 def tearDown(self): 78 self.w.destroy() 79 config.quit() 80 81 82class TFSInterface(TestCase): 83 def setUp(self): 84 self.p = NullPlayer() 85 self.dir = mkdtemp() 86 self.filename = os.path.join(self.dir, "foo") 87 self.fs = FSInterface(self.filename, self.p) 88 89 def tearDown(self): 90 self.p.destroy() 91 shutil.rmtree(self.dir) 92 93 def do(self): 94 while Gtk.events_pending(): 95 Gtk.main_iteration() 96 97 def test_init(self): 98 self.do() 99 self.failIf(os.path.exists(self.filename)) 100 101 def test_start(self): 102 self.p.emit('song_started', AudioFile({"woo": "bar", "~#length": 10})) 103 self.do() 104 with open(self.filename, "rb") as h: 105 self.failUnless(b"woo=bar\n" in h.read()) 106 107 def test_song_ended(self): 108 self.p.emit('song-started', AudioFile({"woo": "bar", "~#length": 10})) 109 self.do() 110 self.p.emit('song-ended', {}, False) 111 self.do() 112 self.failIf(os.path.exists(self.filename)) 113