message_util_unittest.cc 3.9 KB

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