| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "device/bluetooth/bluetooth_gatt_service.h" | 5 #include "device/bluetooth/bluetooth_gatt_service.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" | 8 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" |
| 9 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 9 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 #if defined(OS_ANDROID) | 12 #if defined(OS_ANDROID) |
| 13 #include "device/bluetooth/test/bluetooth_test_android.h" | 13 #include "device/bluetooth/test/bluetooth_test_android.h" |
| 14 #elif defined(OS_MACOSX) | 14 #elif defined(OS_MACOSX) |
| 15 #include "device/bluetooth/test/bluetooth_test_mac.h" | 15 #include "device/bluetooth/test/bluetooth_test_mac.h" |
| 16 #elif defined(OS_WIN) | 16 #elif defined(OS_WIN) |
| 17 #include "device/bluetooth/test/bluetooth_test_win.h" | 17 #include "device/bluetooth/test/bluetooth_test_win.h" |
| 18 #endif | 18 #endif |
| 19 | 19 |
| 20 namespace device { | 20 namespace device { |
| 21 | 21 |
| 22 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) | 22 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
| 23 class BluetoothRemoteGattServiceTest : public BluetoothTest {}; | 23 class BluetoothRemoteGattServiceTest : public BluetoothTest {}; |
| 24 #endif | 24 #endif |
| 25 | 25 |
| 26 #if defined(OS_ANDROID) || defined(OS_WIN) | 26 #if defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
| 27 TEST_F(BluetoothRemoteGattServiceTest, GetIdentifier) { | 27 TEST_F(BluetoothRemoteGattServiceTest, GetIdentifier) { |
| 28 if (!PlatformSupportsLowEnergy()) { |
| 29 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 30 return; |
| 31 } |
| 28 InitWithFakeAdapter(); | 32 InitWithFakeAdapter(); |
| 29 StartLowEnergyDiscoverySession(); | 33 StartLowEnergyDiscoverySession(); |
| 30 // 2 devices to verify unique IDs across them. | 34 // 2 devices to verify unique IDs across them. |
| 31 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); | 35 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); |
| 32 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); | 36 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); |
| 33 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 37 device1->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 34 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 38 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 35 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 39 device2->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 36 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 40 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 37 SimulateGattConnection(device1); | 41 SimulateGattConnection(device1); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 52 // All IDs are unique, even though they have the same UUID. | 56 // All IDs are unique, even though they have the same UUID. |
| 53 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); | 57 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); |
| 54 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); | 58 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); |
| 55 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); | 59 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); |
| 56 | 60 |
| 57 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); | 61 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); |
| 58 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); | 62 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); |
| 59 | 63 |
| 60 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); | 64 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); |
| 61 } | 65 } |
| 62 #endif // defined(OS_ANDROID) || defined(OS_WIN) | 66 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
| 63 | 67 |
| 64 #if defined(OS_ANDROID) || defined(OS_WIN) | 68 #if defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
| 65 TEST_F(BluetoothRemoteGattServiceTest, GetUUID) { | 69 TEST_F(BluetoothRemoteGattServiceTest, GetUUID) { |
| 70 if (!PlatformSupportsLowEnergy()) { |
| 71 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 72 return; |
| 73 } |
| 66 InitWithFakeAdapter(); | 74 InitWithFakeAdapter(); |
| 67 StartLowEnergyDiscoverySession(); | 75 StartLowEnergyDiscoverySession(); |
| 68 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 76 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 69 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 77 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 70 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 78 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 71 SimulateGattConnection(device); | 79 SimulateGattConnection(device); |
| 72 | 80 |
| 73 // Create multiple instances with the same UUID. | 81 // Create multiple instances with the same UUID. |
| 74 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); | 82 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); |
| 75 std::vector<std::string> services; | 83 std::vector<std::string> services; |
| 76 services.push_back(uuid.canonical_value()); | 84 services.push_back(uuid.canonical_value()); |
| 77 services.push_back(uuid.canonical_value()); | 85 services.push_back(uuid.canonical_value()); |
| 78 SimulateGattServicesDiscovered(device, services); | 86 SimulateGattServicesDiscovered(device, services); |
| 79 | 87 |
| 80 // Each has the same UUID. | 88 // Each has the same UUID. |
| 81 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); | 89 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); |
| 82 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); | 90 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); |
| 83 } | 91 } |
| 84 #endif // defined(OS_ANDROID) || defined(OS_WIN) | 92 #endif // defined(OS_ANDROID) || defined(OS_WIN) || defined(OS_MACOSX) |
| 85 | 93 |
| 86 #if defined(OS_ANDROID) || defined(OS_WIN) | 94 #if defined(OS_ANDROID) || defined(OS_WIN) |
| 87 TEST_F(BluetoothRemoteGattServiceTest, GetCharacteristics_FindNone) { | 95 TEST_F(BluetoothRemoteGattServiceTest, GetCharacteristics_FindNone) { |
| 96 if (!PlatformSupportsLowEnergy()) { |
| 97 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 98 return; |
| 99 } |
| 88 InitWithFakeAdapter(); | 100 InitWithFakeAdapter(); |
| 89 StartLowEnergyDiscoverySession(); | 101 StartLowEnergyDiscoverySession(); |
| 90 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 102 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 91 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 103 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 92 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 104 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 93 SimulateGattConnection(device); | 105 SimulateGattConnection(device); |
| 94 | 106 |
| 95 // Simulate a service, with no Characteristics: | 107 // Simulate a service, with no Characteristics: |
| 96 std::vector<std::string> services; | 108 std::vector<std::string> services; |
| 97 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 109 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 SimulateGattCharacteristicRemoved(service, | 213 SimulateGattCharacteristicRemoved(service, |
| 202 service->GetCharacteristic(removed_char)); | 214 service->GetCharacteristic(removed_char)); |
| 203 EXPECT_EQ(4, observer.gatt_characteristic_removed_count()); | 215 EXPECT_EQ(4, observer.gatt_characteristic_removed_count()); |
| 204 EXPECT_FALSE(service->GetCharacteristic(removed_char)); | 216 EXPECT_FALSE(service->GetCharacteristic(removed_char)); |
| 205 EXPECT_EQ(0u, service->GetCharacteristics().size()); | 217 EXPECT_EQ(0u, service->GetCharacteristics().size()); |
| 206 | 218 |
| 207 EXPECT_EQ(4, observer.gatt_service_changed_count()); | 219 EXPECT_EQ(4, observer.gatt_service_changed_count()); |
| 208 } | 220 } |
| 209 #endif // defined(OS_WIN) | 221 #endif // defined(OS_WIN) |
| 210 | 222 |
| 211 #if defined(OS_WIN) | 223 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 212 TEST_F(BluetoothRemoteGattServiceTest, SimulateGattServiceRemove) { | 224 TEST_F(BluetoothRemoteGattServiceTest, SimulateGattServiceRemove) { |
| 225 if (!PlatformSupportsLowEnergy()) { |
| 226 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 227 return; |
| 228 } |
| 213 InitWithFakeAdapter(); | 229 InitWithFakeAdapter(); |
| 214 StartLowEnergyDiscoverySession(); | 230 StartLowEnergyDiscoverySession(); |
| 215 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 231 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 216 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 232 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 217 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 233 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 218 SimulateGattConnection(device); | 234 SimulateGattConnection(device); |
| 219 | 235 |
| 220 TestBluetoothAdapterObserver observer(adapter_); | 236 TestBluetoothAdapterObserver observer(adapter_); |
| 221 | 237 |
| 222 // Simulate two primary GATT services. | 238 // Simulate two primary GATT services. |
| 223 std::vector<std::string> services; | 239 std::vector<std::string> services; |
| 224 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 240 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 225 services.push_back("01010101-0101-1000-8000-00805f9b34fb"); | 241 services.push_back("01010101-0101-1000-8000-00805f9b34fb"); |
| 226 SimulateGattServicesDiscovered(device, services); | 242 SimulateGattServicesDiscovered(device, services); |
| 227 EXPECT_EQ(2u, device->GetGattServices().size()); | 243 EXPECT_EQ(2u, device->GetGattServices().size()); |
| 228 | 244 |
| 229 // Simulate remove of a primary service. | 245 // Simulate remove of a primary service. |
| 230 BluetoothRemoteGattService* service1 = device->GetGattServices()[0]; | 246 BluetoothRemoteGattService* service1 = device->GetGattServices()[0]; |
| 231 BluetoothRemoteGattService* service2 = device->GetGattServices()[1]; | 247 BluetoothRemoteGattService* service2 = device->GetGattServices()[1]; |
| 232 std::string removed_service = service1->GetIdentifier(); | 248 std::string removed_service = service1->GetIdentifier(); |
| 233 SimulateGattServiceRemoved(device->GetGattService(removed_service)); | 249 SimulateGattServiceRemoved(device->GetGattService(removed_service)); |
| 234 EXPECT_EQ(1, observer.gatt_service_removed_count()); | 250 EXPECT_EQ(1, observer.gatt_service_removed_count()); |
| 235 EXPECT_EQ(1u, device->GetGattServices().size()); | 251 EXPECT_EQ(1u, device->GetGattServices().size()); |
| 236 EXPECT_FALSE(device->GetGattService(removed_service)); | 252 EXPECT_FALSE(device->GetGattService(removed_service)); |
| 237 EXPECT_EQ(device->GetGattServices()[0], service2); | 253 EXPECT_EQ(device->GetGattServices()[0], service2); |
| 238 } | 254 } |
| 239 #endif // defined(OS_WIN) | 255 #endif // defined(OS_WIN) || defined(OS_MACOSX) |
| 240 | 256 |
| 241 } // namespace device | 257 } // namespace device |
| OLD | NEW |