Manage MIDI related objects and sessions' lifecycles correctly
[chromium-blink-merge.git] / media / midi / midi_manager_unittest.cc
blob4a52ca6910c4a783440adb678658bd78ab018aea
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 CompleteStartSession(MidiResult result) override {
64 EXPECT_TRUE(wait_for_result_);
65 result_ = result;
66 wait_for_result_ = false;
69 void ReceiveMidiData(uint32 port_index,
70 const uint8* data,
71 size_t size,
72 double timestamp) override {}
73 void AccumulateMidiBytesSent(size_t size) override {}
75 MidiResult result() const { return result_; }
77 MidiResult WaitForResult() {
78 while (wait_for_result_) {
79 base::RunLoop run_loop;
80 run_loop.RunUntilIdle();
82 return result();
85 private:
86 MidiResult result_;
87 bool wait_for_result_;
89 DISALLOW_COPY_AND_ASSIGN(FakeMidiManagerClient);
92 class MidiManagerTest : public ::testing::Test {
93 public:
94 MidiManagerTest()
95 : manager_(new FakeMidiManager),
96 message_loop_(new base::MessageLoop) {}
97 virtual ~MidiManagerTest() {}
99 protected:
100 void StartTheFirstSession(FakeMidiManagerClient* client) {
101 EXPECT_FALSE(manager_->start_initialization_is_called_);
102 EXPECT_EQ(0U, manager_->GetClientCount());
103 EXPECT_EQ(0U, manager_->GetPendingClientCount());
104 manager_->StartSession(client);
105 EXPECT_EQ(0U, manager_->GetClientCount());
106 EXPECT_EQ(1U, manager_->GetPendingClientCount());
107 EXPECT_TRUE(manager_->start_initialization_is_called_);
108 EXPECT_EQ(0U, manager_->GetClientCount());
109 EXPECT_EQ(1U, manager_->GetPendingClientCount());
110 EXPECT_TRUE(manager_->start_initialization_is_called_);
113 void StartTheNthSession(FakeMidiManagerClient* client, size_t nth) {
114 EXPECT_EQ(nth != 1, manager_->start_initialization_is_called_);
115 EXPECT_EQ(0U, manager_->GetClientCount());
116 EXPECT_EQ(nth - 1, manager_->GetPendingClientCount());
118 // StartInitialization() should not be called for the second and later
119 // sessions.
120 manager_->start_initialization_is_called_ = false;
121 manager_->StartSession(client);
122 EXPECT_EQ(nth == 1, manager_->start_initialization_is_called_);
123 manager_->start_initialization_is_called_ = true;
126 void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) {
127 EXPECT_EQ(before, manager_->GetClientCount());
128 manager_->EndSession(client);
129 EXPECT_EQ(after, manager_->GetClientCount());
132 void CompleteInitialization(MidiResult result) {
133 manager_->CallCompleteInitialization(result);
136 void RunLoopUntilIdle() {
137 base::RunLoop run_loop;
138 run_loop.RunUntilIdle();
141 protected:
142 scoped_ptr<FakeMidiManager> manager_;
144 private:
145 scoped_ptr<base::MessageLoop> message_loop_;
147 DISALLOW_COPY_AND_ASSIGN(MidiManagerTest);
150 TEST_F(MidiManagerTest, StartAndEndSession) {
151 scoped_ptr<FakeMidiManagerClient> client;
152 client.reset(new FakeMidiManagerClient);
154 StartTheFirstSession(client.get());
155 CompleteInitialization(MIDI_OK);
156 EXPECT_EQ(MIDI_OK, client->WaitForResult());
157 EndSession(client.get(), 1U, 0U);
160 TEST_F(MidiManagerTest, StartAndEndSessionWithError) {
161 scoped_ptr<FakeMidiManagerClient> client;
162 client.reset(new FakeMidiManagerClient);
164 StartTheFirstSession(client.get());
165 CompleteInitialization(MIDI_INITIALIZATION_ERROR);
166 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, client->WaitForResult());
167 EndSession(client.get(), 0U, 0U);
170 TEST_F(MidiManagerTest, StartMultipleSessions) {
171 scoped_ptr<FakeMidiManagerClient> client1;
172 scoped_ptr<FakeMidiManagerClient> client2;
173 scoped_ptr<FakeMidiManagerClient> client3;
174 client1.reset(new FakeMidiManagerClient);
175 client2.reset(new FakeMidiManagerClient);
176 client3.reset(new FakeMidiManagerClient);
178 StartTheFirstSession(client1.get());
179 StartTheNthSession(client2.get(), 2);
180 StartTheNthSession(client3.get(), 3);
181 CompleteInitialization(MIDI_OK);
182 EXPECT_EQ(MIDI_OK, client1->WaitForResult());
183 EXPECT_EQ(MIDI_OK, client2->WaitForResult());
184 EXPECT_EQ(MIDI_OK, client3->WaitForResult());
185 EndSession(client1.get(), 3U, 2U);
186 EndSession(client2.get(), 2U, 1U);
187 EndSession(client3.get(), 1U, 0U);
190 // TODO(toyoshim): Add a test for a MidiManagerClient that has multiple
191 // sessions with multiple client_id.
193 TEST_F(MidiManagerTest, TooManyPendingSessions) {
194 // Push as many client requests for starting session as possible.
195 ScopedVector<FakeMidiManagerClient> many_existing_clients;
196 many_existing_clients.resize(MidiManager::kMaxPendingClientCount);
197 for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) {
198 many_existing_clients[i] = new FakeMidiManagerClient;
199 StartTheNthSession(many_existing_clients[i], i + 1);
202 // Push the last client that should be rejected for too many pending requests.
203 scoped_ptr<FakeMidiManagerClient> additional_client(
204 new FakeMidiManagerClient);
205 manager_->start_initialization_is_called_ = false;
206 manager_->StartSession(additional_client.get());
207 EXPECT_FALSE(manager_->start_initialization_is_called_);
208 EXPECT_EQ(MIDI_INITIALIZATION_ERROR, additional_client->result());
210 // Other clients still should not receive a result.
211 RunLoopUntilIdle();
212 for (size_t i = 0; i < many_existing_clients.size(); ++i)
213 EXPECT_EQ(MIDI_NOT_SUPPORTED, many_existing_clients[i]->result());
215 // The result MIDI_OK should be distributed to other clients.
216 CompleteInitialization(MIDI_OK);
217 for (size_t i = 0; i < many_existing_clients.size(); ++i)
218 EXPECT_EQ(MIDI_OK, many_existing_clients[i]->WaitForResult());
220 // Close all successful sessions in FIFO order.
221 size_t sessions = many_existing_clients.size();
222 for (size_t i = 0; i < many_existing_clients.size(); ++i, --sessions)
223 EndSession(many_existing_clients[i], sessions, sessions - 1);
226 TEST_F(MidiManagerTest, AbortSession) {
227 // A client starting a session can be destructed while an asynchronous
228 // initialization is performed.
229 scoped_ptr<FakeMidiManagerClient> client;
230 client.reset(new FakeMidiManagerClient);
232 StartTheFirstSession(client.get());
233 EndSession(client.get(), 0, 0);
234 client.reset();
236 // Following function should not call the destructed |client| function.
237 CompleteInitialization(MIDI_OK);
238 base::RunLoop run_loop;
239 run_loop.RunUntilIdle();
242 TEST_F(MidiManagerTest, CreateMidiManager) {
243 scoped_ptr<FakeMidiManagerClient> client;
244 client.reset(new FakeMidiManagerClient);
246 scoped_ptr<MidiManager> manager(MidiManager::Create());
247 manager->StartSession(client.get());
249 MidiResult result = client->WaitForResult();
250 // This #ifdef needs to be identical to the one in media/midi/midi_manager.cc.
251 // Do not change the condition for disabling this test.
252 #if !defined(OS_MACOSX) && !defined(OS_WIN) && !defined(USE_ALSA) && \
253 !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
254 EXPECT_EQ(MIDI_NOT_SUPPORTED, result);
255 #elif defined(USE_ALSA)
256 // Temporary until http://crbug.com/371230 is resolved.
257 EXPECT_TRUE((result == MIDI_OK) || (result == MIDI_INITIALIZATION_ERROR));
258 #else
259 EXPECT_EQ(MIDI_OK, result);
260 #endif
263 } // namespace
265 } // namespace media