Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: media/midi/midi_manager_usb_unittest.cc

Issue 107163008: [WebMIDI] Introduce MidiManagerUsb (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@usb-midi-stream
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « media/midi/midi_manager_usb.cc ('k') | media/midi/usb_midi_descriptor_parser_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "media/midi/midi_manager_usb.h"
6
7 #include <string>
8
9 #include "base/strings/stringprintf.h"
10 #include "media/midi/usb_midi_device.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace media {
14
15 namespace {
16
17 template<typename T, size_t N>
18 std::vector<T> ToVector(const T (&array)[N]) {
19 return std::vector<T>(array, array + N);
20 }
21
22 class Logger {
23 public:
24 Logger() {}
25 ~Logger() {}
26
27 void AddLog(const std::string& message) { log_ += message; }
28 std::string TakeLog() {
29 std::string result;
30 result.swap(log_);
31 return result;
32 }
33
34 private:
35 std::string log_;
36
37 DISALLOW_COPY_AND_ASSIGN(Logger);
38 };
39
40 class FakeUsbMidiDevice : public UsbMidiDevice {
41 public:
42 explicit FakeUsbMidiDevice(Logger* logger) : logger_(logger) {}
43 virtual ~FakeUsbMidiDevice() {}
44
45 virtual std::vector<uint8> GetDescriptor() OVERRIDE {
46 logger_->AddLog("UsbMidiDevice::GetDescriptor\n");
47 return descriptor_;
48 }
49
50 virtual void Send(int endpoint_number,
51 const std::vector<uint8>& data) OVERRIDE {
52 logger_->AddLog("UsbMidiDevice::Send ");
53 logger_->AddLog(base::StringPrintf("endpoint = %d data =",
54 endpoint_number));
55 for (size_t i = 0; i < data.size(); ++i)
56 logger_->AddLog(base::StringPrintf(" 0x%02x", data[i]));
57 logger_->AddLog("\n");
58 }
59
60 void SetDescriptor(const std::vector<uint8> descriptor) {
61 descriptor_ = descriptor;
62 }
63
64 private:
65 std::vector<uint8> descriptor_;
66 Logger* logger_;
67
68 DISALLOW_COPY_AND_ASSIGN(FakeUsbMidiDevice);
69 };
70
71 class FakeMidiManagerClient : public MIDIManagerClient {
72 public:
73 explicit FakeMidiManagerClient(Logger* logger) : logger_(logger) {}
74 virtual ~FakeMidiManagerClient() {}
75
76 virtual void ReceiveMIDIData(uint32 port_index,
77 const uint8* data,
78 size_t size,
79 double timestamp) OVERRIDE {
80 logger_->AddLog("MIDIManagerClient::ReceiveMIDIData ");
81 logger_->AddLog(base::StringPrintf("port_index = %d data =", port_index));
82 for (size_t i = 0; i < size; ++i)
83 logger_->AddLog(base::StringPrintf(" 0x%02x", data[i]));
84 logger_->AddLog("\n");
85 }
86
87 virtual void AccumulateMIDIBytesSent(size_t size) OVERRIDE {
88 logger_->AddLog("MIDIManagerClient::AccumulateMIDIBytesSent ");
89 // Windows has no "%zu".
90 logger_->AddLog(base::StringPrintf("size = %u\n",
91 static_cast<unsigned>(size)));
92 }
93
94 private:
95 Logger* logger_;
96
97 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
98 };
99
100 class TestUsbMidiDeviceFactory : public UsbMidiDevice::Factory {
101 public:
102 TestUsbMidiDeviceFactory() {}
103 virtual ~TestUsbMidiDeviceFactory() {}
104 virtual void EnumerateDevices(UsbMidiDeviceDelegate* device,
105 Callback callback) OVERRIDE {
106 callback_ = callback;
107 }
108
109 Callback callback_;
110
111 private:
112 DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDeviceFactory);
113 };
114
115 class MidiManagerUsbTest : public ::testing::Test {
116 public:
117 MidiManagerUsbTest()
118 : initialize_callback_run_(false), initialize_result_(false) {
119 scoped_ptr<TestUsbMidiDeviceFactory> factory(new TestUsbMidiDeviceFactory);
120 factory_ = factory.get();
121 manager_.reset(
122 new MidiManagerUsb(factory.PassAs<UsbMidiDevice::Factory>()));
123 }
124 virtual ~MidiManagerUsbTest() {
125 std::string leftover_logs = logger_.TakeLog();
126 if (!leftover_logs.empty()) {
127 ADD_FAILURE() << "Log should be empty: " << leftover_logs;
128 }
129 }
130
131 protected:
132 void Initialize() {
133 manager_->Initialize(base::Bind(&MidiManagerUsbTest::OnInitializeDone,
134 base::Unretained(this)));
135 }
136
137 void OnInitializeDone(bool result) {
138 initialize_callback_run_ = true;
139 initialize_result_ = result;
140 }
141
142 bool initialize_callback_run_;
143 bool initialize_result_;
144
145 scoped_ptr<MidiManagerUsb> manager_;
146 // Owned by manager_.
147 TestUsbMidiDeviceFactory* factory_;
148 Logger logger_;
149
150 private:
151 DISALLOW_COPY_AND_ASSIGN(MidiManagerUsbTest);
152 };
153
154
155 TEST_F(MidiManagerUsbTest, Initialize) {
156 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
157 uint8 descriptor[] = {
158 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
159 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
160 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
161 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
162 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
163 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
164 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
165 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
166 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
167 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
168 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
169 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
170 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
171 0x05, 0x25, 0x01, 0x01, 0x07,
172 };
173 device->SetDescriptor(ToVector(descriptor));
174
175 Initialize();
176 ScopedVector<UsbMidiDevice> devices;
177 devices.push_back(device.release());
178 EXPECT_FALSE(initialize_callback_run_);
179 factory_->callback_.Run(true, &devices);
180 EXPECT_TRUE(initialize_callback_run_);
181 EXPECT_TRUE(initialize_result_);
182
183 ASSERT_EQ(1u, manager_->input_ports().size());
184 ASSERT_EQ(2u, manager_->output_ports().size());
185 ASSERT_TRUE(manager_->input_stream());
186 std::vector<UsbMidiInputStream::JackUniqueKey> keys =
187 manager_->input_stream()->RegisteredJackKeysForTesting();
188 ASSERT_EQ(2u, manager_->output_streams().size());
189 EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id);
190 EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id);
191 ASSERT_EQ(1u, keys.size());
192 EXPECT_EQ(2, keys[0].endpoint_number);
193
194 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n", logger_.TakeLog());
195 }
196
197 TEST_F(MidiManagerUsbTest, InitializeFail) {
198 Initialize();
199
200 EXPECT_FALSE(initialize_callback_run_);
201 factory_->callback_.Run(false, NULL);
202 EXPECT_TRUE(initialize_callback_run_);
203 EXPECT_FALSE(initialize_result_);
204 }
205
206 TEST_F(MidiManagerUsbTest, InitializeFailBecauseOfInvalidDescriptor) {
207 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
208 uint8 descriptor[] = {0x04};
209 device->SetDescriptor(ToVector(descriptor));
210
211 Initialize();
212 ScopedVector<UsbMidiDevice> devices;
213 devices.push_back(device.release());
214 EXPECT_FALSE(initialize_callback_run_);
215 factory_->callback_.Run(true, &devices);
216 EXPECT_TRUE(initialize_callback_run_);
217 EXPECT_FALSE(initialize_result_);
218 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n", logger_.TakeLog());
219 }
220
221 TEST_F(MidiManagerUsbTest, Send) {
222 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
223 FakeMidiManagerClient client(&logger_);
224 uint8 descriptor[] = {
225 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
226 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
227 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
228 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
229 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
230 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
231 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
232 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
233 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
234 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
235 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
236 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
237 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
238 0x05, 0x25, 0x01, 0x01, 0x07,
239 };
240
241 device->SetDescriptor(ToVector(descriptor));
242 uint8 data[] = {
243 0x90, 0x45, 0x7f,
244 0xf0, 0x00, 0x01, 0xf7,
245 };
246
247 Initialize();
248 ScopedVector<UsbMidiDevice> devices;
249 devices.push_back(device.release());
250 EXPECT_FALSE(initialize_callback_run_);
251 factory_->callback_.Run(true, &devices);
252 ASSERT_TRUE(initialize_callback_run_);
253 ASSERT_TRUE(initialize_result_);
254 ASSERT_EQ(2u, manager_->output_streams().size());
255
256 manager_->DispatchSendMIDIData(&client, 1, ToVector(data), 0);
257 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n"
258 "UsbMidiDevice::Send endpoint = 2 data = "
259 "0x19 0x90 0x45 0x7f "
260 "0x14 0xf0 0x00 0x01 "
261 "0x15 0xf7 0x00 0x00\n"
262 "MIDIManagerClient::AccumulateMIDIBytesSent size = 7\n",
263 logger_.TakeLog());
264 }
265
266 TEST_F(MidiManagerUsbTest, Receive) {
267 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
268 FakeMidiManagerClient client(&logger_);
269 uint8 descriptor[] = {
270 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
271 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
272 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
273 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
274 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
275 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
276 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
277 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
278 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
279 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
280 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
281 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
282 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
283 0x05, 0x25, 0x01, 0x01, 0x07,
284 };
285
286 device->SetDescriptor(ToVector(descriptor));
287 uint8 data[] = {
288 0x09, 0x90, 0x45, 0x7f,
289 0x04, 0xf0, 0x00, 0x01,
290 0x49, 0x90, 0x88, 0x99, // This data should be ignored (CN = 4).
291 0x05, 0xf7, 0x00, 0x00,
292 };
293
294 Initialize();
295 ScopedVector<UsbMidiDevice> devices;
296 UsbMidiDevice* device_raw = device.get();
297 devices.push_back(device.release());
298 EXPECT_FALSE(initialize_callback_run_);
299 factory_->callback_.Run(true, &devices);
300 ASSERT_TRUE(initialize_callback_run_);
301 ASSERT_TRUE(initialize_result_);
302
303 manager_->StartSession(&client);
304 manager_->ReceiveUsbMidiData(device_raw, 2, data, arraysize(data), 0);
305 manager_->EndSession(&client);
306
307 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n"
308 "MIDIManagerClient::ReceiveMIDIData port_index = 0 "
309 "data = 0x90 0x45 0x7f\n"
310 "MIDIManagerClient::ReceiveMIDIData port_index = 0 "
311 "data = 0xf0 0x00 0x01\n"
312 "MIDIManagerClient::ReceiveMIDIData port_index = 0 data = 0xf7\n",
313 logger_.TakeLog());
314 }
315
316 } // namespace
317
318 } // namespace media
OLDNEW
« no previous file with comments | « media/midi/midi_manager_usb.cc ('k') | media/midi/usb_midi_descriptor_parser_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698