| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include <string> | 5 #include <string> | 
| 6 | 6 | 
| 7 #include "base/bind.h" | 7 #include "base/bind.h" | 
|  | 8 #include "base/memory/ptr_util.h" | 
| 8 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" | 
| 9 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" | 
| 10 #include "base/test/test_simple_task_runner.h" | 11 #include "base/test/test_simple_task_runner.h" | 
| 11 #include "device/bluetooth/bluetooth_adapter.h" | 12 #include "device/bluetooth/bluetooth_adapter.h" | 
| 12 #include "device/bluetooth/bluetooth_adapter_win.h" | 13 #include "device/bluetooth/bluetooth_adapter_win.h" | 
| 13 #include "device/bluetooth/bluetooth_device.h" | 14 #include "device/bluetooth/bluetooth_device.h" | 
| 14 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" | 15 #include "device/bluetooth/bluetooth_discovery_session_outcome.h" | 
| 15 #include "device/bluetooth/bluetooth_task_manager_win.h" | 16 #include "device/bluetooth/bluetooth_task_manager_win.h" | 
| 16 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 17 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" | 
| 18 | 19 | 
| 19 namespace { | 20 namespace { | 
| 20 | 21 | 
| 21 const char kAdapterAddress[] = "A1:B2:C3:D4:E5:F6"; | 22 const char kAdapterAddress[] = "A1:B2:C3:D4:E5:F6"; | 
| 22 const char kAdapterName[] = "Bluetooth Adapter Name"; | 23 const char kAdapterName[] = "Bluetooth Adapter Name"; | 
| 23 | 24 | 
| 24 const char kTestAudioSdpName[] = "Audio"; | 25 const char kTestAudioSdpName[] = "Audio"; | 
| 25 const char kTestAudioSdpName2[] = "Audio2"; | 26 const char kTestAudioSdpName2[] = "Audio2"; | 
| 26 const char kTestAudioSdpBytes[] = | 27 const char kTestAudioSdpBytes[] = | 
| 27     "35510900000a00010001090001350319110a09000435103506190100090019350619001909" | 28     "35510900000a00010001090001350319110a09000435103506190100090019350619001909" | 
| 28     "010209000535031910020900093508350619110d090102090100250c417564696f20536f75" | 29     "010209000535031910020900093508350619110d090102090100250c417564696f20536f75" | 
| 29     "726365090311090001"; | 30     "726365090311090001"; | 
| 30 const device::BluetoothUUID kTestAudioSdpUuid("110a"); | 31 const device::BluetoothUUID kTestAudioSdpUuid("110a"); | 
| 31 |  | 
| 32 void MakeDeviceState(const std::string& name, |  | 
| 33                      const std::string& address, |  | 
| 34                      device::BluetoothTaskManagerWin::DeviceState* state) { |  | 
| 35   state->name = name; |  | 
| 36   state->address = address; |  | 
| 37   state->bluetooth_class = 0; |  | 
| 38   state->authenticated = false; |  | 
| 39   state->connected = false; |  | 
| 40 } |  | 
| 41 |  | 
| 42 }  // namespace | 32 }  // namespace | 
| 43 | 33 | 
| 44 namespace device { | 34 namespace device { | 
| 45 | 35 | 
| 46 class BluetoothAdapterWinTest : public testing::Test { | 36 class BluetoothAdapterWinTest : public testing::Test { | 
| 47  public: | 37  public: | 
| 48   BluetoothAdapterWinTest() | 38   BluetoothAdapterWinTest() | 
| 49       : ui_task_runner_(new base::TestSimpleTaskRunner()), | 39       : ui_task_runner_(new base::TestSimpleTaskRunner()), | 
| 50         bluetooth_task_runner_(new base::TestSimpleTaskRunner()), | 40         bluetooth_task_runner_(new base::TestSimpleTaskRunner()), | 
| 51         adapter_(new BluetoothAdapterWin( | 41         adapter_(new BluetoothAdapterWin( | 
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 107   scoped_refptr<BluetoothAdapter> adapter_; | 97   scoped_refptr<BluetoothAdapter> adapter_; | 
| 108   BluetoothAdapterWin* adapter_win_; | 98   BluetoothAdapterWin* adapter_win_; | 
| 109   TestBluetoothAdapterObserver observer_; | 99   TestBluetoothAdapterObserver observer_; | 
| 110   bool init_callback_called_; | 100   bool init_callback_called_; | 
| 111   int num_start_discovery_callbacks_; | 101   int num_start_discovery_callbacks_; | 
| 112   int num_start_discovery_error_callbacks_; | 102   int num_start_discovery_error_callbacks_; | 
| 113   int num_stop_discovery_callbacks_; | 103   int num_stop_discovery_callbacks_; | 
| 114   int num_stop_discovery_error_callbacks_; | 104   int num_stop_discovery_error_callbacks_; | 
| 115 }; | 105 }; | 
| 116 | 106 | 
|  | 107 BluetoothTaskManagerWin::DeviceState* MakeDeviceState( | 
|  | 108     const std::string& name, | 
|  | 109     const std::string& address) { | 
|  | 110   auto state = new BluetoothTaskManagerWin::DeviceState(); | 
|  | 111   state->name = name; | 
|  | 112   state->address = address; | 
|  | 113   state->bluetooth_class = 0; | 
|  | 114   state->authenticated = false; | 
|  | 115   state->connected = false; | 
|  | 116   return state; | 
|  | 117 } | 
|  | 118 | 
| 117 TEST_F(BluetoothAdapterWinTest, AdapterNotPresent) { | 119 TEST_F(BluetoothAdapterWinTest, AdapterNotPresent) { | 
| 118   BluetoothTaskManagerWin::AdapterState state; | 120   BluetoothTaskManagerWin::AdapterState state; | 
| 119   adapter_win_->AdapterStateChanged(state); | 121   adapter_win_->AdapterStateChanged(state); | 
| 120   EXPECT_FALSE(adapter_win_->IsPresent()); | 122   EXPECT_FALSE(adapter_win_->IsPresent()); | 
| 121 } | 123 } | 
| 122 | 124 | 
| 123 TEST_F(BluetoothAdapterWinTest, AdapterPresent) { | 125 TEST_F(BluetoothAdapterWinTest, AdapterPresent) { | 
| 124   BluetoothTaskManagerWin::AdapterState state; | 126   BluetoothTaskManagerWin::AdapterState state; | 
| 125   state.address = kAdapterAddress; | 127   state.address = kAdapterAddress; | 
| 126   state.name = kAdapterName; | 128   state.name = kAdapterName; | 
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 427       DiscoverySessionErrorCallback()); | 429       DiscoverySessionErrorCallback()); | 
| 428   ui_task_runner_->ClearPendingTasks(); | 430   ui_task_runner_->ClearPendingTasks(); | 
| 429   adapter_win_->DiscoveryStarted(false); | 431   adapter_win_->DiscoveryStarted(false); | 
| 430   ui_task_runner_->RunPendingTasks(); | 432   ui_task_runner_->RunPendingTasks(); | 
| 431   EXPECT_EQ(1, num_start_discovery_error_callbacks_); | 433   EXPECT_EQ(1, num_start_discovery_error_callbacks_); | 
| 432   EXPECT_EQ(1, num_stop_discovery_callbacks_); | 434   EXPECT_EQ(1, num_stop_discovery_callbacks_); | 
| 433   EXPECT_EQ(0, observer_.discovering_changed_count()); | 435   EXPECT_EQ(0, observer_.discovering_changed_count()); | 
| 434 } | 436 } | 
| 435 | 437 | 
| 436 TEST_F(BluetoothAdapterWinTest, DevicesPolled) { | 438 TEST_F(BluetoothAdapterWinTest, DevicesPolled) { | 
| 437   BluetoothTaskManagerWin::DeviceState* android_phone_state = | 439   auto* android_phone_state = MakeDeviceState("phone", "A1:B2:C3:D4:E5:E0"); | 
| 438       new BluetoothTaskManagerWin::DeviceState(); | 440   auto* laptop_state = MakeDeviceState("laptop", "A1:B2:C3:D4:E5:E1"); | 
| 439   MakeDeviceState("phone", "A1:B2:C3:D4:E5:E0", android_phone_state); | 441   auto* iphone_state = MakeDeviceState("phone", "A1:B2:C3:D4:E5:E2"); | 
| 440   BluetoothTaskManagerWin::DeviceState* laptop_state = | 442 | 
| 441       new BluetoothTaskManagerWin::DeviceState(); | 443   std::vector<std::unique_ptr<BluetoothTaskManagerWin::DeviceState>> devices; | 
| 442   MakeDeviceState("laptop", "A1:B2:C3:D4:E5:E1", laptop_state); | 444 | 
| 443   BluetoothTaskManagerWin::DeviceState* iphone_state = | 445   devices.push_back(base::WrapUnique(android_phone_state)); | 
| 444       new BluetoothTaskManagerWin::DeviceState(); | 446   devices.push_back(base::WrapUnique(laptop_state)); | 
| 445   MakeDeviceState("phone", "A1:B2:C3:D4:E5:E2", iphone_state); | 447   devices.push_back(base::WrapUnique(iphone_state)); | 
| 446   ScopedVector<BluetoothTaskManagerWin::DeviceState> devices; |  | 
| 447   devices.push_back(android_phone_state); |  | 
| 448   devices.push_back(laptop_state); |  | 
| 449   devices.push_back(iphone_state); |  | 
| 450 | 448 | 
| 451   // Add 3 devices | 449   // Add 3 devices | 
| 452   observer_.Reset(); | 450   observer_.Reset(); | 
| 453   adapter_win_->DevicesPolled(devices); | 451   adapter_win_->DevicesPolled(devices); | 
| 454   EXPECT_EQ(3, observer_.device_added_count()); | 452   EXPECT_EQ(3, observer_.device_added_count()); | 
| 455   EXPECT_EQ(0, observer_.device_removed_count()); | 453   EXPECT_EQ(0, observer_.device_removed_count()); | 
| 456   EXPECT_EQ(0, observer_.device_changed_count()); | 454   EXPECT_EQ(0, observer_.device_changed_count()); | 
| 457 | 455 | 
| 458   // Change a device name | 456   // Change a device name | 
| 459   android_phone_state->name = std::string("phone2"); | 457   android_phone_state->name = std::string("phone2"); | 
| (...skipping 17 matching lines...) Expand all  Loading... | 
| 477   adapter_win_->DevicesPolled(devices); | 475   adapter_win_->DevicesPolled(devices); | 
| 478   EXPECT_EQ(0, observer_.device_added_count()); | 476   EXPECT_EQ(0, observer_.device_added_count()); | 
| 479   EXPECT_EQ(1, observer_.device_removed_count()); | 477   EXPECT_EQ(1, observer_.device_removed_count()); | 
| 480   EXPECT_EQ(0, observer_.device_changed_count()); | 478   EXPECT_EQ(0, observer_.device_changed_count()); | 
| 481 | 479 | 
| 482   // Add a service | 480   // Add a service | 
| 483   BluetoothTaskManagerWin::ServiceRecordState* audio_state = | 481   BluetoothTaskManagerWin::ServiceRecordState* audio_state = | 
| 484       new BluetoothTaskManagerWin::ServiceRecordState(); | 482       new BluetoothTaskManagerWin::ServiceRecordState(); | 
| 485   audio_state->name = kTestAudioSdpName; | 483   audio_state->name = kTestAudioSdpName; | 
| 486   base::HexStringToBytes(kTestAudioSdpBytes, &audio_state->sdp_bytes); | 484   base::HexStringToBytes(kTestAudioSdpBytes, &audio_state->sdp_bytes); | 
| 487   laptop_state->service_record_states.push_back(audio_state); | 485   laptop_state->service_record_states.push_back(base::WrapUnique(audio_state)); | 
| 488   observer_.Reset(); | 486   observer_.Reset(); | 
| 489   adapter_win_->DevicesPolled(devices); | 487   adapter_win_->DevicesPolled(devices); | 
| 490   EXPECT_EQ(0, observer_.device_added_count()); | 488   EXPECT_EQ(0, observer_.device_added_count()); | 
| 491   EXPECT_EQ(0, observer_.device_removed_count()); | 489   EXPECT_EQ(0, observer_.device_removed_count()); | 
| 492   EXPECT_EQ(1, observer_.device_changed_count()); | 490   EXPECT_EQ(1, observer_.device_changed_count()); | 
| 493 | 491 | 
| 494   // Change a service | 492   // Change a service | 
| 495   audio_state->name = kTestAudioSdpName2; | 493   audio_state->name = kTestAudioSdpName2; | 
| 496   observer_.Reset(); | 494   observer_.Reset(); | 
| 497   adapter_win_->DevicesPolled(devices); | 495   adapter_win_->DevicesPolled(devices); | 
| 498   EXPECT_EQ(0, observer_.device_added_count()); | 496   EXPECT_EQ(0, observer_.device_added_count()); | 
| 499   EXPECT_EQ(0, observer_.device_removed_count()); | 497   EXPECT_EQ(0, observer_.device_removed_count()); | 
| 500   EXPECT_EQ(1, observer_.device_changed_count()); | 498   EXPECT_EQ(1, observer_.device_changed_count()); | 
| 501 | 499 | 
| 502   // Remove a service | 500   // Remove a service | 
| 503   laptop_state->service_record_states.clear(); | 501   laptop_state->service_record_states.clear(); | 
| 504   observer_.Reset(); | 502   observer_.Reset(); | 
| 505   adapter_win_->DevicesPolled(devices); | 503   adapter_win_->DevicesPolled(devices); | 
| 506   EXPECT_EQ(0, observer_.device_added_count()); | 504   EXPECT_EQ(0, observer_.device_added_count()); | 
| 507   EXPECT_EQ(0, observer_.device_removed_count()); | 505   EXPECT_EQ(0, observer_.device_removed_count()); | 
| 508   EXPECT_EQ(1, observer_.device_changed_count()); | 506   EXPECT_EQ(1, observer_.device_changed_count()); | 
| 509 } | 507 } | 
| 510 | 508 | 
| 511 }  // namespace device | 509 }  // namespace device | 
| OLD | NEW | 
|---|