PlzNavigate: send history params at commit time to the renderer
[chromium-blink-merge.git] / device / bluetooth / bluetooth_audio_sink_chromeos_unittest.cc
blobee6cf2ab8c1197b95468cec0fad9d883a36c7e41
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 state_(audio_sink->GetState()),
38 audio_sink_(audio_sink) {
39 audio_sink_->AddObserver(this);
42 ~TestAudioSinkObserver() override { audio_sink_->RemoveObserver(this); }
44 void BluetoothAudioSinkStateChanged(
45 BluetoothAudioSink* audio_sink,
46 BluetoothAudioSink::State state) override {
47 ++state_changed_count_;
50 void BluetoothAudioSinkVolumeChanged(BluetoothAudioSink* audio_sink,
51 uint16_t volume) override {
52 ++volume_changed_count_;
55 int state_changed_count_;
56 int volume_changed_count_;
57 BluetoothAudioSink::State state_;
59 private:
60 scoped_refptr<BluetoothAudioSink> audio_sink_;
63 class BluetoothAudioSinkChromeOSTest : public testing::Test {
64 public:
65 void SetUp() override {
66 DBusThreadManager::Initialize();
68 callback_count_ = 0;
69 error_callback_count_ = 0;
71 // Initiates Delegate::TransportProperties with default values.
72 properties_.device =
73 ObjectPath(FakeBluetoothMediaTransportClient::kTransportDevicePath);
74 properties_.uuid = BluetoothMediaClient::kBluetoothAudioSinkUUID;
75 properties_.codec = FakeBluetoothMediaTransportClient::kTransportCodec;
76 properties_.configuration =
77 FakeBluetoothMediaTransportClient::kTransportConfiguration;
78 properties_.state = BluetoothMediaTransportClient::kStateIdle;
79 properties_.delay.reset(
80 new uint16_t(FakeBluetoothMediaTransportClient::kTransportDelay));
81 properties_.volume.reset(
82 new uint16_t(FakeBluetoothMediaTransportClient::kTransportVolume));
84 media_ = static_cast<FakeBluetoothMediaClient*>(
85 DBusThreadManager::Get()->GetBluetoothMediaClient());
86 transport_ = static_cast<FakeBluetoothMediaTransportClient*>(
87 DBusThreadManager::Get()->GetBluetoothMediaTransportClient());
89 GetAdapter();
92 void TearDown() override {
93 callback_count_ = 0;
94 error_callback_count_ = 0;
95 observer_.reset();
97 media_->SetVisible(true);
99 // The adapter should outlive audio sink.
100 audio_sink_ = nullptr;
101 adapter_ = nullptr;
102 DBusThreadManager::Shutdown();
105 // Gets the existing Bluetooth adapter.
106 void GetAdapter() {
107 BluetoothAdapterFactory::GetAdapter(
108 base::Bind(&BluetoothAudioSinkChromeOSTest::GetAdapterCallback,
109 base::Unretained(this)));
112 // Called whenever BluetoothAdapter is retrieved successfully.
113 void GetAdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
114 adapter_ = adapter;
116 ASSERT_NE(adapter_.get(), nullptr);
117 ASSERT_TRUE(adapter_->IsInitialized());
118 adapter_->SetPowered(
119 true,
120 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
121 base::Unretained(this)),
122 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback,
123 base::Unretained(this)));
124 ASSERT_TRUE(adapter_->IsPresent());
125 ASSERT_TRUE(adapter_->IsPowered());
126 EXPECT_EQ(callback_count_, 1);
127 EXPECT_EQ(error_callback_count_, 0);
129 // Resets callback_count_.
130 --callback_count_;
133 // Registers BluetoothAudioSinkChromeOS with default codec and capabilities.
134 // If the audio sink is retrieved successfully, the state changes to
135 // STATE_DISCONNECTED.
136 void GetAudioSink() {
137 // Sets up valid codec and capabilities.
138 BluetoothAudioSink::Options options;
139 ASSERT_EQ(options.codec, 0x00);
140 ASSERT_EQ(options.capabilities,
141 std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35}));
143 // Registers |audio_sink_| with valid codec and capabilities
144 adapter_->RegisterAudioSink(
145 options,
146 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback,
147 base::Unretained(this)),
148 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback,
149 base::Unretained(this)));
151 observer_.reset(new TestAudioSinkObserver(audio_sink_));
152 EXPECT_EQ(callback_count_, 1);
153 EXPECT_EQ(error_callback_count_, 0);
154 EXPECT_EQ(observer_->state_changed_count_, 0);
155 EXPECT_EQ(observer_->volume_changed_count_, 0);
158 void GetFakeMediaEndpoint() {
159 BluetoothAudioSinkChromeOS* audio_sink_chromeos =
160 static_cast<BluetoothAudioSinkChromeOS*>(audio_sink_.get());
161 ASSERT_NE(audio_sink_chromeos, nullptr);
163 media_endpoint_ = static_cast<FakeBluetoothMediaEndpointServiceProvider*>(
164 audio_sink_chromeos->GetEndpointServiceProvider());
167 // Called whenever RegisterAudioSink is completed successfully.
168 void RegisterCallback(
169 scoped_refptr<BluetoothAudioSink> audio_sink) {
170 ++callback_count_;
171 audio_sink_ = audio_sink;
173 GetFakeMediaEndpoint();
174 ASSERT_NE(media_endpoint_, nullptr);
175 media_->SetEndpointRegistered(media_endpoint_, true);
177 ASSERT_NE(audio_sink_.get(), nullptr);
178 ASSERT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
181 // Called whenever RegisterAudioSink failed.
182 void RegisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) {
183 ++error_callback_count_;
184 EXPECT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_REGISTERED);
187 // Called whenever there capabilities are returned from SelectConfiguration.
188 void SelectConfigurationCallback(const std::vector<uint8_t>& capabilities) {
189 ++callback_count_;
191 // |capabilities| should be the same as the capabilities for registering an
192 // audio sink in GetAudioSink().
193 EXPECT_EQ(capabilities, std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35}));
196 void UnregisterErrorCallback(BluetoothAudioSink::ErrorCode error_code) {
197 ++error_callback_count_;
198 EXPECT_EQ(error_code, BluetoothAudioSink::ERROR_NOT_UNREGISTERED);
201 // Generic callbacks.
202 void Callback() {
203 ++callback_count_;
206 void ErrorCallback() {
207 ++error_callback_count_;
210 protected:
211 int callback_count_;
212 int error_callback_count_;
214 base::MessageLoop message_loop_;
216 FakeBluetoothMediaClient* media_;
217 FakeBluetoothMediaTransportClient* transport_;
218 FakeBluetoothMediaEndpointServiceProvider* media_endpoint_;
219 scoped_ptr<TestAudioSinkObserver> observer_;
220 scoped_refptr<BluetoothAdapter> adapter_;
221 scoped_refptr<BluetoothAudioSink> audio_sink_;
223 // The default property set used while calling SetConfiguration on a media
224 // endpoint object.
225 BluetoothMediaEndpointServiceProvider::Delegate::TransportProperties
226 properties_;
229 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterSucceeded) {
230 GetAudioSink();
233 TEST_F(BluetoothAudioSinkChromeOSTest, RegisterFailedWithInvalidOptions) {
234 // Sets options with an invalid codec and valid capabilities.
235 BluetoothAudioSink::Options options;
236 options.codec = 0xff;
237 options.capabilities = std::vector<uint8_t>({0x3f, 0xff, 0x12, 0x35});
239 adapter_->RegisterAudioSink(
240 options,
241 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback,
242 base::Unretained(this)),
243 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback,
244 base::Unretained(this)));
246 EXPECT_EQ(callback_count_, 0);
247 EXPECT_EQ(error_callback_count_, 1);
249 // Sets options with a valid codec and invalid capabilities.
250 options.codec = 0x00;
251 options.capabilities.clear();
252 adapter_->RegisterAudioSink(
253 options,
254 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterCallback,
255 base::Unretained(this)),
256 base::Bind(&BluetoothAudioSinkChromeOSTest::RegisterErrorCallback,
257 base::Unretained(this)));
259 EXPECT_EQ(callback_count_, 0);
260 EXPECT_EQ(error_callback_count_, 2);
263 TEST_F(BluetoothAudioSinkChromeOSTest, SelectConfiguration) {
264 GetAudioSink();
266 // Simulates calling SelectConfiguration on the media endpoint object owned by
267 // |audio_sink_| with some fake capabilities.
268 media_endpoint_->SelectConfiguration(
269 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
270 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
271 base::Unretained(this)));
273 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
274 EXPECT_EQ(callback_count_, 2);
275 EXPECT_EQ(error_callback_count_, 0);
276 EXPECT_EQ(observer_->state_changed_count_, 0);
277 EXPECT_EQ(observer_->volume_changed_count_, 0);
280 TEST_F(BluetoothAudioSinkChromeOSTest, SetConfiguration) {
281 GetAudioSink();
283 media_endpoint_->SelectConfiguration(
284 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
285 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
286 base::Unretained(this)));
288 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
289 EXPECT_EQ(callback_count_, 2);
290 EXPECT_EQ(error_callback_count_, 0);
291 EXPECT_EQ(observer_->state_changed_count_, 0);
292 EXPECT_EQ(observer_->volume_changed_count_, 0);
294 // Simulates calling SetConfiguration on the media endpoint object owned by
295 // |audio_sink_| with a fake transport path and a
296 // Delegate::TransportProperties structure.
297 media_endpoint_->SetConfiguration(
298 transport_->GetTransportPath(media_endpoint_->object_path()),
299 properties_);
301 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
302 EXPECT_EQ(callback_count_, 2);
303 EXPECT_EQ(error_callback_count_, 0);
304 EXPECT_EQ(observer_->state_changed_count_, 1);
305 EXPECT_EQ(observer_->volume_changed_count_, 1);
308 TEST_F(BluetoothAudioSinkChromeOSTest, SetConfigurationWithUnexpectedState) {
309 GetAudioSink();
311 media_endpoint_->SelectConfiguration(
312 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
313 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
314 base::Unretained(this)));
316 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
317 EXPECT_EQ(callback_count_, 2);
318 EXPECT_EQ(error_callback_count_, 0);
319 EXPECT_EQ(observer_->state_changed_count_, 0);
320 EXPECT_EQ(observer_->volume_changed_count_, 0);
322 // Set state of Delegate::TransportProperties with an unexpected value.
323 properties_.state = "pending";
325 media_endpoint_->SetConfiguration(
326 transport_->GetTransportPath(media_endpoint_->object_path()),
327 properties_);
329 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
330 EXPECT_EQ(callback_count_, 2);
331 EXPECT_EQ(error_callback_count_, 0);
332 EXPECT_EQ(observer_->state_changed_count_, 0);
333 EXPECT_EQ(observer_->volume_changed_count_, 0);
336 // TODO(mcchou): Adds test on media-removed events for STATE_PENDING and
337 // STATE_ACTIVE.
338 // Checks if the observer is notified on media-removed event when the state of
339 // |audio_sink_| is STATE_DISCONNECTED. Once the media object is removed, the
340 // audio sink is no longer valid.
341 TEST_F(BluetoothAudioSinkChromeOSTest, MediaRemovedDuringDisconnectedState) {
342 GetAudioSink();
344 // Gets the media object and makes it invisible to see if the state of the
345 // audio sink changes accordingly.
346 media_->SetVisible(false);
348 GetFakeMediaEndpoint();
350 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
351 EXPECT_EQ(media_endpoint_, nullptr);
352 EXPECT_EQ(observer_->state_changed_count_, 1);
353 EXPECT_EQ(observer_->volume_changed_count_, 0);
356 // Checks if the observer is notified on media-removed event when the state of
357 // |audio_sink_| is STATE_IDLE. Once the media object is removed, the audio sink
358 // is no longer valid.
359 TEST_F(BluetoothAudioSinkChromeOSTest, MediaRemovedDuringIdleState) {
360 GetAudioSink();
362 media_endpoint_->SelectConfiguration(
363 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
364 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
365 base::Unretained(this)));
367 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
368 EXPECT_EQ(callback_count_, 2);
369 EXPECT_EQ(error_callback_count_, 0);
370 EXPECT_EQ(observer_->state_changed_count_, 0);
371 EXPECT_EQ(observer_->volume_changed_count_, 0);
373 media_endpoint_->SetConfiguration(
374 transport_->GetTransportPath(media_endpoint_->object_path()),
375 properties_);
377 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
378 EXPECT_EQ(callback_count_, 2);
379 EXPECT_EQ(error_callback_count_, 0);
380 EXPECT_EQ(observer_->state_changed_count_, 1);
381 EXPECT_EQ(observer_->volume_changed_count_, 1);
383 // Gets the media object and makes it invisible to see if the state of the
384 // audio sink changes accordingly.
385 media_->SetVisible(false);
387 GetFakeMediaEndpoint();
389 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
390 EXPECT_EQ(media_endpoint_, nullptr);
392 // The state becomes disconnted and then invalid, since the removal of
393 // transport object should happend before media becomes invisible.
394 // State: STATE_IDLE -> STATE_DISCONNECTED -> STATE_INVALID
395 EXPECT_EQ(observer_->state_changed_count_, 3);
396 EXPECT_EQ(observer_->volume_changed_count_, 2);
399 // TODO(mcchou): Add tests on transport-removed event for STATE_PENDING and
400 // STATE_ACTIVE.
401 // Checks if the observer is notified on transport-removed event when the state
402 // of |audio_sink_| is STATE_IDEL. Once the media transport object is removed,
403 // the audio sink is disconnected.
404 TEST_F(BluetoothAudioSinkChromeOSTest, TransportRemovedDuringIdleState) {
405 GetAudioSink();
407 media_endpoint_->SelectConfiguration(
408 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
409 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
410 base::Unretained(this)));
412 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
413 EXPECT_EQ(callback_count_, 2);
414 EXPECT_EQ(error_callback_count_, 0);
415 EXPECT_EQ(observer_->state_changed_count_, 0);
416 EXPECT_EQ(observer_->volume_changed_count_, 0);
418 media_endpoint_->SetConfiguration(
419 transport_->GetTransportPath(media_endpoint_->object_path()),
420 properties_);
422 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
423 EXPECT_EQ(callback_count_, 2);
424 EXPECT_EQ(error_callback_count_, 0);
425 EXPECT_EQ(observer_->state_changed_count_, 1);
426 EXPECT_EQ(observer_->volume_changed_count_, 1);
428 // Makes the transport object invalid to see if the state of the audio sink
429 // changes accordingly.
430 transport_->SetValid(media_endpoint_, false);
432 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
433 EXPECT_NE(media_endpoint_, nullptr);
434 EXPECT_EQ(observer_->state_changed_count_, 2);
435 EXPECT_EQ(observer_->volume_changed_count_, 2);
438 TEST_F(BluetoothAudioSinkChromeOSTest,
439 AdapterPoweredChangedDuringDisconnectedState) {
440 GetAudioSink();
442 adapter_->SetPowered(
443 false,
444 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
445 base::Unretained(this)),
446 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback,
447 base::Unretained(this)));
449 EXPECT_TRUE(adapter_->IsPresent());
450 EXPECT_FALSE(adapter_->IsPowered());
451 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
452 EXPECT_EQ(callback_count_, 2);
453 EXPECT_EQ(error_callback_count_, 0);
454 EXPECT_EQ(observer_->state_changed_count_, 0);
455 EXPECT_EQ(observer_->volume_changed_count_, 0);
457 adapter_->SetPowered(
458 true,
459 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
460 base::Unretained(this)),
461 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback,
462 base::Unretained(this)));
464 EXPECT_TRUE(adapter_->IsPresent());
465 EXPECT_TRUE(adapter_->IsPowered());
466 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
467 EXPECT_EQ(callback_count_, 3);
468 EXPECT_EQ(error_callback_count_, 0);
469 EXPECT_EQ(observer_->state_changed_count_, 0);
470 EXPECT_EQ(observer_->volume_changed_count_, 0);
473 TEST_F(BluetoothAudioSinkChromeOSTest, AdapterPoweredChangedDuringIdleState) {
474 GetAudioSink();
476 media_endpoint_->SelectConfiguration(
477 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
478 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
479 base::Unretained(this)));
481 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
482 EXPECT_EQ(callback_count_, 2);
483 EXPECT_EQ(error_callback_count_, 0);
484 EXPECT_EQ(observer_->state_changed_count_, 0);
485 EXPECT_EQ(observer_->volume_changed_count_, 0);
487 media_endpoint_->SetConfiguration(
488 transport_->GetTransportPath(media_endpoint_->object_path()),
489 properties_);
491 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
492 EXPECT_EQ(callback_count_, 2);
493 EXPECT_EQ(error_callback_count_, 0);
494 EXPECT_EQ(observer_->state_changed_count_, 1);
495 EXPECT_EQ(observer_->volume_changed_count_, 1);
497 adapter_->SetPowered(
498 false,
499 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
500 base::Unretained(this)),
501 base::Bind(&BluetoothAudioSinkChromeOSTest::ErrorCallback,
502 base::Unretained(this)));
503 GetFakeMediaEndpoint();
505 EXPECT_TRUE(adapter_->IsPresent());
506 EXPECT_FALSE(adapter_->IsPowered());
507 EXPECT_NE(media_endpoint_, nullptr);
508 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
509 EXPECT_EQ(callback_count_, 3);
510 EXPECT_EQ(error_callback_count_, 0);
511 EXPECT_EQ(observer_->state_changed_count_, 2);
512 EXPECT_EQ(observer_->volume_changed_count_, 2);
515 // TODO(mcchou): Add tests on UnregisterAudioSink for STATE_PENDING and
516 // STATE_ACTIVE.
518 TEST_F(BluetoothAudioSinkChromeOSTest,
519 UnregisterAudioSinkDuringDisconnectedState) {
520 GetAudioSink();
522 audio_sink_->Unregister(
523 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
524 base::Unretained(this)),
525 base::Bind(&BluetoothAudioSinkChromeOSTest::UnregisterErrorCallback,
526 base::Unretained(this)));
528 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
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_, 0);
535 TEST_F(BluetoothAudioSinkChromeOSTest, UnregisterAudioSinkDuringIdleState) {
536 GetAudioSink();
538 media_endpoint_->SelectConfiguration(
539 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
540 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
541 base::Unretained(this)));
543 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
544 EXPECT_EQ(callback_count_, 2);
545 EXPECT_EQ(error_callback_count_, 0);
546 EXPECT_EQ(observer_->state_changed_count_, 0);
547 EXPECT_EQ(observer_->volume_changed_count_, 0);
549 media_endpoint_->SetConfiguration(
550 transport_->GetTransportPath(media_endpoint_->object_path()),
551 properties_);
553 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
554 EXPECT_EQ(callback_count_, 2);
555 EXPECT_EQ(error_callback_count_, 0);
556 EXPECT_EQ(observer_->state_changed_count_, 1);
557 EXPECT_EQ(observer_->volume_changed_count_, 1);
559 audio_sink_->Unregister(
560 base::Bind(&BluetoothAudioSinkChromeOSTest::Callback,
561 base::Unretained(this)),
562 base::Bind(&BluetoothAudioSinkChromeOSTest::UnregisterErrorCallback,
563 base::Unretained(this)));
565 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_INVALID);
566 EXPECT_EQ(callback_count_, 3);
567 EXPECT_EQ(error_callback_count_, 0);
569 // The state becomes disconnted and then invalid, since the removal of
570 // transport object should happend before the unregistration of endpoint.
571 // State: STATE_IDLE -> STATE_DISCONNECTED -> STATE_INVALID
572 EXPECT_EQ(observer_->state_changed_count_, 3);
573 EXPECT_EQ(observer_->volume_changed_count_, 2);
576 TEST_F(BluetoothAudioSinkChromeOSTest, StateChanged) {
577 GetAudioSink();
579 media_endpoint_->SelectConfiguration(
580 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
581 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
582 base::Unretained(this)));
584 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
585 EXPECT_EQ(callback_count_, 2);
586 EXPECT_EQ(error_callback_count_, 0);
587 EXPECT_EQ(observer_->state_changed_count_, 0);
588 EXPECT_EQ(observer_->volume_changed_count_, 0);
590 media_endpoint_->SetConfiguration(
591 transport_->GetTransportPath(media_endpoint_->object_path()),
592 properties_);
594 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
595 EXPECT_EQ(callback_count_, 2);
596 EXPECT_EQ(error_callback_count_, 0);
597 EXPECT_EQ(observer_->state_changed_count_, 1);
598 EXPECT_EQ(observer_->volume_changed_count_, 1);
600 // Changes the current state of transport to pending.
601 transport_->SetState(media_endpoint_->object_path(), "pending");
603 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_PENDING);
604 EXPECT_EQ(observer_->state_changed_count_, 2);
605 EXPECT_EQ(observer_->volume_changed_count_, 1);
608 TEST_F(BluetoothAudioSinkChromeOSTest, VolumeChanged) {
609 GetAudioSink();
611 media_endpoint_->SelectConfiguration(
612 std::vector<uint8_t>({0x21, 0x15, 0x33, 0x2C}),
613 base::Bind(&BluetoothAudioSinkChromeOSTest::SelectConfigurationCallback,
614 base::Unretained(this)));
616 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_DISCONNECTED);
617 EXPECT_EQ(callback_count_, 2);
618 EXPECT_EQ(error_callback_count_, 0);
619 EXPECT_EQ(observer_->state_changed_count_, 0);
620 EXPECT_EQ(observer_->volume_changed_count_, 0);
622 media_endpoint_->SetConfiguration(
623 transport_->GetTransportPath(media_endpoint_->object_path()),
624 properties_);
626 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
627 EXPECT_EQ(callback_count_, 2);
628 EXPECT_EQ(error_callback_count_, 0);
629 EXPECT_EQ(observer_->state_changed_count_, 1);
630 EXPECT_EQ(observer_->volume_changed_count_, 1);
632 // |kTransportVolume| is the initial volume of the transport, and this
633 // value is propagated to the audio sink via SetConfiguration.
634 EXPECT_EQ(audio_sink_->GetVolume(),
635 FakeBluetoothMediaTransportClient::kTransportVolume);
637 // Changes volume to a valid level.
638 transport_->SetVolume(media_endpoint_->object_path(), 100);
640 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
641 EXPECT_EQ(observer_->state_changed_count_, 1);
642 EXPECT_EQ(observer_->volume_changed_count_, 2);
643 EXPECT_EQ(audio_sink_->GetVolume(), 100);
645 // Changes volume to an invalid level.
646 transport_->SetVolume(media_endpoint_->object_path(), 200);
648 EXPECT_EQ(audio_sink_->GetState(), BluetoothAudioSink::STATE_IDLE);
649 EXPECT_EQ(observer_->state_changed_count_, 1);
650 EXPECT_EQ(observer_->volume_changed_count_, 3);
651 EXPECT_EQ(audio_sink_->GetVolume(), BluetoothAudioSink::kInvalidVolume);
654 } // namespace chromeos