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

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 6 years, 11 months 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 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 UsbMidiInputStreamTest()
67 : device1_(new TestUsbMidiDevice),
68 device2_(new TestUsbMidiDevice),
69 delegate_(new StrictMock<MockDelegate>) {
70 std::vector<UsbMidiJack> jacks;
71
72 jacks.push_back(UsbMidiJack(device1_.get(),
73 84, // jack_id
74 4, // cable_number
75 135)); // endpoint_address
76 jacks.push_back(UsbMidiJack(device2_.get(),
77 85,
78 5,
79 137));
80 jacks.push_back(UsbMidiJack(device2_.get(),
81 84,
82 4,
83 135));
84 jacks.push_back(UsbMidiJack(device1_.get(),
85 85,
86 5,
87 135));
88
89 stream_.reset(new UsbMidiInputStream(jacks, delegate_.get()));
90 }
91
92 scoped_ptr<TestUsbMidiDevice> device1_;
93 scoped_ptr<TestUsbMidiDevice> device2_;
94 scoped_ptr<MockDelegate> delegate_;
95 scoped_ptr<UsbMidiInputStream> stream_;
96
97 private:
98 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStreamTest);
99 };
100
101 TEST_F(UsbMidiInputStreamTest, UnknownMessage) {
102 uint8 data[] = {
103 0x40, 0xff, 0xff, 0xff,
104 0x41, 0xff, 0xff, 0xff,
105 };
106 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
107 }
108
109 TEST_F(UsbMidiInputStreamTest, SystemCommonMessage) {
110 uint8 data[] = {
111 0x45, 0xf8, 0x00, 0x00,
112 0x42, 0xf3, 0x22, 0x00,
113 0x43, 0xf2, 0x33, 0x44,
114 };
115 uint8 expected1[] = { 0xf8, };
116 uint8 expected2[] = { 0xf3, 0x22, };
117 uint8 expected3[] = { 0xf2, 0x33, 0x44, };
118
119 {
120 InSequence s;
121 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0));
122 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0));
123 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0));
124 }
125
126 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
127 }
128
129 TEST_F(UsbMidiInputStreamTest, SystemExclusiveMessage) {
130 uint8 data[] = {
131 0x44, 0xf0, 0x11, 0x22,
132 0x45, 0xf7, 0x00, 0x00,
133 0x46, 0xf0, 0xf7, 0x00,
134 0x47, 0xf0, 0x33, 0xf7,
135 };
136 uint8 expected1[] = { 0xf0, 0x11, 0x22, };
137 uint8 expected2[] = { 0xf7, };
138 uint8 expected3[] = { 0xf0, 0xf7, };
139 uint8 expected4[] = { 0xf0, 0x33, 0xf7, };
140
141 {
142 InSequence s;
143 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0));
144 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0));
145 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0));
146 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected4), 0));
147 }
148
149 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
150 }
151
152 TEST_F(UsbMidiInputStreamTest, ChannelMessage) {
153 uint8 data[] = {
154 0x48, 0x80, 0x11, 0x22,
155 0x49, 0x90, 0x33, 0x44,
156 0x4a, 0xa0, 0x55, 0x66,
157 0x4b, 0xb0, 0x77, 0x88,
158 0x4c, 0xc0, 0x99, 0x00,
159 0x4d, 0xd0, 0xaa, 0x00,
160 0x4e, 0xe0, 0xbb, 0xcc,
161 };
162 uint8 expected1[] = { 0x80, 0x11, 0x22, };
163 uint8 expected2[] = { 0x90, 0x33, 0x44, };
164 uint8 expected3[] = { 0xa0, 0x55, 0x66, };
165 uint8 expected4[] = { 0xb0, 0x77, 0x88, };
166 uint8 expected5[] = { 0xc0, 0x99, };
167 uint8 expected6[] = { 0xd0, 0xaa, };
168 uint8 expected7[] = { 0xe0, 0xbb, 0xcc, };
169
170 {
171 InSequence s;
172 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0));
173 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0));
174 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0));
175 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected4), 0));
176 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected5), 0));
177 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected6), 0));
178 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected7), 0));
179 }
180
181 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
182 }
183
184 TEST_F(UsbMidiInputStreamTest, SingleByteMessage) {
185 uint8 data[] = {
186 0x4f, 0xf8, 0x00, 0x00,
187 };
188
189 uint8 expected[] = { 0xf8, };
190
191 {
192 InSequence s;
193 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected), 0));
194 }
195
196 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
197 }
198
199 TEST_F(UsbMidiInputStreamTest, DispatchForMultipleCables) {
200 uint8 data[] = {
201 0x4f, 0xf8, 0x00, 0x00,
202 0x5f, 0xfa, 0x00, 0x00,
203 0x6f, 0xfb, 0x00, 0x00,
204 };
205 uint8 expected1[] = { 0xf8, };
206 uint8 expected2[] = { 0xfa, };
207
208 {
209 InSequence s;
210 EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 99));
211 EXPECT_CALL(*delegate_, OnReceivedData(3, ToVector(expected2), 99));
212 }
213
214 stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 99);
215 }
216
217 TEST_F(UsbMidiInputStreamTest, DispatchForDevice2) {
218 uint8 data[] = { 0x4f, 0xf8, 0x00, 0x00 };
219 uint8 expected[] = { 0xf8, };
220
221 {
222 InSequence s;
223 EXPECT_CALL(*delegate_, OnReceivedData(2, ToVector(expected), 99));
224 }
225
226 stream_->OnReceivedData(device2_.get(), 7, data, arraysize(data), 99);
227 }
228
229 } // namespace
230
231 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698