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

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

Issue 1415573014: Reland "Add Linux support for the Bluetooth API" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: build fix. Created 5 years, 1 month 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 2014 The Chromium Authors. All rights reserved. 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 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/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "device/bluetooth/bluetooth_adapter.h" 8 #include "device/bluetooth/bluetooth_adapter.h"
9 #include "device/bluetooth/bluetooth_adapter_chromeos.h" 9 #include "device/bluetooth/bluetooth_adapter_bluez.h"
10 #include "device/bluetooth/bluetooth_adapter_factory.h" 10 #include "device/bluetooth/bluetooth_adapter_factory.h"
11 #include "device/bluetooth/bluetooth_device.h" 11 #include "device/bluetooth/bluetooth_device.h"
12 #include "device/bluetooth/bluetooth_device_chromeos.h" 12 #include "device/bluetooth/bluetooth_device_bluez.h"
13 #include "device/bluetooth/bluetooth_socket.h" 13 #include "device/bluetooth/bluetooth_socket.h"
14 #include "device/bluetooth/bluetooth_socket_chromeos.h" 14 #include "device/bluetooth/bluetooth_socket_bluez.h"
15 #include "device/bluetooth/bluetooth_socket_thread.h" 15 #include "device/bluetooth/bluetooth_socket_thread.h"
16 #include "device/bluetooth/bluetooth_uuid.h" 16 #include "device/bluetooth/bluetooth_uuid.h"
17 #include "device/bluetooth/dbus/bluez_dbus_manager.h" 17 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
18 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" 18 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
19 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h" 19 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h"
20 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" 20 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h"
21 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" 21 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h"
22 #include "device/bluetooth/dbus/fake_bluetooth_input_client.h" 22 #include "device/bluetooth/dbus/fake_bluetooth_input_client.h"
23 #include "device/bluetooth/dbus/fake_bluetooth_profile_manager_client.h" 23 #include "device/bluetooth/dbus/fake_bluetooth_profile_manager_client.h"
24 #include "device/bluetooth/dbus/fake_bluetooth_profile_service_provider.h" 24 #include "device/bluetooth/dbus/fake_bluetooth_profile_service_provider.h"
25 #include "net/base/io_buffer.h" 25 #include "net/base/io_buffer.h"
26 #include "net/base/net_errors.h" 26 #include "net/base/net_errors.h"
27 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
28 28
29 using device::BluetoothAdapter; 29 using device::BluetoothAdapter;
30 using device::BluetoothDevice; 30 using device::BluetoothDevice;
31 using device::BluetoothSocket; 31 using device::BluetoothSocket;
32 using device::BluetoothSocketThread; 32 using device::BluetoothSocketThread;
33 using device::BluetoothUUID; 33 using device::BluetoothUUID;
34 34
35 namespace { 35 namespace {
36 36
37 void DoNothingDBusErrorCallback(const std::string& error_name, 37 void DoNothingDBusErrorCallback(const std::string& error_name,
38 const std::string& error_message) {} 38 const std::string& error_message) {}
39 39
40 } // namespace 40 } // namespace
41 41
42 namespace chromeos { 42 namespace bluez {
43 43
44 class BluetoothSocketChromeOSTest : public testing::Test { 44 class BluetoothSocketBlueZTest : public testing::Test {
45 public: 45 public:
46 BluetoothSocketChromeOSTest() 46 BluetoothSocketBlueZTest()
47 : success_callback_count_(0), 47 : success_callback_count_(0),
48 error_callback_count_(0), 48 error_callback_count_(0),
49 last_bytes_sent_(0), 49 last_bytes_sent_(0),
50 last_bytes_received_(0), 50 last_bytes_received_(0),
51 last_reason_(BluetoothSocket::kSystemError) {} 51 last_reason_(BluetoothSocket::kSystemError) {}
52 52
53 void SetUp() override { 53 void SetUp() override {
54 scoped_ptr<bluez::BluezDBusManagerSetter> dbus_setter = 54 scoped_ptr<bluez::BluezDBusManagerSetter> dbus_setter =
55 bluez::BluezDBusManager::GetSetterForTesting(); 55 bluez::BluezDBusManager::GetSetterForTesting();
56 56
(...skipping 12 matching lines...) Expand all
69 dbus_setter->SetBluetoothInputClient( 69 dbus_setter->SetBluetoothInputClient(
70 scoped_ptr<bluez::BluetoothInputClient>( 70 scoped_ptr<bluez::BluetoothInputClient>(
71 new bluez::FakeBluetoothInputClient)); 71 new bluez::FakeBluetoothInputClient));
72 dbus_setter->SetBluetoothProfileManagerClient( 72 dbus_setter->SetBluetoothProfileManagerClient(
73 scoped_ptr<bluez::BluetoothProfileManagerClient>( 73 scoped_ptr<bluez::BluetoothProfileManagerClient>(
74 new bluez::FakeBluetoothProfileManagerClient)); 74 new bluez::FakeBluetoothProfileManagerClient));
75 75
76 BluetoothSocketThread::Get(); 76 BluetoothSocketThread::Get();
77 77
78 // Grab a pointer to the adapter. 78 // Grab a pointer to the adapter.
79 device::BluetoothAdapterFactory::GetAdapter( 79 device::BluetoothAdapterFactory::GetAdapter(base::Bind(
80 base::Bind(&BluetoothSocketChromeOSTest::AdapterCallback, 80 &BluetoothSocketBlueZTest::AdapterCallback, base::Unretained(this)));
81 base::Unretained(this)));
82 ASSERT_TRUE(adapter_.get() != nullptr); 81 ASSERT_TRUE(adapter_.get() != nullptr);
83 ASSERT_TRUE(adapter_->IsInitialized()); 82 ASSERT_TRUE(adapter_->IsInitialized());
84 ASSERT_TRUE(adapter_->IsPresent()); 83 ASSERT_TRUE(adapter_->IsPresent());
85 84
86 // Turn on the adapter. 85 // Turn on the adapter.
87 adapter_->SetPowered( 86 adapter_->SetPowered(true, base::Bind(&base::DoNothing),
88 true, 87 base::Bind(&base::DoNothing));
89 base::Bind(&base::DoNothing),
90 base::Bind(&base::DoNothing));
91 ASSERT_TRUE(adapter_->IsPowered()); 88 ASSERT_TRUE(adapter_->IsPowered());
92 } 89 }
93 90
94 void TearDown() override { 91 void TearDown() override {
95 adapter_ = nullptr; 92 adapter_ = nullptr;
96 BluetoothSocketThread::CleanupForTesting(); 93 BluetoothSocketThread::CleanupForTesting();
97 bluez::BluezDBusManager::Shutdown(); 94 bluez::BluezDBusManager::Shutdown();
98 } 95 }
99 96
100 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { 97 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 153
157 void AcceptSuccessCallback(const BluetoothDevice* device, 154 void AcceptSuccessCallback(const BluetoothDevice* device,
158 scoped_refptr<BluetoothSocket> socket) { 155 scoped_refptr<BluetoothSocket> socket) {
159 ++success_callback_count_; 156 ++success_callback_count_;
160 last_device_ = device; 157 last_device_ = device;
161 last_socket_ = socket; 158 last_socket_ = socket;
162 159
163 message_loop_.QuitWhenIdle(); 160 message_loop_.QuitWhenIdle();
164 } 161 }
165 162
166 void ImmediateSuccessCallback() { 163 void ImmediateSuccessCallback() { ++success_callback_count_; }
167 ++success_callback_count_;
168 }
169 164
170 protected: 165 protected:
171 base::MessageLoop message_loop_; 166 base::MessageLoop message_loop_;
172 167
173 scoped_refptr<BluetoothAdapter> adapter_; 168 scoped_refptr<BluetoothAdapter> adapter_;
174 169
175 unsigned int success_callback_count_; 170 unsigned int success_callback_count_;
176 unsigned int error_callback_count_; 171 unsigned int error_callback_count_;
177 172
178 std::string last_message_; 173 std::string last_message_;
179 scoped_refptr<BluetoothSocket> last_socket_; 174 scoped_refptr<BluetoothSocket> last_socket_;
180 int last_bytes_sent_; 175 int last_bytes_sent_;
181 int last_bytes_received_; 176 int last_bytes_received_;
182 scoped_refptr<net::IOBuffer> last_io_buffer_; 177 scoped_refptr<net::IOBuffer> last_io_buffer_;
183 BluetoothSocket::ErrorReason last_reason_; 178 BluetoothSocket::ErrorReason last_reason_;
184 const BluetoothDevice* last_device_; 179 const BluetoothDevice* last_device_;
185 }; 180 };
186 181
187 TEST_F(BluetoothSocketChromeOSTest, Connect) { 182 TEST_F(BluetoothSocketBlueZTest, Connect) {
188 BluetoothDevice* device = adapter_->GetDevice( 183 BluetoothDevice* device = adapter_->GetDevice(
189 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); 184 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
190 ASSERT_TRUE(device != nullptr); 185 ASSERT_TRUE(device != nullptr);
191 186
192 device->ConnectToService( 187 device->ConnectToService(
193 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), 188 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
194 base::Bind(&BluetoothSocketChromeOSTest::ConnectToServiceSuccessCallback, 189 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback,
195 base::Unretained(this)), 190 base::Unretained(this)),
196 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 191 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
197 base::Unretained(this))); 192 base::Unretained(this)));
198 message_loop_.Run(); 193 message_loop_.Run();
199 194
200 EXPECT_EQ(1U, success_callback_count_); 195 EXPECT_EQ(1U, success_callback_count_);
201 EXPECT_EQ(0U, error_callback_count_); 196 EXPECT_EQ(0U, error_callback_count_);
202 EXPECT_TRUE(last_socket_.get() != nullptr); 197 EXPECT_TRUE(last_socket_.get() != nullptr);
203 198
204 // Take ownership of the socket for the remainder of the test. 199 // Take ownership of the socket for the remainder of the test.
205 scoped_refptr<BluetoothSocket> socket = last_socket_; 200 scoped_refptr<BluetoothSocket> socket = last_socket_;
206 last_socket_ = nullptr; 201 last_socket_ = nullptr;
207 success_callback_count_ = 0; 202 success_callback_count_ = 0;
208 error_callback_count_ = 0; 203 error_callback_count_ = 0;
209 204
210 // Send data to the socket, expect all of the data to be sent. 205 // Send data to the socket, expect all of the data to be sent.
211 scoped_refptr<net::StringIOBuffer> write_buffer( 206 scoped_refptr<net::StringIOBuffer> write_buffer(
212 new net::StringIOBuffer("test")); 207 new net::StringIOBuffer("test"));
213 208
214 socket->Send(write_buffer.get(), write_buffer->size(), 209 socket->Send(write_buffer.get(), write_buffer->size(),
215 base::Bind(&BluetoothSocketChromeOSTest::SendSuccessCallback, 210 base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback,
216 base::Unretained(this)), 211 base::Unretained(this)),
217 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 212 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
218 base::Unretained(this))); 213 base::Unretained(this)));
219 message_loop_.Run(); 214 message_loop_.Run();
220 215
221 EXPECT_EQ(1U, success_callback_count_); 216 EXPECT_EQ(1U, success_callback_count_);
222 EXPECT_EQ(0U, error_callback_count_); 217 EXPECT_EQ(0U, error_callback_count_);
223 EXPECT_EQ(last_bytes_sent_, write_buffer->size()); 218 EXPECT_EQ(last_bytes_sent_, write_buffer->size());
224 219
225 success_callback_count_ = 0; 220 success_callback_count_ = 0;
226 error_callback_count_ = 0; 221 error_callback_count_ = 0;
227 222
228 // Receive data from the socket, and fetch the buffer from the callback; since 223 // Receive data from the socket, and fetch the buffer from the callback; since
229 // the fake is an echo server, we expect to receive what we wrote. 224 // the fake is an echo server, we expect to receive what we wrote.
230 socket->Receive( 225 socket->Receive(4096,
231 4096, 226 base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback,
232 base::Bind(&BluetoothSocketChromeOSTest::ReceiveSuccessCallback, 227 base::Unretained(this)),
233 base::Unretained(this)), 228 base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback,
234 base::Bind(&BluetoothSocketChromeOSTest::ReceiveErrorCallback, 229 base::Unretained(this)));
235 base::Unretained(this)));
236 message_loop_.Run(); 230 message_loop_.Run();
237 231
238 EXPECT_EQ(1U, success_callback_count_); 232 EXPECT_EQ(1U, success_callback_count_);
239 EXPECT_EQ(0U, error_callback_count_); 233 EXPECT_EQ(0U, error_callback_count_);
240 EXPECT_EQ(4, last_bytes_received_); 234 EXPECT_EQ(4, last_bytes_received_);
241 EXPECT_TRUE(last_io_buffer_.get() != nullptr); 235 EXPECT_TRUE(last_io_buffer_.get() != nullptr);
242 236
243 // Take ownership of the received buffer. 237 // Take ownership of the received buffer.
244 scoped_refptr<net::IOBuffer> read_buffer = last_io_buffer_; 238 scoped_refptr<net::IOBuffer> read_buffer = last_io_buffer_;
245 last_io_buffer_ = nullptr; 239 last_io_buffer_ = nullptr;
246 success_callback_count_ = 0; 240 success_callback_count_ = 0;
247 error_callback_count_ = 0; 241 error_callback_count_ = 0;
248 242
249 std::string data = std::string(read_buffer->data(), last_bytes_received_); 243 std::string data = std::string(read_buffer->data(), last_bytes_received_);
250 EXPECT_EQ("test", data); 244 EXPECT_EQ("test", data);
251 245
252 read_buffer = nullptr; 246 read_buffer = nullptr;
253 247
254 // Receive data again; the socket will have been closed, this should cause a 248 // Receive data again; the socket will have been closed, this should cause a
255 // disconnected error to be returned via the error callback. 249 // disconnected error to be returned via the error callback.
256 socket->Receive( 250 socket->Receive(4096,
257 4096, 251 base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback,
258 base::Bind(&BluetoothSocketChromeOSTest::ReceiveSuccessCallback, 252 base::Unretained(this)),
259 base::Unretained(this)), 253 base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback,
260 base::Bind(&BluetoothSocketChromeOSTest::ReceiveErrorCallback, 254 base::Unretained(this)));
261 base::Unretained(this)));
262 message_loop_.Run(); 255 message_loop_.Run();
263 256
264 EXPECT_EQ(0U, success_callback_count_); 257 EXPECT_EQ(0U, success_callback_count_);
265 EXPECT_EQ(1U, error_callback_count_); 258 EXPECT_EQ(1U, error_callback_count_);
266 EXPECT_EQ(BluetoothSocket::kDisconnected, last_reason_); 259 EXPECT_EQ(BluetoothSocket::kDisconnected, last_reason_);
267 EXPECT_EQ(net::ErrorToString(net::OK), last_message_); 260 EXPECT_EQ(net::ErrorToString(net::OK), last_message_);
268 261
269 success_callback_count_ = 0; 262 success_callback_count_ = 0;
270 error_callback_count_ = 0; 263 error_callback_count_ = 0;
271 264
272 // Send data again; since the socket is closed we should get a system error 265 // Send data again; since the socket is closed we should get a system error
273 // equivalent to the connection reset error. 266 // equivalent to the connection reset error.
274 write_buffer = new net::StringIOBuffer("second test"); 267 write_buffer = new net::StringIOBuffer("second test");
275 268
276 socket->Send(write_buffer.get(), write_buffer->size(), 269 socket->Send(write_buffer.get(), write_buffer->size(),
277 base::Bind(&BluetoothSocketChromeOSTest::SendSuccessCallback, 270 base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback,
278 base::Unretained(this)), 271 base::Unretained(this)),
279 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 272 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
280 base::Unretained(this))); 273 base::Unretained(this)));
281 message_loop_.Run(); 274 message_loop_.Run();
282 275
283 EXPECT_EQ(0U, success_callback_count_); 276 EXPECT_EQ(0U, success_callback_count_);
284 EXPECT_EQ(1U, error_callback_count_); 277 EXPECT_EQ(1U, error_callback_count_);
285 EXPECT_EQ(net::ErrorToString(net::ERR_CONNECTION_RESET), last_message_); 278 EXPECT_EQ(net::ErrorToString(net::ERR_CONNECTION_RESET), last_message_);
286 279
287 success_callback_count_ = 0; 280 success_callback_count_ = 0;
288 error_callback_count_ = 0; 281 error_callback_count_ = 0;
289 282
290 // Close our end of the socket. 283 // Close our end of the socket.
291 socket->Disconnect(base::Bind(&BluetoothSocketChromeOSTest::SuccessCallback, 284 socket->Disconnect(base::Bind(&BluetoothSocketBlueZTest::SuccessCallback,
292 base::Unretained(this))); 285 base::Unretained(this)));
293 286
294 message_loop_.Run(); 287 message_loop_.Run();
295 EXPECT_EQ(1U, success_callback_count_); 288 EXPECT_EQ(1U, success_callback_count_);
296 } 289 }
297 290
298 TEST_F(BluetoothSocketChromeOSTest, Listen) { 291 TEST_F(BluetoothSocketBlueZTest, Listen) {
299 adapter_->CreateRfcommService( 292 adapter_->CreateRfcommService(
300 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), 293 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
301 BluetoothAdapter::ServiceOptions(), 294 BluetoothAdapter::ServiceOptions(),
302 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback, 295 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback,
303 base::Unretained(this)), 296 base::Unretained(this)),
304 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 297 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
305 base::Unretained(this))); 298 base::Unretained(this)));
306 299
307 message_loop_.Run(); 300 message_loop_.Run();
308 301
309 EXPECT_EQ(1U, success_callback_count_); 302 EXPECT_EQ(1U, success_callback_count_);
310 EXPECT_EQ(0U, error_callback_count_); 303 EXPECT_EQ(0U, error_callback_count_);
311 EXPECT_TRUE(last_socket_.get() != nullptr); 304 EXPECT_TRUE(last_socket_.get() != nullptr);
312 305
313 // Take ownership of the socket for the remainder of the test. 306 // Take ownership of the socket for the remainder of the test.
314 scoped_refptr<BluetoothSocket> server_socket = last_socket_; 307 scoped_refptr<BluetoothSocket> server_socket = last_socket_;
315 last_socket_ = nullptr; 308 last_socket_ = nullptr;
316 success_callback_count_ = 0; 309 success_callback_count_ = 0;
317 error_callback_count_ = 0; 310 error_callback_count_ = 0;
318 311
319 // Simulate an incoming connection by just calling the ConnectProfile method 312 // Simulate an incoming connection by just calling the ConnectProfile method
320 // of the underlying fake device client (from the BlueZ point of view, 313 // of the underlying fake device client (from the BlueZ point of view,
321 // outgoing and incoming look the same). 314 // outgoing and incoming look the same).
322 // 315 //
323 // This is done before the Accept() call to simulate a pending call at the 316 // This is done before the Accept() call to simulate a pending call at the
324 // point that Accept() is called. 317 // point that Accept() is called.
325 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client = 318 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client =
326 static_cast<bluez::FakeBluetoothDeviceClient*>( 319 static_cast<bluez::FakeBluetoothDeviceClient*>(
327 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()); 320 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient());
328 BluetoothDevice* device = adapter_->GetDevice( 321 BluetoothDevice* device = adapter_->GetDevice(
329 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); 322 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
330 ASSERT_TRUE(device != nullptr); 323 ASSERT_TRUE(device != nullptr);
331 fake_bluetooth_device_client->ConnectProfile( 324 fake_bluetooth_device_client->ConnectProfile(
332 static_cast<BluetoothDeviceChromeOS*>(device)->object_path(), 325 static_cast<BluetoothDeviceBlueZ*>(device)->object_path(),
333 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, 326 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid,
334 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); 327 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback));
335 328
336 message_loop_.RunUntilIdle(); 329 message_loop_.RunUntilIdle();
337 330
338 server_socket->Accept( 331 server_socket->Accept(
339 base::Bind(&BluetoothSocketChromeOSTest::AcceptSuccessCallback, 332 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback,
340 base::Unretained(this)), 333 base::Unretained(this)),
341 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 334 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
342 base::Unretained(this))); 335 base::Unretained(this)));
343 336
344 message_loop_.Run(); 337 message_loop_.Run();
345 338
346 EXPECT_EQ(1U, success_callback_count_); 339 EXPECT_EQ(1U, success_callback_count_);
347 EXPECT_EQ(0U, error_callback_count_); 340 EXPECT_EQ(0U, error_callback_count_);
348 EXPECT_TRUE(last_socket_.get() != nullptr); 341 EXPECT_TRUE(last_socket_.get() != nullptr);
349 342
350 // Take ownership of the client socket for the remainder of the test. 343 // Take ownership of the client socket for the remainder of the test.
351 scoped_refptr<BluetoothSocket> client_socket = last_socket_; 344 scoped_refptr<BluetoothSocket> client_socket = last_socket_;
352 last_socket_ = nullptr; 345 last_socket_ = nullptr;
353 success_callback_count_ = 0; 346 success_callback_count_ = 0;
354 error_callback_count_ = 0; 347 error_callback_count_ = 0;
355 348
356 // Close our end of the client socket. 349 // Close our end of the client socket.
357 client_socket->Disconnect( 350 client_socket->Disconnect(base::Bind(
358 base::Bind(&BluetoothSocketChromeOSTest::SuccessCallback, 351 &BluetoothSocketBlueZTest::SuccessCallback, base::Unretained(this)));
359 base::Unretained(this)));
360 352
361 message_loop_.Run(); 353 message_loop_.Run();
362 354
363 EXPECT_EQ(1U, success_callback_count_); 355 EXPECT_EQ(1U, success_callback_count_);
364 client_socket = nullptr; 356 client_socket = nullptr;
365 success_callback_count_ = 0; 357 success_callback_count_ = 0;
366 error_callback_count_ = 0; 358 error_callback_count_ = 0;
367 359
368 // Run a second connection test, this time calling Accept() before the 360 // Run a second connection test, this time calling Accept() before the
369 // incoming connection comes in. 361 // incoming connection comes in.
370 server_socket->Accept( 362 server_socket->Accept(
371 base::Bind(&BluetoothSocketChromeOSTest::AcceptSuccessCallback, 363 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback,
372 base::Unretained(this)), 364 base::Unretained(this)),
373 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 365 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
374 base::Unretained(this))); 366 base::Unretained(this)));
375 367
376 message_loop_.RunUntilIdle(); 368 message_loop_.RunUntilIdle();
377 369
378 fake_bluetooth_device_client->ConnectProfile( 370 fake_bluetooth_device_client->ConnectProfile(
379 static_cast<BluetoothDeviceChromeOS*>(device)->object_path(), 371 static_cast<BluetoothDeviceBlueZ*>(device)->object_path(),
380 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, 372 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid,
381 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); 373 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback));
382 374
383 message_loop_.Run(); 375 message_loop_.Run();
384 376
385 EXPECT_EQ(1U, success_callback_count_); 377 EXPECT_EQ(1U, success_callback_count_);
386 EXPECT_EQ(0U, error_callback_count_); 378 EXPECT_EQ(0U, error_callback_count_);
387 EXPECT_TRUE(last_socket_.get() != nullptr); 379 EXPECT_TRUE(last_socket_.get() != nullptr);
388 380
389 // Take ownership of the client socket for the remainder of the test. 381 // Take ownership of the client socket for the remainder of the test.
390 client_socket = last_socket_; 382 client_socket = last_socket_;
391 last_socket_ = nullptr; 383 last_socket_ = nullptr;
392 success_callback_count_ = 0; 384 success_callback_count_ = 0;
393 error_callback_count_ = 0; 385 error_callback_count_ = 0;
394 386
395 // Close our end of the client socket. 387 // Close our end of the client socket.
396 client_socket->Disconnect( 388 client_socket->Disconnect(base::Bind(
397 base::Bind(&BluetoothSocketChromeOSTest::SuccessCallback, 389 &BluetoothSocketBlueZTest::SuccessCallback, base::Unretained(this)));
398 base::Unretained(this)));
399 390
400 message_loop_.Run(); 391 message_loop_.Run();
401 392
402 EXPECT_EQ(1U, success_callback_count_); 393 EXPECT_EQ(1U, success_callback_count_);
403 client_socket = nullptr; 394 client_socket = nullptr;
404 success_callback_count_ = 0; 395 success_callback_count_ = 0;
405 error_callback_count_ = 0; 396 error_callback_count_ = 0;
406 397
407 // Now close the server socket. 398 // Now close the server socket.
408 server_socket->Disconnect( 399 server_socket->Disconnect(
409 base::Bind(&BluetoothSocketChromeOSTest::ImmediateSuccessCallback, 400 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback,
410 base::Unretained(this))); 401 base::Unretained(this)));
411 402
412 message_loop_.RunUntilIdle(); 403 message_loop_.RunUntilIdle();
413 404
414 EXPECT_EQ(1U, success_callback_count_); 405 EXPECT_EQ(1U, success_callback_count_);
415 } 406 }
416 407
417 TEST_F(BluetoothSocketChromeOSTest, ListenBeforeAdapterStart) { 408 TEST_F(BluetoothSocketBlueZTest, ListenBeforeAdapterStart) {
418 // Start off with an invisible adapter, register the profile, then make 409 // Start off with an invisible adapter, register the profile, then make
419 // the adapter visible. 410 // the adapter visible.
420 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = 411 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client =
421 static_cast<bluez::FakeBluetoothAdapterClient*>( 412 static_cast<bluez::FakeBluetoothAdapterClient*>(
422 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()); 413 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient());
423 fake_bluetooth_adapter_client->SetVisible(false); 414 fake_bluetooth_adapter_client->SetVisible(false);
424 415
425 adapter_->CreateRfcommService( 416 adapter_->CreateRfcommService(
426 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), 417 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
427 BluetoothAdapter::ServiceOptions(), 418 BluetoothAdapter::ServiceOptions(),
428 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback, 419 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback,
429 base::Unretained(this)), 420 base::Unretained(this)),
430 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 421 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
431 base::Unretained(this))); 422 base::Unretained(this)));
432 message_loop_.Run(); 423 message_loop_.Run();
433 424
434 EXPECT_EQ(1U, success_callback_count_); 425 EXPECT_EQ(1U, success_callback_count_);
435 EXPECT_EQ(0U, error_callback_count_); 426 EXPECT_EQ(0U, error_callback_count_);
436 EXPECT_TRUE(last_socket_.get() != nullptr); 427 EXPECT_TRUE(last_socket_.get() != nullptr);
437 428
438 // Take ownership of the socket for the remainder of the test. 429 // Take ownership of the socket for the remainder of the test.
439 scoped_refptr<BluetoothSocket> socket = last_socket_; 430 scoped_refptr<BluetoothSocket> socket = last_socket_;
440 last_socket_ = nullptr; 431 last_socket_ = nullptr;
(...skipping 16 matching lines...) Expand all
457 448
458 message_loop_.RunUntilIdle(); 449 message_loop_.RunUntilIdle();
459 450
460 profile_service_provider = 451 profile_service_provider =
461 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( 452 fake_bluetooth_profile_manager_client->GetProfileServiceProvider(
462 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); 453 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid);
463 EXPECT_TRUE(profile_service_provider != nullptr); 454 EXPECT_TRUE(profile_service_provider != nullptr);
464 455
465 // Cleanup the socket. 456 // Cleanup the socket.
466 socket->Disconnect( 457 socket->Disconnect(
467 base::Bind(&BluetoothSocketChromeOSTest::ImmediateSuccessCallback, 458 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback,
468 base::Unretained(this))); 459 base::Unretained(this)));
469 460
470 message_loop_.RunUntilIdle(); 461 message_loop_.RunUntilIdle();
471 462
472 EXPECT_EQ(1U, success_callback_count_); 463 EXPECT_EQ(1U, success_callback_count_);
473 } 464 }
474 465
475 TEST_F(BluetoothSocketChromeOSTest, ListenAcrossAdapterRestart) { 466 TEST_F(BluetoothSocketBlueZTest, ListenAcrossAdapterRestart) {
476 // The fake adapter starts off visible by default. 467 // The fake adapter starts off visible by default.
477 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = 468 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client =
478 static_cast<bluez::FakeBluetoothAdapterClient*>( 469 static_cast<bluez::FakeBluetoothAdapterClient*>(
479 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()); 470 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient());
480 471
481 adapter_->CreateRfcommService( 472 adapter_->CreateRfcommService(
482 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), 473 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
483 BluetoothAdapter::ServiceOptions(), 474 BluetoothAdapter::ServiceOptions(),
484 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback, 475 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback,
485 base::Unretained(this)), 476 base::Unretained(this)),
486 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 477 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
487 base::Unretained(this))); 478 base::Unretained(this)));
488 message_loop_.Run(); 479 message_loop_.Run();
489 480
490 EXPECT_EQ(1U, success_callback_count_); 481 EXPECT_EQ(1U, success_callback_count_);
491 EXPECT_EQ(0U, error_callback_count_); 482 EXPECT_EQ(0U, error_callback_count_);
492 EXPECT_TRUE(last_socket_.get() != nullptr); 483 EXPECT_TRUE(last_socket_.get() != nullptr);
493 484
494 // Take ownership of the socket for the remainder of the test. 485 // Take ownership of the socket for the remainder of the test.
495 scoped_refptr<BluetoothSocket> socket = last_socket_; 486 scoped_refptr<BluetoothSocket> socket = last_socket_;
496 last_socket_ = nullptr; 487 last_socket_ = nullptr;
(...skipping 22 matching lines...) Expand all
519 510
520 message_loop_.RunUntilIdle(); 511 message_loop_.RunUntilIdle();
521 512
522 profile_service_provider = 513 profile_service_provider =
523 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( 514 fake_bluetooth_profile_manager_client->GetProfileServiceProvider(
524 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); 515 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid);
525 EXPECT_TRUE(profile_service_provider != nullptr); 516 EXPECT_TRUE(profile_service_provider != nullptr);
526 517
527 // Cleanup the socket. 518 // Cleanup the socket.
528 socket->Disconnect( 519 socket->Disconnect(
529 base::Bind(&BluetoothSocketChromeOSTest::ImmediateSuccessCallback, 520 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback,
530 base::Unretained(this))); 521 base::Unretained(this)));
531 522
532 message_loop_.RunUntilIdle(); 523 message_loop_.RunUntilIdle();
533 524
534 EXPECT_EQ(1U, success_callback_count_); 525 EXPECT_EQ(1U, success_callback_count_);
535 } 526 }
536 527
537 TEST_F(BluetoothSocketChromeOSTest, PairedConnectFails) { 528 TEST_F(BluetoothSocketBlueZTest, PairedConnectFails) {
538 BluetoothDevice* device = adapter_->GetDevice( 529 BluetoothDevice* device = adapter_->GetDevice(
539 bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress); 530 bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress);
540 ASSERT_TRUE(device != nullptr); 531 ASSERT_TRUE(device != nullptr);
541 532
542 device->ConnectToService( 533 device->ConnectToService(
543 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), 534 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
544 base::Bind(&BluetoothSocketChromeOSTest::ConnectToServiceSuccessCallback, 535 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback,
545 base::Unretained(this)), 536 base::Unretained(this)),
546 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 537 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
547 base::Unretained(this))); 538 base::Unretained(this)));
548 message_loop_.Run(); 539 message_loop_.Run();
549 540
550 EXPECT_EQ(0U, success_callback_count_); 541 EXPECT_EQ(0U, success_callback_count_);
551 EXPECT_EQ(1U, error_callback_count_); 542 EXPECT_EQ(1U, error_callback_count_);
552 EXPECT_TRUE(last_socket_.get() == nullptr); 543 EXPECT_TRUE(last_socket_.get() == nullptr);
553 544
554 device->ConnectToService( 545 device->ConnectToService(
555 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), 546 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
556 base::Bind(&BluetoothSocketChromeOSTest::ConnectToServiceSuccessCallback, 547 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback,
557 base::Unretained(this)), 548 base::Unretained(this)),
558 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 549 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
559 base::Unretained(this))); 550 base::Unretained(this)));
560 message_loop_.Run(); 551 message_loop_.Run();
561 552
562 EXPECT_EQ(0U, success_callback_count_); 553 EXPECT_EQ(0U, success_callback_count_);
563 EXPECT_EQ(2U, error_callback_count_); 554 EXPECT_EQ(2U, error_callback_count_);
564 EXPECT_TRUE(last_socket_.get() == nullptr); 555 EXPECT_TRUE(last_socket_.get() == nullptr);
565 } 556 }
566 557
567 TEST_F(BluetoothSocketChromeOSTest, SocketListenTwice) { 558 TEST_F(BluetoothSocketBlueZTest, SocketListenTwice) {
568 adapter_->CreateRfcommService( 559 adapter_->CreateRfcommService(
569 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), 560 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
570 BluetoothAdapter::ServiceOptions(), 561 BluetoothAdapter::ServiceOptions(),
571 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback, 562 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback,
572 base::Unretained(this)), 563 base::Unretained(this)),
573 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 564 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
574 base::Unretained(this))); 565 base::Unretained(this)));
575 566
576 message_loop_.Run(); 567 message_loop_.Run();
577 568
578 EXPECT_EQ(1U, success_callback_count_); 569 EXPECT_EQ(1U, success_callback_count_);
579 EXPECT_EQ(0U, error_callback_count_); 570 EXPECT_EQ(0U, error_callback_count_);
580 EXPECT_TRUE(last_socket_.get() != nullptr); 571 EXPECT_TRUE(last_socket_.get() != nullptr);
581 572
582 // Take control of this socket. 573 // Take control of this socket.
583 scoped_refptr<BluetoothSocket> server_socket; 574 scoped_refptr<BluetoothSocket> server_socket;
584 server_socket.swap(last_socket_); 575 server_socket.swap(last_socket_);
585 576
586 server_socket->Accept( 577 server_socket->Accept(
587 base::Bind(&BluetoothSocketChromeOSTest::AcceptSuccessCallback, 578 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback,
588 base::Unretained(this)), 579 base::Unretained(this)),
589 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 580 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
590 base::Unretained(this))); 581 base::Unretained(this)));
591 582
592 server_socket->Close(); 583 server_socket->Close();
593 584
594 server_socket = nullptr; 585 server_socket = nullptr;
595 586
596 message_loop_.RunUntilIdle(); 587 message_loop_.RunUntilIdle();
597 588
598 EXPECT_EQ(1U, success_callback_count_); 589 EXPECT_EQ(1U, success_callback_count_);
599 EXPECT_EQ(1U, error_callback_count_); 590 EXPECT_EQ(1U, error_callback_count_);
600 591
601 adapter_->CreateRfcommService( 592 adapter_->CreateRfcommService(
602 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), 593 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid),
603 BluetoothAdapter::ServiceOptions(), 594 BluetoothAdapter::ServiceOptions(),
604 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback, 595 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback,
605 base::Unretained(this)), 596 base::Unretained(this)),
606 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 597 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
607 base::Unretained(this))); 598 base::Unretained(this)));
608 599
609 message_loop_.Run(); 600 message_loop_.Run();
610 601
611 EXPECT_EQ(2U, success_callback_count_); 602 EXPECT_EQ(2U, success_callback_count_);
612 EXPECT_EQ(1U, error_callback_count_); 603 EXPECT_EQ(1U, error_callback_count_);
613 EXPECT_TRUE(last_socket_.get() != nullptr); 604 EXPECT_TRUE(last_socket_.get() != nullptr);
614 605
615 // Take control of this socket. 606 // Take control of this socket.
616 server_socket.swap(last_socket_); 607 server_socket.swap(last_socket_);
617 608
618 server_socket->Accept( 609 server_socket->Accept(
619 base::Bind(&BluetoothSocketChromeOSTest::AcceptSuccessCallback, 610 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback,
620 base::Unretained(this)), 611 base::Unretained(this)),
621 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback, 612 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback,
622 base::Unretained(this))); 613 base::Unretained(this)));
623 614
624 server_socket->Close(); 615 server_socket->Close();
625 616
626 server_socket = nullptr; 617 server_socket = nullptr;
627 618
628 message_loop_.RunUntilIdle(); 619 message_loop_.RunUntilIdle();
629 620
630 EXPECT_EQ(2U, success_callback_count_); 621 EXPECT_EQ(2U, success_callback_count_);
631 EXPECT_EQ(2U, error_callback_count_); 622 EXPECT_EQ(2U, error_callback_count_);
632 } 623 }
633 624
634 } // namespace chromeos 625 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698