Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: media/midi/usb_midi_input_stream_unittest.cc

Issue 107513012: [WebMIDI] Introduce UsbMidi{Input, Output}Stream. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@usb-midi-parser
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 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_input_stream.h"
6
7 #include <vector>
8
9 #include "media/midi/usb_midi_device.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace media {
14
15 namespace {
16
17 using ::testing::InSequence;
18 using ::testing::StrictMock;
19
20 typedef ::testing::MockFunction<void(int)> Checkpoint; // NOLINT
21
22 template<typename T, size_t N>
23 std::vector<T> ToVector(const T((&array)[N])) {
24 return std::vector<T>(array, array + N);
25 }
26
27 class TestUsbMidiDevice : public UsbMidiDevice {
28 public:
29 TestUsbMidiDevice() {}
30 virtual ~TestUsbMidiDevice() {}
31 virtual std::vector<uint8> GetDescriptor() OVERRIDE {
32 return std::vector<uint8>();
33 }
34 virtual void Send(int endpoint_number,
35 const std::vector<uint8>& data) OVERRIDE {}
36
37 private:
38 DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDevice);
39 };
40
41 class MockDelegate : public UsbMidiInputStream::Delegate {
42 public:
43 MockDelegate() {}
44 virtual ~MockDelegate() {}
45 virtual void OnReceivedData(size_t jack_index,
46 const uint8* data,
47 size_t size,
48 double timestamp) OVERRIDE {
49 // In order to use gmock parameter matching,
50 // we define and use OnReceiveData with std::vector<uint8>.
51 OnReceivedData(jack_index,
52 std::vector<uint8>(data, data + size),
53 timestamp);
54 }
55
56 MOCK_METHOD3(OnReceivedData, void(size_t,
57 const std::vector<uint8>&,
58 double));
59
60 private:
61 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
62 };
63
64 class UsbMidiInputStreamTest : public ::testing::Test {
65 protected:
66
67 UsbMidiInputStreamTest()
68 : device1_(new TestUsbMidiDevice),
69 device2_(new TestUsbMidiDevice),
70 delegate_(new StrictMock<MockDelegate>) {
71 std::vector<UsbMidiJack> jacks;
72
73 jacks.push_back(UsbMidiJack(device1_.get(),
74 84, // jack_id
75 4, // cable_number
76 135)); // endpoint_address
77 jacks.push_back(UsbMidiJack(device2_.get(),
78 85,
79 5,
80 137));
81 jacks.push_back(UsbMidiJack(device2_.get(),
82 84,
83 4,
84 135));
85 jacks.push_back(UsbMidiJack(device1_.get(),
86 85,
87 5,
88 135));
89
90 stream_.reset(new UsbMidiInputStream(jacks, delegate_.get()));
91 }
92
93 scoped_ptr<TestUsbMidiDevice> device1_;
94 scoped_ptr<TestUsbMidiDevice> device2_;
95 scoped_ptr<MockDelegate> delegate_;
96 scoped_ptr<UsbMidiInputStream> stream_;
97
98 private:
99 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStreamTest);
100 };
101
102 TEST_F(UsbMidiInputStreamTest, UnknownMessage) {
103 uint8 data[] = {
104 0x40, 0xff, 0xff, 0xff,
105 0x41, 0xff, 0xff, 0xff,
106 };
107 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
108 }
109
110 TEST_F(UsbMidiInputStreamTest, SystemCommonMessage) {
111 uint8 data[] = {
112 0x45, 0xf8, 0x00, 0x00,
113 0x42, 0xf3, 0x22, 0x00,
114 0x43, 0xf2, 0x33, 0x44,
115 };
116 uint8 expected1[] = { 0xf8, };
117 uint8 expected2[] = { 0xf3, 0x22, };
118 uint8 expected3[] = { 0xf2, 0x33, 0x44, };
119
120 {
121 InSequence s;
122 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0));
123 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0));
124 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0));
125 }
126
127 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
128 }
129
130 TEST_F(UsbMidiInputStreamTest, SystemExclusiveMessage) {
131 uint8 data[] = {
132 0x44, 0xf0, 0x11, 0x22,
133 0x45, 0xf7, 0x00, 0x00,
134 0x46, 0xf0, 0xf7, 0x00,
135 0x47, 0xf0, 0x33, 0xf7,
136 };
137 uint8 expected1[] = { 0xf0, 0x11, 0x22, };
138 uint8 expected2[] = { 0xf7, };
139 uint8 expected3[] = { 0xf0, 0xf7, };
140 uint8 expected4[] = { 0xf0, 0x33, 0xf7, };
141
142 {
143 InSequence s;
144 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0));
145 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0));
146 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0));
147 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected4), 0));
148 }
149
150 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
151 }
152
153 TEST_F(UsbMidiInputStreamTest, ChannelMessage) {
154 uint8 data[] = {
155 0x48, 0x80, 0x11, 0x22,
156 0x49, 0x90, 0x33, 0x44,
157 0x4a, 0xa0, 0x55, 0x66,
158 0x4b, 0xb0, 0x77, 0x88,
159 0x4c, 0xc0, 0x99, 0x00,
160 0x4d, 0xd0, 0xaa, 0x00,
161 0x4e, 0xe0, 0xbb, 0xcc,
162 };
163 uint8 expected1[] = { 0x80, 0x11, 0x22, };
164 uint8 expected2[] = { 0x90, 0x33, 0x44, };
165 uint8 expected3[] = { 0xa0, 0x55, 0x66, };
166 uint8 expected4[] = { 0xb0, 0x77, 0x88, };
167 uint8 expected5[] = { 0xc0, 0x99, };
168 uint8 expected6[] = { 0xd0, 0xaa, };
169 uint8 expected7[] = { 0xe0, 0xbb, 0xcc, };
170
171 {
172 InSequence s;
173 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0));
174 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0));
175 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0));
176 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected4), 0));
177 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected5), 0));
178 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected6), 0));
179 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected7), 0));
180 }
181
182 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
183 }
184
185 TEST_F(UsbMidiInputStreamTest, SingleByteMessage) {
186 uint8 data[] = {
187 0x4f, 0xf8, 0x00, 0x00,
188 };
189
190 uint8 expected[] = { 0xf8, };
191
192 {
193 InSequence s;
194 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected), 0));
195 }
196
197 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
198 }
199
200 TEST_F(UsbMidiInputStreamTest, DispatchForMultipleCables) {
201 uint8 data[] = {
202 0x4f, 0xf8, 0x00, 0x00,
203 0x5f, 0xfa, 0x00, 0x00,
204 0x6f, 0xfb, 0x00, 0x00,
205 };
206 uint8 expected1[] = { 0xf8, };
207 uint8 expected2[] = { 0xfa, };
208
209 {
210 InSequence s;
211 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 99));
212 EXPECT_CALL(*delegate_, OnReceivedData(3, ToVector(expected2), 99));
213 }
214
215 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 99);
216 }
217
218 TEST_F(UsbMidiInputStreamTest, DispatchForDevice2) {
219 uint8 data[] = { 0x4f, 0xf8, 0x00, 0x00 };
220 uint8 expected[] = { 0xf8, };
221
222 {
223 InSequence s;
224 EXPECT_CALL(*delegate_, OnReceivedData(2, ToVector(expected), 99));
225 }
226
227 stream_->OnReceivedData(device2_.get(), 7, data, arraysize(data), 99);
228 }
229
230 TEST_F(UsbMidiInputStreamTest, PendingData) {
231 uint8 data1[] = { 0x4f, 0xf8, };
232 uint8 data2[] = {
233 0x00, 0x00,
234 0x5f, 0xfa, 0x00, 0x00,
235 0x5f, 0xfb, 0x00,
236 };
237 uint8 data3[] = {
238 0x00,
239 0x4f, 0xfa,
240 };
241 uint8 expected1[] = { 0xf8, };
242 uint8 expected2[] = { 0xfa, };
243 uint8 expected3[] = { 0xfb, };
244 Checkpoint checkpoint;
245
246 {
247 InSequence s;
248 EXPECT_CALL(checkpoint, Call(0));
249 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0));
250 EXPECT_CALL(*delegate_, OnReceivedData(3, ToVector(expected2), 0));
251 EXPECT_CALL(checkpoint, Call(1));
252 EXPECT_CALL(*delegate_, OnReceivedData(3, ToVector(expected3), 0));
253 }
254
255 stream_->OnReceivedData(device1_.get(), 7, data1, arraysize(data1), 0);
256 checkpoint.Call(0);
257 stream_->OnReceivedData(device1_.get(), 7, data2, arraysize(data2), 0);
258 checkpoint.Call(1);
259 stream_->OnReceivedData(device1_.get(), 7, data3, arraysize(data3), 0);
260 }
261
262 } // namespace
263
264 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698