Chromium Code Reviews| Index: media/midi/midi_manager_usb_unittest.cc |
| diff --git a/media/midi/midi_manager_usb_unittest.cc b/media/midi/midi_manager_usb_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..5c9870c1ec78355b865d381691f8af00eef6f3b9 |
| --- /dev/null |
| +++ b/media/midi/midi_manager_usb_unittest.cc |
| @@ -0,0 +1,276 @@ |
| +// Copyright 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/midi_manager_usb.h" |
| + |
| +#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::Return; |
| +using ::testing::StrictMock; |
| + |
| +template<typename T, size_t N> |
| +std::vector<T> ToVector(const T (&array)[N]) { |
| + return std::vector<T>(array, array + N); |
| +} |
| + |
| +class MockUsbMidiDevice : public UsbMidiDevice { |
| + public: |
| + virtual ~MockUsbMidiDevice() {} |
| + |
| + MOCK_METHOD1(RequestPermission, |
| + void(base::Callback<void(bool result)> callback)); |
| + MOCK_METHOD0(GetDescriptor, std::vector<uint8>()); |
| + MOCK_METHOD2(Send, void(int, const std::vector<uint8>&)); |
| +}; |
| + |
| +class MockMIDIManagerClient : public MIDIManagerClient { |
| + public: |
| + virtual ~MockMIDIManagerClient() {} |
| + |
| + // Define ReceiveMIDIData with vector parameter for the ease of |
| + // test expectation description. |
| + MOCK_METHOD3(ReceiveMIDIData, |
| + void(uint32, const std::vector<uint8>&, double)); |
| + |
| + virtual void ReceiveMIDIData(uint32 port_index, |
| + const uint8* data, |
| + size_t size, |
| + double timestamp) OVERRIDE { |
| + ReceiveMIDIData(port_index, |
| + std::vector<uint8>(data, data + size), |
| + timestamp); |
| + } |
| + |
| + MOCK_METHOD1(AccumulateMIDIBytesSent, void(size_t)); |
| +}; |
| + |
| +class TestUsbMidiDeviceFactory : public UsbMidiDevice::Factory { |
| + public: |
| + TestUsbMidiDeviceFactory() {} |
| + virtual ~TestUsbMidiDeviceFactory() {} |
| + virtual void EnumerateDevices(UsbMidiDeviceDelegate* device, |
| + Callback callback) OVERRIDE { |
| + callback_ = callback; |
| + } |
| + |
| + Callback callback_; |
| +}; |
| + |
| +class MIDIManagerUsbTest : public ::testing::Test { |
| + public: |
| + MIDIManagerUsbTest() : is_initialized_(false), is_well_initialized_(false) { |
| + scoped_ptr<TestUsbMidiDeviceFactory> factory(new TestUsbMidiDeviceFactory); |
| + factory_ = factory.get(); |
| + manager_.reset( |
| + new MIDIManagerUsb(factory.PassAs<UsbMidiDevice::Factory>())); |
| + } |
| + |
| + protected: |
| + void Initialize() { |
| + manager_->Initialize(base::Bind(&MIDIManagerUsbTest::OnInitializeDone, |
| + base::Unretained(this))); |
| + } |
| + |
| + void OnInitializeDone(bool result) { |
| + is_initialized_ = true; |
| + is_well_initialized_ = result; |
| + } |
| + |
| + bool is_initialized_; |
| + bool is_well_initialized_; |
| + |
| + scoped_ptr<MIDIManagerUsb> manager_; |
| + // Owned by manager_. |
| + TestUsbMidiDeviceFactory* factory_; |
| +}; |
| + |
| + |
| +TEST_F(MIDIManagerUsbTest, Initialize) { |
| + scoped_ptr<MockUsbMidiDevice> device(new StrictMock<MockUsbMidiDevice>); |
| + uint8 descriptor[] = { |
| + 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, |
| + 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, |
| + 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, |
| + 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00, |
| + 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02, |
| + 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, |
| + 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, |
| + 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, |
| + 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, |
| + 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03, |
| + 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02, |
| + 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, |
| + 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, |
| + 0x05, 0x25, 0x01, 0x01, 0x07, |
| + }; |
| + { |
| + InSequence s; |
| + EXPECT_CALL(*device, GetDescriptor()) |
| + .WillOnce(Return(ToVector(descriptor))); |
| + } |
| + |
| + Initialize(); |
| + ScopedVector<UsbMidiDevice> devices; |
| + devices.push_back(device.release()); |
| + EXPECT_FALSE(is_initialized_); |
| + factory_->callback_.Run(true, &devices); |
| + EXPECT_TRUE(is_initialized_); |
| + EXPECT_TRUE(is_well_initialized_); |
| + |
| + ASSERT_EQ(1u, manager_->input_ports().size()); |
| + ASSERT_EQ(2u, manager_->output_ports().size()); |
| + ASSERT_TRUE(manager_->input_stream()); |
| + std::vector<UsbMidiJack> input_jacks = manager_->input_stream()->jacks(); |
| + ASSERT_EQ(1u, input_jacks.size()); |
| + ASSERT_EQ(2u, manager_->output_streams().size()); |
| + EXPECT_EQ(7u, input_jacks[0].jack_id); |
| + EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id); |
| + EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id); |
| + EXPECT_EQ(2u, input_jacks[0].endpoint_number()); |
| +} |
| + |
| +TEST_F(MIDIManagerUsbTest, InitializeFail) { |
| + Initialize(); |
| + |
| + EXPECT_FALSE(is_initialized_); |
| + factory_->callback_.Run(false, NULL); |
| + EXPECT_TRUE(is_initialized_); |
| + EXPECT_FALSE(is_well_initialized_); |
| +} |
| + |
| +TEST_F(MIDIManagerUsbTest, InitializeFailBecauseOfInvalidDescriptor) { |
| + scoped_ptr<MockUsbMidiDevice> device(new StrictMock<MockUsbMidiDevice>); |
| + uint8 descriptor[] = {0x04}; |
| + { |
| + InSequence s; |
| + EXPECT_CALL(*device, GetDescriptor()) |
| + .WillOnce(Return(ToVector(descriptor))); |
| + } |
| + |
| + Initialize(); |
| + ScopedVector<UsbMidiDevice> devices; |
| + devices.push_back(device.release()); |
| + EXPECT_FALSE(is_initialized_); |
| + factory_->callback_.Run(true, &devices); |
| + EXPECT_TRUE(is_initialized_); |
| + EXPECT_FALSE(is_well_initialized_); |
| +} |
| + |
| +TEST_F(MIDIManagerUsbTest, Send) { |
| + scoped_ptr<MockUsbMidiDevice> device(new StrictMock<MockUsbMidiDevice>); |
| + uint8 descriptor[] = { |
| + 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, |
| + 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, |
| + 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, |
| + 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00, |
| + 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02, |
| + 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, |
| + 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, |
| + 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, |
| + 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, |
| + 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03, |
| + 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02, |
| + 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, |
| + 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, |
| + 0x05, 0x25, 0x01, 0x01, 0x07, |
| + }; |
| + |
|
Takashi Toyoshima
2014/01/15 12:25:49
remove one empty line?
yhirano
2014/01/23 04:19:11
Done.
|
| + |
| + uint8 data[] = { |
| + 0x90, 0x45, 0x7f, |
| + 0xf0, 0x00, 0x01, 0xf7, |
| + }; |
| + uint8 expected_data[] = { |
| + 0x19, 0x90, 0x45, 0x7f, |
| + 0x14, 0xf0, 0x00, 0x01, |
| + 0x15, 0xf7, 0x00, 0x00, |
| + }; |
| + |
| + { |
| + InSequence s; |
| + EXPECT_CALL(*device, GetDescriptor()) |
| + .WillOnce(Return(ToVector(descriptor))); |
| + EXPECT_CALL(*device, Send(2, ToVector(expected_data))); |
| + } |
| + |
| + Initialize(); |
| + ScopedVector<UsbMidiDevice> devices; |
| + devices.push_back(device.release()); |
| + EXPECT_FALSE(is_initialized_); |
| + factory_->callback_.Run(true, &devices); |
| + ASSERT_TRUE(is_initialized_); |
| + ASSERT_TRUE(is_well_initialized_); |
| + |
| + manager_->DispatchSendMIDIData(NULL, 1, ToVector(data), 0); |
| +} |
| + |
| +TEST_F(MIDIManagerUsbTest, Receive) { |
| + scoped_ptr<MockUsbMidiDevice> device(new StrictMock<MockUsbMidiDevice>); |
| + scoped_ptr<MockMIDIManagerClient> client( |
| + new StrictMock<MockMIDIManagerClient>); |
| + uint8 descriptor[] = { |
| + 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, |
| + 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, |
| + 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, |
| + 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00, |
| + 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02, |
| + 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, |
| + 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, |
| + 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, |
| + 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, |
| + 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03, |
| + 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02, |
| + 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, |
| + 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, |
| + 0x05, 0x25, 0x01, 0x01, 0x07, |
| + }; |
| + |
| + uint8 data[] = { |
| + 0x09, 0x90, 0x45, 0x7f, |
| + 0x04, 0xf0, 0x00, 0x01, |
| + 0x49, 0x90, 0x88, 0x99, // This data should be ignored (CN = 4). |
| + 0x05, 0xf7, 0x00, 0x00, |
| + }; |
| + uint8 expected1[] = { |
| + 0x90, 0x45, 0x7f, |
| + }; |
| + uint8 expected2[] = { |
| + 0xf0, 0x00, 0x01, |
| + }; |
| + uint8 expected3[] = { |
| + 0xf7, |
| + }; |
| + { |
| + InSequence s; |
| + EXPECT_CALL(*device, GetDescriptor()) |
| + .WillOnce(Return(ToVector(descriptor))); |
| + EXPECT_CALL(*client, ReceiveMIDIData(0, ToVector(expected1), 0)).Times(1); |
| + EXPECT_CALL(*client, ReceiveMIDIData(0, ToVector(expected2), 0)).Times(1); |
| + EXPECT_CALL(*client, ReceiveMIDIData(0, ToVector(expected3), 0)).Times(1); |
| + } |
| + |
| + Initialize(); |
| + ScopedVector<UsbMidiDevice> devices; |
| + UsbMidiDevice* device_raw = device.get(); |
| + devices.push_back(device.release()); |
| + EXPECT_FALSE(is_initialized_); |
| + factory_->callback_.Run(true, &devices); |
| + ASSERT_TRUE(is_initialized_); |
| + ASSERT_TRUE(is_well_initialized_); |
| + |
| + manager_->StartSession(client.get()); |
| + manager_->ReceiveUsbMidiData(device_raw, 2, data, arraysize(data), 0); |
| + manager_->EndSession(client.get()); |
| +} |
| + |
| +} // namespace |
| + |
| +} // namespace media |