|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- #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
- typedef test_mocks::SerialMock<32> SerialMock;
- typedef midi::SerialMIDI<SerialMock> Transport;
- typedef midi::MidiInterface<Transport> MidiInterface;
- typedef std::vector<byte> Buffer;
-
- template<unsigned Size>
- struct VariableSysExSettings : midi::DefaultSettings
- {
- static const unsigned SysExMaxSize = Size;
- };
-
- // -----------------------------------------------------------------------------
-
- TEST(MidiThru, defaultValues)
- {
- SerialMock serial;
- Transport transport(serial);
- MidiInterface midi((Transport&)transport);
-
- EXPECT_EQ(midi.getThruState(), true);
- EXPECT_EQ(midi.getFilterMode(), midi::Thru::Full);
- midi.begin(); // Should not change the state
- EXPECT_EQ(midi.getThruState(), true);
- EXPECT_EQ(midi.getFilterMode(), midi::Thru::Full);
- }
-
- TEST(MidiThru, beginEnablesThru)
- {
- SerialMock serial;
- Transport transport(serial);
- MidiInterface midi((Transport&)transport);
-
- midi.turnThruOff();
- EXPECT_EQ(midi.getThruState(), false);
- EXPECT_EQ(midi.getFilterMode(), midi::Thru::Off);
- midi.begin();
- EXPECT_EQ(midi.getThruState(), true);
- EXPECT_EQ(midi.getFilterMode(), midi::Thru::Full);
- }
-
- TEST(MidiThru, setGet)
- {
- SerialMock serial;
- Transport transport(serial);
- MidiInterface midi((Transport&)transport);
-
- midi.turnThruOff();
- EXPECT_EQ(midi.getThruState(), false);
- EXPECT_EQ(midi.getFilterMode(), midi::Thru::Off);
-
- midi.turnThruOn();
- EXPECT_EQ(midi.getThruState(), true);
- EXPECT_EQ(midi.getFilterMode(), midi::Thru::Full);
- midi.turnThruOn(midi::Thru::SameChannel);
- EXPECT_EQ(midi.getThruState(), true);
- EXPECT_EQ(midi.getFilterMode(), midi::Thru::SameChannel);
- midi.turnThruOn(midi::Thru::DifferentChannel);
- EXPECT_EQ(midi.getThruState(), true);
- EXPECT_EQ(midi.getFilterMode(), midi::Thru::DifferentChannel);
-
- midi.setThruFilterMode(midi::Thru::Full);
- EXPECT_EQ(midi.getThruState(), true);
- EXPECT_EQ(midi.getFilterMode(), midi::Thru::Full);
- midi.setThruFilterMode(midi::Thru::SameChannel);
- EXPECT_EQ(midi.getThruState(), true);
- EXPECT_EQ(midi.getFilterMode(), midi::Thru::SameChannel);
- midi.setThruFilterMode(midi::Thru::DifferentChannel);
- EXPECT_EQ(midi.getThruState(), true);
- EXPECT_EQ(midi.getFilterMode(), midi::Thru::DifferentChannel);
- midi.setThruFilterMode(midi::Thru::Off);
- EXPECT_EQ(midi.getThruState(), false);
- EXPECT_EQ(midi.getFilterMode(), midi::Thru::Off);
- }
-
- TEST(MidiThru, off)
- {
- SerialMock serial;
- Transport transport(serial);
- MidiInterface midi((Transport&)transport);
-
- midi.begin(MIDI_CHANNEL_OMNI);
- midi.turnThruOff();
-
- static const unsigned rxSize = 5;
- static const byte rxData[rxSize] = { 0x9b, 12, 34, 56, 78 };
- serial.mRxBuffer.write(rxData, rxSize);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), true);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), true);
-
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- }
-
- TEST(MidiThru, full)
- {
- SerialMock serial;
- Transport transport(serial);
- MidiInterface midi((Transport&)transport);
-
- Buffer buffer;
-
- midi.begin(MIDI_CHANNEL_OMNI);
- midi.setThruFilterMode(midi::Thru::Full);
-
- static const unsigned rxSize = 6;
- static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
- serial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), true);
-
- buffer.clear();
- buffer.resize(3);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
- serial.mTxBuffer.read(&buffer[0], 3);
- EXPECT_THAT(buffer, ElementsAreArray({
- 0x9b, 12, 34
- }));
-
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), true);
-
- buffer.clear();
- buffer.resize(3);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
- serial.mTxBuffer.read(&buffer[0], 3);
- EXPECT_THAT(buffer, ElementsAreArray({
- 0x9c, 56, 78
- }));
- }
-
- TEST(MidiThru, sameChannel)
- {
- SerialMock serial;
- Transport transport(serial);
- MidiInterface midi((Transport&)transport);
-
- Buffer buffer;
-
- midi.begin(12);
- midi.setThruFilterMode(midi::Thru::SameChannel);
-
- static const unsigned rxSize = 6;
- static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
- serial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), true);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), false);
-
- buffer.clear();
- buffer.resize(3);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
- serial.mTxBuffer.read(&buffer[0], 3);
- EXPECT_THAT(buffer, ElementsAreArray({
- 0x9b, 12, 34
- }));
- }
-
- TEST(MidiThru, sameChannelOmni) // Acts like full
- {
- SerialMock serial;
- Transport transport(serial);
- MidiInterface midi((Transport&)transport);
-
- Buffer buffer;
-
- midi.begin(MIDI_CHANNEL_OMNI);
- midi.setThruFilterMode(midi::Thru::SameChannel);
-
- static const unsigned rxSize = 6;
- static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
- serial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), true);
-
- buffer.clear();
- buffer.resize(3);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
- serial.mTxBuffer.read(&buffer[0], 3);
- EXPECT_THAT(buffer, ElementsAreArray({
- 0x9b, 12, 34
- }));
-
- buffer.clear();
- buffer.resize(3);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), true);
-
- EXPECT_EQ(serial.mTxBuffer.getLength(), 3); // Not using TX running status
- serial.mTxBuffer.read(&buffer[0], 3);
- EXPECT_THAT(buffer, ElementsAreArray({
- 0x9c, 56, 78
- }));
- }
-
- TEST(MidiThru, differentChannel)
- {
- SerialMock serial;
- Transport transport(serial);
- MidiInterface midi((Transport&)transport);
-
- Buffer buffer;
-
- midi.begin(12);
- midi.setThruFilterMode(midi::Thru::DifferentChannel);
-
- static const unsigned rxSize = 6;
- static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
- serial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), true);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), false);
-
- buffer.clear();
- buffer.resize(3);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
- serial.mTxBuffer.read(&buffer[0], 3);
- EXPECT_THAT(buffer, ElementsAreArray({
- 0x9c, 56, 78
- }));
- }
-
- TEST(MidiThru, differentChannelOmni) // Acts like off
- {
- SerialMock serial;
- Transport transport(serial);
- MidiInterface midi((Transport&)transport);
-
- Buffer buffer;
-
- midi.begin(MIDI_CHANNEL_OMNI);
- midi.setThruFilterMode(midi::Thru::DifferentChannel);
-
- static const unsigned rxSize = 6;
- static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
- serial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), true);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
-
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), true);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- }
-
- TEST(MidiThru, multiByteThru)
- {
- typedef VariableSettings<false, false> MultiByteParsing;
- typedef midi::MidiInterface<Transport, MultiByteParsing> MultiByteMidiInterface;
-
- SerialMock serial;
- Transport transport(serial);
- MultiByteMidiInterface midi((Transport&)transport);
-
- Buffer buffer;
-
- midi.begin(MIDI_CHANNEL_OMNI);
- midi.setThruFilterMode(midi::Thru::Full);
-
- static const unsigned rxSize = 6;
- static const byte rxData[rxSize] = { 0x9b, 12, 34, 56, 78 };
- serial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(midi.read(), true);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
- EXPECT_EQ(midi.read(), true);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 6);
-
- buffer.clear();
- buffer.resize(6);
- serial.mTxBuffer.read(&buffer[0], 6);
- EXPECT_THAT(buffer, ElementsAreArray({
- 0x9b, 12, 34, 0x9b, 56, 78
- }));
- }
-
- TEST(MidiThru, withTxRunningStatus)
- {
- typedef VariableSettings<true, true> Settings;
- typedef midi::MidiInterface<Transport, Settings> RsMidiInterface;
-
- SerialMock serial;
- Transport transport(serial);
- RsMidiInterface midi((Transport&)transport);
-
- Buffer buffer;
-
- midi.begin(MIDI_CHANNEL_OMNI);
- midi.setThruFilterMode(midi::Thru::Full);
-
- static const unsigned rxSize = 5;
- static const byte rxData[rxSize] = { 0x9b, 12, 34, 56, 78 };
- serial.mRxBuffer.write(rxData, rxSize);
-
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), true);
-
- buffer.clear();
- buffer.resize(3);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 3);
- serial.mTxBuffer.read(&buffer[0], 3);
- EXPECT_THAT(buffer, ElementsAreArray({
- 0x9b, 12, 34
- }));
-
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- EXPECT_EQ(midi.read(), true);
-
- buffer.clear();
- buffer.resize(2);
- EXPECT_EQ(serial.mTxBuffer.getLength(), 2);
- serial.mTxBuffer.read(&buffer[0], 2);
- EXPECT_THAT(buffer, ElementsAreArray({
- 56, 78
- }));
- }
-
- TEST(MidiThru, invalidMode)
- {
- SerialMock serial;
- Transport transport(serial);
- MidiInterface midi((Transport&)transport);
-
- midi.begin(MIDI_CHANNEL_OMNI);
- midi.setThruFilterMode(midi::Thru::Mode(42));
-
- static const unsigned rxSize = 6;
- static const byte rxData[rxSize] = { 0x9b, 12, 34, 0x9c, 56, 78 };
- serial.mRxBuffer.write(rxData, rxSize);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), true);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), false);
- EXPECT_EQ(midi.read(), true);
-
- EXPECT_EQ(serial.mTxBuffer.getLength(), 0);
- }
-
- END_UNNAMED_NAMESPACE
|