Process Alt-Svc headers.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_device_unittest.cc
blobc92392d1327d80b24175c263957cc0e690ac60b5
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 #endif
17 namespace device {
19 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) {
20 // There are three valid separators (':', '-', and none).
21 // Case shouldn't matter.
22 const char* const kValidFormats[] = {
23 "1A:2B:3C:4D:5E:6F",
24 "1a:2B:3c:4D:5e:6F",
25 "1a:2b:3c:4d:5e:6f",
26 "1A-2B-3C-4D-5E-6F",
27 "1a-2B-3c-4D-5e-6F",
28 "1a-2b-3c-4d-5e-6f",
29 "1A2B3C4D5E6F",
30 "1a2B3c4D5e6F",
31 "1a2b3c4d5e6f",
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[] = {
43 // Empty string.
44 "",
45 // Too short.
46 "1A:2B:3C:4D:5E",
47 // Too long.
48 "1A:2B:3C:4D:5E:6F:70",
49 // Missing a separator.
50 "1A:2B:3C:4D:5E6F",
51 // Mixed separators.
52 "1A:2B-3C:4D-5E:6F",
53 // Invalid characters.
54 "1A:2B-3C:4D-5E:6X",
55 // Separators in the wrong place.
56 "1:A2:B3:C4:D5:E6F",
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(),
73 GetErrorCallback());
74 base::RunLoop().RunUntilIdle();
75 DiscoverLowEnergyDevice(1);
76 base::RunLoop().RunUntilIdle();
77 BluetoothDevice* device = observer.last_device();
78 ASSERT_TRUE(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(),
100 GetErrorCallback());
101 base::RunLoop().RunUntilIdle();
102 DiscoverLowEnergyDevice(3);
103 base::RunLoop().RunUntilIdle();
104 BluetoothDevice* device = observer.last_device();
105 ASSERT_TRUE(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
116 // file.
118 } // namespace device