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

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

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

Powered by Google App Engine
This is Rietveld 408576698