ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_profile_chromeos_unittest.cc
blob42b7aa3379aedabfc1693eb17f17446ab20cdbc1
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 : fake_delegate_paired_(FakeBluetoothDeviceClient::kPairedDevicePath),
29 fake_delegate_autopair_(FakeBluetoothDeviceClient::kLegacyAutopairPath),
30 fake_delegate_listen_(""),
31 profile_(nullptr),
32 success_callback_count_(0),
33 error_callback_count_(0) {}
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 adapter_ = nullptr;
66 DBusThreadManager::Shutdown();
69 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
70 adapter_ = adapter;
73 class FakeDelegate
74 : public chromeos::BluetoothProfileServiceProvider::Delegate {
75 public:
76 FakeDelegate(std::string device_path) : connections_(0) {
77 device_path_ = dbus::ObjectPath(device_path);
80 // BluetoothProfileServiceProvider::Delegate:
81 void Released() override {
82 // noop
85 void NewConnection(
86 const dbus::ObjectPath& device_path,
87 scoped_ptr<dbus::FileDescriptor> fd,
88 const BluetoothProfileServiceProvider::Delegate::Options& options,
89 const ConfirmationCallback& callback) override {
90 VLOG(1) << "connection for " << device_path.value() << " on "
91 << device_path_.value();
92 ++connections_;
93 fd->CheckValidity();
94 close(fd->TakeValue());
95 callback.Run(SUCCESS);
96 if (device_path_.value() != "")
97 ASSERT_EQ(device_path_, device_path);
100 void RequestDisconnection(const dbus::ObjectPath& device_path,
101 const ConfirmationCallback& callback) override {
102 VLOG(1) << "disconnect " << device_path.value();
103 ++disconnections_;
106 void Cancel() override {
107 VLOG(1) << "cancel";
108 // noop
111 unsigned int connections_;
112 unsigned int disconnections_;
113 dbus::ObjectPath device_path_;
116 FakeDelegate fake_delegate_paired_;
117 FakeDelegate fake_delegate_autopair_;
118 FakeDelegate fake_delegate_listen_;
120 BluetoothAdapterProfileChromeOS* profile_;
122 void ProfileSuccessCallback(BluetoothAdapterProfileChromeOS* profile) {
123 profile_ = profile;
124 ++success_callback_count_;
127 void MatchedProfileCallback(BluetoothAdapterProfileChromeOS* profile) {
128 VLOG(1) << "Matched Profile Callback";
129 ASSERT_EQ(profile_, profile);
130 ++success_callback_count_;
133 void DBusConnectSuccessCallback() { ++success_callback_count_; }
135 void DBusErrorCallback(const std::string& error_name,
136 const std::string& error_message) {
137 VLOG(1) << "DBus Connect Error: " << error_name << " - " << error_message;
138 ++error_callback_count_;
141 void BasicErrorCallback(const std::string& error_message) {
142 VLOG(1) << "Error: " << error_message;
143 ++error_callback_count_;
146 protected:
147 base::MessageLoop message_loop_;
149 scoped_refptr<BluetoothAdapter> adapter_;
151 unsigned int success_callback_count_;
152 unsigned int error_callback_count_;
155 TEST_F(BluetoothAdapterProfileChromeOSTest, DelegateCount) {
156 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid);
157 BluetoothProfileManagerClient::Options options;
159 options.require_authentication.reset(new bool(false));
161 BluetoothAdapterProfileChromeOS::Register(
162 uuid, options,
163 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback,
164 base::Unretained(this)),
165 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
166 base::Unretained(this)));
168 message_loop_.RunUntilIdle();
170 EXPECT_TRUE(profile_);
171 EXPECT_EQ(1U, success_callback_count_);
172 EXPECT_EQ(0U, error_callback_count_);
174 EXPECT_EQ(0U, profile_->DelegateCount());
176 profile_->SetDelegate(fake_delegate_paired_.device_path_,
177 &fake_delegate_paired_);
179 EXPECT_EQ(1U, profile_->DelegateCount());
181 profile_->RemoveDelegate(fake_delegate_autopair_.device_path_,
182 base::Bind(&base::DoNothing));
184 EXPECT_EQ(1U, profile_->DelegateCount());
186 profile_->RemoveDelegate(fake_delegate_paired_.device_path_,
187 base::Bind(&base::DoNothing));
189 EXPECT_EQ(0U, profile_->DelegateCount());
191 delete profile_;
194 TEST_F(BluetoothAdapterProfileChromeOSTest, BlackHole) {
195 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid);
196 BluetoothProfileManagerClient::Options options;
198 options.require_authentication.reset(new bool(false));
200 BluetoothAdapterProfileChromeOS::Register(
201 uuid, options,
202 base::Bind(&BluetoothAdapterProfileChromeOSTest::ProfileSuccessCallback,
203 base::Unretained(this)),
204 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
205 base::Unretained(this)));
207 message_loop_.RunUntilIdle();
209 EXPECT_TRUE(profile_);
210 EXPECT_EQ(1U, success_callback_count_);
211 EXPECT_EQ(0U, error_callback_count_);
213 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile(
214 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath),
215 FakeBluetoothProfileManagerClient::kRfcommUuid,
216 base::Bind(
217 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback,
218 base::Unretained(this)),
219 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback,
220 base::Unretained(this)));
222 message_loop_.RunUntilIdle();
224 EXPECT_EQ(1U, success_callback_count_);
225 EXPECT_EQ(1U, error_callback_count_);
227 EXPECT_EQ(0U, fake_delegate_paired_.connections_);
229 delete profile_;
232 TEST_F(BluetoothAdapterProfileChromeOSTest, Routing) {
233 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid);
234 BluetoothProfileManagerClient::Options options;
236 options.require_authentication.reset(new bool(false));
238 profile_ = 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_);
307 delete profile_;
310 TEST_F(BluetoothAdapterProfileChromeOSTest, SimultaneousRegister) {
311 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid);
312 BluetoothProfileManagerClient::Options options;
313 BluetoothAdapterChromeOS* adapter =
314 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
316 options.require_authentication.reset(new bool(false));
318 success_callback_count_ = 0;
319 error_callback_count_ = 0;
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)));
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)));
336 message_loop_.RunUntilIdle();
338 EXPECT_TRUE(profile_);
339 EXPECT_EQ(2U, success_callback_count_);
340 EXPECT_EQ(0U, error_callback_count_);
342 adapter->ReleaseProfile(fake_delegate_paired_.device_path_, profile_);
343 adapter->ReleaseProfile(fake_delegate_autopair_.device_path_, profile_);
345 message_loop_.RunUntilIdle();
348 TEST_F(BluetoothAdapterProfileChromeOSTest, SimultaneousRegisterFail) {
349 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kUnregisterableUuid);
350 BluetoothProfileManagerClient::Options options;
351 BluetoothAdapterChromeOS* adapter =
352 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
354 options.require_authentication.reset(new bool(false));
356 success_callback_count_ = 0;
357 error_callback_count_ = 0;
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)));
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)));
374 message_loop_.RunUntilIdle();
376 EXPECT_FALSE(profile_);
377 EXPECT_EQ(0U, success_callback_count_);
378 EXPECT_EQ(2U, error_callback_count_);
381 } // namespace chromeos