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"
19 TEST(BluetoothDeviceTest
, CanonicalizeAddressFormat_AcceptsAllValidFormats
) {
20 // There are three valid separators (':', '-', and none).
21 // Case shouldn't matter.
22 const char* const kValidFormats
[] = {
34 for (size_t i
= 0; i
< arraysize(kValidFormats
); ++i
) {
35 SCOPED_TRACE(std::string("Input format: '") + kValidFormats
[i
] + "'");
36 EXPECT_EQ("1A:2B:3C:4D:5E:6F",
37 BluetoothDevice::CanonicalizeAddress(kValidFormats
[i
]));
41 TEST(BluetoothDeviceTest
, CanonicalizeAddressFormat_RejectsInvalidFormats
) {
42 const char* const kValidFormats
[] = {
48 "1A:2B:3C:4D:5E:6F:70",
49 // Missing a separator.
53 // Invalid characters.
55 // Separators in the wrong place.
59 for (size_t i
= 0; i
< arraysize(kValidFormats
); ++i
) {
60 SCOPED_TRACE(std::string("Input format: '") + kValidFormats
[i
] + "'");
61 EXPECT_EQ(std::string(),
62 BluetoothDevice::CanonicalizeAddress(kValidFormats
[i
]));
66 #if defined(OS_ANDROID)
67 // Verifies basic device properties, e.g. GetAddress, GetName, ...
68 TEST_F(BluetoothTest
, DeviceProperties
) {
69 InitWithFakeAdapter();
70 TestBluetoothAdapterObserver
observer(adapter_
);
72 adapter_
->StartDiscoverySession(GetDiscoverySessionCallback(),
74 base::RunLoop().RunUntilIdle();
75 DiscoverLowEnergyDevice(1);
76 base::RunLoop().RunUntilIdle();
77 BluetoothDevice
* device
= observer
.last_device();
79 EXPECT_EQ(0x1F00u
, device
->GetBluetoothClass());
80 EXPECT_EQ("AA:00:00:00:00:01", device
->GetAddress());
81 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
82 EXPECT_EQ(0, device
->GetVendorID());
83 EXPECT_EQ(0, device
->GetProductID());
84 EXPECT_EQ(0, device
->GetDeviceID());
85 EXPECT_EQ(base::UTF8ToUTF16("FakeBluetoothDevice"), device
->GetName());
86 EXPECT_EQ(true, device
->IsPaired());
87 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
88 EXPECT_TRUE(ContainsValue(uuids
, BluetoothUUID("1800")));
89 EXPECT_TRUE(ContainsValue(uuids
, BluetoothUUID("1801")));
91 #endif // defined(OS_ANDROID)
93 #if defined(OS_ANDROID)
94 // Device with no advertised Service UUIDs.
95 TEST_F(BluetoothTest
, DeviceNoUUIDs
) {
96 InitWithFakeAdapter();
97 TestBluetoothAdapterObserver
observer(adapter_
);
99 adapter_
->StartDiscoverySession(GetDiscoverySessionCallback(),
101 base::RunLoop().RunUntilIdle();
102 DiscoverLowEnergyDevice(3);
103 base::RunLoop().RunUntilIdle();
104 BluetoothDevice
* device
= observer
.last_device();
106 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
107 EXPECT_EQ(0u, uuids
.size());
109 #endif // defined(OS_ANDROID)
111 // TODO(scheib): Test with a device with no name. http://crbug.com/506415
112 // BluetoothDevice::GetAddressWithLocalizedDeviceTypeName() will run, which
113 // requires string resources to be loaded. For that, something like
114 // InitSharedInstance must be run. See unittest files that call that. It will
115 // also require build configuration to generate string resources into a .pak
118 } // namespace device