chiark / gitweb /
Merge audio timing fix branch.
[disorder] / tests / play.py
CommitLineData
abf54aca 1#! /usr/bin/env python
f5eb2aff
RK
2#
3# This file is part of DisOrder.
2b2a5fed 4# Copyright (C) 2007, 2008 Richard Kettlewell
f5eb2aff 5#
e7eb3a27 6# This program is free software: you can redistribute it and/or modify
f5eb2aff 7# it under the terms of the GNU General Public License as published by
e7eb3a27 8# the Free Software Foundation, either version 3 of the License, or
f5eb2aff
RK
9# (at your option) any later version.
10#
e7eb3a27
RK
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
f5eb2aff 16# You should have received a copy of the GNU General Public License
e7eb3a27 17# along with this program. If not, see <http://www.gnu.org/licenses/>.
f5eb2aff 18#
05438e89 19import dtest,time,disorder,re,sys
f5eb2aff
RK
20
21def test():
22 """Play some tracks"""
23 dtest.start_daemon()
f0feb22e 24 dtest.create_user()
56613a97 25 dtest.rescan() # ensure all files are scanned
f5eb2aff 26 c = disorder.client()
2b2a5fed
RK
27 c.random_disable()
28 assert c.random_enabled() == False
f5eb2aff 29 track = u"%s/Joe Bloggs/First Album/02:Second track.ogg" % dtest.tracks
f0feb22e 30 print " adding track to queue"
db86b8ed 31 c.disable()
2b2a5fed 32 assert c.enabled() == False
f5eb2aff 33 c.play(track)
81e440ce 34 print " checking track turned up in queue"
f5eb2aff
RK
35 q = c.queue()
36 ts = filter(lambda t: t['track'] == track and 'submitter' in t, q)
6ce6b5a9 37 assert len(ts) == 1, "checking track appears exactly once in queue"
f5eb2aff
RK
38 t = ts[0]
39 assert t['submitter'] == u'fred', "check queue submitter"
40 i = t['id']
81e440ce 41 print " waiting for track"
db86b8ed 42 c.enable()
2b2a5fed 43 assert c.enabled() == True
f5eb2aff 44 p = c.playing()
546c7978 45 r = c.recent()
bf219db9 46 limit = 60
546c7978 47 while not((p is not None and p['id'] == i)
bf219db9 48 or (len(filter(lambda t: t['track'] == track
49 and 'submitter' in t, r)) > 0)) and limit > 0:
f5eb2aff
RK
50 time.sleep(1)
51 p = c.playing()
546c7978 52 r = c.recent()
bf219db9 53 limit -= 1
54 assert limit > 0, "check track did complete in a reasonable time"
81e440ce 55 print " checking track turned up in recent list"
6ce6b5a9
RK
56 while (p is not None and p['id'] == i):
57 time.sleep(1)
58 p = c.playing()
59 r = c.recent()
60 ts = filter(lambda t: t['track'] == track and 'submitter' in t, r)
61 assert len(ts) == 1, "check track appears exactly once in recent"
f5eb2aff
RK
62 t = ts[0]
63 assert t['submitter'] == u'fred', "check recent entry submitter"
2b2a5fed
RK
64
65 print " testing scratches"
66 retry = False
05438e89
RK
67 scratchlimit = 5
68 while scratchlimit > 0:
69 scratchlimit -= 1
2b2a5fed
RK
70 c.disable()
71 print " starting a track"
72 c.play(track)
73 c.enable()
81e440ce 74 p = c.playing()
2b2a5fed
RK
75 if p is None:
76 print " track played too quickly, trying again..."
77 continue
78 print " scratching track"
79 i = p['id']
80 c.scratch(i)
81 print " waiting for track to finish"
82 p = c.playing()
bf219db9 83 limit = 60
84 while (p is not None and p['id'] == i) and limit > 0:
2b2a5fed
RK
85 time.sleep(1)
86 p = c.playing()
bf219db9 87 limit -= 1
88 assert limit > 0, "check track finishes in a reasonable period"
2b2a5fed
RK
89 print " checking scratched track turned up in recent list"
90 r = c.recent()
91 ts = filter(lambda t: t['id'] == i, r)
92 assert len(ts) == 1, "check scratched track appears exactly once in recent"
93 if ts[0]['state'] == 'ok':
94 print " track played too quickly, trying again..."
95 continue
96 assert ts[0]['state'] == 'scratched', "checking track scratched"
97 break
05438e89
RK
98 if scratchlimit == 0:
99 # TODO this is really not a great approach!
100 print " didn't complete in a reasonable time"
101 sys.exit(77)
be603af9 102 print " waiting for scratch to complete"
2b2a5fed
RK
103 p = c.recent()
104 while p is not None:
be603af9
RK
105 time.sleep(1)
106 p = c.playing()
107 assert p is None, "checking nothing is playing"
af371dd1 108 assert c.enabled() == True
af371dd1
RK
109 c.random_enable()
110 assert c.random_enabled() == True
f5eb2aff
RK
111
112if __name__ == '__main__':
113 dtest.run()