aboutsummaryrefslogtreecommitdiffstats
path: root/solo-tool-project/test/solo_tool_integrationtest.py
blob: 2a818ed1111580076dbfa898efc75e565217db58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import pathlib
import shutil
import pytest

from solo_tool.solo_tool import SoloTool
from player_mock import Player as MockPlayer

@pytest.fixture
def mockPlayer():
    return MockPlayer()

@pytest.fixture
def uut(mockPlayer):
    return SoloTool(mockPlayer)

@pytest.fixture
def prepared_tmp_path(tmp_path):
    testFiles = [
        "test.flac",
        "test.mp3",
        "test_session.json"
    ]
    for f in testFiles:
        shutil.copy(pathlib.Path(f), tmp_path)

    return tmp_path

def test_playerControls(uut, mockPlayer):
    assert mockPlayer.state == MockPlayer.STOPPED
    assert uut.isPlaying() == False
    uut.play()
    assert mockPlayer.state == MockPlayer.PLAYING
    assert uut.isPlaying() == True
    uut.pause()
    assert mockPlayer.state == MockPlayer.PAUSED
    assert uut.isPlaying() == False
    uut.stop()
    assert mockPlayer.state == MockPlayer.STOPPED
    assert uut.isPlaying() == False

    assert mockPlayer.rate == 1.0
    uut.rate = 0.5
    assert mockPlayer.rate == 0.5
    assert uut.rate == 0.5

    assert mockPlayer.position == 0.0
    uut.position = 0.5
    assert mockPlayer.position == 0.5
    assert uut.position == 0.5

    assert mockPlayer.volume == 1.0
    uut.volume = 0.5
    assert mockPlayer.volume == 0.5
    assert uut.volume == 0.5

def test_sanitizePlaybackRate(uut):
    # Initial value
    assert uut.rate == 1.0

    # Valid rates are >= 0.0, invalid is ignored
    uut.rate = -0.1
    assert uut.rate == 1.0

    uut.rate = 0.0
    assert uut.rate == 0.0
    
    uut.rate = 0.0001
    assert uut.rate == 0.0001

    uut.rate = 150.0
    assert uut.rate == 150.0

def test_sanitizePlaybackPosition(uut):
    # Initial value
    assert uut.position == 0.0

    # Valid positions are in [0, 1], invalid is limited
    uut.position = 0.2
    assert uut.position == 0.2

    uut.position = -0.1
    assert uut.position == 0.0

    uut.position = 1.0
    assert uut.position == 1.0

    uut.position = 0.4
    assert uut.position == 0.4

    uut.position = 1.5
    assert uut.position == 1.0

def test_sanitizePlaybackVolume(uut):
    # Initial value
    assert uut.volume == 1.0

    # Valid volumes are >= 0.0, invalid is ignored
    uut.volume = -0.1
    assert uut.volume == 1.0

    uut.volume = 0.0
    assert uut.volume == 0.0

    uut.volume = 1.0
    assert uut.volume == 1.0

    uut.volume = 150.0
    assert uut.volume == 150.0

def test_addAndSelectSongs(uut, mockPlayer):
    songs = [
        "test.mp3",
        "test.flac"
    ]

    # Songs are added one by one
    for song in songs:
        uut.addSong(song)

    # Songs are not selected automatically
    assert mockPlayer.currentSong == None
    assert uut.song == None

    # Song order is preserved
    assert uut.songs == songs

    # Modifying the song list directly has no effect
    uut.songs.append("something")
    assert uut.songs == songs

    # Songs are selected by index
    for i, s in enumerate(uut.songs):
        uut.song = i
        assert mockPlayer.currentSong == uut.songs[i]
        assert uut.song == i

    # The current song cannot be de-selected
    uut.song = None
    assert uut.song == len(uut.songs) - 1

    # Non-existent songs cannot be selected
    uut.song = -1
    assert uut.song == len(uut.songs) - 1

    uut.song = 2
    assert uut.song == len(uut.songs) - 1

def test_addAndJumpToKeyPoints(uut, mockPlayer):
    uut.addSong("test.flac")
    uut.addSong("test.mp3")

    def checkJump(before, expectedAfter):
        mockPlayer.position = before
        uut.jump()
        assert mockPlayer.position == expectedAfter

    # Key points are None as long as no song is selected
    uut.keyPoints = [0.1, 0.2]
    uut.keyPoint = 0.5
    assert uut.keyPoints is None
    assert uut.keyPoint is None

    uut.song = 0

    # Once a song is selected, jump to start by default
    assert uut.keyPoint == 0.0
    checkJump(0.5, 0.0)

    # By default songs have an empty list of key points
    assert uut.keyPoints == []

    uut.keyPoints = [0.2, 0.4, 0.1, 0.2]

    # Added key points are not automatically selected
    assert uut.keyPoint == 0.0
    checkJump(0.1, 0.0)

    # Any key point can be selected
    uut.keyPoint = uut.keyPoints[0]
    checkJump(0.0, uut.keyPoints[0])

    uut.keyPoint = 0.5
    checkJump(0.0, 0.5)

