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 virtual ~MockUsbMidiDevice() {}
29 virtual std::vector
<uint8
> GetDescriptor() OVERRIDE
{
30 return std::vector
<uint8
>();
33 virtual void Send(int endpoint_number
, const std::vector
<uint8
>& data
)
35 for (size_t i
= 0; i
< data
.size(); ++i
) {
36 log_
+= base::StringPrintf("0x%02x ", data
[i
]);
38 log_
+= base::StringPrintf("(endpoint = %d)\n", endpoint_number
);
41 const std::string
& log() const { return log_
; }
43 void ClearLog() { log_
= ""; }
48 DISALLOW_COPY_AND_ASSIGN(MockUsbMidiDevice
);
51 class UsbMidiOutputStreamTest
: public ::testing::Test
{
53 UsbMidiOutputStreamTest() {
54 UsbMidiJack
jack(&device_
, 1, 2, 4);
55 stream_
.reset(new UsbMidiOutputStream(jack
));
58 MockUsbMidiDevice device_
;
59 scoped_ptr
<UsbMidiOutputStream
> stream_
;
62 DISALLOW_COPY_AND_ASSIGN(UsbMidiOutputStreamTest
);
65 TEST_F(UsbMidiOutputStreamTest
, SendEmpty
) {
66 stream_
->Send(std::vector
<uint8
>());
68 EXPECT_EQ("", device_
.log());
71 TEST_F(UsbMidiOutputStreamTest
, SendNoteOn
) {
72 uint8 data
[] = { 0x90, 0x45, 0x7f};
74 stream_
->Send(ToVector(data
));
75 EXPECT_EQ("0x29 0x90 0x45 0x7f (endpoint = 4)\n", device_
.log());
78 TEST_F(UsbMidiOutputStreamTest
, SendNoteOnPending
) {
79 stream_
->Send(std::vector
<uint8
>(1, 0x90));
80 stream_
->Send(std::vector
<uint8
>(1, 0x45));
81 EXPECT_EQ("", device_
.log());
83 stream_
->Send(std::vector
<uint8
>(1, 0x7f));
84 EXPECT_EQ("0x29 0x90 0x45 0x7f (endpoint = 4)\n", device_
.log());
87 stream_
->Send(std::vector
<uint8
>(1, 0x90));
88 stream_
->Send(std::vector
<uint8
>(1, 0x45));
89 EXPECT_EQ("", device_
.log());
92 TEST_F(UsbMidiOutputStreamTest
, SendNoteOnBurst
) {
93 uint8 data1
[] = { 0x90, };
94 uint8 data2
[] = { 0x45, 0x7f, 0x90, 0x45, 0x71, 0x90, 0x45, 0x72, 0x90, };
96 stream_
->Send(ToVector(data1
));
97 stream_
->Send(ToVector(data2
));
98 EXPECT_EQ("0x29 0x90 0x45 0x7f "
99 "0x29 0x90 0x45 0x71 "
100 "0x29 0x90 0x45 0x72 (endpoint = 4)\n", device_
.log());
103 TEST_F(UsbMidiOutputStreamTest
, SendNoteOff
) {
104 uint8 data
[] = { 0x80, 0x33, 0x44, };
106 stream_
->Send(ToVector(data
));
107 EXPECT_EQ("0x28 0x80 0x33 0x44 (endpoint = 4)\n", device_
.log());
110 TEST_F(UsbMidiOutputStreamTest
, SendPolyphonicKeyPress
) {
111 uint8 data
[] = { 0xa0, 0x33, 0x44, };
113 stream_
->Send(ToVector(data
));
114 EXPECT_EQ("0x2a 0xa0 0x33 0x44 (endpoint = 4)\n", device_
.log());
117 TEST_F(UsbMidiOutputStreamTest
, SendControlChange
) {
118 uint8 data
[] = { 0xb7, 0x33, 0x44, };
120 stream_
->Send(ToVector(data
));
121 EXPECT_EQ("0x2b 0xb7 0x33 0x44 (endpoint = 4)\n", device_
.log());
124 TEST_F(UsbMidiOutputStreamTest
, SendProgramChange
) {
125 uint8 data
[] = { 0xc2, 0x33, };
127 stream_
->Send(ToVector(data
));
128 EXPECT_EQ("0x2c 0xc2 0x33 0x00 (endpoint = 4)\n", device_
.log());
131 TEST_F(UsbMidiOutputStreamTest
, SendChannelPressure
) {
132 uint8 data
[] = { 0xd1, 0x33, 0x44, };
134 stream_
->Send(ToVector(data
));
135 EXPECT_EQ("0x2d 0xd1 0x33 0x44 (endpoint = 4)\n", device_
.log());
138 TEST_F(UsbMidiOutputStreamTest
, SendPitchWheelChange
) {
139 uint8 data
[] = { 0xe4, 0x33, 0x44, };
141 stream_
->Send(ToVector(data
));
142 EXPECT_EQ("0x2e 0xe4 0x33 0x44 (endpoint = 4)\n", device_
.log());
145 TEST_F(UsbMidiOutputStreamTest
, SendTwoByteSysEx
) {
146 uint8 data
[] = { 0xf0, 0xf7, };
148 stream_
->Send(ToVector(data
));
149 EXPECT_EQ("0x26 0xf0 0xf7 0x00 (endpoint = 4)\n", device_
.log());
152 TEST_F(UsbMidiOutputStreamTest
, SendThreeByteSysEx
) {
153 uint8 data
[] = { 0xf0, 0x4f, 0xf7, };
155 stream_
->Send(ToVector(data
));
156 EXPECT_EQ("0x27 0xf0 0x4f 0xf7 (endpoint = 4)\n", device_
.log());
159 TEST_F(UsbMidiOutputStreamTest
, SendFourByteSysEx
) {
160 uint8 data
[] = { 0xf0, 0x00, 0x01, 0xf7, };
162 stream_
->Send(ToVector(data
));
163 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
164 "0x25 0xf7 0x00 0x00 (endpoint = 4)\n", device_
.log());
167 TEST_F(UsbMidiOutputStreamTest
, SendFiveByteSysEx
) {
168 uint8 data
[] = { 0xf0, 0x00, 0x01, 0x02, 0xf7, };
170 stream_
->Send(ToVector(data
));
171 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
172 "0x26 0x02 0xf7 0x00 (endpoint = 4)\n", device_
.log());
175 TEST_F(UsbMidiOutputStreamTest
, SendSixByteSysEx
) {
176 uint8 data
[] = { 0xf0, 0x00, 0x01, 0x02, 0x03, 0xf7, };
178 stream_
->Send(ToVector(data
));
179 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
180 "0x27 0x02 0x03 0xf7 (endpoint = 4)\n", device_
.log());
183 TEST_F(UsbMidiOutputStreamTest
, SendPendingSysEx
) {
184 uint8 data1
[] = { 0xf0, 0x33, };
185 uint8 data2
[] = { 0x44, 0x55, 0x66, };
186 uint8 data3
[] = { 0x77, 0x88, 0x99, 0xf7, };
188 stream_
->Send(ToVector(data1
));
189 EXPECT_EQ("", device_
.log());
191 stream_
->Send(ToVector(data2
));
192 EXPECT_EQ("0x24 0xf0 0x33 0x44 (endpoint = 4)\n", device_
.log());
195 stream_
->Send(ToVector(data3
));
196 EXPECT_EQ("0x24 0x55 0x66 0x77 0x27 0x88 0x99 0xf7 (endpoint = 4)\n",
200 TEST_F(UsbMidiOutputStreamTest
, SendNoteOnAfterSysEx
) {
201 uint8 data
[] = { 0xf0, 0x00, 0x01, 0x02, 0x03, 0xf7, 0x90, 0x44, 0x33, };
203 stream_
->Send(ToVector(data
));
204 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
205 "0x27 0x02 0x03 0xf7 "
206 "0x29 0x90 0x44 0x33 (endpoint = 4)\n", device_
.log());
209 TEST_F(UsbMidiOutputStreamTest
, SendTimeCodeQuarterFrame
) {
210 uint8 data
[] = { 0xf1, 0x22, };
212 stream_
->Send(ToVector(data
));
213 EXPECT_EQ("0x22 0xf1 0x22 0x00 (endpoint = 4)\n", device_
.log());
216 TEST_F(UsbMidiOutputStreamTest
, SendSongPositionPointer
) {
217 uint8 data
[] = { 0xf2, 0x22, 0x33, };
219 stream_
->Send(ToVector(data
));
220 EXPECT_EQ("0x23 0xf2 0x22 0x33 (endpoint = 4)\n", device_
.log());
223 TEST_F(UsbMidiOutputStreamTest
, SendSongSelect
) {
224 uint8 data
[] = { 0xf3, 0x22, };
226 stream_
->Send(ToVector(data
));
227 EXPECT_EQ("0x22 0xf3 0x22 0x00 (endpoint = 4)\n", device_
.log());
230 TEST_F(UsbMidiOutputStreamTest
, TuneRequest
) {
231 uint8 data
[] = { 0xf6, };
233 stream_
->Send(ToVector(data
));
234 EXPECT_EQ("0x25 0xf6 0x00 0x00 (endpoint = 4)\n", device_
.log());
237 TEST_F(UsbMidiOutputStreamTest
, SendSongPositionPointerPending
) {
238 uint8 data1
[] = { 0xf2, 0x22, };
239 uint8 data2
[] = { 0x33, };
241 stream_
->Send(ToVector(data1
));
242 EXPECT_EQ("", device_
.log());
244 stream_
->Send(ToVector(data2
));
245 EXPECT_EQ("0x23 0xf2 0x22 0x33 (endpoint = 4)\n", device_
.log());
248 TEST_F(UsbMidiOutputStreamTest
, SendRealTimeMessages
) {
249 uint8 data
[] = { 0xf8, 0xfa, 0xfb, 0xfc, 0xfe, 0xff, };
251 stream_
->Send(ToVector(data
));
252 EXPECT_EQ("0x25 0xf8 0x00 0x00 "
253 "0x25 0xfa 0x00 0x00 "
254 "0x25 0xfb 0x00 0x00 "
255 "0x25 0xfc 0x00 0x00 "
256 "0x25 0xfe 0x00 0x00 "
257 "0x25 0xff 0x00 0x00 (endpoint = 4)\n", device_
.log());
260 TEST_F(UsbMidiOutputStreamTest
, SendRealTimeInSysExMessage
) {
262 0xf0, 0x00, 0x01, 0x02,
267 stream_
->Send(ToVector(data
));
268 EXPECT_EQ("0x24 0xf0 0x00 0x01 "
269 "0x25 0xf8 0x00 0x00 "
270 "0x25 0xfa 0x00 0x00 "
271 "0x27 0x02 0x03 0xf7 (endpoint = 4)\n", device_
.log());