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

Side by Side Diff: device/bluetooth/bluetooth_audio_sink_chromeos_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698