import pytest from fixtures import mockPlayer, testSongs from solo_tool.solo_tool import SoloTool from solo_tool.midi_controller_actition import ActitionController CHANNEL = 15 REWIND = 102 SET = 103 JUMP = 104 PLAY = 105 class MidiWrapperMock: def __init__(self): self.sentMessages = list() def setCallback(self, callback): self.callback = callback def simulateInput(self, control, channel): if self.callback is not None: self.callback(control, channel) def getLatestMessage(self): return self.sentMessages[-1] @pytest.fixture def soloTool(mockPlayer, testSongs): st = SoloTool(player=mockPlayer) for song in testSongs: st.addSong(song) return st @pytest.fixture def midiWrapperMock(soloTool): return MidiWrapperMock() @pytest.fixture def uut(soloTool, midiWrapperMock): uut = ActitionController(midiWrapperMock) uut.setSoloTool(soloTool) return uut def test_rewindMessage(uut, soloTool, mockPlayer, midiWrapperMock): soloTool.song = 1 mockPlayer.position = 0.5 # Sending rewind goes back to the start of the song midiWrapperMock.simulateInput(REWIND, CHANNEL) assert mockPlayer.getPlaybackPosition() == 0.0 # Sending again does not change the song assert soloTool.song == 1 midiWrapperMock.simulateInput(REWIND, CHANNEL) assert soloTool.song == 1 assert mockPlayer.position == 0.0 def test_setMessage(uut, soloTool, mockPlayer, midiWrapperMock): callbackValue = None callbackCalled = False def callback(keyPoint): nonlocal callbackCalled, callbackValue callbackValue = keyPoint callbackCalled = True soloTool.registerKeyPointSelectionCallback(callback) # Sending set sets the current position as the key point assert soloTool.keyPoint == 0.0 mockPlayer.position = 0.3 midiWrapperMock.simulateInput(SET, CHANNEL) assert soloTool.keyPoint == 0.3 assert callbackCalled assert callbackValue == 0.3 # Sending it again does nothing callbackCalled = False midiWrapperMock.simulateInput(SET, CHANNEL) assert soloTool.keyPoint == 0.3 assert not callbackCalled def test_jumpMessage(uut, soloTool, mockPlayer, midiWrapperMock): soloTool.keyPoint = 0.5 mockPlayer.position = 0.0 # Sending jump sets the player position to the current key point midiWrapperMock.simulateInput(JUMP, CHANNEL) assert mockPlayer.position == 0.5 # Sending again does nothing midiWrapperMock.simulateInput(JUMP, CHANNEL) assert mockPlayer.position == 0.5 def test_playMessage(uut, soloTool, mockPlayer, midiWrapperMock): callbackValue = None callbackCalled = False def callback(state): nonlocal callbackCalled, callbackValue callbackValue = state callbackCalled = True soloTool.registerPlayingStateCallback(callback) # Sending play starts playing assert not mockPlayer.isPlaying() midiWrapperMock.simulateInput(PLAY, CHANNEL) assert mockPlayer.isPlaying() assert callbackCalled assert callbackValue == True # Sending again stops playing callbackCalled = False midiWrapperMock.simulateInput(PLAY, CHANNEL) assert not mockPlayer.isPlaying() assert callbackCalled assert callbackValue == False