[MIDI][Android] Support device attach event.
[chromium-blink-merge.git] / media / midi / usb_midi_input_stream_unittest.cc
blob0ec1568aabc6d1d6b463b48fd6b3b55d617f027e
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 stream_.reset(new UsbMidiInputStream(&delegate_));
58 stream_->Add(UsbMidiJack(&device1_,
59 84, // jack_id
60 4, // cable_number
61 135)); // endpoint_address
62 stream_->Add(UsbMidiJack(&device2_,
63 85,
65 137));
66 stream_->Add(UsbMidiJack(&device2_,
67 84,
69 135));
70 stream_->Add(UsbMidiJack(&device1_,
71 85,
73 135));
76 TestUsbMidiDevice device1_;
77 TestUsbMidiDevice device2_;
78 MockDelegate delegate_;
79 scoped_ptr<UsbMidiInputStream> stream_;
81 private:
82 DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStreamTest);
85 TEST_F(UsbMidiInputStreamTest, UnknownMessage) {
86 uint8 data[] = {
87 0x40, 0xff, 0xff, 0xff,
88 0x41, 0xff, 0xff, 0xff,
91 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
92 EXPECT_EQ("", delegate_.received_data());
95 TEST_F(UsbMidiInputStreamTest, SystemCommonMessage) {
96 uint8 data[] = {
97 0x45, 0xf8, 0x00, 0x00,
98 0x42, 0xf3, 0x22, 0x00,
99 0x43, 0xf2, 0x33, 0x44,
102 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
103 EXPECT_EQ("0xf8 \n"
104 "0xf3 0x22 \n"
105 "0xf2 0x33 0x44 \n", delegate_.received_data());
108 TEST_F(UsbMidiInputStreamTest, SystemExclusiveMessage) {
109 uint8 data[] = {
110 0x44, 0xf0, 0x11, 0x22,
111 0x45, 0xf7, 0x00, 0x00,
112 0x46, 0xf0, 0xf7, 0x00,
113 0x47, 0xf0, 0x33, 0xf7,
116 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
117 EXPECT_EQ("0xf0 0x11 0x22 \n"
118 "0xf7 \n"
119 "0xf0 0xf7 \n"
120 "0xf0 0x33 0xf7 \n", delegate_.received_data());
123 TEST_F(UsbMidiInputStreamTest, ChannelMessage) {
124 uint8 data[] = {
125 0x48, 0x80, 0x11, 0x22,
126 0x49, 0x90, 0x33, 0x44,
127 0x4a, 0xa0, 0x55, 0x66,
128 0x4b, 0xb0, 0x77, 0x88,
129 0x4c, 0xc0, 0x99, 0x00,
130 0x4d, 0xd0, 0xaa, 0x00,
131 0x4e, 0xe0, 0xbb, 0xcc,
134 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
135 EXPECT_EQ("0x80 0x11 0x22 \n"
136 "0x90 0x33 0x44 \n"
137 "0xa0 0x55 0x66 \n"
138 "0xb0 0x77 0x88 \n"
139 "0xc0 0x99 \n"
140 "0xd0 0xaa \n"
141 "0xe0 0xbb 0xcc \n", delegate_.received_data());
144 TEST_F(UsbMidiInputStreamTest, SingleByteMessage) {
145 uint8 data[] = {
146 0x4f, 0xf8, 0x00, 0x00,
149 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
150 EXPECT_EQ("0xf8 \n", delegate_.received_data());
153 TEST_F(UsbMidiInputStreamTest, DispatchForMultipleCables) {
154 uint8 data[] = {
155 0x4f, 0xf8, 0x00, 0x00,
156 0x5f, 0xfa, 0x00, 0x00,
157 0x6f, 0xfb, 0x00, 0x00,
160 stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
161 EXPECT_EQ("0xf8 \n0xfa \n", delegate_.received_data());
164 TEST_F(UsbMidiInputStreamTest, DispatchForDevice2) {
165 uint8 data[] = { 0x4f, 0xf8, 0x00, 0x00 };
167 stream_->OnReceivedData(&device2_, 7, data, arraysize(data), TimeTicks());
168 EXPECT_EQ("0xf8 \n", delegate_.received_data());
171 } // namespace
173 } // namespace media