def test_sanitizeKeyPoint(uut):
    song = "test.flac"
    uut.addSong(song)
    uut.song = 0
    uut.keyPoints = [0.2, 0.4, 0.1, 0.2, None, -0.5, 1.0, 1.5]

    # Added key points are automatically de-duplicated, sanitized and sorted to ascending order
    assert uut.keyPoints == [0.1, 0.2, 0.4]

    # Key point and key point list cannot be none
    uut.keyPoint = 0.5

    uut.keyPoint = None
    assert uut.keyPoint == 0.5

    uut.keyPoints = None
    assert uut.keyPoints == [0.1, 0.2, 0.4]

    # Valid key points are in [0, 1)
    uut.keyPoint = -0.1
    assert uut.keyPoint == 0.5

    uut.keyPoint = 1.0
    assert uut.keyPoint == 0.5

    uut.keyPoint = 0.999
    assert uut.keyPoint == 0.999

def test_keyPointsPerSong(uut, mockPlayer):
    songs = [
        ("test.flac", [0.0, 0.5]),
        ("test.mp3", [0.1])
    ]
    
    # Key points list is set for the selected song
    for i, (song, keyPoints) in enumerate(songs):
        uut.addSong(song)
        uut.song = i
        uut.keyPoints = keyPoints

    # Key points list is automatically loaded when the song selection changes
    # Active key point is always reset to 0 when song selection changes
    for i, (song, keyPoints) in enumerate(songs):
        uut.keyPoint = 0.5
        uut.song = i
        assert uut.keyPoints == keyPoints
        assert uut.keyPoint == 0.0

    # Key points are copied, not stored by reference
    for i, (song, keyPoints) in enumerate(songs):
        uut.song = i
        keyPoints.append(1.0)
        assert 1.0 not in uut.keyPoints

def test_addInexistentSong(uut, mockPlayer):
    song = "not/a/real/file"

    with pytest.raises(FileNotFoundError):
        uut.addSong(song)

def test_playingStateNotification(uut, mockPlayer):
    song = "test.flac"
    uut.addSong(song)
    uut.song = 0

    called = False
    receivedValue = None
    def callback(value):
        nonlocal called, receivedValue
        called = True
        receivedValue = value

    uut.registerPlayingStateCallback(callback)

    assert mockPlayer.state == MockPlayer.STOPPED
    assert not called

    uut.play()
    assert called
    assert receivedValue == True
    called = False
    uut.play()
    assert not called

    uut.pause()
    assert called
    assert receivedValue == False
    called = False
    uut.pause()
    assert not called

    uut.play()
    assert called
    assert receivedValue == True
    called = False

    uut.stop()
    assert called
    assert receivedValue == False
    called = False
    uut.stop()
    assert not called

def test_playbackVolumeNotification(uut, mockPlayer):
    song = "test.flac"
    uut.addSong(song)
    uut.song = 0

    called = False
    receivedValue = None
    def callback(value):
        nonlocal called, receivedValue
        called = True
        receivedValue = value

    uut.registerVolumeCallback(callback)

    assert not called

    uut.volume = 0.3
    assert called
    assert receivedValue == 0.3
    called = False

    uut.volume = 0.3
    assert not called

def test_playbackRateNotification(uut, mockPlayer):
    song = "test.flac"
    uut.addSong(song)
    uut.song = 0

    called = False
    receivedValue = None
    def callback(value):
        nonlocal called, receivedValue
        called = True
        receivedValue = value

    uut.registerRateCallback(callback)

    assert not called

    uut.rate = 0.5
    assert called
    assert receivedValue == 0.5
    called = False

    uut.rate = 0.5
    assert not called

def test_currentSongNotification(uut):
    called = False
    receivedValue = None
    def callback(value):
        nonlocal called, receivedValue
        called = True
        receivedValue = value

    uut.registerCurrentSongCallback(callback)
    assert not called

    songs = [
        "test.flac",
        "test.mp3"
    ]

    # Adding a song does not trigger a notification
    uut.addSong(songs[0])
    assert not called

    # Selecting a song for the first time triggers
    uut.song = 0
    assert called
    assert receivedValue == 0
    called = False

    uut.addSong(songs[1])
    assert not called

    # Selecting the same song does not trigger
    uut.song = 0
    assert not called

    uut.song = 1
    assert called
    assert receivedValue == 1
    called = False

def test_currentKeyPointNotification(uut):
    called = False
    receivedValue = None
    def callback(value):
        nonlocal called, receivedValue
        called = True
        receivedValue = value

    uut.registerCurrentKeyPointCallback(callback)
    assert not called

    song = "test.flac"
    uut.addSong(song)
    uut.song = 0

    # Selecting a song for the first time sets the key point to 0.0
    assert called
    assert receivedValue == 0.0
    called = False

    # Changing the key point triggers a notification
    uut.keyPoint = 0.5
    assert called
    assert receivedValue == 0.5
    called = False

    # Adding list of key points does not trigger a notification
    uut.keyPoints = [0.2, 0.4]
    assert not called

    # Assigning the same key point again does not trigger a notification
    uut.keyPoint = 0.5
    assert not called