| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/media/midi_host.h" | 5 #include "content/browser/media/midi_host.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 12 #include "base/run_loop.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "content/common/media/midi_messages.h" | 14 #include "content/common/media/midi_messages.h" |
| 15 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
| 16 #include "media/midi/midi_manager.h" | 16 #include "media/midi/midi_manager.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 18 |
| 19 namespace content { | 19 namespace content { |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const uint8_t kGMOn[] = {0xf0, 0x7e, 0x7f, 0x09, 0x01, 0xf7}; | |
| 23 const uint8_t kGSOn[] = { | |
| 24 0xf0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7f, 0x00, 0x41, 0xf7, | |
| 25 }; | |
| 26 const uint8_t kNoteOn[] = {0x90, 0x3c, 0x7f}; | 22 const uint8_t kNoteOn[] = {0x90, 0x3c, 0x7f}; |
| 27 const uint8_t kNoteOnWithRunningStatus[] = { | |
| 28 0x90, 0x3c, 0x7f, 0x3c, 0x7f, 0x3c, 0x7f, | |
| 29 }; | |
| 30 const uint8_t kChannelPressure[] = {0xd0, 0x01}; | |
| 31 const uint8_t kChannelPressureWithRunningStatus[] = { | |
| 32 0xd0, 0x01, 0x01, 0x01, | |
| 33 }; | |
| 34 const uint8_t kTimingClock[] = {0xf8}; | |
| 35 const uint8_t kBrokenData1[] = {0x90}; | |
| 36 const uint8_t kBrokenData2[] = {0xf7}; | |
| 37 const uint8_t kBrokenData3[] = {0xf2, 0x00}; | |
| 38 const uint8_t kDataByte0[] = {0x00}; | |
| 39 | |
| 40 const int kRenderProcessId = 0; | 23 const int kRenderProcessId = 0; |
| 41 | 24 |
| 42 template <typename T, size_t N> | |
| 43 const std::vector<T> AsVector(const T(&data)[N]) { | |
| 44 std::vector<T> buffer; | |
| 45 buffer.insert(buffer.end(), data, data + N); | |
| 46 return buffer; | |
| 47 } | |
| 48 | |
| 49 template <typename T, size_t N> | |
| 50 void PushToVector(const T(&data)[N], std::vector<T>* buffer) { | |
| 51 buffer->insert(buffer->end(), data, data + N); | |
| 52 } | |
| 53 | |
| 54 enum MidiEventType { | 25 enum MidiEventType { |
| 55 DISPATCH_SEND_MIDI_DATA, | 26 DISPATCH_SEND_MIDI_DATA, |
| 56 }; | 27 }; |
| 57 | 28 |
| 58 struct MidiEvent { | 29 struct MidiEvent { |
| 59 MidiEvent(MidiEventType in_type, | 30 MidiEvent(MidiEventType in_type, |
| 60 uint32_t in_port_index, | 31 uint32_t in_port_index, |
| 61 const std::vector<uint8_t>& in_data, | 32 const std::vector<uint8_t>& in_data, |
| 62 double in_timestamp) | 33 double in_timestamp) |
| 63 : type(in_type), | 34 : type(in_type), |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 FakeMidiManager manager_; | 125 FakeMidiManager manager_; |
| 155 scoped_refptr<MidiHostForTesting> host_; | 126 scoped_refptr<MidiHostForTesting> host_; |
| 156 std::vector<uint8_t> data_; | 127 std::vector<uint8_t> data_; |
| 157 int32_t port_id_; | 128 int32_t port_id_; |
| 158 | 129 |
| 159 DISALLOW_COPY_AND_ASSIGN(MidiHostTest); | 130 DISALLOW_COPY_AND_ASSIGN(MidiHostTest); |
| 160 }; | 131 }; |
| 161 | 132 |
| 162 } // namespace | 133 } // namespace |
| 163 | 134 |
| 164 TEST_F(MidiHostTest, IsValidWebMIDIData) { | |
| 165 // Test single event scenario | |
| 166 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(AsVector(kGMOn))); | |
| 167 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(AsVector(kGSOn))); | |
| 168 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(AsVector(kNoteOn))); | |
| 169 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(AsVector(kChannelPressure))); | |
| 170 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(AsVector(kTimingClock))); | |
| 171 EXPECT_FALSE(MidiHost::IsValidWebMIDIData(AsVector(kBrokenData1))); | |
| 172 EXPECT_FALSE(MidiHost::IsValidWebMIDIData(AsVector(kBrokenData2))); | |
| 173 EXPECT_FALSE(MidiHost::IsValidWebMIDIData(AsVector(kBrokenData3))); | |
| 174 EXPECT_FALSE(MidiHost::IsValidWebMIDIData(AsVector(kDataByte0))); | |
| 175 | |
| 176 // MIDI running status should be disallowed | |
| 177 EXPECT_FALSE(MidiHost::IsValidWebMIDIData( | |
| 178 AsVector(kNoteOnWithRunningStatus))); | |
| 179 EXPECT_FALSE(MidiHost::IsValidWebMIDIData( | |
| 180 AsVector(kChannelPressureWithRunningStatus))); | |
| 181 | |
| 182 // Multiple messages are allowed as long as each of them is complete. | |
| 183 { | |
| 184 std::vector<uint8_t> buffer; | |
| 185 PushToVector(kGMOn, &buffer); | |
| 186 PushToVector(kNoteOn, &buffer); | |
| 187 PushToVector(kGSOn, &buffer); | |
| 188 PushToVector(kTimingClock, &buffer); | |
| 189 PushToVector(kNoteOn, &buffer); | |
| 190 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(buffer)); | |
| 191 PushToVector(kBrokenData1, &buffer); | |
| 192 EXPECT_FALSE(MidiHost::IsValidWebMIDIData(buffer)); | |
| 193 } | |
| 194 | |
| 195 // MIDI realtime message can be placed at any position. | |
| 196 { | |
| 197 const uint8_t kNoteOnWithRealTimeClock[] = { | |
| 198 0x90, 0xf8, 0x3c, 0x7f, 0x90, 0xf8, 0x3c, 0xf8, 0x7f, 0xf8, | |
| 199 }; | |
| 200 EXPECT_TRUE(MidiHost::IsValidWebMIDIData( | |
| 201 AsVector(kNoteOnWithRealTimeClock))); | |
| 202 | |
| 203 const uint8_t kGMOnWithRealTimeClock[] = { | |
| 204 0xf0, 0xf8, 0x7e, 0x7f, 0x09, 0x01, 0xf8, 0xf7, | |
| 205 }; | |
| 206 EXPECT_TRUE(MidiHost::IsValidWebMIDIData( | |
| 207 AsVector(kGMOnWithRealTimeClock))); | |
| 208 } | |
| 209 } | |
| 210 | |
| 211 // Test if sending data to out of range port is ignored. | 135 // Test if sending data to out of range port is ignored. |
| 212 TEST_F(MidiHostTest, OutputPortCheck) { | 136 TEST_F(MidiHostTest, OutputPortCheck) { |
| 213 // Only one output port is available. | 137 // Only one output port is available. |
| 214 AddOutputPort(); | 138 AddOutputPort(); |
| 215 | 139 |
| 216 // Sending data to port 0 should be delivered. | 140 // Sending data to port 0 should be delivered. |
| 217 uint32_t port0 = 0; | 141 uint32_t port0 = 0; |
| 218 OnSendData(port0); | 142 OnSendData(port0); |
| 219 RunLoopUntilIdle(); | 143 RunLoopUntilIdle(); |
| 220 EXPECT_EQ(1U, GetEventSize()); | 144 EXPECT_EQ(1U, GetEventSize()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 232 // Sending data to port 0 and 1 should be delivered now. | 156 // Sending data to port 0 and 1 should be delivered now. |
| 233 OnSendData(port0); | 157 OnSendData(port0); |
| 234 OnSendData(port1); | 158 OnSendData(port1); |
| 235 RunLoopUntilIdle(); | 159 RunLoopUntilIdle(); |
| 236 EXPECT_EQ(3U, GetEventSize()); | 160 EXPECT_EQ(3U, GetEventSize()); |
| 237 CheckSendEventAt(1, port0); | 161 CheckSendEventAt(1, port0); |
| 238 CheckSendEventAt(2, port1); | 162 CheckSendEventAt(2, port1); |
| 239 } | 163 } |
| 240 | 164 |
| 241 } // namespace conent | 165 } // namespace conent |
| OLD | NEW |