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

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

Issue 261263002: Web MIDI: add an unit test to check MidiManager instantiation (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: win build fix Created 6 years, 7 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 | Annotate | Revision Log
« no previous file with comments | « media/midi/midi_manager_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/midi/midi_manager_usb.h" 5 #include "media/midi/midi_manager_usb.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
9 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
10 #include "base/time/time.h" 12 #include "base/time/time.h"
11 #include "media/midi/usb_midi_device.h" 13 #include "media/midi/usb_midi_device.h"
12 #include "testing/gtest/include/gtest/gtest.h" 14 #include "testing/gtest/include/gtest/gtest.h"
13 15
14 namespace media { 16 namespace media {
15 17
16 namespace { 18 namespace {
17 19
18 template<typename T, size_t N> 20 template<typename T, size_t N>
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 128
127 class MidiManagerUsbForTesting : public MidiManagerUsb { 129 class MidiManagerUsbForTesting : public MidiManagerUsb {
128 public: 130 public:
129 explicit MidiManagerUsbForTesting( 131 explicit MidiManagerUsbForTesting(
130 scoped_ptr<UsbMidiDevice::Factory> device_factory) 132 scoped_ptr<UsbMidiDevice::Factory> device_factory)
131 : MidiManagerUsb(device_factory.PassAs<UsbMidiDevice::Factory>()) {} 133 : MidiManagerUsb(device_factory.PassAs<UsbMidiDevice::Factory>()) {}
132 virtual ~MidiManagerUsbForTesting() {} 134 virtual ~MidiManagerUsbForTesting() {}
133 135
134 void CallCompleteInitialization(MidiResult result) { 136 void CallCompleteInitialization(MidiResult result) {
135 CompleteInitialization(result); 137 CompleteInitialization(result);
138 base::RunLoop run_loop;
139 run_loop.RunUntilIdle();
136 } 140 }
137 141
138 private: 142 private:
139 DISALLOW_COPY_AND_ASSIGN(MidiManagerUsbForTesting); 143 DISALLOW_COPY_AND_ASSIGN(MidiManagerUsbForTesting);
140 }; 144 };
141 145
142 class MidiManagerUsbTest : public ::testing::Test { 146 class MidiManagerUsbTest : public ::testing::Test {
143 public: 147 public:
144 MidiManagerUsbTest() { 148 MidiManagerUsbTest() : message_loop_(new base::MessageLoop) {
145 scoped_ptr<TestUsbMidiDeviceFactory> factory(new TestUsbMidiDeviceFactory); 149 scoped_ptr<TestUsbMidiDeviceFactory> factory(new TestUsbMidiDeviceFactory);
146 factory_ = factory.get(); 150 factory_ = factory.get();
147 manager_.reset( 151 manager_.reset(
148 new MidiManagerUsbForTesting(factory.PassAs<UsbMidiDevice::Factory>())); 152 new MidiManagerUsbForTesting(factory.PassAs<UsbMidiDevice::Factory>()));
149 } 153 }
150 virtual ~MidiManagerUsbTest() { 154 virtual ~MidiManagerUsbTest() {
151 std::string leftover_logs = logger_.TakeLog(); 155 std::string leftover_logs = logger_.TakeLog();
152 if (!leftover_logs.empty()) { 156 if (!leftover_logs.empty()) {
153 ADD_FAILURE() << "Log should be empty: " << leftover_logs; 157 ADD_FAILURE() << "Log should be empty: " << leftover_logs;
154 } 158 }
(...skipping 10 matching lines...) Expand all
165 } 169 }
166 170
167 bool IsInitializationCallbackInvoked() { 171 bool IsInitializationCallbackInvoked() {
168 return client_->complete_start_session_; 172 return client_->complete_start_session_;
169 } 173 }
170 174
171 MidiResult GetInitializationResult() { 175 MidiResult GetInitializationResult() {
172 return client_->result_; 176 return client_->result_;
173 } 177 }
174 178
179 void RunCallbackUntilCallbackInvoked(
180 bool result, UsbMidiDevice::Devices* devices) {
181 factory_->callback_.Run(result, devices);
182 base::RunLoop run_loop;
183 while (!client_->complete_start_session_)
184 run_loop.RunUntilIdle();
185 }
186
175 scoped_ptr<MidiManagerUsbForTesting> manager_; 187 scoped_ptr<MidiManagerUsbForTesting> manager_;
176 scoped_ptr<FakeMidiManagerClient> client_; 188 scoped_ptr<FakeMidiManagerClient> client_;
177 // Owned by manager_. 189 // Owned by manager_.
178 TestUsbMidiDeviceFactory* factory_; 190 TestUsbMidiDeviceFactory* factory_;
179 Logger logger_; 191 Logger logger_;
180 192
181 private: 193 private:
194 scoped_ptr<base::MessageLoop> message_loop_;
195
182 DISALLOW_COPY_AND_ASSIGN(MidiManagerUsbTest); 196 DISALLOW_COPY_AND_ASSIGN(MidiManagerUsbTest);
183 }; 197 };
184 198
185 199
186 TEST_F(MidiManagerUsbTest, Initialize) { 200 TEST_F(MidiManagerUsbTest, Initialize) {
187 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_)); 201 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
188 uint8 descriptor[] = { 202 uint8 descriptor[] = {
189 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, 203 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
190 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, 204 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
191 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00, 205 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
192 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00, 206 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
193 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02, 207 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
194 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51, 208 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
195 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02, 209 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
196 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09, 210 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
197 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24, 211 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
198 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03, 212 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
199 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02, 213 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
200 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02, 214 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
201 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00, 215 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
202 0x05, 0x25, 0x01, 0x01, 0x07, 216 0x05, 0x25, 0x01, 0x01, 0x07,
203 }; 217 };
204 device->SetDescriptor(ToVector(descriptor)); 218 device->SetDescriptor(ToVector(descriptor));
205 219
206 Initialize(); 220 Initialize();
207 ScopedVector<UsbMidiDevice> devices; 221 ScopedVector<UsbMidiDevice> devices;
208 devices.push_back(device.release()); 222 devices.push_back(device.release());
209 EXPECT_FALSE(IsInitializationCallbackInvoked()); 223 EXPECT_FALSE(IsInitializationCallbackInvoked());
210 factory_->callback_.Run(true, &devices); 224 RunCallbackUntilCallbackInvoked(true, &devices);
211 EXPECT_TRUE(IsInitializationCallbackInvoked());
212 EXPECT_EQ(MIDI_OK, GetInitializationResult()); 225 EXPECT_EQ(MIDI_OK, GetInitializationResult());
213 226
214 ASSERT_EQ(1u, manager_->input_ports().size()); 227 ASSERT_EQ(1u, manager_->input_ports().size());
215 ASSERT_EQ(2u, manager_->output_ports().size()); 228 ASSERT_EQ(2u, manager_->output_ports().size());
216 ASSERT_TRUE(manager_->input_stream()); 229 ASSERT_TRUE(manager_->input_stream());
217 std::vector<UsbMidiInputStream::JackUniqueKey> keys = 230 std::vector<UsbMidiInputStream::JackUniqueKey> keys =
218 manager_->input_stream()->RegisteredJackKeysForTesting(); 231 manager_->input_stream()->RegisteredJackKeysForTesting();
219 ASSERT_EQ(2u, manager_->output_streams().size()); 232 ASSERT_EQ(2u, manager_->output_streams().size());
220 EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id); 233 EXPECT_EQ(2u, manager_->output_streams()[0]->jack().jack_id);
221 EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id); 234 EXPECT_EQ(3u, manager_->output_streams()[1]->jack().jack_id);
222 ASSERT_EQ(1u, keys.size()); 235 ASSERT_EQ(1u, keys.size());
223 EXPECT_EQ(2, keys[0].endpoint_number); 236 EXPECT_EQ(2, keys[0].endpoint_number);
224 237
225 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n", logger_.TakeLog()); 238 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n", logger_.TakeLog());
226 } 239 }
227 240
228 TEST_F(MidiManagerUsbTest, InitializeFail) { 241 TEST_F(MidiManagerUsbTest, InitializeFail) {
229 Initialize(); 242 Initialize();
230 243
231 EXPECT_FALSE(IsInitializationCallbackInvoked()); 244 EXPECT_FALSE(IsInitializationCallbackInvoked());
232 factory_->callback_.Run(false, NULL); 245 RunCallbackUntilCallbackInvoked(false, NULL);
233 EXPECT_TRUE(IsInitializationCallbackInvoked());
234 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, GetInitializationResult()); 246 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, GetInitializationResult());
235 } 247 }
236 248
237 TEST_F(MidiManagerUsbTest, InitializeFailBecauseOfInvalidDescriptor) { 249 TEST_F(MidiManagerUsbTest, InitializeFailBecauseOfInvalidDescriptor) {
238 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_)); 250 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
239 uint8 descriptor[] = {0x04}; 251 uint8 descriptor[] = {0x04};
240 device->SetDescriptor(ToVector(descriptor)); 252 device->SetDescriptor(ToVector(descriptor));
241 253
242 Initialize(); 254 Initialize();
243 ScopedVector<UsbMidiDevice> devices; 255 ScopedVector<UsbMidiDevice> devices;
244 devices.push_back(device.release()); 256 devices.push_back(device.release());
245 EXPECT_FALSE(IsInitializationCallbackInvoked()); 257 EXPECT_FALSE(IsInitializationCallbackInvoked());
246 factory_->callback_.Run(true, &devices); 258 RunCallbackUntilCallbackInvoked(true, &devices);
247 EXPECT_TRUE(IsInitializationCallbackInvoked());
248 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, GetInitializationResult()); 259 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, GetInitializationResult());
249 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n", logger_.TakeLog()); 260 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n", logger_.TakeLog());
250 } 261 }
251 262
252 TEST_F(MidiManagerUsbTest, Send) { 263 TEST_F(MidiManagerUsbTest, Send) {
253 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_)); 264 scoped_ptr<FakeUsbMidiDevice> device(new FakeUsbMidiDevice(&logger_));
254 FakeMidiManagerClient client(&logger_); 265 FakeMidiManagerClient client(&logger_);
255 uint8 descriptor[] = { 266 uint8 descriptor[] = {
256 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a, 267 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
257 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02, 268 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
(...skipping 14 matching lines...) Expand all
272 device->SetDescriptor(ToVector(descriptor)); 283 device->SetDescriptor(ToVector(descriptor));
273 uint8 data[] = { 284 uint8 data[] = {
274 0x90, 0x45, 0x7f, 285 0x90, 0x45, 0x7f,
275 0xf0, 0x00, 0x01, 0xf7, 286 0xf0, 0x00, 0x01, 0xf7,
276 }; 287 };
277 288
278 Initialize(); 289 Initialize();
279 ScopedVector<UsbMidiDevice> devices; 290 ScopedVector<UsbMidiDevice> devices;
280 devices.push_back(device.release()); 291 devices.push_back(device.release());
281 EXPECT_FALSE(IsInitializationCallbackInvoked()); 292 EXPECT_FALSE(IsInitializationCallbackInvoked());
282 factory_->callback_.Run(true, &devices); 293 RunCallbackUntilCallbackInvoked(true, &devices);
283 EXPECT_TRUE(IsInitializationCallbackInvoked());
284 EXPECT_EQ(MIDI_OK, GetInitializationResult()); 294 EXPECT_EQ(MIDI_OK, GetInitializationResult());
285 ASSERT_EQ(2u, manager_->output_streams().size()); 295 ASSERT_EQ(2u, manager_->output_streams().size());
286 296
287 manager_->DispatchSendMidiData(&client, 1, ToVector(data), 0); 297 manager_->DispatchSendMidiData(&client, 1, ToVector(data), 0);
288 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n" 298 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n"
289 "UsbMidiDevice::Send endpoint = 2 data = " 299 "UsbMidiDevice::Send endpoint = 2 data = "
290 "0x19 0x90 0x45 0x7f " 300 "0x19 0x90 0x45 0x7f "
291 "0x14 0xf0 0x00 0x01 " 301 "0x14 0xf0 0x00 0x01 "
292 "0x15 0xf7 0x00 0x00\n" 302 "0x15 0xf7 0x00 0x00\n"
293 "MidiManagerClient::AccumulateMidiBytesSent size = 7\n", 303 "MidiManagerClient::AccumulateMidiBytesSent size = 7\n",
(...skipping 25 matching lines...) Expand all
319 0x04, 0xf0, 0x00, 0x01, 329 0x04, 0xf0, 0x00, 0x01,
320 0x49, 0x90, 0x88, 0x99, // This data should be ignored (CN = 4). 330 0x49, 0x90, 0x88, 0x99, // This data should be ignored (CN = 4).
321 0x05, 0xf7, 0x00, 0x00, 331 0x05, 0xf7, 0x00, 0x00,
322 }; 332 };
323 333
324 Initialize(); 334 Initialize();
325 ScopedVector<UsbMidiDevice> devices; 335 ScopedVector<UsbMidiDevice> devices;
326 UsbMidiDevice* device_raw = device.get(); 336 UsbMidiDevice* device_raw = device.get();
327 devices.push_back(device.release()); 337 devices.push_back(device.release());
328 EXPECT_FALSE(IsInitializationCallbackInvoked()); 338 EXPECT_FALSE(IsInitializationCallbackInvoked());
329 factory_->callback_.Run(true, &devices); 339 RunCallbackUntilCallbackInvoked(true, &devices);
330 EXPECT_TRUE(IsInitializationCallbackInvoked());
331 EXPECT_EQ(MIDI_OK, GetInitializationResult()); 340 EXPECT_EQ(MIDI_OK, GetInitializationResult());
332 341
333 manager_->ReceiveUsbMidiData(device_raw, 2, data, arraysize(data), 342 manager_->ReceiveUsbMidiData(device_raw, 2, data, arraysize(data),
334 base::TimeTicks()); 343 base::TimeTicks());
335 Finalize(); 344 Finalize();
336 345
337 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n" 346 EXPECT_EQ("UsbMidiDevice::GetDescriptor\n"
338 "MidiManagerClient::ReceiveMidiData port_index = 0 " 347 "MidiManagerClient::ReceiveMidiData port_index = 0 "
339 "data = 0x90 0x45 0x7f\n" 348 "data = 0x90 0x45 0x7f\n"
340 "MidiManagerClient::ReceiveMidiData port_index = 0 " 349 "MidiManagerClient::ReceiveMidiData port_index = 0 "
341 "data = 0xf0 0x00 0x01\n" 350 "data = 0xf0 0x00 0x01\n"
342 "MidiManagerClient::ReceiveMidiData port_index = 0 data = 0xf7\n", 351 "MidiManagerClient::ReceiveMidiData port_index = 0 data = 0xf7\n",
343 logger_.TakeLog()); 352 logger_.TakeLog());
344 } 353 }
345 354
346 } // namespace 355 } // namespace
347 356
348 } // namespace media 357 } // namespace media
OLDNEW
« no previous file with comments | « media/midi/midi_manager_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698