Chromium Code Reviews| Index: media/midi/usb_midi_input_stream_unittest.cc |
| diff --git a/media/midi/usb_midi_input_stream_unittest.cc b/media/midi/usb_midi_input_stream_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..666ff0387325c0ee9d31243946117087677d5f72 |
| --- /dev/null |
| +++ b/media/midi/usb_midi_input_stream_unittest.cc |
| @@ -0,0 +1,261 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "media/midi/usb_midi_input_stream.h" |
| + |
| +#include <vector> |
| + |
| +#include "media/midi/usb_midi_device.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace media { |
| + |
| +namespace { |
| + |
| +using ::testing::InSequence; |
| +using ::testing::StrictMock; |
| + |
| +typedef ::testing::MockFunction<void(int)> Checkpoint; // NOLINT |
|
Takashi Toyoshima
2013/12/24 04:19:32
Can you elaborate why we need to use NOLINT annota
yhirano
2013/12/24 05:57:01
Without this, cpplint complains like this:
All par
Takashi Toyoshima
2013/12/24 06:47:18
So maybe you can move this typedef inside UsbMidiI
|
| + |
| +template<typename T, size_t N> |
| +std::vector<T> ToVector(const T((&array)[N])) { |
| + return std::vector<T>(array, array + N); |
| +} |
| + |
| +class TestUsbMidiDevice : public UsbMidiDevice { |
| + public: |
| + TestUsbMidiDevice() {} |
| + virtual ~TestUsbMidiDevice() {} |
| + virtual std::vector<uint8> GetDescriptor() OVERRIDE { |
| + return std::vector<uint8>(); |
| + } |
| + virtual void Send(uint8 endpoint_number, |
| + const std::vector<uint8>& data) OVERRIDE {} |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDevice); |
| +}; |
| + |
| +class MockDelegate : public UsbMidiInputStream::Delegate { |
| + public: |
| + MockDelegate() {} |
| + virtual ~MockDelegate() {} |
| + virtual void OnReceivedData(size_t jack_index, |
| + const uint8* data, |
| + size_t size, |
| + double timestamp) OVERRIDE { |
| + // In order to use gmock parameter matching, |
| + // we define and use OnReceiveData with std::vector<uint8>. |
| + OnReceivedData(jack_index, |
| + std::vector<uint8>(data, data + size), |
| + timestamp); |
| + } |
| + |
| + MOCK_METHOD3(OnReceivedData, void(size_t, |
| + const std::vector<uint8>&, |
| + double)); |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(MockDelegate); |
| +}; |
| + |
| +class UsbMidiInputStreamTest : public ::testing::Test { |
| + protected: |
| + |
| + UsbMidiInputStreamTest() |
| + : device1_(new TestUsbMidiDevice), |
| + device2_(new TestUsbMidiDevice), |
| + delegate_(new StrictMock<MockDelegate>) { |
| + std::vector<UsbMidiJack> jacks; |
| + |
| + jacks.push_back(UsbMidiJack(device1_.get(), |
| + 84, // jack_id |
| + 4, // cable_number |
| + 135)); // endpoint_address |
| + jacks.push_back(UsbMidiJack(device2_.get(), |
| + 85, |
| + 5, |
| + 137)); |
| + jacks.push_back(UsbMidiJack(device2_.get(), |
| + 84, |
| + 4, |
| + 135)); |
| + jacks.push_back(UsbMidiJack(device1_.get(), |
| + 85, |
| + 5, |
| + 135)); |
| + |
| + stream_.reset(new UsbMidiInputStream(jacks, delegate_.get())); |
| + } |
| + |
| + scoped_ptr<TestUsbMidiDevice> device1_; |
| + scoped_ptr<TestUsbMidiDevice> device2_; |
| + scoped_ptr<MockDelegate> delegate_; |
| + scoped_ptr<UsbMidiInputStream> stream_; |
|
Takashi Toyoshima
2013/12/24 04:19:32
DISALLOW_... ?
yhirano
2013/12/24 05:57:01
Done.
|
| +}; |
| + |
| +TEST_F(UsbMidiInputStreamTest, UnknownMessage) { |
| + uint8 data[] = { |
| + 0x40, 0xff, 0xff, 0xff, |
| + 0x41, 0xff, 0xff, 0xff, |
| + }; |
| + stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0); |
| +} |
| + |
| +TEST_F(UsbMidiInputStreamTest, SystemCommonMessage) { |
| + uint8 data[] = { |
| + 0x45, 0xf8, 0x00, 0x00, |
| + 0x42, 0xf3, 0x22, 0x00, |
| + 0x43, 0xf2, 0x33, 0x44, |
| + }; |
| + uint8 expected1[] = { 0xf8, }; |
| + uint8 expected2[] = { 0xf3, 0x22, }; |
| + uint8 expected3[] = { 0xf2, 0x33, 0x44, }; |
| + |
| + { |
| + InSequence s; |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0)); |
| + } |
| + |
| + stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0); |
| +} |
| + |
| +TEST_F(UsbMidiInputStreamTest, SystemExclusiveMessage) { |
| + uint8 data[] = { |
| + 0x44, 0xf0, 0x11, 0x22, |
| + 0x45, 0xf7, 0x00, 0x00, |
| + 0x46, 0xf0, 0xf7, 0x00, |
| + 0x47, 0xf0, 0x33, 0xf7, |
| + }; |
| + uint8 expected1[] = { 0xf0, 0x11, 0x22, }; |
| + uint8 expected2[] = { 0xf7, }; |
| + uint8 expected3[] = { 0xf0, 0xf7, }; |
| + uint8 expected4[] = { 0xf0, 0x33, 0xf7, }; |
| + |
| + { |
| + InSequence s; |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected4), 0)); |
| + } |
| + |
| + stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0); |
| +} |
| + |
| +TEST_F(UsbMidiInputStreamTest, ChannelMessage) { |
| + uint8 data[] = { |
| + 0x48, 0x80, 0x11, 0x22, |
| + 0x49, 0x90, 0x33, 0x44, |
| + 0x4a, 0xa0, 0x55, 0x66, |
| + 0x4b, 0xb0, 0x77, 0x88, |
| + 0x4c, 0xc0, 0x99, 0x00, |
| + 0x4d, 0xd0, 0xaa, 0x00, |
| + 0x4e, 0xe0, 0xbb, 0xcc, |
| + }; |
| + uint8 expected1[] = { 0x80, 0x11, 0x22, }; |
| + uint8 expected2[] = { 0x90, 0x33, 0x44, }; |
| + uint8 expected3[] = { 0xa0, 0x55, 0x66, }; |
| + uint8 expected4[] = { 0xb0, 0x77, 0x88, }; |
| + uint8 expected5[] = { 0xc0, 0x99, }; |
| + uint8 expected6[] = { 0xd0, 0xaa, }; |
| + uint8 expected7[] = { 0xe0, 0xbb, 0xcc, }; |
| + |
| + { |
| + InSequence s; |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected4), 0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected5), 0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected6), 0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected7), 0)); |
| + } |
| + |
| + stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0); |
| +} |
| + |
| +TEST_F(UsbMidiInputStreamTest, SingleByteMessage) { |
| + uint8 data[] = { |
| + 0x4f, 0xf8, 0x00, 0x00, |
| + }; |
| + |
| + uint8 expected[] = { 0xf8, }; |
| + |
| + { |
| + InSequence s; |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected), 0)); |
| + } |
| + |
| + stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0); |
| +} |
| + |
| +TEST_F(UsbMidiInputStreamTest, DispatchForMultipleCables) { |
| + uint8 data[] = { |
| + 0x4f, 0xf8, 0x00, 0x00, |
| + 0x5f, 0xfa, 0x00, 0x00, |
| + 0x6f, 0xfb, 0x00, 0x00, |
| + }; |
| + uint8 expected1[] = { 0xf8, }; |
| + uint8 expected2[] = { 0xfa, }; |
| + |
| + { |
| + InSequence s; |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 99)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(3, ToVector(expected2), 99)); |
| + } |
| + |
| + stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 99); |
| +} |
| + |
| +TEST_F(UsbMidiInputStreamTest, DispatchForDevice2) { |
| + uint8 data[] = { 0x4f, 0xf8, 0x00, 0x00 }; |
| + uint8 expected[] = { 0xf8, }; |
| + |
| + { |
| + InSequence s; |
| + EXPECT_CALL(*delegate_, OnReceivedData(2, ToVector(expected), 99)); |
| + } |
| + |
| + stream_->OnReceivedData(device2_.get(), 7, data, arraysize(data), 99); |
| +} |
| + |
| +TEST_F(UsbMidiInputStreamTest, PendingData) { |
| + uint8 data1[] = { 0x4f, 0xf8, }; |
| + uint8 data2[] = { |
| + 0x00, 0x00, |
| + 0x5f, 0xfa, 0x00, 0x00, |
| + 0x5f, 0xfb, 0x00, |
| + }; |
| + uint8 data3[] = { |
| + 0x00, |
| + 0x4f, 0xfa, |
| + }; |
| + uint8 expected1[] = { 0xf8, }; |
| + uint8 expected2[] = { 0xfa, }; |
| + uint8 expected3[] = { 0xfb, }; |
| + Checkpoint checkpoint; |
| + |
| + { |
| + InSequence s; |
| + EXPECT_CALL(checkpoint, Call(0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(3, ToVector(expected2), 0)); |
| + EXPECT_CALL(checkpoint, Call(1)); |
| + EXPECT_CALL(*delegate_, OnReceivedData(3, ToVector(expected3), 0)); |
| + } |
| + |
| + stream_->OnReceivedData(device1_.get(), 7, data1, arraysize(data1), 0); |
| + checkpoint.Call(0); |
| + stream_->OnReceivedData(device1_.get(), 7, data2, arraysize(data2), 0); |
| + checkpoint.Call(1); |
| + stream_->OnReceivedData(device1_.get(), 7, data3, arraysize(data3), 0); |
| +} |
| + |
| +} // namespace |
| + |
| +} // namespace media |