| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/media/midi_host.h" | 5 #include "content/browser/media/midi_host.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 14 #include "content/common/media/midi_messages.h" | 15 #include "content/common/media/midi_messages.h" |
| 15 #include "content/public/test/test_browser_thread.h" | 16 #include "content/public/test/test_browser_thread.h" |
| 16 #include "media/midi/midi_manager.h" | 17 #include "media/midi/midi_manager.h" |
| 18 #include "media/midi/midi_service.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 20 |
| 19 namespace content { | 21 namespace content { |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 22 using midi::mojom::PortState; | 24 using midi::mojom::PortState; |
| 23 | 25 |
| 24 const uint8_t kNoteOn[] = {0x90, 0x3c, 0x7f}; | 26 const uint8_t kNoteOn[] = {0x90, 0x3c, 0x7f}; |
| 25 const int kRenderProcessId = 0; | 27 const int kRenderProcessId = 0; |
| 26 | 28 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 53 events_.push_back(MidiEvent(DISPATCH_SEND_MIDI_DATA, | 55 events_.push_back(MidiEvent(DISPATCH_SEND_MIDI_DATA, |
| 54 port_index, | 56 port_index, |
| 55 data, | 57 data, |
| 56 timestamp)); | 58 timestamp)); |
| 57 } | 59 } |
| 58 std::vector<MidiEvent> events_; | 60 std::vector<MidiEvent> events_; |
| 59 }; | 61 }; |
| 60 | 62 |
| 61 class MidiHostForTesting : public MidiHost { | 63 class MidiHostForTesting : public MidiHost { |
| 62 public: | 64 public: |
| 63 MidiHostForTesting(int renderer_process_id, | 65 MidiHostForTesting(int renderer_process_id, midi::MidiService* midi_service) |
| 64 midi::MidiManager* midi_manager) | 66 : MidiHost(renderer_process_id, midi_service) {} |
| 65 : MidiHost(renderer_process_id, midi_manager) {} | |
| 66 | 67 |
| 67 private: | 68 private: |
| 68 ~MidiHostForTesting() override {} | 69 ~MidiHostForTesting() override {} |
| 69 | 70 |
| 70 // BrowserMessageFilter implementation. | 71 // BrowserMessageFilter implementation. |
| 71 // Override ShutdownForBadMessage() to do nothing since the original | 72 // Override ShutdownForBadMessage() to do nothing since the original |
| 72 // implementation to kill a malicious renderer process causes a check failure | 73 // implementation to kill a malicious renderer process causes a check failure |
| 73 // in unit tests. | 74 // in unit tests. |
| 74 void ShutdownForBadMessage() override {} | 75 void ShutdownForBadMessage() override {} |
| 75 }; | 76 }; |
| 76 | 77 |
| 77 class MidiHostTest : public testing::Test { | 78 class MidiHostTest : public testing::Test { |
| 78 public: | 79 public: |
| 79 MidiHostTest() | 80 MidiHostTest() |
| 80 : io_browser_thread_(BrowserThread::IO, &message_loop_), | 81 : io_browser_thread_(BrowserThread::IO, &message_loop_), |
| 81 host_(new MidiHostForTesting(kRenderProcessId, &manager_)), | |
| 82 data_(kNoteOn, kNoteOn + arraysize(kNoteOn)), | 82 data_(kNoteOn, kNoteOn + arraysize(kNoteOn)), |
| 83 port_id_(0) {} | 83 port_id_(0) { |
| 84 manager_ = new FakeMidiManager; |
| 85 service_.reset(new midi::MidiService(base::WrapUnique(manager_))); |
| 86 host_ = new MidiHostForTesting(kRenderProcessId, service_.get()); |
| 87 } |
| 84 ~MidiHostTest() override { | 88 ~MidiHostTest() override { |
| 85 manager_.Shutdown(); | 89 service_->Shutdown(); |
| 86 RunLoopUntilIdle(); | 90 RunLoopUntilIdle(); |
| 87 } | 91 } |
| 88 | 92 |
| 89 protected: | 93 protected: |
| 90 void AddOutputPort() { | 94 void AddOutputPort() { |
| 91 const std::string id = base::StringPrintf("i-can-%d", port_id_++); | 95 const std::string id = base::StringPrintf("i-can-%d", port_id_++); |
| 92 const std::string manufacturer("yukatan"); | 96 const std::string manufacturer("yukatan"); |
| 93 const std::string name("doki-doki-pi-pine"); | 97 const std::string name("doki-doki-pi-pine"); |
| 94 const std::string version("3.14159265359"); | 98 const std::string version("3.14159265359"); |
| 95 PortState state = PortState::CONNECTED; | 99 PortState state = PortState::CONNECTED; |
| 96 midi::MidiPortInfo info(id, manufacturer, name, version, state); | 100 midi::MidiPortInfo info(id, manufacturer, name, version, state); |
| 97 | 101 |
| 98 host_->AddOutputPort(info); | 102 host_->AddOutputPort(info); |
| 99 } | 103 } |
| 100 | 104 |
| 101 void OnSendData(uint32_t port) { | 105 void OnSendData(uint32_t port) { |
| 102 std::unique_ptr<IPC::Message> message( | 106 std::unique_ptr<IPC::Message> message( |
| 103 new MidiHostMsg_SendData(port, data_, 0.0)); | 107 new MidiHostMsg_SendData(port, data_, 0.0)); |
| 104 host_->OnMessageReceived(*message.get()); | 108 host_->OnMessageReceived(*message.get()); |
| 105 } | 109 } |
| 106 | 110 |
| 107 size_t GetEventSize() const { | 111 size_t GetEventSize() const { return manager_->events_.size(); } |
| 108 return manager_.events_.size(); | |
| 109 } | |
| 110 | 112 |
| 111 void CheckSendEventAt(size_t at, uint32_t port) { | 113 void CheckSendEventAt(size_t at, uint32_t port) { |
| 112 EXPECT_EQ(DISPATCH_SEND_MIDI_DATA, manager_.events_[at].type); | 114 EXPECT_EQ(DISPATCH_SEND_MIDI_DATA, manager_->events_[at].type); |
| 113 EXPECT_EQ(port, manager_.events_[at].port_index); | 115 EXPECT_EQ(port, manager_->events_[at].port_index); |
| 114 EXPECT_EQ(data_, manager_.events_[at].data); | 116 EXPECT_EQ(data_, manager_->events_[at].data); |
| 115 EXPECT_EQ(0.0, manager_.events_[at].timestamp); | 117 EXPECT_EQ(0.0, manager_->events_[at].timestamp); |
| 116 } | 118 } |
| 117 | 119 |
| 118 void RunLoopUntilIdle() { | 120 void RunLoopUntilIdle() { |
| 119 base::RunLoop run_loop; | 121 base::RunLoop run_loop; |
| 120 run_loop.RunUntilIdle(); | 122 run_loop.RunUntilIdle(); |
| 121 } | 123 } |
| 122 | 124 |
| 123 private: | 125 private: |
| 124 base::MessageLoop message_loop_; | 126 base::MessageLoop message_loop_; |
| 125 TestBrowserThread io_browser_thread_; | 127 TestBrowserThread io_browser_thread_; |
| 126 | 128 |
| 127 FakeMidiManager manager_; | |
| 128 scoped_refptr<MidiHostForTesting> host_; | |
| 129 std::vector<uint8_t> data_; | 129 std::vector<uint8_t> data_; |
| 130 int32_t port_id_; | 130 int32_t port_id_; |
| 131 FakeMidiManager* manager_; // Raw pointer for testing, owned by |service_|. |
| 132 std::unique_ptr<midi::MidiService> service_; |
| 133 scoped_refptr<MidiHostForTesting> host_; |
| 131 | 134 |
| 132 DISALLOW_COPY_AND_ASSIGN(MidiHostTest); | 135 DISALLOW_COPY_AND_ASSIGN(MidiHostTest); |
| 133 }; | 136 }; |
| 134 | 137 |
| 135 } // namespace | 138 } // namespace |
| 136 | 139 |
| 137 // Test if sending data to out of range port is ignored. | 140 // Test if sending data to out of range port is ignored. |
| 138 TEST_F(MidiHostTest, OutputPortCheck) { | 141 TEST_F(MidiHostTest, OutputPortCheck) { |
| 139 // Only one output port is available. | 142 // Only one output port is available. |
| 140 AddOutputPort(); | 143 AddOutputPort(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 158 // Sending data to port 0 and 1 should be delivered now. | 161 // Sending data to port 0 and 1 should be delivered now. |
| 159 OnSendData(port0); | 162 OnSendData(port0); |
| 160 OnSendData(port1); | 163 OnSendData(port1); |
| 161 RunLoopUntilIdle(); | 164 RunLoopUntilIdle(); |
| 162 EXPECT_EQ(3U, GetEventSize()); | 165 EXPECT_EQ(3U, GetEventSize()); |
| 163 CheckSendEventAt(1, port0); | 166 CheckSendEventAt(1, port0); |
| 164 CheckSendEventAt(2, port1); | 167 CheckSendEventAt(2, port1); |
| 165 } | 168 } |
| 166 | 169 |
| 167 } // namespace conent | 170 } // namespace conent |
| OLD | NEW |