summaryrefslogtreecommitdiffstats
path: root/media/midi/usb_midi_output_stream_unittest.cc
blob: b0e2d5be55fd235febfcba32179aef169c2474af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/midi/usb_midi_output_stream.h"

#include <string>
#include <vector>

#include "base/memory/scoped_ptr.h"
#include "base/strings/stringprintf.h"
#include "media/midi/usb_midi_device.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {
namespace midi {

namespace {

template<typename T, size_t N>
std::vector<T> ToVector(const T((&array)[N])) {
  return std::vector<T>(array, array + N);
}

class MockUsbMidiDevice : public UsbMidiDevice {
 public:
  MockUsbMidiDevice() {}
  ~MockUsbMidiDevice() override {}

  std::vector<uint8> GetDescriptors() override { return std::vector<uint8>(); }
  std::string GetManufacturer() override { return std::string(); }
  std::string GetProductName() override { return std::string(); }
  std::string GetDeviceVersion() override { return std::string(); }

  void Send(int endpoint_number, const std::vector<uint8>& data) override {
    for (size_t i = 0; i < data.size(); ++i) {
      log_ += base::StringPrintf("0x%02x ", data[i]);
    }
    log_ += base::StringPrintf("(endpoint = %d)\n", endpoint_number);
  }

  const std::string& log() const { return log_; }

  void ClearLog() { log_ = ""; }

 private:
  std::string log_;

  DISALLOW_COPY_AND_ASSIGN(MockUsbMidiDevice);
};

class UsbMidiOutputStreamTest : public ::testing::Test {
 protected:
  UsbMidiOutputStreamTest() {
    UsbMidiJack jack(&device_, 1, 2, 4);
    stream_.reset(new UsbMidiOutputStream(jack));
  }

  MockUsbMidiDevice device_;
  scoped_ptr<UsbMidiOutputStream> stream_;

