aboutsummaryrefslogtreecommitdiffstats
path: root/notifier_unittest.py
blob: 13f2285061cd2afbc98908fa60d5ae60dba41454 (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
import pytest

from notifier import Notifier
from player_mock import Player

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

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

def test_allEvents(uut):
    def checkEvent(uut, event):
        callbacks = 2
        calledFlags = [False] * 2

        def createCallback(i):
            def cb():
                nonlocal calledFlags
                calledFlags[i] = True
            return cb

        for i in range(0, callbacks):
            uut.registerCallback(event, createCallback(i))

        assert not any(calledFlags)
        uut.notify(event)
        assert all(calledFlags)

    checkEvent(uut, Notifier.PLAYING_STATE_EVENT)
    checkEvent(uut, Notifier.PLAYBACK_VOLUME_EVENT)
    checkEvent(uut, Notifier.PLAYBACK_RATE_EVENT)
    checkEvent(uut, Notifier.CURRENT_SONG_EVENT)
    checkEvent(uut, Notifier.CURRENT_AB_EVENT)

def test_eventWithoutRegisteredCallbacks(uut):
    uut.notify(Notifier.PLAYING_STATE_EVENT)
    # expect no crash

def test_eventsWithMockPlayer(uut, mockPlayer):
    def checkEvent(eventCode, simulateEvent):
        called = False
        def callback():
            nonlocal called
            called = True

        uut.registerCallback(eventCode, callback)

        assert not called
        simulateEvent()
        assert called

    checkEvent(Notifier.PLAYING_STATE_EVENT, mockPlayer.simulatePlayingStateChanged)
    checkEvent(Notifier.PLAYBACK_VOLUME_EVENT, mockPlayer.simulatePlaybackVolumeChanged)

def test_singleEventNotification(uut):
    playingStateCalled = False
    def playingStateCallback():
        nonlocal playingStateCalled
        playingStateCalled = True

    volumeCalled = False
    def volumeCallback():
        nonlocal volumeCalled
        volumeCalled = True

    uut.registerCallback(Notifier.PLAYING_STATE_EVENT, playingStateCallback)
    uut.registerCallback(Notifier.PLAYBACK_VOLUME_EVENT, volumeCallback)

    assert not playingStateCalled
    assert not volumeCalled

    uut.notify(Notifier.PLAYING_STATE_EVENT)
    assert playingStateCalled
    assert not volumeCalled

    playingStateCalled = False

    uut.notify(Notifier.PLAYBACK_VOLUME_EVENT)
    assert not playingStateCalled
    assert volumeCalled

    volumeCalled = False

    uut.notify(Notifier.PLAYBACK_RATE_EVENT)
    assert not playingStateCalled
    assert not volumeCalled