This sets up API to release OutputSurface from LTHClient.
[chromium-blink-merge.git] / media / midi / midi_manager_usb_unittest.cc
blobe2205aa3b4640fabf673d71e8de9d9d456e63e93
1 // Copyright 2014 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.
5 #include "media/midi/midi_manager_usb.h"
7 #include <string>
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/time/time.h"
13 #include "media/midi/usb_midi_device.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace media {
17 namespace midi {
19 namespace {
21 template<typename T, size_t N>
22 std::vector<T> ToVector(const T (&array)[N]) {
23 return std::vector<T>(array, array + N);
26 class Logger {
27 public:
28 Logger() {}
29 ~Logger() {}
31 void AddLog(const std::string& message) { log_ += message; }
32 std::string TakeLog() {
33 std::string result;
34 result.swap(log_);
35 return result;
38 private:
39 std::string log_;
41 DISALLOW_COPY_AND_ASSIGN(Logger);
44 class FakeUsbMidiDevice : public UsbMidiDevice {
45 public:
46 explicit FakeUsbMidiDevice(Logger* logger) : logger_(logger) {}
47 ~FakeUsbMidiDevice() override {}
49 std::vector<uint8> GetDescriptors() override {
50 logger_->AddLog("UsbMidiDevice::GetDescriptors\n");
51 return descriptors_;
54 std::string GetManufacturer() override { return manufacturer_; }
55 std::string GetProductName() override { return product_name_; }
56 std::string GetDeviceVersion() override { return device_version_; }
58 void Send(int endpoint_number, const std::vector<uint8>& data) override {
59 logger_->AddLog("UsbMidiDevice::Send ");
60 logger_->AddLog(base::StringPrintf("endpoint = %d data =",
61 endpoint_number));
62 for (size_t i = 0; i < data.size(); ++i)
63 logger_->AddLog(base::StringPrintf(" 0x%02x", data[i]));
64 logger_->AddLog("\n");
67 void SetDescriptors(const std::vector<uint8> descriptors) {
68 descriptors_ = descriptors;
70 void SetManufacturer(const std::string& manufacturer) {
71 manufacturer_ = manufacturer;
73 void SetProductName(const std::string& product_name) {
74 product_name_ = product_name;
76 void SetDeviceVersion(const std::string& device_version) {
77 device_version_ = device_version;
80 private:
81 std::vector<uint8> descriptors_;
82 std::string manufacturer_;
83 std::string product_name_;
84 std::string device_version_;
85 Logger* logger_;
87 DISALLOW_COPY_AND_ASSIGN(FakeUsbMidiDevice);
90 class FakeMidiManagerClient : public MidiManagerClient {
91 public:
92 explicit FakeMidiManagerClient(Logger* logger)
93 : complete_start_session_(false),
94 result_(Result::NOT_SUPPORTED),
95 logger_(logger) {}
96 ~FakeMidiManagerClient() override {}
98 void AddInputPort(const MidiPortInfo& info) override {
99 input_ports_.push_back(info);
102 void AddOutputPort(const MidiPortInfo& info) override {
103 output_ports_.push_back(info);
106 void SetInputPortState(uint32 port_index, MidiPortState state) override {}
108 void SetOutputPortState(uint32 port_index, MidiPortState state) override {}
110 void CompleteStartSession(Result result) override {
111 complete_start_session_ = true;
112 result_ = result;
115 void ReceiveMidiData(uint32 port_index,
116 const uint8* data,
117 size_t size,
118 double timestamp) override {
119 logger_->AddLog("MidiManagerClient::ReceiveMidiData ");
120 logger_->AddLog(
121 base::StringPrintf("usb:port_index = %d data =", port_index));
122 for (size_t i = 0; i < size; ++i)
123 logger_->AddLog(base::StringPrintf(" 0x%02x", data[i]));
124 logger_->AddLog("\n");
127 void AccumulateMidiBytesSent(size_t size) override {
128 logger_->AddLog("MidiManagerClient::AccumulateMidiBytesSent ");
129 // Windows has no "%zu".
130 logger_->AddLog(base::StringPrintf("size = %u\n",
131 static_cast<unsigned>(size)));
134 bool complete_start_session_;
135 Result result_;
136 MidiPortInfoList input_ports_;
137 MidiPortInfoList output_ports_;
139 private:
140 Logger* logger_;
142 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
145 class TestUsbMidiDeviceFactory : public UsbMidiDevice::Factory {
146 public:
147 TestUsbMidiDeviceFactory() {}
148 ~TestUsbMidiDeviceFactory() override {}
149 void EnumerateDevices(UsbMidiDeviceDelegate* device,
150 Callback callback) override {
151 callback_ = callback;
154 Callback callback_;
156 private:
157 DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDeviceFactory);
160 class MidiManagerUsbForTesting : public MidiManagerUsb {
161 public:
162 explicit MidiManagerUsbForTesting(
163 scoped_ptr<UsbMidiDevice::Factory> device_factory)
164 : MidiManagerUsb(device_factory.Pass()) {}
165 ~MidiManagerUsbForTesting() override {}
167 void CallCompleteInitialization(Result result) {
168 CompleteInitialization(result);
169 base::RunLoop run_loop;
170 run_loop.RunUntilIdle();
173 private:
174 DISALLOW_COPY_AND_ASSIGN(MidiManagerUsbForTesting);
177 class MidiManagerUsbTest : public ::testing::Test {
178 public:
179 MidiManagerUsbTest() : message_loop_(new base::MessageLoop) {
180 scoped_ptr<TestUsbMidiDeviceFactory> factory(new TestUsbMidiDeviceFactory);
181 factory_ = factory.get();
182 manager_.reset(new MidiManagerUsbForTesting(factory.Pass()));
184 ~MidiManagerUsbTest() override {
185 std::string leftover_logs = logger_.TakeLog();
186 if (!leftover_logs.empty()) {
187 ADD_FAILURE() << "Log should be empty: " << leftover_logs;
191 protected:
192 void Initialize() {
193 client_.reset(new FakeMidiManagerClient(&logger_));
194 manager_->StartSession(client_.get());
197 void Finalize() {
198 manager_->EndSession(client_.get());
201 bool IsInitializationCallbackInvoked() {
202 return client_->complete_start_session_;
205 Result GetInitializationResult() { return client_->result_; }
207 void RunCallbackUntilCallbackInvoked(
208 bool result, UsbMidiDevice::Devices* devices) {
209 factory_->callback_.Run(result, devices);
210 while (!client_->complete_start_session_) {
211 base::RunLoop run_loop;
212 run_loop.RunUntilIdle();
216 const MidiPortInfoList& input_ports() { return client_->input_ports_; }
217 const MidiPortInfoList& output_ports() { return client_->output_ports_; }
219 scoped_ptr<MidiManagerUsbForTesting> manager_;
220 scoped_ptr<FakeMidiManagerClient> client_;
221 // Owned by manager_.
222 TestUsbMidiDeviceFactory* factory_;
223 Logger logger_;
225 private:
226 scoped_ptr<base::MessageLoop> message_loop_;
228 DISALLOW_COPY_AND_ASSIGN(MidiManagerUsbTest);
232 TEST_F(MidiManagerUsbTest, Initialize) {
233 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
234 uint8 descriptors[] = {
235 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
236 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
237 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
238 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
239 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
240 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
241 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
242 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
243 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
244 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
245 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
246 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
247 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
248 0x05, 0x25, 0x01, 0x01, 0x07,
250 device->SetDescriptors(ToVector(descriptors));
251 device->SetManufacturer("vendor1");
252 device->SetProductName("device1");
253 device->SetDeviceVersion("1.02");
255 Initialize();
256 ScopedVector<UsbMidiDevice> devices;
257 devices.push_back(device.Pass());
258 EXPECT_FALSE(IsInitializationCallbackInvoked());
259 RunCallbackUntilCallbackInvoked(true, &devices);
260 EXPECT_EQ(Result::OK, GetInitializationResult());
262 ASSERT_EQ(1u, input_ports().size());
263 EXPECT_EQ("usb:port-0-2", input_ports()[0].id);
264 EXPECT_EQ("vendor1", input_ports()[0].manufacturer);
265 EXPECT_EQ("device1", input_ports()[0].name);
266 EXPECT_EQ("1.02", input_ports()[0].version);
268 ASSERT_EQ(2u, output_ports().size());
269 EXPECT_EQ("usb:port-0-0", output_ports()[0].id);
270 EXPECT_EQ("vendor1", output_ports()[0].manufacturer);
271 EXPECT_EQ("device1", output_ports()[0].name);
272 EXPECT_EQ("1.02", output_ports()[0].version);
273 EXPECT_EQ("usb:port-0-1", output_ports()[1].id);
274 EXPECT_EQ("vendor1", output_ports()[1].manufacturer);
275 EXPECT_EQ("device1", output_ports()[1].name);
276 EXPECT_EQ("1.02", output_ports()[1].version);
278 ASSERT_TRUE(manager_->input_stream());
279 std::vector<UsbMidiJack> jacks = manager_->input_stream()->jacks();
280 ASSERT_EQ(2u, manager_->output_streams().size());
281 EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id);
282 EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id);
283 ASSERT_EQ(1u, jacks.size());
284 EXPECT_EQ(2, jacks[0].endpoint_number());
286 EXPECT_EQ("UsbMidiDevice::GetDescriptors\n", logger_.TakeLog());
289 TEST_F(MidiManagerUsbTest, InitializeMultipleDevices) {
290 scoped_ptr<FakeUsbMidiDevice> device1(new FakeUsbMidiDevice(&logger_));
291 scoped_ptr<FakeUsbMidiDevice> device2(new FakeUsbMidiDevice(&logger_));
292 uint8 descriptors[] = {
293 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, 0x2d, 0x75,
294 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, 0x75, 0x00, 0x02, 0x01,
295 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00,
296 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01,
297 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
298 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, 0x01, 0x03,
299 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, 0x24, 0x03, 0x01, 0x07,
300 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, 0x03, 0x02, 0x04, 0x01, 0x02, 0x01,
301 0x00, 0x09, 0x24, 0x03, 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05,
302 0x02, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
303 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x05, 0x25,
304 0x01, 0x01, 0x07,
306 device1->SetDescriptors(ToVector(descriptors));
307 device1->SetManufacturer("vendor1");
308 device1->SetProductName("device1");
309 device1->SetDeviceVersion("1.02");
310 device2->SetDescriptors(ToVector(descriptors));
311 device2->SetManufacturer("vendor2");
312 device2->SetProductName("device2");
313 device2->SetDeviceVersion("98.76");
315 Initialize();
316 ScopedVector<UsbMidiDevice> devices;
317 devices.push_back(device1.Pass());
318 devices.push_back(device2.Pass());
319 EXPECT_FALSE(IsInitializationCallbackInvoked());
320 RunCallbackUntilCallbackInvoked(true, &devices);
321 EXPECT_EQ(Result::OK, GetInitializationResult());
323 ASSERT_EQ(2u, input_ports().size());
324 EXPECT_EQ("usb:port-0-2", input_ports()[0].id);
325 EXPECT_EQ("vendor1", input_ports()[0].manufacturer);
326 EXPECT_EQ("device1", input_ports()[0].name);
327 EXPECT_EQ("1.02", input_ports()[0].version);
328 EXPECT_EQ("usb:port-1-2", input_ports()[1].id);
329 EXPECT_EQ("vendor2", input_ports()[1].manufacturer);
330 EXPECT_EQ("device2", input_ports()[1].name);
331 EXPECT_EQ("98.76", input_ports()[1].version);
333 ASSERT_EQ(4u, output_ports().size());
334 EXPECT_EQ("usb:port-0-0", output_ports()[0].id);
335 EXPECT_EQ("vendor1", output_ports()[0].manufacturer);
336 EXPECT_EQ("device1", output_ports()[0].name);
337 EXPECT_EQ("1.02", output_ports()[0].version);
338 EXPECT_EQ("usb:port-0-1", output_ports()[1].id);
339 EXPECT_EQ("vendor1", output_ports()[1].manufacturer);
340 EXPECT_EQ("device1", output_ports()[1].name);
341 EXPECT_EQ("1.02", output_ports()[1].version);
342 EXPECT_EQ("usb:port-1-0", output_ports()[2].id);
343 EXPECT_EQ("vendor2", output_ports()[2].manufacturer);
344 EXPECT_EQ("device2", output_ports()[2].name);
345 EXPECT_EQ("98.76", output_ports()[2].version);
346 EXPECT_EQ("usb:port-1-1", output_ports()[3].id);
347 EXPECT_EQ("vendor2", output_ports()[3].manufacturer);
348 EXPECT_EQ("device2", output_ports()[3].name);
349 EXPECT_EQ("98.76", output_ports()[3].version);
351 ASSERT_TRUE(manager_->input_stream());
352 std::vector<UsbMidiJack> jacks = manager_->input_stream()->jacks();
353 ASSERT_EQ(4u, manager_->output_streams().size());
354 EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id);
355 EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id);
356 ASSERT_EQ(2u, jacks.size());
357 EXPECT_EQ(2, jacks[0].endpoint_number());
359 EXPECT_EQ(
360 "UsbMidiDevice::GetDescriptors\n"
361 "UsbMidiDevice::GetDescriptors\n",
362 logger_.TakeLog());
365 TEST_F(MidiManagerUsbTest, InitializeFail) {
366 Initialize();
368 EXPECT_FALSE(IsInitializationCallbackInvoked());
369 RunCallbackUntilCallbackInvoked(false, NULL);
370 EXPECT_EQ(Result::INITIALIZATION_ERROR, GetInitializationResult());
373 TEST_F(MidiManagerUsbTest, InitializeFailBecauseOfInvalidDescriptors) {
374 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
375 uint8 descriptors[] = {0x04};
376 device->SetDescriptors(ToVector(descriptors));
378 Initialize();
379 ScopedVector<UsbMidiDevice> devices;
380 devices.push_back(device.Pass());
381 EXPECT_FALSE(IsInitializationCallbackInvoked());
382 RunCallbackUntilCallbackInvoked(true, &devices);
383 EXPECT_EQ(Result::INITIALIZATION_ERROR, GetInitializationResult());
384 EXPECT_EQ("UsbMidiDevice::GetDescriptors\n", logger_.TakeLog());
387 TEST_F(MidiManagerUsbTest, Send) {
388 Initialize();
389 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
390 uint8 descriptors[] = {
391 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
392 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
393 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
394 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
395 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
396 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
397 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
398 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
399 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
400 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
401 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
402 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
403 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
404 0x05, 0x25, 0x01, 0x01, 0x07,
407 device->SetDescriptors(ToVector(descriptors));
408 uint8 data[] = {
409 0x90, 0x45, 0x7f,
410 0xf0, 0x00, 0x01, 0xf7,
413 ScopedVector<UsbMidiDevice> devices;
414 devices.push_back(device.Pass());
415 EXPECT_FALSE(IsInitializationCallbackInvoked());
416 RunCallbackUntilCallbackInvoked(true, &devices);
417 EXPECT_EQ(Result::OK, GetInitializationResult());
418 ASSERT_EQ(2u, manager_->output_streams().size());
420 manager_->DispatchSendMidiData(client_.get(), 1, ToVector(data), 0);
421 // Since UsbMidiDevice::Send is posted as a task, RunLoop should run to
422 // invoke the task.
423 base::RunLoop run_loop;
424 run_loop.RunUntilIdle();
425 EXPECT_EQ("UsbMidiDevice::GetDescriptors\n"
426 "UsbMidiDevice::Send endpoint = 2 data = "
427 "0x19 0x90 0x45 0x7f "
428 "0x14 0xf0 0x00 0x01 "
429 "0x15 0xf7 0x00 0x00\n"
430 "MidiManagerClient::AccumulateMidiBytesSent size = 7\n",
431 logger_.TakeLog());
434 TEST_F(MidiManagerUsbTest, SendFromCompromizedRenderer) {
435 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
436 uint8 descriptors[] = {
437 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
438 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
439 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
440 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
441 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
442 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
443 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
444 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
445 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
446 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
447 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
448 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
449 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
450 0x05, 0x25, 0x01, 0x01, 0x07,
453 device->SetDescriptors(ToVector(descriptors));
454 uint8 data[] = {
455 0x90, 0x45, 0x7f,
456 0xf0, 0x00, 0x01, 0xf7,
459 Initialize();
460 ScopedVector<UsbMidiDevice> devices;
461 devices.push_back(device.Pass());
462 EXPECT_FALSE(IsInitializationCallbackInvoked());
463 RunCallbackUntilCallbackInvoked(true, &devices);
464 EXPECT_EQ(Result::OK, GetInitializationResult());
465 ASSERT_EQ(2u, manager_->output_streams().size());
466 EXPECT_EQ("UsbMidiDevice::GetDescriptors\n", logger_.TakeLog());
468 // The specified port index is invalid. The manager must ignore the request.
469 manager_->DispatchSendMidiData(client_.get(), 99, ToVector(data), 0);
470 EXPECT_EQ("", logger_.TakeLog());
472 // The specified port index is invalid. The manager must ignore the request.
473 manager_->DispatchSendMidiData(client_.get(), 2, ToVector(data), 0);
474 EXPECT_EQ("", logger_.TakeLog());
477 TEST_F(MidiManagerUsbTest, Receive) {
478 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
479 uint8 descriptors[] = {
480 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
481 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
482 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
483 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
484 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
485 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
486 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
487 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
488 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
489 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
490 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
491 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
492 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
493 0x05, 0x25, 0x01, 0x01, 0x07,
496 device->SetDescriptors(ToVector(descriptors));
497 uint8 data[] = {
498 0x09, 0x90, 0x45, 0x7f,
499 0x04, 0xf0, 0x00, 0x01,
500 0x49, 0x90, 0x88, 0x99, // This data should be ignored (CN = 4).
501 0x05, 0xf7, 0x00, 0x00,
504 Initialize();
505 ScopedVector<UsbMidiDevice> devices;
506 UsbMidiDevice* device_raw = device.get();
507 devices.push_back(device.Pass());
508 EXPECT_FALSE(IsInitializationCallbackInvoked());
509 RunCallbackUntilCallbackInvoked(true, &devices);
510 EXPECT_EQ(Result::OK, GetInitializationResult());
512 manager_->ReceiveUsbMidiData(device_raw, 2, data, arraysize(data),
513 base::TimeTicks());
514 Finalize();
516 EXPECT_EQ(
517 "UsbMidiDevice::GetDescriptors\n"
518 "MidiManagerClient::ReceiveMidiData usb:port_index = 0 "
519 "data = 0x90 0x45 0x7f\n"
520 "MidiManagerClient::ReceiveMidiData usb:port_index = 0 "
521 "data = 0xf0 0x00 0x01\n"
522 "MidiManagerClient::ReceiveMidiData usb:port_index = 0 data = 0xf7\n",
523 logger_.TakeLog());
526 TEST_F(MidiManagerUsbTest, AttachDevice) {
527 uint8 descriptors[] = {
528 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
529 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
530 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
531 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
532 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
533 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
534 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
535 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
536 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
537 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
538 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
539 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
540 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
541 0x05, 0x25, 0x01, 0x01, 0x07,
544 Initialize();
545 ScopedVector<UsbMidiDevice> devices;
546 EXPECT_FALSE(IsInitializationCallbackInvoked());
547 RunCallbackUntilCallbackInvoked(true, &devices);
548 EXPECT_EQ(Result::OK, GetInitializationResult());
550 ASSERT_EQ(0u, input_ports().size());
551 ASSERT_EQ(0u, output_ports().size());
552 ASSERT_TRUE(manager_->input_stream());
553 std::vector<UsbMidiJack> jacks = manager_->input_stream()->jacks();
554 ASSERT_EQ(0u, manager_->output_streams().size());
555 ASSERT_EQ(0u, jacks.size());
556 EXPECT_EQ("", logger_.TakeLog());
558 scoped_ptr<FakeUsbMidiDevice> new_device(new FakeUsbMidiDevice(&logger_));
559 new_device->SetDescriptors(ToVector(descriptors));
560 manager_->OnDeviceAttached(new_device.Pass());
562 ASSERT_EQ(1u, input_ports().size());
563 ASSERT_EQ(2u, output_ports().size());
564 ASSERT_TRUE(manager_->input_stream());
565 jacks = manager_->input_stream()->jacks();
566 ASSERT_EQ(2u, manager_->output_streams().size());
567 EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id);
568 EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id);
569 ASSERT_EQ(1u, jacks.size());
570 EXPECT_EQ(2, jacks[0].endpoint_number());
571 EXPECT_EQ("UsbMidiDevice::GetDescriptors\n", logger_.TakeLog());
574 } // namespace
576 } // namespace midi
577 } // namespace media