Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1410)

Unified Diff: media/midi/usb_midi_input_stream_unittest.cc

Issue 107513012: [WebMIDI] Introduce UsbMidi{Input, Output}Stream. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@usb-midi-parser
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/midi/usb_midi_input_stream_unittest.cc
diff --git a/media/midi/usb_midi_input_stream_unittest.cc b/media/midi/usb_midi_input_stream_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..576fd715c68d4c749f177b13d1252269e4018be7
--- /dev/null
+++ b/media/midi/usb_midi_input_stream_unittest.cc
@@ -0,0 +1,231 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/midi/usb_midi_input_stream.h"
+
+#include <vector>
+
+#include "media/midi/usb_midi_device.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace media {
+
+namespace {
+
+using ::testing::InSequence;
+using ::testing::StrictMock;
+
+typedef ::testing::MockFunction<void(int)> Checkpoint; // NOLINT
+
+template<typename T, size_t N>
+std::vector<T> ToVector(const T((&array)[N])) {
+ return std::vector<T>(array, array + N);
+}
+
+class TestUsbMidiDevice : public UsbMidiDevice {
+ public:
+ TestUsbMidiDevice() {}
+ virtual ~TestUsbMidiDevice() {}
+ virtual std::vector<uint8> GetDescriptor() OVERRIDE {
+ return std::vector<uint8>();
+ }
+ virtual void Send(int endpoint_number,
+ const std::vector<uint8>& data) OVERRIDE {}
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(TestUsbMidiDevice);
+};
+
+class MockDelegate : public UsbMidiInputStream::Delegate {
+ public:
+ MockDelegate() {}
+ virtual ~MockDelegate() {}
+ virtual void OnReceivedData(size_t jack_index,
+ const uint8* data,
+ size_t size,
+ double timestamp) OVERRIDE {
+ // In order to use gmock parameter matching,
+ // we define and use OnReceiveData with std::vector<uint8>.
+ OnReceivedData(jack_index,
+ std::vector<uint8>(data, data + size),
+ timestamp);
+ }
+
+ MOCK_METHOD3(OnReceivedData, void(size_t,
+ const std::vector<uint8>&,
+ double));
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockDelegate);
+};
+
+class UsbMidiInputStreamTest : public ::testing::Test {
+ protected:
+ UsbMidiInputStreamTest()
+ : device1_(new TestUsbMidiDevice),
+ device2_(new TestUsbMidiDevice),
+ delegate_(new StrictMock<MockDelegate>) {
+ std::vector<UsbMidiJack> jacks;
+
+ jacks.push_back(UsbMidiJack(device1_.get(),
+ 84, // jack_id
+ 4, // cable_number
+ 135)); // endpoint_address
+ jacks.push_back(UsbMidiJack(device2_.get(),
+ 85,
+ 5,
+ 137));
+ jacks.push_back(UsbMidiJack(device2_.get(),
+ 84,
+ 4,
+ 135));
+ jacks.push_back(UsbMidiJack(device1_.get(),
+ 85,
+ 5,
+ 135));
+
+ stream_.reset(new UsbMidiInputStream(jacks, delegate_.get()));
+ }
+
+ scoped_ptr<TestUsbMidiDevice> device1_;
scherkus (not reviewing) 2014/01/16 21:55:52 it doesn't look like any of these need to be scope
yhirano 2014/01/20 09:12:19 Do you mean they should be objects rather than poi
scherkus (not reviewing) 2014/01/21 21:25:25 yep!
+ scoped_ptr<TestUsbMidiDevice> device2_;
+ scoped_ptr<MockDelegate> delegate_;
+ scoped_ptr<UsbMidiInputStream> stream_;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(UsbMidiInputStreamTest);
+};
+
+TEST_F(UsbMidiInputStreamTest, UnknownMessage) {
+ uint8 data[] = {
+ 0x40, 0xff, 0xff, 0xff,
+ 0x41, 0xff, 0xff, 0xff,
+ };
+ stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
+}
+
+TEST_F(UsbMidiInputStreamTest, SystemCommonMessage) {
+ uint8 data[] = {
+ 0x45, 0xf8, 0x00, 0x00,
+ 0x42, 0xf3, 0x22, 0x00,
+ 0x43, 0xf2, 0x33, 0x44,
+ };
+ uint8 expected1[] = { 0xf8, };
+ uint8 expected2[] = { 0xf3, 0x22, };
+ uint8 expected3[] = { 0xf2, 0x33, 0x44, };
+
+ {
+ InSequence s;
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0));
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0));
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0));
scherkus (not reviewing) 2014/01/16 21:55:52 These days I heavily discourage writing tests usin
yhirano 2014/01/20 09:12:19 Done.
+ }
+
+ stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
+}
+
+TEST_F(UsbMidiInputStreamTest, SystemExclusiveMessage) {
+ uint8 data[] = {
+ 0x44, 0xf0, 0x11, 0x22,
+ 0x45, 0xf7, 0x00, 0x00,
+ 0x46, 0xf0, 0xf7, 0x00,
+ 0x47, 0xf0, 0x33, 0xf7,
+ };
+ uint8 expected1[] = { 0xf0, 0x11, 0x22, };
+ uint8 expected2[] = { 0xf7, };
+ uint8 expected3[] = { 0xf0, 0xf7, };
+ uint8 expected4[] = { 0xf0, 0x33, 0xf7, };
+
+ {
+ InSequence s;
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0));
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0));
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0));
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected4), 0));
+ }
+
+ stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
+}
+
+TEST_F(UsbMidiInputStreamTest, ChannelMessage) {
+ uint8 data[] = {
+ 0x48, 0x80, 0x11, 0x22,
+ 0x49, 0x90, 0x33, 0x44,
+ 0x4a, 0xa0, 0x55, 0x66,
+ 0x4b, 0xb0, 0x77, 0x88,
+ 0x4c, 0xc0, 0x99, 0x00,
+ 0x4d, 0xd0, 0xaa, 0x00,
+ 0x4e, 0xe0, 0xbb, 0xcc,
+ };
+ uint8 expected1[] = { 0x80, 0x11, 0x22, };
+ uint8 expected2[] = { 0x90, 0x33, 0x44, };
+ uint8 expected3[] = { 0xa0, 0x55, 0x66, };
+ uint8 expected4[] = { 0xb0, 0x77, 0x88, };
+ uint8 expected5[] = { 0xc0, 0x99, };
+ uint8 expected6[] = { 0xd0, 0xaa, };
+ uint8 expected7[] = { 0xe0, 0xbb, 0xcc, };
+
+ {
+ InSequence s;
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 0));
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected2), 0));
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected3), 0));
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected4), 0));
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected5), 0));
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected6), 0));
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected7), 0));
+ }
+
+ stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
+}
+
+TEST_F(UsbMidiInputStreamTest, SingleByteMessage) {
+ uint8 data[] = {
+ 0x4f, 0xf8, 0x00, 0x00,
+ };
+
+ uint8 expected[] = { 0xf8, };
+
+ {
+ InSequence s;
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected), 0));
+ }
+
+ stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 0);
+}
+
+TEST_F(UsbMidiInputStreamTest, DispatchForMultipleCables) {
+ uint8 data[] = {
+ 0x4f, 0xf8, 0x00, 0x00,
+ 0x5f, 0xfa, 0x00, 0x00,
+ 0x6f, 0xfb, 0x00, 0x00,
+ };
+ uint8 expected1[] = { 0xf8, };
+ uint8 expected2[] = { 0xfa, };
+
+ {
+ InSequence s;
+ EXPECT_CALL(*delegate_, OnReceivedData(0, ToVector(expected1), 99));
+ EXPECT_CALL(*delegate_, OnReceivedData(3, ToVector(expected2), 99));
+ }
+
+ stream_->OnReceivedData(device1_.get(), 7, data, arraysize(data), 99);
+}
+
+TEST_F(UsbMidiInputStreamTest, DispatchForDevice2) {
+ uint8 data[] = { 0x4f, 0xf8, 0x00, 0x00 };
+ uint8 expected[] = { 0xf8, };
+
+ {
+ InSequence s;
+ EXPECT_CALL(*delegate_, OnReceivedData(2, ToVector(expected), 99));
+ }
+
+ stream_->OnReceivedData(device2_.get(), 7, data, arraysize(data), 99);
+}
+
+} // namespace
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698