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