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.
5 #include "media/midi/usb_midi_descriptor_parser.h"
7 #include "testing/gtest/include/gtest/gtest.h"
13 TEST(UsbMidiDescriptorParserTest
, ParseEmpty
) {
14 UsbMidiDescriptorParser parser
;
15 std::vector
<UsbMidiJack
> jacks
;
16 EXPECT_TRUE(parser
.Parse(nullptr, nullptr, 0, &jacks
));
17 EXPECT_TRUE(jacks
.empty());
20 TEST(UsbMidiDescriptorParserTest
, InvalidSize
) {
21 UsbMidiDescriptorParser parser
;
22 std::vector
<UsbMidiJack
> jacks
;
23 uint8 data
[] = {0x04};
24 EXPECT_FALSE(parser
.Parse(nullptr, data
, arraysize(data
), &jacks
));
25 EXPECT_TRUE(jacks
.empty());
28 TEST(UsbMidiDescriptorParserTest
, NonExistingJackIsAssociated
) {
29 UsbMidiDescriptorParser parser
;
30 std::vector
<UsbMidiJack
> jacks
;
31 // Jack id=1 is found in a CS_ENDPOINT descriptor, but there is no definition
34 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, 0x07,
35 0x24, 0x01, 0x00, 0x01, 0x07, 0x00, 0x05, 0x25, 0x01, 0x01,
38 EXPECT_FALSE(parser
.Parse(nullptr, data
, arraysize(data
), &jacks
));
39 EXPECT_TRUE(jacks
.empty());
42 TEST(UsbMidiDescriptorParserTest
,
43 JacksShouldBeIgnoredWhenParserIsNotParsingMidiInterface
) {
44 UsbMidiDescriptorParser parser
;
45 std::vector
<UsbMidiJack
> jacks
;
46 // a NON-MIDI INTERFACE descriptor followed by ENDPOINT and CS_ENDPOINT
47 // descriptors (Compare with the previous test case).
49 0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x02, 0x00, 0x00, 0x07,
50 0x24, 0x01, 0x00, 0x01, 0x07, 0x00, 0x05, 0x25, 0x01, 0x01,
53 EXPECT_TRUE(parser
.Parse(nullptr, data
, arraysize(data
), &jacks
));
54 EXPECT_TRUE(jacks
.empty());
57 TEST(UsbMidiDescriptorParserTest
, Parse
) {
58 UsbMidiDescriptorParser parser
;
59 std::vector
<UsbMidiJack
> jacks
;
60 // A complete device descriptor.
62 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
63 0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
64 0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
65 0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
66 0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
67 0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
68 0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
69 0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
70 0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
71 0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
72 0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
73 0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
74 0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
75 0x05, 0x25, 0x01, 0x01, 0x07,
77 EXPECT_TRUE(parser
.Parse(nullptr, data
, arraysize(data
), &jacks
));
78 ASSERT_EQ(3u, jacks
.size());
80 EXPECT_EQ(2u, jacks
[0].jack_id
);
81 EXPECT_EQ(0u, jacks
[0].cable_number
);
82 EXPECT_EQ(2u, jacks
[0].endpoint_number());
83 EXPECT_EQ(UsbMidiJack::DIRECTION_OUT
, jacks
[0].direction());
84 EXPECT_EQ(nullptr, jacks
[0].device
);
86 EXPECT_EQ(3u, jacks
[1].jack_id
);
87 EXPECT_EQ(1u, jacks
[1].cable_number
);
88 EXPECT_EQ(2u, jacks
[1].endpoint_number());
89 EXPECT_EQ(UsbMidiJack::DIRECTION_OUT
, jacks
[1].direction());
90 EXPECT_EQ(nullptr, jacks
[1].device
);
92 EXPECT_EQ(7u, jacks
[2].jack_id
);
93 EXPECT_EQ(0u, jacks
[2].cable_number
);
94 EXPECT_EQ(2u, jacks
[2].endpoint_number());
95 EXPECT_EQ(UsbMidiJack::DIRECTION_IN
, jacks
[2].direction());
96 EXPECT_EQ(nullptr, jacks
[2].device
);
99 TEST(UsbMidiDescriptorParserTest
, ParseDeviceInfoEmpty
) {
100 UsbMidiDescriptorParser parser
;
101 UsbMidiDescriptorParser::DeviceInfo info
;
102 EXPECT_FALSE(parser
.ParseDeviceInfo(nullptr, 0, &info
));
105 TEST(UsbMidiDescriptorParserTest
, ParseDeviceInfo
) {
106 UsbMidiDescriptorParser parser
;
107 UsbMidiDescriptorParser::DeviceInfo info
;
109 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x01, 0x23,
110 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x00, 0x0a,
112 EXPECT_TRUE(parser
.ParseDeviceInfo(data
, arraysize(data
), &info
));
114 EXPECT_EQ(0x2301, info
.vendor_id
);
115 EXPECT_EQ(0x6745, info
.product_id
);
116 EXPECT_EQ(0xab89, info
.bcd_device_version
);
117 EXPECT_EQ(0xcd, info
.manufacturer_index
);
118 EXPECT_EQ(0xef, info
.product_index
);