Delete unused downloads page asset.
[chromium-blink-merge.git] / media / midi / midi_manager_usb_unittest.cc
blob21dd2369262c5a0eea757532b3a1ccfdcdfe200f
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(base::StringPrintf("port_index = %d data =", port_index));
121 for (size_t i = 0; i < size; ++i)
122 logger_->AddLog(base::StringPrintf(" 0x%02x", data[i]));
123 logger_->AddLog("\n");
126 void AccumulateMidiBytesSent(size_t size) override {
127 logger_->AddLog("MidiManagerClient::AccumulateMidiBytesSent ");
128 // Windows has no "%zu".
129 logger_->AddLog(base::StringPrintf("size = %u\n",
130 static_cast<unsigned>(size)));
133 bool complete_start_session_;
134 Result result_;
135 MidiPortInfoList input_ports_;
136 MidiPortInfoList output_ports_;
138 private:
139 Logger* logger_;
141 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
144 class TestUsbMidiDeviceFactory : public UsbMidiDevice::Factory {
145 public:
146 TestUsbMidiDeviceFactory() {}
147 ~TestUsbMidiDeviceFactory() override {}
148 void EnumerateDevices(UsbMidiDeviceDelegate* device,
149 Callback callback) override {
150 callback_ = callback;
153 Callback callback_;
155 private:
156 DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDeviceFactory);
159 class MidiManagerUsbForTesting : public MidiManagerUsb {
160 public:
161 explicit MidiManagerUsbForTesting(
162 scoped_ptr<UsbMidiDevice::Factory> device_factory)
163 : MidiManagerUsb(device_factory.Pass()) {}
164 ~MidiManagerUsbForTesting() override {}
166 void CallCompleteInitialization(Result result) {
167 CompleteInitialization(result);
168 base::RunLoop run_loop;
169 run_loop.RunUntilIdle();
172 private:
173 DISALLOW_COPY_AND_ASSIGN(MidiManagerUsbForTesting);
176 class MidiManagerUsbTest : public ::testing::Test {
177 public:
178 MidiManagerUsbTest() : message_loop_(new base::MessageLoop) {
179 scoped_ptr<TestUsbMidiDeviceFactory> factory(new TestUsbMidiDeviceFactory);
180 factory_ = factory.get();
181 manager_.reset(new MidiManagerUsbForTesting(factory.Pass()));
183 ~MidiManagerUsbTest() override {
184 std::string leftover_logs = logger_.TakeLog();
185 if (!leftover_logs.empty()) {
186 ADD_FAILURE() << "Log should be empty: " << leftover_logs;
190 protected:
191 void Initialize() {
192 client_.reset(new FakeMidiManagerClient(&logger_));
193 manager_->StartSession(client_.get());
196 void Finalize() {
197 manager_->EndSession(client_.get());
200 bool IsInitializationCallbackInvoked() {
201 return client_->complete_start_session_;
204 Result GetInitializationResult() { return client_->result_; }
206 void RunCallbackUntilCallbackInvoked(
207 bool result, UsbMidiDevice::Devices* devices) {
208 factory_->callback_.Run(result, devices);
209 while (!client_->complete_start_session_) {
210 base::RunLoop run_loop;
211 run_loop.RunUntilIdle();
215 const MidiPortInfoList& input_ports() { return client_->input_ports_; }
216 const MidiPortInfoList& output_ports() { return client_->output_ports_; }
218 scoped_ptr<MidiManagerUsbForTesting> manager_;
219 scoped_ptr<FakeMidiManagerClient> client_;
220 // Owned by manager_.
221 TestUsbMidiDeviceFactory* factory_;
222 Logger logger_;
224 private:
225 scoped_ptr<base::MessageLoop> message_loop_;
227 DISALLOW_COPY_AND_ASSIGN(MidiManagerUsbTest);
231 TEST_F(MidiManagerUsbTest, Initialize) {
232 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
233 uint8 descriptors[] = {
234 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
235 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
236 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
237 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
238 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
239 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
240 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
241 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
242 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
243 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
244 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
245 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
246 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
247 0x05, 0x25, 0x01, 0x01, 0x07,
249 device->SetDescriptors(ToVector(descriptors));
250 device->SetManufacturer("vendor1");
251 device->SetProductName("device1");
252 device->SetDeviceVersion("1.02");
254 Initialize();
255 ScopedVector<UsbMidiDevice> devices;
256 devices.push_back(device.Pass());
257 EXPECT_FALSE(IsInitializationCallbackInvoked());
258 RunCallbackUntilCallbackInvoked(true, &devices);
259 EXPECT_EQ(Result::OK, GetInitializationResult());
261 ASSERT_EQ(1u, input_ports().size());
262 EXPECT_EQ("port-0-2", input_ports()[0].id);
263 EXPECT_EQ("vendor1", input_ports()[0].manufacturer);
264 EXPECT_EQ("device1", input_ports()[0].name);
265 EXPECT_EQ("1.02", input_ports()[0].version);
267 ASSERT_EQ(2u, output_ports().size());
268 EXPECT_EQ("port-0-0", output_ports()[0].id);
269 EXPECT_EQ("vendor1", output_ports()[0].manufacturer);
270 EXPECT_EQ("device1", output_ports()[0].name);
271 EXPECT_EQ("1.02", output_ports()[0].version);
272 EXPECT_EQ("port-0-1", output_ports()[1].id);
273 EXPECT_EQ("vendor1", output_ports()[1].manufacturer);
274 EXPECT_EQ("device1", output_ports()[1].name);
275 EXPECT_EQ("1.02", output_ports()[1].version);
277 ASSERT_TRUE(manager_->input_stream());
278 std::vector<UsbMidiJack> jacks = manager_->input_stream()->jacks();
279 ASSERT_EQ(2u, manager_->output_streams().size());
280 EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id);
281 EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id);
282 ASSERT_EQ(1u, jacks.size());
283 EXPECT_EQ(2, jacks[0].endpoint_number());
285 EXPECT_EQ("UsbMidiDevice::GetDescriptors\n", logger_.TakeLog());
288 TEST_F(MidiManagerUsbTest, InitializeMultipleDevices) {
289 scoped_ptr<FakeUsbMidiDevice> device1(new FakeUsbMidiDevice(&logger_));
290 scoped_ptr<FakeUsbMidiDevice> device2(new FakeUsbMidiDevice(&logger_));
291 uint8 descriptors[] = {
292 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, 0x2d, 0x75,
293 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, 0x75, 0x00, 0x02, 0x01,
294 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00,
295 0x09, 0x24, 0x01, 0x00, 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01,
296 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
297 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, 0x01, 0x03,
298 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, 0x24, 0x03, 0x01, 0x07,
299 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, 0x03, 0x02, 0x04, 0x01, 0x02, 0x01,
300 0x00, 0x09, 0x24, 0x03, 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05,
301 0x02, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
302 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 0x05, 0x25,
303 0x01, 0x01, 0x07,
305 device1->SetDescriptors(ToVector(descriptors));
306 device1->SetManufacturer("vendor1");
307 device1->SetProductName("device1");
308 device1->SetDeviceVersion("1.02");
309 device2->SetDescriptors(ToVector(descriptors));
310 device2->SetManufacturer("vendor2");
311 device2->SetProductName("device2");
312 device2->SetDeviceVersion("98.76");
314 Initialize();
315 ScopedVector<UsbMidiDevice> devices;
316 devices.push_back(device1.Pass());
317 devices.push_back(device2.Pass());
318 EXPECT_FALSE(IsInitializationCallbackInvoked());
319 RunCallbackUntilCallbackInvoked(true, &devices);
320 EXPECT_EQ(Result::OK, GetInitializationResult());
322 ASSERT_EQ(2u, input_ports().size());
323 EXPECT_EQ("port-0-2", input_ports()[0].id);
324 EXPECT_EQ("vendor1", input_ports()[0].manufacturer);
325 EXPECT_EQ("device1", input_ports()[0].name);
326 EXPECT_EQ("1.02", input_ports()[0].version);
327 EXPECT_EQ("port-1-2", input_ports()[1].id);
328 EXPECT_EQ("vendor2", input_ports()[1].manufacturer);
329 EXPECT_EQ("device2", input_ports()[1].name);
330 EXPECT_EQ("98.76", input_ports()[1].version);
332 ASSERT_EQ(4u, output_ports().size());
333 EXPECT_EQ("port-0-0", output_ports()[0].id);
334 EXPECT_EQ("vendor1", output_ports()[0].manufacturer);
335 EXPECT_EQ("device1", output_ports()[0].name);
336 EXPECT_EQ("1.02", output_ports()[0].version);
337 EXPECT_EQ("port-0-1", output_ports()[1].id);
338 EXPECT_EQ("vendor1", output_ports()[1].manufacturer);
339 EXPECT_EQ("device1", output_ports()[1].name);
340 EXPECT_EQ("1.02", output_ports()[1].version);
341 EXPECT_EQ("port-1-0", output_ports()[2].id);
342 EXPECT_EQ("vendor2", output_ports()[2].manufacturer);
343 EXPECT_EQ("device2", output_ports()[2].name);
344 EXPECT_EQ("98.76", output_ports()[2].version);
345 EXPECT_EQ("port-1-1", output_ports()[3].id);
346 EXPECT_EQ("vendor2", output_ports()[3].manufacturer);
347 EXPECT_EQ("device2", output_ports()[3].name);
348 EXPECT_EQ("98.76", output_ports()[3].version);
350 ASSERT_TRUE(manager_->input_stream());
351 std::vector<UsbMidiJack> jacks = manager_->input_stream()->jacks();
352 ASSERT_EQ(4u, manager_->output_streams().size());
353 EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id);
354 EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id);
355 ASSERT_EQ(2u, jacks.size());
356 EXPECT_EQ(2, jacks[0].endpoint_number());
358 EXPECT_EQ(
359 "UsbMidiDevice::GetDescriptors\n"
360 "UsbMidiDevice::GetDescriptors\n",
361 logger_.TakeLog());
364 TEST_F(MidiManagerUsbTest, InitializeFail) {
365 Initialize();
367 EXPECT_FALSE(IsInitializationCallbackInvoked());
368 RunCallbackUntilCallbackInvoked(false, NULL);
369 EXPECT_EQ(Result::INITIALIZATION_ERROR, GetInitializationResult());
372 TEST_F(MidiManagerUsbTest, InitializeFailBecauseOfInvalidDescriptors) {
373 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
374 uint8 descriptors[] = {0x04};
375 device->SetDescriptors(ToVector(descriptors));
377 Initialize();
378 ScopedVector<UsbMidiDevice> devices;
379 devices.push_back(device.Pass());
380 EXPECT_FALSE(IsInitializationCallbackInvoked());
381 RunCallbackUntilCallbackInvoked(true, &devices);
382 EXPECT_EQ(Result::INITIALIZATION_ERROR, GetInitializationResult());
383 EXPECT_EQ("UsbMidiDevice::GetDescriptors\n", logger_.TakeLog());
386 TEST_F(MidiManagerUsbTest, Send) {
387 Initialize();
388 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
389 uint8 descriptors[] = {
390 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
391 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
392 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
393 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
394 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
395 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
396 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
397 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
398 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
399 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
400 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
401 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
402 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
403 0x05, 0x25, 0x01, 0x01, 0x07,
406 device->SetDescriptors(ToVector(descriptors));
407 uint8 data[] = {
408 0x90, 0x45, 0x7f,
409 0xf0, 0x00, 0x01, 0xf7,
412 ScopedVector<UsbMidiDevice> devices;
413 devices.push_back(device.Pass());
414 EXPECT_FALSE(IsInitializationCallbackInvoked());
415 RunCallbackUntilCallbackInvoked(true, &devices);
416 EXPECT_EQ(Result::OK, GetInitializationResult());
417 ASSERT_EQ(2u, manager_->output_streams().size());
419 manager_->DispatchSendMidiData(client_.get(), 1, ToVector(data), 0);
420 // Since UsbMidiDevice::Send is posted as a task, RunLoop should run to
421 // invoke the task.
422 base::RunLoop run_loop;
423 run_loop.RunUntilIdle();
424 EXPECT_EQ("UsbMidiDevice::GetDescriptors\n"
425 "UsbMidiDevice::Send endpoint = 2 data = "
426 "0x19 0x90 0x45 0x7f "
427 "0x14 0xf0 0x00 0x01 "
428 "0x15 0xf7 0x00 0x00\n"
429 "MidiManagerClient::AccumulateMidiBytesSent size = 7\n",
430 logger_.TakeLog());
433 TEST_F(MidiManagerUsbTest, SendFromCompromizedRenderer) {
434 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
435 uint8 descriptors[] = {
436 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
437 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
438 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
439 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
440 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
441 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
442 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
443 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
444 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
445 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
446 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
447 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
448 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
449 0x05, 0x25, 0x01, 0x01, 0x07,
452 device->SetDescriptors(ToVector(descriptors));
453 uint8 data[] = {
454 0x90, 0x45, 0x7f,
455 0xf0, 0x00, 0x01, 0xf7,
458 Initialize();
459 ScopedVector<UsbMidiDevice> devices;
460 devices.push_back(device.Pass());
461 EXPECT_FALSE(IsInitializationCallbackInvoked());
462 RunCallbackUntilCallbackInvoked(true, &devices);
463 EXPECT_EQ(Result::OK, GetInitializationResult());
464 ASSERT_EQ(2u, manager_->output_streams().size());
465 EXPECT_EQ("UsbMidiDevice::GetDescriptors\n", logger_.TakeLog());
467 // The specified port index is invalid. The manager must ignore the request.
468 manager_->DispatchSendMidiData(client_.get(), 99, ToVector(data), 0);
469 EXPECT_EQ("", logger_.TakeLog());
471 // The specified port index is invalid. The manager must ignore the request.
472 manager_->DispatchSendMidiData(client_.get(), 2, ToVector(data), 0);
473 EXPECT_EQ("", logger_.TakeLog());
476 TEST_F(MidiManagerUsbTest, Receive) {
477 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
478 uint8 descriptors[] = {
479 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
480 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
481 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
482 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
483 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
484 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
485 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
486 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
487 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
488 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
489 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
490 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
491 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
492 0x05, 0x25, 0x01, 0x01, 0x07,
495 device->SetDescriptors(ToVector(descriptors));
496 uint8 data[] = {
497 0x09, 0x90, 0x45, 0x7f,
498 0x04, 0xf0, 0x00, 0x01,
499 0x49, 0x90, 0x88, 0x99, // This data should be ignored (CN = 4).
500 0x05, 0xf7, 0x00, 0x00,
503 Initialize();
504 ScopedVector<UsbMidiDevice> devices;
505 UsbMidiDevice* device_raw = device.get();
506 devices.push_back(device.Pass());
507 EXPECT_FALSE(IsInitializationCallbackInvoked());
508 RunCallbackUntilCallbackInvoked(true, &devices);
509 EXPECT_EQ(Result::OK, GetInitializationResult());
511 manager_->ReceiveUsbMidiData(device_raw, 2, data, arraysize(data),
512 base::TimeTicks());
513 Finalize();
515 EXPECT_EQ("UsbMidiDevice::GetDescriptors\n"
516 "MidiManagerClient::ReceiveMidiData port_index = 0 "
517 "data = 0x90 0x45 0x7f\n"
518 "MidiManagerClient::ReceiveMidiData port_index = 0 "
519 "data = 0xf0 0x00 0x01\n"
520 "MidiManagerClient::ReceiveMidiData port_index = 0 data = 0xf7\n",
521 logger_.TakeLog());
524 TEST_F(MidiManagerUsbTest, AttachDevice) {
525 uint8 descriptors[] = {
526 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
527 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
528 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
529 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
530 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
531 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
532 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
533 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
534 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
535 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
536 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
537 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
538 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
539 0x05, 0x25, 0x01, 0x01, 0x07,
542 Initialize();
543 ScopedVector<UsbMidiDevice> devices;
544 EXPECT_FALSE(IsInitializationCallbackInvoked());
545 RunCallbackUntilCallbackInvoked(true, &devices);
546 EXPECT_EQ(Result::OK, GetInitializationResult());
548 ASSERT_EQ(0u, input_ports().size());
549 ASSERT_EQ(0u, output_ports().size());
550 ASSERT_TRUE(manager_->input_stream());
551 std::vector<UsbMidiJack> jacks = manager_->input_stream()->jacks();
552 ASSERT_EQ(0u, manager_->output_streams().size());
553 ASSERT_EQ(0u, jacks.size());
554 EXPECT_EQ("", logger_.TakeLog());
556 scoped_ptr<FakeUsbMidiDevice> new_device(new FakeUsbMidiDevice(&logger_));
557 new_device->SetDescriptors(ToVector(descriptors));
558 manager_->OnDeviceAttached(new_device.Pass());
560 ASSERT_EQ(1u, input_ports().size());
561 ASSERT_EQ(2u, output_ports().size());
562 ASSERT_TRUE(manager_->input_stream());
563 jacks = manager_->input_stream()->jacks();
564 ASSERT_EQ(2u, manager_->output_streams().size());
565 EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id);
566 EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id);
567 ASSERT_EQ(1u, jacks.size());
568 EXPECT_EQ(2, jacks[0].endpoint_number());
569 EXPECT_EQ("UsbMidiDevice::GetDescriptors\n", logger_.TakeLog());
572 } // namespace
574 } // namespace midi
575 } // namespace media