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

Side by Side Diff: content/browser/media/midi_host_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698