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"
13 const uint8 kGMOn
[] = { 0xf0, 0x7e, 0x7f, 0x09, 0x01, 0xf7 };
14 const uint8 kGSOn
[] = {
15 0xf0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7f, 0x00, 0x41, 0xf7,
17 const uint8 kNoteOn
[] = { 0x90, 0x3c, 0x7f };
18 const uint8 kNoteOnWithRunningStatus
[] = {
19 0x90, 0x3c, 0x7f, 0x3c, 0x7f, 0x3c, 0x7f,
21 const uint8 kChannelPressure
[] = { 0xd0, 0x01 };
22 const uint8 kChannelPressureWithRunningStatus
[] = {
23 0xd0, 0x01, 0x01, 0x01,
25 const uint8 kTimingClock
[] = { 0xf8 };
26 const uint8 kMTCFrame
[] = { 0xf1, 0x00 };
27 const uint8 kBrokenData1
[] = { 0x90 };
28 const uint8 kBrokenData2
[] = { 0xf7 };
29 const uint8 kBrokenData3
[] = { 0xf2, 0x00 };
30 const uint8 kDataByte0
[] = { 0x00 };
32 template <typename T
, size_t N
>
33 void Add(MidiMessageQueue
* queue
, const T(&array
)[N
]) {
37 template <typename T
, size_t N
>
38 ::testing::AssertionResult
ExpectEqualSequence(
39 const char* expr1
, const char* expr2
,
40 const T(&expected
)[N
], const std::vector
<T
>& actual
) {
41 if (actual
.size() != N
) {
42 return ::testing::AssertionFailure()
43 << "expected: " << ::testing::PrintToString(expected
)
44 << ", actual: " << ::testing::PrintToString(actual
);
46 for (size_t i
= 0; i
< N
; ++i
) {
47 if (expected
[i
] != actual
[i
]) {
48 return ::testing::AssertionFailure()
49 << "expected: " << ::testing::PrintToString(expected
)
50 << ", actual: " << ::testing::PrintToString(actual
);
53 return ::testing::AssertionSuccess();
56 #define EXPECT_MESSAGE(expected, actual) \
57 EXPECT_PRED_FORMAT2(ExpectEqualSequence, expected, actual)
59 TEST(MidiMessageQueueTest
, EmptyData
) {
60 MidiMessageQueue
queue(false);
61 std::vector
<uint8
> message
;
63 EXPECT_TRUE(message
.empty());
66 TEST(MidiMessageQueueTest
, RunningStatusDisabled
) {
67 MidiMessageQueue
queue(false);
69 Add(&queue
, kBrokenData1
);
70 Add(&queue
, kNoteOnWithRunningStatus
);
71 Add(&queue
, kBrokenData2
);
72 Add(&queue
, kChannelPressureWithRunningStatus
);
73 Add(&queue
, kBrokenData3
);
75 Add(&queue
, kBrokenData1
);
77 Add(&queue
, kBrokenData2
);
78 Add(&queue
, kTimingClock
);
79 Add(&queue
, kBrokenData3
);
81 std::vector
<uint8
> message
;
83 EXPECT_MESSAGE(kGMOn
, message
);
85 EXPECT_MESSAGE(kNoteOn
, message
) << "Running status should be ignored";
87 EXPECT_MESSAGE(kChannelPressure
, message
)
88 << "Running status should be ignored";
90 EXPECT_MESSAGE(kNoteOn
, message
);
92 EXPECT_MESSAGE(kGSOn
, message
);
94 EXPECT_MESSAGE(kTimingClock
, message
);
96 EXPECT_TRUE(message
.empty());
99 TEST(MidiMessageQueueTest
, RunningStatusEnabled
) {
100 MidiMessageQueue
queue(true);
102 Add(&queue
, kBrokenData1
);
103 Add(&queue
, kNoteOnWithRunningStatus
);
104 Add(&queue
, kBrokenData2
);
105 Add(&queue
, kChannelPressureWithRunningStatus
);
106 Add(&queue
, kBrokenData3
);
107 Add(&queue
, kNoteOn
);
108 Add(&queue
, kBrokenData1
);
110 Add(&queue
, kBrokenData2
);
111 Add(&queue
, kTimingClock
);
112 Add(&queue
, kDataByte0
);
114 std::vector
<uint8
> message
;
116 EXPECT_MESSAGE(kGMOn
, message
);
118 EXPECT_MESSAGE(kNoteOn
, message
);
120 EXPECT_MESSAGE(kNoteOn
, message
)
121 << "Running status should be converted into a canonical MIDI message";
123 EXPECT_MESSAGE(kNoteOn
, message
)
124 << "Running status should be converted into a canonical MIDI message";
126 EXPECT_MESSAGE(kChannelPressure
, message
);
128 EXPECT_MESSAGE(kChannelPressure
, message
)
129 << "Running status should be converted into a canonical MIDI message";
131 EXPECT_MESSAGE(kChannelPressure
, message
)
132 << "Running status should be converted into a canonical MIDI message";
134 EXPECT_MESSAGE(kNoteOn
, message
);
136 EXPECT_MESSAGE(kGSOn
, message
);
138 EXPECT_MESSAGE(kTimingClock
, message
);
140 EXPECT_TRUE(message
.empty())
141 << "Running status must not be applied to real time messages";
144 TEST(MidiMessageQueueTest
, RunningStatusEnabledWithRealTimeEvent
) {
145 MidiMessageQueue
queue(true);
146 const uint8 kNoteOnWithRunningStatusWithTimingClock
[] = {
147 0x90, 0xf8, 0x3c, 0xf8, 0x7f, 0xf8, 0x3c, 0xf8, 0x7f, 0xf8, 0x3c, 0xf8,
150 Add(&queue
, kNoteOnWithRunningStatusWithTimingClock
);
151 std::vector
<uint8
> message
;
153 EXPECT_MESSAGE(kTimingClock
, message
);
155 EXPECT_MESSAGE(kTimingClock
, message
);
157 EXPECT_MESSAGE(kNoteOn
, message
);
159 EXPECT_MESSAGE(kTimingClock
, message
);
161 EXPECT_MESSAGE(kTimingClock
, message
);
163 EXPECT_MESSAGE(kNoteOn
, message
);
165 EXPECT_MESSAGE(kTimingClock
, message
);
167 EXPECT_MESSAGE(kTimingClock
, message
);
169 EXPECT_MESSAGE(kNoteOn
, message
);
171 EXPECT_TRUE(message
.empty());
174 TEST(MidiMessageQueueTest
, RunningStatusEnabledWithSystemCommonMessage
) {
175 MidiMessageQueue
queue(true);
176 const uint8 kNoteOnWithRunningStatusWithSystemCommonMessage
[] = {
177 0x90, 0x3c, 0x7f, 0xf1, 0x00, 0x3c, 0x7f, 0xf8, 0x90, 0x3c, 0x7f,
179 Add(&queue
, kNoteOnWithRunningStatusWithSystemCommonMessage
);
180 std::vector
<uint8
> message
;
182 EXPECT_MESSAGE(kNoteOn
, message
);
184 EXPECT_MESSAGE(kMTCFrame
, message
);
186 EXPECT_MESSAGE(kTimingClock
, message
) << "Running status should be reset";
188 EXPECT_MESSAGE(kNoteOn
, message
);
190 EXPECT_TRUE(message
.empty());