Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / media / midi / usb_midi_input_stream_unittest.cc
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 "base/time/time.h"
13 #include "media/midi/usb_midi_device.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using base::TimeTicks;
17
18 namespace media {
19
20 namespace {
21
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 {}
28
29  private:
30   DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDevice);
31 };
32
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";
44   }
45
46   const std::string& received_data() const { return received_data_; }
47
48  private:
49   std::string received_data_;
50   DISALLOW_COPY_AND_ASSIGN(MockDelegate);
51 };
52
53 class UsbMidiInputStreamTest : public ::testing::Test {
54  protected:
55   UsbMidiInputStreamTest() {
56     std::vector<UsbMidiJack> jacks;
57
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,
64                                 5,
65                                 137));
66     jacks.push_back(UsbMidiJack(&device2_,
67                                 84,
68                                 4,
69                                 135));
70     jacks.push_back(UsbMidiJack(&device1_,
71                                 85,
72                                 5,
73                                 135));
74
75     stream_.reset(new UsbMidiInputStream(jacks, &delegate_));
76   }
77
78   TestUsbMidiDevice device1_;
79   TestUsbMidiDevice device2_;
80   MockDelegate delegate_;
81   scoped_ptr<UsbMidiInputStream> stream_;
82
83  private:
84   DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStreamTest);
85 };
86
87 TEST_F(UsbMidiInputStreamTest, UnknownMessage) {
88   uint8 data[] = {
89     0x40, 0xff, 0xff, 0xff,
90     0x41, 0xff, 0xff, 0xff,
91   };
92
93   stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
94   EXPECT_EQ("", delegate_.received_data());
95 }
96
97 TEST_F(UsbMidiInputStreamTest, SystemCommonMessage) {
98   uint8 data[] = {
99     0x45, 0xf8, 0x00, 0x00,
100     0x42, 0xf3, 0x22, 0x00,
101     0x43, 0xf2, 0x33, 0x44,
102   };
103
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());
108 }
109
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,
116   };
117
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());
123 }
124
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,
134   };
135
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());
144 }
145
146 TEST_F(UsbMidiInputStreamTest, SingleByteMessage) {
147   uint8 data[] = {
148     0x4f, 0xf8, 0x00, 0x00,
149   };
150
151   stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
152   EXPECT_EQ("0xf8 \n", delegate_.received_data());
153 }
154
155 TEST_F(UsbMidiInputStreamTest, DispatchForMultipleCables) {
156   uint8 data[] = {
157     0x4f, 0xf8, 0x00, 0x00,
158     0x5f, 0xfa, 0x00, 0x00,
159     0x6f, 0xfb, 0x00, 0x00,
160   };
161
162   stream_->OnReceivedData(&device1_, 7, data, arraysize(data), TimeTicks());
163   EXPECT_EQ("0xf8 \n0xfa \n", delegate_.received_data());
164 }
165
166 TEST_F(UsbMidiInputStreamTest, DispatchForDevice2) {
167   uint8 data[] = { 0x4f, 0xf8, 0x00, 0x00 };
168
169   stream_->OnReceivedData(&device2_, 7, data, arraysize(data), TimeTicks());
170   EXPECT_EQ("0xf8 \n", delegate_.received_data());
171 }
172
173 }  // namespace
174
175 }  // namespace media