Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/bluez/bluetooth_service_record_bluez.h" | 5 #include "device/bluetooth/bluez/bluetooth_service_record_bluez.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "device/bluetooth/bluetooth_adapter_factory.h" | 15 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 16 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" | 16 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" |
| 17 #include "device/bluetooth/bluez/bluetooth_device_bluez.h" | |
| 17 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 18 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| 19 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" | |
| 20 #include "device/bluetooth/test/bluetooth_test_bluez.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 22 |
| 20 namespace bluez { | 23 namespace bluez { |
| 21 | 24 |
| 22 namespace { | 25 namespace { |
| 23 | 26 |
| 24 constexpr uint16_t kServiceUuidAttributeId = 0x0003; | 27 constexpr uint16_t kServiceUuidAttributeId = 0x0003; |
| 25 | 28 |
| 26 constexpr char kServiceUuid1[] = "00001801-0000-1000-8000-00805f9b34fb"; | 29 constexpr char kServiceUuid1[] = "00001801-0000-1000-8000-00805f9b34fb"; |
| 27 constexpr char kServiceUuid2[] = "00001801-0000-1000-7000-00805f9b34fb"; | 30 constexpr char kServiceUuid2[] = "00001801-0000-1000-7000-00805f9b34fb"; |
| 28 constexpr char kServiceUuid3[] = "00001801-0000-1000-3000-00805f9b34fb"; | 31 constexpr char kServiceUuid3[] = "00001801-0000-1000-3000-00805f9b34fb"; |
| 29 | 32 |
| 33 class TestPairingDelegate : public device::BluetoothDevice::PairingDelegate { | |
| 34 public: | |
| 35 void RequestPinCode(device::BluetoothDevice* device) override { | |
| 36 LOG(ERROR) << "RKC: 1"; | |
|
xiyuan
2016/06/29 22:43:08
remove debugging log, here and other places?
rkc
2016/06/30 20:02:24
Whoops.
Done.
| |
| 37 } | |
| 38 void RequestPasskey(device::BluetoothDevice* device) override { | |
| 39 LOG(ERROR) << "RKC: 2"; | |
| 40 } | |
| 41 void DisplayPinCode(device::BluetoothDevice* device, | |
| 42 const std::string& pincode) override { | |
| 43 LOG(ERROR) << "RKC: 3"; | |
| 44 } | |
| 45 void DisplayPasskey(device::BluetoothDevice* device, | |
| 46 uint32_t passkey) override { | |
| 47 LOG(ERROR) << "RKC: 4"; | |
| 48 } | |
| 49 void KeysEntered(device::BluetoothDevice* device, uint32_t entered) override { | |
| 50 LOG(ERROR) << "RKC: 5"; | |
| 51 } | |
| 52 void ConfirmPasskey(device::BluetoothDevice* device, | |
| 53 uint32_t passkey) override { | |
| 54 LOG(ERROR) << "RKC: 6"; | |
| 55 } | |
| 56 void AuthorizePairing(device::BluetoothDevice* device) override { | |
| 57 LOG(ERROR) << "RKC: 7"; | |
| 58 } | |
| 59 }; | |
| 60 | |
| 30 } // namespace | 61 } // namespace |
| 31 | 62 |
| 32 class BluetoothServiceRecordBlueZTest : public testing::Test { | 63 class BluetoothServiceRecordBlueZTest : public device::BluetoothTestBlueZ { |
| 33 public: | 64 public: |
| 34 BluetoothServiceRecordBlueZTest() | 65 BluetoothServiceRecordBlueZTest() |
| 35 : adapter_bluez_(nullptr), | 66 : adapter_bluez_(nullptr), |
| 36 success_callbacks_(0), | 67 success_callbacks_(0), |
| 37 error_callbacks_(0), | 68 error_callbacks_(0), |
| 38 run_loop_(nullptr), | |
| 39 last_seen_handle_(0) {} | 69 last_seen_handle_(0) {} |
| 40 | 70 |
| 41 void SetUp() override { | 71 void SetUp() override { |
| 42 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter = | 72 BluetoothTestBlueZ::SetUp(); |
| 43 bluez::BluezDBusManager::GetSetterForTesting(); | 73 InitWithFakeAdapter(); |
| 44 | 74 adapter_bluez_ = static_cast<bluez::BluetoothAdapterBlueZ*>(adapter_.get()); |
| 45 device::BluetoothAdapterFactory::GetAdapter( | |
| 46 base::Bind(&BluetoothServiceRecordBlueZTest::AdapterCallback, | |
| 47 base::Unretained(this))); | |
| 48 RunRunLoop(); | |
| 49 } | |
| 50 | |
| 51 void TearDown() override { | |
| 52 device::BluetoothAdapterFactory::Shutdown(); | |
| 53 bluez::BluezDBusManager::Shutdown(); | |
| 54 } | 75 } |
| 55 | 76 |
| 56 uint32_t CreateServiceRecordWithCallbacks( | 77 uint32_t CreateServiceRecordWithCallbacks( |
| 57 const BluetoothServiceRecordBlueZ& record) { | 78 const BluetoothServiceRecordBlueZ& record) { |
| 58 const size_t old_success_callbacks = success_callbacks_; | 79 const size_t old_success_callbacks = success_callbacks_; |
| 59 const size_t old_error_callbacks = error_callbacks_; | 80 const size_t old_error_callbacks = error_callbacks_; |
| 60 // Note: Our fake implementation will never give us a handle of 0, so we | 81 // Note: Our fake implementation will never give us a handle of 0, so we |
| 61 // can know that we will never have a record with the handle 0. Hence using | 82 // can know that we will never have a record with the handle 0. Hence using |
| 62 // 0 as an invalid handle return value is okay. | 83 // 0 as an invalid handle return value is okay. |
| 63 last_seen_handle_ = 0; | 84 last_seen_handle_ = 0; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 82 base::Bind( | 103 base::Bind( |
| 83 &BluetoothServiceRecordBlueZTest::RemoveServiceSuccessCallback, | 104 &BluetoothServiceRecordBlueZTest::RemoveServiceSuccessCallback, |
| 84 base::Unretained(this)), | 105 base::Unretained(this)), |
| 85 base::Bind(&BluetoothServiceRecordBlueZTest::ErrorCallback, | 106 base::Bind(&BluetoothServiceRecordBlueZTest::ErrorCallback, |
| 86 base::Unretained(this))); | 107 base::Unretained(this))); |
| 87 size_t success = expect_success ? 1 : 0; | 108 size_t success = expect_success ? 1 : 0; |
| 88 EXPECT_EQ(old_success_callbacks + success, success_callbacks_); | 109 EXPECT_EQ(old_success_callbacks + success, success_callbacks_); |
| 89 EXPECT_EQ(old_error_callbacks + 1 - success, error_callbacks_); | 110 EXPECT_EQ(old_error_callbacks + 1 - success, error_callbacks_); |
| 90 } | 111 } |
| 91 | 112 |
| 113 void GetServiceRecords(BluetoothDeviceBlueZ* device, bool expect_success) { | |
| 114 const size_t old_success_callbacks = success_callbacks_; | |
| 115 const size_t old_error_callbacks = error_callbacks_; | |
| 116 records_.clear(); | |
| 117 device->GetServiceRecords( | |
| 118 base::Bind(&BluetoothServiceRecordBlueZTest::GetServiceRecordsCallback, | |
| 119 base::Unretained(this)), | |
| 120 base::Bind(&BluetoothServiceRecordBlueZTest::ErrorCallback, | |
| 121 base::Unretained(this))); | |
| 122 size_t success = expect_success ? 1 : 0; | |
|
Miao
2016/06/30 04:55:59
Instead of passing an extra |bool expect_success|
rkc
2016/06/30 20:02:24
I expect us to have more tests :)
| |
| 123 EXPECT_EQ(old_success_callbacks + success, success_callbacks_); | |
| 124 EXPECT_EQ(old_error_callbacks + 1 - success, error_callbacks_); | |
| 125 } | |
| 126 | |
| 127 void VerifyRecords() { | |
| 128 EXPECT_EQ(2u, records_.size()); | |
| 129 | |
| 130 std::vector<uint16_t> ids0 = records_[0].GetAttributeIds(); | |
| 131 EXPECT_EQ(2u, ids0.size()); | |
| 132 | |
| 133 BluetoothServiceAttributeValueBlueZ service_handle0 = | |
| 134 records_[0].GetAttributeValue(ids0[0]); | |
| 135 int32_t int_value; | |
| 136 EXPECT_TRUE(service_handle0.value().GetAsInteger(&int_value)); | |
| 137 EXPECT_EQ(0x1337, int_value); | |
| 138 | |
| 139 BluetoothServiceAttributeValueBlueZ service_class_list = | |
| 140 records_[0].GetAttributeValue(ids0[1]); | |
| 141 std::string str_value; | |
| 142 EXPECT_TRUE( | |
| 143 service_class_list.sequence()[0].value().GetAsString(&str_value)); | |
| 144 EXPECT_EQ("1802", str_value); | |
| 145 | |
| 146 std::vector<uint16_t> ids1 = records_[1].GetAttributeIds(); | |
| 147 EXPECT_EQ(1u, ids1.size()); | |
| 148 | |
| 149 BluetoothServiceAttributeValueBlueZ service_handle1 = | |
| 150 records_[1].GetAttributeValue(ids1[0]); | |
| 151 EXPECT_TRUE(service_handle1.value().GetAsInteger(&int_value)); | |
| 152 EXPECT_EQ(0xffffffff, static_cast<uint32_t>(int_value)); | |
| 153 } | |
| 154 | |
| 92 protected: | 155 protected: |
| 93 BluetoothServiceRecordBlueZ CreateaServiceRecord(const std::string uuid) { | 156 BluetoothServiceRecordBlueZ CreateaServiceRecord(const std::string uuid) { |
| 94 std::map<uint16_t, BluetoothServiceAttributeValueBlueZ> attributes; | 157 BluetoothServiceRecordBlueZ record; |
| 95 attributes.insert(std::pair<uint16_t, BluetoothServiceAttributeValueBlueZ>( | 158 record.AddRecordEntry(kServiceUuidAttributeId, |
| 96 kServiceUuidAttributeId, | 159 BluetoothServiceAttributeValueBlueZ( |
| 97 BluetoothServiceAttributeValueBlueZ( | 160 BluetoothServiceAttributeValueBlueZ::UUID, 16, |
| 98 BluetoothServiceAttributeValueBlueZ::UUID, 16, | 161 base::MakeUnique<base::StringValue>(uuid))); |
| 99 base::MakeUnique<base::StringValue>(uuid)))); | 162 return record; |
| 100 return BluetoothServiceRecordBlueZ(attributes); | |
| 101 } | 163 } |
| 102 | 164 |
| 103 scoped_refptr<device::BluetoothAdapter> adapter_; | |
| 104 BluetoothAdapterBlueZ* adapter_bluez_; | 165 BluetoothAdapterBlueZ* adapter_bluez_; |
| 105 size_t success_callbacks_; | 166 size_t success_callbacks_; |
| 106 size_t error_callbacks_; | 167 size_t error_callbacks_; |
| 107 | 168 |
| 108 private: | 169 private: |
| 109 void RunRunLoop() { | |
| 110 run_loop_ = base::MakeUnique<base::RunLoop>(); | |
| 111 run_loop_->Run(); | |
| 112 } | |
| 113 | |
| 114 void QuitRunLoop() { | |
| 115 if (run_loop_) | |
| 116 run_loop_->Quit(); | |
| 117 } | |
| 118 | |
| 119 void AdapterCallback(scoped_refptr<device::BluetoothAdapter> adapter) { | |
| 120 adapter_ = adapter; | |
| 121 adapter_bluez_ = static_cast<BluetoothAdapterBlueZ*>(adapter_.get()); | |
| 122 QuitRunLoop(); | |
| 123 } | |
| 124 | |
| 125 void CreateServiceSuccessCallback(uint32_t handle) { | 170 void CreateServiceSuccessCallback(uint32_t handle) { |
| 126 last_seen_handle_ = handle; | 171 last_seen_handle_ = handle; |
| 127 ++success_callbacks_; | 172 ++success_callbacks_; |
| 128 } | 173 } |
| 129 | 174 |
| 130 void RemoveServiceSuccessCallback() { ++success_callbacks_; } | 175 void RemoveServiceSuccessCallback() { ++success_callbacks_; } |
| 131 | 176 |
| 132 void ErrorCallback(BluetoothServiceRecordBlueZ::ErrorCode code) { | 177 void ErrorCallback(BluetoothServiceRecordBlueZ::ErrorCode code) { |
| 133 ++error_callbacks_; | 178 ++error_callbacks_; |
| 134 } | 179 } |
| 135 | 180 |
| 136 base::MessageLoop message_loop_; | 181 void GetServiceRecordsCallback( |
| 137 std::unique_ptr<base::RunLoop> run_loop_; | 182 std::vector<BluetoothServiceRecordBlueZ> records) { |
| 183 records_ = records; | |
| 184 ++success_callbacks_; | |
| 185 } | |
| 186 | |
| 138 uint32_t last_seen_handle_; | 187 uint32_t last_seen_handle_; |
| 188 std::vector<BluetoothServiceRecordBlueZ> records_; | |
| 139 | 189 |
| 140 DISALLOW_COPY_AND_ASSIGN(BluetoothServiceRecordBlueZTest); | 190 DISALLOW_COPY_AND_ASSIGN(BluetoothServiceRecordBlueZTest); |
| 141 }; | 191 }; |
| 142 | 192 |
| 143 TEST_F(BluetoothServiceRecordBlueZTest, CreateAndRemove) { | 193 TEST_F(BluetoothServiceRecordBlueZTest, CreateAndRemove) { |
| 144 uint32_t handle1 = | 194 uint32_t handle1 = |
| 145 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1)); | 195 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1)); |
| 146 uint32_t handle2 = | 196 uint32_t handle2 = |
| 147 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid2)); | 197 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid2)); |
| 148 uint32_t handle3 = | 198 uint32_t handle3 = |
| 149 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1)); | 199 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1)); |
| 150 uint32_t handle4 = | 200 uint32_t handle4 = |
| 151 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid3)); | 201 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid3)); |
| 152 | 202 |
| 153 RemoveServiceRecordWithCallbacks(handle1, true); | 203 RemoveServiceRecordWithCallbacks(handle1, true); |
| 154 RemoveServiceRecordWithCallbacks(handle3, true); | 204 RemoveServiceRecordWithCallbacks(handle3, true); |
| 155 RemoveServiceRecordWithCallbacks(handle1, false); | 205 RemoveServiceRecordWithCallbacks(handle1, false); |
| 156 RemoveServiceRecordWithCallbacks(handle4, true); | 206 RemoveServiceRecordWithCallbacks(handle4, true); |
| 157 RemoveServiceRecordWithCallbacks(handle2, true); | 207 RemoveServiceRecordWithCallbacks(handle2, true); |
| 158 } | 208 } |
| 159 | 209 |
| 210 TEST_F(BluetoothServiceRecordBlueZTest, GetServiceRecords) { | |
| 211 BluetoothDeviceBlueZ* device = | |
| 212 static_cast<BluetoothDeviceBlueZ*>(adapter_->GetDevice( | |
| 213 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress)); | |
| 214 GetServiceRecords(device, false); | |
| 215 device->Connect(nullptr, GetCallback(Call::EXPECTED), | |
| 216 GetConnectErrorCallback(Call::NOT_EXPECTED)); | |
| 217 GetServiceRecords(device, true); | |
| 218 VerifyRecords(); | |
| 219 } | |
| 220 | |
| 160 } // namespace bluez | 221 } // namespace bluez |
| OLD | NEW |