Remove no longer needed toolbar layer method.
[chromium-blink-merge.git] / media / midi / midi_manager_unittest.cc
blob680f9db5bd1882a9aea86863cbc89875bbfe5192
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 "testing/gtest/include/gtest/gtest.h"
17 namespace media {
19 namespace {
21 class FakeMidiManager : public MidiManager {
22 public:
23 FakeMidiManager() : start_initialization_is_called_(false) {}
24 ~FakeMidiManager() override {}
26 // MidiManager implementation.
27 void StartInitialization() override {
28 start_initialization_is_called_ = true;
31 void DispatchSendMidiData(MidiManagerClient* client,
32 uint32 port_index,
33 const std::vector<uint8>& data,
34 double timestamp) override {}
36 // Utility functions for testing.
37 void CallCompleteInitialization(MidiResult result) {
38 CompleteInitialization(result);
41 size_t GetClientCount() const {
42 return clients_size_for_testing();
45 size_t GetPendingClientCount() const {
46 return pending_clients_size_for_testing();
49 bool start_initialization_is_called_;
51 private:
52 DISALLOW_COPY_AND_ASSIGN(FakeMidiManager);
55 class FakeMidiManagerClient : public MidiManagerClient {
56 public:
57 FakeMidiManagerClient()
58 : result_(MIDI_NOT_SUPPORTED),
59 wait_for_result_(true) {}
60 ~FakeMidiManagerClient() override {}
62 // MidiManagerClient implementation.
63 void AddInputPort(const MidiPortInfo& info) override {}
64 void AddOutputPort(const MidiPortInfo& info) override {}
65 void SetInputPortState(uint32 port_index, MidiPortState state) override {}
66 void SetOutputPortState(uint32 port_index, MidiPortState state) override {}
68 void CompleteStartSession(MidiResult result) override {
69 EXPECT_TRUE(wait_for_result_);
70 result_ = result;
71 wait_for_result_ = false;
74 void ReceiveMidiData(uint32 port_index,
75 const uint8* data,
76 size_t size,
77 double timestamp) override {}
78 void AccumulateMidiBytesSent(size_t size) override {}
80 MidiResult result() const { return result_; }
82 MidiResult WaitForResult() {
83 while (wait_for_result_) {
84 base::RunLoop run_loop;
85 run_loop.RunUntilIdle();
87 return result();
90 private:
91 MidiResult result_;
92 bool wait_for_result_;
94 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
97 class MidiManagerTest : public ::testing::Test {
98 public:
99 MidiManagerTest()
100 : manager_(new FakeMidiManager),
101 message_loop_(new base::MessageLoop) {}
102 ~MidiManagerTest() override {}
104 protected:
105 void StartTheFirstSession(FakeMidiManagerClient* client) {
106 EXPECT_FALSE(manager_->start_initialization_is_called_);
107 EXPECT_EQ(0U, manager_->GetClientCount());
108 EXPECT_EQ(0U, manager_->GetPendingClientCount());
109 manager_->StartSession(client);
110 EXPECT_EQ(0U, manager_->GetClientCount());
111 EXPECT_EQ(1U, manager_->GetPendingClientCount());
112 EXPECT_TRUE(manager_->start_initialization_is_called_);
113 EXPECT_EQ(0U, manager_->GetClientCount());
114 EXPECT_EQ(1U, manager_->GetPendingClientCount());
115 EXPECT_TRUE(manager_->start_initialization_is_called_);
118 void StartTheNthSession(FakeMidiManagerClient* client, size_t nth) {
119 EXPECT_EQ(nth != 1, manager_->start_initialization_is_called_);
120 EXPECT_EQ(0U, manager_->GetClientCount());
121 EXPECT_EQ(nth - 1, manager_->GetPendingClientCount());
123 // StartInitialization() should not be called for the second and later
124 // sessions.
125 manager_->start_initialization_is_called_ = false;
126 manager_->StartSession(client);
127 EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_);
128 manager_->start_initialization_is_called_ = true;
131 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) {
132 EXPECT_EQ(before, manager_->GetClientCount());
133 manager_->EndSession(client);
134 EXPECT_EQ(after, manager_->GetClientCount());
137 void CompleteInitialization(MidiResult result) {
138 manager_->CallCompleteInitialization(result);
141 void RunLoopUntilIdle() {
142 base::RunLoop run_loop;
143 run_loop.RunUntilIdle();
146 protected:
147 scoped_ptr<FakeMidiManager> manager_;
149 private:
150 scoped_ptr<base::MessageLoop> message_loop_;
152 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest);
155 TEST_F(MidiManagerTest, StartAndEndSession) {
156 scoped_ptr<FakeMidiManagerClient> client;
157 client.reset(new FakeMidiManagerClient);
159 StartTheFirstSession(client.get());
160 CompleteInitialization(MIDI_OK);
161 EXPECT_EQ(MIDI_OK, client->WaitForResult());
162 EndSession(client.get(), 1U, 0U);
165 TEST_F(MidiManagerTest, StartAndEndSessionWithError) {
166 scoped_ptr<FakeMidiManagerClient> client;
167 client.reset(new FakeMidiManagerClient);
169 StartTheFirstSession(client.get());
170 CompleteInitialization(MIDI_INITIALIZATION_ERROR);
171 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->WaitForResult());
172 EndSession(client.get(), 0U, 0U);
175 TEST_F(MidiManagerTest, StartMultipleSessions) {
176 scoped_ptr<FakeMidiManagerClient> client1;
177 scoped_ptr<FakeMidiManagerClient> client2;
178 scoped_ptr<FakeMidiManagerClient> client3;
179 client1.reset(new FakeMidiManagerClient);
180 client2.reset(new FakeMidiManagerClient);
181 client3.reset(new FakeMidiManagerClient);
183 StartTheFirstSession(client1.get());
184 StartTheNthSession(client2.get(), 2);
185 StartTheNthSession(client3.get(), 3);
186 CompleteInitialization(MIDI_OK);
187 EXPECT_EQ(MIDI_OK, client1->WaitForResult());
188 EXPECT_EQ(MIDI_OK, client2->WaitForResult());
189 EXPECT_EQ(MIDI_OK, client3->WaitForResult());
190 EndSession(client1.get(), 3U, 2U);
191 EndSession(client2.get(), 2U, 1U);
192 EndSession(client3.get(), 1U, 0U);
195 // TODO(toyoshim): Add a test for a MidiManagerClient that has multiple
196 // sessions with multiple client_id.
198 TEST_F(MidiManagerTest, TooManyPendingSessions) {
199 // Push as many client requests for starting session as possible.
200 ScopedVector<FakeMidiManagerClient> many_existing_clients;
201 many_existing_clients.resize(MidiManager::kMaxPendingClientCount);
202 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) {
203 many_existing_clients[i] = new FakeMidiManagerClient;
204 StartTheNthSession(many_existing_clients[i], i + 1);
207 // Push the last client that should be rejected for too many pending requests.
208 scoped_ptr<FakeMidiManagerClient> additional_client(
209 new FakeMidiManagerClient);
210 manager_->start_initialization_is_called_ = false;
211 manager_->StartSession(additional_client.get());
212 EXPECT_FALSE(manager_->start_initialization_is_called_);
213 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, additional_client->result());
215 // Other clients still should not receive a result.
216 RunLoopUntilIdle();
217 for (size_t i = 0; i < many_existing_clients.size(); ++i)
218 EXPECT_EQ(MIDI_NOT_SUPPORTED, many_existing_clients[i]->result());
220 // The result MIDI_OK should be distributed to other clients.
221 CompleteInitialization(MIDI_OK);
222 for (size_t i = 0; i < many_existing_clients.size(); ++i)
223 EXPECT_EQ(MIDI_OK, many_existing_clients[i]->WaitForResult());
225 // Close all successful sessions in FIFO order.
226 size_t sessions = many_existing_clients.size();
227 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions)
228 EndSession(many_existing_clients[i], sessions, sessions - 1);
231 TEST_F(MidiManagerTest, AbortSession) {
232 // A client starting a session can be destructed while an asynchronous
233 // initialization is performed.
234 scoped_ptr<FakeMidiManagerClient> client;
235 client.reset(new FakeMidiManagerClient);
237 StartTheFirstSession(client.get());
238 EndSession(client.get(), 0, 0);
239 client.reset();
241 // Following function should not call the destructed |client| function.
242 CompleteInitialization(MIDI_OK);
243 base::RunLoop run_loop;
244 run_loop.RunUntilIdle();
247 TEST_F(MidiManagerTest, CreateMidiManager) {
248 scoped_ptr<FakeMidiManagerClient> client;
249 client.reset(new FakeMidiManagerClient);
251 scoped_ptr<MidiManager> manager(MidiManager::Create());
252 manager->StartSession(client.get());
254 MidiResult result = client->WaitForResult();
255 // This #ifdef needs to be identical to the one in media/midi/midi_manager.cc.
256 // Do not change the condition for disabling this test.
257 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \
258 !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
259 EXPECT_EQ(MIDI_NOT_SUPPORTED, result);
260 #elif defined(USE_ALSA)
261 // Temporary until http://crbug.com/371230 is resolved.
262 EXPECT_TRUE((result == MIDI_OK) || (result == MIDI_INITIALIZATION_ERROR));
263 #else
264 EXPECT_EQ(MIDI_OK, result);
265 #endif
268 } // namespace
270 } // namespace media