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

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 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_input_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/gmock/include/gmock/gmock.h"
scherkus (not reviewing) 2014/01/21 21:25:25 I believe you can remove this include now
yhirano 2014/01/22 02:33:31 Done.
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace media {
17
18 namespace {
19
20 class TestUsbMidiDevice : public UsbMidiDevice {
21 public:
22 TestUsbMidiDevice() {}
23 virtual ~TestUsbMidiDevice() {}
24 virtual std::vector<uint8> GetDescriptor() OVERRIDE {
25 return std::vector<uint8>();
26 }
27 virtual void Send(int endpoint_number,
28 const std::vector<uint8>& data) OVERRIDE {}
29
30 private:
31 DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDevice);
32 };
33
34 class MockDelegate : public UsbMidiInputStream::Delegate {
35 public:
36 MockDelegate() {}
37 virtual ~MockDelegate() {}
38 virtual void OnReceivedData(size_t jack_index,
39 const uint8* data,
40 size_t size,
41 double timestamp) OVERRIDE {
42 for (size_t i = 0; i < size; ++i)
43 received_data_ += base::StringPrintf("0x%02x ", data[i]);
44 received_data_ += "\n";
45 }
46
47 const std::string& received_data() const { return received_data_; }
48
49 private:
50 std::string received_data_;
51 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
52 };
53
54 class UsbMidiInputStreamTest : public ::testing::Test {
55 protected:
56 UsbMidiInputStreamTest() {
57 std::vector<UsbMidiJack> jacks;
58
59 jacks.push_back(UsbMidiJack(&device1_,
60 84, // jack_id
61 4, // cable_number
62 135)); // endpoint_address
63 jacks.push_back(UsbMidiJack(&device2_,
64 85,
65 5,
66 137));
67 jacks.push_back(UsbMidiJack(&device2_,
68 84,
69 4,
70 135));
71 jacks.push_back(UsbMidiJack(&device1_,
72 85,
73 5,
74 135));
75
76 stream_.reset(new UsbMidiInputStream(jacks, &delegate_));
77 }
78
79 TestUsbMidiDevice device1_;
80 TestUsbMidiDevice device2_;
81 MockDelegate delegate_;
82 scoped_ptr<UsbMidiInputStream> stream_;
83
84 private:
85 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStreamTest);
86 };
87
88 TEST_F(UsbMidiInputStreamTest, UnknownMessage) {
89 uint8 data[] = {
90 0x40, 0xff, 0xff, 0xff,
91 0x41, 0xff, 0xff, 0xff,
92 };
93
94 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), 0);
95 EXPECT_EQ("", delegate_.received_data());
96 }
97
98 TEST_F(UsbMidiInputStreamTest, SystemCommonMessage) {
99 uint8 data[] = {
100 0x45, 0xf8, 0x00, 0x00,
101 0x42, 0xf3, 0x22, 0x00,
102 0x43, 0xf2, 0x33, 0x44,
103 };
104
105 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), 0);
106 EXPECT_EQ("0xf8 \n"
107 "0xf3 0x22 \n"
108 "0xf2 0x33 0x44 \n", delegate_.received_data());
109 }
110
111 TEST_F(UsbMidiInputStreamTest, SystemExclusiveMessage) {
112 uint8 data[] = {
113 0x44, 0xf0, 0x11, 0x22,
114 0x45, 0xf7, 0x00, 0x00,
115 0x46, 0xf0, 0xf7, 0x00,
116 0x47, 0xf0, 0x33, 0xf7,
117 };
118
119 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), 0);
120 EXPECT_EQ("0xf0 0x11 0x22 \n"
121 "0xf7 \n"
122 "0xf0 0xf7 \n"
123 "0xf0 0x33 0xf7 \n", delegate_.received_data());
124 }
125
126 TEST_F(UsbMidiInputStreamTest, ChannelMessage) {
127 uint8 data[] = {
128 0x48, 0x80, 0x11, 0x22,
129 0x49, 0x90, 0x33, 0x44,
130 0x4a, 0xa0, 0x55, 0x66,
131 0x4b, 0xb0, 0x77, 0x88,
132 0x4c, 0xc0, 0x99, 0x00,
133 0x4d, 0xd0, 0xaa, 0x00,
134 0x4e, 0xe0, 0xbb, 0xcc,
135 };
136
137 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), 0);
138 EXPECT_EQ("0x80 0x11 0x22 \n"
139 "0x90 0x33 0x44 \n"
140 "0xa0 0x55 0x66 \n"
141 "0xb0 0x77 0x88 \n"
142 "0xc0 0x99 \n"
143 "0xd0 0xaa \n"
144 "0xe0 0xbb 0xcc \n", delegate_.received_data());
scherkus (not reviewing) 2014/01/21 21:25:25 nice tests :)
145 }
146
147 TEST_F(UsbMidiInputStreamTest, SingleByteMessage) {
148 uint8 data[] = {
149 0x4f, 0xf8, 0x00, 0x00,
150 };
151
152 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), 0);
153 EXPECT_EQ("0xf8 \n", delegate_.received_data());
154 }
155
156 TEST_F(UsbMidiInputStreamTest, DispatchForMultipleCables) {
157 uint8 data[] = {
158 0x4f, 0xf8, 0x00, 0x00,
159 0x5f, 0xfa, 0x00, 0x00,
160 0x6f, 0xfb, 0x00, 0x00,
161 };
162
163 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), 99);
164 EXPECT_EQ("0xf8 \n0xfa \n", delegate_.received_data());
165 }
166
167 TEST_F(UsbMidiInputStreamTest, DispatchForDevice2) {
168 uint8 data[] = { 0x4f, 0xf8, 0x00, 0x00 };
169
170 stream_->OnReceivedData(&device2_, 7, data, arraysize(data), 99);
171 EXPECT_EQ("0xf8 \n", delegate_.received_data());
172 }
173
174 } // namespace
175
176 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698