OLD | NEW |
---|---|
(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 | |
Takashi Toyoshima
2013/12/24 04:19:32
Can you elaborate why we need to use NOLINT annota
yhirano
2013/12/24 05:57:01
Without this, cpplint complains like this:
All par
Takashi Toyoshima
2013/12/24 06:47:18
So maybe you can move this typedef inside UsbMidiI
| |
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(uint8 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_; | |
Takashi Toyoshima
2013/12/24 04:19:32
DISALLOW_... ?
yhirano
2013/12/24 05:57:01
Done.
| |
97 }; | |
98 | |
99 TEST_F(UsbMidiInputStreamTest, UnknownMessage) { | |
100 uint8 data[] = { | |
101 0x40, 0xff, 0xff, 0xff, | |
102 0x41, 0xff, 0xff, 0xff, | |
103 }; | |
104 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0); | |
105 } | |
106 | |
107 TEST_F(UsbMidiInputStreamTest, SystemCommonMessage) { | |
108 uint8 data[] = { | |
109 0x45, 0xf8, 0x00, 0x00, | |
110 0x42, 0xf3, 0x22, 0x00, | |
111 0x43, 0xf2, 0x33, 0x44, | |
112 }; | |
113 uint8 expected1[] = { 0xf8, }; | |
114 uint8 expected2[] = { 0xf3, 0x22, }; | |
115 uint8 expected3[] = { 0xf2, 0x33, 0x44, }; | |
116 | |
117 { | |
118 InSequence s; | |
119 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0)); | |
120 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0)); | |
121 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0)); | |
122 } | |
123 | |
124 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0); | |
125 } | |
126 | |
127 TEST_F(UsbMidiInputStreamTest, SystemExclusiveMessage) { | |
128 uint8 data[] = { | |
129 0x44, 0xf0, 0x11, 0x22, | |
130 0x45, 0xf7, 0x00, 0x00, | |
131 0x46, 0xf0, 0xf7, 0x00, | |
132 0x47, 0xf0, 0x33, 0xf7, | |
133 }; | |
134 uint8 expected1[] = { 0xf0, 0x11, 0x22, }; | |
135 uint8 expected2[] = { 0xf7, }; | |
136 uint8 expected3[] = { 0xf0, 0xf7, }; | |
137 uint8 expected4[] = { 0xf0, 0x33, 0xf7, }; | |
138 | |
139 { | |
140 InSequence s; | |
141 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0)); | |
142 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0)); | |
143 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0)); | |
144 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected4), 0)); | |
145 } | |
146 | |
147 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0); | |
148 } | |
149 | |
150 TEST_F(UsbMidiInputStreamTest, ChannelMessage) { | |
151 uint8 data[] = { | |
152 0x48, 0x80, 0x11, 0x22, | |
153 0x49, 0x90, 0x33, 0x44, | |
154 0x4a, 0xa0, 0x55, 0x66, | |
155 0x4b, 0xb0, 0x77, 0x88, | |
156 0x4c, 0xc0, 0x99, 0x00, | |
157 0x4d, 0xd0, 0xaa, 0x00, | |
158 0x4e, 0xe0, 0xbb, 0xcc, | |
159 }; | |
160 uint8 expected1[] = { 0x80, 0x11, 0x22, }; | |
161 uint8 expected2[] = { 0x90, 0x33, 0x44, }; | |
162 uint8 expected3[] = { 0xa0, 0x55, 0x66, }; | |
163 uint8 expected4[] = { 0xb0, 0x77, 0x88, }; | |
164 uint8 expected5[] = { 0xc0, 0x99, }; | |
165 uint8 expected6[] = { 0xd0, 0xaa, }; | |
166 uint8 expected7[] = { 0xe0, 0xbb, 0xcc, }; | |
167 | |
168 { | |
169 InSequence s; | |
170 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0)); | |
171 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0)); | |
172 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0)); | |
173 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected4), 0)); | |
174 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected5), 0)); | |
175 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected6), 0)); | |
176 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected7), 0)); | |
177 } | |
178 | |
179 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0); | |
180 } | |
181 | |
182 TEST_F(UsbMidiInputStreamTest, SingleByteMessage) { | |
183 uint8 data[] = { | |
184 0x4f, 0xf8, 0x00, 0x00, | |
185 }; | |
186 | |
187 uint8 expected[] = { 0xf8, }; | |
188 | |
189 { | |
190 InSequence s; | |
191 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected), 0)); | |
192 } | |
193 | |
194 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0); | |
195 } | |
196 | |
197 TEST_F(UsbMidiInputStreamTest, DispatchForMultipleCables) { | |
198 uint8 data[] = { | |
199 0x4f, 0xf8, 0x00, 0x00, | |
200 0x5f, 0xfa, 0x00, 0x00, | |
201 0x6f, 0xfb, 0x00, 0x00, | |
202 }; | |
203 uint8 expected1[] = { 0xf8, }; | |
204 uint8 expected2[] = { 0xfa, }; | |
205 | |
206 { | |
207 InSequence s; | |
208 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 99)); | |
209 EXPECT_CALL(*delegate_, OnReceivedData(3, ToVector(expected2), 99)); | |
210 } | |
211 | |
212 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 99); | |
213 } | |
214 | |
215 TEST_F(UsbMidiInputStreamTest, DispatchForDevice2) { | |
216 uint8 data[] = { 0x4f, 0xf8, 0x00, 0x00 }; | |
217 uint8 expected[] = { 0xf8, }; | |
218 | |
219 { | |
220 InSequence s; | |
221 EXPECT_CALL(*delegate_, OnReceivedData(2, ToVector(expected), 99)); | |
222 } | |
223 | |
224 stream_->OnReceivedData(device2_.get(), 7, data, arraysize(data), 99); | |
225 } | |
226 | |
227 TEST_F(UsbMidiInputStreamTest, PendingData) { | |
228 uint8 data1[] = { 0x4f, 0xf8, }; | |
229 uint8 data2[] = { | |
230 0x00, 0x00, | |
231 0x5f, 0xfa, 0x00, 0x00, | |
232 0x5f, 0xfb, 0x00, | |
233 }; | |
234 uint8 data3[] = { | |
235 0x00, | |
236 0x4f, 0xfa, | |
237 }; | |
238 uint8 expected1[] = { 0xf8, }; | |
239 uint8 expected2[] = { 0xfa, }; | |
240 uint8 expected3[] = { 0xfb, }; | |
241 Checkpoint checkpoint; | |
242 | |
243 { | |
244 InSequence s; | |
245 EXPECT_CALL(checkpoint, Call(0)); | |
246 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0)); | |
247 EXPECT_CALL(*delegate_, OnReceivedData(3, ToVector(expected2), 0)); | |
248 EXPECT_CALL(checkpoint, Call(1)); | |
249 EXPECT_CALL(*delegate_, OnReceivedData(3, ToVector(expected3), 0)); | |
250 } | |
251 | |
252 stream_->OnReceivedData(device1_.get(), 7, data1, arraysize(data1), 0); | |
253 checkpoint.Call(0); | |
254 stream_->OnReceivedData(device1_.get(), 7, data2, arraysize(data2), 0); | |
255 checkpoint.Call(1); | |
256 stream_->OnReceivedData(device1_.get(), 7, data3, arraysize(data3), 0); | |
257 } | |
258 | |
259 } // namespace | |
260 | |
261 } // namespace media | |
OLD | NEW |