| OLD | NEW |
| 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_socket_bluez.h" | 5 #include "device/bluetooth/bluez/bluetooth_socket_bluez.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 dbus_setter->SetBluetoothInputClient( | 74 dbus_setter->SetBluetoothInputClient( |
| 75 std::unique_ptr<bluez::BluetoothInputClient>( | 75 std::unique_ptr<bluez::BluetoothInputClient>( |
| 76 new bluez::FakeBluetoothInputClient)); | 76 new bluez::FakeBluetoothInputClient)); |
| 77 dbus_setter->SetBluetoothProfileManagerClient( | 77 dbus_setter->SetBluetoothProfileManagerClient( |
| 78 std::unique_ptr<bluez::BluetoothProfileManagerClient>( | 78 std::unique_ptr<bluez::BluetoothProfileManagerClient>( |
| 79 new bluez::FakeBluetoothProfileManagerClient)); | 79 new bluez::FakeBluetoothProfileManagerClient)); |
| 80 | 80 |
| 81 BluetoothSocketThread::Get(); | 81 BluetoothSocketThread::Get(); |
| 82 | 82 |
| 83 // Grab a pointer to the adapter. | 83 // Grab a pointer to the adapter. |
| 84 device::BluetoothAdapterFactory::GetAdapter(base::Bind( | 84 { |
| 85 &BluetoothSocketBlueZTest::AdapterCallback, base::Unretained(this))); | 85 base::RunLoop run_loop; |
| 86 | 86 device::BluetoothAdapterFactory::GetAdapter( |
| 87 base::RunLoop().Run(); | 87 base::Bind(&BluetoothSocketBlueZTest::AdapterCallback, |
| 88 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 89 run_loop.Run(); |
| 90 } |
| 88 | 91 |
| 89 ASSERT_TRUE(adapter_.get() != nullptr); | 92 ASSERT_TRUE(adapter_.get() != nullptr); |
| 90 ASSERT_TRUE(adapter_->IsInitialized()); | 93 ASSERT_TRUE(adapter_->IsInitialized()); |
| 91 ASSERT_TRUE(adapter_->IsPresent()); | 94 ASSERT_TRUE(adapter_->IsPresent()); |
| 92 | 95 |
| 93 // Turn on the adapter. | 96 // Turn on the adapter. |
| 94 adapter_->SetPowered(true, base::Bind(&base::DoNothing), | 97 adapter_->SetPowered(true, base::Bind(&base::DoNothing), |
| 95 base::Bind(&base::DoNothing)); | 98 base::Bind(&base::DoNothing)); |
| 96 ASSERT_TRUE(adapter_->IsPowered()); | 99 ASSERT_TRUE(adapter_->IsPowered()); |
| 97 } | 100 } |
| 98 | 101 |
| 99 void TearDown() override { | 102 void TearDown() override { |
| 100 adapter_ = nullptr; | 103 adapter_ = nullptr; |
| 101 BluetoothSocketThread::CleanupForTesting(); | 104 BluetoothSocketThread::CleanupForTesting(); |
| 102 bluez::BluezDBusManager::Shutdown(); | 105 bluez::BluezDBusManager::Shutdown(); |
| 103 } | 106 } |
| 104 | 107 |
| 105 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { | 108 void AdapterCallback(base::OnceClosure continuation, |
| 109 scoped_refptr<BluetoothAdapter> adapter) { |
| 106 adapter_ = adapter; | 110 adapter_ = adapter; |
| 107 if (base::MessageLoop::current() && | 111 std::move(continuation).Run(); |
| 108 base::MessageLoop::current()->is_running()) { | |
| 109 base::MessageLoop::current()->QuitWhenIdle(); | |
| 110 } | |
| 111 } | 112 } |
| 112 | 113 |
| 113 void SuccessCallback() { | 114 void SuccessCallback(base::OnceClosure continuation) { |
| 114 ++success_callback_count_; | 115 ++success_callback_count_; |
| 115 message_loop_.QuitWhenIdle(); | 116 std::move(continuation).Run(); |
| 116 } | 117 } |
| 117 | 118 |
| 118 void ErrorCallback(const std::string& message) { | 119 void ErrorCallback(base::OnceClosure continuation, |
| 120 const std::string& message) { |
| 119 ++error_callback_count_; | 121 ++error_callback_count_; |
| 120 last_message_ = message; | 122 last_message_ = message; |
| 121 | 123 std::move(continuation).Run(); |
| 122 if (message_loop_.is_running()) | |
| 123 message_loop_.QuitWhenIdle(); | |
| 124 } | 124 } |
| 125 | 125 |
| 126 void ConnectToServiceSuccessCallback(scoped_refptr<BluetoothSocket> socket) { | 126 void ConnectToServiceSuccessCallback(base::OnceClosure continuation, |
| 127 scoped_refptr<BluetoothSocket> socket) { |
| 127 ++success_callback_count_; | 128 ++success_callback_count_; |
| 128 last_socket_ = socket; | 129 last_socket_ = socket; |
| 129 | 130 std::move(continuation).Run(); |
| 130 message_loop_.QuitWhenIdle(); | |
| 131 } | 131 } |
| 132 | 132 |
| 133 void SendSuccessCallback(int bytes_sent) { | 133 void SendSuccessCallback(base::OnceClosure continuation, int bytes_sent) { |
| 134 ++success_callback_count_; | 134 ++success_callback_count_; |
| 135 last_bytes_sent_ = bytes_sent; | 135 last_bytes_sent_ = bytes_sent; |
| 136 | 136 std::move(continuation).Run(); |
| 137 message_loop_.QuitWhenIdle(); | |
| 138 } | 137 } |
| 139 | 138 |
| 140 void ReceiveSuccessCallback(int bytes_received, | 139 void ReceiveSuccessCallback(base::OnceClosure continuation, |
| 140 int bytes_received, |
| 141 scoped_refptr<net::IOBuffer> io_buffer) { | 141 scoped_refptr<net::IOBuffer> io_buffer) { |
| 142 ++success_callback_count_; | 142 ++success_callback_count_; |
| 143 last_bytes_received_ = bytes_received; | 143 last_bytes_received_ = bytes_received; |
| 144 last_io_buffer_ = io_buffer; | 144 last_io_buffer_ = io_buffer; |
| 145 | 145 std::move(continuation).Run(); |
| 146 message_loop_.QuitWhenIdle(); | |
| 147 } | 146 } |
| 148 | 147 |
| 149 void ReceiveErrorCallback(BluetoothSocket::ErrorReason reason, | 148 void ReceiveErrorCallback(base::OnceClosure continuation, |
| 149 BluetoothSocket::ErrorReason reason, |
| 150 const std::string& error_message) { | 150 const std::string& error_message) { |
| 151 ++error_callback_count_; | 151 ++error_callback_count_; |
| 152 last_reason_ = reason; | 152 last_reason_ = reason; |
| 153 last_message_ = error_message; | 153 last_message_ = error_message; |
| 154 | 154 std::move(continuation).Run(); |
| 155 message_loop_.QuitWhenIdle(); | |
| 156 } | 155 } |
| 157 | 156 |
| 158 void CreateServiceSuccessCallback(scoped_refptr<BluetoothSocket> socket) { | 157 void CreateServiceSuccessCallback(base::OnceClosure continuation, |
| 158 scoped_refptr<BluetoothSocket> socket) { |
| 159 ++success_callback_count_; | 159 ++success_callback_count_; |
| 160 last_socket_ = socket; | 160 last_socket_ = socket; |
| 161 | 161 std::move(continuation).Run(); |
| 162 if (message_loop_.is_running()) | |
| 163 message_loop_.QuitWhenIdle(); | |
| 164 } | 162 } |
| 165 | 163 |
| 166 void AcceptSuccessCallback(const BluetoothDevice* device, | 164 void AcceptSuccessCallback(base::OnceClosure continuation, |
| 165 const BluetoothDevice* device, |
| 167 scoped_refptr<BluetoothSocket> socket) { | 166 scoped_refptr<BluetoothSocket> socket) { |
| 168 ++success_callback_count_; | 167 ++success_callback_count_; |
| 169 last_device_ = device; | 168 last_device_ = device; |
| 170 last_socket_ = socket; | 169 last_socket_ = socket; |
| 171 | 170 std::move(continuation).Run(); |
| 172 message_loop_.QuitWhenIdle(); | |
| 173 } | 171 } |
| 174 | 172 |
| 175 void ImmediateSuccessCallback() { ++success_callback_count_; } | 173 void ImmediateSuccessCallback() { ++success_callback_count_; } |
| 176 | 174 |
| 177 protected: | 175 protected: |
| 178 base::MessageLoop message_loop_; | 176 base::MessageLoop message_loop_; |
| 179 base::test::ScopedAsyncTaskScheduler scoped_async_task_scheduler_; | 177 base::test::ScopedAsyncTaskScheduler scoped_async_task_scheduler_; |
| 180 | 178 |
| 181 scoped_refptr<BluetoothAdapter> adapter_; | 179 scoped_refptr<BluetoothAdapter> adapter_; |
| 182 | 180 |
| 183 unsigned int success_callback_count_; | 181 unsigned int success_callback_count_; |
| 184 unsigned int error_callback_count_; | 182 unsigned int error_callback_count_; |
| 185 | 183 |
| 186 std::string last_message_; | 184 std::string last_message_; |
| 187 scoped_refptr<BluetoothSocket> last_socket_; | 185 scoped_refptr<BluetoothSocket> last_socket_; |
| 188 int last_bytes_sent_; | 186 int last_bytes_sent_; |
| 189 int last_bytes_received_; | 187 int last_bytes_received_; |
| 190 scoped_refptr<net::IOBuffer> last_io_buffer_; | 188 scoped_refptr<net::IOBuffer> last_io_buffer_; |
| 191 BluetoothSocket::ErrorReason last_reason_; | 189 BluetoothSocket::ErrorReason last_reason_; |
| 192 const BluetoothDevice* last_device_; | 190 const BluetoothDevice* last_device_; |
| 193 }; | 191 }; |
| 194 | 192 |
| 195 TEST_F(BluetoothSocketBlueZTest, Connect) { | 193 TEST_F(BluetoothSocketBlueZTest, Connect) { |
| 196 BluetoothDevice* device = adapter_->GetDevice( | 194 BluetoothDevice* device = adapter_->GetDevice( |
| 197 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | 195 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 198 ASSERT_TRUE(device != nullptr); | 196 ASSERT_TRUE(device != nullptr); |
| 199 | 197 |
| 200 device->ConnectToService( | 198 { |
| 201 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 199 base::RunLoop run_loop; |
| 202 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, | 200 device->ConnectToService( |
| 203 base::Unretained(this)), | 201 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 204 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 202 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, |
| 205 base::Unretained(this))); | 203 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 206 base::RunLoop().Run(); | 204 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 205 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 206 run_loop.Run(); |
| 207 } |
| 207 | 208 |
| 208 EXPECT_EQ(1U, success_callback_count_); | 209 EXPECT_EQ(1U, success_callback_count_); |
| 209 EXPECT_EQ(0U, error_callback_count_); | 210 EXPECT_EQ(0U, error_callback_count_); |
| 210 EXPECT_TRUE(last_socket_.get() != nullptr); | 211 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 211 | 212 |
| 212 // Take ownership of the socket for the remainder of the test. | 213 // Take ownership of the socket for the remainder of the test. |
| 213 scoped_refptr<BluetoothSocket> socket = last_socket_; | 214 scoped_refptr<BluetoothSocket> socket = last_socket_; |
| 214 last_socket_ = nullptr; | 215 last_socket_ = nullptr; |
| 215 success_callback_count_ = 0; | 216 success_callback_count_ = 0; |
| 216 error_callback_count_ = 0; | 217 error_callback_count_ = 0; |
| 217 | 218 |
| 218 // Send data to the socket, expect all of the data to be sent. | 219 // Send data to the socket, expect all of the data to be sent. |
| 219 scoped_refptr<net::StringIOBuffer> write_buffer( | 220 scoped_refptr<net::StringIOBuffer> write_buffer( |
| 220 new net::StringIOBuffer("test")); | 221 new net::StringIOBuffer("test")); |
| 221 | 222 |
| 222 socket->Send(write_buffer.get(), write_buffer->size(), | 223 { |
| 223 base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback, | 224 base::RunLoop run_loop; |
| 224 base::Unretained(this)), | 225 socket->Send( |
| 225 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 226 write_buffer.get(), write_buffer->size(), |
| 226 base::Unretained(this))); | 227 base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback, |
| 227 base::RunLoop().Run(); | 228 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 229 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 230 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 231 run_loop.Run(); |
| 232 } |
| 228 | 233 |
| 229 EXPECT_EQ(1U, success_callback_count_); | 234 EXPECT_EQ(1U, success_callback_count_); |
| 230 EXPECT_EQ(0U, error_callback_count_); | 235 EXPECT_EQ(0U, error_callback_count_); |
| 231 EXPECT_EQ(last_bytes_sent_, write_buffer->size()); | 236 EXPECT_EQ(last_bytes_sent_, write_buffer->size()); |
| 232 | 237 |
| 233 success_callback_count_ = 0; | 238 success_callback_count_ = 0; |
| 234 error_callback_count_ = 0; | 239 error_callback_count_ = 0; |
| 235 | 240 |
| 236 // Receive data from the socket, and fetch the buffer from the callback; since | 241 // Receive data from the socket, and fetch the buffer from the callback; since |
| 237 // the fake is an echo server, we expect to receive what we wrote. | 242 // the fake is an echo server, we expect to receive what we wrote. |
| 238 socket->Receive(4096, | 243 { |
| 239 base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback, | 244 base::RunLoop run_loop; |
| 240 base::Unretained(this)), | 245 socket->Receive( |
| 241 base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback, | 246 4096, |
| 242 base::Unretained(this))); | 247 base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback, |
| 243 base::RunLoop().Run(); | 248 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 249 base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback, |
| 250 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 251 run_loop.Run(); |
| 252 } |
| 244 | 253 |
| 245 EXPECT_EQ(1U, success_callback_count_); | 254 EXPECT_EQ(1U, success_callback_count_); |
| 246 EXPECT_EQ(0U, error_callback_count_); | 255 EXPECT_EQ(0U, error_callback_count_); |
| 247 EXPECT_EQ(4, last_bytes_received_); | 256 EXPECT_EQ(4, last_bytes_received_); |
| 248 EXPECT_TRUE(last_io_buffer_.get() != nullptr); | 257 EXPECT_TRUE(last_io_buffer_.get() != nullptr); |
| 249 | 258 |
| 250 // Take ownership of the received buffer. | 259 // Take ownership of the received buffer. |
| 251 scoped_refptr<net::IOBuffer> read_buffer = last_io_buffer_; | 260 scoped_refptr<net::IOBuffer> read_buffer = last_io_buffer_; |
| 252 last_io_buffer_ = nullptr; | 261 last_io_buffer_ = nullptr; |
| 253 success_callback_count_ = 0; | 262 success_callback_count_ = 0; |
| 254 error_callback_count_ = 0; | 263 error_callback_count_ = 0; |
| 255 | 264 |
| 256 std::string data = std::string(read_buffer->data(), last_bytes_received_); | 265 std::string data = std::string(read_buffer->data(), last_bytes_received_); |
| 257 EXPECT_EQ("test", data); | 266 EXPECT_EQ("test", data); |
| 258 | 267 |
| 259 read_buffer = nullptr; | 268 read_buffer = nullptr; |
| 260 | 269 |
| 261 // Receive data again; the socket will have been closed, this should cause a | 270 // Receive data again; the socket will have been closed, this should cause a |
| 262 // disconnected error to be returned via the error callback. | 271 // disconnected error to be returned via the error callback. |
| 263 socket->Receive(4096, | 272 { |
| 264 base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback, | 273 base::RunLoop run_loop; |
| 265 base::Unretained(this)), | 274 socket->Receive( |
| 266 base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback, | 275 4096, |
| 267 base::Unretained(this))); | 276 base::Bind(&BluetoothSocketBlueZTest::ReceiveSuccessCallback, |
| 268 base::RunLoop().Run(); | 277 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 278 base::Bind(&BluetoothSocketBlueZTest::ReceiveErrorCallback, |
| 279 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 280 run_loop.Run(); |
| 281 } |
| 269 | 282 |
| 270 EXPECT_EQ(0U, success_callback_count_); | 283 EXPECT_EQ(0U, success_callback_count_); |
| 271 EXPECT_EQ(1U, error_callback_count_); | 284 EXPECT_EQ(1U, error_callback_count_); |
| 272 EXPECT_EQ(BluetoothSocket::kDisconnected, last_reason_); | 285 EXPECT_EQ(BluetoothSocket::kDisconnected, last_reason_); |
| 273 EXPECT_EQ(net::ErrorToString(net::OK), last_message_); | 286 EXPECT_EQ(net::ErrorToString(net::OK), last_message_); |
| 274 | 287 |
| 275 success_callback_count_ = 0; | 288 success_callback_count_ = 0; |
| 276 error_callback_count_ = 0; | 289 error_callback_count_ = 0; |
| 277 | 290 |
| 278 // Send data again; since the socket is closed we should get a system error | 291 // Send data again; since the socket is closed we should get a system error |
| 279 // equivalent to the connection reset error. | 292 // equivalent to the connection reset error. |
| 280 write_buffer = new net::StringIOBuffer("second test"); | 293 write_buffer = new net::StringIOBuffer("second test"); |
| 281 | 294 |
| 282 socket->Send(write_buffer.get(), write_buffer->size(), | 295 { |
| 283 base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback, | 296 base::RunLoop run_loop; |
| 284 base::Unretained(this)), | 297 socket->Send( |
| 285 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 298 write_buffer.get(), write_buffer->size(), |
| 286 base::Unretained(this))); | 299 base::Bind(&BluetoothSocketBlueZTest::SendSuccessCallback, |
| 287 base::RunLoop().Run(); | 300 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 301 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 302 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 303 run_loop.Run(); |
| 304 } |
| 288 | 305 |
| 289 EXPECT_EQ(0U, success_callback_count_); | 306 EXPECT_EQ(0U, success_callback_count_); |
| 290 EXPECT_EQ(1U, error_callback_count_); | 307 EXPECT_EQ(1U, error_callback_count_); |
| 291 EXPECT_EQ(net::ErrorToString(net::ERR_CONNECTION_RESET), last_message_); | 308 EXPECT_EQ(net::ErrorToString(net::ERR_CONNECTION_RESET), last_message_); |
| 292 | 309 |
| 293 success_callback_count_ = 0; | 310 success_callback_count_ = 0; |
| 294 error_callback_count_ = 0; | 311 error_callback_count_ = 0; |
| 295 | 312 |
| 296 // Close our end of the socket. | 313 // Close our end of the socket. |
| 297 socket->Disconnect(base::Bind(&BluetoothSocketBlueZTest::SuccessCallback, | 314 { |
| 298 base::Unretained(this))); | 315 base::RunLoop run_loop; |
| 299 | 316 socket->Disconnect(base::Bind(&BluetoothSocketBlueZTest::SuccessCallback, |
| 300 base::RunLoop().Run(); | 317 base::Unretained(this), |
| 318 run_loop.QuitWhenIdleClosure())); |
| 319 run_loop.Run(); |
| 320 } |
| 301 EXPECT_EQ(1U, success_callback_count_); | 321 EXPECT_EQ(1U, success_callback_count_); |
| 302 } | 322 } |
| 303 | 323 |
| 304 TEST_F(BluetoothSocketBlueZTest, Listen) { | 324 TEST_F(BluetoothSocketBlueZTest, Listen) { |
| 305 adapter_->CreateRfcommService( | 325 { |
| 306 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 326 base::RunLoop run_loop; |
| 307 BluetoothAdapter::ServiceOptions(), | 327 adapter_->CreateRfcommService( |
| 308 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | 328 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 309 base::Unretained(this)), | 329 BluetoothAdapter::ServiceOptions(), |
| 310 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 330 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, |
| 311 base::Unretained(this))); | 331 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 312 | 332 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 313 base::RunLoop().Run(); | 333 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 334 run_loop.Run(); |
| 335 } |
| 314 | 336 |
| 315 EXPECT_EQ(1U, success_callback_count_); | 337 EXPECT_EQ(1U, success_callback_count_); |
| 316 EXPECT_EQ(0U, error_callback_count_); | 338 EXPECT_EQ(0U, error_callback_count_); |
| 317 EXPECT_TRUE(last_socket_.get() != nullptr); | 339 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 318 | 340 |
| 319 // Take ownership of the socket for the remainder of the test. | 341 // Take ownership of the socket for the remainder of the test. |
| 320 scoped_refptr<BluetoothSocket> server_socket = last_socket_; | 342 scoped_refptr<BluetoothSocket> server_socket = last_socket_; |
| 321 last_socket_ = nullptr; | 343 last_socket_ = nullptr; |
| 322 success_callback_count_ = 0; | 344 success_callback_count_ = 0; |
| 323 error_callback_count_ = 0; | 345 error_callback_count_ = 0; |
| 324 | 346 |
| 325 // Simulate an incoming connection by just calling the ConnectProfile method | 347 // Simulate an incoming connection by just calling the ConnectProfile method |
| 326 // of the underlying fake device client (from the BlueZ point of view, | 348 // of the underlying fake device client (from the BlueZ point of view, |
| 327 // outgoing and incoming look the same). | 349 // outgoing and incoming look the same). |
| 328 // | 350 // |
| 329 // This is done before the Accept() call to simulate a pending call at the | 351 // This is done before the Accept() call to simulate a pending call at the |
| 330 // point that Accept() is called. | 352 // point that Accept() is called. |
| 331 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client = | 353 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client = |
| 332 static_cast<bluez::FakeBluetoothDeviceClient*>( | 354 static_cast<bluez::FakeBluetoothDeviceClient*>( |
| 333 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()); | 355 bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()); |
| 334 BluetoothDevice* device = adapter_->GetDevice( | 356 BluetoothDevice* device = adapter_->GetDevice( |
| 335 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | 357 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); |
| 336 ASSERT_TRUE(device != nullptr); | 358 ASSERT_TRUE(device != nullptr); |
| 337 fake_bluetooth_device_client->ConnectProfile( | 359 { |
| 338 static_cast<BluetoothDeviceBlueZ*>(device)->object_path(), | 360 base::RunLoop run_loop; |
| 339 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | 361 fake_bluetooth_device_client->ConnectProfile( |
| 340 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); | 362 static_cast<BluetoothDeviceBlueZ*>(device)->object_path(), |
| 341 | 363 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, |
| 342 base::RunLoop().RunUntilIdle(); | 364 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); |
| 343 | 365 run_loop.RunUntilIdle(); |
| 344 server_socket->Accept( | 366 } |
| 345 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, | 367 { |
| 346 base::Unretained(this)), | 368 base::RunLoop run_loop; |
| 347 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 369 server_socket->Accept( |
| 348 base::Unretained(this))); | 370 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, |
| 349 | 371 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 350 base::RunLoop().Run(); | 372 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 373 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 374 run_loop.Run(); |
| 375 } |
| 351 | 376 |
| 352 EXPECT_EQ(1U, success_callback_count_); | 377 EXPECT_EQ(1U, success_callback_count_); |
| 353 EXPECT_EQ(0U, error_callback_count_); | 378 EXPECT_EQ(0U, error_callback_count_); |
| 354 EXPECT_TRUE(last_socket_.get() != nullptr); | 379 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 355 | 380 |
| 356 // 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. |
| 357 scoped_refptr<BluetoothSocket> client_socket = last_socket_; | 382 scoped_refptr<BluetoothSocket> client_socket = last_socket_; |
| 358 last_socket_ = nullptr; | 383 last_socket_ = nullptr; |
| 359 success_callback_count_ = 0; | 384 success_callback_count_ = 0; |
| 360 error_callback_count_ = 0; | 385 error_callback_count_ = 0; |
| 361 | 386 |
| 362 // Close our end of the client socket. | 387 // Close our end of the client socket. |
| 363 client_socket->Disconnect(base::Bind( | 388 { |
| 364 &BluetoothSocketBlueZTest::SuccessCallback, base::Unretained(this))); | 389 base::RunLoop run_loop; |
| 365 | 390 client_socket->Disconnect( |
| 366 base::RunLoop().Run(); | 391 base::Bind(&BluetoothSocketBlueZTest::SuccessCallback, |
| 392 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 393 run_loop.Run(); |
| 394 } |
| 367 | 395 |
| 368 EXPECT_EQ(1U, success_callback_count_); | 396 EXPECT_EQ(1U, success_callback_count_); |
| 369 client_socket = nullptr; | 397 client_socket = nullptr; |
| 370 success_callback_count_ = 0; | 398 success_callback_count_ = 0; |
| 371 error_callback_count_ = 0; | 399 error_callback_count_ = 0; |
| 372 | 400 |
| 373 // Run a second connection test, this time calling Accept() before the | 401 // Run a second connection test, this time calling Accept() before the |
| 374 // incoming connection comes in. | 402 // incoming connection comes in. |
| 375 server_socket->Accept( | 403 { |
| 376 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, | 404 base::RunLoop run_loop1; |
| 377 base::Unretained(this)), | 405 // |run_loop2| is expected to be quit in ConnectProfile() through the quit |
| 378 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 406 // closures saved in the Accept() call. |
| 379 base::Unretained(this))); | 407 base::RunLoop run_loop2; |
| 380 | 408 |
| 381 base::RunLoop().RunUntilIdle(); | 409 server_socket->Accept( |
| 410 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, |
| 411 base::Unretained(this), run_loop2.QuitWhenIdleClosure()), |
| 412 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 413 base::Unretained(this), run_loop2.QuitWhenIdleClosure())); |
| 414 run_loop1.RunUntilIdle(); |
| 382 | 415 |
| 383 fake_bluetooth_device_client->ConnectProfile( | 416 fake_bluetooth_device_client->ConnectProfile( |
| 384 static_cast<BluetoothDeviceBlueZ*>(device)->object_path(), | 417 static_cast<BluetoothDeviceBlueZ*>(device)->object_path(), |
| 385 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, | 418 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid, |
| 386 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); | 419 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); |
| 387 | 420 run_loop2.Run(); |
| 388 base::RunLoop().Run(); | 421 } |
| 389 | 422 |
| 390 EXPECT_EQ(1U, success_callback_count_); | 423 EXPECT_EQ(1U, success_callback_count_); |
| 391 EXPECT_EQ(0U, error_callback_count_); | 424 EXPECT_EQ(0U, error_callback_count_); |
| 392 EXPECT_TRUE(last_socket_.get() != nullptr); | 425 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 393 | 426 |
| 394 // Take ownership of the client socket for the remainder of the test. | 427 // Take ownership of the client socket for the remainder of the test. |
| 395 client_socket = last_socket_; | 428 client_socket = last_socket_; |
| 396 last_socket_ = nullptr; | 429 last_socket_ = nullptr; |
| 397 success_callback_count_ = 0; | 430 success_callback_count_ = 0; |
| 398 error_callback_count_ = 0; | 431 error_callback_count_ = 0; |
| 399 | 432 |
| 400 // Close our end of the client socket. | 433 // Close our end of the client socket. |
| 401 client_socket->Disconnect(base::Bind( | 434 { |
| 402 &BluetoothSocketBlueZTest::SuccessCallback, base::Unretained(this))); | 435 base::RunLoop run_loop; |
| 403 | 436 client_socket->Disconnect( |
| 404 base::RunLoop().Run(); | 437 base::Bind(&BluetoothSocketBlueZTest::SuccessCallback, |
| 438 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 439 run_loop.Run(); |
| 440 } |
| 405 | 441 |
| 406 EXPECT_EQ(1U, success_callback_count_); | 442 EXPECT_EQ(1U, success_callback_count_); |
| 407 client_socket = nullptr; | 443 client_socket = nullptr; |
| 408 success_callback_count_ = 0; | 444 success_callback_count_ = 0; |
| 409 error_callback_count_ = 0; | 445 error_callback_count_ = 0; |
| 410 | 446 |
| 411 // Now close the server socket. | 447 // Now close the server socket. |
| 412 server_socket->Disconnect( | 448 { |
| 413 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, | 449 base::RunLoop run_loop; |
| 414 base::Unretained(this))); | 450 server_socket->Disconnect( |
| 415 | 451 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, |
| 416 base::RunLoop().RunUntilIdle(); | 452 base::Unretained(this))); |
| 453 run_loop.RunUntilIdle(); |
| 454 } |
| 417 | 455 |
| 418 EXPECT_EQ(1U, success_callback_count_); | 456 EXPECT_EQ(1U, success_callback_count_); |
| 419 } | 457 } |
| 420 | 458 |
| 421 TEST_F(BluetoothSocketBlueZTest, ListenBeforeAdapterStart) { | 459 TEST_F(BluetoothSocketBlueZTest, ListenBeforeAdapterStart) { |
| 422 // Start off with an invisible adapter, register the profile, then make | 460 // Start off with an invisible adapter, register the profile, then make |
| 423 // the adapter visible. | 461 // the adapter visible. |
| 424 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = | 462 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = |
| 425 static_cast<bluez::FakeBluetoothAdapterClient*>( | 463 static_cast<bluez::FakeBluetoothAdapterClient*>( |
| 426 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()); | 464 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()); |
| 427 fake_bluetooth_adapter_client->SetVisible(false); | 465 fake_bluetooth_adapter_client->SetVisible(false); |
| 428 | 466 |
| 429 adapter_->CreateRfcommService( | 467 { |
| 430 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 468 base::RunLoop run_loop; |
| 431 BluetoothAdapter::ServiceOptions(), | 469 adapter_->CreateRfcommService( |
| 432 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | 470 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 433 base::Unretained(this)), | 471 BluetoothAdapter::ServiceOptions(), |
| 434 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 472 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, |
| 435 base::Unretained(this))); | 473 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 436 base::RunLoop().Run(); | 474 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 475 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 476 run_loop.Run(); |
| 477 } |
| 437 | 478 |
| 438 EXPECT_EQ(1U, success_callback_count_); | 479 EXPECT_EQ(1U, success_callback_count_); |
| 439 EXPECT_EQ(0U, error_callback_count_); | 480 EXPECT_EQ(0U, error_callback_count_); |
| 440 EXPECT_TRUE(last_socket_.get() != nullptr); | 481 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 441 | 482 |
| 442 // Take ownership of the socket for the remainder of the test. | 483 // Take ownership of the socket for the remainder of the test. |
| 443 scoped_refptr<BluetoothSocket> socket = last_socket_; | 484 scoped_refptr<BluetoothSocket> socket = last_socket_; |
| 444 last_socket_ = nullptr; | 485 last_socket_ = nullptr; |
| 445 success_callback_count_ = 0; | 486 success_callback_count_ = 0; |
| 446 error_callback_count_ = 0; | 487 error_callback_count_ = 0; |
| 447 | 488 |
| 448 // But there shouldn't be a profile registered yet. | 489 // But there shouldn't be a profile registered yet. |
| 449 bluez::FakeBluetoothProfileManagerClient* | 490 bluez::FakeBluetoothProfileManagerClient* |
| 450 fake_bluetooth_profile_manager_client = | 491 fake_bluetooth_profile_manager_client = |
| 451 static_cast<bluez::FakeBluetoothProfileManagerClient*>( | 492 static_cast<bluez::FakeBluetoothProfileManagerClient*>( |
| 452 bluez::BluezDBusManager::Get() | 493 bluez::BluezDBusManager::Get() |
| 453 ->GetBluetoothProfileManagerClient()); | 494 ->GetBluetoothProfileManagerClient()); |
| 454 bluez::FakeBluetoothProfileServiceProvider* profile_service_provider = | 495 bluez::FakeBluetoothProfileServiceProvider* profile_service_provider = |
| 455 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | 496 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( |
| 456 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | 497 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); |
| 457 EXPECT_TRUE(profile_service_provider == nullptr); | 498 EXPECT_TRUE(profile_service_provider == nullptr); |
| 458 | 499 |
| 459 // Make the adapter visible. This should register a profile. | 500 // Make the adapter visible. This should register a profile. |
| 460 fake_bluetooth_adapter_client->SetVisible(true); | 501 { |
| 461 | 502 base::RunLoop run_loop; |
| 462 base::RunLoop().RunUntilIdle(); | 503 fake_bluetooth_adapter_client->SetVisible(true); |
| 504 run_loop.RunUntilIdle(); |
| 505 } |
| 463 | 506 |
| 464 profile_service_provider = | 507 profile_service_provider = |
| 465 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | 508 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( |
| 466 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | 509 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); |
| 467 EXPECT_TRUE(profile_service_provider != nullptr); | 510 EXPECT_TRUE(profile_service_provider != nullptr); |
| 468 | 511 |
| 469 // Cleanup the socket. | 512 // Cleanup the socket. |
| 470 socket->Disconnect( | 513 { |
| 471 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, | 514 base::RunLoop run_loop; |
| 472 base::Unretained(this))); | 515 socket->Disconnect( |
| 473 | 516 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, |
| 474 base::RunLoop().RunUntilIdle(); | 517 base::Unretained(this))); |
| 518 run_loop.RunUntilIdle(); |
| 519 } |
| 475 | 520 |
| 476 EXPECT_EQ(1U, success_callback_count_); | 521 EXPECT_EQ(1U, success_callback_count_); |
| 477 } | 522 } |
| 478 | 523 |
| 479 TEST_F(BluetoothSocketBlueZTest, ListenAcrossAdapterRestart) { | 524 TEST_F(BluetoothSocketBlueZTest, ListenAcrossAdapterRestart) { |
| 480 // The fake adapter starts off visible by default. | 525 // The fake adapter starts off visible by default. |
| 481 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = | 526 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client = |
| 482 static_cast<bluez::FakeBluetoothAdapterClient*>( | 527 static_cast<bluez::FakeBluetoothAdapterClient*>( |
| 483 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()); | 528 bluez::BluezDBusManager::Get()->GetBluetoothAdapterClient()); |
| 484 | 529 |
| 485 adapter_->CreateRfcommService( | 530 { |
| 486 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 531 base::RunLoop run_loop; |
| 487 BluetoothAdapter::ServiceOptions(), | 532 adapter_->CreateRfcommService( |
| 488 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | 533 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 489 base::Unretained(this)), | 534 BluetoothAdapter::ServiceOptions(), |
| 490 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 535 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, |
| 491 base::Unretained(this))); | 536 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 492 base::RunLoop().Run(); | 537 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 538 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 539 run_loop.Run(); |
| 540 } |
| 493 | 541 |
| 494 EXPECT_EQ(1U, success_callback_count_); | 542 EXPECT_EQ(1U, success_callback_count_); |
| 495 EXPECT_EQ(0U, error_callback_count_); | 543 EXPECT_EQ(0U, error_callback_count_); |
| 496 EXPECT_TRUE(last_socket_.get() != nullptr); | 544 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 497 | 545 |
| 498 // Take ownership of the socket for the remainder of the test. | 546 // Take ownership of the socket for the remainder of the test. |
| 499 scoped_refptr<BluetoothSocket> socket = last_socket_; | 547 scoped_refptr<BluetoothSocket> socket = last_socket_; |
| 500 last_socket_ = nullptr; | 548 last_socket_ = nullptr; |
| 501 success_callback_count_ = 0; | 549 success_callback_count_ = 0; |
| 502 error_callback_count_ = 0; | 550 error_callback_count_ = 0; |
| 503 | 551 |
| 504 // Make sure the profile was registered with the daemon. | 552 // Make sure the profile was registered with the daemon. |
| 505 bluez::FakeBluetoothProfileManagerClient* | 553 bluez::FakeBluetoothProfileManagerClient* |
| 506 fake_bluetooth_profile_manager_client = | 554 fake_bluetooth_profile_manager_client = |
| 507 static_cast<bluez::FakeBluetoothProfileManagerClient*>( | 555 static_cast<bluez::FakeBluetoothProfileManagerClient*>( |
| 508 bluez::BluezDBusManager::Get() | 556 bluez::BluezDBusManager::Get() |
| 509 ->GetBluetoothProfileManagerClient()); | 557 ->GetBluetoothProfileManagerClient()); |
| 510 bluez::FakeBluetoothProfileServiceProvider* profile_service_provider = | 558 bluez::FakeBluetoothProfileServiceProvider* profile_service_provider = |
| 511 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | 559 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( |
| 512 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | 560 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); |
| 513 EXPECT_TRUE(profile_service_provider != nullptr); | 561 EXPECT_TRUE(profile_service_provider != nullptr); |
| 514 | 562 |
| 515 // Make the adapter invisible, and fiddle with the profile fake to unregister | 563 // Make the adapter invisible, and fiddle with the profile fake to unregister |
| 516 // the profile since this doesn't happen automatically. | 564 // the profile since this doesn't happen automatically. |
| 517 fake_bluetooth_adapter_client->SetVisible(false); | 565 { |
| 518 | 566 base::RunLoop run_loop; |
| 519 base::RunLoop().RunUntilIdle(); | 567 fake_bluetooth_adapter_client->SetVisible(false); |
| 568 run_loop.RunUntilIdle(); |
| 569 } |
| 520 | 570 |
| 521 // Then make the adapter visible again. This should re-register the profile. | 571 // Then make the adapter visible again. This should re-register the profile. |
| 522 fake_bluetooth_adapter_client->SetVisible(true); | 572 { |
| 523 | 573 base::RunLoop run_loop; |
| 524 base::RunLoop().RunUntilIdle(); | 574 fake_bluetooth_adapter_client->SetVisible(true); |
| 575 run_loop.RunUntilIdle(); |
| 576 } |
| 525 | 577 |
| 526 profile_service_provider = | 578 profile_service_provider = |
| 527 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( | 579 fake_bluetooth_profile_manager_client->GetProfileServiceProvider( |
| 528 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); | 580 bluez::FakeBluetoothProfileManagerClient::kRfcommUuid); |
| 529 EXPECT_TRUE(profile_service_provider != nullptr); | 581 EXPECT_TRUE(profile_service_provider != nullptr); |
| 530 | 582 |
| 531 // Cleanup the socket. | 583 // Cleanup the socket. |
| 532 socket->Disconnect( | 584 { |
| 533 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, | 585 base::RunLoop run_loop; |
| 534 base::Unretained(this))); | 586 socket->Disconnect( |
| 535 | 587 base::Bind(&BluetoothSocketBlueZTest::ImmediateSuccessCallback, |
| 536 base::RunLoop().RunUntilIdle(); | 588 base::Unretained(this))); |
| 589 run_loop.RunUntilIdle(); |
| 590 } |
| 537 | 591 |
| 538 EXPECT_EQ(1U, success_callback_count_); | 592 EXPECT_EQ(1U, success_callback_count_); |
| 539 } | 593 } |
| 540 | 594 |
| 541 TEST_F(BluetoothSocketBlueZTest, PairedConnectFails) { | 595 TEST_F(BluetoothSocketBlueZTest, PairedConnectFails) { |
| 542 BluetoothDevice* device = adapter_->GetDevice( | 596 BluetoothDevice* device = adapter_->GetDevice( |
| 543 bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress); | 597 bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress); |
| 544 ASSERT_TRUE(device != nullptr); | 598 ASSERT_TRUE(device != nullptr); |
| 545 | 599 |
| 546 device->ConnectToService( | 600 { |
| 547 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 601 base::RunLoop run_loop; |
| 548 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, | 602 device->ConnectToService( |
| 549 base::Unretained(this)), | 603 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 550 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 604 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, |
| 551 base::Unretained(this))); | 605 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 552 base::RunLoop().Run(); | 606 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 607 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 608 run_loop.Run(); |
| 609 } |
| 553 | 610 |
| 554 EXPECT_EQ(0U, success_callback_count_); | 611 EXPECT_EQ(0U, success_callback_count_); |
| 555 EXPECT_EQ(1U, error_callback_count_); | 612 EXPECT_EQ(1U, error_callback_count_); |
| 556 EXPECT_TRUE(last_socket_.get() == nullptr); | 613 EXPECT_TRUE(last_socket_.get() == nullptr); |
| 557 | 614 |
| 558 device->ConnectToService( | 615 { |
| 559 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 616 base::RunLoop run_loop; |
| 560 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, | 617 device->ConnectToService( |
| 561 base::Unretained(this)), | 618 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 562 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 619 base::Bind(&BluetoothSocketBlueZTest::ConnectToServiceSuccessCallback, |
| 563 base::Unretained(this))); | 620 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 564 base::RunLoop().Run(); | 621 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 622 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 623 run_loop.Run(); |
| 624 } |
| 565 | 625 |
| 566 EXPECT_EQ(0U, success_callback_count_); | 626 EXPECT_EQ(0U, success_callback_count_); |
| 567 EXPECT_EQ(2U, error_callback_count_); | 627 EXPECT_EQ(2U, error_callback_count_); |
| 568 EXPECT_TRUE(last_socket_.get() == nullptr); | 628 EXPECT_TRUE(last_socket_.get() == nullptr); |
| 569 } | 629 } |
| 570 | 630 |
| 571 TEST_F(BluetoothSocketBlueZTest, SocketListenTwice) { | 631 TEST_F(BluetoothSocketBlueZTest, SocketListenTwice) { |
| 572 adapter_->CreateRfcommService( | 632 { |
| 573 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 633 base::RunLoop run_loop; |
| 574 BluetoothAdapter::ServiceOptions(), | 634 adapter_->CreateRfcommService( |
| 575 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | 635 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 576 base::Unretained(this)), | 636 BluetoothAdapter::ServiceOptions(), |
| 577 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 637 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, |
| 578 base::Unretained(this))); | 638 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 579 | 639 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 580 base::RunLoop().Run(); | 640 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 641 run_loop.Run(); |
| 642 } |
| 581 | 643 |
| 582 EXPECT_EQ(1U, success_callback_count_); | 644 EXPECT_EQ(1U, success_callback_count_); |
| 583 EXPECT_EQ(0U, error_callback_count_); | 645 EXPECT_EQ(0U, error_callback_count_); |
| 584 EXPECT_TRUE(last_socket_.get() != nullptr); | 646 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 585 | 647 |
| 586 // Take control of this socket. | 648 // Take control of this socket. |
| 587 scoped_refptr<BluetoothSocket> server_socket; | 649 scoped_refptr<BluetoothSocket> server_socket; |
| 588 server_socket.swap(last_socket_); | 650 server_socket.swap(last_socket_); |
| 589 | 651 |
| 590 server_socket->Accept( | 652 { |
| 591 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, | 653 base::RunLoop run_loop; |
| 592 base::Unretained(this)), | 654 server_socket->Accept( |
| 593 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 655 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, |
| 594 base::Unretained(this))); | 656 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 657 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 658 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 595 | 659 |
| 596 server_socket->Close(); | 660 server_socket->Close(); |
| 597 | 661 |
| 598 server_socket = nullptr; | 662 server_socket = nullptr; |
| 599 | 663 run_loop.RunUntilIdle(); |
| 600 base::RunLoop().RunUntilIdle(); | 664 } |
| 601 | 665 |
| 602 EXPECT_EQ(1U, success_callback_count_); | 666 EXPECT_EQ(1U, success_callback_count_); |
| 603 EXPECT_EQ(1U, error_callback_count_); | 667 EXPECT_EQ(1U, error_callback_count_); |
| 604 | 668 |
| 605 adapter_->CreateRfcommService( | 669 { |
| 606 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), | 670 base::RunLoop run_loop; |
| 607 BluetoothAdapter::ServiceOptions(), | 671 adapter_->CreateRfcommService( |
| 608 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, | 672 BluetoothUUID(bluez::FakeBluetoothProfileManagerClient::kRfcommUuid), |
| 609 base::Unretained(this)), | 673 BluetoothAdapter::ServiceOptions(), |
| 610 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 674 base::Bind(&BluetoothSocketBlueZTest::CreateServiceSuccessCallback, |
| 611 base::Unretained(this))); | 675 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 612 | 676 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 613 base::RunLoop().Run(); | 677 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 678 run_loop.Run(); |
| 679 } |
| 614 | 680 |
| 615 EXPECT_EQ(2U, success_callback_count_); | 681 EXPECT_EQ(2U, success_callback_count_); |
| 616 EXPECT_EQ(1U, error_callback_count_); | 682 EXPECT_EQ(1U, error_callback_count_); |
| 617 EXPECT_TRUE(last_socket_.get() != nullptr); | 683 EXPECT_TRUE(last_socket_.get() != nullptr); |
| 618 | 684 |
| 619 // Take control of this socket. | 685 // Take control of this socket. |
| 620 server_socket.swap(last_socket_); | 686 server_socket.swap(last_socket_); |
| 621 | 687 |
| 622 server_socket->Accept( | 688 { |
| 623 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, | 689 base::RunLoop run_loop; |
| 624 base::Unretained(this)), | 690 server_socket->Accept( |
| 625 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, | 691 base::Bind(&BluetoothSocketBlueZTest::AcceptSuccessCallback, |
| 626 base::Unretained(this))); | 692 base::Unretained(this), run_loop.QuitWhenIdleClosure()), |
| 693 base::Bind(&BluetoothSocketBlueZTest::ErrorCallback, |
| 694 base::Unretained(this), run_loop.QuitWhenIdleClosure())); |
| 627 | 695 |
| 628 server_socket->Close(); | 696 server_socket->Close(); |
| 629 | 697 |
| 630 server_socket = nullptr; | 698 server_socket = nullptr; |
| 631 | 699 run_loop.RunUntilIdle(); |
| 632 base::RunLoop().RunUntilIdle(); | 700 } |
| 633 | 701 |
| 634 EXPECT_EQ(2U, success_callback_count_); | 702 EXPECT_EQ(2U, success_callback_count_); |
| 635 EXPECT_EQ(2U, error_callback_count_); | 703 EXPECT_EQ(2U, error_callback_count_); |
| 636 } | 704 } |
| 637 | 705 |
| 638 } // namespace bluez | 706 } // namespace bluez |
| OLD | NEW |