From 2b4323eca801638f0a305ff29afe815a45e103b1 Mon Sep 17 00:00:00 2001 From: Eddy Pedroni Date: Tue, 4 Jan 2022 17:55:00 +0100 Subject: Added current value to notification system, fixed Qt hack --- notifier_unittest.py | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'notifier_unittest.py') diff --git a/notifier_unittest.py b/notifier_unittest.py index 13f2285..4aff8f4 100644 --- a/notifier_unittest.py +++ b/notifier_unittest.py @@ -14,20 +14,24 @@ def uut(mockPlayer): def test_allEvents(uut): def checkEvent(uut, event): callbacks = 2 - calledFlags = [False] * 2 + calledFlags = [False] * callbacks + values = [None] * callbacks def createCallback(i): - def cb(): - nonlocal calledFlags + def cb(value): + nonlocal calledFlags, values calledFlags[i] = True + values[i] = value + return cb for i in range(0, callbacks): uut.registerCallback(event, createCallback(i)) assert not any(calledFlags) - uut.notify(event) + uut.notify(event, 123) assert all(calledFlags) + assert values == [123] * callbacks checkEvent(uut, Notifier.PLAYING_STATE_EVENT) checkEvent(uut, Notifier.PLAYBACK_VOLUME_EVENT) @@ -36,33 +40,39 @@ def test_allEvents(uut): checkEvent(uut, Notifier.CURRENT_AB_EVENT) def test_eventWithoutRegisteredCallbacks(uut): - uut.notify(Notifier.PLAYING_STATE_EVENT) + uut.notify(Notifier.PLAYING_STATE_EVENT, 0) # expect no crash def test_eventsWithMockPlayer(uut, mockPlayer): - def checkEvent(eventCode, simulateEvent): + def checkEvent(eventCode, simulateEvent, expectedValue): called = False - def callback(): - nonlocal called + receivedValue = None + def callback(value): + nonlocal called, receivedValue called = True + receivedValue = value uut.registerCallback(eventCode, callback) assert not called simulateEvent() assert called + assert receivedValue == expectedValue + + mockPlayer.state = 1 + mockPlayer.volume = 75 - checkEvent(Notifier.PLAYING_STATE_EVENT, mockPlayer.simulatePlayingStateChanged) - checkEvent(Notifier.PLAYBACK_VOLUME_EVENT, mockPlayer.simulatePlaybackVolumeChanged) + checkEvent(Notifier.PLAYING_STATE_EVENT, mockPlayer.simulatePlayingStateChanged, True) + checkEvent(Notifier.PLAYBACK_VOLUME_EVENT, mockPlayer.simulatePlaybackVolumeChanged, 75) def test_singleEventNotification(uut): playingStateCalled = False - def playingStateCallback(): + def playingStateCallback(value): nonlocal playingStateCalled playingStateCalled = True volumeCalled = False - def volumeCallback(): + def volumeCallback(value): nonlocal volumeCalled volumeCalled = True @@ -72,18 +82,18 @@ def test_singleEventNotification(uut): assert not playingStateCalled assert not volumeCalled - uut.notify(Notifier.PLAYING_STATE_EVENT) + uut.notify(Notifier.PLAYING_STATE_EVENT, 0) assert playingStateCalled assert not volumeCalled playingStateCalled = False - uut.notify(Notifier.PLAYBACK_VOLUME_EVENT) + uut.notify(Notifier.PLAYBACK_VOLUME_EVENT, 0) assert not playingStateCalled assert volumeCalled volumeCalled = False - uut.notify(Notifier.PLAYBACK_RATE_EVENT) + uut.notify(Notifier.PLAYBACK_RATE_EVENT, 0) assert not playingStateCalled assert not volumeCalled -- cgit v1.2.3