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_output_stream.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/stringprintf.h"
12 #include "media/midi/usb_midi_device.h"
13 #include "testing/gtest/include/gtest/gtest.h"
20 template<typename T
, size_t N
>
21 std::vector
<T
> ToVector(const T((&array
)[N
])) {
22 return std::vector
<T
>(array
, array
+ N
);
25 class MockUsbMidiDevice
: public UsbMidiDevice
{
27 MockUsbMidiDevice() {}
28 ~MockUsbMidiDevice() override
{}
30 std::vector
<uint8
> GetDescriptors() override
{ return std::vector
<uint8
>(); }
31 std::string
GetManufacturer() override
{ return std::string(); }
32 std::string
GetProductName() override
{ return std::string(); }
33 std::string
GetDeviceVersion() override
{ return std::string(); }
35 void Send(int endpoint_number
, const std::vector
<uint8
>& data
) override
{
36 for (size_t i
= 0; i
< data
.size(); ++i
) {
37 log_
+= base::StringPrintf("0x%02x ", data
[i
]);
39 log_
+= base::StringPrintf("(endpoint = %d)\n", endpoint_number
);
42 const std::string
& log() const { return log_
; }
44 void ClearLog() { log_
= ""; }
49 DISALLOW_COPY_AND_ASSIGN(MockUsbMidiDevice
);
52 class UsbMidiOutputStreamTest
: public ::testing::Test
{
54 UsbMidiOutputStreamTest() {
55 UsbMidiJack
jack(&device_
, 1, 2, 4);
56 stream_
.reset(new UsbMidiOutputStream(jack
));
59 MockUsbMidiDevice device_
;
60 scoped_ptr
<UsbMidiOutputStream
> stream_
;
63 DISALLOW_COPY_AND_ASSIGN(UsbMidiOutputStreamTest
);
66 TEST_F(UsbMidiOutputStreamTest
, SendEmpty
) {
67 stream_
->Send(std::vector
<uint8
>());
69 EXPECT_EQ("", device_
.log());
72 TEST_F(UsbMidiOutputStreamTest
, SendNoteOn
) {
73 uint8 data
[] = { 0x90, 0x45, 0x7f};
75 stream_
->Send(ToVector(data
));
76 EXPECT_EQ("0x29 0x90 0x45 0x7f (endpoint = 4)\n", device_
.log());
79 TEST_F(UsbMidiOutputStreamTest
, SendNoteOnPending
) {
80 stream_
->Send(std::vector
<uint8
>(1, 0x90));
81 stream_
->Send(std::vector
<uint8
>(1, 0x45));
82 EXPECT_EQ("", device_
.log());
84 stream_
->Send(std::vector
<uint8
>(1, 0x7f));
85 EXPECT_EQ("0x29 0x90 0x45 0x7f (endpoint = 4)\n", device_
.log());
88 stream_
->Send(std::vector
<uint8
>(1, 0x90));
89 stream_
->Send(std::vector
<uint8
>(1, 0x45));
90 EXPECT_EQ("", device_
.log());
93 TEST_F(UsbMidiOutputStreamTest
, SendNoteOnBurst
) {
94 uint8 data1
[] = { 0x90, };
95 uint8 data2
[] = { 0x45, 0x7f, 0x90, 0x45, 0x71, 0x90, 0x45, 0x72, 0x90, };
97 stream_
->Send(ToVector(data1
));
98 stream_
->Send(ToVector(data2
));
99 EXPECT_EQ("0x29 0x90 0x45 0x7f "
100 "0x29 0x90 0x45 0x71 "
101 "0x29 0x90 0x45 0x72 (endpoint = 4)\n", device_
.log());
104 TEST_F(UsbMidiOutputStreamTest
, SendNoteOff
) {
105 uint8 data
[] = { 0x80, 0x33, 0x44, };
107 stream_
->Send(ToVector(data
));
108 EXPECT_EQ("0x28 0x80 0x33 0x44 (endpoint = 4)\n", device_
.log());
111 TEST_F(UsbMidiOutputStreamTest
, SendPolyphonicKeyPress
) {
112 uint8 data
[] = { 0xa0, 0x33, 0x44, };
114 stream_
->Send(ToVector(data
));
115 EXPECT_EQ("0x2a 0xa0 0x33 0x44 (endpoint = 4)\n", device_
.log());
118 TEST_F(UsbMidiOutputStreamTest
, SendControlChange
) {
119 uint8 data
[] = { 0xb7, 0x33, 0x44, };
121 stream_
->Send(ToVector(data
));
122 EXPECT_EQ("0x2b 0xb7 0x33 0x44 (endpoint = 4)\n", device_
.log());
125 TEST_F(UsbMidiOutputStreamTest
, SendProgramChange
) {
126 uint8 data
[] = { 0xc2, 0x33, };
128 stream_
->Send(ToVector(data
));
129 EXPECT_EQ("0x2c 0xc2 0x33 0x00 (endpoint = 4)\n", device_
.log());
132 TEST_F(UsbMidiOutputStreamTest
, SendChannelPressure
) {
133 uint8 data
[] = { 0xd1, 0x33, 0x44, };
135 stream_
->Send(ToVector(data
));
136 EXPECT_EQ("0x2d 0xd1 0x33 0x44 (endpoint = 4)\n", device_
.log());
139 TEST_F(UsbMidiOutputStreamTest
, SendPitchWheelChange
) {
140 uint8 data
[] = { 0xe4, 0x33, 0x44, };
142 stream_
->Send(ToVector(data
));
143 EXPECT_EQ("0x2e 0xe4 0x33 0x44 (endpoint = 4)\n", device_
.log());
146 TEST_F(UsbMidiOutputStreamTest
, SendTwoByteSysEx
) {
147 uint8 data
[] = { 0xf0, 0xf7, };
149 stream_
->Send(ToVector(data
));
150 EXPECT_EQ("0x26 0xf0 0xf7 0x00 (endpoint = 4)\n", device_
.log());
153 TEST_F(UsbMidiOutputStreamTest
, SendThreeByteSysEx
) {
154 uint8 data
[] = { 0xf0, 0x4f, 0xf7, };
156 stream_
->Send(ToVector(data
));
157 EXPECT_EQ("0x27 0xf0 0x4f 0xf7 (endpoint = 4)\n", device_
.log());
160 TEST_F(UsbMidiOutputStreamTest
, SendFourByteSysEx
) {
161 uint8 data
[] = { 0xf0, 0x00, 0x01, 0xf7, };
163 stream_
->Send(ToVector(data
));
164 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
165 "0x25 0xf7 0x00 0x00 (endpoint = 4)\n", device_
.log());
168 TEST_F(UsbMidiOutputStreamTest
, SendFiveByteSysEx
) {
169 uint8 data
[] = { 0xf0, 0x00, 0x01, 0x02, 0xf7, };
171 stream_
->Send(ToVector(data
));
172 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
173 "0x26 0x02 0xf7 0x00 (endpoint = 4)\n", device_
.log());
176 TEST_F(UsbMidiOutputStreamTest
, SendSixByteSysEx
) {
177 uint8 data
[] = { 0xf0, 0x00, 0x01, 0x02, 0x03, 0xf7, };
179 stream_
->Send(ToVector(data
));
180 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
181 "0x27 0x02 0x03 0xf7 (endpoint = 4)\n", device_
.log());
184 TEST_F(UsbMidiOutputStreamTest
, SendPendingSysEx
) {
185 uint8 data1
[] = { 0xf0, 0x33, };
186 uint8 data2
[] = { 0x44, 0x55, 0x66, };
187 uint8 data3
[] = { 0x77, 0x88, 0x99, 0xf7, };
189 stream_
->Send(ToVector(data1
));
190 EXPECT_EQ("", device_
.log());
192 stream_
->Send(ToVector(data2
));
193 EXPECT_EQ("0x24 0xf0 0x33 0x44 (endpoint = 4)\n", device_
.log());
196 stream_
->Send(ToVector(data3
));
197 EXPECT_EQ("0x24 0x55 0x66 0x77 0x27 0x88 0x99 0xf7 (endpoint = 4)\n",
201 TEST_F(UsbMidiOutputStreamTest
, SendNoteOnAfterSysEx
) {
202 uint8 data
[] = { 0xf0, 0x00, 0x01, 0x02, 0x03, 0xf7, 0x90, 0x44, 0x33, };
204 stream_
->Send(ToVector(data
));
205 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
206 "0x27 0x02 0x03 0xf7 "
207 "0x29 0x90 0x44 0x33 (endpoint = 4)\n", device_
.log());
210 TEST_F(UsbMidiOutputStreamTest
, SendTimeCodeQuarterFrame
) {
211 uint8 data
[] = { 0xf1, 0x22, };
213 stream_
->Send(ToVector(data
));
214 EXPECT_EQ("0x22 0xf1 0x22 0x00 (endpoint = 4)\n", device_
.log());
217 TEST_F(UsbMidiOutputStreamTest
, SendSongPositionPointer
) {
218 uint8 data
[] = { 0xf2, 0x22, 0x33, };
220 stream_
->Send(ToVector(data
));
221 EXPECT_EQ("0x23 0xf2 0x22 0x33 (endpoint = 4)\n", device_
.log());
224 TEST_F(UsbMidiOutputStreamTest
, SendSongSelect
) {
225 uint8 data
[] = { 0xf3, 0x22, };
227 stream_
->Send(ToVector(data
));
228 EXPECT_EQ("0x22 0xf3 0x22 0x00 (endpoint = 4)\n", device_
.log());
231 TEST_F(UsbMidiOutputStreamTest
, TuneRequest
) {
232 uint8 data
[] = { 0xf6, };
234 stream_
->Send(ToVector(data
));
235 EXPECT_EQ("0x25 0xf6 0x00 0x00 (endpoint = 4)\n", device_
.log());
238 TEST_F(UsbMidiOutputStreamTest
, SendSongPositionPointerPending
) {
239 uint8 data1
[] = { 0xf2, 0x22, };
240 uint8 data2
[] = { 0x33, };
242 stream_
->Send(ToVector(data1
));
243 EXPECT_EQ("", device_
.log());
245 stream_
->Send(ToVector(data2
));
246 EXPECT_EQ("0x23 0xf2 0x22 0x33 (endpoint = 4)\n", device_
.log());
249 TEST_F(UsbMidiOutputStreamTest
, SendRealTimeMessages
) {
250 uint8 data
[] = { 0xf8, 0xfa, 0xfb, 0xfc, 0xfe, 0xff, };
252 stream_
->Send(ToVector(data
));
253 EXPECT_EQ("0x25 0xf8 0x00 0x00 "
254 "0x25 0xfa 0x00 0x00 "
255 "0x25 0xfb 0x00 0x00 "
256 "0x25 0xfc 0x00 0x00 "
257 "0x25 0xfe 0x00 0x00 "
258 "0x25 0xff 0x00 0x00 (endpoint = 4)\n", device_
.log());
261 TEST_F(UsbMidiOutputStreamTest
, SendRealTimeInSysExMessage
) {
263 0xf0, 0x00, 0x01, 0x02,
268 stream_
->Send(ToVector(data
));
269 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
270 "0x25 0xf8 0x00 0x00 "
271 "0x25 0xfa 0x00 0x00 "
272 "0x27 0x02 0x03 0xf7 (endpoint = 4)\n", device_
.log());