• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "media/midi/usb_midi_output_stream.h"
6 
7 #include <string>
8 #include <vector>
9 
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"
14 
15 namespace media {
16 
17 namespace {
18 
19 template<typename T, size_t N>
20 std::vector<T> ToVector(const T((&array)[N])) {
21   return std::vector<T>(array, array + N);
22 }
23 
24 class MockUsbMidiDevice : public UsbMidiDevice {
25  public:
MockUsbMidiDevice()26   MockUsbMidiDevice() {}
~MockUsbMidiDevice()27   virtual ~MockUsbMidiDevice() {}
28 
GetDescriptor()29   virtual std::vector<uint8> GetDescriptor() OVERRIDE {
30     return std::vector<uint8>();
31   }
32 
Send(int endpoint_number,const std::vector<uint8> & data)33   virtual void Send(int endpoint_number, const std::vector<uint8>& data)
34       OVERRIDE {
35     for (size_t i = 0; i < data.size(); ++i) {
36       log_ += base::StringPrintf("0x%02x ", data[i]);
37     }
38     log_ += base::StringPrintf("(endpoint = %d)\n", endpoint_number);
39   }
40 
log() const41   const std::string& log() const { return log_; }
42 
ClearLog()43   void ClearLog() { log_ = ""; }
44 
45  private:
46   std::string log_;
47 
48   DISALLOW_COPY_AND_ASSIGN(MockUsbMidiDevice);
49 };
50 
51 class UsbMidiOutputStreamTest : public ::testing::Test {
52  protected:
UsbMidiOutputStreamTest()53   UsbMidiOutputStreamTest() {
54     UsbMidiJack jack(&device_, 1, 2, 4);
55     stream_.reset(new UsbMidiOutputStream(jack));
56   }
57 
58   MockUsbMidiDevice device_;
59   scoped_ptr<UsbMidiOutputStream> stream_;
60 
61  private:
62   DISALLOW_COPY_AND_ASSIGN(UsbMidiOutputStreamTest);
63 };
64 
TEST_F(UsbMidiOutputStreamTest,SendEmpty)65 TEST_F(UsbMidiOutputStreamTest, SendEmpty) {
66   stream_->Send(std::vector<uint8>());
67 
68   EXPECT_EQ("", device_.log());
69 }
70 
TEST_F(UsbMidiOutputStreamTest,SendNoteOn)71 TEST_F(UsbMidiOutputStreamTest, SendNoteOn) {
72   uint8 data[] = { 0x90, 0x45, 0x7f};
73 
74   stream_->Send(ToVector(data));
75   EXPECT_EQ("0x29 0x90 0x45 0x7f (endpoint = 4)\n", device_.log());
76 }
77 
TEST_F(UsbMidiOutputStreamTest,SendNoteOnPending)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());
82 
83   stream_->Send(std::vector<uint8>(1, 0x7f));
84   EXPECT_EQ("0x29 0x90 0x45 0x7f (endpoint = 4)\n", device_.log());
85   device_.ClearLog();
86 
87   stream_->Send(std::vector<uint8>(1, 0x90));
88   stream_->Send(std::vector<uint8>(1, 0x45));
89   EXPECT_EQ("", device_.log());
90 }
91 
TEST_F(UsbMidiOutputStreamTest,SendNoteOnBurst)92 TEST_F(UsbMidiOutputStreamTest, SendNoteOnBurst) {
93   uint8 data1[] = { 0x90, };
94   uint8 data2[] = { 0x45, 0x7f, 0x90, 0x45, 0x71, 0x90, 0x45, 0x72, 0x90, };
95 
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());
101 }
102 
TEST_F(UsbMidiOutputStreamTest,SendNoteOff)103 TEST_F(UsbMidiOutputStreamTest, SendNoteOff) {
104   uint8 data[] = { 0x80, 0x33, 0x44, };
105 
106   stream_->Send(ToVector(data));
107   EXPECT_EQ("0x28 0x80 0x33 0x44 (endpoint = 4)\n", device_.log());
108 }
109 
TEST_F(UsbMidiOutputStreamTest,SendPolyphonicKeyPress)110 TEST_F(UsbMidiOutputStreamTest, SendPolyphonicKeyPress) {
111   uint8 data[] = { 0xa0, 0x33, 0x44, };
112 
113   stream_->Send(ToVector(data));
114   EXPECT_EQ("0x2a 0xa0 0x33 0x44 (endpoint = 4)\n", device_.log());
115 }
116 
TEST_F(UsbMidiOutputStreamTest,SendControlChange)117 TEST_F(UsbMidiOutputStreamTest, SendControlChange) {
118   uint8 data[] = { 0xb7, 0x33, 0x44, };
119 
120   stream_->Send(ToVector(data));
121   EXPECT_EQ("0x2b 0xb7 0x33 0x44 (endpoint = 4)\n", device_.log());
122 }
123 
TEST_F(UsbMidiOutputStreamTest,SendProgramChange)124 TEST_F(UsbMidiOutputStreamTest, SendProgramChange) {
125   uint8 data[] = { 0xc2, 0x33, };
126 
127   stream_->Send(ToVector(data));
128   EXPECT_EQ("0x2c 0xc2 0x33 0x00 (endpoint = 4)\n", device_.log());
129 }
130 
TEST_F(UsbMidiOutputStreamTest,SendChannelPressure)131 TEST_F(UsbMidiOutputStreamTest, SendChannelPressure) {
132   uint8 data[] = { 0xd1, 0x33, 0x44, };
133 
134   stream_->Send(ToVector(data));
135   EXPECT_EQ("0x2d 0xd1 0x33 0x44 (endpoint = 4)\n", device_.log());
136 }
137 
TEST_F(UsbMidiOutputStreamTest,SendPitchWheelChange)138 TEST_F(UsbMidiOutputStreamTest, SendPitchWheelChange) {
139   uint8 data[] = { 0xe4, 0x33, 0x44, };
140 
141   stream_->Send(ToVector(data));
142   EXPECT_EQ("0x2e 0xe4 0x33 0x44 (endpoint = 4)\n", device_.log());
143 }
144 
TEST_F(UsbMidiOutputStreamTest,SendTwoByteSysEx)145 TEST_F(UsbMidiOutputStreamTest, SendTwoByteSysEx) {
146   uint8 data[] = { 0xf0, 0xf7, };
147 
148   stream_->Send(ToVector(data));
149   EXPECT_EQ("0x26 0xf0 0xf7 0x00 (endpoint = 4)\n", device_.log());
150 }
151 
TEST_F(UsbMidiOutputStreamTest,SendThreeByteSysEx)152 TEST_F(UsbMidiOutputStreamTest, SendThreeByteSysEx) {
153   uint8 data[] = { 0xf0, 0x4f, 0xf7, };
154 
155   stream_->Send(ToVector(data));
156   EXPECT_EQ("0x27 0xf0 0x4f 0xf7 (endpoint = 4)\n", device_.log());
157 }
158 
TEST_F(UsbMidiOutputStreamTest,SendFourByteSysEx)159 TEST_F(UsbMidiOutputStreamTest, SendFourByteSysEx) {
160   uint8 data[] = { 0xf0, 0x00, 0x01, 0xf7, };
161 
162   stream_->Send(ToVector(data));
163   EXPECT_EQ("0x24 0xf0 0x00 0x01 "
164             "0x25 0xf7 0x00 0x00 (endpoint = 4)\n", device_.log());
165 }
166 
TEST_F(UsbMidiOutputStreamTest,SendFiveByteSysEx)167 TEST_F(UsbMidiOutputStreamTest, SendFiveByteSysEx) {
168   uint8 data[] = { 0xf0, 0x00, 0x01, 0x02, 0xf7, };
169 
170   stream_->Send(ToVector(data));
171   EXPECT_EQ("0x24 0xf0 0x00 0x01 "
172             "0x26 0x02 0xf7 0x00 (endpoint = 4)\n", device_.log());
173 }
174 
TEST_F(UsbMidiOutputStreamTest,SendSixByteSysEx)175 TEST_F(UsbMidiOutputStreamTest, SendSixByteSysEx) {
176   uint8 data[] = { 0xf0, 0x00, 0x01, 0x02, 0x03, 0xf7, };
177 
178   stream_->Send(ToVector(data));
179   EXPECT_EQ("0x24 0xf0 0x00 0x01 "
180             "0x27 0x02 0x03 0xf7 (endpoint = 4)\n", device_.log());
181 }
182 
TEST_F(UsbMidiOutputStreamTest,SendPendingSysEx)183 TEST_F(UsbMidiOutputStreamTest, SendPendingSysEx) {
184   uint8 data1[] = { 0xf0, 0x33, };
185   uint8 data2[] = { 0x44, 0x55, 0x66, };
186   uint8 data3[] = { 0x77, 0x88, 0x99, 0xf7, };
187 
188   stream_->Send(ToVector(data1));
189   EXPECT_EQ("", device_.log());
190 
191   stream_->Send(ToVector(data2));
192   EXPECT_EQ("0x24 0xf0 0x33 0x44 (endpoint = 4)\n", device_.log());
193   device_.ClearLog();
194 
195   stream_->Send(ToVector(data3));
196   EXPECT_EQ("0x24 0x55 0x66 0x77 0x27 0x88 0x99 0xf7 (endpoint = 4)\n",
197             device_.log());
198 }
199 
TEST_F(UsbMidiOutputStreamTest,SendNoteOnAfterSysEx)200 TEST_F(UsbMidiOutputStreamTest, SendNoteOnAfterSysEx) {
201   uint8 data[] = { 0xf0, 0x00, 0x01, 0x02, 0x03, 0xf7, 0x90, 0x44, 0x33, };
202 
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());
207 }
208 
TEST_F(UsbMidiOutputStreamTest,SendTimeCodeQuarterFrame)209 TEST_F(UsbMidiOutputStreamTest, SendTimeCodeQuarterFrame) {
210   uint8 data[] = { 0xf1, 0x22, };
211 
212   stream_->Send(ToVector(data));
213   EXPECT_EQ("0x22 0xf1 0x22 0x00 (endpoint = 4)\n", device_.log());
214 }
215 
TEST_F(UsbMidiOutputStreamTest,SendSongPositionPointer)216 TEST_F(UsbMidiOutputStreamTest, SendSongPositionPointer) {
217   uint8 data[] = { 0xf2, 0x22, 0x33, };
218 
219   stream_->Send(ToVector(data));
220   EXPECT_EQ("0x23 0xf2 0x22 0x33 (endpoint = 4)\n", device_.log());
221 }
222 
TEST_F(UsbMidiOutputStreamTest,SendSongSelect)223 TEST_F(UsbMidiOutputStreamTest, SendSongSelect) {
224   uint8 data[] = { 0xf3, 0x22, };
225 
226   stream_->Send(ToVector(data));
227   EXPECT_EQ("0x22 0xf3 0x22 0x00 (endpoint = 4)\n", device_.log());
228 }
229 
TEST_F(UsbMidiOutputStreamTest,TuneRequest)230 TEST_F(UsbMidiOutputStreamTest, TuneRequest) {
231   uint8 data[] = { 0xf6, };
232 
233   stream_->Send(ToVector(data));
234   EXPECT_EQ("0x25 0xf6 0x00 0x00 (endpoint = 4)\n", device_.log());
235 }
236 
TEST_F(UsbMidiOutputStreamTest,SendSongPositionPointerPending)237 TEST_F(UsbMidiOutputStreamTest, SendSongPositionPointerPending) {
238   uint8 data1[] = { 0xf2, 0x22, };
239   uint8 data2[] = { 0x33, };
240 
241   stream_->Send(ToVector(data1));
242   EXPECT_EQ("", device_.log());
243 
244   stream_->Send(ToVector(data2));
245   EXPECT_EQ("0x23 0xf2 0x22 0x33 (endpoint = 4)\n", device_.log());
246 }
247 
TEST_F(UsbMidiOutputStreamTest,SendRealTimeMessages)248 TEST_F(UsbMidiOutputStreamTest, SendRealTimeMessages) {
249   uint8 data[] = { 0xf8, 0xfa, 0xfb, 0xfc, 0xfe, 0xff, };
250 
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());
258 }
259 
TEST_F(UsbMidiOutputStreamTest,SendRealTimeInSysExMessage)260 TEST_F(UsbMidiOutputStreamTest, SendRealTimeInSysExMessage) {
261   uint8 data[] = {
262     0xf0, 0x00, 0x01, 0x02,
263     0xf8, 0xfa,
264     0x03, 0xf7,
265   };
266 
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());
272 }
273 
274 }  // namespace
275 
276 }  // namespace media
277