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

Side by Side Diff: device/bluetooth/bluez/bluetooth_audio_sink_bluez_unittest.cc

Issue 2389753003: Delete unused BluetoothAudioSink code. (Closed)
Patch Set: Created 4 years, 2 months 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
(Empty)
1 // Copyright 2015 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.
4
5 #include "device/bluetooth/bluez/bluetooth_audio_sink_bluez.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9
10 #include <memory>
11 #include <vector>
12
13 #include "base/bind.h"
14 #include "base/logging.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/run_loop.h"
18 #include "dbus/object_path.h"
19 #include "device/bluetooth/bluetooth_adapter.h"
20 #include "device/bluetooth/bluetooth_adapter_factory.h"
21 #include "device/bluetooth/bluetooth_audio_sink.h"
22 #include "device/bluetooth/dbus/bluetooth_media_client.h"
23 #include "device/bluetooth/dbus/bluetooth_media_endpoint_service_provider.h"
24 #include "device/bluetooth/dbus/bluetooth_media_transport_client.h"
25 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
26 #include "device/bluetooth/dbus/fake_bluetooth_media_client.h"
27 #include "device/bluetooth/dbus/fake_bluetooth_media_endpoint_service_provider.h "
28 #include "device/bluetooth/dbus/fake_bluetooth_media_transport_client.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30
31 using bluez::FakeBluetoothMediaTransportClient;
32 using dbus::ObjectPath;
33 using device::BluetoothAdapter;
34 using device::BluetoothAdapterFactory;
35 using device::BluetoothAudioSink;
36
37 namespace bluez {
38
39 class TestAudioSinkObserver : public BluetoothAudioSink::Observer {
40 public:
41 explicit TestAudioSinkObserver(scoped_refptr<BluetoothAudioSink> audio_sink)
42 : state_changed_count_(0),
43 volume_changed_count_(0),
44 total_read_(0),
45 state_(audio_sink->GetState()),
46 audio_sink_(audio_sink) {
47 audio_sink_->AddObserver(this);
48 }
49
50 ~TestAudioSinkObserver() override { audio_sink_->RemoveObserver(this); }
51
52 void BluetoothAudioSinkStateChanged(
53 BluetoothAudioSink* audio_sink,
54 BluetoothAudioSink::State state) override {
55 if (state == BluetoothAudioSink::STATE_IDLE)
56 total_read_ = 0;
57
58 ++state_changed_count_;
59 }
60
61 void BluetoothAudioSinkVolumeChanged(BluetoothAudioSink* audio_sink,
62 uint16_t volume) override {
63 ++volume_changed_count_;
64 }
65
66 void BluetoothAudioSinkDataAvailable(BluetoothAudioSink* audio_sink,
67 char* data,
68 size_t size,
69 uint16_t read_mtu) override {
70 total_read_ += size;
71 data_.clear();
72 data_.insert(data_.begin(), data, data + size);
73 read_mtu_ = read_mtu;
74 }
75
76 int state_changed_count_;
77 int volume_changed_count_;
78 int data_available_count_;
79 size_t total_read_;
80 std::vector<char> data_;
81 uint16_t read_mtu_;
82 BluetoothAudioSink::State state_;
83
84 private:
85 scoped_refptr<BluetoothAudioSink> audio_sink_;
86 };
87
88 class BluetoothAudioSinkBlueZTest : public testing::Test {
89 public:
90 void SetUp() override {
91 bluez::BluezDBusManager::Initialize(nullptr /* bus */,
92 true /* use_dbus_stub */);
93
94 callback_count_ = 0;
95 error_callback_count_ = 0;
96
97 fake_media_ = static_cast<bluez::FakeBluetoothMediaClient*>(
98 bluez::BluezDBusManager::Get()->GetBluetoothMediaClient());
99 fake_transport_ = static_cast<FakeBluetoothMediaTransportClient*>(
100 bluez::BluezDBusManager::Get()->GetBluetoothMediaTransportClient());
101
102 // Initiates Delegate::TransportProperties with default values.
103 properties_.device =
104 ObjectPath(FakeBluetoothMediaTransportClient::kTransportDevicePath);
105 properties_.uuid = bluez::BluetoothMediaClient::kBluetoothAudioSinkUUID;
106 properties_.codec = FakeBluetoothMediaTransportClient::kTransportCodec;
107 properties_.configuration = std::vector<uint8_t>(
108 FakeBluetoothMediaTransportClient::kTransportConfiguration,
109 FakeBluetoothMediaTransportClient::kTransportConfiguration +
110 FakeBluetoothMediaTransportClient::kTransportConfigurationLength);
111 properties_.state = bluez::BluetoothMediaTransportClient::kStateIdle;
112 properties_.delay.reset(
113 new uint16_t(FakeBluetoothMediaTransportClient::kTransportDelay));
114 properties_.volume.reset(
115 new uint16_t(FakeBluetoothMediaTransportClient::kTransportVolume));
116
117 GetAdapter();
118 }
119
120 void TearDown() override {
121 callback_count_ = 0;
122 error_callback_count_ = 0;
123 observer_.reset();
124
125 fake_media_->SetVisible(true);
126
127 // The adapter should outlive audio sink.
128 audio_sink_ = nullptr;
129 adapter_ = nullptr;
130 bluez::BluezDBusManager::Shutdown();
131 }
132
133 // Gets the existing Bluetooth adapter.
134 void GetAdapter() {
135 BluetoothAdapterFactory::GetAdapter(
136 base::Bind(&BluetoothAudioSinkBlueZTest::GetAdapterCallback,
137 base::Unretained(this)));
138 base::RunLoop().Run();
139 }
140
141 // Called whenever BluetoothAdapter is retrieved successfully.
142 void GetAdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
143 adapter_ = adapter;
144
145 ASSERT_NE(adapter_.get(), nullptr);
146 ASSERT_TRUE(adapter_->IsInitialized());
147 adapter_->SetPowered(true,
148 base::Bind(&BluetoothAudioSinkBlueZTest::Callback,
149 base::Unretained(this)),
150 base::Bind(&BluetoothAudioSinkBlueZTest::ErrorCallback,
151 base::Unretained(this)));
152 ASSERT_TRUE(adapter_->IsPresent());
153 ASSERT_TRUE(adapter_->IsPowered());
154 EXPECT_EQ(callback_count_, 1);
155 EXPECT_EQ(error_callback_count_, 0);
156
157 // Resets callback_count_.
158 --callback_count_;
159
160 if (base::MessageLoop::current() &&
161 base::MessageLoop::current()->is_running()) {
162 base::MessageLoop::current()->QuitWhenIdle();
163 }
164 }
165
166 // Registers BluetoothAudioSinkBlueZ with default codec and capabilities.
167 // If the audio sink is retrieved successfully, the state changes to
168 // STATE_DISCONNECTED.
169 void GetAudioSink() {
170 // Sets up valid codec and capabilities.
171 BluetoothAudioSink::Options options;
172 ASSERT_EQ(options.codec, 0x00);
173 ASSERT_EQ(options.capabilities,
174 std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35}));
175
176 // Registers |audio_sink_| with valid codec and capabilities
177 adapter_->RegisterAudioSink(
178 options, base::Bind(&BluetoothAudioSinkBlueZTest::RegisterCallback,
179 base::Unretained(this)),
180 base::Bind(&BluetoothAudioSinkBlueZTest::RegisterErrorCallback,
181 base::Unretained(this)));
182
183 observer_.reset(new TestAudioSinkObserver(audio_sink_));
184 EXPECT_EQ(callback_count_, 1);
185 EXPECT_EQ(error_callback_count_, 0);
186 EXPECT_EQ(observer_->state_changed_count_, 0);
187 EXPECT_EQ(observer_->volume_changed_count_, 0);
188 }
189
190 void GetFakeMediaEndpoint() {
191 BluetoothAudioSinkBlueZ* audio_sink_bluez =
192 static_cast<BluetoothAudioSinkBlueZ*>(audio_sink_.get());
193 ASSERT_NE(audio_sink_bluez, nullptr);
194
195 media_endpoint_ =
196 static_cast<bluez::FakeBluetoothMediaEndpointServiceProvider*>(
197 audio_sink_bluez->GetEndpointServiceProvider());
198 }
199
200 // Called whenever RegisterAudioSink is completed successfully.
201 void RegisterCallback(scoped_refptr<BluetoothAudioSink> audio_sink) {
202 ++callback_count_;
203 audio_sink_ = audio_sink;
204
205 GetFakeMediaEndpoint();
206 ASSERT_NE(media_endpoint_, nullptr);
207 fake_media_->SetEndpointRegistered(media_endpoint_, true);
208
209 ASSERT_NE(audio_sink_.get(), nullptr);
210 ASSERT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
211 }
212
213 // Called whenever RegisterAudioSink failed.
214 void RegisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) {
215 ++error_callback_count_;
216 EXPECT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_REGISTERED);
217 }
218
219 // Called whenever there capabilities are returned from SelectConfiguration.
220 void SelectConfigurationCallback(const std::vector<uint8_t>& capabilities) {
221 ++callback_count_;
222
223 // |capabilities| should be the same as the capabilities for registering an
224 // audio sink in GetAudioSink().
225 EXPECT_EQ(capabilities, std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35}));
226 }
227
228 void UnregisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) {
229 ++error_callback_count_;
230 EXPECT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_UNREGISTERED);
231 }
232
233 // Generic callbacks.
234 void Callback() { ++callback_count_; }
235
236 void ErrorCallback() { ++error_callback_count_; }
237
238 protected:
239 int callback_count_;
240 int error_callback_count_;
241
242 base::MessageLoopForIO message_loop_;
243
244 bluez::FakeBluetoothMediaClient* fake_media_;
245 FakeBluetoothMediaTransportClient* fake_transport_;
246 bluez::FakeBluetoothMediaEndpointServiceProvider* media_endpoint_;
247 std::unique_ptr<TestAudioSinkObserver> observer_;
248 scoped_refptr<BluetoothAdapter> adapter_;
249 scoped_refptr<BluetoothAudioSink> audio_sink_;
250
251 // The default property set used while calling SetConfiguration on a media
252 // endpoint object.
253 bluez::BluetoothMediaEndpointServiceProvider::Delegate::TransportProperties
254 properties_;
255 };
256
257 TEST_F(BluetoothAudioSinkBlueZTest, RegisterSucceeded) {
258 GetAudioSink();
259 }
260
261 TEST_F(BluetoothAudioSinkBlueZTest, RegisterFailedWithInvalidOptions) {
262 // Sets options with an invalid codec and valid capabilities.
263 BluetoothAudioSink::Options options;
264 options.codec = 0xff;
265 options.capabilities = std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35});
266
267 adapter_->RegisterAudioSink(
268 options, base::Bind(&BluetoothAudioSinkBlueZTest::RegisterCallback,
269 base::Unretained(this)),
270 base::Bind(&BluetoothAudioSinkBlueZTest::RegisterErrorCallback,
271 base::Unretained(this)));
272
273 EXPECT_EQ(callback_count_, 0);
274 EXPECT_EQ(error_callback_count_, 1);
275
276 // Sets options with a valid codec and invalid capabilities.
277 options.codec = 0x00;
278 options.capabilities.clear();
279 adapter_->RegisterAudioSink(
280 options, base::Bind(&BluetoothAudioSinkBlueZTest::RegisterCallback,
281 base::Unretained(this)),
282 base::Bind(&BluetoothAudioSinkBlueZTest::RegisterErrorCallback,
283 base::Unretained(this)));
284
285 EXPECT_EQ(callback_count_, 0);
286 EXPECT_EQ(error_callback_count_, 2);
287 }
288
289 TEST_F(BluetoothAudioSinkBlueZTest, SelectConfiguration) {
290 GetAudioSink();
291
292 // Simulates calling SelectConfiguration on the media endpoint object owned by
293 // |audio_sink_| with some fake capabilities.
294 media_endpoint_->SelectConfiguration(
295 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
296 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
297 base::Unretained(this)));
298
299 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
300 EXPECT_EQ(callback_count_, 2);
301 EXPECT_EQ(error_callback_count_, 0);
302 EXPECT_EQ(observer_->state_changed_count_, 0);
303 EXPECT_EQ(observer_->volume_changed_count_, 0);
304 }
305
306 TEST_F(BluetoothAudioSinkBlueZTest, SetConfiguration) {
307 GetAudioSink();
308
309 media_endpoint_->SelectConfiguration(
310 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
311 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
312 base::Unretained(this)));
313
314 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
315 EXPECT_EQ(callback_count_, 2);
316 EXPECT_EQ(error_callback_count_, 0);
317 EXPECT_EQ(observer_->state_changed_count_, 0);
318 EXPECT_EQ(observer_->volume_changed_count_, 0);
319
320 // Simulates calling SetConfiguration on the media endpoint object owned by
321 // |audio_sink_| with a fake transport path and a
322 // Delegate::TransportProperties structure.
323 media_endpoint_->SetConfiguration(
324 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
325 properties_);
326
327 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
328 EXPECT_EQ(callback_count_, 2);
329 EXPECT_EQ(error_callback_count_, 0);
330 EXPECT_EQ(observer_->state_changed_count_, 1);
331 EXPECT_EQ(observer_->volume_changed_count_, 1);
332 }
333
334 TEST_F(BluetoothAudioSinkBlueZTest, SetConfigurationWithUnexpectedState) {
335 GetAudioSink();
336
337 media_endpoint_->SelectConfiguration(
338 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
339 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
340 base::Unretained(this)));
341
342 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
343 EXPECT_EQ(callback_count_, 2);
344 EXPECT_EQ(error_callback_count_, 0);
345 EXPECT_EQ(observer_->state_changed_count_, 0);
346 EXPECT_EQ(observer_->volume_changed_count_, 0);
347
348 // Set state of Delegate::TransportProperties with an unexpected value.
349 properties_.state = "pending";
350
351 media_endpoint_->SetConfiguration(
352 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
353 properties_);
354
355 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
356 EXPECT_EQ(callback_count_, 2);
357 EXPECT_EQ(error_callback_count_, 0);
358 EXPECT_EQ(observer_->state_changed_count_, 0);
359 EXPECT_EQ(observer_->volume_changed_count_, 0);
360 }
361
362 // Checks if the observer is notified on media-removed event when the state of
363 // |audio_sink_| is STATE_DISCONNECTED. Once the media object is removed, the
364 // audio sink is no longer valid.
365 TEST_F(BluetoothAudioSinkBlueZTest, MediaRemovedDuringDisconnectedState) {
366 GetAudioSink();
367
368 // Gets the media object and makes it invisible to see if the state of the
369 // audio sink changes accordingly.
370 fake_media_->SetVisible(false);
371
372 GetFakeMediaEndpoint();
373
374 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
375 EXPECT_EQ(media_endpoint_, nullptr);
376 EXPECT_EQ(observer_->state_changed_count_, 1);
377 EXPECT_EQ(observer_->volume_changed_count_, 0);
378 }
379
380 // Checks if the observer is notified on media-removed event when the state of
381 // |audio_sink_| is STATE_IDLE. Once the media object is removed, the audio sink
382 // is no longer valid.
383 TEST_F(BluetoothAudioSinkBlueZTest, MediaRemovedDuringIdleState) {
384 GetAudioSink();
385
386 media_endpoint_->SelectConfiguration(
387 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
388 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
389 base::Unretained(this)));
390
391 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
392 EXPECT_EQ(callback_count_, 2);
393 EXPECT_EQ(error_callback_count_, 0);
394 EXPECT_EQ(observer_->state_changed_count_, 0);
395 EXPECT_EQ(observer_->volume_changed_count_, 0);
396
397 media_endpoint_->SetConfiguration(
398 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
399 properties_);
400
401 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
402 EXPECT_EQ(callback_count_, 2);
403 EXPECT_EQ(error_callback_count_, 0);
404 EXPECT_EQ(observer_->state_changed_count_, 1);
405 EXPECT_EQ(observer_->volume_changed_count_, 1);
406
407 // Gets the media object and makes it invisible to see if the state of the
408 // audio sink changes accordingly.
409 fake_media_->SetVisible(false);
410
411 GetFakeMediaEndpoint();
412
413 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
414 EXPECT_EQ(media_endpoint_, nullptr);
415
416 // The state becomes disconnted and then invalid, since the removal of
417 // transport object should happend before media becomes invisible.
418 // State: STATE_IDLE -> STATE_DISCONNECTED -> STATE_INVALID
419 EXPECT_EQ(observer_->state_changed_count_, 3);
420 EXPECT_EQ(observer_->volume_changed_count_, 2);
421 }
422
423 TEST_F(BluetoothAudioSinkBlueZTest, MediaRemovedDuringActiveState) {
424 GetAudioSink();
425
426 media_endpoint_->SelectConfiguration(
427 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
428 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
429 base::Unretained(this)));
430
431 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
432 EXPECT_EQ(callback_count_, 2);
433 EXPECT_EQ(error_callback_count_, 0);
434 EXPECT_EQ(observer_->state_changed_count_, 0);
435 EXPECT_EQ(observer_->volume_changed_count_, 0);
436
437 media_endpoint_->SetConfiguration(
438 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
439 properties_);
440
441 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
442 EXPECT_EQ(callback_count_, 2);
443 EXPECT_EQ(error_callback_count_, 0);
444 EXPECT_EQ(observer_->state_changed_count_, 1);
445 EXPECT_EQ(observer_->volume_changed_count_, 1);
446
447 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
448
449 base::RunLoop().RunUntilIdle();
450
451 // Acquire is called when the state of |audio_sink_| becomes STATE_PENDING,
452 // and Acquire will trigger state change. Therefore, the state will be
453 // STATE_ACTIVE right after STATE_PENDING.
454 // State: STATE_IDLE -> STATE_PENDING -> STATE_ACTIVE
455 EXPECT_EQ(observer_->state_changed_count_, 3);
456
457 // Gets the media object and makes it invisible to see if the state of the
458 // audio sink changes accordingly.
459 fake_media_->SetVisible(false);
460
461 GetFakeMediaEndpoint();
462
463 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
464 EXPECT_EQ(media_endpoint_, nullptr);
465
466 // The state becomes disconnted and then invalid, since the removal of
467 // transport object should happend before media becomes invisible.
468 // State: STATE_ACTIVE -> STATE_DISCONNECTED -> STATE_INVALID
469 EXPECT_EQ(observer_->state_changed_count_, 5);
470 EXPECT_EQ(observer_->volume_changed_count_, 2);
471 }
472
473 // Checks if the observer is notified on transport-removed event when the state
474 // of |audio_sink_| is STATE_IDEL. Once the media transport object is removed,
475 // the audio sink is disconnected.
476 TEST_F(BluetoothAudioSinkBlueZTest, TransportRemovedDuringIdleState) {
477 GetAudioSink();
478
479 media_endpoint_->SelectConfiguration(
480 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
481 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
482 base::Unretained(this)));
483
484 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
485 EXPECT_EQ(callback_count_, 2);
486 EXPECT_EQ(error_callback_count_, 0);
487 EXPECT_EQ(observer_->state_changed_count_, 0);
488 EXPECT_EQ(observer_->volume_changed_count_, 0);
489
490 media_endpoint_->SetConfiguration(
491 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
492 properties_);
493
494 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
495 EXPECT_EQ(callback_count_, 2);
496 EXPECT_EQ(error_callback_count_, 0);
497 EXPECT_EQ(observer_->state_changed_count_, 1);
498 EXPECT_EQ(observer_->volume_changed_count_, 1);
499
500 // Makes the transport object invalid to see if the state of the audio sink
501 // changes accordingly.
502 fake_transport_->SetValid(media_endpoint_, false);
503
504 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
505 EXPECT_NE(media_endpoint_, nullptr);
506 EXPECT_EQ(observer_->state_changed_count_, 2);
507 EXPECT_EQ(observer_->volume_changed_count_, 2);
508 }
509
510 TEST_F(BluetoothAudioSinkBlueZTest, TransportRemovedDuringActiveState) {
511 GetAudioSink();
512
513 media_endpoint_->SelectConfiguration(
514 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
515 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
516 base::Unretained(this)));
517
518 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
519 EXPECT_EQ(callback_count_, 2);
520 EXPECT_EQ(error_callback_count_, 0);
521 EXPECT_EQ(observer_->state_changed_count_, 0);
522 EXPECT_EQ(observer_->volume_changed_count_, 0);
523
524 media_endpoint_->SetConfiguration(
525 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
526 properties_);
527
528 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
529 EXPECT_EQ(callback_count_, 2);
530 EXPECT_EQ(error_callback_count_, 0);
531 EXPECT_EQ(observer_->state_changed_count_, 1);
532 EXPECT_EQ(observer_->volume_changed_count_, 1);
533
534 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
535
536 base::RunLoop().RunUntilIdle();
537
538 // Acquire is called when the state of |audio_sink_| becomes STATE_PENDING,
539 // and Acquire will trigger state change. Therefore, the state will be
540 // STATE_ACTIVE right after STATE_PENDING.
541 // State: STATE_IDLE -> STATE_PENDING -> STATE_ACTIVE
542 EXPECT_EQ(observer_->state_changed_count_, 3);
543
544 // Makes the transport object invalid to see if the state of the audio sink
545 // changes accordingly.
546 fake_transport_->SetValid(media_endpoint_, false);
547
548 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
549 EXPECT_NE(media_endpoint_, nullptr);
550 EXPECT_EQ(observer_->state_changed_count_, 4);
551 EXPECT_EQ(observer_->volume_changed_count_, 2);
552 }
553
554 TEST_F(BluetoothAudioSinkBlueZTest,
555 AdapterPoweredChangedDuringDisconnectedState) {
556 GetAudioSink();
557
558 adapter_->SetPowered(false, base::Bind(&BluetoothAudioSinkBlueZTest::Callback,
559 base::Unretained(this)),
560 base::Bind(&BluetoothAudioSinkBlueZTest::ErrorCallback,
561 base::Unretained(this)));
562
563 EXPECT_TRUE(adapter_->IsPresent());
564 EXPECT_FALSE(adapter_->IsPowered());
565 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
566 EXPECT_EQ(callback_count_, 2);
567 EXPECT_EQ(error_callback_count_, 0);
568 EXPECT_EQ(observer_->state_changed_count_, 0);
569 EXPECT_EQ(observer_->volume_changed_count_, 0);
570
571 adapter_->SetPowered(true, base::Bind(&BluetoothAudioSinkBlueZTest::Callback,
572 base::Unretained(this)),
573 base::Bind(&BluetoothAudioSinkBlueZTest::ErrorCallback,
574 base::Unretained(this)));
575
576 EXPECT_TRUE(adapter_->IsPresent());
577 EXPECT_TRUE(adapter_->IsPowered());
578 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
579 EXPECT_EQ(callback_count_, 3);
580 EXPECT_EQ(error_callback_count_, 0);
581 EXPECT_EQ(observer_->state_changed_count_, 0);
582 EXPECT_EQ(observer_->volume_changed_count_, 0);
583 }
584
585 TEST_F(BluetoothAudioSinkBlueZTest, AdapterPoweredChangedDuringIdleState) {
586 GetAudioSink();
587
588 media_endpoint_->SelectConfiguration(
589 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
590 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
591 base::Unretained(this)));
592
593 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
594 EXPECT_EQ(callback_count_, 2);
595 EXPECT_EQ(error_callback_count_, 0);
596 EXPECT_EQ(observer_->state_changed_count_, 0);
597 EXPECT_EQ(observer_->volume_changed_count_, 0);
598
599 media_endpoint_->SetConfiguration(
600 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
601 properties_);
602
603 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
604 EXPECT_EQ(callback_count_, 2);
605 EXPECT_EQ(error_callback_count_, 0);
606 EXPECT_EQ(observer_->state_changed_count_, 1);
607 EXPECT_EQ(observer_->volume_changed_count_, 1);
608
609 adapter_->SetPowered(false, base::Bind(&BluetoothAudioSinkBlueZTest::Callback,
610 base::Unretained(this)),
611 base::Bind(&BluetoothAudioSinkBlueZTest::ErrorCallback,
612 base::Unretained(this)));
613 GetFakeMediaEndpoint();
614
615 EXPECT_TRUE(adapter_->IsPresent());
616 EXPECT_FALSE(adapter_->IsPowered());
617 EXPECT_NE(media_endpoint_, nullptr);
618 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
619 EXPECT_EQ(callback_count_, 3);
620 EXPECT_EQ(error_callback_count_, 0);
621 EXPECT_EQ(observer_->state_changed_count_, 2);
622 EXPECT_EQ(observer_->volume_changed_count_, 2);
623 }
624
625 TEST_F(BluetoothAudioSinkBlueZTest,
626 UnregisterAudioSinkDuringDisconnectedState) {
627 GetAudioSink();
628
629 audio_sink_->Unregister(
630 base::Bind(&BluetoothAudioSinkBlueZTest::Callback,
631 base::Unretained(this)),
632 base::Bind(&BluetoothAudioSinkBlueZTest::UnregisterErrorCallback,
633 base::Unretained(this)));
634
635 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
636 EXPECT_EQ(callback_count_, 2);
637 EXPECT_EQ(error_callback_count_, 0);
638 EXPECT_EQ(observer_->state_changed_count_, 1);
639 EXPECT_EQ(observer_->volume_changed_count_, 0);
640 }
641
642 TEST_F(BluetoothAudioSinkBlueZTest, UnregisterAudioSinkDuringIdleState) {
643 GetAudioSink();
644
645 media_endpoint_->SelectConfiguration(
646 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
647 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
648 base::Unretained(this)));
649
650 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
651 EXPECT_EQ(callback_count_, 2);
652 EXPECT_EQ(error_callback_count_, 0);
653 EXPECT_EQ(observer_->state_changed_count_, 0);
654 EXPECT_EQ(observer_->volume_changed_count_, 0);
655
656 media_endpoint_->SetConfiguration(
657 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
658 properties_);
659
660 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
661 EXPECT_EQ(callback_count_, 2);
662 EXPECT_EQ(error_callback_count_, 0);
663 EXPECT_EQ(observer_->state_changed_count_, 1);
664 EXPECT_EQ(observer_->volume_changed_count_, 1);
665
666 audio_sink_->Unregister(
667 base::Bind(&BluetoothAudioSinkBlueZTest::Callback,
668 base::Unretained(this)),
669 base::Bind(&BluetoothAudioSinkBlueZTest::UnregisterErrorCallback,
670 base::Unretained(this)));
671
672 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
673 EXPECT_EQ(callback_count_, 3);
674 EXPECT_EQ(error_callback_count_, 0);
675
676 // The state becomes disconnted and then invalid, since the removal of
677 // transport object should happend before the unregistration of endpoint.
678 // State: STATE_IDLE -> STATE_DISCONNECTED -> STATE_INVALID
679 EXPECT_EQ(observer_->state_changed_count_, 3);
680 EXPECT_EQ(observer_->volume_changed_count_, 2);
681 }
682
683 TEST_F(BluetoothAudioSinkBlueZTest, UnregisterAudioSinkDuringActiveState) {
684 GetAudioSink();
685
686 media_endpoint_->SelectConfiguration(
687 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
688 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
689 base::Unretained(this)));
690
691 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
692 EXPECT_EQ(callback_count_, 2);
693 EXPECT_EQ(error_callback_count_, 0);
694 EXPECT_EQ(observer_->state_changed_count_, 0);
695 EXPECT_EQ(observer_->volume_changed_count_, 0);
696
697 media_endpoint_->SetConfiguration(
698 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
699 properties_);
700
701 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
702 EXPECT_EQ(callback_count_, 2);
703 EXPECT_EQ(error_callback_count_, 0);
704 EXPECT_EQ(observer_->state_changed_count_, 1);
705 EXPECT_EQ(observer_->volume_changed_count_, 1);
706
707 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
708
709 base::RunLoop().RunUntilIdle();
710
711 // Acquire is called when the state of |audio_sink_| becomes STATE_PENDING,
712 // and Acquire will trigger state change. Therefore, the state will be
713 // STATE_ACTIVE right after STATE_PENDING.
714 // State: STATE_IDLE -> STATE_PENDING -> STATE_ACTIVE
715 EXPECT_EQ(observer_->state_changed_count_, 3);
716
717 audio_sink_->Unregister(
718 base::Bind(&BluetoothAudioSinkBlueZTest::Callback,
719 base::Unretained(this)),
720 base::Bind(&BluetoothAudioSinkBlueZTest::UnregisterErrorCallback,
721 base::Unretained(this)));
722
723 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
724 EXPECT_EQ(callback_count_, 3);
725 EXPECT_EQ(error_callback_count_, 0);
726 EXPECT_EQ(observer_->state_changed_count_, 5);
727 EXPECT_EQ(observer_->volume_changed_count_, 2);
728 }
729
730 TEST_F(BluetoothAudioSinkBlueZTest, StateChanged) {
731 GetAudioSink();
732
733 media_endpoint_->SelectConfiguration(
734 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
735 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
736 base::Unretained(this)));
737
738 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
739 EXPECT_EQ(callback_count_, 2);
740 EXPECT_EQ(error_callback_count_, 0);
741 EXPECT_EQ(observer_->state_changed_count_, 0);
742 EXPECT_EQ(observer_->volume_changed_count_, 0);
743
744 media_endpoint_->SetConfiguration(
745 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
746 properties_);
747
748 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
749 EXPECT_EQ(callback_count_, 2);
750 EXPECT_EQ(error_callback_count_, 0);
751 EXPECT_EQ(observer_->state_changed_count_, 1);
752 EXPECT_EQ(observer_->volume_changed_count_, 1);
753
754 // Changes the current state of transport to pending.
755 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
756
757 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_PENDING);
758 EXPECT_EQ(observer_->state_changed_count_, 3);
759 EXPECT_EQ(observer_->volume_changed_count_, 1);
760 }
761
762 TEST_F(BluetoothAudioSinkBlueZTest, VolumeChanged) {
763 GetAudioSink();
764
765 media_endpoint_->SelectConfiguration(
766 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
767 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
768 base::Unretained(this)));
769
770 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
771 EXPECT_EQ(callback_count_, 2);
772 EXPECT_EQ(error_callback_count_, 0);
773 EXPECT_EQ(observer_->state_changed_count_, 0);
774 EXPECT_EQ(observer_->volume_changed_count_, 0);
775
776 media_endpoint_->SetConfiguration(
777 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
778 properties_);
779
780 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
781 EXPECT_EQ(callback_count_, 2);
782 EXPECT_EQ(error_callback_count_, 0);
783 EXPECT_EQ(observer_->state_changed_count_, 1);
784 EXPECT_EQ(observer_->volume_changed_count_, 1);
785
786 // |kTransportVolume| is the initial volume of the transport, and this
787 // value is propagated to the audio sink via SetConfiguration.
788 EXPECT_EQ(audio_sink_->GetVolume(),
789 FakeBluetoothMediaTransportClient::kTransportVolume);
790
791 // Changes volume to a valid level.
792 fake_transport_->SetVolume(media_endpoint_->object_path(), 100);
793
794 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
795 EXPECT_EQ(observer_->state_changed_count_, 1);
796 EXPECT_EQ(observer_->volume_changed_count_, 2);
797 EXPECT_EQ(audio_sink_->GetVolume(), 100);
798
799 // Changes volume to an invalid level.
800 fake_transport_->SetVolume(media_endpoint_->object_path(), 200);
801
802 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
803 EXPECT_EQ(observer_->state_changed_count_, 1);
804 EXPECT_EQ(observer_->volume_changed_count_, 3);
805 EXPECT_EQ(audio_sink_->GetVolume(), BluetoothAudioSink::kInvalidVolume);
806 }
807
808 TEST_F(BluetoothAudioSinkBlueZTest, AcquireFD) {
809 GetAudioSink();
810
811 media_endpoint_->SelectConfiguration(
812 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
813 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
814 base::Unretained(this)));
815
816 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
817 EXPECT_EQ(callback_count_, 2);
818 EXPECT_EQ(error_callback_count_, 0);
819 EXPECT_EQ(observer_->state_changed_count_, 0);
820 EXPECT_EQ(observer_->volume_changed_count_, 0);
821
822 media_endpoint_->SetConfiguration(
823 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
824 properties_);
825
826 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
827 EXPECT_EQ(callback_count_, 2);
828 EXPECT_EQ(error_callback_count_, 0);
829 EXPECT_EQ(observer_->state_changed_count_, 1);
830 EXPECT_EQ(observer_->volume_changed_count_, 1);
831
832 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
833
834 std::vector<char> data_one(16, 0x12);
835 fake_transport_->WriteData(media_endpoint_->object_path(), data_one);
836
837 base::RunLoop().RunUntilIdle();
838
839 // Acquire is called when the state of |audio_sink_| becomes STATE_PENDING,
840 // and Acquire will trigger state change. Therefore, the state will be
841 // STATE_ACTIVE right after STATE_PENDING.
842 // State: STATE_IDLE -> STATE_PENDING -> STATE_ACTIVE
843 EXPECT_EQ(observer_->state_changed_count_, 3);
844 EXPECT_EQ(observer_->total_read_, data_one.size());
845 EXPECT_EQ(observer_->data_, data_one);
846 EXPECT_EQ(observer_->read_mtu_,
847 FakeBluetoothMediaTransportClient::kDefaultReadMtu);
848 }
849
850 // Tests the case where the remote device pauses and resume audio streaming.
851 TEST_F(BluetoothAudioSinkBlueZTest, PauseAndResume) {
852 GetAudioSink();
853
854 media_endpoint_->SelectConfiguration(
855 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
856 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
857 base::Unretained(this)));
858
859 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
860 EXPECT_EQ(callback_count_, 2);
861 EXPECT_EQ(error_callback_count_, 0);
862 EXPECT_EQ(observer_->state_changed_count_, 0);
863 EXPECT_EQ(observer_->volume_changed_count_, 0);
864
865 media_endpoint_->SetConfiguration(
866 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
867 properties_);
868
869 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
870 EXPECT_EQ(callback_count_, 2);
871 EXPECT_EQ(error_callback_count_, 0);
872 EXPECT_EQ(observer_->state_changed_count_, 1);
873 EXPECT_EQ(observer_->volume_changed_count_, 1);
874
875 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
876
877 std::vector<char> data_one(16, 0x12);
878 fake_transport_->WriteData(media_endpoint_->object_path(), data_one);
879
880 base::RunLoop().RunUntilIdle();
881
882 EXPECT_EQ(observer_->data_, data_one);
883 EXPECT_EQ(observer_->read_mtu_,
884 FakeBluetoothMediaTransportClient::kDefaultReadMtu);
885 EXPECT_EQ(observer_->state_changed_count_, 3);
886 EXPECT_EQ(observer_->total_read_, data_one.size());
887
888 // Simulates the situation where the remote device pauses and resume audio
889 // streaming.
890 fake_transport_->SetState(media_endpoint_->object_path(), "idle");
891
892 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
893 EXPECT_EQ(observer_->state_changed_count_, 4);
894
895 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
896
897 std::vector<char> data_two(8, 0x10);
898 fake_transport_->WriteData(media_endpoint_->object_path(), data_two);
899
900 base::RunLoop().RunUntilIdle();
901
902 EXPECT_EQ(observer_->data_, data_two);
903 EXPECT_EQ(observer_->read_mtu_,
904 FakeBluetoothMediaTransportClient::kDefaultReadMtu);
905 EXPECT_EQ(observer_->state_changed_count_, 6);
906 EXPECT_EQ(observer_->total_read_, data_two.size());
907 }
908
909 TEST_F(BluetoothAudioSinkBlueZTest, ContinuouslyStreaming) {
910 GetAudioSink();
911
912 media_endpoint_->SelectConfiguration(
913 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
914 base::Bind(&BluetoothAudioSinkBlueZTest::SelectConfigurationCallback,
915 base::Unretained(this)));
916
917 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
918 EXPECT_EQ(callback_count_, 2);
919 EXPECT_EQ(error_callback_count_, 0);
920 EXPECT_EQ(observer_->state_changed_count_, 0);
921 EXPECT_EQ(observer_->volume_changed_count_, 0);
922
923 media_endpoint_->SetConfiguration(
924 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
925 properties_);
926
927 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
928 EXPECT_EQ(callback_count_, 2);
929 EXPECT_EQ(error_callback_count_, 0);
930 EXPECT_EQ(observer_->state_changed_count_, 1);
931 EXPECT_EQ(observer_->volume_changed_count_, 1);
932
933 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
934
935 std::vector<char> data_one(16, 0x12);
936 fake_transport_->WriteData(media_endpoint_->object_path(), data_one);
937
938 base::RunLoop().RunUntilIdle();
939
940 EXPECT_EQ(observer_->data_, data_one);
941 EXPECT_EQ(observer_->read_mtu_,
942 FakeBluetoothMediaTransportClient::kDefaultReadMtu);
943 EXPECT_EQ(observer_->state_changed_count_, 3);
944 EXPECT_EQ(observer_->total_read_, data_one.size());
945
946 std::vector<char> data_two(8, 0x10);
947 fake_transport_->WriteData(media_endpoint_->object_path(), data_two);
948
949 base::RunLoop().RunUntilIdle();
950
951 EXPECT_EQ(observer_->data_, data_two);
952 EXPECT_EQ(observer_->read_mtu_,
953 FakeBluetoothMediaTransportClient::kDefaultReadMtu);
954 EXPECT_EQ(observer_->state_changed_count_, 3);
955 EXPECT_EQ(observer_->total_read_, data_one.size() + data_two.size());
956 }
957
958 } // namespace bluez
OLDNEW
« no previous file with comments | « device/bluetooth/bluez/bluetooth_audio_sink_bluez.cc ('k') | device/bluetooth/bluez/bluetooth_bluez_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698