~andrewsomething/exaile/karmic

« back to all changes in this revision

Viewing changes to plugins/mpris/test.py

  • Committer: Aren Olson
  • Date: 2009-09-12 00:36:59 UTC
  • Revision ID: reacocard@gmail.com-20090912003659-w373sg0n04uoa8op
remove useless files, add soem of the fixes from lp bug 420019

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009 Abhishek Mukherjee
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 3, or (at your option)
6
 
# any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 
"""
17
 
Simple test case for MPRIS. Make sure you set the global variable FILE with an
18
 
absolute path to a valid playable, local music piece before running this test
19
 
 
20
 
@warning: DO not run this with an old unpatched Notifications plugin enabled.
21
 
Your Galago daemon will get DoS'd. It is normal for your songs to randomly
22
 
start over and over
23
 
"""
24
 
import unittest
25
 
import dbus
26
 
import os
27
 
import time
28
 
 
29
 
OBJECT_NAME = 'org.mpris.exaile'
30
 
INTERFACE = 'org.freedesktop.MediaPlayer'
31
 
FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
32
 
                    os.path.pardir, os.path.pardir,
33
 
                    'tests', 'data', 'music', 'delerium', 'chimera',
34
 
                    '05 - Truly.mp3')
35
 
assert os.path.isfile(FILE), FILE + " must be a valid musical piece"
36
 
FILE = "file://" + FILE
37
 
 
38
 
bus = dbus.SessionBus()
39
 
 
40
 
interface = None
41
 
 
42
 
try:
43
 
    object = bus.get_object(OBJECT_NAME, '/')
44
 
    interface = dbus.Interface(object, INTERFACE)
45
 
except:
46
 
    pass
47
 
 
48
 
# only run mpris tests if the dbus object is available
49
 
if interface:
50
 
    class TestExaileMpris(unittest.TestCase):
51
 
 
52
 
        """
53
 
            Tests Exaile MPRIS plugin
54
 
        """
55
 
 
56
 
        def setUp(self):
57
 
            """
58
 
                Simple setUp that makes dbus connections and assigns them to
59
 
                self.player, self.track_list, and self.root. Also begins playing
60
 
                a song so every test case can assume that a song is playing
61
 
            """
62
 
            bus = dbus.SessionBus()
63
 
            objects = {'root': '/',
64
 
                    'player': '/Player',
65
 
                    'track_list': '/TrackList',
66
 
                    }
67
 
            intfs = {}
68
 
            for key in objects:
69
 
                object = bus.get_object(OBJECT_NAME, objects[key])
70
 
                intfs[key] = dbus.Interface(object, INTERFACE)
71
 
            self.root = intfs['root']
72
 
            self.player = intfs['player']
73
 
            self.track_list = intfs['track_list']
74
 
            self.player.Play()
75
 
            time.sleep(0.5)
76
 
 
77
 
        def __wait_after(function):
78
 
            """
79
 
                Decorator to add a delay after a function call
80
 
            """
81
 
            def inner(*args, **kwargs):
82
 
                function(*args, **kwargs)
83
 
                time.sleep(0.5)
84
 
            return inner
85
 
 
86
 
        @__wait_after
87
 
        def _stop(self):
88
 
            """
89
 
                Stops playing w/ delay
90
 
            """
91
 
            self.player.Stop()
92
 
 
93
 
        @__wait_after
94
 
        def _play(self):
95
 
            """
96
 
                Starts playing w/ delay
97
 
            """
98
 
            self.player.Play()
99
 
 
100
 
        @__wait_after
101
 
        def _pause(self):
102
 
            """
103
 
                Pauses playing w/ delay
104
 
            """
105
 
            self.player.Pause()
106
 
 
107
 
 
108
 
    class TestMprisRoot(TestExaileMpris):
109
 
 
110
 
        """
111
 
            Check / (Root) object functions for MPRIS. Does not check Quit
112
 
        """
113
 
 
114
 
        def testIdentity(self):
115
 
            """
116
 
                Make sure we output Exaile with our identity
117
 
            """
118
 
            id = self.root.Identity()
119
 
            self.assertEqual(id, self.root.Identity())
120
 
            self.assertTrue(id.startswith("Exaile"))
121
 
 
122
 
        def testMprisVersion(self):
123
 
            """
124
 
                Checks that we are using MPRIS version 1.0
125
 
            """
126
 
            version = self.root.MprisVersion()
