| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | |
| 6 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h" | |
| 7 #include "chromeos/dbus/mock_bluetooth_adapter_client.h" | |
| 8 #include "chromeos/dbus/mock_bluetooth_manager_client.h" | |
| 9 #include "chromeos/dbus/mock_dbus_thread_manager.h" | |
| 10 #include "dbus/object_path.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 using ::testing::_; | |
| 14 using ::testing::InSequence; | |
| 15 using ::testing::Return; | |
| 16 using ::testing::SaveArg; | |
| 17 | |
| 18 namespace chromeos { | |
| 19 | |
| 20 class BluetoothAdapterTest : public testing::Test { | |
| 21 public: | |
| 22 virtual void SetUp() { | |
| 23 MockDBusThreadManager* mock_dbus_thread_manager = new MockDBusThreadManager; | |
| 24 | |
| 25 EXPECT_CALL(*mock_dbus_thread_manager, GetSystemBus()) | |
| 26 .WillRepeatedly(Return(reinterpret_cast<dbus::Bus*>(NULL))); | |
| 27 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager); | |
| 28 | |
| 29 mock_manager_client_ = | |
| 30 mock_dbus_thread_manager->mock_bluetooth_manager_client(); | |
| 31 mock_adapter_client_ = | |
| 32 mock_dbus_thread_manager->mock_bluetooth_adapter_client(); | |
| 33 | |
| 34 set_callback_called_ = false; | |
| 35 error_callback_called_ = false; | |
| 36 } | |
| 37 | |
| 38 virtual void TearDown() { | |
| 39 DBusThreadManager::Shutdown(); | |
| 40 } | |
| 41 | |
| 42 void SetCallback() { | |
| 43 set_callback_called_ = true; | |
| 44 } | |
| 45 | |
| 46 void ErrorCallback() { | |
| 47 error_callback_called_ = true; | |
| 48 } | |
| 49 | |
| 50 protected: | |
| 51 MockBluetoothManagerClient* mock_manager_client_; | |
| 52 MockBluetoothAdapterClient* mock_adapter_client_; | |
| 53 | |
| 54 bool set_callback_called_; | |
| 55 bool error_callback_called_; | |
| 56 }; | |
| 57 | |
| 58 TEST_F(BluetoothAdapterTest, DefaultAdapterNotPresent) { | |
| 59 // Create the default adapter instance; | |
| 60 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 61 // a callback to obtain the adapter path. | |
| 62 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 63 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 64 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 65 | |
| 66 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 67 | |
| 68 // Call the adapter callback; make out it failed. | |
| 69 // BluetoothAdapter::Observer::AdapterPresentChanged must not be called. | |
| 70 MockBluetoothAdapter::Observer adapter_observer; | |
| 71 adapter->AddObserver(&adapter_observer); | |
| 72 | |
| 73 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), _)) | |
| 74 .Times(0); | |
| 75 | |
| 76 adapter_callback.Run(dbus::ObjectPath(""), false); | |
| 77 | |
| 78 // Adapter should not be present. | |
| 79 EXPECT_FALSE(adapter->IsPresent()); | |
| 80 } | |
| 81 | |
| 82 TEST_F(BluetoothAdapterTest, DefaultAdapterWithAddress) { | |
| 83 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 84 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 85 | |
| 86 // Create the default adapter instance; | |
| 87 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 88 // a callback to obtain the adapter path. | |
| 89 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 90 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 91 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 92 | |
| 93 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 94 | |
| 95 // Call the adapter callback; | |
| 96 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 97 // the property set. | |
| 98 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 99 adapter_properties.address.ReplaceValue(adapter_address); | |
| 100 | |
| 101 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 102 .WillRepeatedly(Return(&adapter_properties)); | |
| 103 | |
| 104 // BluetoothAdapter::Observer::AdapterPresentChanged will be called to | |
| 105 // indicate the adapter is now present. | |
| 106 MockBluetoothAdapter::Observer adapter_observer; | |
| 107 adapter->AddObserver(&adapter_observer); | |
| 108 | |
| 109 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 110 .Times(1); | |
| 111 | |
| 112 adapter_callback.Run(adapter_path, true); | |
| 113 | |
| 114 // Adapter should be present with the given address. | |
| 115 EXPECT_TRUE(adapter->IsPresent()); | |
| 116 EXPECT_EQ(adapter_address, adapter->address()); | |
| 117 } | |
| 118 | |
| 119 TEST_F(BluetoothAdapterTest, DefaultAdapterWithoutAddress) { | |
| 120 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 121 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 122 | |
| 123 // Create the default adapter instance; | |
| 124 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 125 // a callback to obtain the adapter path. | |
| 126 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 127 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 128 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 129 | |
| 130 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 131 | |
| 132 // Call the adapter callback; | |
| 133 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 134 // the property set. | |
| 135 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 136 | |
| 137 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 138 .WillRepeatedly(Return(&adapter_properties)); | |
| 139 | |
| 140 // BluetoothAdapter::Observer::AdapterPresentChanged must not be called | |
| 141 // yet. | |
| 142 MockBluetoothAdapter::Observer adapter_observer; | |
| 143 adapter->AddObserver(&adapter_observer); | |
| 144 | |
| 145 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), _)) | |
| 146 .Times(0); | |
| 147 | |
| 148 adapter_callback.Run(adapter_path, true); | |
| 149 | |
| 150 // Adapter should not be present yet. | |
| 151 EXPECT_FALSE(adapter->IsPresent()); | |
| 152 | |
| 153 // Tell the adapter the address now; | |
| 154 // BluetoothAdapter::Observer::AdapterPresentChanged now must be called. | |
| 155 adapter_properties.address.ReplaceValue(adapter_address); | |
| 156 | |
| 157 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 158 .Times(1); | |
| 159 | |
| 160 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 161 ->AdapterPropertyChanged(adapter_path, | |
| 162 adapter_properties.address.name()); | |
| 163 | |
| 164 // Adapter should be present with the given address. | |
| 165 EXPECT_TRUE(adapter->IsPresent()); | |
| 166 EXPECT_EQ(adapter_address, adapter->address()); | |
| 167 } | |
| 168 | |
| 169 TEST_F(BluetoothAdapterTest, DefaultAdapterBecomesPresentWithAddress) { | |
| 170 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 171 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 172 | |
| 173 // Create the default adapter instance; | |
| 174 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 175 // a callback to obtain the adapter path. | |
| 176 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 177 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 178 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 179 | |
| 180 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 181 | |
| 182 // Call the adapter callback; make out it failed. | |
| 183 adapter_callback.Run(dbus::ObjectPath(""), false); | |
| 184 | |
| 185 // Tell the adapter the default adapter changed; | |
| 186 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 187 // the property set. | |
| 188 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 189 adapter_properties.address.ReplaceValue(adapter_address); | |
| 190 | |
| 191 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 192 .WillRepeatedly(Return(&adapter_properties)); | |
| 193 | |
| 194 // BluetoothAdapter::Observer::AdapterPresentChanged must be called. | |
| 195 MockBluetoothAdapter::Observer adapter_observer; | |
| 196 adapter->AddObserver(&adapter_observer); | |
| 197 | |
| 198 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 199 .Times(1); | |
| 200 | |
| 201 static_cast<BluetoothManagerClient::Observer*>(adapter.get()) | |
| 202 ->DefaultAdapterChanged(adapter_path); | |
| 203 | |
| 204 // Adapter should be present with the new address. | |
| 205 EXPECT_TRUE(adapter->IsPresent()); | |
| 206 EXPECT_EQ(adapter_address, adapter->address()); | |
| 207 } | |
| 208 | |
| 209 TEST_F(BluetoothAdapterTest, DefaultAdapterReplacedWithAddress) { | |
| 210 const dbus::ObjectPath initial_adapter_path("/fake/hci0"); | |
| 211 const dbus::ObjectPath new_adapter_path("/fake/hci1"); | |
| 212 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 213 const std::string new_adapter_address = "BA:C0:11:CO:FE:FE"; | |
| 214 | |
| 215 // Create the default adapter instance; | |
| 216 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 217 // a callback to obtain the adapter path. | |
| 218 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 219 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 220 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 221 | |
| 222 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 223 | |
| 224 // Call the adapter callback; | |
| 225 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 226 // the property set. | |
| 227 MockBluetoothAdapterClient::Properties initial_adapter_properties; | |
| 228 initial_adapter_properties.address.ReplaceValue(initial_adapter_address); | |
| 229 | |
| 230 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path)) | |
| 231 .WillRepeatedly(Return(&initial_adapter_properties)); | |
| 232 | |
| 233 adapter_callback.Run(initial_adapter_path, true); | |
| 234 | |
| 235 // Tell the adapter the default adapter changed; | |
| 236 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 237 // the property set. | |
| 238 MockBluetoothAdapterClient::Properties new_adapter_properties; | |
| 239 new_adapter_properties.address.ReplaceValue(new_adapter_address); | |
| 240 | |
| 241 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path)) | |
| 242 .WillRepeatedly(Return(&new_adapter_properties)); | |
| 243 | |
| 244 // BluetoothAdapter::Observer::AdapterPresentChanged must be called once | |
| 245 // with false to indicate the old adapter being removed and once with true | |
| 246 // to announce the new adapter. | |
| 247 MockBluetoothAdapter::Observer adapter_observer; | |
| 248 adapter->AddObserver(&adapter_observer); | |
| 249 | |
| 250 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false)) | |
| 251 .Times(1); | |
| 252 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 253 .Times(1); | |
| 254 | |
| 255 static_cast<BluetoothManagerClient::Observer*>(adapter.get()) | |
| 256 ->DefaultAdapterChanged(new_adapter_path); | |
| 257 | |
| 258 // Adapter should be present with the new address. | |
| 259 EXPECT_TRUE(adapter->IsPresent()); | |
| 260 EXPECT_EQ(new_adapter_address, adapter->address()); | |
| 261 } | |
| 262 | |
| 263 TEST_F(BluetoothAdapterTest, DefaultAdapterBecomesPresentWithoutAddress) { | |
| 264 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 265 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 266 | |
| 267 // Create the default adapter instance; | |
| 268 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 269 // a callback to obtain the adapter path. | |
| 270 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 271 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 272 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 273 | |
| 274 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 275 | |
| 276 // Call the adapter callback; make out it failed. | |
| 277 adapter_callback.Run(dbus::ObjectPath(""), false); | |
| 278 | |
| 279 // Tell the adapter the default adapter changed; | |
| 280 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 281 // the property set. | |
| 282 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 283 | |
| 284 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 285 .WillRepeatedly(Return(&adapter_properties)); | |
| 286 | |
| 287 // BluetoothAdapter::Observer::AdapterPresentChanged must not be called. | |
| 288 MockBluetoothAdapter::Observer adapter_observer; | |
| 289 adapter->AddObserver(&adapter_observer); | |
| 290 | |
| 291 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), _)) | |
| 292 .Times(0); | |
| 293 | |
| 294 static_cast<BluetoothManagerClient::Observer*>(adapter.get()) | |
| 295 ->DefaultAdapterChanged(adapter_path); | |
| 296 | |
| 297 // Adapter should not be present yet. | |
| 298 EXPECT_FALSE(adapter->IsPresent()); | |
| 299 | |
| 300 // Tell the adapter the address now; | |
| 301 // BluetoothAdapter::Observer::AdapterPresentChanged now must be called. | |
| 302 adapter_properties.address.ReplaceValue(adapter_address); | |
| 303 | |
| 304 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 305 .Times(1); | |
| 306 | |
| 307 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 308 ->AdapterPropertyChanged(adapter_path, | |
| 309 adapter_properties.address.name()); | |
| 310 | |
| 311 // Adapter should be present with the new address. | |
| 312 EXPECT_TRUE(adapter->IsPresent()); | |
| 313 EXPECT_EQ(adapter_address, adapter->address()); | |
| 314 } | |
| 315 | |
| 316 TEST_F(BluetoothAdapterTest, DefaultAdapterReplacedWithoutAddress) { | |
| 317 const dbus::ObjectPath initial_adapter_path("/fake/hci0"); | |
| 318 const dbus::ObjectPath new_adapter_path("/fake/hci1"); | |
| 319 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 320 const std::string new_adapter_address = "BA:C0:11:CO:FE:FE"; | |
| 321 | |
| 322 // Create the default adapter instance; | |
| 323 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 324 // a callback to obtain the adapter path. | |
| 325 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 326 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 327 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 328 | |
| 329 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 330 | |
| 331 // Call the adapter callback; | |
| 332 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 333 // the property set. | |
| 334 MockBluetoothAdapterClient::Properties initial_adapter_properties; | |
| 335 initial_adapter_properties.address.ReplaceValue(initial_adapter_address); | |
| 336 | |
| 337 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path)) | |
| 338 .WillRepeatedly(Return(&initial_adapter_properties)); | |
| 339 | |
| 340 adapter_callback.Run(initial_adapter_path, true); | |
| 341 | |
| 342 // Tell the adapter the default adapter changed; | |
| 343 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 344 // the property set. | |
| 345 MockBluetoothAdapterClient::Properties new_adapter_properties; | |
| 346 | |
| 347 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path)) | |
| 348 .WillRepeatedly(Return(&new_adapter_properties)); | |
| 349 | |
| 350 // BluetoothAdapter::Observer::AdapterPresentChanged must be called to | |
| 351 // indicate the adapter has gone away. | |
| 352 MockBluetoothAdapter::Observer adapter_observer; | |
| 353 adapter->AddObserver(&adapter_observer); | |
| 354 | |
| 355 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false)) | |
| 356 .Times(1); | |
| 357 | |
| 358 static_cast<BluetoothManagerClient::Observer*>(adapter.get()) | |
| 359 ->DefaultAdapterChanged(new_adapter_path); | |
| 360 | |
| 361 // Adapter should be now marked not present. | |
| 362 EXPECT_FALSE(adapter->IsPresent()); | |
| 363 | |
| 364 // Tell the adapter the address now; | |
| 365 // BluetoothAdapter::Observer::AdapterPresentChanged now must be called. | |
| 366 new_adapter_properties.address.ReplaceValue(new_adapter_address); | |
| 367 | |
| 368 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 369 .Times(1); | |
| 370 | |
| 371 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 372 ->AdapterPropertyChanged(new_adapter_path, | |
| 373 new_adapter_properties.address.name()); | |
| 374 | |
| 375 // Adapter should be present with the new address. | |
| 376 EXPECT_TRUE(adapter->IsPresent()); | |
| 377 EXPECT_EQ(new_adapter_address, adapter->address()); | |
| 378 } | |
| 379 | |
| 380 TEST_F(BluetoothAdapterTest, DefaultAdapterRemoved) { | |
| 381 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 382 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 383 | |
| 384 // Create the default adapter instance; | |
| 385 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 386 // a callback to obtain the adapter path. | |
| 387 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 388 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 389 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 390 | |
| 391 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 392 | |
| 393 // Call the adapter callback; | |
| 394 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 395 // the property set. | |
| 396 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 397 adapter_properties.address.ReplaceValue(adapter_address); | |
| 398 | |
| 399 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 400 .WillRepeatedly(Return(&adapter_properties)); | |
| 401 | |
| 402 adapter_callback.Run(adapter_path, true); | |
| 403 | |
| 404 // Report that the adapter has been removed; | |
| 405 // BluetoothAdapter::Observer::AdapterPresentChanged will be called to | |
| 406 // indicate the adapter is no longer present. | |
| 407 MockBluetoothAdapter::Observer adapter_observer; | |
| 408 adapter->AddObserver(&adapter_observer); | |
| 409 | |
| 410 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false)) | |
| 411 .Times(1); | |
| 412 | |
| 413 static_cast<BluetoothManagerClient::Observer*>(adapter.get()) | |
| 414 ->AdapterRemoved(adapter_path); | |
| 415 | |
| 416 // Adapter should be no longer present. | |
| 417 EXPECT_FALSE(adapter->IsPresent()); | |
| 418 } | |
| 419 | |
| 420 TEST_F(BluetoothAdapterTest, DefaultAdapterWithoutAddressRemoved) { | |
| 421 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 422 | |
| 423 // Create the default adapter instance; | |
| 424 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 425 // a callback to obtain the adapter path. | |
| 426 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 427 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 428 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 429 | |
| 430 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 431 | |
| 432 // Call the adapter callback; | |
| 433 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 434 // the property set. | |
| 435 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 436 | |
| 437 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 438 .WillRepeatedly(Return(&adapter_properties)); | |
| 439 | |
| 440 adapter_callback.Run(adapter_path, true); | |
| 441 | |
| 442 // Report that the adapter has been removed; | |
| 443 // BluetoothAdapter::Observer::AdapterPresentChanged must not be called | |
| 444 // since we never should have announced it in the first place. | |
| 445 MockBluetoothAdapter::Observer adapter_observer; | |
| 446 adapter->AddObserver(&adapter_observer); | |
| 447 | |
| 448 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), _)) | |
| 449 .Times(0); | |
| 450 | |
| 451 static_cast<BluetoothManagerClient::Observer*>(adapter.get()) | |
| 452 ->AdapterRemoved(adapter_path); | |
| 453 | |
| 454 // Adapter should be still no longer present. | |
| 455 EXPECT_FALSE(adapter->IsPresent()); | |
| 456 } | |
| 457 | |
| 458 TEST_F(BluetoothAdapterTest, DefaultAdapterPoweredPropertyInitiallyFalse) { | |
| 459 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 460 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 461 | |
| 462 // Create the default adapter instance; | |
| 463 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 464 // a callback to obtain the adapter path. | |
| 465 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 466 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 467 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 468 | |
| 469 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 470 | |
| 471 // Call the adapter callback; | |
| 472 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 473 // the property set. | |
| 474 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 475 adapter_properties.address.ReplaceValue(adapter_address); | |
| 476 adapter_properties.powered.ReplaceValue(false); | |
| 477 | |
| 478 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 479 .WillRepeatedly(Return(&adapter_properties)); | |
| 480 | |
| 481 adapter_callback.Run(adapter_path, true); | |
| 482 | |
| 483 // Adapter should have the correct property value. | |
| 484 EXPECT_FALSE(adapter->IsPowered()); | |
| 485 } | |
| 486 | |
| 487 TEST_F(BluetoothAdapterTest, DefaultAdapterPoweredPropertyInitiallyTrue) { | |
| 488 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 489 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 490 | |
| 491 // Create the default adapter instance; | |
| 492 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 493 // a callback to obtain the adapter path. | |
| 494 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 495 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 496 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 497 | |
| 498 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 499 | |
| 500 // Call the adapter callback; | |
| 501 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 502 // the property set. | |
| 503 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 504 adapter_properties.address.ReplaceValue(adapter_address); | |
| 505 adapter_properties.powered.ReplaceValue(true); | |
| 506 | |
| 507 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 508 .WillRepeatedly(Return(&adapter_properties)); | |
| 509 | |
| 510 // BluetoothAdapter::Observer::AdapterPoweredChanged will be called. | |
| 511 MockBluetoothAdapter::Observer adapter_observer; | |
| 512 adapter->AddObserver(&adapter_observer); | |
| 513 | |
| 514 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 515 .Times(1); | |
| 516 | |
| 517 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), true)) | |
| 518 .Times(1); | |
| 519 | |
| 520 adapter_callback.Run(adapter_path, true); | |
| 521 | |
| 522 // Adapter should have the correct property value. | |
| 523 EXPECT_TRUE(adapter->IsPowered()); | |
| 524 } | |
| 525 | |
| 526 TEST_F(BluetoothAdapterTest, | |
| 527 DefaultAdapterPoweredPropertyInitiallyTrueWithoutAddress) { | |
| 528 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 529 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 530 | |
| 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)); | |
| 537 | |
| 538 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 539 | |
| 540 // Call the adapter callback; | |
| 541 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 542 // the property set but BluetoothAdapter::Observer::AdapterPoweredChanged | |
| 543 // should not yet be called. | |
| 544 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 545 adapter_properties.powered.ReplaceValue(true); | |
| 546 | |
| 547 MockBluetoothAdapter::Observer adapter_observer; | |
| 548 adapter->AddObserver(&adapter_observer); | |
| 549 | |
| 550 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 551 .WillRepeatedly(Return(&adapter_properties)); | |
| 552 | |
| 553 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), _)) | |
| 554 .Times(0); | |
| 555 | |
| 556 adapter_callback.Run(adapter_path, true); | |
| 557 | |
| 558 // Adapter should not yet have the property value. | |
| 559 EXPECT_FALSE(adapter->IsPowered()); | |
| 560 | |
| 561 // Tell the adapter the address now, | |
| 562 // BluetoothAdapter::Observer::AdapterPresentChanged and | |
| 563 // BluetoothAdapter::Observer::AdapterPoweredChanged now must be called. | |
| 564 adapter_properties.address.ReplaceValue(adapter_address); | |
| 565 | |
| 566 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 567 .Times(1); | |
| 568 | |
| 569 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), true)) | |
| 570 .Times(1); | |
| 571 | |
| 572 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 573 ->AdapterPropertyChanged(adapter_path, | |
| 574 adapter_properties.address.name()); | |
| 575 | |
| 576 // Adapter should have the correct property value. | |
| 577 EXPECT_TRUE(adapter->IsPowered()); | |
| 578 } | |
| 579 | |
| 580 TEST_F(BluetoothAdapterTest, DefaultAdapterPoweredPropertyChanged) { | |
| 581 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 582 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 583 | |
| 584 // Create the default adapter instance; | |
| 585 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 586 // a callback to obtain the adapter path. | |
| 587 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 588 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 589 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 590 | |
| 591 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 592 | |
| 593 // Call the adapter callback; | |
| 594 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 595 // the property set. | |
| 596 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 597 adapter_properties.address.ReplaceValue(adapter_address); | |
| 598 adapter_properties.powered.ReplaceValue(false); | |
| 599 | |
| 600 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 601 .WillRepeatedly(Return(&adapter_properties)); | |
| 602 | |
| 603 adapter_callback.Run(adapter_path, true); | |
| 604 | |
| 605 // Adapter should have the correct property value. | |
| 606 EXPECT_FALSE(adapter->IsPowered()); | |
| 607 | |
| 608 // Report that the property has been changed; | |
| 609 // BluetoothAdapter::Observer::AdapterPoweredChanged will be called. | |
| 610 MockBluetoothAdapter::Observer adapter_observer; | |
| 611 adapter->AddObserver(&adapter_observer); | |
| 612 | |
| 613 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), true)) | |
| 614 .Times(1); | |
| 615 | |
| 616 adapter_properties.powered.ReplaceValue(true); | |
| 617 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 618 ->AdapterPropertyChanged(adapter_path, | |
| 619 adapter_properties.powered.name()); | |
| 620 | |
| 621 // Adapter should have the new property values. | |
| 622 EXPECT_TRUE(adapter->IsPowered()); | |
| 623 } | |
| 624 | |
| 625 TEST_F(BluetoothAdapterTest, DefaultAdapterPoweredPropertyUnchanged) { | |
| 626 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 627 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 628 | |
| 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)); | |
| 635 | |
| 636 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 637 | |
| 638 // Call the adapter callback; | |
| 639 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 640 // the property set. | |
| 641 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 642 adapter_properties.address.ReplaceValue(adapter_address); | |
| 643 adapter_properties.powered.ReplaceValue(true); | |
| 644 | |
| 645 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 646 .WillRepeatedly(Return(&adapter_properties)); | |
| 647 | |
| 648 adapter_callback.Run(adapter_path, true); | |
| 649 | |
| 650 // Adapter should have the correct property value. | |
| 651 EXPECT_TRUE(adapter->IsPowered()); | |
| 652 | |
| 653 // Report that the property has been changed, but don't change the value; | |
| 654 // BluetoothAdapter::Observer::AdapterPoweredChanged should not be called. | |
| 655 MockBluetoothAdapter::Observer adapter_observer; | |
| 656 adapter->AddObserver(&adapter_observer); | |
| 657 | |
| 658 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), _)) | |
| 659 .Times(0); | |
| 660 | |
| 661 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 662 ->AdapterPropertyChanged(adapter_path, | |
| 663 adapter_properties.powered.name()); | |
| 664 | |
| 665 // Adapter should still have the same property values. | |
| 666 EXPECT_TRUE(adapter->IsPowered()); | |
| 667 } | |
| 668 | |
| 669 TEST_F(BluetoothAdapterTest, | |
| 670 DefaultAdapterPoweredPropertyChangedWithoutAddress) { | |
| 671 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 672 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 673 | |
| 674 // Create the default adapter instance; | |
| 675 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 676 // a callback to obtain the adapter path. | |
| 677 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 678 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 679 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 680 | |
| 681 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 682 | |
| 683 // Call the adapter callback; | |
| 684 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 685 // the property set but BluetoothAdapter::Observer::AdapterPoweredChanged | |
| 686 // should not yet be called. | |
| 687 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 688 | |
| 689 MockBluetoothAdapter::Observer adapter_observer; | |
| 690 adapter->AddObserver(&adapter_observer); | |
| 691 | |
| 692 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 693 .WillRepeatedly(Return(&adapter_properties)); | |
| 694 | |
| 695 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), _)) | |
| 696 .Times(0); | |
| 697 | |
| 698 adapter_callback.Run(adapter_path, true); | |
| 699 | |
| 700 // Tell the adapter that its powered property changed, the observer | |
| 701 // method should still not be called because there is no address for | |
| 702 // the adapter so it is not present. | |
| 703 adapter_properties.powered.ReplaceValue(true); | |
| 704 | |
| 705 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), _)) | |
| 706 .Times(0); | |
| 707 | |
| 708 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 709 ->AdapterPropertyChanged(adapter_path, | |
| 710 adapter_properties.powered.name()); | |
| 711 | |
| 712 // Adapter should not yet have the property value. | |
| 713 EXPECT_FALSE(adapter->IsPowered()); | |
| 714 | |
| 715 // Tell the adapter the address now, | |
| 716 // BluetoothAdapter::Observer::AdapterPresentChanged and | |
| 717 // BluetoothAdapter::Observer::AdapterPoweredChanged now must be called. | |
| 718 adapter_properties.address.ReplaceValue(adapter_address); | |
| 719 | |
| 720 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 721 .Times(1); | |
| 722 | |
| 723 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), true)) | |
| 724 .Times(1); | |
| 725 | |
| 726 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 727 ->AdapterPropertyChanged(adapter_path, | |
| 728 adapter_properties.address.name()); | |
| 729 | |
| 730 // Adapter should now have the correct property value. | |
| 731 EXPECT_TRUE(adapter->IsPowered()); | |
| 732 } | |
| 733 | |
| 734 TEST_F(BluetoothAdapterTest, DefaultAdapterPoweredPropertyResetOnReplace) { | |
| 735 const dbus::ObjectPath initial_adapter_path("/fake/hci0"); | |
| 736 const dbus::ObjectPath new_adapter_path("/fake/hci1"); | |
| 737 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 738 const std::string new_adapter_address = "00:C0:11:CO:FE:FE"; | |
| 739 | |
| 740 // Create the default adapter instance; | |
| 741 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 742 // a callback to obtain the adapter path. | |
| 743 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 744 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 745 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 746 | |
| 747 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 748 | |
| 749 // Call the adapter callback; | |
| 750 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 751 // the property set. | |
| 752 MockBluetoothAdapterClient::Properties initial_adapter_properties; | |
| 753 initial_adapter_properties.address.ReplaceValue(initial_adapter_address); | |
| 754 initial_adapter_properties.powered.ReplaceValue(true); | |
| 755 | |
| 756 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path)) | |
| 757 .WillRepeatedly(Return(&initial_adapter_properties)); | |
| 758 | |
| 759 adapter_callback.Run(initial_adapter_path, true); | |
| 760 | |
| 761 // Tell the adapter the default adapter changed; | |
| 762 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 763 // the property set. | |
| 764 MockBluetoothAdapterClient::Properties new_adapter_properties; | |
| 765 new_adapter_properties.address.ReplaceValue(new_adapter_address); | |
| 766 | |
| 767 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path)) | |
| 768 .WillRepeatedly(Return(&new_adapter_properties)); | |
| 769 | |
| 770 // BluetoothAdapter::Observer::AdapterPoweredChanged will be called. | |
| 771 MockBluetoothAdapter::Observer adapter_observer; | |
| 772 adapter->AddObserver(&adapter_observer); | |
| 773 | |
| 774 { | |
| 775 InSequence s; | |
| 776 | |
| 777 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false)) | |
| 778 .Times(1); | |
| 779 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 780 .Times(1); | |
| 781 } | |
| 782 | |
| 783 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), false)) | |
| 784 .Times(1); | |
| 785 | |
| 786 static_cast<BluetoothManagerClient::Observer*>(adapter.get()) | |
| 787 ->DefaultAdapterChanged(new_adapter_path); | |
| 788 | |
| 789 // Adapter should have the new property value. | |
| 790 EXPECT_FALSE(adapter->IsPowered()); | |
| 791 } | |
| 792 | |
| 793 TEST_F(BluetoothAdapterTest, | |
| 794 DefaultAdapterPoweredPropertyResetOnReplaceWhenTrue) { | |
| 795 const dbus::ObjectPath initial_adapter_path("/fake/hci0"); | |
| 796 const dbus::ObjectPath new_adapter_path("/fake/hci1"); | |
| 797 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 798 const std::string new_adapter_address = "BA:C0:11:CO:FE:FE"; | |
| 799 | |
| 800 // Create the default adapter instance; | |
| 801 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 802 // a callback to obtain the adapter path. | |
| 803 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 804 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 805 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 806 | |
| 807 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 808 | |
| 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); | |
| 815 | |
| 816 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path)) | |
| 817 .WillRepeatedly(Return(&initial_adapter_properties)); | |
| 818 | |
| 819 adapter_callback.Run(initial_adapter_path, true); | |
| 820 | |
| 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); | |
| 826 new_adapter_properties.powered.ReplaceValue(true); | |
| 827 | |
| 828 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path)) | |
| 829 .WillRepeatedly(Return(&new_adapter_properties)); | |
| 830 | |
| 831 // BluetoothAdapter::Observer::AdapterPoweredChanged will be called once | |
| 832 // to set the value to false for the previous adapter and once to set the | |
| 833 // value to true for the new adapter. | |
| 834 MockBluetoothAdapter::Observer adapter_observer; | |
| 835 adapter->AddObserver(&adapter_observer); | |
| 836 | |
| 837 { | |
| 838 InSequence s; | |
| 839 | |
| 840 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false)) | |
| 841 .Times(1); | |
| 842 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 843 .Times(1); | |
| 844 } | |
| 845 | |
| 846 { | |
| 847 InSequence s; | |
| 848 | |
| 849 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), false)) | |
| 850 .Times(1); | |
| 851 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), true)) | |
| 852 .Times(1); | |
| 853 } | |
| 854 | |
| 855 static_cast<BluetoothManagerClient::Observer*>(adapter.get()) | |
| 856 ->DefaultAdapterChanged(new_adapter_path); | |
| 857 | |
| 858 // Adapter should have the new property value. | |
| 859 EXPECT_TRUE(adapter->IsPowered()); | |
| 860 } | |
| 861 | |
| 862 TEST_F(BluetoothAdapterTest, DefaultAdapterPoweredPropertyResetOnRemove) { | |
| 863 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 864 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 865 | |
| 866 // Create the default adapter instance; | |
| 867 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 868 // a callback to obtain the adapter path. | |
| 869 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 870 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 871 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 872 | |
| 873 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 874 | |
| 875 // Call the adapter callback; | |
| 876 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 877 // the property set. | |
| 878 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 879 adapter_properties.address.ReplaceValue(adapter_address); | |
| 880 adapter_properties.powered.ReplaceValue(true); | |
| 881 | |
| 882 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 883 .WillRepeatedly(Return(&adapter_properties)); | |
| 884 | |
| 885 adapter_callback.Run(adapter_path, true); | |
| 886 | |
| 887 // Report that the adapter has been removed; | |
| 888 // BluetoothAdapter::Observer::AdapterPoweredChanged will be called. | |
| 889 MockBluetoothAdapter::Observer adapter_observer; | |
| 890 adapter->AddObserver(&adapter_observer); | |
| 891 | |
| 892 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false)) | |
| 893 .Times(1); | |
| 894 EXPECT_CALL(adapter_observer, AdapterPoweredChanged(adapter.get(), false)) | |
| 895 .Times(1); | |
| 896 | |
| 897 static_cast<BluetoothManagerClient::Observer*>(adapter.get()) | |
| 898 ->AdapterRemoved(adapter_path); | |
| 899 | |
| 900 // Adapter should have the new property value. | |
| 901 EXPECT_FALSE(adapter->IsPowered()); | |
| 902 } | |
| 903 | |
| 904 TEST_F(BluetoothAdapterTest, DefaultAdapterSetPowered) { | |
| 905 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 906 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 907 | |
| 908 // Create the default adapter instance; | |
| 909 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 910 // a callback to obtain the adapter path. | |
| 911 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 912 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 913 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 914 | |
| 915 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 916 | |
| 917 // Call the adapter callback; | |
| 918 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 919 // the property set. | |
| 920 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 921 | |
| 922 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 923 .WillRepeatedly(Return(&adapter_properties)); | |
| 924 | |
| 925 adapter_callback.Run(adapter_path, true); | |
| 926 | |
| 927 // Request that the powered property be changed; | |
| 928 // MockBluetoothAdapterClient::Set should be called, passing the address | |
| 929 // of the powered property and a callback to receive the response. | |
| 930 dbus::PropertySet::SetCallback set_callback; | |
| 931 EXPECT_CALL(adapter_properties, Set(&adapter_properties.powered, _)) | |
| 932 .WillOnce(SaveArg<1>(&set_callback)); | |
| 933 | |
| 934 adapter->SetPowered(true, | |
| 935 base::Bind(&BluetoothAdapterTest::SetCallback, | |
| 936 base::Unretained(this)), | |
| 937 base::Bind(&BluetoothAdapterTest::ErrorCallback, | |
| 938 base::Unretained(this))); | |
| 939 | |
| 940 // Reply to the callback to indicate success, the set callback we provided | |
| 941 // should be called but the properties should not be refetched. | |
| 942 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 943 .Times(0); | |
| 944 | |
| 945 set_callback.Run(true); | |
| 946 | |
| 947 EXPECT_TRUE(set_callback_called_); | |
| 948 EXPECT_FALSE(error_callback_called_); | |
| 949 } | |
| 950 | |
| 951 TEST_F(BluetoothAdapterTest, DefaultAdapterSetPoweredError) { | |
| 952 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 953 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 954 | |
| 955 // Create the default adapter instance; | |
| 956 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 957 // a callback to obtain the adapter path. | |
| 958 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 959 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 960 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 961 | |
| 962 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 963 | |
| 964 // Call the adapter callback; | |
| 965 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 966 // the property set. | |
| 967 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 968 | |
| 969 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 970 .WillRepeatedly(Return(&adapter_properties)); | |
| 971 | |
| 972 adapter_callback.Run(adapter_path, true); | |
| 973 | |
| 974 // Request that the powered property be changed; | |
| 975 // MockBluetoothAdapterClient::Set should be called, passing the address | |
| 976 // of the powered property and a callback to receive the response. | |
| 977 dbus::PropertySet::SetCallback set_callback; | |
| 978 EXPECT_CALL(adapter_properties, Set(&adapter_properties.powered, _)) | |
| 979 .WillOnce(SaveArg<1>(&set_callback)); | |
| 980 | |
| 981 adapter->SetPowered(true, | |
| 982 base::Bind(&BluetoothAdapterTest::SetCallback, | |
| 983 base::Unretained(this)), | |
| 984 base::Bind(&BluetoothAdapterTest::ErrorCallback, | |
| 985 base::Unretained(this))); | |
| 986 | |
| 987 // Reply to the callback to indicate failure, the error callback we provided | |
| 988 // should be called but the properties should not be refetched. | |
| 989 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 990 .Times(0); | |
| 991 | |
| 992 set_callback.Run(false); | |
| 993 | |
| 994 EXPECT_FALSE(set_callback_called_); | |
| 995 EXPECT_TRUE(error_callback_called_); | |
| 996 } | |
| 997 | |
| 998 TEST_F(BluetoothAdapterTest, DefaultAdapterDiscoveringPropertyInitiallyFalse) { | |
| 999 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 1000 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 1001 | |
| 1002 // Create the default adapter instance; | |
| 1003 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 1004 // a callback to obtain the adapter path. | |
| 1005 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 1006 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 1007 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 1008 | |
| 1009 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 1010 | |
| 1011 // Call the adapter callback; | |
| 1012 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 1013 // the property set. | |
| 1014 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 1015 adapter_properties.address.ReplaceValue(adapter_address); | |
| 1016 adapter_properties.discovering.ReplaceValue(false); | |
| 1017 | |
| 1018 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 1019 .WillRepeatedly(Return(&adapter_properties)); | |
| 1020 | |
| 1021 adapter_callback.Run(adapter_path, true); | |
| 1022 | |
| 1023 // Adapter should have the correct property value. | |
| 1024 EXPECT_FALSE(adapter->IsDiscovering()); | |
| 1025 } | |
| 1026 | |
| 1027 TEST_F(BluetoothAdapterTest, DefaultAdapterDiscoveringPropertyInitiallyTrue) { | |
| 1028 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 1029 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 1030 | |
| 1031 // Create the default adapter instance; | |
| 1032 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 1033 // a callback to obtain the adapter path. | |
| 1034 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 1035 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 1036 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 1037 | |
| 1038 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 1039 | |
| 1040 // Call the adapter callback; | |
| 1041 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 1042 // the property set. | |
| 1043 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 1044 adapter_properties.address.ReplaceValue(adapter_address); | |
| 1045 adapter_properties.discovering.ReplaceValue(true); | |
| 1046 | |
| 1047 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 1048 .WillRepeatedly(Return(&adapter_properties)); | |
| 1049 | |
| 1050 // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called. | |
| 1051 MockBluetoothAdapter::Observer adapter_observer; | |
| 1052 adapter->AddObserver(&adapter_observer); | |
| 1053 | |
| 1054 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 1055 .Times(1); | |
| 1056 | |
| 1057 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), true)) | |
| 1058 .Times(1); | |
| 1059 | |
| 1060 adapter_callback.Run(adapter_path, true); | |
| 1061 | |
| 1062 // Adapter should have the correct property value. | |
| 1063 EXPECT_TRUE(adapter->IsDiscovering()); | |
| 1064 } | |
| 1065 | |
| 1066 TEST_F(BluetoothAdapterTest, | |
| 1067 DefaultAdapterDiscoveringPropertyInitiallyTrueWithoutAddress) { | |
| 1068 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 1069 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 1070 | |
| 1071 // Create the default adapter instance; | |
| 1072 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 1073 // a callback to obtain the adapter path. | |
| 1074 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 1075 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 1076 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 1077 | |
| 1078 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 1079 | |
| 1080 // Call the adapter callback; | |
| 1081 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 1082 // the property set but BluetoothAdapter::Observer::AdapterDiscoveringChanged | |
| 1083 // should not yet be called. | |
| 1084 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 1085 adapter_properties.discovering.ReplaceValue(true); | |
| 1086 | |
| 1087 MockBluetoothAdapter::Observer adapter_observer; | |
| 1088 adapter->AddObserver(&adapter_observer); | |
| 1089 | |
| 1090 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 1091 .WillRepeatedly(Return(&adapter_properties)); | |
| 1092 | |
| 1093 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), _)) | |
| 1094 .Times(0); | |
| 1095 | |
| 1096 adapter_callback.Run(adapter_path, true); | |
| 1097 | |
| 1098 // Adapter should not yet have the property value. | |
| 1099 EXPECT_FALSE(adapter->IsDiscovering()); | |
| 1100 | |
| 1101 // Tell the adapter the address now, | |
| 1102 // BluetoothAdapter::Observer::AdapterPresentChanged and | |
| 1103 // BluetoothAdapter::Observer::AdapterDiscoveringChanged now must be called. | |
| 1104 adapter_properties.address.ReplaceValue(adapter_address); | |
| 1105 | |
| 1106 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 1107 .Times(1); | |
| 1108 | |
| 1109 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), true)) | |
| 1110 .Times(1); | |
| 1111 | |
| 1112 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 1113 ->AdapterPropertyChanged(adapter_path, | |
| 1114 adapter_properties.address.name()); | |
| 1115 | |
| 1116 // Adapter should have the correct property value. | |
| 1117 EXPECT_TRUE(adapter->IsDiscovering()); | |
| 1118 } | |
| 1119 | |
| 1120 TEST_F(BluetoothAdapterTest, DefaultAdapterDiscoveringPropertyChanged) { | |
| 1121 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 1122 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 1123 | |
| 1124 // Create the default adapter instance; | |
| 1125 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 1126 // a callback to obtain the adapter path. | |
| 1127 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 1128 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 1129 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 1130 | |
| 1131 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 1132 | |
| 1133 // Call the adapter callback; | |
| 1134 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 1135 // the property set. | |
| 1136 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 1137 adapter_properties.address.ReplaceValue(adapter_address); | |
| 1138 adapter_properties.discovering.ReplaceValue(false); | |
| 1139 | |
| 1140 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 1141 .WillRepeatedly(Return(&adapter_properties)); | |
| 1142 | |
| 1143 adapter_callback.Run(adapter_path, true); | |
| 1144 | |
| 1145 // Adapter should have the correct property value. | |
| 1146 EXPECT_FALSE(adapter->IsDiscovering()); | |
| 1147 | |
| 1148 // Report that the property has been changed; | |
| 1149 // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called. | |
| 1150 MockBluetoothAdapter::Observer adapter_observer; | |
| 1151 adapter->AddObserver(&adapter_observer); | |
| 1152 | |
| 1153 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), true)) | |
| 1154 .Times(1); | |
| 1155 | |
| 1156 adapter_properties.discovering.ReplaceValue(true); | |
| 1157 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 1158 ->AdapterPropertyChanged(adapter_path, | |
| 1159 adapter_properties.discovering.name()); | |
| 1160 | |
| 1161 // Adapter should have the new property values. | |
| 1162 EXPECT_TRUE(adapter->IsDiscovering()); | |
| 1163 } | |
| 1164 | |
| 1165 TEST_F(BluetoothAdapterTest, DefaultAdapterDiscoveringPropertyUnchanged) { | |
| 1166 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 1167 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 1168 | |
| 1169 // Create the default adapter instance; | |
| 1170 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 1171 // a callback to obtain the adapter path. | |
| 1172 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 1173 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 1174 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 1175 | |
| 1176 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 1177 | |
| 1178 // Call the adapter callback; | |
| 1179 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 1180 // the property set. | |
| 1181 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 1182 adapter_properties.address.ReplaceValue(adapter_address); | |
| 1183 adapter_properties.discovering.ReplaceValue(true); | |
| 1184 | |
| 1185 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 1186 .WillRepeatedly(Return(&adapter_properties)); | |
| 1187 | |
| 1188 adapter_callback.Run(adapter_path, true); | |
| 1189 | |
| 1190 // Adapter should have the correct property value. | |
| 1191 EXPECT_TRUE(adapter->IsDiscovering()); | |
| 1192 | |
| 1193 // Report that the property has been changed, but don't change the value; | |
| 1194 // BluetoothAdapter::Observer::AdapterDiscoveringChanged should not be | |
| 1195 // called. | |
| 1196 MockBluetoothAdapter::Observer adapter_observer; | |
| 1197 adapter->AddObserver(&adapter_observer); | |
| 1198 | |
| 1199 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), _)) | |
| 1200 .Times(0); | |
| 1201 | |
| 1202 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 1203 ->AdapterPropertyChanged(adapter_path, | |
| 1204 adapter_properties.discovering.name()); | |
| 1205 | |
| 1206 // Adapter should still have the same property values. | |
| 1207 EXPECT_TRUE(adapter->IsDiscovering()); | |
| 1208 } | |
| 1209 | |
| 1210 TEST_F(BluetoothAdapterTest, | |
| 1211 DefaultAdapterDiscoveringPropertyChangedWithoutAddress) { | |
| 1212 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 1213 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 1214 | |
| 1215 // Create the default adapter instance; | |
| 1216 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 1217 // a callback to obtain the adapter path. | |
| 1218 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 1219 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 1220 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 1221 | |
| 1222 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 1223 | |
| 1224 // Call the adapter callback; | |
| 1225 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 1226 // the property set but BluetoothAdapter::Observer::AdapterDiscoveringChanged | |
| 1227 // should not yet be called. | |
| 1228 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 1229 | |
| 1230 MockBluetoothAdapter::Observer adapter_observer; | |
| 1231 adapter->AddObserver(&adapter_observer); | |
| 1232 | |
| 1233 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 1234 .WillRepeatedly(Return(&adapter_properties)); | |
| 1235 | |
| 1236 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), _)) | |
| 1237 .Times(0); | |
| 1238 | |
| 1239 adapter_callback.Run(adapter_path, true); | |
| 1240 | |
| 1241 // Tell the adapter that its discovering property changed, the observer | |
| 1242 // method should still not be called because there is no address for | |
| 1243 // the adapter so it is not present. | |
| 1244 adapter_properties.discovering.ReplaceValue(true); | |
| 1245 | |
| 1246 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), _)) | |
| 1247 .Times(0); | |
| 1248 | |
| 1249 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 1250 ->AdapterPropertyChanged(adapter_path, | |
| 1251 adapter_properties.discovering.name()); | |
| 1252 | |
| 1253 // Adapter should not yet have the property value. | |
| 1254 EXPECT_FALSE(adapter->IsDiscovering()); | |
| 1255 | |
| 1256 // Tell the adapter the address now, | |
| 1257 // BluetoothAdapter::Observer::AdapterPresentChanged and | |
| 1258 // BluetoothAdapter::Observer::AdapterDiscoveringChanged now must be called. | |
| 1259 adapter_properties.address.ReplaceValue(adapter_address); | |
| 1260 | |
| 1261 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 1262 .Times(1); | |
| 1263 | |
| 1264 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), true)) | |
| 1265 .Times(1); | |
| 1266 | |
| 1267 static_cast<BluetoothAdapterClient::Observer*>(adapter.get()) | |
| 1268 ->AdapterPropertyChanged(adapter_path, | |
| 1269 adapter_properties.address.name()); | |
| 1270 | |
| 1271 // Adapter should now have the correct property value. | |
| 1272 EXPECT_TRUE(adapter->IsDiscovering()); | |
| 1273 } | |
| 1274 | |
| 1275 TEST_F(BluetoothAdapterTest, DefaultAdapterDiscoveringPropertyResetOnReplace) { | |
| 1276 const dbus::ObjectPath initial_adapter_path("/fake/hci0"); | |
| 1277 const dbus::ObjectPath new_adapter_path("/fake/hci1"); | |
| 1278 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 1279 const std::string new_adapter_address = "BA:C0:11:CO:FE:FE"; | |
| 1280 | |
| 1281 // Create the default adapter instance; | |
| 1282 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 1283 // a callback to obtain the adapter path. | |
| 1284 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 1285 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 1286 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 1287 | |
| 1288 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 1289 | |
| 1290 // Call the adapter callback; | |
| 1291 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 1292 // the property set. | |
| 1293 MockBluetoothAdapterClient::Properties initial_adapter_properties; | |
| 1294 initial_adapter_properties.address.ReplaceValue(initial_adapter_address); | |
| 1295 initial_adapter_properties.discovering.ReplaceValue(true); | |
| 1296 | |
| 1297 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path)) | |
| 1298 .WillRepeatedly(Return(&initial_adapter_properties)); | |
| 1299 | |
| 1300 adapter_callback.Run(initial_adapter_path, true); | |
| 1301 | |
| 1302 // Tell the adapter the default adapter changed; | |
| 1303 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 1304 // the property set. | |
| 1305 MockBluetoothAdapterClient::Properties new_adapter_properties; | |
| 1306 new_adapter_properties.address.ReplaceValue(new_adapter_address); | |
| 1307 | |
| 1308 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path)) | |
| 1309 .WillRepeatedly(Return(&new_adapter_properties)); | |
| 1310 | |
| 1311 // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called. | |
| 1312 MockBluetoothAdapter::Observer adapter_observer; | |
| 1313 adapter->AddObserver(&adapter_observer); | |
| 1314 | |
| 1315 { | |
| 1316 InSequence s; | |
| 1317 | |
| 1318 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false)) | |
| 1319 .Times(1); | |
| 1320 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 1321 .Times(1); | |
| 1322 } | |
| 1323 | |
| 1324 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), false)) | |
| 1325 .Times(1); | |
| 1326 | |
| 1327 static_cast<BluetoothManagerClient::Observer*>(adapter.get()) | |
| 1328 ->DefaultAdapterChanged(new_adapter_path); | |
| 1329 | |
| 1330 // Adapter should have the new property value. | |
| 1331 EXPECT_FALSE(adapter->IsDiscovering()); | |
| 1332 } | |
| 1333 | |
| 1334 TEST_F(BluetoothAdapterTest, | |
| 1335 DefaultAdapterDiscoveringPropertyResetOnReplaceWhenTrue) { | |
| 1336 const dbus::ObjectPath initial_adapter_path("/fake/hci0"); | |
| 1337 const dbus::ObjectPath new_adapter_path("/fake/hci1"); | |
| 1338 const std::string initial_adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 1339 const std::string new_adapter_address = "BA:C0:11:CO:FE:FE"; | |
| 1340 | |
| 1341 // Create the default adapter instance; | |
| 1342 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 1343 // a callback to obtain the adapter path. | |
| 1344 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 1345 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 1346 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 1347 | |
| 1348 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 1349 | |
| 1350 // Call the adapter callback; | |
| 1351 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 1352 // the property set. | |
| 1353 MockBluetoothAdapterClient::Properties initial_adapter_properties; | |
| 1354 initial_adapter_properties.address.ReplaceValue(initial_adapter_address); | |
| 1355 initial_adapter_properties.discovering.ReplaceValue(true); | |
| 1356 | |
| 1357 EXPECT_CALL(*mock_adapter_client_, GetProperties(initial_adapter_path)) | |
| 1358 .WillRepeatedly(Return(&initial_adapter_properties)); | |
| 1359 | |
| 1360 adapter_callback.Run(initial_adapter_path, true); | |
| 1361 | |
| 1362 // Tell the adapter the default adapter changed; | |
| 1363 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 1364 // the property set. | |
| 1365 MockBluetoothAdapterClient::Properties new_adapter_properties; | |
| 1366 new_adapter_properties.address.ReplaceValue(new_adapter_address); | |
| 1367 new_adapter_properties.discovering.ReplaceValue(true); | |
| 1368 | |
| 1369 EXPECT_CALL(*mock_adapter_client_, GetProperties(new_adapter_path)) | |
| 1370 .WillRepeatedly(Return(&new_adapter_properties)); | |
| 1371 | |
| 1372 // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called once | |
| 1373 // to set the value to false for the previous adapter and once to set the | |
| 1374 // value to true for the new adapter. | |
| 1375 MockBluetoothAdapter::Observer adapter_observer; | |
| 1376 adapter->AddObserver(&adapter_observer); | |
| 1377 | |
| 1378 { | |
| 1379 InSequence s; | |
| 1380 | |
| 1381 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false)) | |
| 1382 .Times(1); | |
| 1383 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), true)) | |
| 1384 .Times(1); | |
| 1385 } | |
| 1386 | |
| 1387 { | |
| 1388 InSequence s; | |
| 1389 | |
| 1390 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), | |
| 1391 false)) | |
| 1392 .Times(1); | |
| 1393 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), | |
| 1394 true)) | |
| 1395 .Times(1); | |
| 1396 } | |
| 1397 | |
| 1398 static_cast<BluetoothManagerClient::Observer*>(adapter.get()) | |
| 1399 ->DefaultAdapterChanged(new_adapter_path); | |
| 1400 | |
| 1401 // Adapter should have the new property value. | |
| 1402 EXPECT_TRUE(adapter->IsDiscovering()); | |
| 1403 } | |
| 1404 | |
| 1405 TEST_F(BluetoothAdapterTest, DefaultAdapterDiscoveringPropertyResetOnRemove) { | |
| 1406 const dbus::ObjectPath adapter_path("/fake/hci0"); | |
| 1407 const std::string adapter_address = "CA:FE:4A:C0:FE:FE"; | |
| 1408 | |
| 1409 // Create the default adapter instance; | |
| 1410 // BluetoothManagerClient::DefaultAdapter will be called once, passing | |
| 1411 // a callback to obtain the adapter path. | |
| 1412 BluetoothManagerClient::AdapterCallback adapter_callback; | |
| 1413 EXPECT_CALL(*mock_manager_client_, DefaultAdapter(_)) | |
| 1414 .WillOnce(SaveArg<0>(&adapter_callback)); | |
| 1415 | |
| 1416 scoped_refptr<BluetoothAdapter> adapter = BluetoothAdapter::DefaultAdapter(); | |
| 1417 | |
| 1418 // Call the adapter callback; | |
| 1419 // BluetoothAdapterClient::GetProperties will be called once to obtain | |
| 1420 // the property set. | |
| 1421 MockBluetoothAdapterClient::Properties adapter_properties; | |
| 1422 adapter_properties.address.ReplaceValue(adapter_address); | |
| 1423 adapter_properties.discovering.ReplaceValue(true); | |
| 1424 | |
| 1425 EXPECT_CALL(*mock_adapter_client_, GetProperties(adapter_path)) | |
| 1426 .WillRepeatedly(Return(&adapter_properties)); | |
| 1427 | |
| 1428 adapter_callback.Run(adapter_path, true); | |
| 1429 | |
| 1430 // Report that the adapter has been removed; | |
| 1431 // BluetoothAdapter::Observer::AdapterDiscoveringChanged will be called. | |
| 1432 MockBluetoothAdapter::Observer adapter_observer; | |
| 1433 adapter->AddObserver(&adapter_observer); | |
| 1434 | |
| 1435 EXPECT_CALL(adapter_observer, AdapterPresentChanged(adapter.get(), false)) | |
| 1436 .Times(1); | |
| 1437 EXPECT_CALL(adapter_observer, AdapterDiscoveringChanged(adapter.get(), false)) | |
| 1438 .Times(1); | |
| 1439 | |
| 1440 static_cast<BluetoothManagerClient::Observer*>(adapter.get()) | |
| 1441 ->AdapterRemoved(adapter_path); | |
| 1442 | |
| 1443 // Adapter should have the new property value. | |
| 1444 EXPECT_FALSE(adapter->IsDiscovering()); | |
| 1445 } | |
| 1446 | |
| 1447 } // namespace chromeos | |
| OLD | NEW |