Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(572)

Side by Side Diff: device/bluetooth/bluez/bluetooth_service_record_bluez_unittest.cc

Issue 2102093003: Implement BluetoothDeviceBlueZ::GetServiceRecords. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
30 } // namespace 33 } // namespace
31 34
32 class BluetoothServiceRecordBlueZTest : public testing::Test { 35 class BluetoothServiceRecordBlueZTest : public device::BluetoothTestBlueZ {
33 public: 36 public:
34 BluetoothServiceRecordBlueZTest() 37 BluetoothServiceRecordBlueZTest()
35 : adapter_bluez_(nullptr), 38 : adapter_bluez_(nullptr),
36 success_callbacks_(0), 39 success_callbacks_(0),
37 error_callbacks_(0), 40 error_callbacks_(0),
38 run_loop_(nullptr),
39 last_seen_handle_(0) {} 41 last_seen_handle_(0) {}
40 42
41 void SetUp() override { 43 void SetUp() override {
42 std::unique_ptr<bluez::BluezDBusManagerSetter> dbus_setter = 44 BluetoothTestBlueZ::SetUp();
43 bluez::BluezDBusManager::GetSetterForTesting(); 45 InitWithFakeAdapter();
44 46 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 } 47 }
55 48
56 uint32_t CreateServiceRecordWithCallbacks( 49 uint32_t CreateServiceRecordWithCallbacks(
57 const BluetoothServiceRecordBlueZ& record) { 50 const BluetoothServiceRecordBlueZ& record) {
58 const size_t old_success_callbacks = success_callbacks_; 51 const size_t old_success_callbacks = success_callbacks_;
59 const size_t old_error_callbacks = error_callbacks_; 52 const size_t old_error_callbacks = error_callbacks_;
60 // Note: Our fake implementation will never give us a handle of 0, so we 53 // 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 54 // 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. 55 // 0 as an invalid handle return value is okay.
63 last_seen_handle_ = 0; 56 last_seen_handle_ = 0;
(...skipping 18 matching lines...) Expand all
82 base::Bind( 75 base::Bind(
83 &BluetoothServiceRecordBlueZTest::RemoveServiceSuccessCallback, 76 &BluetoothServiceRecordBlueZTest::RemoveServiceSuccessCallback,
84 base::Unretained(this)), 77 base::Unretained(this)),
85 base::Bind(&BluetoothServiceRecordBlueZTest::ErrorCallback, 78 base::Bind(&BluetoothServiceRecordBlueZTest::ErrorCallback,
86 base::Unretained(this))); 79 base::Unretained(this)));
87 size_t success = expect_success ? 1 : 0; 80 size_t success = expect_success ? 1 : 0;
88 EXPECT_EQ(old_success_callbacks + success, success_callbacks_); 81 EXPECT_EQ(old_success_callbacks + success, success_callbacks_);
89 EXPECT_EQ(old_error_callbacks + 1 - success, error_callbacks_); 82 EXPECT_EQ(old_error_callbacks + 1 - success, error_callbacks_);
90 } 83 }
91 84
85 void GetServiceRecords(BluetoothDeviceBlueZ* device, bool expect_success) {
86 const size_t old_success_callbacks = success_callbacks_;
87 const size_t old_error_callbacks = error_callbacks_;
88 records_.clear();
89 device->GetServiceRecords(
90 base::Bind(&BluetoothServiceRecordBlueZTest::GetServiceRecordsCallback,
91 base::Unretained(this)),
92 base::Bind(&BluetoothServiceRecordBlueZTest::ErrorCallback,
93 base::Unretained(this)));
94 size_t success = expect_success ? 1 : 0;
95 EXPECT_EQ(old_success_callbacks + success, success_callbacks_);
96 EXPECT_EQ(old_error_callbacks + 1 - success, error_callbacks_);
97 }
98
99 void VerifyRecords() {
100 EXPECT_EQ(2u, records_.size());
101
102 std::vector<uint16_t> ids0 = records_[0].GetAttributeIds();
103 EXPECT_EQ(2u, ids0.size());
104
105 BluetoothServiceAttributeValueBlueZ service_handle0 =
106 records_[0].GetAttributeValue(ids0[0]);
107 int32_t int_value;
108 EXPECT_TRUE(service_handle0.value().GetAsInteger(&int_value));
109 EXPECT_EQ(0x1337, int_value);
110
111 BluetoothServiceAttributeValueBlueZ service_class_list =
112 records_[0].GetAttributeValue(ids0[1]);
113 std::string str_value;
114 EXPECT_TRUE(
115 service_class_list.sequence()[0].value().GetAsString(&str_value));
116 EXPECT_EQ("1802", str_value);
117
118 std::vector<uint16_t> ids1 = records_[1].GetAttributeIds();
119 EXPECT_EQ(1u, ids1.size());
120
121 BluetoothServiceAttributeValueBlueZ service_handle1 =
122 records_[1].GetAttributeValue(ids1[0]);
123 EXPECT_TRUE(service_handle1.value().GetAsInteger(&int_value));
124 EXPECT_EQ(0xffffffff, static_cast<uint32_t>(int_value));
125 }
126
92 protected: 127 protected:
93 BluetoothServiceRecordBlueZ CreateaServiceRecord(const std::string uuid) { 128 BluetoothServiceRecordBlueZ CreateaServiceRecord(const std::string uuid) {
94 std::map<uint16_t, BluetoothServiceAttributeValueBlueZ> attributes; 129 BluetoothServiceRecordBlueZ record;
95 attributes.insert(std::pair<uint16_t, BluetoothServiceAttributeValueBlueZ>( 130 record.AddRecordEntry(kServiceUuidAttributeId,
96 kServiceUuidAttributeId, 131 BluetoothServiceAttributeValueBlueZ(
97 BluetoothServiceAttributeValueBlueZ( 132 BluetoothServiceAttributeValueBlueZ::UUID, 16,
98 BluetoothServiceAttributeValueBlueZ::UUID, 16, 133 base::MakeUnique<base::StringValue>(uuid)));
99 base::MakeUnique<base::StringValue>(uuid)))); 134 return record;
100 return BluetoothServiceRecordBlueZ(attributes);
101 } 135 }
102 136
103 scoped_refptr<device::BluetoothAdapter> adapter_;
104 BluetoothAdapterBlueZ* adapter_bluez_; 137 BluetoothAdapterBlueZ* adapter_bluez_;
105 size_t success_callbacks_; 138 size_t success_callbacks_;
106 size_t error_callbacks_; 139 size_t error_callbacks_;
107 140
108 private: 141 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) { 142 void CreateServiceSuccessCallback(uint32_t handle) {
126 last_seen_handle_ = handle; 143 last_seen_handle_ = handle;
127 ++success_callbacks_; 144 ++success_callbacks_;
128 } 145 }
129 146
130 void RemoveServiceSuccessCallback() { ++success_callbacks_; } 147 void RemoveServiceSuccessCallback() { ++success_callbacks_; }
131 148
132 void ErrorCallback(BluetoothServiceRecordBlueZ::ErrorCode code) { 149 void ErrorCallback(BluetoothServiceRecordBlueZ::ErrorCode code) {
133 ++error_callbacks_; 150 ++error_callbacks_;
134 } 151 }
135 152
136 base::MessageLoop message_loop_; 153 void GetServiceRecordsCallback(
137 std::unique_ptr<base::RunLoop> run_loop_; 154 const std::vector<BluetoothServiceRecordBlueZ>& records) {
155 records_ = records;
156 ++success_callbacks_;
157 }
158
138 uint32_t last_seen_handle_; 159 uint32_t last_seen_handle_;
160 std::vector<BluetoothServiceRecordBlueZ> records_;
139 161
140 DISALLOW_COPY_AND_ASSIGN(BluetoothServiceRecordBlueZTest); 162 DISALLOW_COPY_AND_ASSIGN(BluetoothServiceRecordBlueZTest);
141 }; 163 };
142 164
143 TEST_F(BluetoothServiceRecordBlueZTest, CreateAndRemove) { 165 TEST_F(BluetoothServiceRecordBlueZTest, CreateAndRemove) {
144 uint32_t handle1 = 166 uint32_t handle1 =
145 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1)); 167 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1));
146 uint32_t handle2 = 168 uint32_t handle2 =
147 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid2)); 169 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid2));
148 uint32_t handle3 = 170 uint32_t handle3 =
149 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1)); 171 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid1));
150 uint32_t handle4 = 172 uint32_t handle4 =
151 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid3)); 173 CreateServiceRecordWithCallbacks(CreateaServiceRecord(kServiceUuid3));
152 174
153 RemoveServiceRecordWithCallbacks(handle1, true); 175 RemoveServiceRecordWithCallbacks(handle1, true);
154 RemoveServiceRecordWithCallbacks(handle3, true); 176 RemoveServiceRecordWithCallbacks(handle3, true);
155 RemoveServiceRecordWithCallbacks(handle1, false); 177 RemoveServiceRecordWithCallbacks(handle1, false);
156 RemoveServiceRecordWithCallbacks(handle4, true); 178 RemoveServiceRecordWithCallbacks(handle4, true);
157 RemoveServiceRecordWithCallbacks(handle2, true); 179 RemoveServiceRecordWithCallbacks(handle2, true);
158 } 180 }
159 181
182 TEST_F(BluetoothServiceRecordBlueZTest, GetServiceRecords) {
183 BluetoothDeviceBlueZ* device =
184 static_cast<BluetoothDeviceBlueZ*>(adapter_->GetDevice(
185 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress));
186 GetServiceRecords(device, false);
187 device->Connect(nullptr, GetCallback(Call::EXPECTED),
188 GetConnectErrorCallback(Call::NOT_EXPECTED));
189 GetServiceRecords(device, true);
190 VerifyRecords();
191 }
192
160 } // namespace bluez 193 } // namespace bluez
OLDNEW
« no previous file with comments | « device/bluetooth/bluez/bluetooth_service_record_bluez.cc ('k') | device/bluetooth/dbus/bluetooth_device_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698