Simplify About Chrome settings page.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_audio_sink_chromeos_unittest.cc
blob9781dec1ec2e9aece5da08b1aadc884eaefc7f28
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.
5 #include <vector>
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/message_loop/message_loop.h"
11 #include "chromeos/dbus/bluetooth_media_client.h"
12 #include "chromeos/dbus/bluetooth_media_endpoint_service_provider.h"
13 #include "chromeos/dbus/bluetooth_media_transport_client.h"
14 #include "chromeos/dbus/dbus_thread_manager.h"
15 #include "chromeos/dbus/fake_bluetooth_media_client.h"
16 #include "chromeos/dbus/fake_bluetooth_media_endpoint_service_provider.h"
17 #include "chromeos/dbus/fake_bluetooth_media_transport_client.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/bluetooth_audio_sink_chromeos.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 using dbus::ObjectPath;
26 using device::BluetoothAdapter;
27 using device::BluetoothAdapterFactory;
28 using device::BluetoothAudioSink;
30 namespace chromeos {
32 class TestAudioSinkObserver : public BluetoothAudioSink::Observer {
33 public:
34 explicit TestAudioSinkObserver(scoped_refptr<BluetoothAudioSink> audio_sink)
35 : state_changed_count_(0),
36 volume_changed_count_(0),
37 total_read_(0),
38 state_(audio_sink->GetState()),
39 audio_sink_(audio_sink) {
40 audio_sink_->AddObserver(this);
43 ~TestAudioSinkObserver() override { audio_sink_->RemoveObserver(this); }
45 void BluetoothAudioSinkStateChanged(
46 BluetoothAudioSink* audio_sink,
47 BluetoothAudioSink::State state) override {
48 if (state == BluetoothAudioSink::STATE_IDLE)
49 total_read_ = 0;
51 ++state_changed_count_;
54 void BluetoothAudioSinkVolumeChanged(BluetoothAudioSink* audio_sink,
55 uint16_t volume) override {
56 ++volume_changed_count_;
59 void BluetoothAudioSinkDataAvailable(BluetoothAudioSink* audio_sink,
60 char* data,
61 size_t size,
62 uint16_t read_mtu) override {
63 total_read_ += size;
64 data_.clear();
65 data_.insert(data_.begin(), data, data + size);
66 read_mtu_ = read_mtu;
69 int state_changed_count_;
70 int volume_changed_count_;
71 int data_available_count_;
72 size_t total_read_;
73 std::vector<char> data_;
74 uint16_t read_mtu_;
75 BluetoothAudioSink::State state_;
77 private:
78 scoped_refptr<BluetoothAudioSink> audio_sink_;
81 class BluetoothAudioSinkChromeOSTest : public testing::Test {
82 public:
83 void SetUp() override {
84 DBusThreadManager::Initialize();
86 callback_count_ = 0;
87 error_callback_count_ = 0;
89 fake_media_ = static_cast<FakeBluetoothMediaClient*>(
90 DBusThreadManager::Get()->GetBluetoothMediaClient());
91 fake_transport_ = static_cast<FakeBluetoothMediaTransportClient*>(
92 DBusThreadManager::Get()->GetBluetoothMediaTransportClient());
94 // Initiates Delegate::TransportProperties with default values.
95 properties_.device =
96 ObjectPath(FakeBluetoothMediaTransportClient::kTransportDevicePath);
97 properties_.uuid = BluetoothMediaClient::kBluetoothAudioSinkUUID;
98 properties_.codec = FakeBluetoothMediaTransportClient::kTransportCodec;
99 properties_.configuration =
100 FakeBluetoothMediaTransportClient::kTransportConfiguration;
101 properties_.state = BluetoothMediaTransportClient::kStateIdle;
102 properties_.delay.reset(
103 new uint16_t(FakeBluetoothMediaTransportClient::kTransportDelay));
104 properties_.volume.reset(
105 new uint16_t(FakeBluetoothMediaTransportClient::kTransportVolume));
107 GetAdapter();
110 void TearDown() override {
111 callback_count_ = 0;
112 error_callback_count_ = 0;
113 observer_.reset();
115 fake_media_->SetVisible(true);
117 // The adapter should outlive audio sink.
118 audio_sink_ = nullptr;
119 adapter_ = nullptr;
120 DBusThreadManager::Shutdown();
123 // Gets the existing Bluetooth adapter.
124 void GetAdapter() {
125 BluetoothAdapterFactory::GetAdapter(
126 base::Bind(&BluetoothAudioSinkChromeOSTest::GetAdapterCallback,
127 base::Unretained(this)));
130 // Called whenever BluetoothAdapter is retrieved successfully.
131 void GetAdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
132 adapter_ = adapter;
134 ASSERT_NE(adapter_.get(), nullptr);
135 ASSERT_TRUE(adapter_->IsInitialized());
136 adapter_->SetPowered(
137 true,
138 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
139 base::Unretained(this)),
140 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback,
141 base::Unretained(this)));
142 ASSERT_TRUE(adapter_->IsPresent());
143 ASSERT_TRUE(adapter_->IsPowered());
144 EXPECT_EQ(callback_count_, 1);
145 EXPECT_EQ(error_callback_count_, 0);
147 // Resets callback_count_.
148 --callback_count_;
151 // Registers BluetoothAudioSinkChromeOS with default codec and capabilities.
152 // If the audio sink is retrieved successfully, the state changes to
153 // STATE_DISCONNECTED.
154 void GetAudioSink() {
155 // Sets up valid codec and capabilities.
156 BluetoothAudioSink::Options options;
157 ASSERT_EQ(options.codec, 0x00);
158 ASSERT_EQ(options.capabilities,
159 std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35}));
161 // Registers |audio_sink_| with valid codec and capabilities
162 adapter_->RegisterAudioSink(
163 options,
164 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback,
165 base::Unretained(this)),
166 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback,
167 base::Unretained(this)));
169 observer_.reset(new TestAudioSinkObserver(audio_sink_));
170 EXPECT_EQ(callback_count_, 1);
171 EXPECT_EQ(error_callback_count_, 0);
172 EXPECT_EQ(observer_->state_changed_count_, 0);
173 EXPECT_EQ(observer_->volume_changed_count_, 0);
176 void GetFakeMediaEndpoint() {
177 BluetoothAudioSinkChromeOS* audio_sink_chromeos =
178 static_cast<BluetoothAudioSinkChromeOS*>(audio_sink_.get());
179 ASSERT_NE(audio_sink_chromeos, nullptr);
181 media_endpoint_ = static_cast<FakeBluetoothMediaEndpointServiceProvider*>(
182 audio_sink_chromeos->GetEndpointServiceProvider());
185 // Called whenever RegisterAudioSink is completed successfully.
186 void RegisterCallback(
187 scoped_refptr<BluetoothAudioSink> audio_sink) {
188 ++callback_count_;
189 audio_sink_ = audio_sink;
191 GetFakeMediaEndpoint();
192 ASSERT_NE(media_endpoint_, nullptr);
193 fake_media_->SetEndpointRegistered(media_endpoint_, true);
195 ASSERT_NE(audio_sink_.get(), nullptr);
196 ASSERT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
199 // Called whenever RegisterAudioSink failed.
200 void RegisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) {
201 ++error_callback_count_;
202 EXPECT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_REGISTERED);
205 // Called whenever there capabilities are returned from SelectConfiguration.
206 void SelectConfigurationCallback(const std::vector<uint8_t>& capabilities) {
207 ++callback_count_;
209 // |capabilities| should be the same as the capabilities for registering an
210 // audio sink in GetAudioSink().
211 EXPECT_EQ(capabilities, std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35}));
214 void UnregisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) {
215 ++error_callback_count_;
216 EXPECT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_UNREGISTERED);
219 // Generic callbacks.
220 void Callback() {
221 ++callback_count_;
224 void ErrorCallback() {
225 ++error_callback_count_;
228 protected:
229 int callback_count_;
230 int error_callback_count_;
232 base::MessageLoopForIO message_loop_;
234 FakeBluetoothMediaClient* fake_media_;
235 FakeBluetoothMediaTransportClient* fake_transport_;
236 FakeBluetoothMediaEndpointServiceProvider* media_endpoint_;
237 scoped_ptr<TestAudioSinkObserver> observer_;
238 scoped_refptr<BluetoothAdapter> adapter_;
239 scoped_refptr<BluetoothAudioSink> audio_sink_;
241 // The default property set used while calling SetConfiguration on a media
242 // endpoint object.
243 BluetoothMediaEndpointServiceProvider::Delegate::TransportProperties
244 properties_;
247 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterSucceeded) {
248 GetAudioSink();
251 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterFailedWithInvalidOptions) {
252 // Sets options with an invalid codec and valid capabilities.
253 BluetoothAudioSink::Options options;
254 options.codec = 0xff;
255 options.capabilities = std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35});
257 adapter_->RegisterAudioSink(
258 options,
259 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback,
260 base::Unretained(this)),
261 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback,
262 base::Unretained(this)));
264 EXPECT_EQ(callback_count_, 0);
265 EXPECT_EQ(error_callback_count_, 1);
267 // Sets options with a valid codec and invalid capabilities.
268 options.codec = 0x00;
269 options.capabilities.clear();
270 adapter_->RegisterAudioSink(
271 options,
272 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback,
273 base::Unretained(this)),
274 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback,
275 base::Unretained(this)));
277 EXPECT_EQ(callback_count_, 0);
278 EXPECT_EQ(error_callback_count_, 2);
281 TEST_F(BluetoothAudioSinkChromeOSTest, SelectConfiguration) {
282 GetAudioSink();
284 // Simulates calling SelectConfiguration on the media endpoint object owned by
285 // |audio_sink_| with some fake capabilities.
286 media_endpoint_->SelectConfiguration(
287 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
288 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
289 base::Unretained(this)));
291 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
292 EXPECT_EQ(callback_count_, 2);
293 EXPECT_EQ(error_callback_count_, 0);
294 EXPECT_EQ(observer_->state_changed_count_, 0);
295 EXPECT_EQ(observer_->volume_changed_count_, 0);
298 TEST_F(BluetoothAudioSinkChromeOSTest, SetConfiguration) {
299 GetAudioSink();
301 media_endpoint_->SelectConfiguration(
302 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
303 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
304 base::Unretained(this)));
306 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
307 EXPECT_EQ(callback_count_, 2);
308 EXPECT_EQ(error_callback_count_, 0);
309 EXPECT_EQ(observer_->state_changed_count_, 0);
310 EXPECT_EQ(observer_->volume_changed_count_, 0);
312 // Simulates calling SetConfiguration on the media endpoint object owned by
313 // |audio_sink_| with a fake transport path and a
314 // Delegate::TransportProperties structure.
315 media_endpoint_->SetConfiguration(
316 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
317 properties_);
319 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
320 EXPECT_EQ(callback_count_, 2);
321 EXPECT_EQ(error_callback_count_, 0);
322 EXPECT_EQ(observer_->state_changed_count_, 1);
323 EXPECT_EQ(observer_->volume_changed_count_, 1);
326 TEST_F(BluetoothAudioSinkChromeOSTest, SetConfigurationWithUnexpectedState) {
327 GetAudioSink();
329 media_endpoint_->SelectConfiguration(
330 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
331 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
332 base::Unretained(this)));
334 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
335 EXPECT_EQ(callback_count_, 2);
336 EXPECT_EQ(error_callback_count_, 0);
337 EXPECT_EQ(observer_->state_changed_count_, 0);
338 EXPECT_EQ(observer_->volume_changed_count_, 0);
340 // Set state of Delegate::TransportProperties with an unexpected value.
341 properties_.state = "pending";
343 media_endpoint_->SetConfiguration(
344 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
345 properties_);
347 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
348 EXPECT_EQ(callback_count_, 2);
349 EXPECT_EQ(error_callback_count_, 0);
350 EXPECT_EQ(observer_->state_changed_count_, 0);
351 EXPECT_EQ(observer_->volume_changed_count_, 0);
354 // Checks if the observer is notified on media-removed event when the state of
355 // |audio_sink_| is STATE_DISCONNECTED. Once the media object is removed, the
356 // audio sink is no longer valid.
357 TEST_F(BluetoothAudioSinkChromeOSTest, MediaRemovedDuringDisconnectedState) {
358 GetAudioSink();
360 // Gets the media object and makes it invisible to see if the state of the
361 // audio sink changes accordingly.
362 fake_media_->SetVisible(false);
364 GetFakeMediaEndpoint();
366 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
367 EXPECT_EQ(media_endpoint_, nullptr);
368 EXPECT_EQ(observer_->state_changed_count_, 1);
369 EXPECT_EQ(observer_->volume_changed_count_, 0);
372 // Checks if the observer is notified on media-removed event when the state of
373 // |audio_sink_| is STATE_IDLE. Once the media object is removed, the audio sink
374 // is no longer valid.
375 TEST_F(BluetoothAudioSinkChromeOSTest, MediaRemovedDuringIdleState) {
376 GetAudioSink();
378 media_endpoint_->SelectConfiguration(
379 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
380 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
381 base::Unretained(this)));
383 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
384 EXPECT_EQ(callback_count_, 2);
385 EXPECT_EQ(error_callback_count_, 0);
386 EXPECT_EQ(observer_->state_changed_count_, 0);
387 EXPECT_EQ(observer_->volume_changed_count_, 0);
389 media_endpoint_->SetConfiguration(
390 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
391 properties_);
393 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
394 EXPECT_EQ(callback_count_, 2);
395 EXPECT_EQ(error_callback_count_, 0);
396 EXPECT_EQ(observer_->state_changed_count_, 1);
397 EXPECT_EQ(observer_->volume_changed_count_, 1);
399 // Gets the media object and makes it invisible to see if the state of the
400 // audio sink changes accordingly.
401 fake_media_->SetVisible(false);
403 GetFakeMediaEndpoint();
405 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
406 EXPECT_EQ(media_endpoint_, nullptr);
408 // The state becomes disconnted and then invalid, since the removal of
409 // transport object should happend before media becomes invisible.
410 // State: STATE_IDLE -> STATE_DISCONNECTED -> STATE_INVALID
411 EXPECT_EQ(observer_->state_changed_count_, 3);
412 EXPECT_EQ(observer_->volume_changed_count_, 2);
415 TEST_F(BluetoothAudioSinkChromeOSTest, MediaRemovedDuringActiveState) {
416 GetAudioSink();
418 media_endpoint_->SelectConfiguration(
419 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
420 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
421 base::Unretained(this)));
423 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
424 EXPECT_EQ(callback_count_, 2);
425 EXPECT_EQ(error_callback_count_, 0);
426 EXPECT_EQ(observer_->state_changed_count_, 0);
427 EXPECT_EQ(observer_->volume_changed_count_, 0);
429 media_endpoint_->SetConfiguration(
430 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
431 properties_);
433 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
434 EXPECT_EQ(callback_count_, 2);
435 EXPECT_EQ(error_callback_count_, 0);
436 EXPECT_EQ(observer_->state_changed_count_, 1);
437 EXPECT_EQ(observer_->volume_changed_count_, 1);
439 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
441 message_loop_.RunUntilIdle();
443 // Acquire is called when the state of |audio_sink_| becomes STATE_PENDING,
444 // and Acquire will trigger state change. Therefore, the state will be
445 // STATE_ACTIVE right after STATE_PENDING.
446 // State: STATE_IDLE -> STATE_PENDING -> STATE_ACTIVE
447 EXPECT_EQ(observer_->state_changed_count_, 3);
449 // Gets the media object and makes it invisible to see if the state of the
450 // audio sink changes accordingly.
451 fake_media_->SetVisible(false);
453 GetFakeMediaEndpoint();
455 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
456 EXPECT_EQ(media_endpoint_, nullptr);
458 // The state becomes disconnted and then invalid, since the removal of
459 // transport object should happend before media becomes invisible.
460 // State: STATE_ACTIVE -> STATE_DISCONNECTED -> STATE_INVALID
461 EXPECT_EQ(observer_->state_changed_count_, 5);
462 EXPECT_EQ(observer_->volume_changed_count_, 2);
465 // Checks if the observer is notified on transport-removed event when the state
466 // of |audio_sink_| is STATE_IDEL. Once the media transport object is removed,
467 // the audio sink is disconnected.
468 TEST_F(BluetoothAudioSinkChromeOSTest, TransportRemovedDuringIdleState) {
469 GetAudioSink();
471 media_endpoint_->SelectConfiguration(
472 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
473 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
474 base::Unretained(this)));
476 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
477 EXPECT_EQ(callback_count_, 2);
478 EXPECT_EQ(error_callback_count_, 0);
479 EXPECT_EQ(observer_->state_changed_count_, 0);
480 EXPECT_EQ(observer_->volume_changed_count_, 0);
482 media_endpoint_->SetConfiguration(
483 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
484 properties_);
486 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
487 EXPECT_EQ(callback_count_, 2);
488 EXPECT_EQ(error_callback_count_, 0);
489 EXPECT_EQ(observer_->state_changed_count_, 1);
490 EXPECT_EQ(observer_->volume_changed_count_, 1);
492 // Makes the transport object invalid to see if the state of the audio sink
493 // changes accordingly.
494 fake_transport_->SetValid(media_endpoint_, false);
496 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
497 EXPECT_NE(media_endpoint_, nullptr);
498 EXPECT_EQ(observer_->state_changed_count_, 2);
499 EXPECT_EQ(observer_->volume_changed_count_, 2);
502 TEST_F(BluetoothAudioSinkChromeOSTest, TransportRemovedDuringActiveState) {
503 GetAudioSink();
505 media_endpoint_->SelectConfiguration(
506 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
507 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
508 base::Unretained(this)));
510 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
511 EXPECT_EQ(callback_count_, 2);
512 EXPECT_EQ(error_callback_count_, 0);
513 EXPECT_EQ(observer_->state_changed_count_, 0);
514 EXPECT_EQ(observer_->volume_changed_count_, 0);
516 media_endpoint_->SetConfiguration(
517 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
518 properties_);
520 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
521 EXPECT_EQ(callback_count_, 2);
522 EXPECT_EQ(error_callback_count_, 0);
523 EXPECT_EQ(observer_->state_changed_count_, 1);
524 EXPECT_EQ(observer_->volume_changed_count_, 1);
526 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
528 message_loop_.RunUntilIdle();
530 // Acquire is called when the state of |audio_sink_| becomes STATE_PENDING,
531 // and Acquire will trigger state change. Therefore, the state will be
532 // STATE_ACTIVE right after STATE_PENDING.
533 // State: STATE_IDLE -> STATE_PENDING -> STATE_ACTIVE
534 EXPECT_EQ(observer_->state_changed_count_, 3);
536 // Makes the transport object invalid to see if the state of the audio sink
537 // changes accordingly.
538 fake_transport_->SetValid(media_endpoint_, false);
540 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
541 EXPECT_NE(media_endpoint_, nullptr);
542 EXPECT_EQ(observer_->state_changed_count_, 4);
543 EXPECT_EQ(observer_->volume_changed_count_, 2);
546 TEST_F(BluetoothAudioSinkChromeOSTest,
547 AdapterPoweredChangedDuringDisconnectedState) {
548 GetAudioSink();
550 adapter_->SetPowered(
551 false,
552 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
553 base::Unretained(this)),
554 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback,
555 base::Unretained(this)));
557 EXPECT_TRUE(adapter_->IsPresent());
558 EXPECT_FALSE(adapter_->IsPowered());
559 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
560 EXPECT_EQ(callback_count_, 2);
561 EXPECT_EQ(error_callback_count_, 0);
562 EXPECT_EQ(observer_->state_changed_count_, 0);
563 EXPECT_EQ(observer_->volume_changed_count_, 0);
565 adapter_->SetPowered(
566 true,
567 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
568 base::Unretained(this)),
569 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback,
570 base::Unretained(this)));
572 EXPECT_TRUE(adapter_->IsPresent());
573 EXPECT_TRUE(adapter_->IsPowered());
574 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
575 EXPECT_EQ(callback_count_, 3);
576 EXPECT_EQ(error_callback_count_, 0);
577 EXPECT_EQ(observer_->state_changed_count_, 0);
578 EXPECT_EQ(observer_->volume_changed_count_, 0);
581 TEST_F(BluetoothAudioSinkChromeOSTest, AdapterPoweredChangedDuringIdleState) {
582 GetAudioSink();
584 media_endpoint_->SelectConfiguration(
585 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
586 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
587 base::Unretained(this)));
589 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
590 EXPECT_EQ(callback_count_, 2);
591 EXPECT_EQ(error_callback_count_, 0);
592 EXPECT_EQ(observer_->state_changed_count_, 0);
593 EXPECT_EQ(observer_->volume_changed_count_, 0);
595 media_endpoint_->SetConfiguration(
596 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
597 properties_);
599 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
600 EXPECT_EQ(callback_count_, 2);
601 EXPECT_EQ(error_callback_count_, 0);
602 EXPECT_EQ(observer_->state_changed_count_, 1);
603 EXPECT_EQ(observer_->volume_changed_count_, 1);
605 adapter_->SetPowered(
606 false,
607 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
608 base::Unretained(this)),
609 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback,
610 base::Unretained(this)));
611 GetFakeMediaEndpoint();
613 EXPECT_TRUE(adapter_->IsPresent());
614 EXPECT_FALSE(adapter_->IsPowered());
615 EXPECT_NE(media_endpoint_, nullptr);
616 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
617 EXPECT_EQ(callback_count_, 3);
618 EXPECT_EQ(error_callback_count_, 0);
619 EXPECT_EQ(observer_->state_changed_count_, 2);
620 EXPECT_EQ(observer_->volume_changed_count_, 2);
623 TEST_F(BluetoothAudioSinkChromeOSTest,
624 UnregisterAudioSinkDuringDisconnectedState) {
625 GetAudioSink();
627 audio_sink_->Unregister(
628 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
629 base::Unretained(this)),
630 base::Bind(&BluetoothAudioSinkChromeOSTest::UnregisterErrorCallback,
631 base::Unretained(this)));
633 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
634 EXPECT_EQ(callback_count_, 2);
635 EXPECT_EQ(error_callback_count_, 0);
636 EXPECT_EQ(observer_->state_changed_count_, 1);
637 EXPECT_EQ(observer_->volume_changed_count_, 0);
640 TEST_F(BluetoothAudioSinkChromeOSTest, UnregisterAudioSinkDuringIdleState) {
641 GetAudioSink();
643 media_endpoint_->SelectConfiguration(
644 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
645 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
646 base::Unretained(this)));
648 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
649 EXPECT_EQ(callback_count_, 2);
650 EXPECT_EQ(error_callback_count_, 0);
651 EXPECT_EQ(observer_->state_changed_count_, 0);
652 EXPECT_EQ(observer_->volume_changed_count_, 0);
654 media_endpoint_->SetConfiguration(
655 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
656 properties_);
658 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
659 EXPECT_EQ(callback_count_, 2);
660 EXPECT_EQ(error_callback_count_, 0);
661 EXPECT_EQ(observer_->state_changed_count_, 1);
662 EXPECT_EQ(observer_->volume_changed_count_, 1);
664 audio_sink_->Unregister(
665 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
666 base::Unretained(this)),
667 base::Bind(&BluetoothAudioSinkChromeOSTest::UnregisterErrorCallback,
668 base::Unretained(this)));
670 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
671 EXPECT_EQ(callback_count_, 3);
672 EXPECT_EQ(error_callback_count_, 0);
674 // The state becomes disconnted and then invalid, since the removal of
675 // transport object should happend before the unregistration of endpoint.
676 // State: STATE_IDLE -> STATE_DISCONNECTED -> STATE_INVALID
677 EXPECT_EQ(observer_->state_changed_count_, 3);
678 EXPECT_EQ(observer_->volume_changed_count_, 2);
681 TEST_F(BluetoothAudioSinkChromeOSTest, UnregisterAudioSinkDuringActiveState) {
682 GetAudioSink();
684 media_endpoint_->SelectConfiguration(
685 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
686 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
687 base::Unretained(this)));
689 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
690 EXPECT_EQ(callback_count_, 2);
691 EXPECT_EQ(error_callback_count_, 0);
692 EXPECT_EQ(observer_->state_changed_count_, 0);
693 EXPECT_EQ(observer_->volume_changed_count_, 0);
695 media_endpoint_->SetConfiguration(
696 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
697 properties_);
699 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
700 EXPECT_EQ(callback_count_, 2);
701 EXPECT_EQ(error_callback_count_, 0);
702 EXPECT_EQ(observer_->state_changed_count_, 1);
703 EXPECT_EQ(observer_->volume_changed_count_, 1);
705 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
707 message_loop_.RunUntilIdle();
709 // Acquire is called when the state of |audio_sink_| becomes STATE_PENDING,
710 // and Acquire will trigger state change. Therefore, the state will be
711 // STATE_ACTIVE right after STATE_PENDING.
712 // State: STATE_IDLE -> STATE_PENDING -> STATE_ACTIVE
713 EXPECT_EQ(observer_->state_changed_count_, 3);
715 audio_sink_->Unregister(
716 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
717 base::Unretained(this)),
718 base::Bind(&BluetoothAudioSinkChromeOSTest::UnregisterErrorCallback,
719 base::Unretained(this)));
721 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
722 EXPECT_EQ(callback_count_, 3);
723 EXPECT_EQ(error_callback_count_, 0);
724 EXPECT_EQ(observer_->state_changed_count_, 5);
725 EXPECT_EQ(observer_->volume_changed_count_, 2);
728 TEST_F(BluetoothAudioSinkChromeOSTest, StateChanged) {
729 GetAudioSink();
731 media_endpoint_->SelectConfiguration(
732 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
733 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
734 base::Unretained(this)));
736 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
737 EXPECT_EQ(callback_count_, 2);
738 EXPECT_EQ(error_callback_count_, 0);
739 EXPECT_EQ(observer_->state_changed_count_, 0);
740 EXPECT_EQ(observer_->volume_changed_count_, 0);
742 media_endpoint_->SetConfiguration(
743 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
744 properties_);
746 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
747 EXPECT_EQ(callback_count_, 2);
748 EXPECT_EQ(error_callback_count_, 0);
749 EXPECT_EQ(observer_->state_changed_count_, 1);
750 EXPECT_EQ(observer_->volume_changed_count_, 1);
752 // Changes the current state of transport to pending.
753 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
755 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_PENDING);
756 EXPECT_EQ(observer_->state_changed_count_, 3);
757 EXPECT_EQ(observer_->volume_changed_count_, 1);
760 TEST_F(BluetoothAudioSinkChromeOSTest, VolumeChanged) {
761 GetAudioSink();
763 media_endpoint_->SelectConfiguration(
764 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
765 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
766 base::Unretained(this)));
768 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
769 EXPECT_EQ(callback_count_, 2);
770 EXPECT_EQ(error_callback_count_, 0);
771 EXPECT_EQ(observer_->state_changed_count_, 0);
772 EXPECT_EQ(observer_->volume_changed_count_, 0);
774 media_endpoint_->SetConfiguration(
775 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
776 properties_);
778 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
779 EXPECT_EQ(callback_count_, 2);
780 EXPECT_EQ(error_callback_count_, 0);
781 EXPECT_EQ(observer_->state_changed_count_, 1);
782 EXPECT_EQ(observer_->volume_changed_count_, 1);
784 // |kTransportVolume| is the initial volume of the transport, and this
785 // value is propagated to the audio sink via SetConfiguration.
786 EXPECT_EQ(audio_sink_->GetVolume(),
787 FakeBluetoothMediaTransportClient::kTransportVolume);
789 // Changes volume to a valid level.
790 fake_transport_->SetVolume(media_endpoint_->object_path(), 100);
792 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
793 EXPECT_EQ(observer_->state_changed_count_, 1);
794 EXPECT_EQ(observer_->volume_changed_count_, 2);
795 EXPECT_EQ(audio_sink_->GetVolume(), 100);
797 // Changes volume to an invalid level.
798 fake_transport_->SetVolume(media_endpoint_->object_path(), 200);
800 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
801 EXPECT_EQ(observer_->state_changed_count_, 1);
802 EXPECT_EQ(observer_->volume_changed_count_, 3);
803 EXPECT_EQ(audio_sink_->GetVolume(), BluetoothAudioSink::kInvalidVolume);
806 TEST_F(BluetoothAudioSinkChromeOSTest, AcquireFD) {
807 GetAudioSink();
809 media_endpoint_->SelectConfiguration(
810 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
811 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
812 base::Unretained(this)));
814 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
815 EXPECT_EQ(callback_count_, 2);
816 EXPECT_EQ(error_callback_count_, 0);
817 EXPECT_EQ(observer_->state_changed_count_, 0);
818 EXPECT_EQ(observer_->volume_changed_count_, 0);
820 media_endpoint_->SetConfiguration(
821 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
822 properties_);
824 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
825 EXPECT_EQ(callback_count_, 2);
826 EXPECT_EQ(error_callback_count_, 0);
827 EXPECT_EQ(observer_->state_changed_count_, 1);
828 EXPECT_EQ(observer_->volume_changed_count_, 1);
830 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
832 std::vector<char> data_one(16, 0x12);
833 fake_transport_->WriteData(media_endpoint_->object_path(), data_one);
835 message_loop_.RunUntilIdle();
837 // Acquire is called when the state of |audio_sink_| becomes STATE_PENDING,
838 // and Acquire will trigger state change. Therefore, the state will be
839 // STATE_ACTIVE right after STATE_PENDING.
840 // State: STATE_IDLE -> STATE_PENDING -> STATE_ACTIVE
841 EXPECT_EQ(observer_->state_changed_count_, 3);
842 EXPECT_EQ(observer_->total_read_, data_one.size());
843 EXPECT_EQ(observer_->data_, data_one);
844 EXPECT_EQ(observer_->read_mtu_,
845 FakeBluetoothMediaTransportClient::kDefaultReadMtu);
848 // Tests the case where the remote device pauses and resume audio streaming.
849 TEST_F(BluetoothAudioSinkChromeOSTest, PauseAndResume) {
850 GetAudioSink();
852 media_endpoint_->SelectConfiguration(
853 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
854 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
855 base::Unretained(this)));
857 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
858 EXPECT_EQ(callback_count_, 2);
859 EXPECT_EQ(error_callback_count_, 0);
860 EXPECT_EQ(observer_->state_changed_count_, 0);
861 EXPECT_EQ(observer_->volume_changed_count_, 0);
863 media_endpoint_->SetConfiguration(
864 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
865 properties_);
867 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
868 EXPECT_EQ(callback_count_, 2);
869 EXPECT_EQ(error_callback_count_, 0);
870 EXPECT_EQ(observer_->state_changed_count_, 1);
871 EXPECT_EQ(observer_->volume_changed_count_, 1);
873 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
875 std::vector<char> data_one(16, 0x12);
876 fake_transport_->WriteData(media_endpoint_->object_path(), data_one);
878 message_loop_.RunUntilIdle();
880 EXPECT_EQ(observer_->data_, data_one);
881 EXPECT_EQ(observer_->read_mtu_,
882 FakeBluetoothMediaTransportClient::kDefaultReadMtu);
883 EXPECT_EQ(observer_->state_changed_count_, 3);
884 EXPECT_EQ(observer_->total_read_, data_one.size());
886 // Simulates the situation where the remote device pauses and resume audio
887 // streaming.
888 fake_transport_->SetState(media_endpoint_->object_path(), "idle");
890 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
891 EXPECT_EQ(observer_->state_changed_count_, 4);
893 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
895 std::vector<char> data_two(8, 0x10);
896 fake_transport_->WriteData(media_endpoint_->object_path(), data_two);
898 message_loop_.RunUntilIdle();
900 EXPECT_EQ(observer_->data_, data_two);
901 EXPECT_EQ(observer_->read_mtu_,
902 FakeBluetoothMediaTransportClient::kDefaultReadMtu);
903 EXPECT_EQ(observer_->state_changed_count_, 6);
904 EXPECT_EQ(observer_->total_read_, data_two.size());
907 TEST_F(BluetoothAudioSinkChromeOSTest, ContinuouslyStreaming) {
908 GetAudioSink();
910 media_endpoint_->SelectConfiguration(
911 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
912 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
913 base::Unretained(this)));
915 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
916 EXPECT_EQ(callback_count_, 2);
917 EXPECT_EQ(error_callback_count_, 0);
918 EXPECT_EQ(observer_->state_changed_count_, 0);
919 EXPECT_EQ(observer_->volume_changed_count_, 0);
921 media_endpoint_->SetConfiguration(
922 fake_transport_->GetTransportPath(media_endpoint_->object_path()),
923 properties_);
925 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
926 EXPECT_EQ(callback_count_, 2);
927 EXPECT_EQ(error_callback_count_, 0);
928 EXPECT_EQ(observer_->state_changed_count_, 1);
929 EXPECT_EQ(observer_->volume_changed_count_, 1);
931 fake_transport_->SetState(media_endpoint_->object_path(), "pending");
933 std::vector<char> data_one(16, 0x12);
934 fake_transport_->WriteData(media_endpoint_->object_path(), data_one);
936 message_loop_.RunUntilIdle();
938 EXPECT_EQ(observer_->data_, data_one);
939 EXPECT_EQ(observer_->read_mtu_,
940 FakeBluetoothMediaTransportClient::kDefaultReadMtu);
941 EXPECT_EQ(observer_->state_changed_count_, 3);
942 EXPECT_EQ(observer_->total_read_, data_one.size());
944 std::vector<char> data_two(8, 0x10);
945 fake_transport_->WriteData(media_endpoint_->object_path(), data_two);
947 message_loop_.RunUntilIdle();
949 EXPECT_EQ(observer_->data_, data_two);
950 EXPECT_EQ(observer_->read_mtu_,
951 FakeBluetoothMediaTransportClient::kDefaultReadMtu);
952 EXPECT_EQ(observer_->state_changed_count_, 3);
953 EXPECT_EQ(observer_->total_read_, data_one.size() + data_two.size());
956 } // namespace chromeos