15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "media/midi/usb_midi_descriptor_parser.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace media {
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST(UsbMidiDescriptorParserTest, ParseEmpty) {
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  UsbMidiDescriptorParser parser;
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<UsbMidiJack> jacks;
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(parser.Parse(NULL, NULL, 0, &jacks));
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(jacks.empty());
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST(UsbMidiDescriptorParserTest, InvalidSize) {
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  UsbMidiDescriptorParser parser;
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<UsbMidiJack> jacks;
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint8 data[] = {0x04};
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(parser.Parse(NULL, data, arraysize(data), &jacks));
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(jacks.empty());
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST(UsbMidiDescriptorParserTest, NonExistingJackIsAssociated) {
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  UsbMidiDescriptorParser parser;
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<UsbMidiJack> jacks;
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Jack id=1 is found in a CS_ENDPOINT descriptor, but there is no definition
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // for the jack.
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint8 data[] = {
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00, 0x00, 0x07,
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x24, 0x01, 0x00, 0x01, 0x07, 0x00, 0x05, 0x25, 0x01, 0x01,
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x01,
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_FALSE(parser.Parse(NULL, data, arraysize(data), &jacks));
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(jacks.empty());
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST(UsbMidiDescriptorParserTest,
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     JacksShouldBeIgnoredWhenParserIsNotParsingMidiInterface) {
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  UsbMidiDescriptorParser parser;
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<UsbMidiJack> jacks;
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // a NON-MIDI INTERFACE descriptor followed by ENDPOINT and CS_ENDPOINT
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // descriptors (Compare with the previous test case).
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint8 data[] = {
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x09, 0x04, 0x01, 0x00, 0x02, 0x01, 0x02, 0x00, 0x00, 0x07,
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x24, 0x01, 0x00, 0x01, 0x07, 0x00, 0x05, 0x25, 0x01, 0x01,
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x01,
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(parser.Parse(NULL, data, arraysize(data), &jacks));
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(jacks.empty());
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)TEST(UsbMidiDescriptorParserTest, Parse) {
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  UsbMidiDescriptorParser parser;
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<UsbMidiJack> jacks;
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A complete device descriptor.
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  uint8 data[] = {
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08, 0x86, 0x1a,
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x2d, 0x75, 0x54, 0x02, 0x00, 0x02, 0x00, 0x01, 0x09, 0x02,
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x75, 0x00, 0x02, 0x01, 0x00, 0x80, 0x30, 0x09, 0x04, 0x00,
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x00, 0x00, 0x01, 0x01, 0x00, 0x00, 0x09, 0x24, 0x01, 0x00,
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x01, 0x09, 0x00, 0x01, 0x01, 0x09, 0x04, 0x01, 0x00, 0x02,
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x01, 0x03, 0x00, 0x00, 0x07, 0x24, 0x01, 0x00, 0x01, 0x51,
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x00, 0x06, 0x24, 0x02, 0x01, 0x02, 0x00, 0x06, 0x24, 0x02,
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x01, 0x03, 0x00, 0x06, 0x24, 0x02, 0x02, 0x06, 0x00, 0x09,
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x24, 0x03, 0x01, 0x07, 0x01, 0x06, 0x01, 0x00, 0x09, 0x24,
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x03, 0x02, 0x04, 0x01, 0x02, 0x01, 0x00, 0x09, 0x24, 0x03,
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x02, 0x05, 0x01, 0x03, 0x01, 0x00, 0x09, 0x05, 0x02, 0x02,
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x20, 0x00, 0x00, 0x00, 0x00, 0x06, 0x25, 0x01, 0x02, 0x02,
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x03, 0x09, 0x05, 0x82, 0x02, 0x20, 0x00, 0x00, 0x00, 0x00,
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    0x05, 0x25, 0x01, 0x01, 0x07,
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_TRUE(parser.Parse(NULL, data, arraysize(data), &jacks));
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ASSERT_EQ(3u, jacks.size());
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(2u, jacks[0].jack_id);
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(0u, jacks[0].cable_number);
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(2u, jacks[0].endpoint_number());
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(UsbMidiJack::DIRECTION_OUT, jacks[0].direction());
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(NULL, jacks[0].device);
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(3u, jacks[1].jack_id);
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(1u, jacks[1].cable_number);
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(2u, jacks[1].endpoint_number());
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(UsbMidiJack::DIRECTION_OUT, jacks[1].direction());
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(NULL, jacks[1].device);
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(7u, jacks[2].jack_id);
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(0u, jacks[2].cable_number);
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(2u, jacks[2].endpoint_number());
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(UsbMidiJack::DIRECTION_IN, jacks[2].direction());
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  EXPECT_EQ(NULL, jacks[2].device);
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace media
102