| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_device.h" | 5 #include "device/bluetooth/bluetooth_device.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "device/bluetooth/bluetooth_gatt_service.h" | 13 #include "device/bluetooth/bluetooth_gatt_service.h" |
| 14 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 14 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 16 |
| 17 #if defined(OS_ANDROID) | 17 #if defined(OS_ANDROID) |
| 18 #include "device/bluetooth/test/bluetooth_test_android.h" | 18 #include "device/bluetooth/test/bluetooth_test_android.h" |
| 19 #elif defined(OS_MACOSX) | 19 #elif defined(OS_MACOSX) |
| 20 #include "device/bluetooth/test/bluetooth_test_mac.h" | 20 #include "device/bluetooth/test/bluetooth_test_mac.h" |
| 21 #elif defined(OS_WIN) |
| 22 #include "device/bluetooth/test/bluetooth_test_win.h" |
| 21 #endif | 23 #endif |
| 22 | 24 |
| 23 namespace device { | 25 namespace device { |
| 24 | 26 |
| 25 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { | 27 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) { |
| 26 // There are three valid separators (':', '-', and none). | 28 // There are three valid separators (':', '-', and none). |
| 27 // Case shouldn't matter. | 29 // Case shouldn't matter. |
| 28 const char* const kValidFormats[] = { | 30 const char* const kValidFormats[] = { |
| 29 "1A:2B:3C:4D:5E:6F", | 31 "1A:2B:3C:4D:5E:6F", |
| 30 "1a:2B:3c:4D:5e:6F", | 32 "1a:2B:3c:4D:5e:6F", |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 "1:A2:B3:C4:D5:E6F", | 64 "1:A2:B3:C4:D5:E6F", |
| 63 }; | 65 }; |
| 64 | 66 |
| 65 for (size_t i = 0; i < arraysize(kValidFormats); ++i) { | 67 for (size_t i = 0; i < arraysize(kValidFormats); ++i) { |
| 66 SCOPED_TRACE(std::string("Input format: '") + kValidFormats[i] + "'"); | 68 SCOPED_TRACE(std::string("Input format: '") + kValidFormats[i] + "'"); |
| 67 EXPECT_EQ(std::string(), | 69 EXPECT_EQ(std::string(), |
| 68 BluetoothDevice::CanonicalizeAddress(kValidFormats[i])); | 70 BluetoothDevice::CanonicalizeAddress(kValidFormats[i])); |
| 69 } | 71 } |
| 70 } | 72 } |
| 71 | 73 |
| 72 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 74 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
| 73 // Verifies basic device properties, e.g. GetAddress, GetName, ... | 75 // Verifies basic device properties, e.g. GetAddress, GetName, ... |
| 74 TEST_F(BluetoothTest, LowEnergyDeviceProperties) { | 76 TEST_F(BluetoothTest, LowEnergyDeviceProperties) { |
| 75 if (!PlatformSupportsLowEnergy()) { | 77 if (!PlatformSupportsLowEnergy()) { |
| 76 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 78 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 77 return; | 79 return; |
| 78 } | 80 } |
| 79 InitWithFakeAdapter(); | 81 InitWithFakeAdapter(); |
| 80 StartLowEnergyDiscoverySession(); | 82 StartLowEnergyDiscoverySession(); |
| 81 BluetoothDevice* device = DiscoverLowEnergyDevice(1); | 83 BluetoothDevice* device = DiscoverLowEnergyDevice(1); |
| 82 ASSERT_TRUE(device); | 84 ASSERT_TRUE(device); |
| 85 // Bluetooth class information for BLE device is not available on Windows. |
| 86 #ifndef OS_WIN |
| 83 EXPECT_EQ(0x1F00u, device->GetBluetoothClass()); | 87 EXPECT_EQ(0x1F00u, device->GetBluetoothClass()); |
| 88 #endif |
| 84 EXPECT_EQ(kTestDeviceAddress1, device->GetAddress()); | 89 EXPECT_EQ(kTestDeviceAddress1, device->GetAddress()); |
| 85 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource()); | 90 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource()); |
| 86 EXPECT_EQ(0, device->GetVendorID()); | 91 EXPECT_EQ(0, device->GetVendorID()); |
| 87 EXPECT_EQ(0, device->GetProductID()); | 92 EXPECT_EQ(0, device->GetProductID()); |
| 88 EXPECT_EQ(0, device->GetDeviceID()); | 93 EXPECT_EQ(0, device->GetDeviceID()); |
| 89 EXPECT_EQ(base::UTF8ToUTF16(kTestDeviceName), device->GetName()); | 94 EXPECT_EQ(base::UTF8ToUTF16(kTestDeviceName), device->GetName()); |
| 90 EXPECT_FALSE(device->IsPaired()); | 95 EXPECT_FALSE(device->IsPaired()); |
| 91 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); | 96 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); |
| 92 EXPECT_TRUE(ContainsValue(uuids, BluetoothUUID(kTestUUIDGenericAccess))); | 97 EXPECT_TRUE(ContainsValue(uuids, BluetoothUUID(kTestUUIDGenericAccess))); |
| 93 EXPECT_TRUE(ContainsValue(uuids, BluetoothUUID(kTestUUIDGenericAttribute))); | 98 EXPECT_TRUE(ContainsValue(uuids, BluetoothUUID(kTestUUIDGenericAttribute))); |
| 94 } | 99 } |
| 95 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 100 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
| 96 | 101 |
| 97 #if defined(OS_ANDROID) || defined(OS_MACOSX) | 102 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
| 98 // Device with no advertised Service UUIDs. | 103 // Device with no advertised Service UUIDs. |
| 99 TEST_F(BluetoothTest, LowEnergyDeviceNoUUIDs) { | 104 TEST_F(BluetoothTest, LowEnergyDeviceNoUUIDs) { |
| 100 if (!PlatformSupportsLowEnergy()) { | 105 if (!PlatformSupportsLowEnergy()) { |
| 101 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 106 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 102 return; | 107 return; |
| 103 } | 108 } |
| 104 InitWithFakeAdapter(); | 109 InitWithFakeAdapter(); |
| 105 StartLowEnergyDiscoverySession(); | 110 StartLowEnergyDiscoverySession(); |
| 106 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 111 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 107 ASSERT_TRUE(device); | 112 ASSERT_TRUE(device); |
| 108 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); | 113 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); |
| 109 EXPECT_EQ(0u, uuids.size()); | 114 EXPECT_EQ(0u, uuids.size()); |
| 110 } | 115 } |
| 111 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) | 116 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) |
| 112 | 117 |
| 113 // TODO(scheib): Test with a device with no name. http://crbug.com/506415 | 118 // TODO(scheib): Test with a device with no name. http://crbug.com/506415 |
| 114 // BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() will run, which | 119 // BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() will run, which |
| 115 // requires string resources to be loaded. For that, something like | 120 // requires string resources to be loaded. For that, something like |
| 116 // InitSharedInstance must be run. See unittest files that call that. It will | 121 // InitSharedInstance must be run. See unittest files that call that. It will |
| 117 // also require build configuration to generate string resources into a .pak | 122 // also require build configuration to generate string resources into a .pak |
| 118 // file. | 123 // file. |
| 119 | 124 |
| 120 #if defined(OS_ANDROID) | 125 #if defined(OS_ANDROID) |
| 121 // Basic CreateGattConnection test. | 126 // Basic CreateGattConnection test. |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 460 // TODO: Change to ERROR_AUTH_FAILED. We should be getting a callback | 465 // TODO: Change to ERROR_AUTH_FAILED. We should be getting a callback |
| 461 // only with the first error, but our android framework doesn't yet | 466 // only with the first error, but our android framework doesn't yet |
| 462 // support sending different errors. | 467 // support sending different errors. |
| 463 // http://crbug.com/578191 | 468 // http://crbug.com/578191 |
| 464 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); | 469 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_code_); |
| 465 for (BluetoothGattConnection* connection : gatt_connections_) | 470 for (BluetoothGattConnection* connection : gatt_connections_) |
| 466 EXPECT_FALSE(connection->IsConnected()); | 471 EXPECT_FALSE(connection->IsConnected()); |
| 467 } | 472 } |
| 468 #endif // defined(OS_ANDROID) | 473 #endif // defined(OS_ANDROID) |
| 469 | 474 |
| 470 #if defined(OS_ANDROID) | 475 #if defined(OS_ANDROID) || defined(OS_WIN) |
| 471 TEST_F(BluetoothTest, GattServices_ObserversCalls) { | 476 TEST_F(BluetoothTest, GattServices_ObserversCalls) { |
| 477 if (!PlatformSupportsLowEnergy()) { |
| 478 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 479 return; |
| 480 } |
| 472 InitWithFakeAdapter(); | 481 InitWithFakeAdapter(); |
| 473 StartLowEnergyDiscoverySession(); | 482 StartLowEnergyDiscoverySession(); |
| 474 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 483 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 475 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 484 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 476 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 485 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 477 TestBluetoothAdapterObserver observer(adapter_); | 486 TestBluetoothAdapterObserver observer(adapter_); |
| 478 ResetEventCounts(); | 487 ResetEventCounts(); |
| 479 SimulateGattConnection(device); | 488 SimulateGattConnection(device); |
| 480 EXPECT_EQ(1, gatt_discovery_attempts_); | 489 EXPECT_EQ(1, gatt_discovery_attempts_); |
| 481 | 490 |
| 482 std::vector<std::string> services; | 491 std::vector<std::string> services; |
| 483 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 492 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 484 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 493 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
| 485 SimulateGattServicesDiscovered(device, services); | 494 SimulateGattServicesDiscovered(device, services); |
| 486 | 495 |
| 487 EXPECT_EQ(1, observer.gatt_services_discovered_count()); | 496 EXPECT_EQ(1, observer.gatt_services_discovered_count()); |
| 488 EXPECT_EQ(2, observer.gatt_service_added_count()); | 497 EXPECT_EQ(2, observer.gatt_service_added_count()); |
| 489 } | 498 } |
| 490 #endif // defined(OS_ANDROID) | 499 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
| 491 | 500 |
| 492 #if defined(OS_ANDROID) | 501 #if defined(OS_ANDROID) || defined(OS_WIN) |
| 493 TEST_F(BluetoothTest, GetGattServices_and_GetGattService) { | 502 TEST_F(BluetoothTest, GetGattServices_and_GetGattService) { |
| 503 if (!PlatformSupportsLowEnergy()) { |
| 504 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 505 return; |
| 506 } |
| 494 InitWithFakeAdapter(); | 507 InitWithFakeAdapter(); |
| 495 StartLowEnergyDiscoverySession(); | 508 StartLowEnergyDiscoverySession(); |
| 496 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 509 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 497 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 510 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 498 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 511 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 499 ResetEventCounts(); | 512 ResetEventCounts(); |
| 500 SimulateGattConnection(device); | 513 SimulateGattConnection(device); |
| 501 EXPECT_EQ(1, gatt_discovery_attempts_); | 514 EXPECT_EQ(1, gatt_discovery_attempts_); |
| 502 | 515 |
| 503 std::vector<std::string> services; | 516 std::vector<std::string> services; |
| 504 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); | 517 services.push_back("00000000-0000-1000-8000-00805f9b34fb"); |
| 505 // 2 duplicate UUIDs creating 2 instances. | 518 // 2 duplicate UUIDs creating 2 instances. |
| 506 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 519 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
| 507 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); | 520 services.push_back("00000001-0000-1000-8000-00805f9b34fb"); |
| 508 SimulateGattServicesDiscovered(device, services); | 521 SimulateGattServicesDiscovered(device, services); |
| 509 EXPECT_EQ(3u, device->GetGattServices().size()); | 522 EXPECT_EQ(3u, device->GetGattServices().size()); |
| 510 | 523 |
| 511 // Test GetGattService: | 524 // Test GetGattService: |
| 512 std::string service_id1 = device->GetGattServices()[0]->GetIdentifier(); | 525 std::string service_id1 = device->GetGattServices()[0]->GetIdentifier(); |
| 513 std::string service_id2 = device->GetGattServices()[1]->GetIdentifier(); | 526 std::string service_id2 = device->GetGattServices()[1]->GetIdentifier(); |
| 514 std::string service_id3 = device->GetGattServices()[2]->GetIdentifier(); | 527 std::string service_id3 = device->GetGattServices()[2]->GetIdentifier(); |
| 515 EXPECT_TRUE(device->GetGattService(service_id1)); | 528 EXPECT_TRUE(device->GetGattService(service_id1)); |
| 516 EXPECT_TRUE(device->GetGattService(service_id2)); | 529 EXPECT_TRUE(device->GetGattService(service_id2)); |
| 517 EXPECT_TRUE(device->GetGattService(service_id3)); | 530 EXPECT_TRUE(device->GetGattService(service_id3)); |
| 518 } | 531 } |
| 519 #endif // defined(OS_ANDROID) | 532 #endif // defined(OS_ANDROID) || defined(OS_WIN) |
| 520 | 533 |
| 521 #if defined(OS_ANDROID) | 534 #if defined(OS_ANDROID) |
| 522 TEST_F(BluetoothTest, GetGattServices_DiscoveryError) { | 535 TEST_F(BluetoothTest, GetGattServices_DiscoveryError) { |
| 523 InitWithFakeAdapter(); | 536 InitWithFakeAdapter(); |
| 524 StartLowEnergyDiscoverySession(); | 537 StartLowEnergyDiscoverySession(); |
| 525 BluetoothDevice* device = DiscoverLowEnergyDevice(3); | 538 BluetoothDevice* device = DiscoverLowEnergyDevice(3); |
| 526 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), | 539 device->CreateGattConnection(GetGattConnectionCallback(Call::EXPECTED), |
| 527 GetConnectErrorCallback(Call::NOT_EXPECTED)); | 540 GetConnectErrorCallback(Call::NOT_EXPECTED)); |
| 528 ResetEventCounts(); | 541 ResetEventCounts(); |
| 529 SimulateGattConnection(device); | 542 SimulateGattConnection(device); |
| 530 EXPECT_EQ(1, gatt_discovery_attempts_); | 543 EXPECT_EQ(1, gatt_discovery_attempts_); |
| 531 | 544 |
| 532 SimulateGattServicesDiscoveryError(device); | 545 SimulateGattServicesDiscoveryError(device); |
| 533 EXPECT_EQ(0u, device->GetGattServices().size()); | 546 EXPECT_EQ(0u, device->GetGattServices().size()); |
| 534 } | 547 } |
| 535 #endif // defined(OS_ANDROID) | 548 #endif // defined(OS_ANDROID) |
| 536 | 549 |
| 537 } // namespace device | 550 } // namespace device |
| OLD | NEW |