blob: 2580641b258849f10a9577f6c4b04f62286b08bd [file] [log] [blame]
toyoshimbb2750c2016-10-20 05:13:24 -07001// 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 "media/midi/message_util.h"
6
7#include <stdint.h>
8
toyoshimbb2750c2016-10-20 05:13:24 -07009#include "testing/gtest/include/gtest/gtest.h"
10
11namespace midi {
12namespace {
13
14const uint8_t kGMOn[] = {0xf0, 0x7e, 0x7f, 0x09, 0x01, 0xf7};
15const uint8_t kGSOn[] = {
16 0xf0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7f, 0x00, 0x41, 0xf7,
17};
18const uint8_t kNoteOn[] = {0x90, 0x3c, 0x7f};
19const uint8_t kNoteOnWithRunningStatus[] = {
20 0x90, 0x3c, 0x7f, 0x3c, 0x7f, 0x3c, 0x7f,
21};
22const uint8_t kNoteOnWithRealTimeClock[] = {
23 0x90, 0xf8, 0x3c, 0x7f, 0x90, 0xf8, 0x3c, 0xf8, 0x7f, 0xf8,
24};
25const uint8_t kGMOnWithRealTimeClock[] = {
26 0xf0, 0xf8, 0x7e, 0x7f, 0x09, 0x01, 0xf8, 0xf7,
27};
28const uint8_t kSystemCommonMessageReserved1[] = {0xf4};
29const uint8_t kSystemCommonMessageReserved2[] = {0xf5};
30const uint8_t kSystemCommonMessageTuneRequest[] = {0xf6};
31const uint8_t kChannelPressure[] = {0xd0, 0x01};
32const uint8_t kChannelPressureWithRunningStatus[] = {0xd0, 0x01, 0x01, 0x01};
33const uint8_t kTimingClock[] = {0xf8};
34const uint8_t kBrokenData1[] = {0x90};
35const uint8_t kBrokenData2[] = {0xf7};
36const uint8_t kBrokenData3[] = {0xf2, 0x00};
37const uint8_t kDataByte0[] = {0x00};
38
39template <typename T, size_t N>
40const std::vector<T> AsVector(const T (&data)[N]) {
41 std::vector<T> buffer;
42 buffer.insert(buffer.end(), data, data + N);
43 return buffer;
44}
45
46template <typename T, size_t N>
47void PushToVector(const T (&data)[N], std::vector<T>* buffer) {
48 buffer->insert(buffer->end(), data, data + N);
49}
50
51TEST(MidiMessageUtilTest, GetMessageLength) {
52 // Check basic functionarity
Daniel Cheng94dc01f2022-02-28 07:30:12 +000053 EXPECT_EQ(std::size(kNoteOn), GetMessageLength(kNoteOn[0]));
54 EXPECT_EQ(std::size(kChannelPressure), GetMessageLength(kChannelPressure[0]));
55 EXPECT_EQ(std::size(kTimingClock), GetMessageLength(kTimingClock[0]));
56 EXPECT_EQ(std::size(kSystemCommonMessageTuneRequest),
toyoshimbb2750c2016-10-20 05:13:24 -070057 GetMessageLength(kSystemCommonMessageTuneRequest[0]));
58
59 // SysEx message should be mapped to 0-length
60 EXPECT_EQ(0u, GetMessageLength(kGMOn[0]));
61
62 // Any reserved message should be mapped to 0-length
63 EXPECT_EQ(0u, GetMessageLength(kSystemCommonMessageReserved1[0]));
64 EXPECT_EQ(0u, GetMessageLength(kSystemCommonMessageReserved2[0]));
65
66 // Any data byte should be mapped to 0-length
67 EXPECT_EQ(0u, GetMessageLength(kGMOn[1]));
68 EXPECT_EQ(0u, GetMessageLength(kNoteOn[1]));
69 EXPECT_EQ(0u, GetMessageLength(kChannelPressure[1]));
70}
71
72TEST(MidiMessageUtilTest, IsValidWebMIDIData) {
73 // Test single event scenario
74 EXPECT_TRUE(IsValidWebMIDIData(AsVector(kGMOn)));
75 EXPECT_TRUE(IsValidWebMIDIData(AsVector(kGSOn)));
76 EXPECT_TRUE(IsValidWebMIDIData(AsVector(kNoteOn)));
77 EXPECT_TRUE(IsValidWebMIDIData(AsVector(kChannelPressure)));
78 EXPECT_TRUE(IsValidWebMIDIData(AsVector(kTimingClock)));
79 EXPECT_FALSE(IsValidWebMIDIData(AsVector(kBrokenData1)));
80 EXPECT_FALSE(IsValidWebMIDIData(AsVector(kBrokenData2)));
81 EXPECT_FALSE(IsValidWebMIDIData(AsVector(kBrokenData3)));
82 EXPECT_FALSE(IsValidWebMIDIData(AsVector(kDataByte0)));
83
84 // MIDI running status should be disallowed
85 EXPECT_FALSE(IsValidWebMIDIData(AsVector(kNoteOnWithRunningStatus)));
86 EXPECT_FALSE(IsValidWebMIDIData(AsVector(kChannelPressureWithRunningStatus)));
87
88 // Multiple messages are allowed as long as each of them is complete.
89 {
90 std::vector<uint8_t> buffer;
91 PushToVector(kGMOn, &buffer);
92 PushToVector(kNoteOn, &buffer);
93 PushToVector(kGSOn, &buffer);
94 PushToVector(kTimingClock, &buffer);
95 PushToVector(kNoteOn, &buffer);
96 EXPECT_TRUE(IsValidWebMIDIData(buffer));
97 PushToVector(kBrokenData1, &buffer);
98 EXPECT_FALSE(IsValidWebMIDIData(buffer));
99 }
100
101 // MIDI realtime message can be placed at any position.
102 EXPECT_TRUE(IsValidWebMIDIData(AsVector(kNoteOnWithRealTimeClock)));
103 EXPECT_TRUE(IsValidWebMIDIData(AsVector(kGMOnWithRealTimeClock)));
104}
105
106} // namespace
107} // namespace midi