aboutsummaryrefslogtreecommitdiffstats
path: root/solo-tool-project/test/notifier_unittest.py
diff options
context:
space:
mode:
authorEddy Pedroni <epedroni@pm.me>2024-11-09 20:35:56 +0100
committerEddy Pedroni <epedroni@pm.me>2024-11-09 20:35:56 +0100
commitcda8197669409689be291660f93cb288ab2d31b3 (patch)
tree81db9b0c7c0491e0737cbffb39af6b935c0dfeb8 /solo-tool-project/test/notifier_unittest.py
parenta2257a900d4fffd6f94b73f1c48c62370ed1d684 (diff)
Migrate to project-based structure
Diffstat (limited to 'solo-tool-project/test/notifier_unittest.py')
-rw-r--r--solo-tool-project/test/notifier_unittest.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/solo-tool-project/test/notifier_unittest.py b/solo-tool-project/test/notifier_unittest.py
new file mode 100644
index 0000000..8a6e988
--- /dev/null
+++ b/solo-tool-project/test/notifier_unittest.py
@@ -0,0 +1,100 @@
+import pytest
+
+from solo_tool.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] * callbacks
+ values = [None] * callbacks
+
+ def createCallback(i):
+ 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, 123)
+ assert all(calledFlags)
+ assert values == [123] * callbacks
+
+ 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)
+ checkEvent(uut, Notifier.AB_LIMIT_ENABLED_EVENT)
+
+def test_eventWithoutRegisteredCallbacks(uut):
+ uut.notify(Notifier.PLAYING_STATE_EVENT, 0)
+ # expect no crash
+
+def test_eventsWithMockPlayer(uut, mockPlayer):
+ def checkEvent(eventCode, simulateEvent, expectedValue):
+ called = False
+ 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, True)
+ checkEvent(Notifier.PLAYBACK_VOLUME_EVENT, mockPlayer.simulatePlaybackVolumeChanged, 75)
+
+def test_singleEventNotification(uut):
+ playingStateCalled = False
+ def playingStateCallback(value):
+ nonlocal playingStateCalled
+ playingStateCalled = True
+
+ volumeCalled = False
+ def volumeCallback(value):
+ 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, 0)
+ assert playingStateCalled
+ assert not volumeCalled
+
+ playingStateCalled = False
+
+ uut.notify(Notifier.PLAYBACK_VOLUME_EVENT, 0)
+ assert not playingStateCalled
+ assert volumeCalled
+
+ volumeCalled = False
+
+ uut.notify(Notifier.PLAYBACK_RATE_EVENT, 0)
+ assert not playingStateCalled
+ assert not volumeCalled