1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
media / midi / message_util_unittest.cc [blame]
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "media/midi/message_util.h"
#include <stdint.h>
#include "testing/gtest/include/gtest/gtest.h"
namespace midi {
namespace {
const uint8_t kGMOn[] = {0xf0, 0x7e, 0x7f, 0x09, 0x01, 0xf7};
const uint8_t kGSOn[] = {
0xf0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7f, 0x00, 0x41, 0xf7,
};
const uint8_t kNoteOn[] = {0x90, 0x3c, 0x7f};
const uint8_t kNoteOnWithRunningStatus[] = {
0x90, 0x3c, 0x7f, 0x3c, 0x7f, 0x3c, 0x7f,
};
const uint8_t kNoteOnWithRealTimeClock[] = {
0x90, 0xf8, 0x3c, 0x7f, 0x90, 0xf8, 0x3c, 0xf8, 0x7f, 0xf8,
};
const uint8_t kGMOnWithRealTimeClock[] = {
0xf0, 0xf8, 0x7e, 0x7f, 0x09, 0x01, 0xf8, 0xf7,
};
const uint8_t kSystemCommonMessageReserved1[] = {0xf4};
const uint8_t kSystemCommonMessageReserved2[] = {0xf5};
const uint8_t kSystemCommonMessageTuneRequest[] = {0xf6};
const uint8_t kChannelPressure[] = {0xd0, 0x01};
const uint8_t kChannelPressureWithRunningStatus[] = {0xd0, 0x01, 0x01, 0x01};
const uint8_t kTimingClock[] = {0xf8};
const uint8_t kBrokenData1[] = {0x90};
const uint8_t kBrokenData2[] = {0xf7};
const uint8_t kBrokenData3[] = {0xf2, 0x00};
const uint8_t kDataByte0[] = {0x00};
template <typename T, size_t N>
const std::vector<T> AsVector(const T (&data)[N]) {
std::vector<T> buffer;
buffer.insert(buffer.end(), data, data + N);
return buffer;
}
template <typename T, size_t N>
void PushToVector(const T (&data)[N], std::vector<T>* buffer) {
buffer->insert(buffer->end(), data, data + N);
}
TEST(MidiMessageUtilTest, GetMessageLength) {
// Check basic functionarity
EXPECT_EQ(std::size(kNoteOn), GetMessageLength(kNoteOn[0]));
EXPECT_EQ(std::size(kChannelPressure), GetMessageLength(kChannelPressure[0]));
EXPECT_EQ(std::size(kTimingClock), GetMessageLength(kTimingClock[0]));
EXPECT_EQ(std::size(kSystemCommonMessageTuneRequest),
GetMessageLength(kSystemCommonMessageTuneRequest[0]));
// SysEx message should be mapped to 0-length
EXPECT_EQ(0u, GetMessageLength(kGMOn[0]));
// Any reserved message should be mapped to 0-length
EXPECT_EQ(0u, GetMessageLength(kSystemCommonMessageReserved1[0]));
EXPECT_EQ(0u, GetMessageLength(kSystemCommonMessageReserved2[0]));
// Any data byte should be mapped to 0-length
EXPECT_EQ(0u, GetMessageLength(kGMOn[1]));
EXPECT_EQ(0u, GetMessageLength(kNoteOn[1]));
EXPECT_EQ(0u, GetMessageLength(kChannelPressure[1]));
}
TEST(MidiMessageUtilTest, IsValidWebMIDIData) {
// Test single event scenario
EXPECT_TRUE(IsValidWebMIDIData(AsVector(kGMOn)));
EXPECT_TRUE(IsValidWebMIDIData(AsVector(kGSOn)));
EXPECT_TRUE(IsValidWebMIDIData(AsVector(kNoteOn)));
EXPECT_TRUE(IsValidWebMIDIData(AsVector(kChannelPressure)));
EXPECT_TRUE(IsValidWebMIDIData(AsVector(kTimingClock)));
EXPECT_FALSE(IsValidWebMIDIData(AsVector(kBrokenData1)));
EXPECT_FALSE(IsValidWebMIDIData(AsVector(kBrokenData2)));
EXPECT_FALSE(IsValidWebMIDIData(AsVector(kBrokenData3)));
EXPECT_FALSE(IsValidWebMIDIData(AsVector(kDataByte0)));
// MIDI running status should be disallowed
EXPECT_FALSE(IsValidWebMIDIData(AsVector(kNoteOnWithRunningStatus)));
EXPECT_FALSE(IsValidWebMIDIData(AsVector(kChannelPressureWithRunningStatus)));
// Multiple messages are allowed as long as each of them is complete.
{
std::vector<uint8_t> buffer;
PushToVector(kGMOn, &buffer);
PushToVector(kNoteOn, &buffer);
PushToVector(kGSOn, &buffer);
PushToVector(kTimingClock, &buffer);
PushToVector(kNoteOn, &buffer);
EXPECT_TRUE(IsValidWebMIDIData(buffer));
PushToVector(kBrokenData1, &buffer);
EXPECT_FALSE(IsValidWebMIDIData(buffer));
}
// MIDI realtime message can be placed at any position.
EXPECT_TRUE(IsValidWebMIDIData(AsVector(kNoteOnWithRealTimeClock)));
EXPECT_TRUE(IsValidWebMIDIData(AsVector(kGMOnWithRealTimeClock)));
}
} // namespace
} // namespace midi