aboutsummaryrefslogtreecommitdiffstats
path: root/abcontroller_unittest.py
blob: 9fdcbc30d043da307600788ebb5b66f5017265c7 (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
from abcontroller import ABController
from collections import namedtuple

TCase = namedtuple("TCase", ["currentPosition", "requestedPosition"])
AB = namedtuple("AB", ["a", "b"])
abLimits = AB(0.2, 0.4)

def _checkLimits(uut, tests):
    requestedPosition = None
    def callback(newPosition):
        nonlocal requestedPosition
        requestedPosition = newPosition
    
    originalCallback = uut._setPositionCallback
    uut._setPositionCallback = callback

    for t in tests:
        uut.positionChanged(t.currentPosition)
        assert requestedPosition == t.requestedPosition

    uut._setPositionCallback = originalCallback

def checkLimits(uut, aLimit, bLimit, fail=False):
    tests = [
        TCase(aLimit - 0.1, None),
        TCase(aLimit, None),
        TCase(bLimit - 0.1, None),
        TCase(bLimit, None),
        TCase(bLimit + 0.1, aLimit if not fail else None)
    ]
    _checkLimits(uut, tests)
    if not fail:
        assert uut.getCurrentLimits()[0] == aLimit
        assert uut.getCurrentLimits()[1] == bLimit

def checkDefaultLimits(uut):
    tests = [
        TCase(0.0, None),
        TCase(0.1, 0.0),
        TCase(0.5, 0.0)
    ]
    _checkLimits(uut, tests)

def test_oneSetOfLimits():
    song = "/path/to/song"

    uut = ABController()
    uut.setCurrentSong(song)
    uut.storeLimits(abLimits.a, abLimits.b)
    uut.loadLimits(0)
    assert uut.getLoadedIndex() == 0

    checkLimits(uut, abLimits.a, abLimits.b)
    assert uut.getStoredLimits(song) == [abLimits]

def test_multipleSetsOfLimits():
    song = "/path/to/song"
    abLimits = [
        AB(0.2, 0.4),
        AB(0.3, 0.5),
        AB(0.0, 1.2)
    ]

    uut = ABController()
    uut.setCurrentSong(song)
    for l in abLimits:
        uut.storeLimits(l.a, l.b)
    
    for i, l in enumerate(abLimits):
        uut.loadLimits(i)
        assert uut.getLoadedIndex() == i
        checkLimits(uut, l.a, l.b)

    assert uut.getStoredLimits(song) == abLimits

def test_multipleSongs():
    songs = [
        "/path/to/song",
        "/path/to/another/song"
    ]
    abLimits = [
        AB(0.2, 0.4),
        AB(0.3, 0.5)
    ]
    uut = ABController()
    for i, s in enumerate(songs):
        uut.storeLimits(abLimits[i].a, abLimits[i].b, s)

    for i, s in enumerate(songs):
        uut.setCurrentSong(s)
        uut.loadLimits(0)
        assert uut.getLoadedIndex() == 0
        
        checkLimits(uut, abLimits[i].a, abLimits[i].b)
        assert uut.getStoredLimits(s) == [abLimits[i]]

def test_disableAbRepeat():
    song = "/path/to/song"

    uut = ABController()
    uut.setCurrentSong(song)
    uut.storeLimits(abLimits.a, abLimits.b)
    uut.loadLimits(0)
    assert uut.getLoadedIndex() == 0

    assert uut.isEnabled()

    uut.setEnable(False)
    checkLimits(uut, abLimits.a, abLimits.b, fail=True)
    assert not uut.isEnabled()

    uut.setEnable(True)
    checkLimits(uut, abLimits.a, abLimits.b)
    assert uut.isEnabled()

def test_storeLimitsToSpecificSong():
    song = "/path/to/song"

    uut = ABController()
    uut.storeLimits(abLimits.a, abLimits.b, song)
    uut.setCurrentSong(song)
    uut.loadLimits(0)
    assert uut.getLoadedIndex() == 0

    checkLimits(uut, abLimits.a, abLimits.b)

def test_storeLimitsWithoutCurrentSong():
    uut = ABController()
    uut.storeLimits(abLimits.a, abLimits.b)
    uut.loadLimits(0)
    assert uut.getLoadedIndex() == None

    checkDefaultLimits(uut)
    
def test_storeLimitsToSongWithoutCurrentSong():
    song = "/path/to/song"
    uut = ABController()
    uut.storeLimits(abLimits.a, abLimits.b, song)
    uut.loadLimits(0)
    assert uut.getLoadedIndex() == None

    checkDefaultLimits(uut)

    uut.setCurrentSong(song)

    checkDefaultLimits(uut)

    uut.loadLimits(0)
    assert uut.getLoadedIndex() == 0
    
    checkLimits(uut, abLimits.a, abLimits.b)
    
def test_storeLimitsToCurrentSongButDoNotSetCurrentLimits():
    song = "/path/to/song"
    uut = ABController()
    uut.setCurrentSong(song)
    uut.storeLimits(abLimits.a, abLimits.b)
    assert uut.getLoadedIndex() == None

    checkDefaultLimits(uut)

    uut.loadLimits(0)
    assert uut.getLoadedIndex() == 0

    checkLimits(uut, abLimits.a, abLimits.b)
    
def test_getStoredLimitsOfInexistentSong():
    song = "/path/to/song"
    uut = ABController()
    assert uut.getStoredLimits(song) == None

def test_clearAbController():
    songsWithLimits = [
        ("/path/to/song", AB(0.2, 0.4)),
        ("/path/to/another/song", AB(0.3, 0.5))
    ]

    uut = ABController()
    for s in songsWithLimits:
        uut.storeLimits(s[1].a, s[1].b, s[0])

    for i, s in enumerate(songsWithLimits):
        assert uut.getStoredLimits(s[0]) == [s[1]]

    uut.clear()

    for i, s in enumerate(songsWithLimits):
        assert uut.getStoredLimits(s[0]) == None
    
def test_setTemporaryLimits():
    abLimits = [
        AB(0.2, 0.4),
        AB(0.3, 0.5),
        AB(0.0, 1.2)
    ]
    uut = ABController()

    for l in abLimits:
        uut.setLimits(l.a, l.b)
        assert uut.getLoadedIndex() == None
        checkLimits(uut, l.a, l.b)

def test_setTemporaryLimitsWithCurrentSong():
    songLimits = AB(0.2, 0.4)
    abLimits = [
        AB(0.2, 0.4),
        AB(0.3, 0.5),
        AB(0.0, 1.2)
    ]
    song = "/path/to/song"
    uut = ABController()
    uut.setCurrentSong(song)
    uut.storeLimits(songLimits.a, songLimits.b)
    uut.loadLimits(0)
    assert uut.getLoadedIndex() == 0

    for l in abLimits:
        uut.setLimits(l.a, l.b)
        checkLimits(uut, l.a, l.b)

def test_defaultBehaviour():
    uut = ABController()
    checkDefaultLimits(uut)

def test_nextStoredLimit():
    song = "/path/to/song"
    abLimits = [
        AB(0.2, 0.4),
        AB(0.3, 0.5)
    ]

    uut = ABController()
    uut.setCurrentSong(song)
    for l in abLimits:
        uut.storeLimits(l.a, l.b)
    
    checkDefaultLimits(uut)

    uut.nextStoredAbLimits()
    checkLimits(uut, abLimits[0].a, abLimits[0].b)

    uut.nextStoredAbLimits()
    checkLimits(uut, abLimits[1].a, abLimits[1].b)

    uut.nextStoredAbLimits()
    checkLimits(uut, abLimits[1].a, abLimits[1].b)

def test_previousStoredLimit():
    song = "/path/to/song"
    abLimits = [
        AB(0.2, 0.4),
        AB(0.3, 0.5)
    ]

    uut = ABController()
    uut.setCurrentSong(song)
    for l in abLimits:
        uut.storeLimits(l.a, l.b)
    
    checkDefaultLimits(uut)

    uut.previousStoredAbLimits()
    checkLimits(uut, abLimits[0].a, abLimits[0].b)

    uut.previousStoredAbLimits()
    checkLimits(uut, abLimits[0].a, abLimits[0].b)

    uut.loadLimits(1)
    checkLimits(uut, abLimits[1].a, abLimits[1].b)

    uut.previousStoredAbLimits()
    checkLimits(uut, abLimits[0].a, abLimits[0].b)