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

Side by Side Diff: device/bluetooth/bluetooth_adapter_profile_chromeos_unittest.cc

Issue 935383003: Fix BluetoothAdapterProfileChromeOS lifecycle management (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix for leak detected by LSAN Created 5 years, 9 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "base/bind.h" 5 #include "base/bind.h"
6 #include "base/message_loop/message_loop.h" 6 #include "base/message_loop/message_loop.h"
7 #include "chromeos/dbus/bluetooth_profile_service_provider.h" 7 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
8 #include "chromeos/dbus/dbus_thread_manager.h" 8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" 9 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
10 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" 10 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
11 #include "chromeos/dbus/fake_bluetooth_device_client.h" 11 #include "chromeos/dbus/fake_bluetooth_device_client.h"
12 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h" 12 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h"
13 #include "device/bluetooth/bluetooth_adapter.h" 13 #include "device/bluetooth/bluetooth_adapter.h"
14 #include "device/bluetooth/bluetooth_adapter_chromeos.h" 14 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
15 #include "device/bluetooth/bluetooth_adapter_factory.h" 15 #include "device/bluetooth/bluetooth_adapter_factory.h"
16 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" 16 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h"
17 #include "device/bluetooth/bluetooth_uuid.h" 17 #include "device/bluetooth/bluetooth_uuid.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 19
20 using device::BluetoothAdapter; 20 using device::BluetoothAdapter;
21 using device::BluetoothUUID; 21 using device::BluetoothUUID;
22 22
23 namespace {
24
25 void DoNothingDBusErrorCallback(const std::string& error_name,
26 const std::string& error_message) {
27 }
28
29 } // namespace
30
31 namespace chromeos { 23 namespace chromeos {
32 24
33 class BluetoothAdapterProfileChromeOSTest : public testing::Test { 25 class BluetoothAdapterProfileChromeOSTest : public testing::Test {
34 public: 26 public:
35 BluetoothAdapterProfileChromeOSTest() 27 BluetoothAdapterProfileChromeOSTest()
36 : fake_delegate_paired_(FakeBluetoothDeviceClient::kPairedDevicePath), 28 : fake_delegate_paired_(FakeBluetoothDeviceClient::kPairedDevicePath),
37 fake_delegate_autopair_(FakeBluetoothDeviceClient::kLegacyAutopairPath), 29 fake_delegate_autopair_(FakeBluetoothDeviceClient::kLegacyAutopairPath),
38 fake_delegate_listen_(""), 30 fake_delegate_listen_(""),
39 profile_(nullptr), 31 profile_(nullptr),
40 success_callback_count_(0), 32 success_callback_count_(0),
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 scoped_ptr<dbus::FileDescriptor> fd, 87 scoped_ptr<dbus::FileDescriptor> fd,
96 const BluetoothProfileServiceProvider::Delegate::Options& options, 88 const BluetoothProfileServiceProvider::Delegate::Options& options,
97 const ConfirmationCallback& callback) override { 89 const ConfirmationCallback& callback) override {
98 VLOG(1) << "connection for " << device_path.value() << " on " 90 VLOG(1) << "connection for " << device_path.value() << " on "
99 << device_path_.value(); 91 << device_path_.value();
100 ++connections_; 92 ++connections_;
101 fd->CheckValidity(); 93 fd->CheckValidity();
102 close(fd->TakeValue()); 94 close(fd->TakeValue());
103 callback.Run(SUCCESS); 95 callback.Run(SUCCESS);
104 if (device_path_.value() != "") 96 if (device_path_.value() != "")
105 ASSERT_TRUE(device_path == device_path_); 97 ASSERT_EQ(device_path_, device_path);
106 } 98 }
107 99
108 void RequestDisconnection(const dbus::ObjectPath& device_path, 100 void RequestDisconnection(const dbus::ObjectPath& device_path,
109 const ConfirmationCallback& callback) override { 101 const ConfirmationCallback& callback) override {
110 VLOG(1) << "disconnect " << device_path.value(); 102 VLOG(1) << "disconnect " << device_path.value();
111 ++disconnections_; 103 ++disconnections_;
112 } 104 }
113 105
114 void Cancel() override { 106 void Cancel() override {
115 VLOG(1) << "cancel"; 107 VLOG(1) << "cancel";
116 // noop 108 // noop
117 } 109 }
118 110
119 unsigned int connections_; 111 unsigned int connections_;
120 unsigned int disconnections_; 112 unsigned int disconnections_;
121 dbus::ObjectPath device_path_; 113 dbus::ObjectPath device_path_;
122 }; 114 };
123 115
124 FakeDelegate fake_delegate_paired_; 116 FakeDelegate fake_delegate_paired_;
125 FakeDelegate fake_delegate_autopair_; 117 FakeDelegate fake_delegate_autopair_;
126 FakeDelegate fake_delegate_listen_; 118 FakeDelegate fake_delegate_listen_;
127 119
128 BluetoothAdapterProfileChromeOS* profile_; 120 BluetoothAdapterProfileChromeOS* profile_;
129 121
122 void ProfileSuccessCallback(BluetoothAdapterProfileChromeOS* profile) {
123 profile_ = profile;
124 ++success_callback_count_;
125 }
126
127 void MatchedProfileCallback(BluetoothAdapterProfileChromeOS* profile) {
128 VLOG(1) << "Matched Profile Callback";
129 ASSERT_EQ(profile_, profile);
130 ++success_callback_count_;
131 }
132
130 void DBusConnectSuccessCallback() { ++success_callback_count_; } 133 void DBusConnectSuccessCallback() { ++success_callback_count_; }
131 134
132 void DBusErrorCallback(const std::string& error_name, 135 void DBusErrorCallback(const std::string& error_name,
133 const std::string& error_message) { 136 const std::string& error_message) {
134 VLOG(1) << "DBus Connect Error: " << error_name << " - " << error_message; 137 VLOG(1) << "DBus Connect Error: " << error_name << " - " << error_message;
135 ++error_callback_count_; 138 ++error_callback_count_;
136 } 139 }
137 140
141 void BasicErrorCallback(const std::string& error_message) {
142 VLOG(1) << "Error: " << error_message;
143 ++error_callback_count_;
144 }
145
138 protected: 146 protected:
139 base::MessageLoop message_loop_; 147 base::MessageLoop message_loop_;
140 148
141 scoped_refptr<BluetoothAdapter> adapter_; 149 scoped_refptr<BluetoothAdapter> adapter_;
142 150
143 unsigned int success_callback_count_; 151 unsigned int success_callback_count_;
144 unsigned int error_callback_count_; 152 unsigned int error_callback_count_;
145 }; 153 };
146 154
147 TEST_F(BluetoothAdapterProfileChromeOSTest, DelegateCount) { 155 TEST_F(BluetoothAdapterProfileChromeOSTest, DelegateCount) {
148 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid); 156 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid);
149 BluetoothProfileManagerClient::Options options; 157 BluetoothProfileManagerClient::Options options;
150 158
151 options.require_authentication.reset(new bool(false)); 159 options.require_authentication.reset(new bool(false));
152 160
153 profile_ = BluetoothAdapterProfileChromeOS::Register( 161 BluetoothAdapterProfileChromeOS::Register(
154 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()), uuid, options, 162 uuid, options,
155 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); 163 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback,
164 base::Unretained(this)),
165 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
166 base::Unretained(this)));
156 167
157 message_loop_.RunUntilIdle(); 168 message_loop_.RunUntilIdle();
158 169
159 EXPECT_TRUE(profile_); 170 EXPECT_TRUE(profile_);
171 EXPECT_EQ(1U, success_callback_count_);
172 EXPECT_EQ(0U, error_callback_count_);
160 173
161 EXPECT_EQ(0U, profile_->DelegateCount()); 174 EXPECT_EQ(0U, profile_->DelegateCount());
162 175
163 profile_->SetDelegate(fake_delegate_paired_.device_path_, 176 profile_->SetDelegate(fake_delegate_paired_.device_path_,
164 &fake_delegate_paired_); 177 &fake_delegate_paired_);
165 178
166 EXPECT_EQ(1U, profile_->DelegateCount()); 179 EXPECT_EQ(1U, profile_->DelegateCount());
167 180
168 profile_->RemoveDelegate(fake_delegate_autopair_.device_path_, 181 profile_->RemoveDelegate(fake_delegate_autopair_.device_path_,
169 base::Bind(&base::DoNothing)); 182 base::Bind(&base::DoNothing));
170 183
171 EXPECT_EQ(1U, profile_->DelegateCount()); 184 EXPECT_EQ(1U, profile_->DelegateCount());
172 185
173 profile_->RemoveDelegate(fake_delegate_paired_.device_path_, 186 profile_->RemoveDelegate(fake_delegate_paired_.device_path_,
174 base::Bind(&base::DoNothing)); 187 base::Bind(&base::DoNothing));
175 188
176 EXPECT_EQ(0U, profile_->DelegateCount()); 189 EXPECT_EQ(0U, profile_->DelegateCount());
177 190
178 delete profile_; 191 delete profile_;
179 }; 192 };
180 193
181 TEST_F(BluetoothAdapterProfileChromeOSTest, BlackHole) { 194 TEST_F(BluetoothAdapterProfileChromeOSTest, BlackHole) {
182 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid); 195 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid);
183 BluetoothProfileManagerClient::Options options; 196 BluetoothProfileManagerClient::Options options;
184 197
185 options.require_authentication.reset(new bool(false)); 198 options.require_authentication.reset(new bool(false));
186 199
187 profile_ = BluetoothAdapterProfileChromeOS::Register( 200 BluetoothAdapterProfileChromeOS::Register(
188 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()), uuid, options, 201 uuid, options,
189 base::Bind( 202 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback,
190 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback, 203 base::Unretained(this)),
191 base::Unretained(this)),
192 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, 204 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
193 base::Unretained(this))); 205 base::Unretained(this)));
194 206
195 message_loop_.RunUntilIdle(); 207 message_loop_.RunUntilIdle();
196 208
197 EXPECT_TRUE(profile_); 209 EXPECT_TRUE(profile_);
198 EXPECT_EQ(1U, success_callback_count_); 210 EXPECT_EQ(1U, success_callback_count_);
199 EXPECT_EQ(0U, error_callback_count_); 211 EXPECT_EQ(0U, error_callback_count_);
200 212
201 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( 213 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
(...skipping 14 matching lines...) Expand all
216 228
217 delete profile_; 229 delete profile_;
218 }; 230 };
219 231
220 TEST_F(BluetoothAdapterProfileChromeOSTest, Routing) { 232 TEST_F(BluetoothAdapterProfileChromeOSTest, Routing) {
221 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid); 233 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid);
222 BluetoothProfileManagerClient::Options options; 234 BluetoothProfileManagerClient::Options options;
223 235
224 options.require_authentication.reset(new bool(false)); 236 options.require_authentication.reset(new bool(false));
225 237
226 profile_ = BluetoothAdapterProfileChromeOS::Register( 238 BluetoothAdapterProfileChromeOS::Register(
227 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()), uuid, options, 239 uuid, options,
228 base::Bind( 240 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback,
229 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback, 241 base::Unretained(this)),
230 base::Unretained(this)),
231 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, 242 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
232 base::Unretained(this))); 243 base::Unretained(this)));
233 244
234 message_loop_.RunUntilIdle(); 245 message_loop_.RunUntilIdle();
235 246
236 ASSERT_TRUE(profile_); 247 ASSERT_TRUE(profile_);
248 ASSERT_EQ(1U, success_callback_count_);
249 ASSERT_EQ(0U, error_callback_count_);
237 250
238 profile_->SetDelegate(fake_delegate_paired_.device_path_, 251 profile_->SetDelegate(fake_delegate_paired_.device_path_,
239 &fake_delegate_paired_); 252 &fake_delegate_paired_);
240 profile_->SetDelegate(fake_delegate_autopair_.device_path_, 253 profile_->SetDelegate(fake_delegate_autopair_.device_path_,
241 &fake_delegate_autopair_); 254 &fake_delegate_autopair_);
242 profile_->SetDelegate(fake_delegate_listen_.device_path_, 255 profile_->SetDelegate(fake_delegate_listen_.device_path_,
243 &fake_delegate_listen_); 256 &fake_delegate_listen_);
244 257
245 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( 258 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
246 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath), 259 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath),
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 message_loop_.RunUntilIdle(); 300 message_loop_.RunUntilIdle();
288 301
289 EXPECT_EQ(4U, success_callback_count_); 302 EXPECT_EQ(4U, success_callback_count_);
290 EXPECT_EQ(0U, error_callback_count_); 303 EXPECT_EQ(0U, error_callback_count_);
291 304
292 EXPECT_EQ(1U, fake_delegate_listen_.connections_); 305 EXPECT_EQ(1U, fake_delegate_listen_.connections_);
293 306
294 delete profile_; 307 delete profile_;
295 }; 308 };
296 309
310 TEST_F(BluetoothAdapterProfileChromeOSTest, SimultaneousRegister) {
311 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid);
312 BluetoothProfileManagerClient::Options options;
313 BluetoothAdapterChromeOS* adapter =
314 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
315
316 options.require_authentication.reset(new bool(false));
317
318 success_callback_count_ = 0;
319 error_callback_count_ = 0;
320
321 adapter->UseProfile(
322 uuid, fake_delegate_paired_.device_path_, options, &fake_delegate_paired_,
323 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback,
324 base::Unretained(this)),
325 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback,
326 base::Unretained(this)));
327
328 adapter->UseProfile(
329 uuid, fake_delegate_autopair_.device_path_, options,
330 &fake_delegate_autopair_,
331 base::Bind(&BluetoothAdapterProfileChromeOSTest::MatchedProfileCallback,
332 base::Unretained(this)),
333 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback,
334 base::Unretained(this)));
335
336 message_loop_.RunUntilIdle();
337
338 EXPECT_TRUE(profile_);
339 EXPECT_EQ(2U, success_callback_count_);
340 EXPECT_EQ(0U, error_callback_count_);
341
342 adapter->ReleaseProfile(fake_delegate_paired_.device_path_, profile_);
343 adapter->ReleaseProfile(fake_delegate_autopair_.device_path_, profile_);
344
345 message_loop_.RunUntilIdle();
346 };
347
348 TEST_F(BluetoothAdapterProfileChromeOSTest, SimultaneousRegisterFail) {
349 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kUnregisterableUuid);
350 BluetoothProfileManagerClient::Options options;
351 BluetoothAdapterChromeOS* adapter =
352 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
353
354 options.require_authentication.reset(new bool(false));
355
356 success_callback_count_ = 0;
357 error_callback_count_ = 0;
358
359 adapter->UseProfile(
360 uuid, fake_delegate_paired_.device_path_, options, &fake_delegate_paired_,
361 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback,
362 base::Unretained(this)),
363 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback,
364 base::Unretained(this)));
365
366 adapter->UseProfile(
367 uuid, fake_delegate_autopair_.device_path_, options,
368 &fake_delegate_autopair_,
369 base::Bind(&BluetoothAdapterProfileChromeOSTest::MatchedProfileCallback,
370 base::Unretained(this)),
371 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback,
372 base::Unretained(this)));
373
374 message_loop_.RunUntilIdle();
375
376 EXPECT_FALSE(profile_);
377 EXPECT_EQ(0U, success_callback_count_);
378 EXPECT_EQ(2U, error_callback_count_);
379 };
380
297 } // namespace chromeos 381 } // namespace chromeos
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_adapter_profile_chromeos.cc ('k') | device/bluetooth/bluetooth_chromeos_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698