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 kBrokenData1
[] = { 0x90 };
26 const uint8 kBrokenData2
[] = { 0xf7 };
27 const uint8 kBrokenData3
[] = { 0xf2, 0x00 };
28 const uint8 kDataByte0
[] = { 0x00 };
30 template <typename T
, size_t N
>
31 void Add(MidiMessageQueue
* queue
, const T(&array
)[N
]) {
35 template <typename T
, size_t N
>
36 ::testing::AssertionResult
ExpectEqualSequence(
37 const char* expr1
, const char* expr2
,
38 const T(&expected
)[N
], const std::vector
<T
>& actual
) {
39 if (actual
.size() != N
) {
40 return ::testing::AssertionFailure()
41 << "expected: " << ::testing::PrintToString(expected
)
42 << ", actual: " << ::testing::PrintToString(actual
);
44 for (size_t i
= 0; i
< N
; ++i
) {
45 if (expected
[i
] != actual
[i
]) {
46 return ::testing::AssertionFailure()
47 << "expected: " << ::testing::PrintToString(expected
)
48 << ", actual: " << ::testing::PrintToString(actual
);
51 return ::testing::AssertionSuccess();
54 #define EXPECT_MESSAGE(expected, actual) \
55 EXPECT_PRED_FORMAT2(ExpectEqualSequence, expected, actual)
57 TEST(MidiMessageQueueTest
, EmptyData
) {
58 MidiMessageQueue
queue(false);
59 std::vector
<uint8
> message
;
61 EXPECT_TRUE(message
.empty());
64 TEST(MidiMessageQueueTest
, RunningStatusDisabled
) {
65 MidiMessageQueue
queue(false);
67 Add(&queue
, kBrokenData1
);
68 Add(&queue
, kNoteOnWithRunningStatus
);
69 Add(&queue
, kBrokenData2
);
70 Add(&queue
, kChannelPressureWithRunningStatus
);
71 Add(&queue
, kBrokenData3
);
73 Add(&queue
, kBrokenData1
);
75 Add(&queue
, kBrokenData2
);
76 Add(&queue
, kTimingClock
);
77 Add(&queue
, kBrokenData3
);
79 std::vector
<uint8
> message
;
81 EXPECT_MESSAGE(kGMOn
, message
);
83 EXPECT_MESSAGE(kNoteOn
, message
) << "Running status should be ignored";
85 EXPECT_MESSAGE(kChannelPressure
, message
)
86 << "Running status should be ignored";
88 EXPECT_MESSAGE(kNoteOn
, message
);
90 EXPECT_MESSAGE(kGSOn
, message
);
92 EXPECT_MESSAGE(kTimingClock
, message
);
94 EXPECT_TRUE(message
.empty());
97 TEST(MidiMessageQueueTest
, RunningStatusEnabled
) {
98 MidiMessageQueue
queue(true);
100 Add(&queue
, kBrokenData1
);
101 Add(&queue
, kNoteOnWithRunningStatus
);
102 Add(&queue
, kBrokenData2
);
103 Add(&queue
, kChannelPressureWithRunningStatus
);
104 Add(&queue
, kBrokenData3
);
105 Add(&queue
, kNoteOn
);
106 Add(&queue
, kBrokenData1
);
108 Add(&queue
, kBrokenData2
);
109 Add(&queue
, kTimingClock
);
110 Add(&queue
, kDataByte0
);
112 std::vector
<uint8
> message
;
114 EXPECT_MESSAGE(kGMOn
, message
);
116 EXPECT_MESSAGE(kNoteOn
, message
);
118 EXPECT_MESSAGE(kNoteOn
, message
)
119 << "Running status should be converted into a canonical MIDI message";
121 EXPECT_MESSAGE(kNoteOn
, message
)
122 << "Running status should be converted into a canonical MIDI message";
124 EXPECT_MESSAGE(kChannelPressure
, message
);
126 EXPECT_MESSAGE(kChannelPressure
, message
)
127 << "Running status should be converted into a canonical MIDI message";
129 EXPECT_MESSAGE(kChannelPressure
, message
)
130 << "Running status should be converted into a canonical MIDI message";
132 EXPECT_MESSAGE(kNoteOn
, message
);
134 EXPECT_MESSAGE(kGSOn
, message
);
136 EXPECT_MESSAGE(kTimingClock
, message
);
138 EXPECT_TRUE(message
.empty())
139 << "Running status must not be applied to real time messages";
142 TEST(MidiMessageQueueTest
, RunningStatusEnabledWithRealTimeEvent
) {
143 MidiMessageQueue
queue(true);
144 const uint8 kNoteOnWithRunningStatusWithkTimingClock
[] = {
145 0x90, 0xf8, 0x3c, 0xf8, 0x7f, 0xf8, 0x3c, 0xf8, 0x7f, 0xf8, 0x3c, 0xf8,
148 Add(&queue
, kNoteOnWithRunningStatusWithkTimingClock
);
149 std::vector
<uint8
> message
;
151 EXPECT_MESSAGE(kTimingClock
, message
);
153 EXPECT_MESSAGE(kTimingClock
, message
);
155 EXPECT_MESSAGE(kNoteOn
, message
);
157 EXPECT_MESSAGE(kTimingClock
, message
);
159 EXPECT_MESSAGE(kTimingClock
, message
);
161 EXPECT_MESSAGE(kNoteOn
, message
);
163 EXPECT_MESSAGE(kTimingClock
, message
);
165 EXPECT_MESSAGE(kTimingClock
, message
);
167 EXPECT_MESSAGE(kNoteOn
, message
);
169 EXPECT_TRUE(message
.empty());