 private:
  DISALLOW_COPY_AND_ASSIGN(UsbMidiOutputStreamTest);
};

TEST_F(UsbMidiOutputStreamTest, SendEmpty) {
  stream_->Send(std::vector<uint8>());

  EXPECT_EQ("", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendNoteOn) {
  uint8 data[] = { 0x90, 0x45, 0x7f};

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x29 0x90 0x45 0x7f (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendNoteOnPending) {
  stream_->Send(std::vector<uint8>(1, 0x90));
  stream_->Send(std::vector<uint8>(1, 0x45));
  EXPECT_EQ("", device_.log());

  stream_->Send(std::vector<uint8>(1, 0x7f));
  EXPECT_EQ("0x29 0x90 0x45 0x7f (endpoint = 4)\n", device_.log());
  device_.ClearLog();

  stream_->Send(std::vector<uint8>(1, 0x90));
  stream_->Send(std::vector<uint8>(1, 0x45));
  EXPECT_EQ("", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendNoteOnBurst) {
  uint8 data1[] = { 0x90, };
  uint8 data2[] = { 0x45, 0x7f, 0x90, 0x45, 0x71, 0x90, 0x45, 0x72, 0x90, };

  stream_->Send(ToVector(data1));
  stream_->Send(ToVector(data2));
  EXPECT_EQ("0x29 0x90 0x45 0x7f "
            "0x29 0x90 0x45 0x71 "
            "0x29 0x90 0x45 0x72 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendNoteOff) {
  uint8 data[] = { 0x80, 0x33, 0x44, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x28 0x80 0x33 0x44 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendPolyphonicKeyPress) {
  uint8 data[] = { 0xa0, 0x33, 0x44, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x2a 0xa0 0x33 0x44 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendControlChange) {
  uint8 data[] = { 0xb7, 0x33, 0x44, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x2b 0xb7 0x33 0x44 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendProgramChange) {
  uint8 data[] = { 0xc2, 0x33, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x2c 0xc2 0x33 0x00 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendChannelPressure) {
  uint8 data[] = { 0xd1, 0x33, 0x44, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x2d 0xd1 0x33 0x44 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendPitchWheelChange) {
  uint8 data[] = { 0xe4, 0x33, 0x44, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x2e 0xe4 0x33 0x44 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendTwoByteSysEx) {
  uint8 data[] = { 0xf0, 0xf7, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x26 0xf0 0xf7 0x00 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendThreeByteSysEx) {
  uint8 data[] = { 0xf0, 0x4f, 0xf7, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x27 0xf0 0x4f 0xf7 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendFourByteSysEx) {
  uint8 data[] = { 0xf0, 0x00, 0x01, 0xf7, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x24 0xf0 0x00 0x01 "
            "0x25 0xf7 0x00 0x00 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendFiveByteSysEx) {
  uint8 data[] = { 0xf0, 0x00, 0x01, 0x02, 0xf7, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x24 0xf0 0x00 0x01 "
            "0x26 0x02 0xf7 0x00 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendSixByteSysEx) {
  uint8 data[] = { 0xf0, 0x00, 0x01, 0x02, 0x03, 0xf7, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x24 0xf0 0x00 0x01 "
            "0x27 0x02 0x03 0xf7 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendPendingSysEx) {
  uint8 data1[] = { 0xf0, 0x33, };
  uint8 data2[] = { 0x44, 0x55, 0x66, };
  uint8 data3[] = { 0x77, 0x88, 0x99, 0xf7, };

  stream_->Send(ToVector(data1));
  EXPECT_EQ("", device_.log());

  stream_->Send(ToVector(data2));
  EXPECT_EQ("0x24 0xf0 0x33 0x44 (endpoint = 4)\n", device_.log());
  device_.ClearLog();

  stream_->Send(ToVector(data3));
  EXPECT_EQ("0x24 0x55 0x66 0x77 0x27 0x88 0x99 0xf7 (endpoint = 4)\n",
            device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendNoteOnAfterSysEx) {
  uint8 data[] = { 0xf0, 0x00, 0x01, 0x02, 0x03, 0xf7, 0x90, 0x44, 0x33, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x24 0xf0 0x00 0x01 "
            "0x27 0x02 0x03 0xf7 "
            "0x29 0x90 0x44 0x33 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendTimeCodeQuarterFrame) {
  uint8 data[] = { 0xf1, 0x22, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x22 0xf1 0x22 0x00 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendSongPositionPointer) {
  uint8 data[] = { 0xf2, 0x22, 0x33, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x23 0xf2 0x22 0x33 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendSongSelect) {
  uint8 data[] = { 0xf3, 0x22, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x22 0xf3 0x22 0x00 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, TuneRequest) {
  uint8 data[] = { 0xf6, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x25 0xf6 0x00 0x00 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendSongPositionPointerPending) {
  uint8 data1[] = { 0xf2, 0x22, };
  uint8 data2[] = { 0x33, };

  stream_->Send(ToVector(data1));
  EXPECT_EQ("", device_.log());

  stream_->Send(ToVector(data2));
  EXPECT_EQ("0x23 0xf2 0x22 0x33 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendRealTimeMessages) {
  uint8 data[] = { 0xf8, 0xfa, 0xfb, 0xfc, 0xfe, 0xff, };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x25 0xf8 0x00 0x00 "
            "0x25 0xfa 0x00 0x00 "
            "0x25 0xfb 0x00 0x00 "
            "0x25 0xfc 0x00 0x00 "
            "0x25 0xfe 0x00 0x00 "
            "0x25 0xff 0x00 0x00 (endpoint = 4)\n", device_.log());
}

TEST_F(UsbMidiOutputStreamTest, SendRealTimeInSysExMessage) {
  uint8 data[] = {
    0xf0, 0x00, 0x01, 0x02,
    0xf8, 0xfa,
    0x03, 0xf7,
  };

  stream_->Send(ToVector(data));
  EXPECT_EQ("0x24 0xf0 0x00 0x01 "
            "0x25 0xf8 0x00 0x00 "
            "0x25 0xfa 0x00 0x00 "
            "0x27 0x02 0x03 0xf7 (endpoint = 4)\n", device_.log());
}

}  // namespace

}  // namespace midi
}  // namespace media