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

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

Issue 276573004: Bluetooth: Implement new socket API for Chrome OS (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix DCHECK nits Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/bind.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/message_loop/message_loop.h"
8 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
9 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
10 #include "chromeos/dbus/fake_bluetooth_device_client.h"
11 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
12 #include "chromeos/dbus/fake_bluetooth_input_client.h"
13 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h"
14 #include "chromeos/dbus/fake_dbus_thread_manager.h"
15 #include "device/bluetooth/bluetooth_adapter.h"
16 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
17 #include "device/bluetooth/bluetooth_adapter_factory.h"
18 #include "device/bluetooth/bluetooth_device.h"
19 #include "device/bluetooth/bluetooth_device_chromeos.h"
20 #include "device/bluetooth/bluetooth_socket.h"
21 #include "device/bluetooth/bluetooth_socket_chromeos.h"
22 #include "device/bluetooth/bluetooth_socket_thread.h"
23 #include "device/bluetooth/bluetooth_uuid.h"
24 #include "net/base/io_buffer.h"
25 #include "net/base/net_errors.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27
28 using device::BluetoothAdapter;
29 using device::BluetoothDevice;
30 using device::BluetoothSocket;
31 using device::BluetoothSocketThread;
32 using device::BluetoothUUID;
33
34 namespace {
35
36 void DoNothingDBusErrorCallback(const std::string& error_name,
37 const std::string& error_message) {}
38
39 } // namespace
40
41 namespace chromeos {
42
43 class BluetoothSocketChromeOSTest : public testing::Test {
44 public:
45 BluetoothSocketChromeOSTest()
46 : success_callback_count_(0),
47 error_callback_count_(0),
48 last_bytes_sent_(0),
49 last_bytes_received_(0),
50 last_reason_(BluetoothSocket::kSystemError) {}
51
52 virtual void SetUp() OVERRIDE {
53 scoped_ptr<FakeDBusThreadManager> fake_dbus_thread_manager(
54 new FakeDBusThreadManager);
55
56 fake_dbus_thread_manager->SetBluetoothAdapterClient(
57 scoped_ptr<BluetoothAdapterClient>(new FakeBluetoothAdapterClient));
58 fake_dbus_thread_manager->SetBluetoothAgentManagerClient(
59 scoped_ptr<BluetoothAgentManagerClient>(
60 new FakeBluetoothAgentManagerClient));
61 fake_dbus_thread_manager->SetBluetoothDeviceClient(
62 scoped_ptr<BluetoothDeviceClient>(new FakeBluetoothDeviceClient));
63 fake_dbus_thread_manager->SetBluetoothGattServiceClient(
64 scoped_ptr<BluetoothGattServiceClient>(
65 new FakeBluetoothGattServiceClient));
66 fake_dbus_thread_manager->SetBluetoothInputClient(
67 scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
68 fake_dbus_thread_manager->SetBluetoothProfileManagerClient(
69 scoped_ptr<BluetoothProfileManagerClient>(
70 new FakeBluetoothProfileManagerClient));
71
72 DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager.release());
73 BluetoothSocketThread::Get();
74
75 // Grab a pointer to the adapter.
76 device::BluetoothAdapterFactory::GetAdapter(
77 base::Bind(&BluetoothSocketChromeOSTest::AdapterCallback,
78 base::Unretained(this)));
79 ASSERT_TRUE(adapter_.get() != NULL);
80 ASSERT_TRUE(adapter_->IsInitialized());
81 ASSERT_TRUE(adapter_->IsPresent());
82
83 // Turn on the adapter.
84 adapter_->SetPowered(
85 true,
86 base::Bind(&base::DoNothing),
87 base::Bind(&base::DoNothing));
88 ASSERT_TRUE(adapter_->IsPowered());
89 }
90
91 virtual void TearDown() OVERRIDE {
92 adapter_ = NULL;
93 BluetoothSocketThread::CleanupForTesting();
94 DBusThreadManager::Shutdown();
95 }
96
97 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
98 adapter_ = adapter;
99 }
100
101 void SuccessCallback() {
102 ++success_callback_count_;
103 message_loop_.Quit();
104 }
105
106 void ErrorCallback(const std::string& message) {
107 ++error_callback_count_;
108 last_message_ = message;
109
110 message_loop_.Quit();
111 }
112
113 void ConnectToServiceSuccessCallback(scoped_refptr<BluetoothSocket> socket) {
114 ++success_callback_count_;
115 last_socket_ = socket;
116
117 message_loop_.Quit();
118 }
119
120 void SendSuccessCallback(int bytes_sent) {
121 ++success_callback_count_;
122 last_bytes_sent_ = bytes_sent;
123
124 message_loop_.Quit();
125 }
126
127 void ReceiveSuccessCallback(int bytes_received,
128 scoped_refptr<net::IOBuffer> io_buffer) {
129 ++success_callback_count_;
130 last_bytes_received_ = bytes_received;
131 last_io_buffer_ = io_buffer;
132
133 message_loop_.Quit();
134 }
135
136 void ReceiveErrorCallback(BluetoothSocket::ErrorReason reason,
137 const std::string& error_message) {
138 ++error_callback_count_;
139 last_reason_ = reason;
140 last_message_ = error_message;
141
142 message_loop_.Quit();
143 }
144
145 void CreateServiceSuccessCallback(scoped_refptr<BluetoothSocket> socket) {
146 ++success_callback_count_;
147 last_socket_ = socket;
148 }
149
150 void AcceptSuccessCallback(const BluetoothDevice* device,
151 scoped_refptr<BluetoothSocket> socket) {
152 ++success_callback_count_;
153 last_device_ = device;
154 last_socket_ = socket;
155
156 message_loop_.Quit();
157 }
158
159 void ImmediateSuccessCallback() {
160 ++success_callback_count_;
161 }
162
163 protected:
164 base::MessageLoop message_loop_;
165
166 scoped_refptr<BluetoothAdapter> adapter_;
167
168 unsigned int success_callback_count_;
169 unsigned int error_callback_count_;
170
171 std::string last_message_;
172 scoped_refptr<BluetoothSocket> last_socket_;
173 int last_bytes_sent_;
174 int last_bytes_received_;
175 scoped_refptr<net::IOBuffer> last_io_buffer_;
176 BluetoothSocket::ErrorReason last_reason_;
177 const BluetoothDevice* last_device_;
178 };
179
180 TEST_F(BluetoothSocketChromeOSTest, Connect) {
181 BluetoothDevice* device = adapter_->GetDevice(
182 FakeBluetoothDeviceClient::kPairedDeviceAddress);
183 ASSERT_TRUE(device != NULL);
184
185 device->ConnectToService(
186 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid),
187 base::Bind(&BluetoothSocketChromeOSTest::ConnectToServiceSuccessCallback,
188 base::Unretained(this)),
189 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback,
190 base::Unretained(this)));
191
192 message_loop_.Run();
193
194 EXPECT_EQ(1U, success_callback_count_);
195 EXPECT_EQ(0U, error_callback_count_);
196 EXPECT_TRUE(last_socket_.get() != NULL);
197
198 // Take ownership of the socket for the remainder of the test.
199 scoped_refptr<BluetoothSocket> socket = last_socket_;
200 last_socket_ = NULL;
201 success_callback_count_ = 0;
202 error_callback_count_ = 0;
203
204 // Send data to the socket, expect all of the data to be sent.
205 scoped_refptr<net::StringIOBuffer> write_buffer(
206 new net::StringIOBuffer("test"));
207
208 socket->Send(write_buffer.get(), write_buffer->size(),
209 base::Bind(&BluetoothSocketChromeOSTest::SendSuccessCallback,
210 base::Unretained(this)),
211 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback,
212 base::Unretained(this)));
213 message_loop_.Run();
214
215 EXPECT_EQ(1U, success_callback_count_);
216 EXPECT_EQ(0U, error_callback_count_);
217 EXPECT_EQ(last_bytes_sent_, write_buffer->size());
218
219 success_callback_count_ = 0;
220 error_callback_count_ = 0;
221
222 // Receive data from the socket, and fetch the buffer from the callback; since
223 // the fake is an echo server, we expect to receive what we wrote.
224 socket->Receive(
225 4096,
226 base::Bind(&BluetoothSocketChromeOSTest::ReceiveSuccessCallback,
227 base::Unretained(this)),
228 base::Bind(&BluetoothSocketChromeOSTest::ReceiveErrorCallback,
229 base::Unretained(this)));
230 message_loop_.Run();
231
232 EXPECT_EQ(1U, success_callback_count_);
233 EXPECT_EQ(0U, error_callback_count_);
234 EXPECT_EQ(4, last_bytes_received_);
235 EXPECT_TRUE(last_io_buffer_.get() != NULL);
236
237 // Take ownership of the received buffer.
238 scoped_refptr<net::IOBuffer> read_buffer = last_io_buffer_;
239 last_io_buffer_ = NULL;
240 success_callback_count_ = 0;
241 error_callback_count_ = 0;
242
243 std::string data = std::string(read_buffer->data(), last_bytes_received_);
244 EXPECT_EQ("test", data);
245
246 read_buffer = NULL;
247
248 // Receive data again; the socket will have been closed, this should cause a
249 // disconnected error to be returned via the error callback.
250 socket->Receive(
251 4096,
252 base::Bind(&BluetoothSocketChromeOSTest::ReceiveSuccessCallback,
253 base::Unretained(this)),
254 base::Bind(&BluetoothSocketChromeOSTest::ReceiveErrorCallback,
255 base::Unretained(this)));
256 message_loop_.Run();
257
258 EXPECT_EQ(0U, success_callback_count_);
259 EXPECT_EQ(1U, error_callback_count_);
260 EXPECT_EQ(BluetoothSocket::kDisconnected, last_reason_);
261 EXPECT_EQ(net::ErrorToString(net::ERR_CONNECTION_CLOSED), last_message_);
262
263 success_callback_count_ = 0;
264 error_callback_count_ = 0;
265
266 // Send data again; since the socket is closed we should get a system error
267 // equivalent to the connection reset error.
268 write_buffer = new net::StringIOBuffer("second test");
269
270 socket->Send(write_buffer.get(), write_buffer->size(),
271 base::Bind(&BluetoothSocketChromeOSTest::SendSuccessCallback,
272 base::Unretained(this)),
273 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback,
274 base::Unretained(this)));
275 message_loop_.Run();
276
277 EXPECT_EQ(0U, success_callback_count_);
278 EXPECT_EQ(1U, error_callback_count_);
279 EXPECT_EQ(net::ErrorToString(net::ERR_CONNECTION_RESET), last_message_);
280
281 success_callback_count_ = 0;
282 error_callback_count_ = 0;
283
284 // Close our end of the socket.
285 socket->Disconnect(base::Bind(&BluetoothSocketChromeOSTest::SuccessCallback,
286 base::Unretained(this)));
287
288 message_loop_.Run();
289 EXPECT_EQ(1U, success_callback_count_);
290 }
291
292 TEST_F(BluetoothSocketChromeOSTest, Listen) {
293 adapter_->CreateRfcommService(
294 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid),
295 BluetoothAdapter::kChannelAuto,
296 false,
297 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback,
298 base::Unretained(this)),
299 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback,
300 base::Unretained(this)));
301
302 EXPECT_EQ(1U, success_callback_count_);
303 EXPECT_EQ(0U, error_callback_count_);
304 EXPECT_TRUE(last_socket_.get() != NULL);
305
306 // Take ownership of the socket for the remainder of the test.
307 scoped_refptr<BluetoothSocket> server_socket = last_socket_;
308 last_socket_ = NULL;
309 success_callback_count_ = 0;
310 error_callback_count_ = 0;
311
312 // Simulate an incoming connection by just calling the ConnectProfile method
313 // of the underlying fake device client (from the BlueZ point of view,
314 // outgoing and incoming look the same).
315 //
316 // This is done before the Accept() call to simulate a pending call at the
317 // point that Accept() is called.
318 FakeBluetoothDeviceClient* fake_bluetooth_device_client =
319 static_cast<FakeBluetoothDeviceClient*>(
320 DBusThreadManager::Get()->GetBluetoothDeviceClient());
321 BluetoothDevice* device = adapter_->GetDevice(
322 FakeBluetoothDeviceClient::kPairedDeviceAddress);
323 ASSERT_TRUE(device != NULL);
324 fake_bluetooth_device_client->ConnectProfile(
325 static_cast<BluetoothDeviceChromeOS*>(device)->object_path(),
326 FakeBluetoothProfileManagerClient::kRfcommUuid,
327 base::Bind(&base::DoNothing),
328 base::Bind(&DoNothingDBusErrorCallback));
329
330 server_socket->Accept(
331 base::Bind(&BluetoothSocketChromeOSTest::AcceptSuccessCallback,
332 base::Unretained(this)),
333 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback,
334 base::Unretained(this)));
335
336 message_loop_.Run();
337
338 EXPECT_EQ(1U, success_callback_count_);
339 EXPECT_EQ(0U, error_callback_count_);
340 EXPECT_TRUE(last_socket_.get() != NULL);
341
342 // Take ownership of the client socket for the remainder of the test.
343 scoped_refptr<BluetoothSocket> client_socket = last_socket_;
344 last_socket_ = NULL;
345 success_callback_count_ = 0;
346 error_callback_count_ = 0;
347
348 // Close our end of the client socket.
349 client_socket->Disconnect(
350 base::Bind(&BluetoothSocketChromeOSTest::SuccessCallback,
351 base::Unretained(this)));
352
353 message_loop_.Run();
354
355 EXPECT_EQ(1U, success_callback_count_);
356 client_socket = NULL;
357 success_callback_count_ = 0;
358 error_callback_count_ = 0;
359
360 // Run a second connection test, this time calling Accept() before the
361 // incoming connection comes in.
362 server_socket->Accept(
363 base::Bind(&BluetoothSocketChromeOSTest::AcceptSuccessCallback,
364 base::Unretained(this)),
365 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback,
366 base::Unretained(this)));
367
368 fake_bluetooth_device_client->ConnectProfile(
369 static_cast<BluetoothDeviceChromeOS*>(device)->object_path(),
370 FakeBluetoothProfileManagerClient::kRfcommUuid,
371 base::Bind(&base::DoNothing),
372 base::Bind(&DoNothingDBusErrorCallback));
373
374 message_loop_.Run();
375
376 EXPECT_EQ(1U, success_callback_count_);
377 EXPECT_EQ(0U, error_callback_count_);
378 EXPECT_TRUE(last_socket_.get() != NULL);
379
380 // Take ownership of the client socket for the remainder of the test.
381 client_socket = last_socket_;
382 last_socket_ = NULL;
383 success_callback_count_ = 0;
384 error_callback_count_ = 0;
385
386 // Close our end of the client socket.
387 client_socket->Disconnect(
388 base::Bind(&BluetoothSocketChromeOSTest::SuccessCallback,
389 base::Unretained(this)));
390
391 message_loop_.Run();
392
393 EXPECT_EQ(1U, success_callback_count_);
394 client_socket = NULL;
395 success_callback_count_ = 0;
396 error_callback_count_ = 0;
397
398 // Now close the server socket.
399 server_socket->Disconnect(
400 base::Bind(&BluetoothSocketChromeOSTest::ImmediateSuccessCallback,
401 base::Unretained(this)));
402
403 EXPECT_EQ(1U, success_callback_count_);
404 }
405
406 TEST_F(BluetoothSocketChromeOSTest, ListenBeforeAdapterStart) {
407 // Start off with an invisible adapter, register the profile, then make
408 // the adapter visible.
409 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client =
410 static_cast<FakeBluetoothAdapterClient*>(
411 DBusThreadManager::Get()->GetBluetoothAdapterClient());
412 fake_bluetooth_adapter_client->SetVisible(false);
413
414 adapter_->CreateRfcommService(
415 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid),
416 BluetoothAdapter::kChannelAuto,
417 false,
418 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback,
419 base::Unretained(this)),
420 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback,
421 base::Unretained(this)));
422
423 EXPECT_EQ(1U, success_callback_count_);
424 EXPECT_EQ(0U, error_callback_count_);
425 EXPECT_TRUE(last_socket_.get() != NULL);
426
427 // Take ownership of the socket for the remainder of the test.
428 scoped_refptr<BluetoothSocket> socket = last_socket_;
429 last_socket_ = NULL;
430 success_callback_count_ = 0;
431 error_callback_count_ = 0;
432
433 // But there shouldn't be a profile registered yet.
434 FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client =
435 static_cast<FakeBluetoothProfileManagerClient*>(
436 DBusThreadManager::Get()->GetBluetoothProfileManagerClient());
437 FakeBluetoothProfileServiceProvider* profile_service_provider =
438 fake_bluetooth_profile_manager_client->GetProfileServiceProvider(
439 FakeBluetoothProfileManagerClient::kRfcommUuid);
440 EXPECT_TRUE(profile_service_provider == NULL);
441
442 // Make the adapter visible. This should register a profile.
443 fake_bluetooth_adapter_client->SetVisible(true);
444
445 profile_service_provider =
446 fake_bluetooth_profile_manager_client->GetProfileServiceProvider(
447 FakeBluetoothProfileManagerClient::kRfcommUuid);
448 EXPECT_TRUE(profile_service_provider != NULL);
449
450 // Cleanup the socket.
451 socket->Disconnect(
452 base::Bind(&BluetoothSocketChromeOSTest::ImmediateSuccessCallback,
453 base::Unretained(this)));
454
455 EXPECT_EQ(1U, success_callback_count_);
456 }
457
458 TEST_F(BluetoothSocketChromeOSTest, ListenAcrossAdapterRestart) {
459 // The fake adapter starts off visible by default.
460 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client =
461 static_cast<FakeBluetoothAdapterClient*>(
462 DBusThreadManager::Get()->GetBluetoothAdapterClient());
463
464 adapter_->CreateRfcommService(
465 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid),
466 BluetoothAdapter::kChannelAuto,
467 false,
468 base::Bind(&BluetoothSocketChromeOSTest::CreateServiceSuccessCallback,
469 base::Unretained(this)),
470 base::Bind(&BluetoothSocketChromeOSTest::ErrorCallback,
471 base::Unretained(this)));
472
473 EXPECT_EQ(1U, success_callback_count_);
474 EXPECT_EQ(0U, error_callback_count_);
475 EXPECT_TRUE(last_socket_.get() != NULL);
476
477 // Take ownership of the socket for the remainder of the test.
478 scoped_refptr<BluetoothSocket> socket = last_socket_;
479 last_socket_ = NULL;
480 success_callback_count_ = 0;
481 error_callback_count_ = 0;
482
483 // Make sure the profile was registered with the daemon.
484 FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client =
485 static_cast<FakeBluetoothProfileManagerClient*>(
486 DBusThreadManager::Get()->GetBluetoothProfileManagerClient());
487 FakeBluetoothProfileServiceProvider* profile_service_provider =
488 fake_bluetooth_profile_manager_client->GetProfileServiceProvider(
489 FakeBluetoothProfileManagerClient::kRfcommUuid);
490 EXPECT_TRUE(profile_service_provider != NULL);
491
492 // Make the adapter invisible, and fiddle with the profile fake to unregister
493 // the profile since this doesn't happen automatically.
494 fake_bluetooth_adapter_client->SetVisible(false);
495 fake_bluetooth_profile_manager_client->UnregisterProfile(
496 static_cast<BluetoothSocketChromeOS*>(socket.get())->object_path(),
497 base::Bind(&base::DoNothing),
498 base::Bind(&DoNothingDBusErrorCallback));
499
500 // Then make the adapter visible again. This should re-register the profile.
501 fake_bluetooth_adapter_client->SetVisible(true);
502
503 profile_service_provider =
504 fake_bluetooth_profile_manager_client->GetProfileServiceProvider(
505 FakeBluetoothProfileManagerClient::kRfcommUuid);
506 EXPECT_TRUE(profile_service_provider != NULL);
507
508 // Cleanup the socket.
509 socket->Disconnect(
510 base::Bind(&BluetoothSocketChromeOSTest::ImmediateSuccessCallback,
511 base::Unretained(this)));
512
513 EXPECT_EQ(1U, success_callback_count_);
514 }
515
516 } // namespace chromeos
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_socket_chromeos.cc ('k') | device/bluetooth/bluetooth_socket_thread.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698