Index: device/bluetooth/bluetooth_device_win_unittest.cc |
diff --git a/device/bluetooth/bluetooth_device_win_unittest.cc b/device/bluetooth/bluetooth_device_win_unittest.cc |
index e3dd2dfd342ba45762a06efe31c829cd98250388..3d2646331772c16e96895cfbb53e2831c36c96c1 100644 |
--- a/device/bluetooth/bluetooth_device_win_unittest.cc |
+++ b/device/bluetooth/bluetooth_device_win_unittest.cc |
@@ -8,6 +8,7 @@ |
#include "base/strings/string_number_conversions.h" |
#include "base/test/test_simple_task_runner.h" |
#include "device/bluetooth/bluetooth_device_win.h" |
+#include "device/bluetooth/bluetooth_gatt_service.h" |
#include "device/bluetooth/bluetooth_service_record_win.h" |
#include "device/bluetooth/bluetooth_socket_thread.h" |
#include "device/bluetooth/bluetooth_task_manager_win.h" |
@@ -42,9 +43,16 @@ class BluetoothDeviceWinTest : public testing::Test { |
BluetoothDeviceWinTest() { |
scoped_refptr<base::SequencedTaskRunner> ui_task_runner( |
new base::TestSimpleTaskRunner()); |
+ scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner( |
+ new base::TestSimpleTaskRunner()); |
scoped_refptr<BluetoothSocketThread> socket_thread( |
BluetoothSocketThread::Get()); |
+ // Create an adapter. |
+ adapter_ = new BluetoothAdapterWin(base::Bind( |
+ &BluetoothDeviceWinTest::AdapterInitCallback, base::Unretained(this))); |
+ adapter_->InitForTest(ui_task_runner, bluetooth_task_runner); |
+ |
// Add device with audio/video services. |
device_state_.reset(new BluetoothTaskManagerWin::DeviceState()); |
device_state_->name = kDeviceName; |
@@ -62,20 +70,51 @@ class BluetoothDeviceWinTest : public testing::Test { |
base::HexStringToBytes(kTestVideoSdpBytes, &video_state->sdp_bytes); |
device_state_->service_record_states.push_back(video_state); |
- device_.reset(new BluetoothDeviceWin(NULL, *device_state_, ui_task_runner, |
- socket_thread, NULL, |
+ device_.reset(new BluetoothDeviceWin(adapter_.get(), *device_state_, |
+ ui_task_runner, socket_thread, NULL, |
net::NetLog::Source())); |
// Add empty device. |
empty_device_state_.reset(new BluetoothTaskManagerWin::DeviceState()); |
empty_device_state_->name = kDeviceName; |
empty_device_state_->address = kDeviceAddress; |
- empty_device_.reset(new BluetoothDeviceWin(NULL, *empty_device_state_, |
- ui_task_runner, socket_thread, |
- NULL, net::NetLog::Source())); |
+ empty_device_.reset(new BluetoothDeviceWin( |
+ adapter_.get(), *empty_device_state_, ui_task_runner, socket_thread, |
+ NULL, net::NetLog::Source())); |
+ } |
+ |
+ void AdapterInitCallback() {} |
+ |
+ void UpdateGattServices(std::vector<BluetoothUUID> service_uuids) { |
+ BluetoothTaskManagerWin::DeviceState* device_state = |
+ new BluetoothTaskManagerWin::DeviceState(); |
+ device_state->name = kDeviceName; |
+ device_state->address = kDeviceAddress; |
+ |
+ BluetoothTaskManagerWin::ServiceRecordState* service_record_state; |
+ for (unsigned int i = 0; i < service_uuids.size(); i++) { |
+ service_record_state = new BluetoothTaskManagerWin::ServiceRecordState(); |
+ service_record_state->gatt_uuid = service_uuids[i]; |
+ device_state->service_record_states.push_back(service_record_state); |
+ } |
+ empty_device_->UpdateGattServices(*device_state); |
+ } |
+ |
+ bool IsThisServiceObjectExist(BluetoothUUID uuid) { |
+ std::vector<BluetoothGattService*> services = |
+ empty_device_->GetGattServices(); |
+ std::vector<BluetoothGattService*>::iterator it = services.begin(); |
+ for (; it != services.end(); it++) { |
+ if ((*it)->GetUUID() == uuid) |
+ break; |
+ } |
+ if (it != services.end()) |
+ return true; |
+ return false; |
} |
protected: |
+ scoped_refptr<BluetoothAdapterWin> adapter_; |
scoped_ptr<BluetoothDeviceWin> device_; |
scoped_ptr<BluetoothTaskManagerWin::DeviceState> device_state_; |
scoped_ptr<BluetoothDeviceWin> empty_device_; |
@@ -100,4 +139,47 @@ TEST_F(BluetoothDeviceWinTest, IsEqual) { |
EXPECT_TRUE(empty_device_->IsEqual(*empty_device_state_)); |
} |
+TEST_F(BluetoothDeviceWinTest, GattServiceUpdate) { |
+ EXPECT_EQ(empty_device_->GetGattServices().size(), 0); |
+ |
+ // Add number_of_test_services to the device. |
+ uint16_t number_of_test_services = 3; |
+ uint16_t uuid_value_start_from = 1000; |
+ std::vector<BluetoothUUID> service_uuids; |
+ for (uint16_t i = 0; i < number_of_test_services; i++) { |
+ service_uuids.push_back( |
+ BluetoothUUID(std::to_string(uuid_value_start_from + i))); |
+ } |
+ UpdateGattServices(service_uuids); |
+ |
+ // Check service objects have been created. |
+ EXPECT_EQ(empty_device_->GetGattServices().size(), number_of_test_services); |
+ for (auto uuid : service_uuids) |
+ EXPECT_TRUE(IsThisServiceObjectExist(uuid)); |
+ |
+ // Update service without changing. |
+ UpdateGattServices(service_uuids); |
+ |
+ // Check service objects have not been changed. |
+ EXPECT_EQ(empty_device_->GetGattServices().size(), number_of_test_services); |
+ for (auto uuid : service_uuids) |
+ EXPECT_TRUE(IsThisServiceObjectExist(uuid)); |
+ |
+ // Remove one service. |
+ BluetoothUUID removed_service_uuid = service_uuids[0]; |
+ service_uuids.erase(service_uuids.begin()); |
+ // Add a new service. |
+ service_uuids.push_back(BluetoothUUID( |
+ std::to_string(uuid_value_start_from + number_of_test_services))); |
+ UpdateGattServices(service_uuids); |
+ |
+ // Check remove service's object has been removed. |
+ EXPECT_FALSE(IsThisServiceObjectExist(removed_service_uuid)); |
+ |
+ // Check new service and the other original services are there. |
+ EXPECT_EQ(empty_device_->GetGattServices().size(), number_of_test_services); |
+ for (auto uuid : service_uuids) |
+ EXPECT_TRUE(IsThisServiceObjectExist(uuid)); |
+} |
+ |
} // namespace device |