1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
media / midi / midi_manager_unittest.cc [blame]
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/midi/midi_manager.h"
#include <stddef.h>
#include <stdint.h>
#include <array>
#include <memory>
#include <vector>
#include "base/check_op.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/weak_ptr.h"
#include "base/system/system_monitor.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "build/build_config.h"
#include "media/midi/midi_service.h"
#include "media/midi/task_service.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_WIN)
#include "media/midi/midi_manager_win.h"
#endif // BUILDFLAG(IS_WIN)
namespace midi {
namespace {
using mojom::PortState;
using mojom::Result;
class FakeMidiManager : public MidiManager {
public:
explicit FakeMidiManager(MidiService* service) : MidiManager(service) {}
FakeMidiManager(const FakeMidiManager&) = delete;
FakeMidiManager& operator=(const FakeMidiManager&) = delete;
~FakeMidiManager() override = default;
base::WeakPtr<FakeMidiManager> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
// MidiManager implementation.
void StartInitialization() override {
DCHECK(!initialized_);
initialized_ = true;
}
void DispatchSendMidiData(MidiManagerClient* client,
uint32_t port_index,
const std::vector<uint8_t>& data,
base::TimeTicks timestamp) override {}
// Utility functions for testing.
void CallCompleteInitialization(Result result) {
CompleteInitialization(result);
}
size_t GetClientCount() { return GetClientCountForTesting(); }
size_t GetPendingClientCount() { return GetPendingClientCountForTesting(); }
bool IsInitialized() const { return initialized_; }
private:
bool initialized_ = false;
base::WeakPtrFactory<FakeMidiManager> weak_factory_{this};
};
class FakeMidiManagerFactory : public MidiService::ManagerFactory {
public:
FakeMidiManagerFactory() {}
FakeMidiManagerFactory(const FakeMidiManagerFactory&) = delete;
FakeMidiManagerFactory& operator=(const FakeMidiManagerFactory&) = delete;
~FakeMidiManagerFactory() override = default;
std::unique_ptr<MidiManager> Create(MidiService* service) override {
std::unique_ptr<FakeMidiManager> manager =
std::make_unique<FakeMidiManager>(service);
manager_ = manager->GetWeakPtr();
return manager;
}
base::WeakPtr<FakeMidiManagerFactory> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
base::WeakPtr<FakeMidiManager> manager() {
#if BUILDFLAG(IS_MAC)
// To avoid Core MIDI issues, MidiManager won't be destructed on macOS.
// See https://crbug.com/718140.
if (!manager_ ||
(!manager_->GetClientCount() && !manager_->GetPendingClientCount())) {
return nullptr;
}
#endif
return manager_;
}
private:
base::WeakPtr<FakeMidiManager> manager_ = nullptr;
base::WeakPtrFactory<FakeMidiManagerFactory> weak_factory_{this};
};
class FakeMidiManagerClient : public MidiManagerClient {
public:
explicit FakeMidiManagerClient(base::OnceClosure on_session_start_cb)
: on_session_start_cb_(std::move(on_session_start_cb)) {}
FakeMidiManagerClient(const FakeMidiManagerClient&) = delete;
FakeMidiManagerClient& operator=(const FakeMidiManagerClient&) = delete;
~FakeMidiManagerClient() override = default;
// MidiManagerClient implementation.
void AddInputPort(const mojom::PortInfo& info) override {}
void AddOutputPort(const mojom::PortInfo& info) override {}
void SetInputPortState(uint32_t port_index, PortState state) override {}
void SetOutputPortState(uint32_t port_index, PortState state) override {}
void CompleteStartSession(Result result) override {
CHECK(on_session_start_cb_);
result_ = result;
std::move(on_session_start_cb_).Run();
}
void ReceiveMidiData(uint32_t port_index,
const uint8_t* data,
size_t size,
base::TimeTicks timestamp) override {}
void AccumulateMidiBytesSent(size_t size) override {}
void Detach() override {}
Result result() const { return result_; }
private:
Result result_ = Result::NOT_SUPPORTED;
base::OnceClosure on_session_start_cb_;
};
class MidiManagerTest : public ::testing::Test {
public:
MidiManagerTest() {
std::unique_ptr<FakeMidiManagerFactory> factory =
std::make_unique<FakeMidiManagerFactory>();
factory_ = factory->GetWeakPtr();
service_ = std::make_unique<MidiService>(std::move(factory));
}
MidiManagerTest(const MidiManagerTest&) = delete;
MidiManagerTest& operator=(const MidiManagerTest&) = delete;
~MidiManagerTest() override {
service_->Shutdown();
env_.RunUntilIdle();
}
protected:
void StartTheFirstSession(FakeMidiManagerClient* client) {
DCHECK(factory_);
EXPECT_FALSE(factory_->manager());
service_->StartSession(client);
ASSERT_TRUE(factory_->manager());
EXPECT_TRUE(factory_->manager()->IsInitialized());
EXPECT_EQ(0U, factory_->manager()->GetClientCount());
EXPECT_EQ(1U, factory_->manager()->GetPendingClientCount());
}
void StartTheNthSession(FakeMidiManagerClient* client, size_t nth) {
DCHECK(factory_);
DCHECK_NE(1U, nth);
ASSERT_TRUE(factory_->manager());
EXPECT_TRUE(factory_->manager()->IsInitialized());
EXPECT_EQ(0U, factory_->manager()->GetClientCount());
EXPECT_EQ(nth - 1U, factory_->manager()->GetPendingClientCount());
service_->StartSession(client);
EXPECT_EQ(nth, factory_->manager()->GetPendingClientCount());
}
void StartSession(FakeMidiManagerClient* client) {
service_->StartSession(client);
}
void EndSession(FakeMidiManagerClient* client, size_t before, size_t after) {
DCHECK(factory_);
ASSERT_TRUE(factory_->manager());
EXPECT_EQ(before, factory_->manager()->GetClientCount());
EXPECT_TRUE(service_->EndSession(client));
if (after) {
ASSERT_TRUE(factory_->manager());
EXPECT_EQ(after, factory_->manager()->GetClientCount());
} else {
EXPECT_FALSE(factory_->manager());
}
}
bool CompleteInitialization(Result result) {
DCHECK(factory_);
if (!factory_->manager())
return false;
factory_->manager()->CallCompleteInitialization(result);
return true;
}
base::WeakPtr<FakeMidiManagerFactory> factory() { return factory_; }
private:
base::test::TaskEnvironment env_;
base::WeakPtr<FakeMidiManagerFactory> factory_;
std::unique_ptr<MidiService> service_;
};
TEST_F(MidiManagerTest, StartAndEndSession) {
base::test::TestFuture<void> test_future;
std::unique_ptr<FakeMidiManagerClient> client =
std::make_unique<FakeMidiManagerClient>(test_future.GetCallback());
StartTheFirstSession(client.get());
EXPECT_TRUE(CompleteInitialization(Result::OK));
ASSERT_TRUE(test_future.Wait());
EXPECT_EQ(Result::OK, client->result());
EndSession(client.get(), 1U, 0U);
}
TEST_F(MidiManagerTest, StartAndEndSessionWithError) {
base::test::TestFuture<void> test_future;
std::unique_ptr<FakeMidiManagerClient> client =
std::make_unique<FakeMidiManagerClient>(test_future.GetCallback());
StartTheFirstSession(client.get());
EXPECT_TRUE(CompleteInitialization(Result::INITIALIZATION_ERROR));
ASSERT_TRUE(test_future.Wait());
EXPECT_EQ(Result::INITIALIZATION_ERROR, client->result());
EndSession(client.get(), 1U, 0U);
}
TEST_F(MidiManagerTest, StartMultipleSessions) {
base::test::TestFuture<void> future1;
std::unique_ptr<FakeMidiManagerClient> client1 =
std::make_unique<FakeMidiManagerClient>(future1.GetCallback());
base::test::TestFuture<void> future2;
std::unique_ptr<FakeMidiManagerClient> client2 =
std::make_unique<FakeMidiManagerClient>(future2.GetCallback());
base::test::TestFuture<void> future3;
std::unique_ptr<FakeMidiManagerClient> client3 =
std::make_unique<FakeMidiManagerClient>(future3.GetCallback());
StartTheFirstSession(client1.get());
StartTheNthSession(client2.get(), 2);
StartTheNthSession(client3.get(), 3);
EXPECT_TRUE(CompleteInitialization(Result::OK));
ASSERT_TRUE(future1.Wait());
EXPECT_EQ(Result::OK, client1->result());
ASSERT_TRUE(future2.Wait());
EXPECT_EQ(Result::OK, client2->result());
ASSERT_TRUE(future3.Wait());
EXPECT_EQ(Result::OK, client3->result());
EndSession(client1.get(), 3U, 2U);
EndSession(client2.get(), 2U, 1U);
EndSession(client3.get(), 1U, 0U);
}
TEST_F(MidiManagerTest, TooManyPendingSessions) {
// Push as many client requests for starting session as possible.
std::array<std::unique_ptr<base::test::TestFuture<void>>,
MidiManager::kMaxPendingClientCount>
test_futures;
std::array<std::unique_ptr<FakeMidiManagerClient>,
MidiManager::kMaxPendingClientCount>
many_existing_clients;
test_futures[0] = std::make_unique<base::test::TestFuture<void>>();
many_existing_clients[0] =
std::make_unique<FakeMidiManagerClient>(test_futures[0]->GetCallback());
StartTheFirstSession(many_existing_clients[0].get());
for (size_t i = 1; i < MidiManager::kMaxPendingClientCount; ++i) {
test_futures[i] = std::make_unique<base::test::TestFuture<void>>();
many_existing_clients[i] =
std::make_unique<FakeMidiManagerClient>(test_futures[i]->GetCallback());
StartTheNthSession(many_existing_clients[i].get(), i + 1);
}
ASSERT_TRUE(factory()->manager());
EXPECT_TRUE(factory()->manager()->IsInitialized());
// Push the last client that should be rejected for too many pending requests.
std::unique_ptr<FakeMidiManagerClient> additional_client =
std::make_unique<FakeMidiManagerClient>(base::DoNothing());
StartSession(additional_client.get());
EXPECT_EQ(Result::INITIALIZATION_ERROR, additional_client->result());
// Other clients still should not receive a result.
for (const auto& existing_client : many_existing_clients) {
EXPECT_EQ(Result::NOT_SUPPORTED, existing_client->result());
}
// The Result::OK should be distributed to other clients.
EXPECT_TRUE(CompleteInitialization(Result::OK));
for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i) {
ASSERT_TRUE(test_futures[i]->Wait());
EXPECT_EQ(Result::OK, many_existing_clients[i]->result());
}
// Close all successful sessions in FIFO order.
size_t sessions = MidiManager::kMaxPendingClientCount;
for (size_t i = 0; i < MidiManager::kMaxPendingClientCount; ++i, --sessions) {
EndSession(many_existing_clients[i].get(), sessions, sessions - 1);
}
}
TEST_F(MidiManagerTest, AbortSession) {
// A client starting a session can be destructed while an asynchronous
// initialization is performed.
base::test::TestFuture<void> test_future;
std::unique_ptr<FakeMidiManagerClient> client =
std::make_unique<FakeMidiManagerClient>(test_future.GetCallback());
StartTheFirstSession(client.get());
EndSession(client.get(), 0, 0);
client.reset();
// Following function should not call the destructed |client| function.
EXPECT_FALSE(CompleteInitialization(Result::OK));
EXPECT_FALSE(test_future.IsReady());
}
class PlatformMidiManagerTest : public ::testing::Test {
public:
PlatformMidiManagerTest()
: client_(std::make_unique<FakeMidiManagerClient>(
test_future_.GetCallback())),
service_(std::make_unique<MidiService>()) {}
PlatformMidiManagerTest(const PlatformMidiManagerTest&) = delete;
PlatformMidiManagerTest& operator=(const PlatformMidiManagerTest&) = delete;
~PlatformMidiManagerTest() override {
service_->Shutdown();
env_.RunUntilIdle();
}
MidiService* service() { return service_.get(); }
FakeMidiManagerClient* client() { return client_.get(); }
base::test::TestFuture<void>* future() { return &test_future_; }
void StartSession() { service_->StartSession(client_.get()); }
void EndSession() { service_->EndSession(client_.get()); }
// This #ifdef needs to be identical to the one in media/midi/midi_manager.cc.
// Do not change the condition for disabling this test.
bool IsSupported() {
#if !BUILDFLAG(IS_APPLE) && !BUILDFLAG(IS_WIN) && \
!(defined(USE_ALSA) && defined(USE_UDEV)) && !BUILDFLAG(IS_ANDROID)
return false;
#else
return true;
#endif
}
private:
// SystemMonitor is needed on Windows.
base::SystemMonitor system_monitor;
base::test::TaskEnvironment env_;
base::test::TestFuture<void> test_future_;
std::unique_ptr<FakeMidiManagerClient> client_;
std::unique_ptr<MidiService> service_;
};
#if BUILDFLAG(IS_ANDROID)
// The test sometimes fails on Android. https://crbug.com/844027
#define MAYBE_CreatePlatformMidiManager DISABLED_CreatePlatformMidiManager
#else
#define MAYBE_CreatePlatformMidiManager CreatePlatformMidiManager
#endif
TEST_F(PlatformMidiManagerTest, MAYBE_CreatePlatformMidiManager) {
StartSession();
ASSERT_TRUE(future()->Wait());
Result result = client()->result();
#if defined(USE_ALSA)
// Temporary until http://crbug.com/371230 is resolved.
EXPECT_TRUE(result == Result::OK || result == Result::INITIALIZATION_ERROR);
#else
EXPECT_EQ(IsSupported() ? Result::OK : Result::NOT_SUPPORTED, result);
#endif
}
TEST_F(PlatformMidiManagerTest, InstanceIdOverflow) {
service()->task_service()->OverflowInstanceIdForTesting();
#if BUILDFLAG(IS_WIN)
MidiManagerWin::OverflowInstanceIdForTesting();
#endif // BUILDFLAG(IS_WIN)
StartSession();
ASSERT_TRUE(future()->Wait());
EXPECT_EQ(
IsSupported() ? Result::INITIALIZATION_ERROR : Result::NOT_SUPPORTED,
client()->result());
EndSession();
}
} // namespace
} // namespace midi