usb_midi_descriptor_parser_unittest.cc revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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/usb_midi_descriptor_parser.h"
6
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace media {
10
11namespace {
12
13TEST(UsbMidiDescriptorParserTest, ParseEmpty) {
14  UsbMidiDescriptorParser parser;
15  std::vector<UsbMidiJack> jacks;
16  EXPECT_TRUE(parser.Parse(NULL, NULL, 0, &jacks));
17  EXPECT_TRUE(jacks.empty());
18}
19
20TEST(UsbMidiDescriptorParserTest, InvalidSize) {
21  UsbMidiDescriptorParser parser;
22  std::vector<UsbMidiJack> jacks;
23  uint8 data[] = {0x04};
24  EXPECT_FALSE(parser.Parse(NULL, data, arraysize(data), &jacks));
25  EXPECT_TRUE(jacks.empty());
26}
27
28TEST(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
32  // for the jack.
33  uint8 data[] = {
34    0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, 0x07,
35    0x24, 0x01, 0x00, 0x01, 0x07, 0x00, 0x05, 0x25, 0x01, 0x01,
36    0x01,
37  };
38  EXPECT_FALSE(parser.Parse(NULL, data, arraysize(data), &jacks));
39  EXPECT_TRUE(jacks.empty());
40}
41
42TEST(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).
48  uint8 data[] = {
49    0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x02, 0x00, 0x00, 0x07,
50    0x24, 0x01, 0x00, 0x01, 0x07, 0x00, 0x05, 0x25, 0x01, 0x01,
51    0x01,
52  };
53  EXPECT_TRUE(parser.Parse(NULL, data, arraysize(data), &jacks));
54  EXPECT_TRUE(jacks.empty());
55}
56
57TEST(UsbMidiDescriptorParserTest, Parse) {
58  UsbMidiDescriptorParser parser;
59  std::vector<UsbMidiJack> jacks;
60  // A complete device descriptor.
61  uint8 data[] = {
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,
76  };
77  EXPECT_TRUE(parser.Parse(NULL, data, arraysize(data), &jacks));
78  ASSERT_EQ(3u, jacks.size());
79
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(NULL, jacks[0].device);
85
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(NULL, jacks[1].device);
91
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(NULL, jacks[2].device);
97}
98
99}  // namespace
100
101}  // namespace media
102