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"
19 template<typename T
, size_t N
>
20 std::vector
<T
> ToVector(const T((&array
)[N
])) {
21 return std::vector
<T
>(array
, array
+ N
);
24 class MockUsbMidiDevice
: public UsbMidiDevice
{
26 MockUsbMidiDevice() {}
27 ~MockUsbMidiDevice() override
{}
29 std::vector
<uint8
> GetDescriptor() override
{ return std::vector
<uint8
>(); }
31 void Send(int endpoint_number
, const std::vector
<uint8
>& data
) override
{
32 for (size_t i
= 0; i
< data
.size(); ++i
) {
33 log_
+= base::StringPrintf("0x%02x ", data
[i
]);
35 log_
+= base::StringPrintf("(endpoint = %d)\n", endpoint_number
);
38 const std::string
& log() const { return log_
; }
40 void ClearLog() { log_
= ""; }
45 DISALLOW_COPY_AND_ASSIGN(MockUsbMidiDevice
);
48 class UsbMidiOutputStreamTest
: public ::testing::Test
{
50 UsbMidiOutputStreamTest() {
51 UsbMidiJack
jack(&device_
, 1, 2, 4);
52 stream_
.reset(new UsbMidiOutputStream(jack
));
55 MockUsbMidiDevice device_
;
56 scoped_ptr
<UsbMidiOutputStream
> stream_
;
59 DISALLOW_COPY_AND_ASSIGN(UsbMidiOutputStreamTest
);
62 TEST_F(UsbMidiOutputStreamTest
, SendEmpty
) {
63 stream_
->Send(std::vector
<uint8
>());
65 EXPECT_EQ("", device_
.log());
68 TEST_F(UsbMidiOutputStreamTest
, SendNoteOn
) {
69 uint8 data
[] = { 0x90, 0x45, 0x7f};
71 stream_
->Send(ToVector(data
));
72 EXPECT_EQ("0x29 0x90 0x45 0x7f (endpoint = 4)\n", device_
.log());
75 TEST_F(UsbMidiOutputStreamTest
, SendNoteOnPending
) {
76 stream_
->Send(std::vector
<uint8
>(1, 0x90));
77 stream_
->Send(std::vector
<uint8
>(1, 0x45));
78 EXPECT_EQ("", device_
.log());
80 stream_
->Send(std::vector
<uint8
>(1, 0x7f));
81 EXPECT_EQ("0x29 0x90 0x45 0x7f (endpoint = 4)\n", device_
.log());
84 stream_
->Send(std::vector
<uint8
>(1, 0x90));
85 stream_
->Send(std::vector
<uint8
>(1, 0x45));
86 EXPECT_EQ("", device_
.log());
89 TEST_F(UsbMidiOutputStreamTest
, SendNoteOnBurst
) {
90 uint8 data1
[] = { 0x90, };
91 uint8 data2
[] = { 0x45, 0x7f, 0x90, 0x45, 0x71, 0x90, 0x45, 0x72, 0x90, };
93 stream_
->Send(ToVector(data1
));
94 stream_
->Send(ToVector(data2
));
95 EXPECT_EQ("0x29 0x90 0x45 0x7f "
96 "0x29 0x90 0x45 0x71 "
97 "0x29 0x90 0x45 0x72 (endpoint = 4)\n", device_
.log());
100 TEST_F(UsbMidiOutputStreamTest
, SendNoteOff
) {
101 uint8 data
[] = { 0x80, 0x33, 0x44, };
103 stream_
->Send(ToVector(data
));
104 EXPECT_EQ("0x28 0x80 0x33 0x44 (endpoint = 4)\n", device_
.log());
107 TEST_F(UsbMidiOutputStreamTest
, SendPolyphonicKeyPress
) {
108 uint8 data
[] = { 0xa0, 0x33, 0x44, };
110 stream_
->Send(ToVector(data
));
111 EXPECT_EQ("0x2a 0xa0 0x33 0x44 (endpoint = 4)\n", device_
.log());
114 TEST_F(UsbMidiOutputStreamTest
, SendControlChange
) {
115 uint8 data
[] = { 0xb7, 0x33, 0x44, };
117 stream_
->Send(ToVector(data
));
118 EXPECT_EQ("0x2b 0xb7 0x33 0x44 (endpoint = 4)\n", device_
.log());
121 TEST_F(UsbMidiOutputStreamTest
, SendProgramChange
) {
122 uint8 data
[] = { 0xc2, 0x33, };
124 stream_
->Send(ToVector(data
));
125 EXPECT_EQ("0x2c 0xc2 0x33 0x00 (endpoint = 4)\n", device_
.log());
128 TEST_F(UsbMidiOutputStreamTest
, SendChannelPressure
) {
129 uint8 data
[] = { 0xd1, 0x33, 0x44, };
131 stream_
->Send(ToVector(data
));
132 EXPECT_EQ("0x2d 0xd1 0x33 0x44 (endpoint = 4)\n", device_
.log());
135 TEST_F(UsbMidiOutputStreamTest
, SendPitchWheelChange
) {
136 uint8 data
[] = { 0xe4, 0x33, 0x44, };
138 stream_
->Send(ToVector(data
));
139 EXPECT_EQ("0x2e 0xe4 0x33 0x44 (endpoint = 4)\n", device_
.log());
142 TEST_F(UsbMidiOutputStreamTest
, SendTwoByteSysEx
) {
143 uint8 data
[] = { 0xf0, 0xf7, };
145 stream_
->Send(ToVector(data
));
146 EXPECT_EQ("0x26 0xf0 0xf7 0x00 (endpoint = 4)\n", device_
.log());
149 TEST_F(UsbMidiOutputStreamTest
, SendThreeByteSysEx
) {
150 uint8 data
[] = { 0xf0, 0x4f, 0xf7, };
152 stream_
->Send(ToVector(data
));
153 EXPECT_EQ("0x27 0xf0 0x4f 0xf7 (endpoint = 4)\n", device_
.log());
156 TEST_F(UsbMidiOutputStreamTest
, SendFourByteSysEx
) {
157 uint8 data
[] = { 0xf0, 0x00, 0x01, 0xf7, };
159 stream_
->Send(ToVector(data
));
160 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
161 "0x25 0xf7 0x00 0x00 (endpoint = 4)\n", device_
.log());
164 TEST_F(UsbMidiOutputStreamTest
, SendFiveByteSysEx
) {
165 uint8 data
[] = { 0xf0, 0x00, 0x01, 0x02, 0xf7, };
167 stream_
->Send(ToVector(data
));
168 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
169 "0x26 0x02 0xf7 0x00 (endpoint = 4)\n", device_
.log());
172 TEST_F(UsbMidiOutputStreamTest
, SendSixByteSysEx
) {
173 uint8 data
[] = { 0xf0, 0x00, 0x01, 0x02, 0x03, 0xf7, };
175 stream_
->Send(ToVector(data
));
176 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
177 "0x27 0x02 0x03 0xf7 (endpoint = 4)\n", device_
.log());
180 TEST_F(UsbMidiOutputStreamTest
, SendPendingSysEx
) {
181 uint8 data1
[] = { 0xf0, 0x33, };
182 uint8 data2
[] = { 0x44, 0x55, 0x66, };
183 uint8 data3
[] = { 0x77, 0x88, 0x99, 0xf7, };
185 stream_
->Send(ToVector(data1
));
186 EXPECT_EQ("", device_
.log());
188 stream_
->Send(ToVector(data2
));
189 EXPECT_EQ("0x24 0xf0 0x33 0x44 (endpoint = 4)\n", device_
.log());
192 stream_
->Send(ToVector(data3
));
193 EXPECT_EQ("0x24 0x55 0x66 0x77 0x27 0x88 0x99 0xf7 (endpoint = 4)\n",
197 TEST_F(UsbMidiOutputStreamTest
, SendNoteOnAfterSysEx
) {
198 uint8 data
[] = { 0xf0, 0x00, 0x01, 0x02, 0x03, 0xf7, 0x90, 0x44, 0x33, };
200 stream_
->Send(ToVector(data
));
201 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
202 "0x27 0x02 0x03 0xf7 "
203 "0x29 0x90 0x44 0x33 (endpoint = 4)\n", device_
.log());
206 TEST_F(UsbMidiOutputStreamTest
, SendTimeCodeQuarterFrame
) {
207 uint8 data
[] = { 0xf1, 0x22, };
209 stream_
->Send(ToVector(data
));
210 EXPECT_EQ("0x22 0xf1 0x22 0x00 (endpoint = 4)\n", device_
.log());
213 TEST_F(UsbMidiOutputStreamTest
, SendSongPositionPointer
) {
214 uint8 data
[] = { 0xf2, 0x22, 0x33, };
216 stream_
->Send(ToVector(data
));
217 EXPECT_EQ("0x23 0xf2 0x22 0x33 (endpoint = 4)\n", device_
.log());
220 TEST_F(UsbMidiOutputStreamTest
, SendSongSelect
) {
221 uint8 data
[] = { 0xf3, 0x22, };
223 stream_
->Send(ToVector(data
));
224 EXPECT_EQ("0x22 0xf3 0x22 0x00 (endpoint = 4)\n", device_
.log());
227 TEST_F(UsbMidiOutputStreamTest
, TuneRequest
) {
228 uint8 data
[] = { 0xf6, };
230 stream_
->Send(ToVector(data
));
231 EXPECT_EQ("0x25 0xf6 0x00 0x00 (endpoint = 4)\n", device_
.log());
234 TEST_F(UsbMidiOutputStreamTest
, SendSongPositionPointerPending
) {
235 uint8 data1
[] = { 0xf2, 0x22, };
236 uint8 data2
[] = { 0x33, };
238 stream_
->Send(ToVector(data1
));
239 EXPECT_EQ("", device_
.log());
241 stream_
->Send(ToVector(data2
));
242 EXPECT_EQ("0x23 0xf2 0x22 0x33 (endpoint = 4)\n", device_
.log());
245 TEST_F(UsbMidiOutputStreamTest
, SendRealTimeMessages
) {
246 uint8 data
[] = { 0xf8, 0xfa, 0xfb, 0xfc, 0xfe, 0xff, };
248 stream_
->Send(ToVector(data
));
249 EXPECT_EQ("0x25 0xf8 0x00 0x00 "
250 "0x25 0xfa 0x00 0x00 "
251 "0x25 0xfb 0x00 0x00 "
252 "0x25 0xfc 0x00 0x00 "
253 "0x25 0xfe 0x00 0x00 "
254 "0x25 0xff 0x00 0x00 (endpoint = 4)\n", device_
.log());
257 TEST_F(UsbMidiOutputStreamTest
, SendRealTimeInSysExMessage
) {
259 0xf0, 0x00, 0x01, 0x02,
264 stream_
->Send(ToVector(data
));
265 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
266 "0x25 0xf8 0x00 0x00 "
267 "0x25 0xfa 0x00 0x00 "
268 "0x27 0x02 0x03 0xf7 (endpoint = 4)\n", device_
.log());