127
 
            self.assertEqual(dbus.UInt16(1), version[0])
128
 
            self.assertEqual(dbus.UInt16(0), version[1])
129
 
 
130
 
    class TestTrackList(TestExaileMpris):
131
 
 
132
 
        """
133
 
            Tests the /TrackList object for MPRIS
134
 
        """
135
 
 
136
 
        def testGetMetadata(self):
137
 
            """
138
 
                Make sure we can get metadata. Also makes sure that locations will
139
 
                not change randomly
140
 
            """
141
 
            md = self.track_list.GetMetadata(0)
142
 
            md_2 = self.track_list.GetMetadata(0)
143
 
            self.assertEqual(md, md_2)
144
 
 
145
 
        def testAppendDelWithoutPlay(self):
146
 
            """
147
 
                Tests appending and deleting songs from the playlist without
148
 
                playing them
149
 
            """
150
 
            cur_track = self.track_list.GetCurrentTrack()
151
 
            len = self.track_list.GetLength()
152
 
 
153
 
            self.assertEqual(0, self.track_list.AddTrack(FILE, False))
154
 
            self.assertEqual(len + 1, self.track_list.GetLength())
155
 
            self.assertEqual(cur_track, self.track_list.GetCurrentTrack())
156
 
 
157
 
            md = self.track_list.GetMetadata(len)
158
 
            self.assertEqual(FILE, md['location'])
159
 
 
160
 
            self.track_list.DelTrack(len)
161
 
            self.assertEqual(len, self.track_list.GetLength())
162
 
            self.assertEqual(cur_track, self.track_list.GetCurrentTrack())
163
 
 
164
 
        def testAppendDelWithPlay(self):
165
 
            """
166
 
                Tests appending songs into the playlist with playing the songs
167
 
            """
168
 
            cur_track = self.track_list.GetCurrentTrack()
169
 
            cur_md = self.track_list.GetMetadata(cur_track)
170
 
            len = self.track_list.GetLength()
171
 
 
172
 
            self.assertEqual(0, self.track_list.AddTrack(FILE, True))
173
 
            self.assertEqual(len + 1, self.track_list.GetLength())
174
 
 
175
 
            md = self.track_list.GetMetadata(len)
176
 
            self.assertEqual(FILE, md['location'])
177
 
            self.assertEqual(len, self.track_list.GetCurrentTrack())
178
 
 
179
 
            self.track_list.DelTrack(len)
180
 
            self.assertEqual(len, self.track_list.GetLength())
181
 
 
182
 
            self.track_list.AddTrack(cur_md['location'], True)
183
 
 
184
 
        def testGetCurrentTrack(self):
185
 
            """
186
 
                Check the GetCurrentTrack information
187
 
            """
188
 
            cur_track = self.track_list.GetCurrentTrack()
189
 
            self.assertTrue(cur_track >= 0, "Tests start with playing music")
190
 
 
191
 
            self._stop()
192
 
            self.assertEqual(dbus.Int32(-1), self.track_list.GetCurrentTrack(),
193
 
                        "Our implementation returns -1 if no tracks are playing")
194
 
 
195
 
            self._play()
196
 
            self.assertEqual(cur_track, self.track_list.GetCurrentTrack(),
197
 
                    "After a stop and play, we should be at the same track")
198
 
 
199
 
        def __test_bools(self, getter, setter):
200
 
            """
201
 
                Generic function for checking that a boolean value changes
202
 
            """
203
 
            cur_val = getter()
204
 
            if cur_val == dbus.Int32(0):
205
 
                val = False
206
 
            elif cur_val == dbus.Int32(1):
207
 
                val = True
208
 
            else:
209
 
                self.fail("Got an invalid value from status")
210
 
 
211
 
            setter(False)
212
 
            status = getter()
213
 
            self.assertEqual(dbus.Int32(0), status)
214
 
 
215
 
            setter(True)
216
 
            status = getter()
217
 
            self.assertEqual(dbus.Int32(1), status)
218
 
 
219
 
            setter(val)
220
 
            self.track_list.SetLoop(val)
221
 
 
222
 
        def testLoop(self):
223
 
            """
224
 
                Tests that you can change the loop settings
225
 
            """
226
 
            self.__test_bools(lambda: self.player.GetStatus()[3],
227
 
                            lambda x: self.track_list.SetLoop(x))
228
 
 
229
 
        def testRandom(self):
