clang/win: Fix component builds after https://codereview.chromium.org/1272113002
[chromium-blink-merge.git] / media / midi / usb_midi_input_stream_unittest.cc
blob99a969052e13e453bac643afb874c62ee69beee7
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.
5 #include "media/midi/usb_midi_input_stream.h"
7 #include <string>
8 #include <vector>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/stringprintf.h"
12 #include "base/time/time.h"
13 #include "media/midi/usb_midi_device.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using base::TimeTicks;
18 namespace media {
19 namespace midi {
21 namespace {
23 class TestUsbMidiDevice : public UsbMidiDevice {
24 public:
25 TestUsbMidiDevice() {}
26 ~TestUsbMidiDevice() override {}
27 std::vector<uint8> GetDescriptors() override { return std::vector<uint8>(); }
28 std::string GetManufacturer() override { return std::string(); }
29 std::string GetProductName() override { return std::string(); }
30 std::string GetDeviceVersion() override { return std::string(); }
31 void Send(int endpoint_number, const std::vector<uint8>& data) override {}
33 private:
34 DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDevice);
37 class MockDelegate : public UsbMidiInputStream::Delegate {
38 public:
39 MockDelegate() {}
40 ~MockDelegate() override {}
41 void OnReceivedData(size_t jack_index,
42 const uint8* data,
43 size_t size,
44 base::TimeTicks time) override {
45 for (size_t i = 0; i < size; ++i)
46 received_data_ += base::StringPrintf("0x%02x ", data[i]);
47 received_data_ += "\n";
50 const std::string& received_data() const { return received_data_; }
52 private:
53 std::string received_data_;
54 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
57 class UsbMidiInputStreamTest : public ::testing::Test {
58 protected:
59 UsbMidiInputStreamTest() {
60 stream_.reset(new UsbMidiInputStream(&delegate_));
62 stream_->Add(UsbMidiJack(&device1_,
63 84, // jack_id
64 4, // cable_number
65 135)); // endpoint_address
66 stream_->Add(UsbMidiJack(&device2_,
67 85,
69 137));
70 stream_->Add(UsbMidiJack(&device2_,
71 84,
73 135));
74 stream_->Add(UsbMidiJack(&device1_,
75 85,
77 135));
80 TestUsbMidiDevice device1_;
81 TestUsbMidiDevice device2_;
82 MockDelegate delegate_;
83 scoped_ptr<UsbMidiInputStream> stream_;
85 private:
86 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStreamTest);
89 TEST_F(UsbMidiInputStreamTest, UnknownMessage) {
90 uint8 data[] = {
91 0x40, 0xff, 0xff, 0xff,
92 0x41, 0xff, 0xff, 0xff,
95 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
96 EXPECT_EQ("", delegate_.received_data());
99 TEST_F(UsbMidiInputStreamTest, SystemCommonMessage) {
100 uint8 data[] = {
101 0x45, 0xf8, 0x00, 0x00,
102 0x42, 0xf3, 0x22, 0x00,
103 0x43, 0xf2, 0x33, 0x44,
106 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
107 EXPECT_EQ("0xf8 \n"
108 "0xf3 0x22 \n"
109 "0xf2 0x33 0x44 \n", delegate_.received_data());
112 TEST_F(UsbMidiInputStreamTest, SystemExclusiveMessage) {
113 uint8 data[] = {
114 0x44, 0xf0, 0x11, 0x22,
115 0x45, 0xf7, 0x00, 0x00,
116 0x46, 0xf0, 0xf7, 0x00,
117 0x47, 0xf0, 0x33, 0xf7,
120 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
121 EXPECT_EQ("0xf0 0x11 0x22 \n"
122 "0xf7 \n"
123 "0xf0 0xf7 \n"
124 "0xf0 0x33 0xf7 \n", delegate_.received_data());
127 TEST_F(UsbMidiInputStreamTest, ChannelMessage) {
128 uint8 data[] = {
129 0x48, 0x80, 0x11, 0x22,
130 0x49, 0x90, 0x33, 0x44,
131 0x4a, 0xa0, 0x55, 0x66,
132 0x4b, 0xb0, 0x77, 0x88,
133 0x4c, 0xc0, 0x99, 0x00,
134 0x4d, 0xd0, 0xaa, 0x00,
135 0x4e, 0xe0, 0xbb, 0xcc,
138 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
139 EXPECT_EQ("0x80 0x11 0x22 \n"
140 "0x90 0x33 0x44 \n"
141 "0xa0 0x55 0x66 \n"
142 "0xb0 0x77 0x88 \n"
143 "0xc0 0x99 \n"
144 "0xd0 0xaa \n"
145 "0xe0 0xbb 0xcc \n", delegate_.received_data());
148 TEST_F(UsbMidiInputStreamTest, SingleByteMessage) {
149 uint8 data[] = {
150 0x4f, 0xf8, 0x00, 0x00,
153 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
154 EXPECT_EQ("0xf8 \n", delegate_.received_data());
157 TEST_F(UsbMidiInputStreamTest, DispatchForMultipleCables) {
158 uint8 data[] = {
159 0x4f, 0xf8, 0x00, 0x00,
160 0x5f, 0xfa, 0x00, 0x00,
161 0x6f, 0xfb, 0x00, 0x00,
164 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
165 EXPECT_EQ("0xf8 \n0xfa \n", delegate_.received_data());
168 TEST_F(UsbMidiInputStreamTest, DispatchForDevice2) {
169 uint8 data[] = { 0x4f, 0xf8, 0x00, 0x00 };
171 stream_->OnReceivedData(&device2_, 7, data, arraysize(data), TimeTicks());
172 EXPECT_EQ("0xf8 \n", delegate_.received_data());
175 } // namespace
177 } // namespace midi
178 } // namespace media