Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / media / midi / usb_midi_input_stream_unittest.cc
blob153dc4406918c6789394ca55aaa95ff34e9c9da3
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> GetDescriptors() override { return std::vector<uint8>(); }
27 std::string GetManufacturer() override { return std::string(); }
28 std::string GetProductName() override { return std::string(); }
29 std::string GetDeviceVersion() override { return std::string(); }
30 void Send(int endpoint_number, const std::vector<uint8>& data) override {}
32 private:
33 DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDevice);
36 class MockDelegate : public UsbMidiInputStream::Delegate {
37 public:
38 MockDelegate() {}
39 ~MockDelegate() override {}
40 void OnReceivedData(size_t jack_index,
41 const uint8* data,
42 size_t size,
43 base::TimeTicks time) override {
44 for (size_t i = 0; i < size; ++i)
45 received_data_ += base::StringPrintf("0x%02x ", data[i]);
46 received_data_ += "\n";
49 const std::string& received_data() const { return received_data_; }
51 private:
52 std::string received_data_;
53 DISALLOW_COPY_AND_ASSIGN(MockDelegate);
56 class UsbMidiInputStreamTest : public ::testing::Test {
57 protected:
58 UsbMidiInputStreamTest() {
59 stream_.reset(new UsbMidiInputStream(&delegate_));
61 stream_->Add(UsbMidiJack(&device1_,
62 84, // jack_id
63 4, // cable_number
64 135)); // endpoint_address
65 stream_->Add(UsbMidiJack(&device2_,
66 85,
68 137));
69 stream_->Add(UsbMidiJack(&device2_,
70 84,
72 135));
73 stream_->Add(UsbMidiJack(&device1_,
74 85,
76 135));
79 TestUsbMidiDevice device1_;
80 TestUsbMidiDevice device2_;
81 MockDelegate delegate_;
82 scoped_ptr<UsbMidiInputStream> stream_;
84 private:
85 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStreamTest);
88 TEST_F(UsbMidiInputStreamTest, UnknownMessage) {
89 uint8 data[] = {
90 0x40, 0xff, 0xff, 0xff,
91 0x41, 0xff, 0xff, 0xff,
94 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
95 EXPECT_EQ("", delegate_.received_data());
98 TEST_F(UsbMidiInputStreamTest, SystemCommonMessage) {
99 uint8 data[] = {
100 0x45, 0xf8, 0x00, 0x00,
101 0x42, 0xf3, 0x22, 0x00,
102 0x43, 0xf2, 0x33, 0x44,
105 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
106 EXPECT_EQ("0xf8 \n"
107 "0xf3 0x22 \n"
108 "0xf2 0x33 0x44 \n", delegate_.received_data());
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,
119 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
120 EXPECT_EQ("0xf0 0x11 0x22 \n"
121 "0xf7 \n"
122 "0xf0 0xf7 \n"
123 "0xf0 0x33 0xf7 \n", delegate_.received_data());
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,
137 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
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());
147 TEST_F(UsbMidiInputStreamTest, SingleByteMessage) {
148 uint8 data[] = {
149 0x4f, 0xf8, 0x00, 0x00,
152 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
153 EXPECT_EQ("0xf8 \n", delegate_.received_data());
156 TEST_F(UsbMidiInputStreamTest, DispatchForMultipleCables) {
157 uint8 data[] = {
158 0x4f, 0xf8, 0x00, 0x00,
159 0x5f, 0xfa, 0x00, 0x00,
160 0x6f, 0xfb, 0x00, 0x00,
163 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
164 EXPECT_EQ("0xf8 \n0xfa \n", delegate_.received_data());
167 TEST_F(UsbMidiInputStreamTest, DispatchForDevice2) {
168 uint8 data[] = { 0x4f, 0xf8, 0x00, 0x00 };
170 stream_->OnReceivedData(&device2_, 7, data, arraysize(data), TimeTicks());
171 EXPECT_EQ("0xf8 \n", delegate_.received_data());
174 } // namespace
176 } // namespace media