OLD | NEW |
1 // Copyright 2014 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 "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" | 7 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_android.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 #if defined(OS_ANDROID) | 10 #if defined(OS_ANDROID) |
11 #include "device/bluetooth/test/bluetooth_test_android.h" | 11 #include "device/bluetooth/test/bluetooth_test_android.h" |
12 #elif defined(OS_MACOSX) | 12 #elif defined(OS_MACOSX) |
13 #include "device/bluetooth/test/bluetooth_test_mac.h" | 13 #include "device/bluetooth/test/bluetooth_test_mac.h" |
14 #endif | 14 #endif |
15 | 15 |
16 namespace device { | 16 namespace device { |
17 | 17 |
| 18 #if defined(OS_ANDROID) || defined(OS_MACOSX) |
| 19 class BluetoothGattServiceTest : public BluetoothTest {}; |
| 20 #endif |
| 21 |
18 #if defined(OS_ANDROID) | 22 #if defined(OS_ANDROID) |
19 TEST_F(BluetoothTest, GetUUIDAndGetIdentifier) { | 23 TEST_F(BluetoothGattServiceTest, GetIdentifier) { |
| 24 InitWithFakeAdapter(); |
| 25 StartLowEnergyDiscoverySession(); |
| 26 // 2 devices to verify unique IDs across them. |
| 27 BluetoothDevice* device1 = DiscoverLowEnergyDevice(3); |
| 28 BluetoothDevice* device2 = DiscoverLowEnergyDevice(4); |
| 29 device1->CreateGattConnection(GetGattConnectionCallback(), |
| 30 GetConnectErrorCallback()); |
| 31 device2->CreateGattConnection(GetGattConnectionCallback(), |
| 32 GetConnectErrorCallback()); |
| 33 SimulateGattConnection(device1); |
| 34 SimulateGattConnection(device2); |
| 35 |
| 36 // 2 duplicate UUIDs creating 2 service instances on each device. |
| 37 std::vector<std::string> services; |
| 38 std::string uuid = "00000000-0000-1000-8000-00805f9b34fb"; |
| 39 services.push_back(uuid); |
| 40 services.push_back(uuid); |
| 41 SimulateGattServicesDiscovered(device1, services); |
| 42 SimulateGattServicesDiscovered(device2, services); |
| 43 BluetoothGattService* service1 = device1->GetGattServices()[0]; |
| 44 BluetoothGattService* service2 = device1->GetGattServices()[1]; |
| 45 BluetoothGattService* service3 = device2->GetGattServices()[0]; |
| 46 BluetoothGattService* service4 = device2->GetGattServices()[1]; |
| 47 |
| 48 // All IDs are unique, even though they have the same UUID. |
| 49 EXPECT_NE(service1->GetIdentifier(), service2->GetIdentifier()); |
| 50 EXPECT_NE(service1->GetIdentifier(), service3->GetIdentifier()); |
| 51 EXPECT_NE(service1->GetIdentifier(), service4->GetIdentifier()); |
| 52 |
| 53 EXPECT_NE(service2->GetIdentifier(), service3->GetIdentifier()); |
| 54 EXPECT_NE(service2->GetIdentifier(), service4->GetIdentifier()); |
| 55 |
| 56 EXPECT_NE(service3->GetIdentifier(), service4->GetIdentifier()); |
| 57 } |
| 58 #endif // defined(OS_ANDROID) |
| 59 |
| 60 #if defined(OS_ANDROID) |
| 61 TEST_F(BluetoothGattServiceTest, GetUUID) { |
20 InitWithFakeAdapter(); | 62 InitWithFakeAdapter(); |
21 StartLowEnergyDiscoverySession(); | 63 StartLowEnergyDiscoverySession(); |
22 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 64 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
23 device->CreateGattConnection(GetGattConnectionCallback(), | 65 device->CreateGattConnection(GetGattConnectionCallback(), |
24 GetConnectErrorCallback()); | 66 GetConnectErrorCallback()); |
25 SimulateGattConnection(device); | 67 SimulateGattConnection(device); |
26 | 68 |
27 // Create multiple instances with the same UUID. | 69 // Create multiple instances with the same UUID. |
28 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); | 70 BluetoothUUID uuid("00000000-0000-1000-8000-00805f9b34fb"); |
29 std::vector<std::string> services; | 71 std::vector<std::string> services; |
30 services.push_back(uuid.canonical_value()); | 72 services.push_back(uuid.canonical_value()); |
31 services.push_back(uuid.canonical_value()); | 73 services.push_back(uuid.canonical_value()); |
32 SimulateGattServicesDiscovered(device, services); | 74 SimulateGattServicesDiscovered(device, services); |
33 | 75 |
34 // Each has the same UUID. | 76 // Each has the same UUID. |
35 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); | 77 EXPECT_EQ(uuid, device->GetGattServices()[0]->GetUUID()); |
36 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); | 78 EXPECT_EQ(uuid, device->GetGattServices()[1]->GetUUID()); |
37 | |
38 // Instance IDs are unique. | |
39 EXPECT_NE(device->GetGattServices()[0]->GetIdentifier(), | |
40 device->GetGattServices()[1]->GetIdentifier()); | |
41 } | 79 } |
42 #endif // defined(OS_ANDROID) | 80 #endif // defined(OS_ANDROID) |
43 | 81 |
44 #if defined(OS_ANDROID) | 82 #if defined(OS_ANDROID) |
45 TEST_F(BluetoothTest, GetCharacteristics_FindNone) { | 83 TEST_F(BluetoothGattServiceTest, GetCharacteristics_FindNone) { |
46 InitWithFakeAdapter(); | 84 InitWithFakeAdapter(); |
47 StartLowEnergyDiscoverySession(); | 85 StartLowEnergyDiscoverySession(); |
48 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 86 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
49 device->CreateGattConnection(GetGattConnectionCallback(), | 87 device->CreateGattConnection(GetGattConnectionCallback(), |
50 GetConnectErrorCallback()); | 88 GetConnectErrorCallback()); |
51 SimulateGattConnection(device); | 89 SimulateGattConnection(device); |
52 | 90 |
53 // Simulate a service, with no Characteristics: | 91 // Simulate a service, with no Characteristics: |
54 std::vector<std::string> services; | 92 std::vector<std::string> services; |
55 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 93 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
56 SimulateGattServicesDiscovered(device, services); | 94 SimulateGattServicesDiscovered(device, services); |
57 BluetoothGattService* service = device->GetGattServices()[0]; | 95 BluetoothGattService* service = device->GetGattServices()[0]; |
58 | 96 |
59 EXPECT_EQ(0u, service->GetCharacteristics().size()); | 97 EXPECT_EQ(0u, service->GetCharacteristics().size()); |
60 } | 98 } |
61 #endif // defined(OS_ANDROID) | 99 #endif // defined(OS_ANDROID) |
62 | 100 |
63 #if defined(OS_ANDROID) | 101 #if defined(OS_ANDROID) |
64 TEST_F(BluetoothTest, GetCharacteristic_FindSeveral) { | 102 TEST_F(BluetoothGattServiceTest, GetCharacteristics_and_GetCharacteristic) { |
65 InitWithFakeAdapter(); | 103 InitWithFakeAdapter(); |
66 StartLowEnergyDiscoverySession(); | 104 StartLowEnergyDiscoverySession(); |
67 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 105 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
68 device->CreateGattConnection(GetGattConnectionCallback(), | 106 device->CreateGattConnection(GetGattConnectionCallback(), |
69 GetConnectErrorCallback()); | 107 GetConnectErrorCallback()); |
70 SimulateGattConnection(device); | 108 SimulateGattConnection(device); |
71 | 109 |
72 // Simulate a service, with no Characteristics: | 110 // Simulate a service, with several Characteristics: |
73 std::vector<std::string> services; | 111 std::vector<std::string> services; |
74 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 112 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
75 SimulateGattServicesDiscovered(device, services); | 113 SimulateGattServicesDiscovered(device, services); |
76 BluetoothGattService* service = device->GetGattServices()[0]; | 114 BluetoothGattService* service = device->GetGattServices()[0]; |
77 | |
78 std::string characteristic_uuid1 = "00000001-0000-1000-8000-00805f9b34fb"; | 115 std::string characteristic_uuid1 = "00000001-0000-1000-8000-00805f9b34fb"; |
79 std::string characteristic_uuid2 = "00000002-0000-1000-8000-00805f9b34fb"; | 116 std::string characteristic_uuid2 = "00000002-0000-1000-8000-00805f9b34fb"; |
80 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID. | 117 std::string characteristic_uuid3 = characteristic_uuid2; // Duplicate UUID. |
81 SimulateGattCharacteristic(service, characteristic_uuid1); | 118 SimulateGattCharacteristic(service, characteristic_uuid1); |
82 SimulateGattCharacteristic(service, characteristic_uuid2); | 119 SimulateGattCharacteristic(service, characteristic_uuid2); |
83 SimulateGattCharacteristic(service, characteristic_uuid3); | 120 SimulateGattCharacteristic(service, characteristic_uuid3); |
84 | 121 |
85 EXPECT_EQ(3u, service->GetCharacteristics().size()); | 122 EXPECT_EQ(3u, service->GetCharacteristics().size()); |
86 // TODO(scheib): Implement GetIdentifier. crbug.com/545682 | 123 std::string char_id1 = service->GetCharacteristics()[0]->GetIdentifier(); |
87 // std::string characteristic_id1 = | 124 std::string char_id2 = service->GetCharacteristics()[1]->GetIdentifier(); |
88 // service->GetCharacteristics()[0]->GetIdentifier(); | 125 std::string char_id3 = service->GetCharacteristics()[2]->GetIdentifier(); |
89 // ... | 126 EXPECT_TRUE(service->GetCharacteristic(char_id1)); |
90 // EXPECT_NE(characteristic_id2, characteristic_id3); | 127 EXPECT_TRUE(service->GetCharacteristic(char_id2)); |
91 // For now, just hard-code: | 128 EXPECT_TRUE(service->GetCharacteristic(char_id3)); |
92 std::string characteristic_id1 = "00000001-0000-1000-8000-00805f9b34fb0"; | |
93 std::string characteristic_id2 = "00000002-0000-1000-8000-00805f9b34fb0"; | |
94 std::string characteristic_id3 = "00000002-0000-1000-8000-00805f9b34fb1"; | |
95 EXPECT_TRUE(service->GetCharacteristic(characteristic_id1)); | |
96 EXPECT_TRUE(service->GetCharacteristic(characteristic_id2)); | |
97 EXPECT_TRUE(service->GetCharacteristic(characteristic_id3)); | |
98 } | 129 } |
99 #endif // defined(OS_ANDROID) | 130 #endif // defined(OS_ANDROID) |
100 | 131 |
101 } // namespace device | 132 } // namespace device |
OLD | NEW |