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
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_device.h"
7 #include "base/macros.h"
8 #include "base/run_loop.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 #if defined(OS_ANDROID)
14 #include "device/bluetooth/test/bluetooth_test_android.h"
15 #elif defined(OS_MACOSX)
16 #include "device/bluetooth/test/bluetooth_test_mac.h"
21 TEST(BluetoothDeviceTest
, CanonicalizeAddressFormat_AcceptsAllValidFormats
) {
22 // There are three valid separators (':', '-', and none).
23 // Case shouldn't matter.
24 const char* const kValidFormats
[] = {
36 for (size_t i
= 0; i
< arraysize(kValidFormats
); ++i
) {
37 SCOPED_TRACE(std::string("Input format: '") + kValidFormats
[i
] + "'");
38 EXPECT_EQ("1A:2B:3C:4D:5E:6F",
39 BluetoothDevice::CanonicalizeAddress(kValidFormats
[i
]));
43 TEST(BluetoothDeviceTest
, CanonicalizeAddressFormat_RejectsInvalidFormats
) {
44 const char* const kValidFormats
[] = {
50 "1A:2B:3C:4D:5E:6F:70",
51 // Missing a separator.
55 // Invalid characters.
57 // Separators in the wrong place.
61 for (size_t i
= 0; i
< arraysize(kValidFormats
); ++i
) {
62 SCOPED_TRACE(std::string("Input format: '") + kValidFormats
[i
] + "'");
63 EXPECT_EQ(std::string(),
64 BluetoothDevice::CanonicalizeAddress(kValidFormats
[i
]));
68 #if defined(OS_ANDROID) || defined(OS_MACOSX)
69 // Verifies basic device properties, e.g. GetAddress, GetName, ...
70 TEST_F(BluetoothTest
, LowEnergyDeviceProperties
) {
71 if (!PlatformSupportsLowEnergy()) {
72 LOG(WARNING
) << "Low Energy Bluetooth unavailable, skipping unit test.";
75 InitWithFakeAdapter();
76 TestBluetoothAdapterObserver
observer(adapter_
);
78 adapter_
->StartDiscoverySession(GetDiscoverySessionCallback(),
80 base::RunLoop().RunUntilIdle();
81 DiscoverLowEnergyDevice(1);
82 base::RunLoop().RunUntilIdle();
83 BluetoothDevice
* device
= observer
.last_device();
85 EXPECT_EQ(0x1F00u
, device
->GetBluetoothClass());
86 EXPECT_EQ(kTestDeviceAddress1
, device
->GetAddress());
87 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
88 EXPECT_EQ(0, device
->GetVendorID());
89 EXPECT_EQ(0, device
->GetProductID());
90 EXPECT_EQ(0, device
->GetDeviceID());
91 EXPECT_EQ(base::UTF8ToUTF16(kTestDeviceName
), device
->GetName());
92 EXPECT_FALSE(device
->IsPaired());
93 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
94 EXPECT_TRUE(ContainsValue(uuids
, BluetoothUUID(kTestUUIDGenericAccess
)));
95 EXPECT_TRUE(ContainsValue(uuids
, BluetoothUUID(kTestUUIDGenericAttribute
)));
97 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
99 #if defined(OS_ANDROID) || defined(OS_MACOSX)
100 // Device with no advertised Service UUIDs.
101 TEST_F(BluetoothTest
, LowEnergyDeviceNoUUIDs
) {
102 if (!PlatformSupportsLowEnergy()) {
103 LOG(WARNING
) << "Low Energy Bluetooth unavailable, skipping unit test.";
106 InitWithFakeAdapter();
107 TestBluetoothAdapterObserver
observer(adapter_
);
109 adapter_
->StartDiscoverySession(GetDiscoverySessionCallback(),
111 base::RunLoop().RunUntilIdle();
112 DiscoverLowEnergyDevice(3);
113 base::RunLoop().RunUntilIdle();
114 BluetoothDevice
* device
= observer
.last_device();
116 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
117 EXPECT_EQ(0u, uuids
.size());
119 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
121 // TODO(scheib): Test with a device with no name. http://crbug.com/506415
122 // BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() will run, which
123 // requires string resources to be loaded. For that, something like
124 // InitSharedInstance must be run. See unittest files that call that. It will
125 // also require build configuration to generate string resources into a .pak
128 } // namespace device