|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- #include "unit-tests.h"
- #include "unit-tests_Settings.h"
- #include <src/MIDI.h>
- #include <test/mocks/test-mocks_SerialMock.h>
-
- BEGIN_MIDI_NAMESPACE
-
- END_MIDI_NAMESPACE
-
- // -----------------------------------------------------------------------------
-
- BEGIN_UNNAMED_NAMESPACE
-
- using namespace testing;
- USING_NAMESPACE_UNIT_TESTS
-
- template<unsigned Size>
- struct VariableSysExSettings : midi::DefaultSettings
- {
- static const unsigned SysExMaxSize = Size;
- };
-
- typedef test_mocks::SerialMock<256> SerialMock;
- typedef midi::SerialMIDI<SerialMock> Transport;
-
- typedef VariableSysExSettings<256> Settings;
- typedef midi::MidiInterface<Transport, Settings> MidiInterface;
-
- MidiInterface* midi;
-
- class MidiInputCallbacks : public Test
- {
- public:
- MidiInputCallbacks()
- : mTransport(mSerial)
- , mMidi(mTransport)
- {
- }
- virtual ~MidiInputCallbacks()
- {
- }
-
- protected:
- virtual void SetUp()
- {
- midi = &mMidi;
- }
-
- virtual void TearDown()
- {
- midi = nullptr;
- }
-
- protected:
- SerialMock mSerial;
- Transport mTransport;
- MidiInterface mMidi;
- };
-
- // --
-
- void handleNoteOn(byte inChannel, byte inPitch, byte inVelocity)
- {
- EXPECT_NE(midi, nullptr);
- midi->sendNoteOn(inPitch, inVelocity, inChannel);
- }
-
- TEST_F(MidiInputCallbacks, noteOn)
- {
- mMidi.setHandleNoteOn(handleNoteOn);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 3;
- static const byte rxData[rxSize] = { 0x9b, 12, 34 };
- mSerial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::NoteOn);
- EXPECT_EQ(mMidi.getChannel(), 12);
- EXPECT_EQ(mMidi.getData1(), 12);
- EXPECT_EQ(mMidi.getData2(), 34);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 3);
- byte buffer[3] = { 0 };
- mSerial.mTxBuffer.read(buffer, 3);
- EXPECT_THAT(buffer, ContainerEq(rxData));
-
- // Test null velocity note on
- EXPECT_EQ(MidiInterface::Settings::HandleNullVelocityNoteOnAsNoteOff, true);
- mSerial.mRxBuffer.write(0x9b);
- mSerial.mRxBuffer.write(12);
- mSerial.mRxBuffer.write(0);
-
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::NoteOff);
- EXPECT_EQ(mMidi.getChannel(), 12);
- EXPECT_EQ(mMidi.getData1(), 12);
- EXPECT_EQ(mMidi.getData2(), 0);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 0);
- }
-
- // --
-
- void handleNoteOff(byte inChannel, byte inPitch, byte inVelocity)
- {
- EXPECT_NE(midi, nullptr);
- midi->sendNoteOff(inPitch, inVelocity, inChannel);
- }
-
- TEST_F(MidiInputCallbacks, noteOff)
- {
- mMidi.setHandleNoteOff(handleNoteOff);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 3;
- static const byte rxData[rxSize] = { 0x8b, 12, 34 };
- mSerial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::NoteOff);
- EXPECT_EQ(mMidi.getChannel(), 12);
- EXPECT_EQ(mMidi.getData1(), 12);
- EXPECT_EQ(mMidi.getData2(), 34);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 3);
- byte buffer[3] = { 0 };
- mSerial.mTxBuffer.read(buffer, 3);
- EXPECT_THAT(buffer, ContainerEq(rxData));
-
- // Test null velocity note on
- EXPECT_EQ(MidiInterface::Settings::HandleNullVelocityNoteOnAsNoteOff, true);
- mSerial.mRxBuffer.write(0x9b);
- mSerial.mRxBuffer.write(12);
- mSerial.mRxBuffer.write(0);
-
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::NoteOff);
- EXPECT_EQ(mMidi.getChannel(), 12);
- EXPECT_EQ(mMidi.getData1(), 12);
- EXPECT_EQ(mMidi.getData2(), 0);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 3);
- mSerial.mTxBuffer.read(buffer, 3);
- EXPECT_THAT(buffer, ElementsAreArray({
- 0x8b, 12, 0
- }));
- }
-
- // --
-
-
-
- void handleAfterTouchPoly(byte inChannel, byte inNote, byte inValue)
- {
- EXPECT_NE(midi, nullptr);
- midi->sendAfterTouch(inNote, inValue, inChannel);
- }
-
- TEST_F(MidiInputCallbacks, afterTouchPoly)
- {
- mMidi.setHandleAfterTouchPoly(handleAfterTouchPoly);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 3;
- static const byte rxData[rxSize] = { 0xab, 12, 34 };
- mSerial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::AfterTouchPoly);
- EXPECT_EQ(mMidi.getChannel(), 12);
- EXPECT_EQ(mMidi.getData1(), 12);
- EXPECT_EQ(mMidi.getData2(), 34);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 3);
- byte buffer[3] = { 0 };
- mSerial.mTxBuffer.read(buffer, 3);
- EXPECT_THAT(buffer, ContainerEq(rxData));
- }
-
- // --
-
- void handleControlChange(byte inChannel, byte inNumber, byte inValue)
- {
- EXPECT_NE(midi, nullptr);
- midi->sendControlChange(inNumber, inValue, inChannel);
- }
-
- TEST_F(MidiInputCallbacks, controlChange)
- {
- mMidi.setHandleControlChange(handleControlChange);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 3;
- static const byte rxData[rxSize] = { 0xbb, 12, 34 };
- mSerial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::ControlChange);
- EXPECT_EQ(mMidi.getChannel(), 12);
- EXPECT_EQ(mMidi.getData1(), 12);
- EXPECT_EQ(mMidi.getData2(), 34);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 3);
- byte buffer[3] = { 0 };
- mSerial.mTxBuffer.read(buffer, 3);
- EXPECT_THAT(buffer, ContainerEq(rxData));
- }
-
- // --
-
- void handleProgramChange(byte inChannel, byte inNumber)
- {
- EXPECT_NE(midi, nullptr);
- midi->sendProgramChange(inNumber, inChannel);
- }
-
- TEST_F(MidiInputCallbacks, programChange)
- {
- mMidi.setHandleProgramChange(handleProgramChange);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 2;
- static const byte rxData[rxSize] = { 0xcb, 12 };
- mSerial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::ProgramChange);
- EXPECT_EQ(mMidi.getChannel(), 12);
- EXPECT_EQ(mMidi.getData1(), 12);
- EXPECT_EQ(mMidi.getData2(), 0);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 2);
- byte buffer[2] = { 0 };
- mSerial.mTxBuffer.read(buffer, 2);
- EXPECT_THAT(buffer, ContainerEq(rxData));
- }
-
- // --
-
- void handleAfterTouchChannel(byte inChannel, byte inPressure)
- {
- EXPECT_NE(midi, nullptr);
- midi->sendAfterTouch(inPressure, inChannel);
- }
-
-
- TEST_F(MidiInputCallbacks, afterTouchChannel)
- {
- mMidi.setHandleAfterTouchChannel(handleAfterTouchChannel);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 2;
- static const byte rxData[rxSize] = { 0xdb, 12 };
- mSerial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::AfterTouchChannel);
- EXPECT_EQ(mMidi.getChannel(), 12);
- EXPECT_EQ(mMidi.getData1(), 12);
- EXPECT_EQ(mMidi.getData2(), 0);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 2);
- byte buffer[2] = { 0 };
- mSerial.mTxBuffer.read(buffer, 2);
- EXPECT_THAT(buffer, ContainerEq(rxData));
- }
-
- // --
-
- void handlePitchBend(byte inChannel, int inValue)
- {
- EXPECT_NE(midi, nullptr);
- midi->sendPitchBend(inValue, inChannel);
- }
-
- TEST_F(MidiInputCallbacks, pitchBend)
- {
- mMidi.setHandlePitchBend(handlePitchBend);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 3;
- static const byte rxData[rxSize] = { 0xeb, 12, 34 };
- mSerial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::PitchBend);
- EXPECT_EQ(mMidi.getChannel(), 12);
- EXPECT_EQ(mMidi.getData1(), 12);
- EXPECT_EQ(mMidi.getData2(), 34);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 3);
- byte buffer[3] = { 0 };
- mSerial.mTxBuffer.read(buffer, 3);
- EXPECT_THAT(buffer, ContainerEq(rxData));
- }
-
- // --
-
- void handleSysEx(byte* inData, unsigned inSize)
- {
- EXPECT_NE(midi, nullptr);
- midi->sendSysEx(inSize, inData, true);
- }
-
- TEST_F(MidiInputCallbacks, sysEx)
- {
- mMidi.setHandleSystemExclusive(handleSysEx);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 15;
- static const byte rxData[rxSize] = {
- 0xf0, 'H','e','l','l','o',',',' ','W','o','r','l','d','!', 0xf7
- };
- mSerial.mRxBuffer.write(rxData, rxSize);
-
- for (unsigned i = 0; i < rxSize - 1; ++i)
- {
- EXPECT_EQ(mMidi.read(), false);
- }
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::SystemExclusive);
- EXPECT_EQ(mMidi.getChannel(), 0);
- EXPECT_EQ(mMidi.getSysExArrayLength(), rxSize);
-
- EXPECT_EQ(unsigned(mSerial.mTxBuffer.getLength()), rxSize);
- const std::vector<byte> sysExData(mMidi.getSysExArray(),
- mMidi.getSysExArray() + rxSize);
- EXPECT_THAT(sysExData, ElementsAreArray(rxData));
- }
-
- TEST_F(MidiInputCallbacks, sysExLong)
- {
- mMidi.setHandleSystemExclusive(handleSysEx);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 210;
- static const byte rxData[rxSize] = {
- 0xf0,
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 'H','e','l','l','o',',',' ','W','o','r','l','d','!',
- 0xf7
- };
- mSerial.mRxBuffer.write(rxData, rxSize);
-
- for (unsigned i = 0; i < rxSize - 1; ++i)
- {
- EXPECT_EQ(mMidi.read(), false);
- }
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::SystemExclusive);
- EXPECT_EQ(mMidi.getChannel(), 0);
- EXPECT_EQ(mMidi.getSysExArrayLength(), rxSize);
-
- EXPECT_EQ(unsigned(mSerial.mTxBuffer.getLength()), rxSize);
- const std::vector<byte> sysExData(mMidi.getSysExArray(),
- mMidi.getSysExArray() + rxSize);
- EXPECT_THAT(sysExData, ElementsAreArray(rxData));
- }
-
- // --
-
- void handleMtcQuarterFrame(byte inData)
- {
- EXPECT_NE(midi, nullptr);
- midi->sendTimeCodeQuarterFrame(inData);
- }
-
- TEST_F(MidiInputCallbacks, mtcQuarterFrame)
- {
- mMidi.setHandleTimeCodeQuarterFrame(handleMtcQuarterFrame);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 2;
- static const byte rxData[rxSize] = { 0xf1, 12 };
- mSerial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::TimeCodeQuarterFrame);
- EXPECT_EQ(mMidi.getChannel(), 0);
- EXPECT_EQ(mMidi.getData1(), 12);
- EXPECT_EQ(mMidi.getData2(), 0);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 2);
- byte buffer[2] = { 0 };
- mSerial.mTxBuffer.read(buffer, 2);
- EXPECT_THAT(buffer, ContainerEq(rxData));
- }
-
- // --
-
- void handleSongPosition(unsigned inBeats)
- {
- EXPECT_NE(midi, nullptr);
- midi->sendSongPosition(inBeats);
- }
-
- TEST_F(MidiInputCallbacks, songPosition)
- {
- mMidi.setHandleSongPosition(handleSongPosition);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 3;
- static const byte rxData[rxSize] = { 0xf2, 12, 34 };
- mSerial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::SongPosition);
- EXPECT_EQ(mMidi.getChannel(), 0);
- EXPECT_EQ(mMidi.getData1(), 12);
- EXPECT_EQ(mMidi.getData2(), 34);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 3);
- byte buffer[3] = { 0 };
- mSerial.mTxBuffer.read(buffer, 3);
- EXPECT_THAT(buffer, ContainerEq(rxData));
- }
-
- // --
-
- void handleSongSelect(byte inSongNumber)
- {
- EXPECT_NE(midi, nullptr);
- midi->sendSongSelect(inSongNumber);
- }
-
- TEST_F(MidiInputCallbacks, songSelect)
- {
- mMidi.setHandleSongSelect(handleSongSelect);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 2;
- static const byte rxData[rxSize] = { 0xf3, 12 };
- mSerial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(mMidi.read(), false);
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::SongSelect);
- EXPECT_EQ(mMidi.getChannel(), 0);
- EXPECT_EQ(mMidi.getData1(), 12);
- EXPECT_EQ(mMidi.getData2(), 0);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 2);
- byte buffer[2] = { 0 };
- mSerial.mTxBuffer.read(buffer, 2);
- EXPECT_THAT(buffer, ContainerEq(rxData));
- }
-
- // --
-
- void handleTuneRequest()
- {
- EXPECT_NE(midi, nullptr);
- midi->sendTuneRequest();
- }
-
- TEST_F(MidiInputCallbacks, tuneRequest)
- {
- mMidi.setHandleTuneRequest(handleTuneRequest);
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- mSerial.mRxBuffer.write(0xf6);
-
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), midi::TuneRequest);
- EXPECT_EQ(mMidi.getChannel(), 0);
- EXPECT_EQ(mMidi.getData1(), 0);
- EXPECT_EQ(mMidi.getData2(), 0);
-
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 1);
- EXPECT_EQ(mSerial.mTxBuffer.read(), 0xf6);
- }
-
- // --
-
- void handleClock()
- {
- EXPECT_NE(midi, nullptr);
- midi->sendRealTime(midi::Clock);
- }
- void handleStart()
- {
- EXPECT_NE(midi, nullptr);
- midi->sendRealTime(midi::Start);
- }
- void handleContinue()
- {
- EXPECT_NE(midi, nullptr);
- midi->sendRealTime(midi::Continue);
- }
- void handleStop()
- {
- EXPECT_NE(midi, nullptr);
- midi->sendRealTime(midi::Stop);
- }
- void handleActiveSensing()
- {
- EXPECT_NE(midi, nullptr);
- midi->sendRealTime(midi::ActiveSensing);
- }
- void handleSystemReset()
- {
- EXPECT_NE(midi, nullptr);
- midi->sendRealTime(midi::SystemReset);
- }
-
- TEST_F(MidiInputCallbacks, realTime)
- {
- mMidi.setHandleClock(handleClock);
- mMidi.setHandleStart(handleStart);
- mMidi.setHandleContinue(handleContinue);
- mMidi.setHandleStop(handleStop);
- mMidi.setHandleActiveSensing(handleActiveSensing);
- mMidi.setHandleSystemReset(handleSystemReset);
-
- mMidi.begin(MIDI_CHANNEL_OMNI);
- mMidi.turnThruOff();
-
- static const unsigned rxSize = 6;
- static const byte rxData[rxSize] = {
- 0xf8, 0xfa, 0xfb, 0xfc, 0xfe, 0xff
- };
- mSerial.mRxBuffer.write(rxData, rxSize);
- static const midi::MidiType types[rxSize] = {
- midi::Clock,
- midi::Start,
- midi::Continue,
- midi::Stop,
- midi::ActiveSensing,
- midi::SystemReset,
- };
-
- for (unsigned i = 0; i < rxSize; ++i)
- {
- EXPECT_EQ(mMidi.read(), true);
- EXPECT_EQ(mMidi.getType(), types[i]);
- EXPECT_EQ(mMidi.getChannel(), 0);
- EXPECT_EQ(mMidi.getData1(), 0);
- EXPECT_EQ(mMidi.getData2(), 0);
- EXPECT_EQ(mSerial.mTxBuffer.getLength(), 1);
-
- const byte read = mSerial.mTxBuffer.read();
- EXPECT_EQ(read, rxData[i]);
- EXPECT_EQ(read, types[i]);
- }
- }
-
- END_UNNAMED_NAMESPACE
|