1 // Copyright 2015 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_alsa.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 class MidiManagerAlsaTest
: public ::testing::Test
{
13 void SetUp() override
{
14 // Pre-instantiate typical MidiPort instances that are often used in
17 // Inputs. port_input_0_ == port_input_1_.
18 port_input_0_
.reset(new MidiManagerAlsa::MidiPort(
19 "path", "id", 1, 2, 5, "client_name", "port_name", "manufacturer",
20 "version", MidiManagerAlsa::MidiPort::Type::kInput
));
21 port_input_1_
.reset(new MidiManagerAlsa::MidiPort(
22 "path", "id", 1, 2, 5, "client_name", "port_name", "manufacturer",
23 "version", MidiManagerAlsa::MidiPort::Type::kInput
));
24 port_input_minimal_
.reset(
25 new MidiManagerAlsa::MidiPort("", "", 0, 0, 0, "", "", "", "",
26 MidiManagerAlsa::MidiPort::Type::kInput
));
27 // Outputs. port_output_0_ == port_output_1_.
28 port_output_0_
.reset(new MidiManagerAlsa::MidiPort(
29 "path", "id", 1, 2, 5, "client_name", "port_name", "manufacturer",
30 "version", MidiManagerAlsa::MidiPort::Type::kOutput
));
31 port_output_1_
.reset(new MidiManagerAlsa::MidiPort(
32 "path", "id", 1, 2, 5, "client_name", "port_name", "manufacturer",
33 "version", MidiManagerAlsa::MidiPort::Type::kOutput
));
35 // MidiPort fields that differ from port_input_0_ in a single way each time.
36 // Used for testing the Match* and Find* methods.
37 port_input_0_alt_path_
.reset(new MidiManagerAlsa::MidiPort(
38 "path2", "id", 1, 2, 5, "client_name", "port_name", "manufacturer",
39 "version", MidiManagerAlsa::MidiPort::Type::kInput
));
40 port_input_0_alt_id_
.reset(new MidiManagerAlsa::MidiPort(
41 "path", "id2", 1, 2, 5, "client_name", "port_name", "manufacturer",
42 "version", MidiManagerAlsa::MidiPort::Type::kInput
));
43 port_input_0_alt_client_name_
.reset(new MidiManagerAlsa::MidiPort(
44 "path", "id", 1, 2, 5, "client_name2", "port_name", "manufacturer",
45 "version", MidiManagerAlsa::MidiPort::Type::kInput
));
46 port_input_0_alt_port_name_
.reset(new MidiManagerAlsa::MidiPort(
47 "path", "id", 1, 2, 5, "client_name", "port_name2", "manufacturer",
48 "version", MidiManagerAlsa::MidiPort::Type::kInput
));
49 port_input_0_alt_client_id_
.reset(new MidiManagerAlsa::MidiPort(
50 "path", "id", 2, 2, 5, "client_name", "port_name", "manufacturer",
51 "version", MidiManagerAlsa::MidiPort::Type::kInput
));
52 port_input_0_alt_port_id_
.reset(new MidiManagerAlsa::MidiPort(
53 "path", "id", 1, 3, 5, "client_name", "port_name", "manufacturer",
54 "version", MidiManagerAlsa::MidiPort::Type::kInput
));
55 port_input_0_alt_midi_device_
.reset(new MidiManagerAlsa::MidiPort(
56 "path", "id", 1, 2, 6, "client_name", "port_name", "manufacturer",
57 "version", MidiManagerAlsa::MidiPort::Type::kInput
));
59 // "No card" variants of above. For testing FindDisconnected.
60 port_input_0_no_card_
.reset(new MidiManagerAlsa::MidiPort(
61 "", "", 1, 2, -1, "client_name", "port_name", "manufacturer", "version",
62 MidiManagerAlsa::MidiPort::Type::kInput
));
63 port_input_1_no_card_
.reset(new MidiManagerAlsa::MidiPort(
64 "", "", 1, 2, -1, "client_name", "port_name", "manufacturer", "version",
65 MidiManagerAlsa::MidiPort::Type::kInput
));
66 port_output_0_no_card_
.reset(new MidiManagerAlsa::MidiPort(
67 "", "", 1, 2, -1, "client_name", "port_name", "manufacturer", "version",
68 MidiManagerAlsa::MidiPort::Type::kOutput
));
70 // No card variants of the alt variants from above. For more testing
71 // of Match* and Find*.
72 port_input_0_no_card_alt_client_name_
.reset(new MidiManagerAlsa::MidiPort(
73 "", "", 1, 2, -1, "client_name2", "port_name", "manufacturer",
74 "version", MidiManagerAlsa::MidiPort::Type::kInput
));
75 port_input_0_no_card_alt_port_name_
.reset(new MidiManagerAlsa::MidiPort(
76 "", "", 1, 2, -1, "client_name", "port_name2", "manufacturer",
77 "version", MidiManagerAlsa::MidiPort::Type::kInput
));
78 port_input_0_no_card_alt_client_id_
.reset(new MidiManagerAlsa::MidiPort(
79 "", "", 2, 2, -1, "client_name", "port_name", "manufacturer", "version",
80 MidiManagerAlsa::MidiPort::Type::kInput
));
81 port_input_0_no_card_alt_port_id_
.reset(new MidiManagerAlsa::MidiPort(
82 "", "", 1, 3, -1, "client_name", "port_name", "manufacturer", "version",
83 MidiManagerAlsa::MidiPort::Type::kInput
));
86 // Counts ports for help with testing ToMidiPortState().
87 int CountPorts(MidiManagerAlsa::TemporaryMidiPortState
& state
) {
89 for (auto it
= state
.begin(); it
!= state
.end(); ++it
)
94 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_
;
95 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_1_
;
96 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_minimal_
;
97 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_output_0_
;
98 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_output_1_
;
100 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_alt_path_
;
101 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_alt_id_
;
102 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_alt_client_name_
;
103 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_alt_port_name_
;
104 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_alt_client_id_
;
105 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_alt_port_id_
;
106 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_alt_midi_device_
;
108 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_no_card_
;
109 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_1_no_card_
;
110 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_output_0_no_card_
;
112 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_no_card_alt_client_name_
;
113 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_no_card_alt_port_name_
;
114 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_no_card_alt_client_id_
;
115 scoped_ptr
<MidiManagerAlsa::MidiPort
> port_input_0_no_card_alt_port_id_
;
117 // State fields to avoid declaring in test fixtures below.
118 MidiManagerAlsa::MidiPortState midi_port_state_0_
;
119 MidiManagerAlsa::MidiPortState midi_port_state_1_
;
120 MidiManagerAlsa::TemporaryMidiPortState temporary_midi_port_state_0_
;
121 MidiManagerAlsa::AlsaSeqState alsa_seq_state_0_
;
124 // Tests that ExtractManufacturerString works as expected.
125 TEST_F(MidiManagerAlsaTest
, ExtractManufacturer
) {
126 EXPECT_EQ("My\\x20Vendor", MidiManagerAlsa::ExtractManufacturerString(
127 "My\\x20Vendor", "1234", "My Vendor, Inc.",
128 "Card", "My Vendor Inc Card at bus"));
129 EXPECT_EQ("My Vendor", MidiManagerAlsa::ExtractManufacturerString(
130 "My Vendor", "1234", "My Vendor, Inc.", "Card",
131 "My Vendor Inc Card at bus"));
132 EXPECT_EQ("My Vendor, Inc.", MidiManagerAlsa::ExtractManufacturerString(
133 "1234", "1234", "My Vendor, Inc.", "Card",
134 "My Vendor Inc Card at bus"));
135 EXPECT_EQ("My Vendor Inc",
136 MidiManagerAlsa::ExtractManufacturerString(
137 "1234", "1234", "", "Card", "My Vendor Inc Card at bus"));
138 EXPECT_EQ("My Vendor Inc",
139 MidiManagerAlsa::ExtractManufacturerString(
140 "", "", "", "Card", "My Vendor Inc Card at bus"));
141 EXPECT_EQ("", MidiManagerAlsa::ExtractManufacturerString("1234", "1234", "",
142 "Card", "Longname"));
143 EXPECT_EQ("Keystation\\x20Mini\\x2032",
144 MidiManagerAlsa::ExtractManufacturerString(
145 "Keystation\\x20Mini\\x2032", "129d",
146 "Evolution Electronics, Ltd", "Keystation Mini 32",
147 "Keystation Mini 32 Keystation Mini 32 at"
148 " usb-0000:00:14.0-2.4.4, full speed"));
149 EXPECT_EQ("Keystation Mini 32",
150 MidiManagerAlsa::ExtractManufacturerString(
151 "Keystation Mini 32", "129d", "Evolution Electronics, Ltd",
152 "Keystation Mini 32",
153 "Keystation Mini 32 Keystation Mini 32 at"
154 " usb-0000:00:14.0-2.4.4, full speed"));
155 EXPECT_EQ("Keystation Mini 32", MidiManagerAlsa::ExtractManufacturerString(
156 "", "", "", "Keystation Mini 32",
157 "Keystation Mini 32 Keystation Mini 32 at"
158 " usb-0000:00:14.0-2.4.4, full speed"));
159 EXPECT_EQ("", MidiManagerAlsa::ExtractManufacturerString(
160 "", "", "", "Serial MIDI (UART16550A)",
161 "Serial MIDI (UART16550A) [Soundcanvas] at 0x3f8, irq 4"));
162 EXPECT_EQ("", MidiManagerAlsa::ExtractManufacturerString(
163 "", "", "", "VirMIDI", "Virtual MIDI Card 1"));
164 EXPECT_EQ("C-Media Electronics Inc",
165 MidiManagerAlsa::ExtractManufacturerString(
166 "", "0x13f6", "C-Media Electronics Inc", "C-Media CMI8738 MIDI",
167 "C-Media CMI8738 (model 55) at 0xd000, irq 19"));
168 EXPECT_EQ("C-Media Electronics Inc",
169 MidiManagerAlsa::ExtractManufacturerString(
170 "", "0x13f6", "C-Media Electronics Inc", "C-Media CMI8738 FM",
171 "C-Media CMI8738 (model 55) at 0xd000, irq 19"));
174 // Tests that verify proper serialization and generation of opaque key for
176 TEST_F(MidiManagerAlsaTest
, JSONPortMetadata
) {
178 "{\"clientId\":1,\"clientName\":\"client_name\",\"id\":\"id\","
179 "\"midiDevice\":5,\"path\":\"path\",\"portId\":2,\"portName\":\"port_"
180 "name\",\"type\":\"input\"}",
181 port_input_0_
->JSONValue());
183 EXPECT_EQ("03F255B7EE4D9D061597289CB16B45F997DBDB20D8E44429B052019C84E20A4A",
184 port_input_0_
->OpaqueKey());
187 "{\"clientId\":1,\"clientName\":\"client_name\",\"id\":\"id\","
188 "\"midiDevice\":5,\"path\":\"path\",\"portId\":2,\"portName\":\"port_"
189 "name\",\"type\":\"output\"}",
190 port_output_0_
->JSONValue());
191 EXPECT_EQ("3A3380FD64B8C79900C052D64C3F52E9ECD6537D00ECB02B8FA30032C0C03924",
192 port_output_0_
->OpaqueKey());
194 EXPECT_EQ("{\"clientId\":0,\"midiDevice\":0,\"portId\":0,\"type\":\"input\"}",
195 port_input_minimal_
->JSONValue());
196 EXPECT_EQ("3BC2A85598E5026D01DBCB022016C8A3362A9C7F912B88E303BF619C56D0C111",
197 port_input_minimal_
->OpaqueKey());
200 // Tests Match* methods.
201 TEST_F(MidiManagerAlsaTest
, MatchConnected
) {
202 // The query can be disconnected or connected, but the target
203 // must be connected.
204 port_input_1_
->set_connected(false);
205 EXPECT_TRUE(port_input_0_
->MatchConnected(*port_input_1_
.get()));
206 EXPECT_FALSE(port_input_1_
->MatchConnected(*port_input_0_
.get()));
209 EXPECT_FALSE(port_input_0_
->MatchConnected(*port_output_0_
.get()));
211 // Differing in 1 field. None should succeed.
212 EXPECT_FALSE(port_input_0_
->MatchConnected(*port_input_0_alt_path_
.get()));
213 EXPECT_FALSE(port_input_0_
->MatchConnected(*port_input_0_alt_id_
.get()));
215 port_input_0_
->MatchConnected(*port_input_0_alt_client_name_
.get()));
217 port_input_0_
->MatchConnected(*port_input_0_alt_port_name_
.get()));
219 port_input_0_
->MatchConnected(*port_input_0_alt_client_id_
.get()));
220 EXPECT_FALSE(port_input_0_
->MatchConnected(*port_input_0_alt_port_id_
.get()));
222 port_input_0_
->MatchConnected(*port_input_0_alt_midi_device_
.get()));
225 TEST_F(MidiManagerAlsaTest
, MatchCard1
) {
226 // The query can be disconnected or connected, but the target
227 // must be disonnected.
228 EXPECT_FALSE(port_input_0_
->MatchCardPass1(*port_input_1_
.get()));
229 port_input_0_
->set_connected(false);
230 EXPECT_TRUE(port_input_0_
->MatchCardPass1(*port_input_1_
.get()));
233 EXPECT_FALSE(port_input_0_
->MatchCardPass1(*port_output_0_
.get()));
235 // Tests matches differing in 1 field.
236 // client_name, port_name, client_id are ok to differ.
237 EXPECT_FALSE(port_input_0_
->MatchCardPass1(*port_input_0_alt_path_
.get()));
238 EXPECT_FALSE(port_input_0_
->MatchCardPass1(*port_input_0_alt_id_
.get()));
240 port_input_0_
->MatchCardPass1(*port_input_0_alt_client_name_
.get()));
242 port_input_0_
->MatchCardPass1(*port_input_0_alt_port_name_
.get()));
244 port_input_0_
->MatchCardPass1(*port_input_0_alt_client_id_
.get()));
245 EXPECT_FALSE(port_input_0_
->MatchCardPass1(*port_input_0_alt_port_id_
.get()));
247 port_input_0_
->MatchCardPass1(*port_input_0_alt_midi_device_
.get()));
250 TEST_F(MidiManagerAlsaTest
, MatchCard2
) {
251 // The query can be disconnected or connected, but the target
252 // must be disonnected.
253 EXPECT_FALSE(port_input_0_
->MatchCardPass2(*port_input_1_
.get()));
254 port_input_0_
->set_connected(false);
255 EXPECT_TRUE(port_input_0_
->MatchCardPass2(*port_input_1_
.get()));
258 EXPECT_FALSE(port_input_0_
->MatchCardPass2(*port_output_0_
.get()));
260 // Tests matches differing in 1 field.
261 // client_name, port_name, path, client_id are ok to differ.
262 EXPECT_TRUE(port_input_0_
->MatchCardPass2(*port_input_0_alt_path_
.get()));
263 EXPECT_FALSE(port_input_0_
->MatchCardPass2(*port_input_0_alt_id_
.get()));
265 port_input_0_
->MatchCardPass2(*port_input_0_alt_client_name_
.get()));
267 port_input_0_
->MatchCardPass2(*port_input_0_alt_port_name_
.get()));
269 port_input_0_
->MatchCardPass2(*port_input_0_alt_client_id_
.get()));
270 EXPECT_FALSE(port_input_0_
->MatchCardPass2(*port_input_0_alt_port_id_
.get()));
272 port_input_0_
->MatchCardPass2(*port_input_0_alt_midi_device_
.get()));
275 TEST_F(MidiManagerAlsaTest
, MatchNoCard1
) {
276 // The query can be disconnected or connected, but the target
277 // must be disonnected.
278 // path and id must be empty. midi_device must be -1.
279 EXPECT_FALSE(port_input_0_no_card_
->MatchNoCardPass1(*port_input_1_
.get()));
280 port_input_0_no_card_
->set_connected(false);
281 EXPECT_FALSE(port_input_0_no_card_
->MatchNoCardPass1(*port_input_1_
.get()));
283 port_input_0_no_card_
->MatchNoCardPass1(*port_input_1_no_card_
.get()));
287 port_input_0_no_card_
->MatchNoCardPass1(*port_output_0_no_card_
.get()));
289 // Tests matches differing in 1 field.
290 EXPECT_FALSE(port_input_0_no_card_
->MatchNoCardPass1(
291 *port_input_0_no_card_alt_client_name_
.get()));
292 EXPECT_FALSE(port_input_0_no_card_
->MatchNoCardPass1(
293 *port_input_0_no_card_alt_port_name_
.get()));
294 EXPECT_FALSE(port_input_0_no_card_
->MatchNoCardPass1(
295 *port_input_0_no_card_alt_client_id_
.get()));
296 EXPECT_FALSE(port_input_0_no_card_
->MatchNoCardPass1(
297 *port_input_0_no_card_alt_port_id_
.get()));
300 TEST_F(MidiManagerAlsaTest
, MatchNoCard2
) {
301 // The query can be disconnected or connected, but the target
302 // must be disonnected.
303 // path and id must be empty. midi_device must be -1.
304 EXPECT_FALSE(port_input_0_no_card_
->MatchNoCardPass2(*port_input_1_
.get()));
305 port_input_0_no_card_
->set_connected(false);
306 EXPECT_FALSE(port_input_0_no_card_
->MatchNoCardPass2(*port_input_1_
.get()));
308 port_input_0_no_card_
->MatchNoCardPass2(*port_input_1_no_card_
.get()));
312 port_input_0_no_card_
->MatchNoCardPass2(*port_output_0_no_card_
.get()));
314 // Tests matches differing in 1 field.
315 // client_id ok to differ.
316 EXPECT_FALSE(port_input_0_no_card_
->MatchNoCardPass2(
317 *port_input_0_no_card_alt_client_name_
.get()));
318 EXPECT_FALSE(port_input_0_no_card_
->MatchNoCardPass2(
319 *port_input_0_no_card_alt_port_name_
.get()));
320 EXPECT_TRUE(port_input_0_no_card_
->MatchNoCardPass2(
321 *port_input_0_no_card_alt_client_id_
.get()));
322 EXPECT_FALSE(port_input_0_no_card_
->MatchNoCardPass2(
323 *port_input_0_no_card_alt_port_id_
.get()));
326 // Tests that MidiPorts start connected.
327 TEST_F(MidiManagerAlsaTest
, PortStartsConnected
) {
328 EXPECT_TRUE(port_output_0_
->connected());
329 EXPECT_TRUE(port_input_0_
->connected());
332 // Tests that the web_port_index gets updated by MidiPortState.
333 TEST_F(MidiManagerAlsaTest
, PortIndexSet
) {
334 auto* port_input_0_tracking_pointer
= port_input_0_
.get();
335 auto* port_output_0_tracking_pointer
= port_output_0_
.get();
336 auto* port_input_1_tracking_pointer
= port_input_1_
.get();
337 auto* port_output_1_tracking_pointer
= port_input_1_
.get();
339 // Explicitly initialize web_port_index.
340 port_input_1_
->set_web_port_index(5000);
341 port_output_1_
->set_web_port_index(5000);
343 midi_port_state_0_
.Insert(port_input_0_
.Pass());
344 midi_port_state_0_
.Insert(port_output_0_
.Pass());
345 midi_port_state_0_
.Insert(port_input_1_
.Pass());
346 midi_port_state_0_
.Insert(port_output_1_
.Pass());
348 // First port of each type has index of 0.
349 EXPECT_EQ(0U, port_input_0_tracking_pointer
->web_port_index());
350 EXPECT_EQ(0U, port_output_0_tracking_pointer
->web_port_index());
351 // Second port of each type has index of 1.
352 EXPECT_EQ(1U, port_input_1_tracking_pointer
->web_port_index());
353 EXPECT_EQ(1U, port_output_1_tracking_pointer
->web_port_index());
356 // Tests that the web_port_index is not updated by TemporaryMidiPortState.
357 TEST_F(MidiManagerAlsaTest
, PortIndexNotSet
) {
358 auto* port_input_0_tracking_pointer
= port_input_0_
.get();
359 auto* port_output_0_tracking_pointer
= port_output_0_
.get();
360 auto* port_input_1_tracking_pointer
= port_input_1_
.get();
361 auto* port_output_1_tracking_pointer
= port_input_1_
.get();
363 // Explicitly initialize web_port_index.
364 port_input_1_
->set_web_port_index(5000);
365 port_output_1_
->set_web_port_index(5000);
367 temporary_midi_port_state_0_
.Insert(port_input_0_
.Pass());
368 temporary_midi_port_state_0_
.Insert(port_output_0_
.Pass());
369 temporary_midi_port_state_0_
.Insert(port_input_1_
.Pass());
370 temporary_midi_port_state_0_
.Insert(port_output_1_
.Pass());
372 // web_port_index is untouched.
373 EXPECT_EQ(0U, port_input_0_tracking_pointer
->web_port_index());
374 EXPECT_EQ(0U, port_output_0_tracking_pointer
->web_port_index());
375 EXPECT_EQ(5000U, port_input_1_tracking_pointer
->web_port_index());
376 EXPECT_EQ(5000U, port_output_1_tracking_pointer
->web_port_index());
379 // Tests that inputs and outputs stay separate in MidiPortState.
380 TEST_F(MidiManagerAlsaTest
, SeparateInputOutput
) {
381 auto* port_input_0_tracking_pointer
= port_input_0_
.get();
382 auto* port_output_0_tracking_pointer
= port_output_0_
.get();
383 auto* port_input_1_tracking_pointer
= port_input_1_
.get();
384 auto* port_output_1_tracking_pointer
= port_input_1_
.get();
386 // First port of each type has index of 0.
387 EXPECT_EQ(0U, midi_port_state_0_
.Insert(port_input_0_
.Pass()));
388 EXPECT_EQ(0U, midi_port_state_0_
.Insert(port_output_0_
.Pass()));
390 // Second port of each type has index of 1.
391 EXPECT_EQ(1U, midi_port_state_0_
.Insert(port_input_1_
.Pass()));
392 EXPECT_EQ(1U, midi_port_state_0_
.Insert(port_output_1_
.Pass()));
394 // Check again that the field matches what was returned.
395 EXPECT_EQ(0U, port_input_0_tracking_pointer
->web_port_index());
396 EXPECT_EQ(0U, port_output_0_tracking_pointer
->web_port_index());
397 EXPECT_EQ(1U, port_input_1_tracking_pointer
->web_port_index());
398 EXPECT_EQ(1U, port_output_1_tracking_pointer
->web_port_index());
401 // Tests FindConnected.
402 TEST_F(MidiManagerAlsaTest
, FindConnected
) {
403 auto* port_input_0_tracking_pointer
= port_input_0_
.get();
404 auto* port_input_1_tracking_pointer
= port_input_1_
.get();
406 // Insert port_input_0.
407 midi_port_state_0_
.Insert(port_input_0_
.Pass());
408 // Look for port_input_1 (every field matches port_input_0).
409 auto it
= midi_port_state_0_
.FindConnected(*port_input_1_tracking_pointer
);
410 EXPECT_EQ(port_input_0_tracking_pointer
, *it
);
411 // Look for something else that we won't find.
412 EXPECT_EQ(midi_port_state_0_
.end(),
413 midi_port_state_0_
.FindConnected(*port_input_0_alt_path_
));
416 TEST_F(MidiManagerAlsaTest
, FindConnected2
) {
417 auto* port_input_0_tracking_pointer
= port_input_0_
.get();
418 auto* port_input_1_tracking_pointer
= port_input_1_
.get();
420 // Insert some stuff.
421 midi_port_state_0_
.Insert(port_input_0_alt_path_
.Pass());
422 midi_port_state_0_
.Insert(port_input_0_alt_id_
.Pass());
423 midi_port_state_0_
.Insert(port_input_0_alt_client_name_
.Pass());
424 // Insert port_input_0.
425 midi_port_state_0_
.Insert(port_input_0_
.Pass());
426 // Insert some more stuff.
427 midi_port_state_0_
.Insert(port_input_0_alt_port_id_
.Pass());
428 // Look for port_input_1 (matches to port_input_0).
429 auto it
= midi_port_state_0_
.FindConnected(*port_input_1_tracking_pointer
);
430 EXPECT_EQ(port_input_0_tracking_pointer
, *it
);
431 // Look for something else that we won't find.
432 EXPECT_EQ(midi_port_state_0_
.end(),
433 midi_port_state_0_
.FindConnected(*port_input_minimal_
));
436 TEST_F(MidiManagerAlsaTest
, FindConnected3
) {
437 // midi_port_state_0_ is empty to start.
438 EXPECT_EQ(midi_port_state_0_
.end(),
439 midi_port_state_0_
.FindConnected(*port_input_minimal_
));
442 // Tests FindDisconnected.
443 TEST_F(MidiManagerAlsaTest
, FindDisconnected
) {
444 // midi_port_state_0_ is empty to start.
445 EXPECT_EQ(midi_port_state_0_
.end(),
446 midi_port_state_0_
.FindDisconnected(*port_input_minimal_
));
449 TEST_F(MidiManagerAlsaTest
, FindDisconnected2
) {
450 auto* port_input_0_tracking_pointer
= port_input_0_
.get();
451 auto* port_input_1_tracking_pointer
= port_input_1_
.get();
452 auto* port_input_1_no_card_tracking_pointer
= port_input_1_no_card_
.get();
454 // Ports need to be disconnected to find them.
455 port_input_0_alt_id_
->set_connected(false);
456 port_input_0_alt_path_
->set_connected(false);
457 port_input_0_
->set_connected(false);
459 // Insert some stuff.
460 midi_port_state_0_
.Insert(port_input_0_alt_id_
.Pass());
461 midi_port_state_0_
.Insert(port_input_0_alt_path_
.Pass());
462 // Insert port_input_0.
463 midi_port_state_0_
.Insert(port_input_0_
.Pass());
465 // Add "no card" stuff.
466 port_input_1_no_card_
->set_connected(false);
467 midi_port_state_0_
.Insert(port_input_1_no_card_
.Pass());
469 // Insert some more stuff.
470 midi_port_state_0_
.Insert(port_input_0_alt_port_id_
.Pass());
472 // Look for port_input_1, should trigger exact match.
473 EXPECT_EQ(port_input_0_tracking_pointer
, *midi_port_state_0_
.FindDisconnected(
474 *port_input_1_tracking_pointer
));
476 // Look for no card exact match.
477 EXPECT_EQ(port_input_1_no_card_tracking_pointer
,
478 *midi_port_state_0_
.FindDisconnected(*port_input_0_no_card_
.get()));
480 // Look for something else that we won't find.
481 EXPECT_EQ(midi_port_state_0_
.end(),
482 midi_port_state_0_
.FindDisconnected(*port_input_minimal_
));
485 TEST_F(MidiManagerAlsaTest
, FindDisconnected3
) {
486 auto* port_input_0_tracking_pointer
= port_input_0_
.get();
487 auto* port_input_0_alt_path_tracking_pointer
= port_input_0_alt_path_
.get();
488 auto* port_input_1_no_card_tracking_pointer
= port_input_1_no_card_
.get();
490 // Ports need to be disconnected to find them.
491 port_input_0_alt_path_
->set_connected(false);
492 port_input_0_
->set_connected(false);
494 // Insert some stuff.
495 midi_port_state_0_
.Insert(port_input_0_alt_path_
.Pass());
496 midi_port_state_0_
.Insert(port_input_0_alt_id_
.Pass());
498 // Add no card stuff.
499 port_input_1_no_card_
->set_connected(false);
500 midi_port_state_0_
.Insert(port_input_1_no_card_
.Pass());
502 // Look for port_input_0, should find port_input_0_alt_path.
504 port_input_0_alt_path_tracking_pointer
,
505 *midi_port_state_0_
.FindDisconnected(*port_input_0_tracking_pointer
));
507 // Look for no card partial match.
508 EXPECT_EQ(port_input_1_no_card_tracking_pointer
,
509 *midi_port_state_0_
.FindDisconnected(
510 *port_input_0_no_card_alt_client_id_
.get()));
513 EXPECT_EQ(midi_port_state_0_
.end(),
514 midi_port_state_0_
.FindDisconnected(
515 *port_input_0_no_card_alt_port_id_
.get()));
517 // Look for something else that we won't find.
518 EXPECT_EQ(midi_port_state_0_
.end(),
519 midi_port_state_0_
.FindDisconnected(*port_input_minimal_
));
522 // Tests AlsaSeqState -> MidiPortState.
523 TEST_F(MidiManagerAlsaTest
, ToMidiPortState
) {
525 EXPECT_EQ(0, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
527 // Still empty, because there are no ports yet.
528 alsa_seq_state_0_
.ClientStart(0, "0", SND_SEQ_KERNEL_CLIENT
);
529 EXPECT_EQ(0, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
531 // Add a port, now it has 1 item when converted.
532 alsa_seq_state_0_
.PortStart(
533 0, 0, "0:0", MidiManagerAlsa::AlsaSeqState::PortDirection::kInput
, true);
534 EXPECT_EQ(1, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
536 // Close client. This closes its ports and returns count to 0.
537 alsa_seq_state_0_
.ClientExit(0);
538 EXPECT_EQ(0, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
540 // Add another port, without client. This does nothing.
541 alsa_seq_state_0_
.PortStart(
542 0, 0, "0:0", MidiManagerAlsa::AlsaSeqState::PortDirection::kInput
, true);
543 EXPECT_EQ(0, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
545 // Close client again. This does nothing.
546 alsa_seq_state_0_
.ClientExit(0);
547 EXPECT_EQ(0, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
549 // Add duplex port. This will add 2 ports when converted.
550 alsa_seq_state_0_
.ClientStart(0, "0", SND_SEQ_KERNEL_CLIENT
);
551 alsa_seq_state_0_
.PortStart(
552 0, 0, "0:0", MidiManagerAlsa::AlsaSeqState::PortDirection::kDuplex
, true);
553 EXPECT_EQ(2, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
555 // Add an output port. Now we are at 3.
556 alsa_seq_state_0_
.PortStart(
557 0, 1, "0:1", MidiManagerAlsa::AlsaSeqState::PortDirection::kOutput
, true);
558 EXPECT_EQ(3, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
560 // Add another client. Still at 3.
561 alsa_seq_state_0_
.ClientStart(1, "1", SND_SEQ_KERNEL_CLIENT
);
562 EXPECT_EQ(3, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
564 // Add a port. Now at 4.
565 alsa_seq_state_0_
.PortStart(
566 1, 0, "1:0", MidiManagerAlsa::AlsaSeqState::PortDirection::kInput
, true);
567 EXPECT_EQ(4, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
569 // Add a duplicate port. Still at 4.
570 alsa_seq_state_0_
.PortStart(
571 1, 0, "1:0", MidiManagerAlsa::AlsaSeqState::PortDirection::kInput
, true);
572 EXPECT_EQ(4, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
574 // Add a duplicate client. This will close the ports from the previous client.
575 alsa_seq_state_0_
.ClientStart(1, "1", SND_SEQ_KERNEL_CLIENT
);
576 EXPECT_EQ(3, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
578 // Remove a duplex port. This reduces count by 2.
579 alsa_seq_state_0_
.PortExit(0, 0);
580 EXPECT_EQ(1, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
582 // Remove a non-existent port. No change.
583 alsa_seq_state_0_
.PortExit(0, 0);
584 EXPECT_EQ(1, CountPorts(*alsa_seq_state_0_
.ToMidiPortState().get()));
586 // Verify the last entry.
587 EXPECT_TRUE((*alsa_seq_state_0_
.ToMidiPortState()->begin())
588 ->MatchConnected(MidiManagerAlsa::MidiPort(
589 "", "", 0, 1, -1, "0", "0:1", "", "",
590 MidiManagerAlsa::MidiPort::Type::kOutput
)));
593 // TODO(agoode): Test old -> new state event generation, using mocks.