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

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 logger_->AddLog(base::StringPrintf("size = %zu\n", size));
90 }
91
92 private:
93 Logger* logger_;
94
95 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
96 };
97
98 class TestUsbMidiDeviceFactory : public UsbMidiDevice::Factory {
99 public:
100 TestUsbMidiDeviceFactory() {}
101 virtual ~TestUsbMidiDeviceFactory() {}
102 virtual void EnumerateDevices(UsbMidiDeviceDelegate* device,
103 Callback callback) OVERRIDE {
104 callback_ = callback;
105 }
106
107 Callback callback_;
108
109 private:
110 DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDeviceFactory);
111 };
112
113 class MidiManagerUsbTest : public ::testing::Test {
114 public:
115 MidiManagerUsbTest()
116 : initialize_callback_run_(false), initialize_result_(false) {
scherkus (not reviewing) 2014/01/28 17:56:43 this should be indented by 4 more spaces
yhirano 2014/01/29 01:20:52 Done.
117 scoped_ptr<TestUsbMidiDeviceFactory> factory(new TestUsbMidiDeviceFactory);
118 factory_ = factory.get();
119 manager_.reset(
120 new MidiManagerUsb(factory.PassAs<UsbMidiDevice::Factory>()));
121 }
122 virtual ~MidiManagerUsbTest() {
123 if (!logger_.TakeLog().empty()) {
124 ADD_FAILURE() << "There are some log messages not consumed: ";
scherkus (not reviewing) 2014/01/28 17:56:43 shouldn't you be printing out the logs? std::stri
yhirano 2014/01/29 01:20:52 Done.
125 }
126 }
127
128 protected:
129 void Initialize() {
130 manager_->Initialize(base::Bind(&MidiManagerUsbTest::OnInitializeDone,
131 base::Unretained(this)));
132 }
133
134 void OnInitializeDone(bool result) {
135 initialize_callback_run_ = true;
136 initialize_result_ = result;
137 }
138
139 bool initialize_callback_run_;
140 bool initialize_result_;
141
142 scoped_ptr<MidiManagerUsb> manager_;
143 // Owned by manager_.
144 TestUsbMidiDeviceFactory* factory_;
145 Logger logger_;
146
147 private:
148 DISALLOW_COPY_AND_ASSIGN(MidiManagerUsbTest);
149 };
150
151
152 TEST_F(MidiManagerUsbTest, Initialize) {
153 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
154 uint8 descriptor[] = {
155 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
156 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
157 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
158 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
159 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
160 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
161 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
162 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
163 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
164 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
165 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
166 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
167 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
168 0x05, 0x25, 0x01, 0x01, 0x07,
169 };
170 device->SetDescriptor(ToVector(descriptor));
171
172 Initialize();
173 ScopedVector<UsbMidiDevice> devices;
174 devices.push_back(device.release());
175 EXPECT_FALSE(initialize_callback_run_);
176 factory_->callback_.Run(true, &devices);
177 EXPECT_TRUE(initialize_callback_run_);
178 EXPECT_TRUE(initialize_result_);
179
180 ASSERT_EQ(1u, manager_->input_ports().size());
181 ASSERT_EQ(2u, manager_->output_ports().size());
182 ASSERT_TRUE(manager_->input_stream());
183 std::vector<UsbMidiInputStream::JackUniqueKey> keys =
184 manager_->input_stream()->RegisteredJackKeysForTesting();
185 ASSERT_EQ(2u, manager_->output_streams().size());
186 EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id);
187 EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id);
188 ASSERT_EQ(1u, keys.size());
189 EXPECT_EQ(2, keys[0].endpoint_number);
190
191 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n", logger_.TakeLog());
192 }
193
194 TEST_F(MidiManagerUsbTest, InitializeFail) {
195 Initialize();
196
197 EXPECT_FALSE(initialize_callback_run_);
198 factory_->callback_.Run(false, NULL);
199 EXPECT_TRUE(initialize_callback_run_);
200 EXPECT_FALSE(initialize_result_);
201 }
202
203 TEST_F(MidiManagerUsbTest, InitializeFailBecauseOfInvalidDescriptor) {
204 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
205 uint8 descriptor[] = {0x04};
206 device->SetDescriptor(ToVector(descriptor));
207
208 Initialize();
209 ScopedVector<UsbMidiDevice> devices;
210 devices.push_back(device.release());
211 EXPECT_FALSE(initialize_callback_run_);
212 factory_->callback_.Run(true, &devices);
213 EXPECT_TRUE(initialize_callback_run_);
214 EXPECT_FALSE(initialize_result_);
215 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n", logger_.TakeLog());
216 }
217
218 TEST_F(MidiManagerUsbTest, Send) {
219 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
220 FakeMidiManagerClient client(&logger_);
221 uint8 descriptor[] = {
222 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
223 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
224 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
225 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
226 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
227 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
228 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
229 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
230 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
231 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
232 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
233 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
234 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
235 0x05, 0x25, 0x01, 0x01, 0x07,
236 };
237
238 device->SetDescriptor(ToVector(descriptor));
239 uint8 data[] = {
240 0x90, 0x45, 0x7f,
241 0xf0, 0x00, 0x01, 0xf7,
242 };
243
244 Initialize();
245 ScopedVector<UsbMidiDevice> devices;
246 devices.push_back(device.release());
247 EXPECT_FALSE(initialize_callback_run_);
248 factory_->callback_.Run(true, &devices);
249 ASSERT_TRUE(initialize_callback_run_);
250 ASSERT_TRUE(initialize_result_);
251
252 manager_->DispatchSendMIDIData(&client, 1, ToVector(data), 0);
253 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n"
254 "UsbMidiDevice::Send endpoint = 2 data = "
255 "0x19 0x90 0x45 0x7f "
256 "0x14 0xf0 0x00 0x01 "
257 "0x15 0xf7 0x00 0x00\n"
258 "MIDIManagerClient::AccumulateMIDIBytesSent size = 7\n",
259 logger_.TakeLog());
260 }
261
262 TEST_F(MidiManagerUsbTest, Receive) {
263 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
264 FakeMidiManagerClient client(&logger_);
265 uint8 descriptor[] = {
266 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
267 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
268 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
269 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
270 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
271 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
272 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
273 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
274 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
275 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
276 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
277 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
278 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
279 0x05, 0x25, 0x01, 0x01, 0x07,
280 };
281
282 device->SetDescriptor(ToVector(descriptor));
283 uint8 data[] = {
284 0x09, 0x90, 0x45, 0x7f,
285 0x04, 0xf0, 0x00, 0x01,
286 0x49, 0x90, 0x88, 0x99, // This data should be ignored (CN = 4).
287 0x05, 0xf7, 0x00, 0x00,
288 };
289
290 Initialize();
291 ScopedVector<UsbMidiDevice> devices;
292 UsbMidiDevice* device_raw = device.get();
293 devices.push_back(device.release());
294 EXPECT_FALSE(initialize_callback_run_);
295 factory_->callback_.Run(true, &devices);
296 ASSERT_TRUE(initialize_callback_run_);
297 ASSERT_TRUE(initialize_result_);
298
299 manager_->StartSession(&client);
300 manager_->ReceiveUsbMidiData(device_raw, 2, data, arraysize(data), 0);
301 manager_->EndSession(&client);
302
303 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n"
304 "MIDIManagerClient::ReceiveMIDIData port_index = 0 "
305 "data = 0x90 0x45 0x7f\n"
306 "MIDIManagerClient::ReceiveMIDIData port_index = 0 "
307 "data = 0xf0 0x00 0x01\n"
308 "MIDIManagerClient::ReceiveMIDIData port_index = 0 data = 0xf7\n",
309 logger_.TakeLog());
310 }
311
312 } // namespace
313
314 } // 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