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
3 // found in the LICENSE file.
4
5 #include "content/browser/media/midi_host.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace content {
10 namespace {
11
12 const uint8 kGMOn[] = { 0xf0, 0x7e, 0x7f, 0x09, 0x01, 0xf7 };
13 const uint8 kGSOn[] = {
14 0xf0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7f, 0x00, 0x41, 0xf7,
15 };
16 const uint8 kNoteOn[] = { 0x90, 0x3c, 0x7f };
17 const uint8 kNoteOnWithRunningStatus[] = {
18 0x90, 0x3c, 0x7f, 0x3c, 0x7f, 0x3c, 0x7f,
19 };
20 const uint8 kChannelPressure[] = { 0xd0, 0x01 };
21 const uint8 kChannelPressureWithRunningStatus[] = {
22 0xd0, 0x01, 0x01, 0x01,
23 };
24 const uint8 kTimingClock[] = { 0xf8 };
25 const uint8 kBrokenData1[] = { 0x90 };
26 const uint8 kBrokenData2[] = { 0xf7 };
27 const uint8 kBrokenData3[] = { 0xf2, 0x00 };
28 const uint8 kDataByte0[] = { 0x00 };
29
30 template <typename T, size_t N>
AsVector(const T (& data)[N])31 const std::vector<T> AsVector(const T(&data)[N]) {
32 std::vector<T> buffer;
33 buffer.insert(buffer.end(), data, data + N);
34 return buffer;
35 }
36
37 template <typename T, size_t N>
PushToVector(const T (& data)[N],std::vector<T> * buffer)38 void PushToVector(const T(&data)[N], std::vector<T>* buffer) {
39 buffer->insert(buffer->end(), data, data + N);
40 }
41
42 } // namespace
43
TEST(MidiHostTest,IsValidWebMIDIData)44 TEST(MidiHostTest, IsValidWebMIDIData) {
45 // Test single event scenario
46 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(AsVector(kGMOn)));
47 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(AsVector(kGSOn)));
48 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(AsVector(kNoteOn)));
49 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(AsVector(kChannelPressure)));
50 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(AsVector(kTimingClock)));
51 EXPECT_FALSE(MidiHost::IsValidWebMIDIData(AsVector(kBrokenData1)));
52 EXPECT_FALSE(MidiHost::IsValidWebMIDIData(AsVector(kBrokenData2)));
53 EXPECT_FALSE(MidiHost::IsValidWebMIDIData(AsVector(kBrokenData3)));
54 EXPECT_FALSE(MidiHost::IsValidWebMIDIData(AsVector(kDataByte0)));
55
56 // MIDI running status should be disallowed
57 EXPECT_FALSE(MidiHost::IsValidWebMIDIData(
58 AsVector(kNoteOnWithRunningStatus)));
59 EXPECT_FALSE(MidiHost::IsValidWebMIDIData(
60 AsVector(kChannelPressureWithRunningStatus)));
61
62 // Multiple messages are allowed as long as each of them is complete.
63 {
64 std::vector<uint8> buffer;
65 PushToVector(kGMOn, &buffer);
66 PushToVector(kNoteOn, &buffer);
67 PushToVector(kGSOn, &buffer);
68 PushToVector(kTimingClock, &buffer);
69 PushToVector(kNoteOn, &buffer);
70 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(buffer));
71 PushToVector(kBrokenData1, &buffer);
72 EXPECT_FALSE(MidiHost::IsValidWebMIDIData(buffer));
73 }
74
75 // MIDI realtime message can be placed at any position.
76 {
77 const uint8 kNoteOnWithRealTimeClock[] = {
78 0x90, 0xf8, 0x3c, 0x7f, 0x90, 0xf8, 0x3c, 0xf8, 0x7f, 0xf8,
79 };
80 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(
81 AsVector(kNoteOnWithRealTimeClock)));
82
83 const uint8 kGMOnWithRealTimeClock[] = {
84 0xf0, 0xf8, 0x7e, 0x7f, 0x09, 0x01, 0xf8, 0xf7,
85 };
86 EXPECT_TRUE(MidiHost::IsValidWebMIDIData(
87 AsVector(kGMOnWithRealTimeClock)));
88 }
89 }
90
91 } // namespace conent
92