Add OWNERS to content/browser/quota
[chromium-blink-merge.git] / device / bluetooth / bluetooth_device_unittest.cc
blob4cea1b6fa6e48e13990e2564085aae1aad3bb134
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"
17 #endif
19 namespace device {
21 TEST(BluetoothDeviceTest, CanonicalizeAddressFormat_AcceptsAllValidFormats) {
22 // There are three valid separators (':', '-', and none).
23 // Case shouldn't matter.
24 const char* const kValidFormats[] = {
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 "1a-2B-3c-4D-5e-6F",
30 "1a-2b-3c-4d-5e-6f",
31 "1A2B3C4D5E6F",
32 "1a2B3c4D5e6F",
33 "1a2b3c4d5e6f",
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[] = {
45 // Empty string.
46 "",
47 // Too short.
48 "1A:2B:3C:4D:5E",
49 // Too long.
50 "1A:2B:3C:4D:5E:6F:70",
51 // Missing a separator.
52 "1A:2B:3C:4D:5E6F",
53 // Mixed separators.
54 "1A:2B-3C:4D-5E:6F",
55 // Invalid characters.
56 "1A:2B-3C:4D-5E:6X",
57 // Separators in the wrong place.
58 "1:A2:B3:C4:D5:E6F",
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.";
73 return;
75 InitWithFakeAdapter();
76 TestBluetoothAdapterObserver observer(adapter_);
78 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
79 GetErrorCallback());
80 base::RunLoop().RunUntilIdle();
81 DiscoverLowEnergyDevice(1);
82 base::RunLoop().RunUntilIdle();
83 BluetoothDevice* device = observer.last_device();
84 ASSERT_TRUE(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.";
104 return;
106 InitWithFakeAdapter();
107 TestBluetoothAdapterObserver observer(adapter_);
109 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
110 GetErrorCallback());
111 base::RunLoop().RunUntilIdle();
112 DiscoverLowEnergyDevice(3);
113 base::RunLoop().RunUntilIdle();
114 BluetoothDevice* device = observer.last_device();
115 ASSERT_TRUE(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
126 // file.
128 } // namespace device