Create a new-installs-only uniformity trial.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_chromeos_unittest.cc
blob890d24141246720297b4b83f7478d0d02b23ea9a
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/dbus/mock_bluetooth_adapter_client.h"
6 #include "chromeos/dbus/mock_bluetooth_manager_client.h"
7 #include "chromeos/dbus/mock_dbus_thread_manager.h"
8 #include "dbus/object_path.h"
9 #include "device/bluetooth/bluetooth_adapter.h"
10 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
11 #include "device/bluetooth/bluetooth_adapter_factory.h"
12 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using device::BluetoothAdapter;
16 using device::BluetoothAdapterFactory;
17 using device::MockBluetoothAdapter;
18 using ::testing::_;
19 using ::testing::InSequence;
20 using ::testing::Return;
21 using ::testing::SaveArg;
23 namespace chromeos {
25 class BluetoothAdapterChromeOsTest : public testing::Test {
26 public:
27 virtual void SetUp() {
28 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager;
30 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus())
31 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL)));
32 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager);
34 mock_manager_client_ =
35 mock_dbus_thread_manager->mock_bluetooth_manager_client();
36 mock_adapter_client_ =
37 mock_dbus_thread_manager->mock_bluetooth_adapter_client();
39 set_callback_called_ = false;
40 error_callback_called_ = false;
43 virtual void TearDown() {
44 DBusThreadManager::Shutdown();
47 void SetCallback() {
48 set_callback_called_ = true;
51 void ErrorCallback() {
52 error_callback_called_ = true;
55 protected:
56 MockBluetoothManagerClient* mock_manager_client_;
57 MockBluetoothAdapterClient* mock_adapter_client_;
59 bool set_callback_called_;
60 bool error_callback_called_;
63 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterNotPresent) {
64 // Create the default adapter instance;
65 // BluetoothManagerClient::DefaultAdapter will be called once, passing
66 // a callback to obtain the adapter path.
67 BluetoothManagerClient::AdapterCallback adapter_callback;
68 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
69 .WillOnce(SaveArg<0>(&adapter_callback));
71 scoped_refptr<BluetoothAdapter> adapter =
72 BluetoothAdapterFactory::DefaultAdapter();
73 ASSERT_TRUE(adapter.get() != NULL);
75 // Call the adapter callback; make out it failed.
76 // BluetoothAdapter::Observer::AdapterPresentChanged must not be called.
77 MockBluetoothAdapter::Observer adapter_observer;
78 adapter->AddObserver(&adapter_observer);
80 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), _))
81 .Times(0);
83 adapter_callback.Run(dbus::ObjectPath(""), false);
85 // Adapter should not be present.
86 EXPECT_FALSE(adapter->IsPresent());
89 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterWithAddress) {
90 const dbus::ObjectPath adapter_path("/fake/hci0");
91 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
93 // Create the default adapter instance;
94 // BluetoothManagerClient::DefaultAdapter will be called once, passing
95 // a callback to obtain the adapter path.
96 BluetoothManagerClient::AdapterCallback adapter_callback;
97 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
98 .WillOnce(SaveArg<0>(&adapter_callback));
100 scoped_refptr<BluetoothAdapter> adapter =
101 BluetoothAdapterFactory::DefaultAdapter();
103 // Call the adapter callback;
104 // BluetoothAdapterClient::GetProperties will be called once to obtain
105 // the property set.
106 MockBluetoothAdapterClient::Properties adapter_properties;
107 adapter_properties.address.ReplaceValue(adapter_address);
109 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
110 .WillRepeatedly(Return(&adapter_properties));
112 // BluetoothAdapter::Observer::AdapterPresentChanged will be called to
113 // indicate the adapter is now present.
114 MockBluetoothAdapter::Observer adapter_observer;
115 adapter->AddObserver(&adapter_observer);
117 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
118 .Times(1);
120 adapter_callback.Run(adapter_path, true);
122 // Adapter should be present with the given address.
123 EXPECT_TRUE(adapter->IsPresent());
124 EXPECT_EQ(adapter_address, adapter->address());
127 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterWithoutAddress) {
128 const dbus::ObjectPath adapter_path("/fake/hci0");
129 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
131 // Create the default adapter instance;
132 // BluetoothManagerClient::DefaultAdapter will be called once, passing
133 // a callback to obtain the adapter path.
134 BluetoothManagerClient::AdapterCallback adapter_callback;
135 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
136 .WillOnce(SaveArg<0>(&adapter_callback));
138 scoped_refptr<BluetoothAdapter> adapter =
139 BluetoothAdapterFactory::DefaultAdapter();
141 // Call the adapter callback;
142 // BluetoothAdapterClient::GetProperties will be called once to obtain
143 // the property set.
144 MockBluetoothAdapterClient::Properties adapter_properties;
146 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
147 .WillRepeatedly(Return(&adapter_properties));
149 // BluetoothAdapter::Observer::AdapterPresentChanged must not be called
150 // yet.
151 MockBluetoothAdapter::Observer adapter_observer;
152 adapter->AddObserver(&adapter_observer);
154 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), _))
155 .Times(0);
157 adapter_callback.Run(adapter_path, true);
159 // Adapter should not be present yet.
160 EXPECT_FALSE(adapter->IsPresent());
162 // Tell the adapter the address now;
163 // BluetoothAdapter::Observer::AdapterPresentChanged now must be called.
164 adapter_properties.address.ReplaceValue(adapter_address);
166 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
167 .Times(1);
169 BluetoothAdapterChromeOs* adapter_chromeos =
170 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
172 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
173 ->AdapterPropertyChanged(adapter_path,
174 adapter_properties.address.name());
176 // Adapter should be present with the given address.
177 EXPECT_TRUE(adapter->IsPresent());
178 EXPECT_EQ(adapter_address, adapter->address());
181 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterBecomesPresentWithAddress) {
182 const dbus::ObjectPath adapter_path("/fake/hci0");
183 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
185 // Create the default adapter instance;
186 // BluetoothManagerClient::DefaultAdapter will be called once, passing
187 // a callback to obtain the adapter path.
188 BluetoothManagerClient::AdapterCallback adapter_callback;
189 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
190 .WillOnce(SaveArg<0>(&adapter_callback));
192 scoped_refptr<BluetoothAdapter> adapter =
193 BluetoothAdapterFactory::DefaultAdapter();
195 // Call the adapter callback; make out it failed.
196 adapter_callback.Run(dbus::ObjectPath(""), false);
198 // Tell the adapter the default adapter changed;
199 // BluetoothAdapterClient::GetProperties will be called once to obtain
200 // the property set.
201 MockBluetoothAdapterClient::Properties adapter_properties;
202 adapter_properties.address.ReplaceValue(adapter_address);
204 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
205 .WillRepeatedly(Return(&adapter_properties));
207 // BluetoothAdapter::Observer::AdapterPresentChanged must be called.
208 MockBluetoothAdapter::Observer adapter_observer;
209 adapter->AddObserver(&adapter_observer);
211 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
212 .Times(1);
214 BluetoothAdapterChromeOs* adapter_chromeos =
215 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
217 static_cast<BluetoothManagerClient::Observer*>(adapter_chromeos)
218 ->DefaultAdapterChanged(adapter_path);
220 // Adapter should be present with the new address.
221 EXPECT_TRUE(adapter->IsPresent());
222 EXPECT_EQ(adapter_address, adapter->address());
225 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterReplacedWithAddress) {
226 const dbus::ObjectPath initial_adapter_path("/fake/hci0");
227 const dbus::ObjectPath new_adapter_path("/fake/hci1");
228 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE";
229 const std::string new_adapter_address = "BA:C0:11:CO:FE:FE";
231 // Create the default adapter instance;
232 // BluetoothManagerClient::DefaultAdapter will be called once, passing
233 // a callback to obtain the adapter path.
234 BluetoothManagerClient::AdapterCallback adapter_callback;
235 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
236 .WillOnce(SaveArg<0>(&adapter_callback));
238 scoped_refptr<BluetoothAdapter> adapter =
239 BluetoothAdapterFactory::DefaultAdapter();
241 // Call the adapter callback;
242 // BluetoothAdapterClient::GetProperties will be called once to obtain
243 // the property set.
244 MockBluetoothAdapterClient::Properties initial_adapter_properties;
245 initial_adapter_properties.address.ReplaceValue(initial_adapter_address);
247 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path))
248 .WillRepeatedly(Return(&initial_adapter_properties));
250 adapter_callback.Run(initial_adapter_path, true);
252 // Tell the adapter the default adapter changed;
253 // BluetoothAdapterClient::GetProperties will be called once to obtain
254 // the property set.
255 MockBluetoothAdapterClient::Properties new_adapter_properties;
256 new_adapter_properties.address.ReplaceValue(new_adapter_address);
258 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path))
259 .WillRepeatedly(Return(&new_adapter_properties));
261 // BluetoothAdapter::Observer::AdapterPresentChanged must be called once
262 // with false to indicate the old adapter being removed and once with true
263 // to announce the new adapter.
264 MockBluetoothAdapter::Observer adapter_observer;
265 adapter->AddObserver(&adapter_observer);
267 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false))
268 .Times(1);
269 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
270 .Times(1);
272 BluetoothAdapterChromeOs* adapter_chromeos =
273 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
275 static_cast<BluetoothManagerClient::Observer*>(adapter_chromeos)
276 ->DefaultAdapterChanged(new_adapter_path);
278 // Adapter should be present with the new address.
279 EXPECT_TRUE(adapter->IsPresent());
280 EXPECT_EQ(new_adapter_address, adapter->address());
283 TEST_F(BluetoothAdapterChromeOsTest,
284 DefaultAdapterBecomesPresentWithoutAddress) {
285 const dbus::ObjectPath adapter_path("/fake/hci0");
286 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
288 // Create the default adapter instance;
289 // BluetoothManagerClient::DefaultAdapter will be called once, passing
290 // a callback to obtain the adapter path.
291 BluetoothManagerClient::AdapterCallback adapter_callback;
292 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
293 .WillOnce(SaveArg<0>(&adapter_callback));
295 scoped_refptr<BluetoothAdapter> adapter =
296 BluetoothAdapterFactory::DefaultAdapter();
298 // Call the adapter callback; make out it failed.
299 adapter_callback.Run(dbus::ObjectPath(""), false);
301 // Tell the adapter the default adapter changed;
302 // BluetoothAdapterClient::GetProperties will be called once to obtain
303 // the property set.
304 MockBluetoothAdapterClient::Properties adapter_properties;
306 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
307 .WillRepeatedly(Return(&adapter_properties));
309 // BluetoothAdapter::Observer::AdapterPresentChanged must not be called.
310 MockBluetoothAdapter::Observer adapter_observer;
311 adapter->AddObserver(&adapter_observer);
313 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), _))
314 .Times(0);
316 BluetoothAdapterChromeOs* adapter_chromeos =
317 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
319 static_cast<BluetoothManagerClient::Observer*>(adapter_chromeos)
320 ->DefaultAdapterChanged(adapter_path);
322 // Adapter should not be present yet.
323 EXPECT_FALSE(adapter->IsPresent());
325 // Tell the adapter the address now;
326 // BluetoothAdapter::Observer::AdapterPresentChanged now must be called.
327 adapter_properties.address.ReplaceValue(adapter_address);
329 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
330 .Times(1);
332 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
333 ->AdapterPropertyChanged(adapter_path,
334 adapter_properties.address.name());
336 // Adapter should be present with the new address.
337 EXPECT_TRUE(adapter->IsPresent());
338 EXPECT_EQ(adapter_address, adapter->address());
341 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterReplacedWithoutAddress) {
342 const dbus::ObjectPath initial_adapter_path("/fake/hci0");
343 const dbus::ObjectPath new_adapter_path("/fake/hci1");
344 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE";
345 const std::string new_adapter_address = "BA:C0:11:CO:FE:FE";
347 // Create the default adapter instance;
348 // BluetoothManagerClient::DefaultAdapter will be called once, passing
349 // a callback to obtain the adapter path.
350 BluetoothManagerClient::AdapterCallback adapter_callback;
351 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
352 .WillOnce(SaveArg<0>(&adapter_callback));
354 scoped_refptr<BluetoothAdapter> adapter =
355 BluetoothAdapterFactory::DefaultAdapter();
357 // Call the adapter callback;
358 // BluetoothAdapterClient::GetProperties will be called once to obtain
359 // the property set.
360 MockBluetoothAdapterClient::Properties initial_adapter_properties;
361 initial_adapter_properties.address.ReplaceValue(initial_adapter_address);
363 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path))
364 .WillRepeatedly(Return(&initial_adapter_properties));
366 adapter_callback.Run(initial_adapter_path, true);
368 // Tell the adapter the default adapter changed;
369 // BluetoothAdapterClient::GetProperties will be called once to obtain
370 // the property set.
371 MockBluetoothAdapterClient::Properties new_adapter_properties;
373 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path))
374 .WillRepeatedly(Return(&new_adapter_properties));
376 // BluetoothAdapter::Observer::AdapterPresentChanged must be called to
377 // indicate the adapter has gone away.
378 MockBluetoothAdapter::Observer adapter_observer;
379 adapter->AddObserver(&adapter_observer);
381 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false))
382 .Times(1);
384 BluetoothAdapterChromeOs* adapter_chromeos =
385 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
387 static_cast<BluetoothManagerClient::Observer*>(adapter_chromeos)
388 ->DefaultAdapterChanged(new_adapter_path);
390 // Adapter should be now marked not present.
391 EXPECT_FALSE(adapter->IsPresent());
393 // Tell the adapter the address now;
394 // BluetoothAdapter::Observer::AdapterPresentChanged now must be called.
395 new_adapter_properties.address.ReplaceValue(new_adapter_address);
397 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
398 .Times(1);
400 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
401 ->AdapterPropertyChanged(new_adapter_path,
402 new_adapter_properties.address.name());
404 // Adapter should be present with the new address.
405 EXPECT_TRUE(adapter->IsPresent());
406 EXPECT_EQ(new_adapter_address, adapter->address());
409 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterRemoved) {
410 const dbus::ObjectPath adapter_path("/fake/hci0");
411 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
413 // Create the default adapter instance;
414 // BluetoothManagerClient::DefaultAdapter will be called once, passing
415 // a callback to obtain the adapter path.
416 BluetoothManagerClient::AdapterCallback adapter_callback;
417 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
418 .WillOnce(SaveArg<0>(&adapter_callback));
420 scoped_refptr<BluetoothAdapter> adapter =
421 BluetoothAdapterFactory::DefaultAdapter();
423 // Call the adapter callback;
424 // BluetoothAdapterClient::GetProperties will be called once to obtain
425 // the property set.
426 MockBluetoothAdapterClient::Properties adapter_properties;
427 adapter_properties.address.ReplaceValue(adapter_address);
429 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
430 .WillRepeatedly(Return(&adapter_properties));
432 adapter_callback.Run(adapter_path, true);
434 // Report that the adapter has been removed;
435 // BluetoothAdapter::Observer::AdapterPresentChanged will be called to
436 // indicate the adapter is no longer present.
437 MockBluetoothAdapter::Observer adapter_observer;
438 adapter->AddObserver(&adapter_observer);
440 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false))
441 .Times(1);
443 BluetoothAdapterChromeOs* adapter_chromeos =
444 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
446 static_cast<BluetoothManagerClient::Observer*>(adapter_chromeos)
447 ->AdapterRemoved(adapter_path);
449 // Adapter should be no longer present.
450 EXPECT_FALSE(adapter->IsPresent());
453 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterWithoutAddressRemoved) {
454 const dbus::ObjectPath adapter_path("/fake/hci0");
456 // Create the default adapter instance;
457 // BluetoothManagerClient::DefaultAdapter will be called once, passing
458 // a callback to obtain the adapter path.
459 BluetoothManagerClient::AdapterCallback adapter_callback;
460 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
461 .WillOnce(SaveArg<0>(&adapter_callback));
463 scoped_refptr<BluetoothAdapter> adapter =
464 BluetoothAdapterFactory::DefaultAdapter();
466 // Call the adapter callback;
467 // BluetoothAdapterClient::GetProperties will be called once to obtain
468 // the property set.
469 MockBluetoothAdapterClient::Properties adapter_properties;
471 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
472 .WillRepeatedly(Return(&adapter_properties));
474 adapter_callback.Run(adapter_path, true);
476 // Report that the adapter has been removed;
477 // BluetoothAdapter::Observer::AdapterPresentChanged must not be called
478 // since we never should have announced it in the first place.
479 MockBluetoothAdapter::Observer adapter_observer;
480 adapter->AddObserver(&adapter_observer);
482 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), _))
483 .Times(0);
485 BluetoothAdapterChromeOs* adapter_chromeos =
486 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
488 static_cast<BluetoothManagerClient::Observer*>(adapter_chromeos)
489 ->AdapterRemoved(adapter_path);
491 // Adapter should be still no longer present.
492 EXPECT_FALSE(adapter->IsPresent());
495 TEST_F(BluetoothAdapterChromeOsTest,
496 DefaultAdapterPoweredPropertyInitiallyFalse) {
497 const dbus::ObjectPath adapter_path("/fake/hci0");
498 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
500 // Create the default adapter instance;
501 // BluetoothManagerClient::DefaultAdapter will be called once, passing
502 // a callback to obtain the adapter path.
503 BluetoothManagerClient::AdapterCallback adapter_callback;
504 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
505 .WillOnce(SaveArg<0>(&adapter_callback));
507 scoped_refptr<BluetoothAdapter> adapter =
508 BluetoothAdapterFactory::DefaultAdapter();
510 // Call the adapter callback;
511 // BluetoothAdapterClient::GetProperties will be called once to obtain
512 // the property set.
513 MockBluetoothAdapterClient::Properties adapter_properties;
514 adapter_properties.address.ReplaceValue(adapter_address);
515 adapter_properties.powered.ReplaceValue(false);
517 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
518 .WillRepeatedly(Return(&adapter_properties));
520 adapter_callback.Run(adapter_path, true);
522 // Adapter should have the correct property value.
523 EXPECT_FALSE(adapter->IsPowered());
526 TEST_F(BluetoothAdapterChromeOsTest,
527 DefaultAdapterPoweredPropertyInitiallyTrue) {
528 const dbus::ObjectPath adapter_path("/fake/hci0");
529 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
531 // Create the default adapter instance;
532 // BluetoothManagerClient::DefaultAdapter will be called once, passing
533 // a callback to obtain the adapter path.
534 BluetoothManagerClient::AdapterCallback adapter_callback;
535 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
536 .WillOnce(SaveArg<0>(&adapter_callback));
538 scoped_refptr<BluetoothAdapter> adapter =
539 BluetoothAdapterFactory::DefaultAdapter();
541 // Call the adapter callback;
542 // BluetoothAdapterClient::GetProperties will be called once to obtain
543 // the property set.
544 MockBluetoothAdapterClient::Properties adapter_properties;
545 adapter_properties.address.ReplaceValue(adapter_address);
546 adapter_properties.powered.ReplaceValue(true);
548 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
549 .WillRepeatedly(Return(&adapter_properties));
551 // BluetoothAdapter::Observer::AdapterPoweredChanged will be called.
552 MockBluetoothAdapter::Observer adapter_observer;
553 adapter->AddObserver(&adapter_observer);
555 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
556 .Times(1);
558 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), true))
559 .Times(1);
561 adapter_callback.Run(adapter_path, true);
563 // Adapter should have the correct property value.
564 EXPECT_TRUE(adapter->IsPowered());
567 TEST_F(BluetoothAdapterChromeOsTest,
568 DefaultAdapterPoweredPropertyInitiallyTrueWithoutAddress) {
569 const dbus::ObjectPath adapter_path("/fake/hci0");
570 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
572 // Create the default adapter instance;
573 // BluetoothManagerClient::DefaultAdapter will be called once, passing
574 // a callback to obtain the adapter path.
575 BluetoothManagerClient::AdapterCallback adapter_callback;
576 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
577 .WillOnce(SaveArg<0>(&adapter_callback));
579 scoped_refptr<BluetoothAdapter> adapter =
580 BluetoothAdapterFactory::DefaultAdapter();
582 // Call the adapter callback;
583 // BluetoothAdapterClient::GetProperties will be called once to obtain
584 // the property set but BluetoothAdapter::Observer::AdapterPoweredChanged
585 // should not yet be called.
586 MockBluetoothAdapterClient::Properties adapter_properties;
587 adapter_properties.powered.ReplaceValue(true);
589 MockBluetoothAdapter::Observer adapter_observer;
590 adapter->AddObserver(&adapter_observer);
592 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
593 .WillRepeatedly(Return(&adapter_properties));
595 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), _))
596 .Times(0);
598 adapter_callback.Run(adapter_path, true);
600 // Adapter should not yet have the property value.
601 EXPECT_FALSE(adapter->IsPowered());
603 // Tell the adapter the address now,
604 // BluetoothAdapter::Observer::AdapterPresentChanged and
605 // BluetoothAdapter::Observer::AdapterPoweredChanged now must be called.
606 adapter_properties.address.ReplaceValue(adapter_address);
608 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
609 .Times(1);
611 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), true))
612 .Times(1);
614 BluetoothAdapterChromeOs* adapter_chromeos =
615 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
617 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
618 ->AdapterPropertyChanged(adapter_path,
619 adapter_properties.address.name());
621 // Adapter should have the correct property value.
622 EXPECT_TRUE(adapter->IsPowered());
625 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterPoweredPropertyChanged) {
626 const dbus::ObjectPath adapter_path("/fake/hci0");
627 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
629 // Create the default adapter instance;
630 // BluetoothManagerClient::DefaultAdapter will be called once, passing
631 // a callback to obtain the adapter path.
632 BluetoothManagerClient::AdapterCallback adapter_callback;
633 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
634 .WillOnce(SaveArg<0>(&adapter_callback));
636 scoped_refptr<BluetoothAdapter> adapter =
637 BluetoothAdapterFactory::DefaultAdapter();
639 // Call the adapter callback;
640 // BluetoothAdapterClient::GetProperties will be called once to obtain
641 // the property set.
642 MockBluetoothAdapterClient::Properties adapter_properties;
643 adapter_properties.address.ReplaceValue(adapter_address);
644 adapter_properties.powered.ReplaceValue(false);
646 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
647 .WillRepeatedly(Return(&adapter_properties));
649 adapter_callback.Run(adapter_path, true);
651 // Adapter should have the correct property value.
652 EXPECT_FALSE(adapter->IsPowered());
654 // Report that the property has been changed;
655 // BluetoothAdapter::Observer::AdapterPoweredChanged will be called.
656 MockBluetoothAdapter::Observer adapter_observer;
657 adapter->AddObserver(&adapter_observer);
659 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), true))
660 .Times(1);
662 adapter_properties.powered.ReplaceValue(true);
664 BluetoothAdapterChromeOs* adapter_chromeos =
665 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
667 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
668 ->AdapterPropertyChanged(adapter_path,
669 adapter_properties.powered.name());
671 // Adapter should have the new property values.
672 EXPECT_TRUE(adapter->IsPowered());
675 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterPoweredPropertyUnchanged) {
676 const dbus::ObjectPath adapter_path("/fake/hci0");
677 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
679 // Create the default adapter instance;
680 // BluetoothManagerClient::DefaultAdapter will be called once, passing
681 // a callback to obtain the adapter path.
682 BluetoothManagerClient::AdapterCallback adapter_callback;
683 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
684 .WillOnce(SaveArg<0>(&adapter_callback));
686 scoped_refptr<BluetoothAdapter> adapter =
687 BluetoothAdapterFactory::DefaultAdapter();
689 // Call the adapter callback;
690 // BluetoothAdapterClient::GetProperties will be called once to obtain
691 // the property set.
692 MockBluetoothAdapterClient::Properties adapter_properties;
693 adapter_properties.address.ReplaceValue(adapter_address);
694 adapter_properties.powered.ReplaceValue(true);
696 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
697 .WillRepeatedly(Return(&adapter_properties));
699 adapter_callback.Run(adapter_path, true);
701 // Adapter should have the correct property value.
702 EXPECT_TRUE(adapter->IsPowered());
704 // Report that the property has been changed, but don't change the value;
705 // BluetoothAdapter::Observer::AdapterPoweredChanged should not be called.
706 MockBluetoothAdapter::Observer adapter_observer;
707 adapter->AddObserver(&adapter_observer);
709 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), _))
710 .Times(0);
712 BluetoothAdapterChromeOs* adapter_chromeos =
713 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
715 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
716 ->AdapterPropertyChanged(adapter_path,
717 adapter_properties.powered.name());
719 // Adapter should still have the same property values.
720 EXPECT_TRUE(adapter->IsPowered());
723 TEST_F(BluetoothAdapterChromeOsTest,
724 DefaultAdapterPoweredPropertyChangedWithoutAddress) {
725 const dbus::ObjectPath adapter_path("/fake/hci0");
726 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
728 // Create the default adapter instance;
729 // BluetoothManagerClient::DefaultAdapter will be called once, passing
730 // a callback to obtain the adapter path.
731 BluetoothManagerClient::AdapterCallback adapter_callback;
732 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
733 .WillOnce(SaveArg<0>(&adapter_callback));
735 scoped_refptr<BluetoothAdapter> adapter =
736 BluetoothAdapterFactory::DefaultAdapter();
738 // Call the adapter callback;
739 // BluetoothAdapterClient::GetProperties will be called once to obtain
740 // the property set but BluetoothAdapter::Observer::AdapterPoweredChanged
741 // should not yet be called.
742 MockBluetoothAdapterClient::Properties adapter_properties;
744 MockBluetoothAdapter::Observer adapter_observer;
745 adapter->AddObserver(&adapter_observer);
747 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
748 .WillRepeatedly(Return(&adapter_properties));
750 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), _))
751 .Times(0);
753 adapter_callback.Run(adapter_path, true);
755 // Tell the adapter that its powered property changed, the observer
756 // method should still not be called because there is no address for
757 // the adapter so it is not present.
758 adapter_properties.powered.ReplaceValue(true);
760 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), _))
761 .Times(0);
763 BluetoothAdapterChromeOs* adapter_chromeos =
764 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
766 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
767 ->AdapterPropertyChanged(adapter_path,
768 adapter_properties.powered.name());
770 // Adapter should not yet have the property value.
771 EXPECT_FALSE(adapter->IsPowered());
773 // Tell the adapter the address now,
774 // BluetoothAdapter::Observer::AdapterPresentChanged and
775 // BluetoothAdapter::Observer::AdapterPoweredChanged now must be called.
776 adapter_properties.address.ReplaceValue(adapter_address);
778 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
779 .Times(1);
781 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), true))
782 .Times(1);
784 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
785 ->AdapterPropertyChanged(adapter_path,
786 adapter_properties.address.name());
788 // Adapter should now have the correct property value.
789 EXPECT_TRUE(adapter->IsPowered());
792 TEST_F(BluetoothAdapterChromeOsTest,
793 DefaultAdapterPoweredPropertyResetOnReplace) {
794 const dbus::ObjectPath initial_adapter_path("/fake/hci0");
795 const dbus::ObjectPath new_adapter_path("/fake/hci1");
796 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE";
797 const std::string new_adapter_address = "00:C0:11:CO:FE:FE";
799 // Create the default adapter instance;
800 // BluetoothManagerClient::DefaultAdapter will be called once, passing
801 // a callback to obtain the adapter path.
802 BluetoothManagerClient::AdapterCallback adapter_callback;
803 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
804 .WillOnce(SaveArg<0>(&adapter_callback));
806 scoped_refptr<BluetoothAdapter> adapter =
807 BluetoothAdapterFactory::DefaultAdapter();
809 // Call the adapter callback;
810 // BluetoothAdapterClient::GetProperties will be called once to obtain
811 // the property set.
812 MockBluetoothAdapterClient::Properties initial_adapter_properties;
813 initial_adapter_properties.address.ReplaceValue(initial_adapter_address);
814 initial_adapter_properties.powered.ReplaceValue(true);
816 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path))
817 .WillRepeatedly(Return(&initial_adapter_properties));
819 adapter_callback.Run(initial_adapter_path, true);
821 // Tell the adapter the default adapter changed;
822 // BluetoothAdapterClient::GetProperties will be called once to obtain
823 // the property set.
824 MockBluetoothAdapterClient::Properties new_adapter_properties;
825 new_adapter_properties.address.ReplaceValue(new_adapter_address);
827 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path))
828 .WillRepeatedly(Return(&new_adapter_properties));
830 // BluetoothAdapter::Observer::AdapterPoweredChanged will be called.
831 MockBluetoothAdapter::Observer adapter_observer;
832 adapter->AddObserver(&adapter_observer);
835 InSequence s;
837 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false))
838 .Times(1);
839 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
840 .Times(1);
843 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), false))
844 .Times(1);
846 BluetoothAdapterChromeOs* adapter_chromeos =
847 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
849 static_cast<BluetoothManagerClient::Observer*>(adapter_chromeos)
850 ->DefaultAdapterChanged(new_adapter_path);
852 // Adapter should have the new property value.
853 EXPECT_FALSE(adapter->IsPowered());
856 TEST_F(BluetoothAdapterChromeOsTest,
857 DefaultAdapterPoweredPropertyResetOnReplaceWhenTrue) {
858 const dbus::ObjectPath initial_adapter_path("/fake/hci0");
859 const dbus::ObjectPath new_adapter_path("/fake/hci1");
860 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE";
861 const std::string new_adapter_address = "BA:C0:11:CO:FE:FE";
863 // Create the default adapter instance;
864 // BluetoothManagerClient::DefaultAdapter will be called once, passing
865 // a callback to obtain the adapter path.
866 BluetoothManagerClient::AdapterCallback adapter_callback;
867 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
868 .WillOnce(SaveArg<0>(&adapter_callback));
870 scoped_refptr<BluetoothAdapter> adapter =
871 BluetoothAdapterFactory::DefaultAdapter();
873 // Call the adapter callback;
874 // BluetoothAdapterClient::GetProperties will be called once to obtain
875 // the property set.
876 MockBluetoothAdapterClient::Properties initial_adapter_properties;
877 initial_adapter_properties.address.ReplaceValue(initial_adapter_address);
878 initial_adapter_properties.powered.ReplaceValue(true);
880 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path))
881 .WillRepeatedly(Return(&initial_adapter_properties));
883 adapter_callback.Run(initial_adapter_path, true);
885 // Tell the adapter the default adapter changed;
886 // BluetoothAdapterClient::GetProperties will be called once to obtain
887 // the property set.
888 MockBluetoothAdapterClient::Properties new_adapter_properties;
889 new_adapter_properties.address.ReplaceValue(new_adapter_address);
890 new_adapter_properties.powered.ReplaceValue(true);
892 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path))
893 .WillRepeatedly(Return(&new_adapter_properties));
895 // BluetoothAdapter::Observer::AdapterPoweredChanged will be called once
896 // to set the value to false for the previous adapter and once to set the
897 // value to true for the new adapter.
898 MockBluetoothAdapter::Observer adapter_observer;
899 adapter->AddObserver(&adapter_observer);
902 InSequence s;
904 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false))
905 .Times(1);
906 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
907 .Times(1);
911 InSequence s;
913 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), false))
914 .Times(1);
915 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), true))
916 .Times(1);
919 BluetoothAdapterChromeOs* adapter_chromeos =
920 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
922 static_cast<BluetoothManagerClient::Observer*>(adapter_chromeos)
923 ->DefaultAdapterChanged(new_adapter_path);
925 // Adapter should have the new property value.
926 EXPECT_TRUE(adapter->IsPowered());
929 TEST_F(BluetoothAdapterChromeOsTest,
930 DefaultAdapterPoweredPropertyResetOnRemove) {
931 const dbus::ObjectPath adapter_path("/fake/hci0");
932 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
934 // Create the default adapter instance;
935 // BluetoothManagerClient::DefaultAdapter will be called once, passing
936 // a callback to obtain the adapter path.
937 BluetoothManagerClient::AdapterCallback adapter_callback;
938 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
939 .WillOnce(SaveArg<0>(&adapter_callback));
941 scoped_refptr<BluetoothAdapter> adapter =
942 BluetoothAdapterFactory::DefaultAdapter();
944 // Call the adapter callback;
945 // BluetoothAdapterClient::GetProperties will be called once to obtain
946 // the property set.
947 MockBluetoothAdapterClient::Properties adapter_properties;
948 adapter_properties.address.ReplaceValue(adapter_address);
949 adapter_properties.powered.ReplaceValue(true);
951 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
952 .WillRepeatedly(Return(&adapter_properties));
954 adapter_callback.Run(adapter_path, true);
956 // Report that the adapter has been removed;
957 // BluetoothAdapter::Observer::AdapterPoweredChanged will be called.
958 MockBluetoothAdapter::Observer adapter_observer;
959 adapter->AddObserver(&adapter_observer);
961 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false))
962 .Times(1);
963 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), false))
964 .Times(1);
966 BluetoothAdapterChromeOs* adapter_chromeos =
967 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
969 static_cast<BluetoothManagerClient::Observer*>(adapter_chromeos)
970 ->AdapterRemoved(adapter_path);
972 // Adapter should have the new property value.
973 EXPECT_FALSE(adapter->IsPowered());
976 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterSetPowered) {
977 const dbus::ObjectPath adapter_path("/fake/hci0");
978 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
980 // Create the default adapter instance;
981 // BluetoothManagerClient::DefaultAdapter will be called once, passing
982 // a callback to obtain the adapter path.
983 BluetoothManagerClient::AdapterCallback adapter_callback;
984 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
985 .WillOnce(SaveArg<0>(&adapter_callback));
987 scoped_refptr<BluetoothAdapter> adapter =
988 BluetoothAdapterFactory::DefaultAdapter();
990 // Call the adapter callback;
991 // BluetoothAdapterClient::GetProperties will be called once to obtain
992 // the property set.
993 MockBluetoothAdapterClient::Properties adapter_properties;
995 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
996 .WillRepeatedly(Return(&adapter_properties));
998 adapter_callback.Run(adapter_path, true);
1000 // Request that the powered property be changed;
1001 // MockBluetoothAdapterClient::Set should be called, passing the address
1002 // of the powered property and a callback to receive the response.
1003 dbus::PropertySet::SetCallback set_callback;
1004 EXPECT_CALL(adapter_properties, Set(&adapter_properties.powered, _))
1005 .WillOnce(SaveArg<1>(&set_callback));
1007 adapter->SetPowered(true,
1008 base::Bind(&BluetoothAdapterChromeOsTest::SetCallback,
1009 base::Unretained(this)),
1010 base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
1011 base::Unretained(this)));
1013 // Reply to the callback to indicate success, the set callback we provided
1014 // should be called but the properties should not be refetched.
1015 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
1016 .Times(0);
1018 set_callback.Run(true);
1020 EXPECT_TRUE(set_callback_called_);
1021 EXPECT_FALSE(error_callback_called_);
1024 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterSetPoweredError) {
1025 const dbus::ObjectPath adapter_path("/fake/hci0");
1026 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
1028 // Create the default adapter instance;
1029 // BluetoothManagerClient::DefaultAdapter will be called once, passing
1030 // a callback to obtain the adapter path.
1031 BluetoothManagerClient::AdapterCallback adapter_callback;
1032 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
1033 .WillOnce(SaveArg<0>(&adapter_callback));
1035 scoped_refptr<BluetoothAdapter> adapter =
1036 BluetoothAdapterFactory::DefaultAdapter();
1038 // Call the adapter callback;
1039 // BluetoothAdapterClient::GetProperties will be called once to obtain
1040 // the property set.
1041 MockBluetoothAdapterClient::Properties adapter_properties;
1043 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
1044 .WillRepeatedly(Return(&adapter_properties));
1046 adapter_callback.Run(adapter_path, true);
1048 // Request that the powered property be changed;
1049 // MockBluetoothAdapterClient::Set should be called, passing the address
1050 // of the powered property and a callback to receive the response.
1051 dbus::PropertySet::SetCallback set_callback;
1052 EXPECT_CALL(adapter_properties, Set(&adapter_properties.powered, _))
1053 .WillOnce(SaveArg<1>(&set_callback));
1055 adapter->SetPowered(true,
1056 base::Bind(&BluetoothAdapterChromeOsTest::SetCallback,
1057 base::Unretained(this)),
1058 base::Bind(&BluetoothAdapterChromeOsTest::ErrorCallback,
1059 base::Unretained(this)));
1061 // Reply to the callback to indicate failure, the error callback we provided
1062 // should be called but the properties should not be refetched.
1063 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
1064 .Times(0);
1066 set_callback.Run(false);
1068 EXPECT_FALSE(set_callback_called_);
1069 EXPECT_TRUE(error_callback_called_);
1072 TEST_F(BluetoothAdapterChromeOsTest,
1073 DefaultAdapterDiscoveringPropertyInitiallyFalse) {
1074 const dbus::ObjectPath adapter_path("/fake/hci0");
1075 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
1077 // Create the default adapter instance;
1078 // BluetoothManagerClient::DefaultAdapter will be called once, passing
1079 // a callback to obtain the adapter path.
1080 BluetoothManagerClient::AdapterCallback adapter_callback;
1081 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
1082 .WillOnce(SaveArg<0>(&adapter_callback));
1084 scoped_refptr<BluetoothAdapter> adapter =
1085 BluetoothAdapterFactory::DefaultAdapter();
1087 // Call the adapter callback;
1088 // BluetoothAdapterClient::GetProperties will be called once to obtain
1089 // the property set.
1090 MockBluetoothAdapterClient::Properties adapter_properties;
1091 adapter_properties.address.ReplaceValue(adapter_address);
1092 adapter_properties.discovering.ReplaceValue(false);
1094 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
1095 .WillRepeatedly(Return(&adapter_properties));
1097 adapter_callback.Run(adapter_path, true);
1099 // Adapter should have the correct property value.
1100 EXPECT_FALSE(adapter->IsDiscovering());
1103 TEST_F(BluetoothAdapterChromeOsTest,
1104 DefaultAdapterDiscoveringPropertyInitiallyTrue) {
1105 const dbus::ObjectPath adapter_path("/fake/hci0");
1106 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
1108 // Create the default adapter instance;
1109 // BluetoothManagerClient::DefaultAdapter will be called once, passing
1110 // a callback to obtain the adapter path.
1111 BluetoothManagerClient::AdapterCallback adapter_callback;
1112 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
1113 .WillOnce(SaveArg<0>(&adapter_callback));
1115 scoped_refptr<BluetoothAdapter> adapter =
1116 BluetoothAdapterFactory::DefaultAdapter();
1118 // Call the adapter callback;
1119 // BluetoothAdapterClient::GetProperties will be called once to obtain
1120 // the property set.
1121 MockBluetoothAdapterClient::Properties adapter_properties;
1122 adapter_properties.address.ReplaceValue(adapter_address);
1123 adapter_properties.discovering.ReplaceValue(true);
1125 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
1126 .WillRepeatedly(Return(&adapter_properties));
1128 // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
1129 MockBluetoothAdapter::Observer adapter_observer;
1130 adapter->AddObserver(&adapter_observer);
1132 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
1133 .Times(1);
1135 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), true))
1136 .Times(1);
1138 adapter_callback.Run(adapter_path, true);
1140 // Adapter should have the correct property value.
1141 EXPECT_TRUE(adapter->IsDiscovering());
1144 TEST_F(BluetoothAdapterChromeOsTest,
1145 DefaultAdapterDiscoveringPropertyInitiallyTrueWithoutAddress) {
1146 const dbus::ObjectPath adapter_path("/fake/hci0");
1147 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
1149 // Create the default adapter instance;
1150 // BluetoothManagerClient::DefaultAdapter will be called once, passing
1151 // a callback to obtain the adapter path.
1152 BluetoothManagerClient::AdapterCallback adapter_callback;
1153 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
1154 .WillOnce(SaveArg<0>(&adapter_callback));
1156 scoped_refptr<BluetoothAdapter> adapter =
1157 BluetoothAdapterFactory::DefaultAdapter();
1159 // Call the adapter callback;
1160 // BluetoothAdapterClient::GetProperties will be called once to obtain
1161 // the property set but BluetoothAdapter::Observer::AdapterDiscoveringChanged
1162 // should not yet be called.
1163 MockBluetoothAdapterClient::Properties adapter_properties;
1164 adapter_properties.discovering.ReplaceValue(true);
1166 MockBluetoothAdapter::Observer adapter_observer;
1167 adapter->AddObserver(&adapter_observer);
1169 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
1170 .WillRepeatedly(Return(&adapter_properties));
1172 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), _))
1173 .Times(0);
1175 adapter_callback.Run(adapter_path, true);
1177 // Adapter should not yet have the property value.
1178 EXPECT_FALSE(adapter->IsDiscovering());
1180 // Tell the adapter the address now,
1181 // BluetoothAdapter::Observer::AdapterPresentChanged and
1182 // BluetoothAdapter::Observer::AdapterDiscoveringChanged now must be called.
1183 adapter_properties.address.ReplaceValue(adapter_address);
1185 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
1186 .Times(1);
1188 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), true))
1189 .Times(1);
1191 BluetoothAdapterChromeOs* adapter_chromeos =
1192 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
1194 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
1195 ->AdapterPropertyChanged(adapter_path,
1196 adapter_properties.address.name());
1198 // Adapter should have the correct property value.
1199 EXPECT_TRUE(adapter->IsDiscovering());
1202 TEST_F(BluetoothAdapterChromeOsTest, DefaultAdapterDiscoveringPropertyChanged) {
1203 const dbus::ObjectPath adapter_path("/fake/hci0");
1204 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
1206 // Create the default adapter instance;
1207 // BluetoothManagerClient::DefaultAdapter will be called once, passing
1208 // a callback to obtain the adapter path.
1209 BluetoothManagerClient::AdapterCallback adapter_callback;
1210 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
1211 .WillOnce(SaveArg<0>(&adapter_callback));
1213 scoped_refptr<BluetoothAdapter> adapter =
1214 BluetoothAdapterFactory::DefaultAdapter();
1216 // Call the adapter callback;
1217 // BluetoothAdapterClient::GetProperties will be called once to obtain
1218 // the property set.
1219 MockBluetoothAdapterClient::Properties adapter_properties;
1220 adapter_properties.address.ReplaceValue(adapter_address);
1221 adapter_properties.discovering.ReplaceValue(false);
1223 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
1224 .WillRepeatedly(Return(&adapter_properties));
1226 adapter_callback.Run(adapter_path, true);
1228 // Adapter should have the correct property value.
1229 EXPECT_FALSE(adapter->IsDiscovering());
1231 // Report that the property has been changed;
1232 // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
1233 MockBluetoothAdapter::Observer adapter_observer;
1234 adapter->AddObserver(&adapter_observer);
1236 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), true))
1237 .Times(1);
1239 adapter_properties.discovering.ReplaceValue(true);
1241 BluetoothAdapterChromeOs* adapter_chromeos =
1242 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
1244 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
1245 ->AdapterPropertyChanged(adapter_path,
1246 adapter_properties.discovering.name());
1248 // Adapter should have the new property values.
1249 EXPECT_TRUE(adapter->IsDiscovering());
1252 TEST_F(BluetoothAdapterChromeOsTest,
1253 DefaultAdapterDiscoveringPropertyUnchanged) {
1254 const dbus::ObjectPath adapter_path("/fake/hci0");
1255 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
1257 // Create the default adapter instance;
1258 // BluetoothManagerClient::DefaultAdapter will be called once, passing
1259 // a callback to obtain the adapter path.
1260 BluetoothManagerClient::AdapterCallback adapter_callback;
1261 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
1262 .WillOnce(SaveArg<0>(&adapter_callback));
1264 scoped_refptr<BluetoothAdapter> adapter =
1265 BluetoothAdapterFactory::DefaultAdapter();
1267 // Call the adapter callback;
1268 // BluetoothAdapterClient::GetProperties will be called once to obtain
1269 // the property set.
1270 MockBluetoothAdapterClient::Properties adapter_properties;
1271 adapter_properties.address.ReplaceValue(adapter_address);
1272 adapter_properties.discovering.ReplaceValue(true);
1274 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
1275 .WillRepeatedly(Return(&adapter_properties));
1277 adapter_callback.Run(adapter_path, true);
1279 // Adapter should have the correct property value.
1280 EXPECT_TRUE(adapter->IsDiscovering());
1282 // Report that the property has been changed, but don't change the value;
1283 // BluetoothAdapter::Observer::AdapterDiscoveringChanged should not be
1284 // called.
1285 MockBluetoothAdapter::Observer adapter_observer;
1286 adapter->AddObserver(&adapter_observer);
1288 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), _))
1289 .Times(0);
1291 BluetoothAdapterChromeOs* adapter_chromeos =
1292 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
1294 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
1295 ->AdapterPropertyChanged(adapter_path,
1296 adapter_properties.discovering.name());
1298 // Adapter should still have the same property values.
1299 EXPECT_TRUE(adapter->IsDiscovering());
1302 TEST_F(BluetoothAdapterChromeOsTest,
1303 DefaultAdapterDiscoveringPropertyChangedWithoutAddress) {
1304 const dbus::ObjectPath adapter_path("/fake/hci0");
1305 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
1307 // Create the default adapter instance;
1308 // BluetoothManagerClient::DefaultAdapter will be called once, passing
1309 // a callback to obtain the adapter path.
1310 BluetoothManagerClient::AdapterCallback adapter_callback;
1311 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
1312 .WillOnce(SaveArg<0>(&adapter_callback));
1314 scoped_refptr<BluetoothAdapter> adapter =
1315 BluetoothAdapterFactory::DefaultAdapter();
1317 // Call the adapter callback;
1318 // BluetoothAdapterClient::GetProperties will be called once to obtain
1319 // the property set but BluetoothAdapter::Observer::AdapterDiscoveringChanged
1320 // should not yet be called.
1321 MockBluetoothAdapterClient::Properties adapter_properties;
1323 MockBluetoothAdapter::Observer adapter_observer;
1324 adapter->AddObserver(&adapter_observer);
1326 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
1327 .WillRepeatedly(Return(&adapter_properties));
1329 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), _))
1330 .Times(0);
1332 adapter_callback.Run(adapter_path, true);
1334 // Tell the adapter that its discovering property changed, the observer
1335 // method should still not be called because there is no address for
1336 // the adapter so it is not present.
1337 adapter_properties.discovering.ReplaceValue(true);
1339 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), _))
1340 .Times(0);
1342 BluetoothAdapterChromeOs* adapter_chromeos =
1343 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
1345 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
1346 ->AdapterPropertyChanged(adapter_path,
1347 adapter_properties.discovering.name());
1349 // Adapter should not yet have the property value.
1350 EXPECT_FALSE(adapter->IsDiscovering());
1352 // Tell the adapter the address now,
1353 // BluetoothAdapter::Observer::AdapterPresentChanged and
1354 // BluetoothAdapter::Observer::AdapterDiscoveringChanged now must be called.
1355 adapter_properties.address.ReplaceValue(adapter_address);
1357 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
1358 .Times(1);
1360 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), true))
1361 .Times(1);
1363 static_cast<BluetoothAdapterClient::Observer*>(adapter_chromeos)
1364 ->AdapterPropertyChanged(adapter_path,
1365 adapter_properties.address.name());
1367 // Adapter should now have the correct property value.
1368 EXPECT_TRUE(adapter->IsDiscovering());
1371 TEST_F(BluetoothAdapterChromeOsTest,
1372 DefaultAdapterDiscoveringPropertyResetOnReplace) {
1373 const dbus::ObjectPath initial_adapter_path("/fake/hci0");
1374 const dbus::ObjectPath new_adapter_path("/fake/hci1");
1375 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE";
1376 const std::string new_adapter_address = "BA:C0:11:CO:FE:FE";
1378 // Create the default adapter instance;
1379 // BluetoothManagerClient::DefaultAdapter will be called once, passing
1380 // a callback to obtain the adapter path.
1381 BluetoothManagerClient::AdapterCallback adapter_callback;
1382 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
1383 .WillOnce(SaveArg<0>(&adapter_callback));
1385 scoped_refptr<BluetoothAdapter> adapter =
1386 BluetoothAdapterFactory::DefaultAdapter();
1388 // Call the adapter callback;
1389 // BluetoothAdapterClient::GetProperties will be called once to obtain
1390 // the property set.
1391 MockBluetoothAdapterClient::Properties initial_adapter_properties;
1392 initial_adapter_properties.address.ReplaceValue(initial_adapter_address);
1393 initial_adapter_properties.discovering.ReplaceValue(true);
1395 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path))
1396 .WillRepeatedly(Return(&initial_adapter_properties));
1398 adapter_callback.Run(initial_adapter_path, true);
1400 // Tell the adapter the default adapter changed;
1401 // BluetoothAdapterClient::GetProperties will be called once to obtain
1402 // the property set.
1403 MockBluetoothAdapterClient::Properties new_adapter_properties;
1404 new_adapter_properties.address.ReplaceValue(new_adapter_address);
1406 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path))
1407 .WillRepeatedly(Return(&new_adapter_properties));
1409 // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
1410 MockBluetoothAdapter::Observer adapter_observer;
1411 adapter->AddObserver(&adapter_observer);
1414 InSequence s;
1416 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false))
1417 .Times(1);
1418 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
1419 .Times(1);
1422 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), false))
1423 .Times(1);
1425 BluetoothAdapterChromeOs* adapter_chromeos =
1426 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
1428 static_cast<BluetoothManagerClient::Observer*>(adapter_chromeos)
1429 ->DefaultAdapterChanged(new_adapter_path);
1431 // Adapter should have the new property value.
1432 EXPECT_FALSE(adapter->IsDiscovering());
1435 TEST_F(BluetoothAdapterChromeOsTest,
1436 DefaultAdapterDiscoveringPropertyResetOnReplaceWhenTrue) {
1437 const dbus::ObjectPath initial_adapter_path("/fake/hci0");
1438 const dbus::ObjectPath new_adapter_path("/fake/hci1");
1439 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE";
1440 const std::string new_adapter_address = "BA:C0:11:CO:FE:FE";
1442 // Create the default adapter instance;
1443 // BluetoothManagerClient::DefaultAdapter will be called once, passing
1444 // a callback to obtain the adapter path.
1445 BluetoothManagerClient::AdapterCallback adapter_callback;
1446 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
1447 .WillOnce(SaveArg<0>(&adapter_callback));
1449 scoped_refptr<BluetoothAdapter> adapter =
1450 BluetoothAdapterFactory::DefaultAdapter();
1452 // Call the adapter callback;
1453 // BluetoothAdapterClient::GetProperties will be called once to obtain
1454 // the property set.
1455 MockBluetoothAdapterClient::Properties initial_adapter_properties;
1456 initial_adapter_properties.address.ReplaceValue(initial_adapter_address);
1457 initial_adapter_properties.discovering.ReplaceValue(true);
1459 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path))
1460 .WillRepeatedly(Return(&initial_adapter_properties));
1462 adapter_callback.Run(initial_adapter_path, true);
1464 // Tell the adapter the default adapter changed;
1465 // BluetoothAdapterClient::GetProperties will be called once to obtain
1466 // the property set.
1467 MockBluetoothAdapterClient::Properties new_adapter_properties;
1468 new_adapter_properties.address.ReplaceValue(new_adapter_address);
1469 new_adapter_properties.discovering.ReplaceValue(true);
1471 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path))
1472 .WillRepeatedly(Return(&new_adapter_properties));
1474 // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called once
1475 // to set the value to false for the previous adapter and once to set the
1476 // value to true for the new adapter.
1477 MockBluetoothAdapter::Observer adapter_observer;
1478 adapter->AddObserver(&adapter_observer);
1481 InSequence s;
1483 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false))
1484 .Times(1);
1485 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true))
1486 .Times(1);
1490 InSequence s;
1492 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(),
1493 false))
1494 .Times(1);
1495 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(),
1496 true))
1497 .Times(1);
1500 BluetoothAdapterChromeOs* adapter_chromeos =
1501 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
1503 static_cast<BluetoothManagerClient::Observer*>(adapter_chromeos)
1504 ->DefaultAdapterChanged(new_adapter_path);
1506 // Adapter should have the new property value.
1507 EXPECT_TRUE(adapter->IsDiscovering());
1510 TEST_F(BluetoothAdapterChromeOsTest,
1511 DefaultAdapterDiscoveringPropertyResetOnRemove) {
1512 const dbus::ObjectPath adapter_path("/fake/hci0");
1513 const std::string adapter_address = "CA:FE:4A:C0:FE:FE";
1515 // Create the default adapter instance;
1516 // BluetoothManagerClient::DefaultAdapter will be called once, passing
1517 // a callback to obtain the adapter path.
1518 BluetoothManagerClient::AdapterCallback adapter_callback;
1519 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_))
1520 .WillOnce(SaveArg<0>(&adapter_callback));
1522 scoped_refptr<BluetoothAdapter> adapter =
1523 BluetoothAdapterFactory::DefaultAdapter();
1525 // Call the adapter callback;
1526 // BluetoothAdapterClient::GetProperties will be called once to obtain
1527 // the property set.
1528 MockBluetoothAdapterClient::Properties adapter_properties;
1529 adapter_properties.address.ReplaceValue(adapter_address);
1530 adapter_properties.discovering.ReplaceValue(true);
1532 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path))
1533 .WillRepeatedly(Return(&adapter_properties));
1535 adapter_callback.Run(adapter_path, true);
1537 // Report that the adapter has been removed;
1538 // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called.
1539 MockBluetoothAdapter::Observer adapter_observer;
1540 adapter->AddObserver(&adapter_observer);
1542 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false))
1543 .Times(1);
1544 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), false))
1545 .Times(1);
1547 BluetoothAdapterChromeOs* adapter_chromeos =
1548 static_cast<BluetoothAdapterChromeOs*>(adapter.get());
1550 static_cast<BluetoothManagerClient::Observer*>(adapter_chromeos)
1551 ->AdapterRemoved(adapter_path);
1553 // Adapter should have the new property value.
1554 EXPECT_FALSE(adapter->IsDiscovering());
1557 } // namespace chromeos