aboutsummaryrefslogtreecommitdiffstats
path: root/notifier_unittest.py
blob: b840c161c34d8b80c91425035958a5caa801923a (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
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 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)

def test_allEvents(uut):
    checkEvent(uut, Notifier.PLAYING_STATE_EVENT)

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

def test_playingStateEventWithMockPlayer(uut, mockPlayer):
    called = False
    def callback():
        nonlocal called
        called = True

    uut.registerCallback(Notifier.PLAYING_STATE_EVENT, callback)

    assert not called
    mockPlayer.simulatePlayingStateChanged()
    assert called