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

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, 11 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
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 void AddLog(const std::string& message) { log_ += message; }
25 std::string TakeLog() {
26 std::string result;
27 result.swap(log_);
28 return result;
29 }
30
31 private:
32 std::string log_;
33 };
scherkus (not reviewing) 2014/01/27 19:23:49 DISALLOW etc...
yhirano 2014/01/28 04:31:05 Done.
34
35 class FakeUsbMidiDevice : public UsbMidiDevice {
36 public:
37 explicit FakeUsbMidiDevice(Logger* logger) : logger_(logger) {}
38 virtual ~FakeUsbMidiDevice() {}
39
40 virtual std::vector<uint8> GetDescriptor() OVERRIDE {
41 logger_->AddLog("UsbMidiDevice::GetDescriptor\n");
42 return descriptor_;
43 }
44
45 virtual void Send(int endpoint_number,
46 const std::vector<uint8>& data) OVERRIDE {
47 logger_->AddLog("UsbMidiDevice::Send ");
48 logger_->AddLog(base::StringPrintf("endpoint = %d data =",
49 endpoint_number));
50 for (size_t i = 0; i < data.size(); ++i)
51 logger_->AddLog(base::StringPrintf(" 0x%02x", data[i]));
52 logger_->AddLog("\n");
53 }
54
55 void SetDescriptor(const std::vector<uint8> descriptor) {
56 descriptor_ = descriptor;
57 }
58
59 private:
60 std::vector<uint8> descriptor_;
61 Logger* logger_;
62 };
scherkus (not reviewing) 2014/01/27 19:23:49 DISALLOW etc...
yhirano 2014/01/28 04:31:05 Done.
63
64 class FakeMIDIManagerClient : public MIDIManagerClient {
65 public:
66 explicit FakeMIDIManagerClient(Logger* logger) : logger_(logger) {}
67 virtual ~FakeMIDIManagerClient() {}
68
69 virtual void ReceiveMIDIData(uint32 port_index,
70 const uint8* data,
71 size_t size,
72 double timestamp) OVERRIDE {
73 logger_->AddLog("MIDIManagerClient::ReceiveMIDIData ");
74 logger_->AddLog(base::StringPrintf("port_index = %d data =", port_index));
75 for (size_t i = 0; i < size; ++i)
76 logger_->AddLog(base::StringPrintf(" 0x%02x", data[i]));
77 logger_->AddLog("\n");
78 }
79
80 virtual void AccumulateMIDIBytesSent(size_t size) OVERRIDE {
81 logger_->AddLog("MIDIManagerClient::AccumulateMIDIBytesSent ");
82 logger_->AddLog(base::StringPrintf("size = %zu\n", size));
83 }
84
85 private:
86 Logger* logger_;
87 };
scherkus (not reviewing) 2014/01/27 19:23:49 DISALLOW etc...
yhirano 2014/01/28 04:31:05 Done.
88
89 class TestUsbMidiDeviceFactory : public UsbMidiDevice::Factory {
90 public:
91 TestUsbMidiDeviceFactory() {}
92 virtual ~TestUsbMidiDeviceFactory() {}
93 virtual void EnumerateDevices(UsbMidiDeviceDelegate* device,
94 Callback callback) OVERRIDE {
95 callback_ = callback;
96 }
97
98 Callback callback_;
99 };
scherkus (not reviewing) 2014/01/27 19:23:49 DISALLOW etc...
yhirano 2014/01/28 04:31:05 Done.
100
101 class MIDIManagerUsbTest : public ::testing::Test {
102 public:
103 MIDIManagerUsbTest() : is_initialized_(false), is_well_initialized_(false) {
104 scoped_ptr<TestUsbMidiDeviceFactory> factory(new TestUsbMidiDeviceFactory);
105 factory_ = factory.get();
106 manager_.reset(
107 new MIDIManagerUsb(factory.PassAs<UsbMidiDevice::Factory>()));
108 }
109 virtual ~MIDIManagerUsbTest() {
110 if (!logger_.TakeLog().empty()) {
111 ADD_FAILURE();
scherkus (not reviewing) 2014/01/27 19:23:49 I'd print out the contents of the log: ADD_FAILU
yhirano 2014/01/28 04:31:05 Done.
112 }
113 }
114
115 protected:
116 void Initialize() {
117 manager_->Initialize(base::Bind(&MIDIManagerUsbTest::OnInitializeDone,
118 base::Unretained(this)));
119 }
120
121 void OnInitializeDone(bool result) {
122 is_initialized_ = true;
scherkus (not reviewing) 2014/01/27 19:23:49 nit: I think a better name for these two would be
yhirano 2014/01/28 04:31:05 Done.
123 is_well_initialized_ = result;
124 }
125
126 bool is_initialized_;
127 bool is_well_initialized_;
128
129 scoped_ptr<MIDIManagerUsb> manager_;
130 // Owned by manager_.
131 TestUsbMidiDeviceFactory* factory_;
132 Logger logger_;
133 };
scherkus (not reviewing) 2014/01/27 19:23:49 DISALLOW etc...
yhirano 2014/01/28 04:31:05 Done.
134
135
136 TEST_F(MIDIManagerUsbTest, Initialize) {
137 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
138 uint8 descriptor[] = {
139 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
140 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
141 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
142 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
143 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
144 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
145 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
146 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
147 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
148 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
149 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
150 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
151 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
152 0x05, 0x25, 0x01, 0x01, 0x07,
153 };
154 device->SetDescriptor(ToVector(descriptor));
155
156 Initialize();
157 ScopedVector<UsbMidiDevice> devices;
158 devices.push_back(device.release());
159 EXPECT_FALSE(is_initialized_);
160 factory_->callback_.Run(true, &devices);
161 EXPECT_TRUE(is_initialized_);
162 EXPECT_TRUE(is_well_initialized_);
163
164 ASSERT_EQ(1u, manager_->input_ports().size());
165 ASSERT_EQ(2u, manager_->output_ports().size());
166 ASSERT_TRUE(manager_->input_stream());
167 std::vector<UsbMidiInputStream::JackUniqueKey> keys =
168 manager_->input_stream()->RegisteredJackKeys();
169 ASSERT_EQ(2u, manager_->output_streams().size());
170 EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id);
171 EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id);
172 ASSERT_EQ(1u, keys.size());
173 EXPECT_EQ(2, keys[0].endpoint_number);
174
175 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n", logger_.TakeLog());
176 }
177
178 TEST_F(MIDIManagerUsbTest, InitializeFail) {
179 Initialize();
180
181 EXPECT_FALSE(is_initialized_);
182 factory_->callback_.Run(false, NULL);
183 EXPECT_TRUE(is_initialized_);
184 EXPECT_FALSE(is_well_initialized_);
185 }
186
187 TEST_F(MIDIManagerUsbTest, InitializeFailBecauseOfInvalidDescriptor) {
188 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
189 uint8 descriptor[] = {0x04};
190 device->SetDescriptor(ToVector(descriptor));
191
192 Initialize();
193 ScopedVector<UsbMidiDevice> devices;
194 devices.push_back(device.release());
195 EXPECT_FALSE(is_initialized_);
196 factory_->callback_.Run(true, &devices);
197 EXPECT_TRUE(is_initialized_);
198 EXPECT_FALSE(is_well_initialized_);
199 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n", logger_.TakeLog());
200 }
201
202 TEST_F(MIDIManagerUsbTest, Send) {
203 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
204 FakeMIDIManagerClient client(&logger_);
205 uint8 descriptor[] = {
206 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
207 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
208 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
209 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
210 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
211 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
212 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
213 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
214 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
215 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
216 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
217 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
218 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
219 0x05, 0x25, 0x01, 0x01, 0x07,
220 };
221
222 device->SetDescriptor(ToVector(descriptor));
223 uint8 data[] = {
224 0x90, 0x45, 0x7f,
225 0xf0, 0x00, 0x01, 0xf7,
226 };
227
228 Initialize();
229 ScopedVector<UsbMidiDevice> devices;
230 devices.push_back(device.release());
231 EXPECT_FALSE(is_initialized_);
232 factory_->callback_.Run(true, &devices);
233 ASSERT_TRUE(is_initialized_);
234 ASSERT_TRUE(is_well_initialized_);
235
236 manager_->DispatchSendMIDIData(&client, 1, ToVector(data), 0);
237 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n"
238 "UsbMidiDevice::Send endpoint = 2 data = "
239 "0x19 0x90 0x45 0x7f "
240 "0x14 0xf0 0x00 0x01 "
241 "0x15 0xf7 0x00 0x00\n"
242 "MIDIManagerClient::AccumulateMIDIBytesSent size = 7\n",
243 logger_.TakeLog());
244 }
245
246 TEST_F(MIDIManagerUsbTest, Receive) {
247 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
248 FakeMIDIManagerClient client(&logger_);
249 uint8 descriptor[] = {
250 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
251 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
252 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
253 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
254 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
255 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
256 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
257 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
258 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
259 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
260 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
261 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
262 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
263 0x05, 0x25, 0x01, 0x01, 0x07,
264 };
265
266 device->SetDescriptor(ToVector(descriptor));
267 uint8 data[] = {
268 0x09, 0x90, 0x45, 0x7f,
269 0x04, 0xf0, 0x00, 0x01,
270 0x49, 0x90, 0x88, 0x99, // This data should be ignored (CN = 4).
271 0x05, 0xf7, 0x00, 0x00,
272 };
273
274 Initialize();
275 ScopedVector<UsbMidiDevice> devices;
276 UsbMidiDevice* device_raw = device.get();
277 devices.push_back(device.release());
278 EXPECT_FALSE(is_initialized_);
279 factory_->callback_.Run(true, &devices);
280 ASSERT_TRUE(is_initialized_);
281 ASSERT_TRUE(is_well_initialized_);
282
283 manager_->StartSession(&client);
284 manager_->ReceiveUsbMidiData(device_raw, 2, data, arraysize(data), 0);
285 manager_->EndSession(&client);
286
287 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n"
288 "MIDIManagerClient::ReceiveMIDIData port_index = 0 "
289 "data = 0x90 0x45 0x7f\n"
290 "MIDIManagerClient::ReceiveMIDIData port_index = 0 "
291 "data = 0xf0 0x00 0x01\n"
292 "MIDIManagerClient::ReceiveMIDIData port_index = 0 data = 0xf7\n",
293 logger_.TakeLog());
294 }
295
296 } // namespace
297
298 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698