Update V8 to version 4.7.42.
[chromium-blink-merge.git] / media / midi / midi_manager_unittest.cc
blob2b56780e22e793fea044a223692ba74ad13b23f0
1 // Copyright 2014 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.
5 #include "media/midi/midi_manager.h"
7 #include <vector>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/run_loop.h"
15 #include "base/system_monitor/system_monitor.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 namespace media {
19 namespace midi {
21 namespace {
23 class FakeMidiManager : public MidiManager {
24 public:
25 FakeMidiManager() : start_initialization_is_called_(false) {}
26 ~FakeMidiManager() override {}
28 // MidiManager implementation.
29 void StartInitialization() override {
30 start_initialization_is_called_ = true;
33 void DispatchSendMidiData(MidiManagerClient* client,
34 uint32 port_index,
35 const std::vector<uint8>& data,
36 double timestamp) override {}
38 // Utility functions for testing.
39 void CallCompleteInitialization(Result result) {
40 CompleteInitialization(result);
43 size_t GetClientCount() const {
44 return clients_size_for_testing();
47 size_t GetPendingClientCount() const {
48 return pending_clients_size_for_testing();
51 bool start_initialization_is_called_;
53 private:
54 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager);
57 class FakeMidiManagerClient : public MidiManagerClient {
58 public:
59 FakeMidiManagerClient()
60 : result_(Result::NOT_SUPPORTED), wait_for_result_(true) {}
61 ~FakeMidiManagerClient() override {}
63 // MidiManagerClient implementation.
64 void AddInputPort(const MidiPortInfo& info) override {}
65 void AddOutputPort(const MidiPortInfo& info) override {}
66 void SetInputPortState(uint32 port_index, MidiPortState state) override {}
67 void SetOutputPortState(uint32 port_index, MidiPortState state) override {}
69 void CompleteStartSession(Result result) override {
70 EXPECT_TRUE(wait_for_result_);
71 result_ = result;
72 wait_for_result_ = false;
75 void ReceiveMidiData(uint32 port_index,
76 const uint8* data,
77 size_t size,
78 double timestamp) override {}
79 void AccumulateMidiBytesSent(size_t size) override {}
81 Result result() const { return result_; }
83 Result WaitForResult() {
84 while (wait_for_result_) {
85 base::RunLoop run_loop;
86 run_loop.RunUntilIdle();
88 return result();
91 private:
92 Result result_;
93 bool wait_for_result_;
95 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
98 class MidiManagerTest : public ::testing::Test {
99 public:
100 MidiManagerTest()
101 : manager_(new FakeMidiManager),
102 message_loop_(new base::MessageLoop) {}
103 ~MidiManagerTest() override {}
105 protected:
106 void StartTheFirstSession(FakeMidiManagerClient* client) {
107 EXPECT_FALSE(manager_->start_initialization_is_called_);
108 EXPECT_EQ(0U, manager_->GetClientCount());
109 EXPECT_EQ(0U, manager_->GetPendingClientCount());
110 manager_->StartSession(client);
111 EXPECT_EQ(0U, manager_->GetClientCount());
112 EXPECT_EQ(1U, manager_->GetPendingClientCount());
113 EXPECT_TRUE(manager_->start_initialization_is_called_);
114 EXPECT_EQ(0U, manager_->GetClientCount());
115 EXPECT_EQ(1U, manager_->GetPendingClientCount());
116 EXPECT_TRUE(manager_->start_initialization_is_called_);
119 void StartTheNthSession(FakeMidiManagerClient* client, size_t nth) {
120 EXPECT_EQ(nth != 1, manager_->start_initialization_is_called_);
121 EXPECT_EQ(0U, manager_->GetClientCount());
122 EXPECT_EQ(nth - 1, manager_->GetPendingClientCount());
124 // StartInitialization() should not be called for the second and later
125 // sessions.
126 manager_->start_initialization_is_called_ = false;
127 manager_->StartSession(client);
128 EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_);
129 manager_->start_initialization_is_called_ = true;
132 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) {
133 EXPECT_EQ(before, manager_->GetClientCount());
134 manager_->EndSession(client);
135 EXPECT_EQ(after, manager_->GetClientCount());
138 void CompleteInitialization(Result result) {
139 manager_->CallCompleteInitialization(result);
142 void RunLoopUntilIdle() {
143 base::RunLoop run_loop;
144 run_loop.RunUntilIdle();
147 protected:
148 scoped_ptr<FakeMidiManager> manager_;
150 private:
151 scoped_ptr<base::MessageLoop> message_loop_;
153 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest);
156 TEST_F(MidiManagerTest, StartAndEndSession) {
157 scoped_ptr<FakeMidiManagerClient> client;
158 client.reset(new FakeMidiManagerClient);
160 StartTheFirstSession(client.get());
161 CompleteInitialization(Result::OK);
162 EXPECT_EQ(Result::OK, client->WaitForResult());
163 EndSession(client.get(), 1U, 0U);
166 TEST_F(MidiManagerTest, StartAndEndSessionWithError) {
167 scoped_ptr<FakeMidiManagerClient> client;
168 client.reset(new FakeMidiManagerClient);
170 StartTheFirstSession(client.get());
171 CompleteInitialization(Result::INITIALIZATION_ERROR);
172 EXPECT_EQ(Result::INITIALIZATION_ERROR, client->WaitForResult());
173 EndSession(client.get(), 0U, 0U);
176 TEST_F(MidiManagerTest, StartMultipleSessions) {
177 scoped_ptr<FakeMidiManagerClient> client1;
178 scoped_ptr<FakeMidiManagerClient> client2;
179 scoped_ptr<FakeMidiManagerClient> client3;
180 client1.reset(new FakeMidiManagerClient);
181 client2.reset(new FakeMidiManagerClient);
182 client3.reset(new FakeMidiManagerClient);
184 StartTheFirstSession(client1.get());
185 StartTheNthSession(client2.get(), 2);
186 StartTheNthSession(client3.get(), 3);
187 CompleteInitialization(Result::OK);
188 EXPECT_EQ(Result::OK, client1->WaitForResult());
189 EXPECT_EQ(Result::OK, client2->WaitForResult());
190 EXPECT_EQ(Result::OK, client3->WaitForResult());
191 EndSession(client1.get(), 3U, 2U);
192 EndSession(client2.get(), 2U, 1U);
193 EndSession(client3.get(), 1U, 0U);
196 // TODO(toyoshim): Add a test for a MidiManagerClient that has multiple
197 // sessions with multiple client_id.
199 TEST_F(MidiManagerTest, TooManyPendingSessions) {
200 // Push as many client requests for starting session as possible.
201 ScopedVector<FakeMidiManagerClient> many_existing_clients;
202 many_existing_clients.resize(MidiManager::kMaxPendingClientCount);
203 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) {
204 many_existing_clients[i] = new FakeMidiManagerClient;
205 StartTheNthSession(many_existing_clients[i], i + 1);
208 // Push the last client that should be rejected for too many pending requests.
209 scoped_ptr<FakeMidiManagerClient> additional_client(
210 new FakeMidiManagerClient);
211 manager_->start_initialization_is_called_ = false;
212 manager_->StartSession(additional_client.get());
213 EXPECT_FALSE(manager_->start_initialization_is_called_);
214 EXPECT_EQ(Result::INITIALIZATION_ERROR, additional_client->result());
216 // Other clients still should not receive a result.
217 RunLoopUntilIdle();
218 for (size_t i = 0; i < many_existing_clients.size(); ++i)
219 EXPECT_EQ(Result::NOT_SUPPORTED, many_existing_clients[i]->result());
221 // The Result::OK should be distributed to other clients.
222 CompleteInitialization(Result::OK);
223 for (size_t i = 0; i < many_existing_clients.size(); ++i)
224 EXPECT_EQ(Result::OK, many_existing_clients[i]->WaitForResult());
226 // Close all successful sessions in FIFO order.
227 size_t sessions = many_existing_clients.size();
228 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions)
229 EndSession(many_existing_clients[i], sessions, sessions - 1);
232 TEST_F(MidiManagerTest, AbortSession) {
233 // A client starting a session can be destructed while an asynchronous
234 // initialization is performed.
235 scoped_ptr<FakeMidiManagerClient> client;
236 client.reset(new FakeMidiManagerClient);
238 StartTheFirstSession(client.get());
239 EndSession(client.get(), 0, 0);
240 client.reset();
242 // Following function should not call the destructed |client| function.
243 CompleteInitialization(Result::OK);
244 base::RunLoop run_loop;
245 run_loop.RunUntilIdle();
248 TEST_F(MidiManagerTest, CreateMidiManager) {
249 // SystemMonitor is needed on Windows.
250 base::SystemMonitor system_monitor;
252 scoped_ptr<FakeMidiManagerClient> client;
253 client.reset(new FakeMidiManagerClient);
255 scoped_ptr<MidiManager> manager(MidiManager::Create());
256 manager->StartSession(client.get());
258 Result result = client->WaitForResult();
259 // This #ifdef needs to be identical to the one in media/midi/midi_manager.cc.
260 // Do not change the condition for disabling this test.
261 #if !defined(OS_MACOSX) && !defined(OS_WIN) && \
262 !(defined(USE_ALSA) && defined(USE_UDEV)) && !defined(OS_ANDROID)
263 EXPECT_EQ(Result::NOT_SUPPORTED, result);
264 #elif defined(USE_ALSA)
265 // Temporary until http://crbug.com/371230 is resolved.
266 EXPECT_TRUE(result == Result::OK || result == Result::INITIALIZATION_ERROR);
267 #else
268 EXPECT_EQ(Result::OK, result);
269 #endif
272 } // namespace
274 } // namespace midi
275 } // namespace media