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