Roll leveldb 3f7758:803d69 (v1.17 -> v1.18)
[chromium-blink-merge.git] / media / midi / usb_midi_input_stream_unittest.cc
blob835d67273f753f2cd3606f3a4752ae0532c17ec7
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 {
20 namespace {
22 class TestUsbMidiDevice : public UsbMidiDevice {
23 public:
24 TestUsbMidiDevice() {}
25 ~TestUsbMidiDevice() override {}
26 std::vector<uint8> GetDescriptor() override { return std::vector<uint8>(); }
27 void Send(int endpoint_number, const std::vector<uint8>& data) override {}
29 private:
30 DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDevice);
33 class MockDelegate : public UsbMidiInputStream::Delegate {
34 public:
35 MockDelegate() {}
36 ~MockDelegate() override {}
37 void OnReceivedData(size_t jack_index,
38 const uint8* data,
39 size_t size,
40 base::TimeTicks time) override {
41 for (size_t i = 0; i < size; ++i)
42 received_data_ += base::StringPrintf("0x%02x ", data[i]);
43 received_data_ += "\n";
46 const std::string& received_data() const { return received_data_; }
48 private:
49 std::string received_data_;
50 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
53 class UsbMidiInputStreamTest : public ::testing::Test {
54 protected:
55 UsbMidiInputStreamTest() {
56 std::vector<UsbMidiJack> jacks;
58 jacks.push_back(UsbMidiJack(&device1_,
59 84, // jack_id
60 4, // cable_number
61 135)); // endpoint_address
62 jacks.push_back(UsbMidiJack(&device2_,
63 85,
65 137));
66 jacks.push_back(UsbMidiJack(&device2_,
67 84,
69 135));
70 jacks.push_back(UsbMidiJack(&device1_,
71 85,
73 135));
75 stream_.reset(new UsbMidiInputStream(jacks, &delegate_));
78 TestUsbMidiDevice device1_;
79 TestUsbMidiDevice device2_;
80 MockDelegate delegate_;
81 scoped_ptr<UsbMidiInputStream> stream_;
83 private:
84 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStreamTest);
87 TEST_F(UsbMidiInputStreamTest, UnknownMessage) {
88 uint8 data[] = {
89 0x40, 0xff, 0xff, 0xff,
90 0x41, 0xff, 0xff, 0xff,
93 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
94 EXPECT_EQ("", delegate_.received_data());
97 TEST_F(UsbMidiInputStreamTest, SystemCommonMessage) {
98 uint8 data[] = {
99 0x45, 0xf8, 0x00, 0x00,
100 0x42, 0xf3, 0x22, 0x00,
101 0x43, 0xf2, 0x33, 0x44,
104 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
105 EXPECT_EQ("0xf8 \n"
106 "0xf3 0x22 \n"
107 "0xf2 0x33 0x44 \n", delegate_.received_data());
110 TEST_F(UsbMidiInputStreamTest, SystemExclusiveMessage) {
111 uint8 data[] = {
112 0x44, 0xf0, 0x11, 0x22,
113 0x45, 0xf7, 0x00, 0x00,
114 0x46, 0xf0, 0xf7, 0x00,
115 0x47, 0xf0, 0x33, 0xf7,
118 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
119 EXPECT_EQ("0xf0 0x11 0x22 \n"
120 "0xf7 \n"
121 "0xf0 0xf7 \n"
122 "0xf0 0x33 0xf7 \n", delegate_.received_data());
125 TEST_F(UsbMidiInputStreamTest, ChannelMessage) {
126 uint8 data[] = {
127 0x48, 0x80, 0x11, 0x22,
128 0x49, 0x90, 0x33, 0x44,
129 0x4a, 0xa0, 0x55, 0x66,
130 0x4b, 0xb0, 0x77, 0x88,
131 0x4c, 0xc0, 0x99, 0x00,
132 0x4d, 0xd0, 0xaa, 0x00,
133 0x4e, 0xe0, 0xbb, 0xcc,
136 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
137 EXPECT_EQ("0x80 0x11 0x22 \n"
138 "0x90 0x33 0x44 \n"
139 "0xa0 0x55 0x66 \n"
140 "0xb0 0x77 0x88 \n"
141 "0xc0 0x99 \n"
142 "0xd0 0xaa \n"
143 "0xe0 0xbb 0xcc \n", delegate_.received_data());
146 TEST_F(UsbMidiInputStreamTest, SingleByteMessage) {
147 uint8 data[] = {
148 0x4f, 0xf8, 0x00, 0x00,
151 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
152 EXPECT_EQ("0xf8 \n", delegate_.received_data());
155 TEST_F(UsbMidiInputStreamTest, DispatchForMultipleCables) {
156 uint8 data[] = {
157 0x4f, 0xf8, 0x00, 0x00,
158 0x5f, 0xfa, 0x00, 0x00,
159 0x6f, 0xfb, 0x00, 0x00,
162 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
163 EXPECT_EQ("0xf8 \n0xfa \n", delegate_.received_data());
166 TEST_F(UsbMidiInputStreamTest, DispatchForDevice2) {
167 uint8 data[] = { 0x4f, 0xf8, 0x00, 0x00 };
169 stream_->OnReceivedData(&device2_, 7, data, arraysize(data), TimeTicks());
170 EXPECT_EQ("0xf8 \n", delegate_.received_data());
173 } // namespace
175 } // namespace media