Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "media/midi/midi_manager_usb.h" | |
| 6 | |
| 7 #include "media/midi/usb_midi_device.h" | |
| 8 #include "testing/gmock/include/gmock/gmock.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace media { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 using ::testing::InSequence; | |
| 16 using ::testing::Return; | |
| 17 using ::testing::StrictMock; | |
| 18 | |
| 19 template<typename T, size_t N> | |
| 20 std::vector<T> ToVector(const T (&array)[N]) { | |
| 21 return std::vector<T>(array, array + N); | |
| 22 } | |
| 23 | |
| 24 class MockUsbMidiDevice : public UsbMidiDevice { | |
| 25 public: | |
| 26 virtual ~MockUsbMidiDevice() {} | |
| 27 | |
| 28 MOCK_METHOD1(RequestPermission, | |
| 29 void(base::Callback<void(bool result)> callback)); | |
| 30 MOCK_METHOD0(GetDescriptor, std::vector<uint8>()); | |
| 31 MOCK_METHOD2(Send, void(int, const std::vector<uint8>&)); | |
| 32 }; | |
| 33 | |
| 34 class MockMIDIManagerClient : public MIDIManagerClient { | |
| 35 public: | |
| 36 virtual ~MockMIDIManagerClient() {} | |
| 37 | |
| 38 // Define ReceiveMIDIData with vector parameter for the ease of | |
| 39 // test expectation description. | |
| 40 MOCK_METHOD3(ReceiveMIDIData, | |
| 41 void(uint32, const std::vector<uint8>&, double)); | |
| 42 | |
| 43 virtual void ReceiveMIDIData(uint32 port_index, | |
| 44 const uint8* data, | |
| 45 size_t size, | |
| 46 double timestamp) OVERRIDE { | |
| 47 ReceiveMIDIData(port_index, | |
| 48 std::vector<uint8>(data, data + size), | |
| 49 timestamp); | |
| 50 } | |
| 51 | |
| 52 MOCK_METHOD1(AccumulateMIDIBytesSent, void(size_t)); | |
| 53 }; | |
| 54 | |
| 55 class TestUsbMidiDeviceFactory : public UsbMidiDevice::Factory { | |
| 56 public: | |
| 57 TestUsbMidiDeviceFactory() {} | |
| 58 virtual ~TestUsbMidiDeviceFactory() {} | |
| 59 virtual void EnumerateDevices(UsbMidiDeviceDelegate* device, | |
| 60 Callback callback) OVERRIDE { | |
| 61 callback_ = callback; | |
| 62 } | |
| 63 | |
| 64 Callback callback_; | |
| 65 }; | |
| 66 | |
| 67 class MIDIManagerUsbTest : public ::testing::Test { | |
| 68 public: | |
| 69 MIDIManagerUsbTest() : is_initialized_(false), is_well_initialized_(false) { | |
| 70 scoped_ptr<TestUsbMidiDeviceFactory> factory(new TestUsbMidiDeviceFactory); | |
| 71 factory_ = factory.get(); | |
| 72 manager_.reset( | |
| 73 new MIDIManagerUsb(factory.PassAs<UsbMidiDevice::Factory>())); | |
| 74 } | |
| 75 | |
| 76 protected: | |
| 77 void Initialize() { | |
| 78 manager_->Initialize(base::Bind(&MIDIManagerUsbTest::OnInitializeDone, | |
| 79 base::Unretained(this))); | |
| 80 } | |
| 81 | |
| 82 void OnInitializeDone(bool result) { | |
| 83 is_initialized_ = true; | |
| 84 is_well_initialized_ = result; | |
| 85 } | |
| 86 | |
| 87 bool is_initialized_; | |
| 88 bool is_well_initialized_; | |
| 89 | |
| 90 scoped_ptr<MIDIManagerUsb> manager_; | |
| 91 // Owned by manager_. | |
| 92 TestUsbMidiDeviceFactory* factory_; | |
| 93 }; | |
| 94 | |
| 95 | |
| 96 TEST_F(MIDIManagerUsbTest, Initialize) { | |
| 97 scoped_ptr<MockUsbMidiDevice> device(new StrictMock<MockUsbMidiDevice>); | |
| 98 uint8 descriptor[] = { | |
| 99 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, | |
| 100 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, | |
| 101 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, | |
| 102 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00, | |
| 103 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02, | |
| 104 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, | |
| 105 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, | |
| 106 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, | |
| 107 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, | |
| 108 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03, | |
| 109 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02, | |
| 110 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, | |
| 111 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, | |
| 112 0x05, 0x25, 0x01, 0x01, 0x07, | |
| 113 }; | |
| 114 { | |
| 115 InSequence s; | |
| 116 EXPECT_CALL(*device, GetDescriptor()) | |
| 117 .WillOnce(Return(ToVector(descriptor))); | |
| 118 } | |
| 119 | |
| 120 Initialize(); | |
| 121 ScopedVector<UsbMidiDevice> devices; | |
| 122 devices.push_back(device.release()); | |
| 123 EXPECT_FALSE(is_initialized_); | |
| 124 factory_->callback_.Run(true, &devices); | |
| 125 EXPECT_TRUE(is_initialized_); | |
| 126 EXPECT_TRUE(is_well_initialized_); | |
| 127 | |
| 128 ASSERT_EQ(1u, manager_->input_ports().size()); | |
| 129 ASSERT_EQ(2u, manager_->output_ports().size()); | |
| 130 ASSERT_TRUE(manager_->input_stream()); | |
| 131 std::vector<UsbMidiJack> input_jacks = manager_->input_stream()->jacks(); | |
| 132 ASSERT_EQ(1u, input_jacks.size()); | |
| 133 ASSERT_EQ(2u, manager_->output_streams().size()); | |
| 134 EXPECT_EQ(7u, input_jacks[0].jack_id); | |
| 135 EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id); | |
| 136 EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id); | |
| 137 EXPECT_EQ(2u, input_jacks[0].endpoint_number()); | |
| 138 } | |
| 139 | |
| 140 TEST_F(MIDIManagerUsbTest, InitializeFail) { | |
| 141 Initialize(); | |
| 142 | |
| 143 EXPECT_FALSE(is_initialized_); | |
| 144 factory_->callback_.Run(false, NULL); | |
| 145 EXPECT_TRUE(is_initialized_); | |
| 146 EXPECT_FALSE(is_well_initialized_); | |
| 147 } | |
| 148 | |
| 149 TEST_F(MIDIManagerUsbTest, InitializeFailBecauseOfInvalidDescriptor) { | |
| 150 scoped_ptr<MockUsbMidiDevice> device(new StrictMock<MockUsbMidiDevice>); | |
| 151 uint8 descriptor[] = {0x04}; | |
| 152 { | |
| 153 InSequence s; | |
| 154 EXPECT_CALL(*device, GetDescriptor()) | |
| 155 .WillOnce(Return(ToVector(descriptor))); | |
| 156 } | |
| 157 | |
| 158 Initialize(); | |
| 159 ScopedVector<UsbMidiDevice> devices; | |
| 160 devices.push_back(device.release()); | |
| 161 EXPECT_FALSE(is_initialized_); | |
| 162 factory_->callback_.Run(true, &devices); | |
| 163 EXPECT_TRUE(is_initialized_); | |
| 164 EXPECT_FALSE(is_well_initialized_); | |
| 165 } | |
| 166 | |
| 167 TEST_F(MIDIManagerUsbTest, Send) { | |
| 168 scoped_ptr<MockUsbMidiDevice> device(new StrictMock<MockUsbMidiDevice>); | |
| 169 uint8 descriptor[] = { | |
| 170 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, | |
| 171 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, | |
| 172 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, | |
| 173 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00, | |
| 174 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02, | |
| 175 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, | |
| 176 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, | |
| 177 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, | |
| 178 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, | |
| 179 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03, | |
| 180 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02, | |
| 181 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, | |
| 182 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, | |
| 183 0x05, 0x25, 0x01, 0x01, 0x07, | |
| 184 }; | |
| 185 | |
|
Takashi Toyoshima
2014/01/15 12:25:49
remove one empty line?
yhirano
2014/01/23 04:19:11
Done.
| |
| 186 | |
| 187 uint8 data[] = { | |
| 188 0x90, 0x45, 0x7f, | |
| 189 0xf0, 0x00, 0x01, 0xf7, | |
| 190 }; | |
| 191 uint8 expected_data[] = { | |
| 192 0x19, 0x90, 0x45, 0x7f, | |
| 193 0x14, 0xf0, 0x00, 0x01, | |
| 194 0x15, 0xf7, 0x00, 0x00, | |
| 195 }; | |
| 196 | |
| 197 { | |
| 198 InSequence s; | |
| 199 EXPECT_CALL(*device, GetDescriptor()) | |
| 200 .WillOnce(Return(ToVector(descriptor))); | |
| 201 EXPECT_CALL(*device, Send(2, ToVector(expected_data))); | |
| 202 } | |
| 203 | |
| 204 Initialize(); | |
| 205 ScopedVector<UsbMidiDevice> devices; | |
| 206 devices.push_back(device.release()); | |
| 207 EXPECT_FALSE(is_initialized_); | |
| 208 factory_->callback_.Run(true, &devices); | |
| 209 ASSERT_TRUE(is_initialized_); | |
| 210 ASSERT_TRUE(is_well_initialized_); | |
| 211 | |
| 212 manager_->DispatchSendMIDIData(NULL, 1, ToVector(data), 0); | |
| 213 } | |
| 214 | |
| 215 TEST_F(MIDIManagerUsbTest, Receive) { | |
| 216 scoped_ptr<MockUsbMidiDevice> device(new StrictMock<MockUsbMidiDevice>); | |
| 217 scoped_ptr<MockMIDIManagerClient> client( | |
| 218 new StrictMock<MockMIDIManagerClient>); | |
| 219 uint8 descriptor[] = { | |
| 220 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, | |
| 221 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, | |
| 222 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, | |
| 223 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00, | |
| 224 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02, | |
| 225 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, | |
| 226 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, | |
| 227 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, | |
| 228 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, | |
| 229 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03, | |
| 230 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02, | |
| 231 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, | |
| 232 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, | |
| 233 0x05, 0x25, 0x01, 0x01, 0x07, | |
| 234 }; | |
| 235 | |
| 236 uint8 data[] = { | |
| 237 0x09, 0x90, 0x45, 0x7f, | |
| 238 0x04, 0xf0, 0x00, 0x01, | |
| 239 0x49, 0x90, 0x88, 0x99, // This data should be ignored (CN = 4). | |
| 240 0x05, 0xf7, 0x00, 0x00, | |
| 241 }; | |
| 242 uint8 expected1[] = { | |
| 243 0x90, 0x45, 0x7f, | |
| 244 }; | |
| 245 uint8 expected2[] = { | |
| 246 0xf0, 0x00, 0x01, | |
| 247 }; | |
| 248 uint8 expected3[] = { | |
| 249 0xf7, | |
| 250 }; | |
| 251 { | |
| 252 InSequence s; | |
| 253 EXPECT_CALL(*device, GetDescriptor()) | |
| 254 .WillOnce(Return(ToVector(descriptor))); | |
| 255 EXPECT_CALL(*client, ReceiveMIDIData(0, ToVector(expected1), 0)).Times(1); | |
| 256 EXPECT_CALL(*client, ReceiveMIDIData(0, ToVector(expected2), 0)).Times(1); | |
| 257 EXPECT_CALL(*client, ReceiveMIDIData(0, ToVector(expected3), 0)).Times(1); | |
| 258 } | |
| 259 | |
| 260 Initialize(); | |
| 261 ScopedVector<UsbMidiDevice> devices; | |
| 262 UsbMidiDevice* device_raw = device.get(); | |
| 263 devices.push_back(device.release()); | |
| 264 EXPECT_FALSE(is_initialized_); | |
| 265 factory_->callback_.Run(true, &devices); | |
| 266 ASSERT_TRUE(is_initialized_); | |
| 267 ASSERT_TRUE(is_well_initialized_); | |
| 268 | |
| 269 manager_->StartSession(client.get()); | |
| 270 manager_->ReceiveUsbMidiData(device_raw, 2, data, arraysize(data), 0); | |
| 271 manager_->EndSession(client.get()); | |
| 272 } | |
| 273 | |
| 274 } // namespace | |
| 275 | |
| 276 } // namespace media | |
| OLD | NEW |