OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/sequenced_task_runner.h" | 7 #include "base/sequenced_task_runner.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/test/test_simple_task_runner.h" | 9 #include "base/test/test_simple_task_runner.h" |
10 #include "device/bluetooth/bluetooth_device_win.h" | 10 #include "device/bluetooth/bluetooth_device_win.h" |
| 11 #include "device/bluetooth/bluetooth_gatt_service.h" |
11 #include "device/bluetooth/bluetooth_service_record_win.h" | 12 #include "device/bluetooth/bluetooth_service_record_win.h" |
12 #include "device/bluetooth/bluetooth_socket_thread.h" | 13 #include "device/bluetooth/bluetooth_socket_thread.h" |
13 #include "device/bluetooth/bluetooth_task_manager_win.h" | 14 #include "device/bluetooth/bluetooth_task_manager_win.h" |
14 #include "device/bluetooth/bluetooth_uuid.h" | 15 #include "device/bluetooth/bluetooth_uuid.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 const char kDeviceName[] = "Device"; | 20 const char kDeviceName[] = "Device"; |
20 const char kDeviceAddress[] = "01:02:03:0A:10:A0"; | 21 const char kDeviceAddress[] = "01:02:03:0A:10:A0"; |
(...skipping 14 matching lines...) Expand all Loading... |
35 | 36 |
36 } // namespace | 37 } // namespace |
37 | 38 |
38 namespace device { | 39 namespace device { |
39 | 40 |
40 class BluetoothDeviceWinTest : public testing::Test { | 41 class BluetoothDeviceWinTest : public testing::Test { |
41 public: | 42 public: |
42 BluetoothDeviceWinTest() { | 43 BluetoothDeviceWinTest() { |
43 scoped_refptr<base::SequencedTaskRunner> ui_task_runner( | 44 scoped_refptr<base::SequencedTaskRunner> ui_task_runner( |
44 new base::TestSimpleTaskRunner()); | 45 new base::TestSimpleTaskRunner()); |
| 46 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner( |
| 47 new base::TestSimpleTaskRunner()); |
45 scoped_refptr<BluetoothSocketThread> socket_thread( | 48 scoped_refptr<BluetoothSocketThread> socket_thread( |
46 BluetoothSocketThread::Get()); | 49 BluetoothSocketThread::Get()); |
47 | 50 |
| 51 // Create an adapter. |
| 52 adapter_ = new BluetoothAdapterWin(base::Bind( |
| 53 &BluetoothDeviceWinTest::AdapterInitCallback, base::Unretained(this))); |
| 54 adapter_->InitForTest(ui_task_runner, bluetooth_task_runner); |
| 55 |
48 // Add device with audio/video services. | 56 // Add device with audio/video services. |
49 device_state_.reset(new BluetoothTaskManagerWin::DeviceState()); | 57 device_state_.reset(new BluetoothTaskManagerWin::DeviceState()); |
50 device_state_->name = kDeviceName; | 58 device_state_->name = kDeviceName; |
51 device_state_->address = kDeviceAddress; | 59 device_state_->address = kDeviceAddress; |
52 | 60 |
53 BluetoothTaskManagerWin::ServiceRecordState* audio_state = | 61 BluetoothTaskManagerWin::ServiceRecordState* audio_state = |
54 new BluetoothTaskManagerWin::ServiceRecordState(); | 62 new BluetoothTaskManagerWin::ServiceRecordState(); |
55 audio_state->name = kTestAudioSdpName; | 63 audio_state->name = kTestAudioSdpName; |
56 base::HexStringToBytes(kTestAudioSdpBytes, &audio_state->sdp_bytes); | 64 base::HexStringToBytes(kTestAudioSdpBytes, &audio_state->sdp_bytes); |
57 device_state_->service_record_states.push_back(audio_state); | 65 device_state_->service_record_states.push_back(audio_state); |
58 | 66 |
59 BluetoothTaskManagerWin::ServiceRecordState* video_state = | 67 BluetoothTaskManagerWin::ServiceRecordState* video_state = |
60 new BluetoothTaskManagerWin::ServiceRecordState(); | 68 new BluetoothTaskManagerWin::ServiceRecordState(); |
61 video_state->name = kTestVideoSdpName; | 69 video_state->name = kTestVideoSdpName; |
62 base::HexStringToBytes(kTestVideoSdpBytes, &video_state->sdp_bytes); | 70 base::HexStringToBytes(kTestVideoSdpBytes, &video_state->sdp_bytes); |
63 device_state_->service_record_states.push_back(video_state); | 71 device_state_->service_record_states.push_back(video_state); |
64 | 72 |
65 device_.reset(new BluetoothDeviceWin(NULL, *device_state_, ui_task_runner, | 73 device_.reset(new BluetoothDeviceWin(adapter_.get(), *device_state_, |
66 socket_thread, NULL, | 74 ui_task_runner, socket_thread, NULL, |
67 net::NetLog::Source())); | 75 net::NetLog::Source())); |
68 | 76 |
69 // Add empty device. | 77 // Add empty device. |
70 empty_device_state_.reset(new BluetoothTaskManagerWin::DeviceState()); | 78 empty_device_state_.reset(new BluetoothTaskManagerWin::DeviceState()); |
71 empty_device_state_->name = kDeviceName; | 79 empty_device_state_->name = kDeviceName; |
72 empty_device_state_->address = kDeviceAddress; | 80 empty_device_state_->address = kDeviceAddress; |
73 empty_device_.reset(new BluetoothDeviceWin(NULL, *empty_device_state_, | 81 empty_device_.reset(new BluetoothDeviceWin( |
74 ui_task_runner, socket_thread, | 82 adapter_.get(), *empty_device_state_, ui_task_runner, socket_thread, |
75 NULL, net::NetLog::Source())); | 83 NULL, net::NetLog::Source())); |
| 84 } |
| 85 |
| 86 void AdapterInitCallback() {} |
| 87 |
| 88 void UpdateGattServices(std::vector<BluetoothUUID> service_uuids) { |
| 89 BluetoothTaskManagerWin::DeviceState* device_state = |
| 90 new BluetoothTaskManagerWin::DeviceState(); |
| 91 device_state->name = kDeviceName; |
| 92 device_state->address = kDeviceAddress; |
| 93 |
| 94 BluetoothTaskManagerWin::ServiceRecordState* service_record_state; |
| 95 for (unsigned int i = 0; i < service_uuids.size(); i++) { |
| 96 service_record_state = new BluetoothTaskManagerWin::ServiceRecordState(); |
| 97 service_record_state->gatt_uuid = service_uuids[i]; |
| 98 device_state->service_record_states.push_back(service_record_state); |
| 99 } |
| 100 empty_device_->UpdateGattServices(*device_state); |
| 101 } |
| 102 |
| 103 bool IsThisServiceObjectExist(BluetoothUUID uuid) { |
| 104 std::vector<BluetoothGattService*> services = |
| 105 empty_device_->GetGattServices(); |
| 106 std::vector<BluetoothGattService*>::iterator it = services.begin(); |
| 107 for (; it != services.end(); it++) { |
| 108 if ((*it)->GetUUID() == uuid) |
| 109 break; |
| 110 } |
| 111 if (it != services.end()) |
| 112 return true; |
| 113 return false; |
76 } | 114 } |
77 | 115 |
78 protected: | 116 protected: |
| 117 scoped_refptr<BluetoothAdapterWin> adapter_; |
79 scoped_ptr<BluetoothDeviceWin> device_; | 118 scoped_ptr<BluetoothDeviceWin> device_; |
80 scoped_ptr<BluetoothTaskManagerWin::DeviceState> device_state_; | 119 scoped_ptr<BluetoothTaskManagerWin::DeviceState> device_state_; |
81 scoped_ptr<BluetoothDeviceWin> empty_device_; | 120 scoped_ptr<BluetoothDeviceWin> empty_device_; |
82 scoped_ptr<BluetoothTaskManagerWin::DeviceState> empty_device_state_; | 121 scoped_ptr<BluetoothTaskManagerWin::DeviceState> empty_device_state_; |
83 }; | 122 }; |
84 | 123 |
85 TEST_F(BluetoothDeviceWinTest, GetUUIDs) { | 124 TEST_F(BluetoothDeviceWinTest, GetUUIDs) { |
86 BluetoothDevice::UUIDList uuids = device_->GetUUIDs(); | 125 BluetoothDevice::UUIDList uuids = device_->GetUUIDs(); |
87 | 126 |
88 EXPECT_EQ(2u, uuids.size()); | 127 EXPECT_EQ(2u, uuids.size()); |
89 EXPECT_EQ(kTestAudioSdpUuid, uuids[0]); | 128 EXPECT_EQ(kTestAudioSdpUuid, uuids[0]); |
90 EXPECT_EQ(kTestVideoSdpUuid, uuids[1]); | 129 EXPECT_EQ(kTestVideoSdpUuid, uuids[1]); |
91 | 130 |
92 uuids = empty_device_->GetUUIDs(); | 131 uuids = empty_device_->GetUUIDs(); |
93 EXPECT_EQ(0u, uuids.size()); | 132 EXPECT_EQ(0u, uuids.size()); |
94 } | 133 } |
95 | 134 |
96 TEST_F(BluetoothDeviceWinTest, IsEqual) { | 135 TEST_F(BluetoothDeviceWinTest, IsEqual) { |
97 EXPECT_TRUE(device_->IsEqual(*device_state_)); | 136 EXPECT_TRUE(device_->IsEqual(*device_state_)); |
98 EXPECT_FALSE(device_->IsEqual(*empty_device_state_)); | 137 EXPECT_FALSE(device_->IsEqual(*empty_device_state_)); |
99 EXPECT_FALSE(empty_device_->IsEqual(*device_state_)); | 138 EXPECT_FALSE(empty_device_->IsEqual(*device_state_)); |
100 EXPECT_TRUE(empty_device_->IsEqual(*empty_device_state_)); | 139 EXPECT_TRUE(empty_device_->IsEqual(*empty_device_state_)); |
101 } | 140 } |
102 | 141 |
| 142 TEST_F(BluetoothDeviceWinTest, GattServiceUpdate) { |
| 143 EXPECT_EQ(empty_device_->GetGattServices().size(), 0); |
| 144 |
| 145 // Add number_of_test_services to the device. |
| 146 uint16_t number_of_test_services = 3; |
| 147 uint16_t uuid_value_start_from = 1000; |
| 148 std::vector<BluetoothUUID> service_uuids; |
| 149 for (uint16_t i = 0; i < number_of_test_services; i++) { |
| 150 service_uuids.push_back( |
| 151 BluetoothUUID(std::to_string(uuid_value_start_from + i))); |
| 152 } |
| 153 UpdateGattServices(service_uuids); |
| 154 |
| 155 // Check service objects have been created. |
| 156 EXPECT_EQ(empty_device_->GetGattServices().size(), number_of_test_services); |
| 157 for (auto uuid : service_uuids) |
| 158 EXPECT_TRUE(IsThisServiceObjectExist(uuid)); |
| 159 |
| 160 // Update service without changing. |
| 161 UpdateGattServices(service_uuids); |
| 162 |
| 163 // Check service objects have not been changed. |
| 164 EXPECT_EQ(empty_device_->GetGattServices().size(), number_of_test_services); |
| 165 for (auto uuid : service_uuids) |
| 166 EXPECT_TRUE(IsThisServiceObjectExist(uuid)); |
| 167 |
| 168 // Remove one service. |
| 169 BluetoothUUID removed_service_uuid = service_uuids[0]; |
| 170 service_uuids.erase(service_uuids.begin()); |
| 171 // Add a new service. |
| 172 service_uuids.push_back(BluetoothUUID( |
| 173 std::to_string(uuid_value_start_from + number_of_test_services))); |
| 174 UpdateGattServices(service_uuids); |
| 175 |
| 176 // Check remove service's object has been removed. |
| 177 EXPECT_FALSE(IsThisServiceObjectExist(removed_service_uuid)); |
| 178 |
| 179 // Check new service and the other original services are there. |
| 180 EXPECT_EQ(empty_device_->GetGattServices().size(), number_of_test_services); |
| 181 for (auto uuid : service_uuids) |
| 182 EXPECT_TRUE(IsThisServiceObjectExist(uuid)); |
| 183 } |
| 184 |
103 } // namespace device | 185 } // namespace device |
OLD | NEW |