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.
5 #include "media/midi/midi_message_queue.h"
7 #include "testing/gtest/include/gtest/gtest.h"
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,
16 const uint8 kNoteOn
[] = { 0x90, 0x3c, 0x7f };
17 const uint8 kNoteOnWithRunningStatus
[] = {
18 0x90, 0x3c, 0x7f, 0x3c, 0x7f, 0x3c, 0x7f,
20 const uint8 kChannelPressure
[] = { 0xd0, 0x01 };
21 const uint8 kChannelPressureWithRunningStatus
[] = {
22 0xd0, 0x01, 0x01, 0x01,
24 const uint8 kTimingClock
[] = { 0xf8 };
25 const uint8 kMTCFrame
[] = { 0xf1, 0x00 };
26 const uint8 kBrokenData1
[] = { 0x90 };
27 const uint8 kBrokenData2
[] = { 0xf7 };
28 const uint8 kBrokenData3
[] = { 0xf2, 0x00 };
29 const uint8 kDataByte0
[] = { 0x00 };
31 template <typename T
, size_t N
>
32 void Add(MidiMessageQueue
* queue
, const T(&array
)[N
]) {
36 template <typename T
, size_t N
>
37 ::testing::AssertionResult
ExpectEqualSequence(
38 const char* expr1
, const char* expr2
,
39 const T(&expected
)[N
], const std::vector
<T
>& actual
) {
40 if (actual
.size() != N
) {
41 return ::testing::AssertionFailure()
42 << "expected: " << ::testing::PrintToString(expected
)
43 << ", actual: " << ::testing::PrintToString(actual
);
45 for (size_t i
= 0; i
< N
; ++i
) {
46 if (expected
[i
] != actual
[i
]) {
47 return ::testing::AssertionFailure()
48 << "expected: " << ::testing::PrintToString(expected
)
49 << ", actual: " << ::testing::PrintToString(actual
);
52 return ::testing::AssertionSuccess();
55 #define EXPECT_MESSAGE(expected, actual) \
56 EXPECT_PRED_FORMAT2(ExpectEqualSequence, expected, actual)
58 TEST(MidiMessageQueueTest
, EmptyData
) {
59 MidiMessageQueue
queue(false);
60 std::vector
<uint8
> message
;
62 EXPECT_TRUE(message
.empty());
65 TEST(MidiMessageQueueTest
, RunningStatusDisabled
) {
66 MidiMessageQueue
queue(false);
68 Add(&queue
, kBrokenData1
);
69 Add(&queue
, kNoteOnWithRunningStatus
);
70 Add(&queue
, kBrokenData2
);
71 Add(&queue
, kChannelPressureWithRunningStatus
);
72 Add(&queue
, kBrokenData3
);
74 Add(&queue
, kBrokenData1
);
76 Add(&queue
, kBrokenData2
);
77 Add(&queue
, kTimingClock
);
78 Add(&queue
, kBrokenData3
);
80 std::vector
<uint8
> message
;
82 EXPECT_MESSAGE(kGMOn
, message
);
84 EXPECT_MESSAGE(kNoteOn
, message
) << "Running status should be ignored";
86 EXPECT_MESSAGE(kChannelPressure
, message
)
87 << "Running status should be ignored";
89 EXPECT_MESSAGE(kNoteOn
, message
);
91 EXPECT_MESSAGE(kGSOn
, message
);
93 EXPECT_MESSAGE(kTimingClock
, message
);
95 EXPECT_TRUE(message
.empty());
98 TEST(MidiMessageQueueTest
, RunningStatusEnabled
) {
99 MidiMessageQueue
queue(true);
101 Add(&queue
, kBrokenData1
);
102 Add(&queue
, kNoteOnWithRunningStatus
);
103 Add(&queue
, kBrokenData2
);
104 Add(&queue
, kChannelPressureWithRunningStatus
);
105 Add(&queue
, kBrokenData3
);
106 Add(&queue
, kNoteOn
);
107 Add(&queue
, kBrokenData1
);
109 Add(&queue
, kBrokenData2
);
110 Add(&queue
, kTimingClock
);
111 Add(&queue
, kDataByte0
);
113 std::vector
<uint8
> message
;
115 EXPECT_MESSAGE(kGMOn
, message
);
117 EXPECT_MESSAGE(kNoteOn
, message
);
119 EXPECT_MESSAGE(kNoteOn
, message
)
120 << "Running status should be converted into a canonical MIDI message";
122 EXPECT_MESSAGE(kNoteOn
, message
)
123 << "Running status should be converted into a canonical MIDI message";
125 EXPECT_MESSAGE(kChannelPressure
, message
);
127 EXPECT_MESSAGE(kChannelPressure
, message
)
128 << "Running status should be converted into a canonical MIDI message";
130 EXPECT_MESSAGE(kChannelPressure
, message
)
131 << "Running status should be converted into a canonical MIDI message";
133 EXPECT_MESSAGE(kNoteOn
, message
);
135 EXPECT_MESSAGE(kGSOn
, message
);
137 EXPECT_MESSAGE(kTimingClock
, message
);
139 EXPECT_TRUE(message
.empty())
140 << "Running status must not be applied to real time messages";
143 TEST(MidiMessageQueueTest
, RunningStatusEnabledWithRealTimeEvent
) {
144 MidiMessageQueue
queue(true);
145 const uint8 kNoteOnWithRunningStatusWithTimingClock
[] = {
146 0x90, 0xf8, 0x3c, 0xf8, 0x7f, 0xf8, 0x3c, 0xf8, 0x7f, 0xf8, 0x3c, 0xf8,
149 Add(&queue
, kNoteOnWithRunningStatusWithTimingClock
);
150 std::vector
<uint8
> message
;
152 EXPECT_MESSAGE(kTimingClock
, message
);
154 EXPECT_MESSAGE(kTimingClock
, message
);
156 EXPECT_MESSAGE(kNoteOn
, message
);
158 EXPECT_MESSAGE(kTimingClock
, message
);
160 EXPECT_MESSAGE(kTimingClock
, message
);
162 EXPECT_MESSAGE(kNoteOn
, message
);
164 EXPECT_MESSAGE(kTimingClock
, message
);
166 EXPECT_MESSAGE(kTimingClock
, message
);
168 EXPECT_MESSAGE(kNoteOn
, message
);
170 EXPECT_TRUE(message
.empty());
173 TEST(MidiMessageQueueTest
, RunningStatusEnabledWithSystemCommonMessage
) {
174 MidiMessageQueue
queue(true);
175 const uint8 kNoteOnWithRunningStatusWithSystemCommonMessage
[] = {
176 0x90, 0x3c, 0x7f, 0xf1, 0x00, 0x3c, 0x7f, 0xf8, 0x90, 0x3c, 0x7f,
178 Add(&queue
, kNoteOnWithRunningStatusWithSystemCommonMessage
);
179 std::vector
<uint8
> message
;
181 EXPECT_MESSAGE(kNoteOn
, message
);
183 EXPECT_MESSAGE(kMTCFrame
, message
);
185 EXPECT_MESSAGE(kTimingClock
, message
) << "Running status should be reset";
187 EXPECT_MESSAGE(kNoteOn
, message
);
189 EXPECT_TRUE(message
.empty());