Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "device/bluetooth/bluez/bluetooth_service_record_bluez.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/memory/ref_counted.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
| 16 #include "device/bluetooth/bluez/bluetooth_adapter_bluez.h" | |
| 17 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 namespace bluez { | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 constexpr uint16_t kServiceUuidAttributeId = 0x0003; | |
| 25 | |
| 26 constexpr char kServiceUuid1[] = "00001801-0000-1000-8000-00805f9b34fb"; | |
| 27 constexpr char kServiceUuid2[] = "00001801-0000-1000-7000-00805f9b34fb"; | |
| 28 constexpr char kServiceUuid3[] = "00001801-0000-1000-3000-00805f9b34fb"; | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 class BluetoothServiceRecordBlueZTest : public testing::Test { | |
| 33 public: | |
| 34 BluetoothServiceRecordBlueZTest() | |
| 35 : adapter_bluez_(nullptr), | |
| 36 success_callbacks_(0), | |
| 37 error_callbacks_(0), | |
| 38 run_loop_(nullptr), | |
| 39 last_seen_handle_(0) {} | |
| 40 | |
| 41 void SetUp() override { | |
| 42 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter = | |
| 43 bluez::BluezDBusManager::GetSetterForTesting(); | |
| 44 | |
| 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 } | |
| 55 | |
| 56 uint32_t CreateServiceRecordWithCallbacks( | |
| 57 const BluetoothServiceRecordBlueZ& record) { | |
| 58 const size_t old_success_callbacks = success_callbacks_; | |
| 59 const size_t old_error_callbacks = error_callbacks_; | |
| 60 // 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 | |
| 62 // 0 as an invalid handle return value is okay. | |
| 63 last_seen_handle_ = 0; | |
| 64 adapter_bluez_->CreateServiceRecord( | |
| 65 record, | |
| 66 base::Bind( | |
| 67 &BluetoothServiceRecordBlueZTest::CreateServiceSuccessCallback, | |
| 68 base::Unretained(this)), | |
| 69 base::Bind(&BluetoothServiceRecordBlueZTest::ErrorCallback, | |
| 70 base::Unretained(this))); | |
| 71 EXPECT_EQ(old_success_callbacks + 1, success_callbacks_); | |
| 72 EXPECT_EQ(old_error_callbacks, error_callbacks_); | |
| 73 EXPECT_NE(0u, last_seen_handle_); | |
| 74 return last_seen_handle_; | |
| 75 } | |
| 76 | |
| 77 void RemoveServiceRecordWithCallbacks(uint32_t handle, bool expect_success) { | |
| 78 const size_t old_success_callbacks = success_callbacks_; | |
| 79 const size_t old_error_callbacks = error_callbacks_; | |
| 80 adapter_bluez_->RemoveServiceRecord( | |
| 81 handle, | |
| 82 base::Bind( | |
| 83 &BluetoothServiceRecordBlueZTest::RemoveServiceSuccessCallback, | |
| 84 base::Unretained(this)), | |
| 85 base::Bind(&BluetoothServiceRecordBlueZTest::ErrorCallback, | |
| 86 base::Unretained(this))); | |
| 87 size_t success = expect_success ? 1 : 0; | |
| 88 EXPECT_EQ(old_success_callbacks + success, success_callbacks_); | |
| 89 EXPECT_EQ(old_error_callbacks + 1 - success, error_callbacks_); | |
| 90 } | |
| 91 | |
| 92 protected: | |
| 93 BluetoothServiceRecordBlueZ CreateaServiceRecord(const std::string uuid) { | |
| 94 std::map<uint16_t, BluetoothServiceAttributeValueBlueZ> attributes; | |
| 95 attributes.emplace(kServiceUuidAttributeId, | |
| 96 BluetoothServiceAttributeValueBlueZ( | |
| 97 BluetoothServiceAttributeValueBlueZ::UUID, 16, | |
| 98 base::WrapUnique(new base::StringValue(uuid)))); | |
|
xiyuan
2016/06/22 21:28:44
nit: base::MakeUnique
rkc
2016/06/23 19:55:44
Done.
| |
| 99 return BluetoothServiceRecordBlueZ(attributes); | |
| 100 } | |
| 101 | |
| 102 scoped_refptr<device::BluetoothAdapter> adapter_; | |
| 103 BluetoothAdapterBlueZ* adapter_bluez_; | |
| 104 size_t success_callbacks_; | |
| 105 size_t error_callbacks_; | |
| 106 | |
| 107 private: | |
| 108 void RunRunLoop() { | |
| 109 run_loop_ = base::WrapUnique(new base::RunLoop()); | |
| 110 run_loop_->Run(); | |
| 111 } | |
| 112 | |
| 113 void QuitRunLoop() { | |
| 114 if (run_loop_) | |
| 115 run_loop_->Quit(); | |
| 116 } | |
| 117 | |
| 118 void AdapterCallback(scoped_refptr<device::BluetoothAdapter> adapter) { | |
| 119 adapter_ = adapter; | |
| 120 adapter_bluez_ = static_cast<BluetoothAdapterBlueZ*>(adapter_.get()); | |
| 121 QuitRunLoop(); | |
| 122 } | |
| 123 | |
| 124 void CreateServiceSuccessCallback(uint32_t handle) { | |
| 125 last_seen_handle_ = handle; | |
| 126 ++success_callbacks_; | |
| 127 } | |
| 128 | |
| 129 void RemoveServiceSuccessCallback() { ++success_callbacks_; } | |
| 130 | |
| 131 void ErrorCallback(BluetoothServiceRecordBlueZ::ErrorCode code) { | |
| 132 ++error_callbacks_; | |
| 133 } | |
| 134 | |
| 135 base::MessageLoop message_loop_; | |
| 136 std::unique_ptr<base::RunLoop> run_loop_; | |
| 137 uint32_t last_seen_handle_; | |
| 138 | |
| 139 DISALLOW_COPY_AND_ASSIGN(BluetoothServiceRecordBlueZTest); | |
| 140 }; | |
| 141 | |
| 142 TEST_F(BluetoothServiceRecordBlueZTest, CreateAndRemove) { | |
| 143 uint32_t handle1 = | |
| 144 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1)); | |
| 145 uint32_t handle2 = | |
| 146 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid2)); | |
| 147 uint32_t handle3 = | |
| 148 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1)); | |
| 149 uint32_t handle4 = | |
| 150 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid3)); | |
| 151 | |
| 152 RemoveServiceRecordWithCallbacks(handle1, true); | |
| 153 RemoveServiceRecordWithCallbacks(handle3, true); | |
| 154 RemoveServiceRecordWithCallbacks(handle1, false); | |
| 155 RemoveServiceRecordWithCallbacks(handle4, true); | |
| 156 RemoveServiceRecordWithCallbacks(handle2, true); | |
| 157 } | |
| 158 | |
| 159 } // namespace bluez | |
| OLD | NEW |