Add OWNERS to content/browser/quota
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_profile_chromeos_unittest.cc
blobf5b96f817408e39c3c6c51a7dd1d7e7de3c687d6
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
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/message_loop/message_loop.h"
7 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
8 #include "chromeos/dbus/dbus_thread_manager.h"
9 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
10 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
11 #include "chromeos/dbus/fake_bluetooth_device_client.h"
12 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h"
13 #include "device/bluetooth/bluetooth_adapter.h"
14 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
15 #include "device/bluetooth/bluetooth_adapter_factory.h"
16 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h"
17 #include "device/bluetooth/bluetooth_uuid.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using device::BluetoothAdapter;
21 using device::BluetoothUUID;
23 namespace chromeos {
25 class BluetoothAdapterProfileChromeOSTest : public testing::Test {
26 public:
27 BluetoothAdapterProfileChromeOSTest()
28 : success_callback_count_(0),
29 error_callback_count_(0),
30 fake_delegate_paired_(FakeBluetoothDeviceClient::kPairedDevicePath),
31 fake_delegate_autopair_(FakeBluetoothDeviceClient::kLegacyAutopairPath),
32 fake_delegate_listen_(""),
33 profile_user_ptr_(nullptr) {}
35 void SetUp() override {
36 scoped_ptr<DBusThreadManagerSetter> dbus_setter =
37 DBusThreadManager::GetSetterForTesting();
39 dbus_setter->SetBluetoothAdapterClient(
40 scoped_ptr<BluetoothAdapterClient>(new FakeBluetoothAdapterClient));
41 dbus_setter->SetBluetoothAgentManagerClient(
42 scoped_ptr<BluetoothAgentManagerClient>(
43 new FakeBluetoothAgentManagerClient));
44 dbus_setter->SetBluetoothDeviceClient(
45 scoped_ptr<BluetoothDeviceClient>(new FakeBluetoothDeviceClient));
46 dbus_setter->SetBluetoothProfileManagerClient(
47 scoped_ptr<BluetoothProfileManagerClient>(
48 new FakeBluetoothProfileManagerClient));
50 // Grab a pointer to the adapter.
51 device::BluetoothAdapterFactory::GetAdapter(
52 base::Bind(&BluetoothAdapterProfileChromeOSTest::AdapterCallback,
53 base::Unretained(this)));
54 ASSERT_TRUE(adapter_.get() != nullptr);
55 ASSERT_TRUE(adapter_->IsInitialized());
56 ASSERT_TRUE(adapter_->IsPresent());
58 // Turn on the adapter.
59 adapter_->SetPowered(true, base::Bind(&base::DoNothing),
60 base::Bind(&base::DoNothing));
61 ASSERT_TRUE(adapter_->IsPowered());
64 void TearDown() override {
65 profile_.reset();
66 adapter_ = nullptr;
67 DBusThreadManager::Shutdown();
70 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
71 adapter_ = adapter;
74 class FakeDelegate
75 : public chromeos::BluetoothProfileServiceProvider::Delegate {
76 public:
77 FakeDelegate(std::string device_path) : connections_(0) {
78 device_path_ = dbus::ObjectPath(device_path);
81 // BluetoothProfileServiceProvider::Delegate:
82 void Released() override {
83 // noop
86 void NewConnection(
87 const dbus::ObjectPath& device_path,
88 scoped_ptr<dbus::FileDescriptor> fd,
89 const BluetoothProfileServiceProvider::Delegate::Options& options,
90 const ConfirmationCallback& callback) override {
91 ++connections_;
92 fd->CheckValidity();
93 close(fd->TakeValue());
94 callback.Run(SUCCESS);
95 if (device_path_.value() != "")
96 ASSERT_EQ(device_path_, device_path);
99 void RequestDisconnection(const dbus::ObjectPath& device_path,
100 const ConfirmationCallback& callback) override {
101 ++disconnections_;
104 void Cancel() override {
105 // noop
108 unsigned int connections_;
109 unsigned int disconnections_;
110 dbus::ObjectPath device_path_;
113 void ProfileSuccessCallback(
114 scoped_ptr<BluetoothAdapterProfileChromeOS> profile) {
115 profile_.swap(profile);
116 ++success_callback_count_;
119 void ProfileUserSuccessCallback(BluetoothAdapterProfileChromeOS* profile) {
120 profile_user_ptr_ = profile;
121 ++success_callback_count_;
124 void MatchedProfileCallback(BluetoothAdapterProfileChromeOS* profile) {
125 ASSERT_EQ(profile_user_ptr_, profile);
126 ++success_callback_count_;
129 void DBusConnectSuccessCallback() { ++success_callback_count_; }
131 void DBusErrorCallback(const std::string& error_name,
132 const std::string& error_message) {
133 ++error_callback_count_;
136 void BasicErrorCallback(const std::string& error_message) {
137 ++error_callback_count_;
140 protected:
141 base::MessageLoop message_loop_;
143 scoped_refptr<BluetoothAdapter> adapter_;
145 unsigned int success_callback_count_;
146 unsigned int error_callback_count_;
148 FakeDelegate fake_delegate_paired_;
149 FakeDelegate fake_delegate_autopair_;
150 FakeDelegate fake_delegate_listen_;
152 scoped_ptr<BluetoothAdapterProfileChromeOS> profile_;
154 // unowned pointer as expected to be used by clients of
155 // BluetoothAdapterChromeOS::UseProfile like BluetoothSocketChromeOS
156 BluetoothAdapterProfileChromeOS* profile_user_ptr_;
159 TEST_F(BluetoothAdapterProfileChromeOSTest, DelegateCount) {
160 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid);
161 BluetoothProfileManagerClient::Options options;
163 options.require_authentication.reset(new bool(false));
165 BluetoothAdapterProfileChromeOS::Register(
166 uuid, options,
167 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback,
168 base::Unretained(this)),
169 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
170 base::Unretained(this)));
172 message_loop_.RunUntilIdle();
174 EXPECT_TRUE(profile_);
175 EXPECT_EQ(1U, success_callback_count_);
176 EXPECT_EQ(0U, error_callback_count_);
178 EXPECT_EQ(0U, profile_->DelegateCount());
180 profile_->SetDelegate(fake_delegate_paired_.device_path_,
181 &fake_delegate_paired_);
183 EXPECT_EQ(1U, profile_->DelegateCount());
185 profile_->RemoveDelegate(fake_delegate_autopair_.device_path_,
186 base::Bind(&base::DoNothing));
188 EXPECT_EQ(1U, profile_->DelegateCount());
190 profile_->RemoveDelegate(fake_delegate_paired_.device_path_,
191 base::Bind(&base::DoNothing));
193 EXPECT_EQ(0U, profile_->DelegateCount());
196 TEST_F(BluetoothAdapterProfileChromeOSTest, BlackHole) {
197 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid);
198 BluetoothProfileManagerClient::Options options;
200 options.require_authentication.reset(new bool(false));
202 BluetoothAdapterProfileChromeOS::Register(
203 uuid, options,
204 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback,
205 base::Unretained(this)),
206 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
207 base::Unretained(this)));
209 message_loop_.RunUntilIdle();
211 EXPECT_TRUE(profile_);
212 EXPECT_EQ(1U, success_callback_count_);
213 EXPECT_EQ(0U, error_callback_count_);
215 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
216 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath),
217 FakeBluetoothProfileManagerClient::kRfcommUuid,
218 base::Bind(
219 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback,
220 base::Unretained(this)),
221 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
222 base::Unretained(this)));
224 message_loop_.RunUntilIdle();
226 EXPECT_EQ(1U, success_callback_count_);
227 EXPECT_EQ(1U, error_callback_count_);
229 EXPECT_EQ(0U, fake_delegate_paired_.connections_);
232 TEST_F(BluetoothAdapterProfileChromeOSTest, Routing) {
233 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid);
234 BluetoothProfileManagerClient::Options options;
236 options.require_authentication.reset(new bool(false));
238 BluetoothAdapterProfileChromeOS::Register(
239 uuid, options,
240 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback,
241 base::Unretained(this)),
242 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
243 base::Unretained(this)));
245 message_loop_.RunUntilIdle();
247 ASSERT_TRUE(profile_);
248 ASSERT_EQ(1U, success_callback_count_);
249 ASSERT_EQ(0U, error_callback_count_);
251 profile_->SetDelegate(fake_delegate_paired_.device_path_,
252 &fake_delegate_paired_);
253 profile_->SetDelegate(fake_delegate_autopair_.device_path_,
254 &fake_delegate_autopair_);
255 profile_->SetDelegate(fake_delegate_listen_.device_path_,
256 &fake_delegate_listen_);
258 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
259 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath),
260 FakeBluetoothProfileManagerClient::kRfcommUuid,
261 base::Bind(
262 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback,
263 base::Unretained(this)),
264 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
265 base::Unretained(this)));
267 message_loop_.RunUntilIdle();
269 EXPECT_EQ(2U, success_callback_count_);
270 EXPECT_EQ(0U, error_callback_count_);
272 EXPECT_EQ(1U, fake_delegate_paired_.connections_);
274 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
275 dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath),
276 FakeBluetoothProfileManagerClient::kRfcommUuid,
277 base::Bind(
278 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback,
279 base::Unretained(this)),
280 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
281 base::Unretained(this)));
283 message_loop_.RunUntilIdle();
285 EXPECT_EQ(3U, success_callback_count_);
286 EXPECT_EQ(0U, error_callback_count_);
288 EXPECT_EQ(1U, fake_delegate_autopair_.connections_);
290 // Incoming connections look the same from BlueZ.
291 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
292 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath),
293 FakeBluetoothProfileManagerClient::kRfcommUuid,
294 base::Bind(
295 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback,
296 base::Unretained(this)),
297 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
298 base::Unretained(this)));
300 message_loop_.RunUntilIdle();
302 EXPECT_EQ(4U, success_callback_count_);
303 EXPECT_EQ(0U, error_callback_count_);
305 EXPECT_EQ(1U, fake_delegate_listen_.connections_);
308 TEST_F(BluetoothAdapterProfileChromeOSTest, SimultaneousRegister) {
309 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid);
310 BluetoothProfileManagerClient::Options options;
311 BluetoothAdapterChromeOS* adapter =
312 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
314 options.require_authentication.reset(new bool(false));
316 success_callback_count_ = 0;
317 error_callback_count_ = 0;
319 adapter->UseProfile(
320 uuid, fake_delegate_paired_.device_path_, options, &fake_delegate_paired_,
321 base::Bind(
322 &BluetoothAdapterProfileChromeOSTest::ProfileUserSuccessCallback,
323 base::Unretained(this)),
324 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback,
325 base::Unretained(this)));
327 adapter->UseProfile(
328 uuid, fake_delegate_autopair_.device_path_, options,
329 &fake_delegate_autopair_,
330 base::Bind(&BluetoothAdapterProfileChromeOSTest::MatchedProfileCallback,
331 base::Unretained(this)),
332 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback,
333 base::Unretained(this)));
335 message_loop_.RunUntilIdle();
337 EXPECT_TRUE(profile_user_ptr_);
338 EXPECT_EQ(2U, success_callback_count_);
339 EXPECT_EQ(0U, error_callback_count_);
341 adapter->ReleaseProfile(fake_delegate_paired_.device_path_,
342 profile_user_ptr_);
343 adapter->ReleaseProfile(fake_delegate_autopair_.device_path_,
344 profile_user_ptr_);
346 message_loop_.RunUntilIdle();
349 TEST_F(BluetoothAdapterProfileChromeOSTest, SimultaneousRegisterFail) {
350 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kUnregisterableUuid);
351 BluetoothProfileManagerClient::Options options;
352 BluetoothAdapterChromeOS* adapter =
353 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
355 options.require_authentication.reset(new bool(false));
357 success_callback_count_ = 0;
358 error_callback_count_ = 0;
360 adapter->UseProfile(
361 uuid, fake_delegate_paired_.device_path_, options, &fake_delegate_paired_,
362 base::Bind(
363 &BluetoothAdapterProfileChromeOSTest::ProfileUserSuccessCallback,
364 base::Unretained(this)),
365 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback,
366 base::Unretained(this)));
368 adapter->UseProfile(
369 uuid, fake_delegate_autopair_.device_path_, options,
370 &fake_delegate_autopair_,
371 base::Bind(&BluetoothAdapterProfileChromeOSTest::MatchedProfileCallback,
372 base::Unretained(this)),
373 base::Bind(&BluetoothAdapterProfileChromeOSTest::BasicErrorCallback,
374 base::Unretained(this)));
376 message_loop_.RunUntilIdle();
378 EXPECT_FALSE(profile_user_ptr_);
379 EXPECT_EQ(0U, success_callback_count_);
380 EXPECT_EQ(2U, error_callback_count_);
383 } // namespace chromeos