Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / device / bluetooth / test / test_bluetooth_adapter_observer.cc
blobe03ae6ca1ae5c66676516fdbabe2cdbb087e0f37
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
7 #include "base/message_loop/message_loop.h"
8 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
9 #include "device/bluetooth/bluetooth_gatt_descriptor.h"
10 #include "device/bluetooth/bluetooth_gatt_service.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace device {
15 TestBluetoothAdapterObserver::TestBluetoothAdapterObserver(
16 scoped_refptr<BluetoothAdapter> adapter)
17 : adapter_(adapter) {
18 Reset();
19 adapter_->AddObserver(this);
22 TestBluetoothAdapterObserver::~TestBluetoothAdapterObserver() {
23 adapter_->RemoveObserver(this);
26 void TestBluetoothAdapterObserver::Reset() {
27 present_changed_count_ = 0;
28 powered_changed_count_ = 0;
29 discoverable_changed_count_ = 0;
30 discovering_changed_count_ = 0;
31 last_present_ = false;
32 last_powered_ = false;
33 last_discovering_ = false;
34 device_added_count_ = 0;
35 device_changed_count_ = 0;
36 device_removed_count_ = 0;
37 last_device_ = NULL;
38 last_device_address_.clear();
39 gatt_service_added_count_ = 0;
40 gatt_service_removed_count_ = 0;
41 gatt_service_changed_count_ = 0;
42 gatt_discovery_complete_count_ = 0;
43 gatt_characteristic_added_count_ = 0;
44 gatt_characteristic_removed_count_ = 0;
45 gatt_characteristic_value_changed_count_ = 0;
46 gatt_descriptor_added_count_ = 0;
47 gatt_descriptor_removed_count_ = 0;
48 gatt_descriptor_value_changed_count_ = 0;
49 last_gatt_service_id_.clear();
50 last_gatt_service_uuid_ = BluetoothUUID();
51 last_gatt_characteristic_id_.clear();
52 last_gatt_characteristic_uuid_ = BluetoothUUID();
53 last_changed_characteristic_value_.clear();
54 last_gatt_descriptor_id_.clear();
55 last_gatt_descriptor_uuid_ = BluetoothUUID();
56 last_changed_descriptor_value_.clear();
59 void TestBluetoothAdapterObserver::AdapterPresentChanged(
60 BluetoothAdapter* adapter,
61 bool present) {
62 EXPECT_EQ(adapter_.get(), adapter);
64 ++present_changed_count_;
65 last_present_ = present;
68 void TestBluetoothAdapterObserver::AdapterPoweredChanged(
69 BluetoothAdapter* adapter,
70 bool powered) {
71 EXPECT_EQ(adapter_.get(), adapter);
73 ++powered_changed_count_;
74 last_powered_ = powered;
77 void TestBluetoothAdapterObserver::AdapterDiscoverableChanged(
78 BluetoothAdapter* adapter,
79 bool discoverable) {
80 EXPECT_EQ(adapter_.get(), adapter);
82 ++discoverable_changed_count_;
85 void TestBluetoothAdapterObserver::AdapterDiscoveringChanged(
86 BluetoothAdapter* adapter,
87 bool discovering) {
88 EXPECT_EQ(adapter_.get(), adapter);
90 ++discovering_changed_count_;
91 last_discovering_ = discovering;
94 void TestBluetoothAdapterObserver::DeviceAdded(BluetoothAdapter* adapter,
95 BluetoothDevice* device) {
96 EXPECT_EQ(adapter_.get(), adapter);
98 ++device_added_count_;
99 last_device_ = device;
100 last_device_address_ = device->GetAddress();
102 QuitMessageLoop();
105 void TestBluetoothAdapterObserver::DeviceChanged(BluetoothAdapter* adapter,
106 BluetoothDevice* device) {
107 EXPECT_EQ(adapter_.get(), adapter);
109 ++device_changed_count_;
110 last_device_ = device;
111 last_device_address_ = device->GetAddress();
113 QuitMessageLoop();
116 void TestBluetoothAdapterObserver::DeviceRemoved(BluetoothAdapter* adapter,
117 BluetoothDevice* device) {
118 EXPECT_EQ(adapter_.get(), adapter);
120 ++device_removed_count_;
121 // Can't save device, it may be freed
122 last_device_address_ = device->GetAddress();
124 QuitMessageLoop();
127 void TestBluetoothAdapterObserver::GattServiceAdded(
128 BluetoothAdapter* adapter,
129 BluetoothDevice* device,
130 BluetoothGattService* service) {
131 ASSERT_EQ(adapter_.get(), adapter);
132 ASSERT_EQ(service->GetDevice(), device);
134 ++gatt_service_added_count_;
135 last_gatt_service_id_ = service->GetIdentifier();
136 last_gatt_service_uuid_ = service->GetUUID();
138 EXPECT_FALSE(service->IsLocal());
139 EXPECT_TRUE(service->IsPrimary());
141 EXPECT_EQ(device->GetGattService(last_gatt_service_id_), service);
143 QuitMessageLoop();
146 void TestBluetoothAdapterObserver::GattServiceRemoved(
147 BluetoothAdapter* adapter,
148 BluetoothDevice* device,
149 BluetoothGattService* service) {
150 ASSERT_EQ(adapter_.get(), adapter);
151 ASSERT_EQ(service->GetDevice(), device);
153 ++gatt_service_removed_count_;
154 last_gatt_service_id_ = service->GetIdentifier();
155 last_gatt_service_uuid_ = service->GetUUID();
157 EXPECT_FALSE(service->IsLocal());
158 EXPECT_TRUE(service->IsPrimary());
160 // The device should return NULL for this service.
161 EXPECT_FALSE(device->GetGattService(last_gatt_service_id_));
163 QuitMessageLoop();
166 void TestBluetoothAdapterObserver::GattDiscoveryCompleteForService(
167 BluetoothAdapter* adapter,
168 BluetoothGattService* service) {
169 ASSERT_EQ(adapter_.get(), adapter);
170 ++gatt_discovery_complete_count_;
172 QuitMessageLoop();
175 void TestBluetoothAdapterObserver::GattServiceChanged(
176 BluetoothAdapter* adapter,
177 BluetoothGattService* service) {
178 ASSERT_EQ(adapter_.get(), adapter);
179 ++gatt_service_changed_count_;
181 QuitMessageLoop();
184 void TestBluetoothAdapterObserver::GattCharacteristicAdded(
185 BluetoothAdapter* adapter,
186 BluetoothGattCharacteristic* characteristic) {
187 ASSERT_EQ(adapter_.get(), adapter);
189 ++gatt_characteristic_added_count_;
190 last_gatt_characteristic_id_ = characteristic->GetIdentifier();
191 last_gatt_characteristic_uuid_ = characteristic->GetUUID();
193 ASSERT_TRUE(characteristic->GetService());
194 EXPECT_EQ(characteristic->GetService()->GetCharacteristic(
195 last_gatt_characteristic_id_),
196 characteristic);
198 QuitMessageLoop();
201 void TestBluetoothAdapterObserver::GattCharacteristicRemoved(
202 BluetoothAdapter* adapter,
203 BluetoothGattCharacteristic* characteristic) {
204 ASSERT_EQ(adapter_.get(), adapter);
206 ++gatt_characteristic_removed_count_;
207 last_gatt_characteristic_id_ = characteristic->GetIdentifier();
208 last_gatt_characteristic_uuid_ = characteristic->GetUUID();
210 // The service should return NULL for this characteristic.
211 ASSERT_TRUE(characteristic->GetService());
212 EXPECT_FALSE(characteristic->GetService()->GetCharacteristic(
213 last_gatt_characteristic_id_));
215 QuitMessageLoop();
218 void TestBluetoothAdapterObserver::GattDescriptorAdded(
219 BluetoothAdapter* adapter,
220 BluetoothGattDescriptor* descriptor) {
221 ASSERT_EQ(adapter_.get(), adapter);
223 ++gatt_descriptor_added_count_;
224 last_gatt_descriptor_id_ = descriptor->GetIdentifier();
225 last_gatt_descriptor_uuid_ = descriptor->GetUUID();
227 ASSERT_TRUE(descriptor->GetCharacteristic());
228 EXPECT_EQ(
229 descriptor->GetCharacteristic()->GetDescriptor(last_gatt_descriptor_id_),
230 descriptor);
232 QuitMessageLoop();
235 void TestBluetoothAdapterObserver::GattDescriptorRemoved(
236 BluetoothAdapter* adapter,
237 BluetoothGattDescriptor* descriptor) {
238 ASSERT_EQ(adapter_.get(), adapter);
240 ++gatt_descriptor_removed_count_;
241 last_gatt_descriptor_id_ = descriptor->GetIdentifier();
242 last_gatt_descriptor_uuid_ = descriptor->GetUUID();
244 // The characteristic should return NULL for this descriptor..
245 ASSERT_TRUE(descriptor->GetCharacteristic());
246 EXPECT_FALSE(
247 descriptor->GetCharacteristic()->GetDescriptor(last_gatt_descriptor_id_));
249 QuitMessageLoop();
252 void TestBluetoothAdapterObserver::GattCharacteristicValueChanged(
253 BluetoothAdapter* adapter,
254 BluetoothGattCharacteristic* characteristic,
255 const std::vector<uint8>& value) {
256 ASSERT_EQ(adapter_.get(), adapter);
258 ++gatt_characteristic_value_changed_count_;
259 last_gatt_characteristic_id_ = characteristic->GetIdentifier();
260 last_gatt_characteristic_uuid_ = characteristic->GetUUID();
261 last_changed_characteristic_value_ = value;
263 ASSERT_TRUE(characteristic->GetService());
264 EXPECT_EQ(characteristic->GetService()->GetCharacteristic(
265 last_gatt_characteristic_id_),
266 characteristic);
268 QuitMessageLoop();
271 void TestBluetoothAdapterObserver::GattDescriptorValueChanged(
272 BluetoothAdapter* adapter,
273 BluetoothGattDescriptor* descriptor,
274 const std::vector<uint8>& value) {
275 ASSERT_EQ(adapter_.get(), adapter);
277 ++gatt_descriptor_value_changed_count_;
278 last_gatt_descriptor_id_ = descriptor->GetIdentifier();
279 last_gatt_descriptor_uuid_ = descriptor->GetUUID();
280 last_changed_descriptor_value_ = value;
282 ASSERT_TRUE(descriptor->GetCharacteristic());
283 EXPECT_EQ(
284 descriptor->GetCharacteristic()->GetDescriptor(last_gatt_descriptor_id_),
285 descriptor);
287 QuitMessageLoop();
290 void TestBluetoothAdapterObserver::QuitMessageLoop() {
291 if (base::MessageLoop::current() &&
292 base::MessageLoop::current()->is_running())
293 base::MessageLoop::current()->Quit();
296 } // namespace device