230
 
            """
231
 
                Tests that you can change the random settings
232
 
            """
233
 
            self.__test_bools(lambda: self.player.GetStatus()[1],
234
 
                            lambda x: self.track_list.SetRandom(x))
235
 
 
236
 
    class TestPlayer(TestExaileMpris):
237
 
 
238
 
        """
239
 
            Tests the /Player object for MPRIS
240
 
        """
241
 
 
242
 
        def testNextPrev(self):
243
 
            """
244
 
                Make sure you can skip back and forward
245
 
            """
246
 
            cur_track = self.track_list.GetCurrentTrack()
247
 
            self.player.Next()
248
 
            new_track = self.track_list.GetCurrentTrack()
249
 
            self.assertNotEqual(cur_track, new_track)
250
 
            self.player.Prev()
251
 
            self.assertEqual(cur_track, self.track_list.GetCurrentTrack())
252
 
 
253
 
        def testStopPlayPause(self):
254
 
            """
255
 
                Make sure play, pause, and stop behaive as designed
256
 
            """
257
 
            self._stop()
258
 
            self.assertEqual(dbus.Int32(2), self.player.GetStatus()[0])
259
 
 
260
 
            self._play()
261
 
            self.assertEqual(dbus.Int32(0), self.player.GetStatus()[0])
262
 
            self._play()
263
 
            self.assertEqual(dbus.Int32(0), self.player.GetStatus()[0])
264
 
            
265
 
            self._pause()
266
 
            self.assertEqual(dbus.Int32(1), self.player.GetStatus()[0])
267
 
            self._pause()
268
 
            self.assertEqual(dbus.Int32(0), self.player.GetStatus()[0])
269
 
 
270
 
            self._stop()
271
 
            self.assertEqual(dbus.Int32(2), self.player.GetStatus()[0])
272
 
            self._pause()
273
 
            self.assertEqual(dbus.Int32(2), self.player.GetStatus()[0])
274
 
 
275
 
        def testVolume(self):
276
 
            """
277
 
                Test to make sure volumes are set happily
278
 
            """
279
 
            vol = self.player.VolumeGet()
280
 
            self.player.VolumeSet(1 - vol)
281
 
            self.assertEqual(1 - vol, self.player.VolumeGet())
282
 
            self.player.VolumeSet(vol)
283
 
            self.assertEqual(vol, self.player.VolumeGet())
284
 
 
285
 
        def testPosition(self):
286
 
            """
287
 
                Test the PositionGet and PositionSet functions. Unfortuantely this
288
 
                is very time sensitive and thus has about a 10 second sleep in the
289
 
                function
290
 
            """
291
 
            time.sleep(3)
292
 
            self._pause()
293
 
 
294
 
            pos = self.player.PositionGet()
295
 
            time.sleep(1)
296
 
            self.assertEqual(pos, self.player.PositionGet(),
297
 
                    "Position shouldn't move while paused")
298
 
 
299
 
            self._pause()
300
 
            time.sleep(4)
301
 
            last_pos = self.player.PositionGet()
302
 
            self.assertTrue(pos < last_pos,
303
 
                    "Position shouldn't advance while paused: %d >= %d" %
304
 
                        (pos, last_pos))
305
 
 
306
 
            self.player.PositionSet(pos)
307
 
            time.sleep(2)
308
 
            self._pause()
309
 
            mid_pos = self.player.PositionGet(),
310
 
            self.assertTrue(mid_pos[0] < last_pos,
311
 
                    "Resetting to position %d, %d should be between that at %d"
312
 
                            % (pos, mid_pos[0], last_pos))
313
 
 
314
 
            self._pause()
315
 
            time.sleep(0.5)
316
 
            self.assertTrue(pos < self.player.PositionGet(),
317
 
                    "Make sure it still advances")
318
 
 
319
 
            self.player.PositionSet(-1)
320
 
            self.assertTrue(pos < self.player.PositionGet(),
321
 
                    "Don't move to invalid position")
322
 
        
323
 
 
324
 
def suite():
325
 
    sub_test = [TestMprisRoot, TestTrackList]
326
 
 
327
 
    suites = []
328
 
 
329
 
    for test in sub_test:
330
 
        suites.append(unittest.defaultTestLoader.loadTestsFromTestCase(test))
331
 
 
332
 
    return unittest.TestSuite(suites)
333
 
 
334
 
if __name__ == "__main__":
335
 
    unittest.main()
336