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_input_stream.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/time/time.h"
13 #include "media/midi/usb_midi_device.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using base::TimeTicks
;
23 class TestUsbMidiDevice
: public UsbMidiDevice
{
25 TestUsbMidiDevice() {}
26 ~TestUsbMidiDevice() override
{}
27 std::vector
<uint8
> GetDescriptors() override
{ return std::vector
<uint8
>(); }
28 std::string
GetManufacturer() override
{ return std::string(); }
29 std::string
GetProductName() override
{ return std::string(); }
30 std::string
GetDeviceVersion() override
{ return std::string(); }
31 void Send(int endpoint_number
, const std::vector
<uint8
>& data
) override
{}
34 DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDevice
);
37 class MockDelegate
: public UsbMidiInputStream::Delegate
{
40 ~MockDelegate() override
{}
41 void OnReceivedData(size_t jack_index
,
44 base::TimeTicks time
) override
{
45 for (size_t i
= 0; i
< size
; ++i
)
46 received_data_
+= base::StringPrintf("0x%02x ", data
[i
]);
47 received_data_
+= "\n";
50 const std::string
& received_data() const { return received_data_
; }
53 std::string received_data_
;
54 DISALLOW_COPY_AND_ASSIGN(MockDelegate
);
57 class UsbMidiInputStreamTest
: public ::testing::Test
{
59 UsbMidiInputStreamTest() {
60 stream_
.reset(new UsbMidiInputStream(&delegate_
));
62 stream_
->Add(UsbMidiJack(&device1_
,
65 135)); // endpoint_address
66 stream_
->Add(UsbMidiJack(&device2_
,
70 stream_
->Add(UsbMidiJack(&device2_
,
74 stream_
->Add(UsbMidiJack(&device1_
,
80 TestUsbMidiDevice device1_
;
81 TestUsbMidiDevice device2_
;
82 MockDelegate delegate_
;
83 scoped_ptr
<UsbMidiInputStream
> stream_
;
86 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStreamTest
);
89 TEST_F(UsbMidiInputStreamTest
, UnknownMessage
) {
91 0x40, 0xff, 0xff, 0xff,
92 0x41, 0xff, 0xff, 0xff,
95 stream_
->OnReceivedData(&device1_
, 7, data
, arraysize(data
), TimeTicks());
96 EXPECT_EQ("", delegate_
.received_data());
99 TEST_F(UsbMidiInputStreamTest
, SystemCommonMessage
) {
101 0x45, 0xf8, 0x00, 0x00,
102 0x42, 0xf3, 0x22, 0x00,
103 0x43, 0xf2, 0x33, 0x44,
106 stream_
->OnReceivedData(&device1_
, 7, data
, arraysize(data
), TimeTicks());
109 "0xf2 0x33 0x44 \n", delegate_
.received_data());
112 TEST_F(UsbMidiInputStreamTest
, SystemExclusiveMessage
) {
114 0x44, 0xf0, 0x11, 0x22,
115 0x45, 0xf7, 0x00, 0x00,
116 0x46, 0xf0, 0xf7, 0x00,
117 0x47, 0xf0, 0x33, 0xf7,
120 stream_
->OnReceivedData(&device1_
, 7, data
, arraysize(data
), TimeTicks());
121 EXPECT_EQ("0xf0 0x11 0x22 \n"
124 "0xf0 0x33 0xf7 \n", delegate_
.received_data());
127 TEST_F(UsbMidiInputStreamTest
, ChannelMessage
) {
129 0x48, 0x80, 0x11, 0x22,
130 0x49, 0x90, 0x33, 0x44,
131 0x4a, 0xa0, 0x55, 0x66,
132 0x4b, 0xb0, 0x77, 0x88,
133 0x4c, 0xc0, 0x99, 0x00,
134 0x4d, 0xd0, 0xaa, 0x00,
135 0x4e, 0xe0, 0xbb, 0xcc,
138 stream_
->OnReceivedData(&device1_
, 7, data
, arraysize(data
), TimeTicks());
139 EXPECT_EQ("0x80 0x11 0x22 \n"
145 "0xe0 0xbb 0xcc \n", delegate_
.received_data());
148 TEST_F(UsbMidiInputStreamTest
, SingleByteMessage
) {
150 0x4f, 0xf8, 0x00, 0x00,
153 stream_
->OnReceivedData(&device1_
, 7, data
, arraysize(data
), TimeTicks());
154 EXPECT_EQ("0xf8 \n", delegate_
.received_data());
157 TEST_F(UsbMidiInputStreamTest
, DispatchForMultipleCables
) {
159 0x4f, 0xf8, 0x00, 0x00,
160 0x5f, 0xfa, 0x00, 0x00,
161 0x6f, 0xfb, 0x00, 0x00,
164 stream_
->OnReceivedData(&device1_
, 7, data
, arraysize(data
), TimeTicks());
165 EXPECT_EQ("0xf8 \n0xfa \n", delegate_
.received_data());
168 TEST_F(UsbMidiInputStreamTest
, DispatchForDevice2
) {
169 uint8 data
[] = { 0x4f, 0xf8, 0x00, 0x00 };
171 stream_
->OnReceivedData(&device2_
, 7, data
, arraysize(data
), TimeTicks());
172 EXPECT_EQ("0xf8 \n", delegate_
.received_data());