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

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

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

Powered by Google App Engine
This is Rietveld 408576698