OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
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" | |
19 | |
20 using device::BluetoothAdapter; | |
21 using device::BluetoothUUID; | |
22 | |
23 namespace { | |
24 | |
25 void DoNothingDBusErrorCallback(const std::string& error_name, | |
26 const std::string& error_message) { | |
27 } | |
28 | |
29 } // namespace | |
30 | |
31 namespace chromeos { | |
32 | |
33 class BluetoothAdapterProfileChromeOSTest : public testing::Test { | |
34 public: | |
35 BluetoothAdapterProfileChromeOSTest() | |
36 : fake_delegate_paired_(FakeBluetoothDeviceClient::kPairedDevicePath), | |
37 fake_delegate_autopair_(FakeBluetoothDeviceClient::kLegacyAutopairPath), | |
38 fake_delegate_listen_(""), | |
39 profile_(NULL), | |
armansito
2015/01/29 04:44:18
nit: Use nullptr instead NULL whenever you can.
| |
40 success_callback_count_(0), | |
41 error_callback_count_(0) {} | |
42 | |
43 void SetUp() override { | |
44 scoped_ptr<DBusThreadManagerSetter> dbus_setter = | |
45 DBusThreadManager::GetSetterForTesting(); | |
46 | |
47 dbus_setter->SetBluetoothAdapterClient( | |
48 scoped_ptr<BluetoothAdapterClient>(new FakeBluetoothAdapterClient)); | |
49 dbus_setter->SetBluetoothAgentManagerClient( | |
50 scoped_ptr<BluetoothAgentManagerClient>( | |
51 new FakeBluetoothAgentManagerClient)); | |
52 dbus_setter->SetBluetoothDeviceClient( | |
53 scoped_ptr<BluetoothDeviceClient>(new FakeBluetoothDeviceClient)); | |
54 dbus_setter->SetBluetoothProfileManagerClient( | |
55 scoped_ptr<BluetoothProfileManagerClient>( | |
56 new FakeBluetoothProfileManagerClient)); | |
57 | |
58 // Grab a pointer to the adapter. | |
59 device::BluetoothAdapterFactory::GetAdapter( | |
60 base::Bind(&BluetoothAdapterProfileChromeOSTest::AdapterCallback, | |
61 base::Unretained(this))); | |
62 ASSERT_TRUE(adapter_.get() != NULL); | |
63 ASSERT_TRUE(adapter_->IsInitialized()); | |
64 ASSERT_TRUE(adapter_->IsPresent()); | |
65 | |
66 // Turn on the adapter. | |
67 adapter_->SetPowered(true, base::Bind(&base::DoNothing), | |
68 base::Bind(&base::DoNothing)); | |
69 ASSERT_TRUE(adapter_->IsPowered()); | |
70 } | |
71 | |
72 void TearDown() override { | |
73 adapter_ = NULL; | |
74 DBusThreadManager::Shutdown(); | |
75 } | |
76 | |
77 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { | |
78 adapter_ = adapter; | |
79 } | |
80 | |
81 class FakeDelegate | |
82 : public chromeos::BluetoothProfileServiceProvider::Delegate { | |
83 public: | |
84 FakeDelegate(std::string device_path) : connections_(0) { | |
85 device_path_ = dbus::ObjectPath(device_path); | |
86 } | |
87 | |
88 // BluetoothProfileServiceProvider::Delegate: | |
89 void Released() override { | |
90 // noop | |
91 } | |
92 | |
93 void NewConnection( | |
94 const dbus::ObjectPath& device_path, | |
95 scoped_ptr<dbus::FileDescriptor> fd, | |
96 const BluetoothProfileServiceProvider::Delegate::Options& options, | |
97 const ConfirmationCallback& callback) override { | |
98 VLOG(1) << "connection for " << device_path.value() << " on " | |
99 << device_path_.value(); | |
100 ++connections_; | |
101 fd->CheckValidity(); | |
102 close(fd->TakeValue()); | |
103 callback.Run(SUCCESS); | |
104 if (device_path_.value() != "") | |
105 ASSERT_TRUE(device_path == device_path_); | |
106 } | |
107 | |
108 void RequestDisconnection(const dbus::ObjectPath& device_path, | |
109 const ConfirmationCallback& callback) override { | |
110 VLOG(1) << "disconnect " << device_path.value(); | |
111 ++disconnections_; | |
112 } | |
113 | |
114 void Cancel() override { | |
115 VLOG(1) << "cancel"; | |
116 // noop | |
117 } | |
118 | |
119 unsigned int connections_; | |
120 unsigned int disconnections_; | |
121 dbus::ObjectPath device_path_; | |
122 }; | |
123 | |
124 FakeDelegate fake_delegate_paired_; | |
125 FakeDelegate fake_delegate_autopair_; | |
126 FakeDelegate fake_delegate_listen_; | |
127 | |
128 BluetoothAdapterProfileChromeOS* profile_; | |
129 | |
130 void DBusConnectSuccessCallback() { ++success_callback_count_; } | |
131 | |
132 void DBusErrorCallback(const std::string& error_name, | |
133 const std::string& error_message) { | |
134 VLOG(1) << "DBus Connect Error: " << error_name << " - " << error_message; | |
135 ++error_callback_count_; | |
136 } | |
137 | |
138 protected: | |
139 base::MessageLoop message_loop_; | |
140 | |
141 scoped_refptr<BluetoothAdapter> adapter_; | |
142 | |
143 unsigned int success_callback_count_; | |
144 unsigned int error_callback_count_; | |
145 }; | |
146 | |
147 TEST_F(BluetoothAdapterProfileChromeOSTest, DelegateCount) { | |
148 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid); | |
149 BluetoothProfileManagerClient::Options options; | |
150 | |
151 options.require_authentication.reset(new bool(false)); | |
152 | |
153 profile_ = BluetoothAdapterProfileChromeOS::Register( | |
154 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()), uuid, options, | |
155 base::Bind(&base::DoNothing), base::Bind(&DoNothingDBusErrorCallback)); | |
156 | |
157 message_loop_.RunUntilIdle(); | |
158 | |
159 EXPECT_TRUE(profile_); | |
160 | |
161 EXPECT_EQ(0U, profile_->DelegateCount()); | |
162 | |
163 profile_->SetDelegate(fake_delegate_paired_.device_path_, | |
164 &fake_delegate_paired_); | |
165 | |
166 EXPECT_EQ(1U, profile_->DelegateCount()); | |
167 | |
168 profile_->RemoveDelegate(fake_delegate_autopair_.device_path_, | |
169 base::Bind(&base::DoNothing)); | |
170 | |
171 EXPECT_EQ(1U, profile_->DelegateCount()); | |
172 | |
173 profile_->RemoveDelegate(fake_delegate_paired_.device_path_, | |
174 base::Bind(&base::DoNothing)); | |
175 | |
176 EXPECT_EQ(0U, profile_->DelegateCount()); | |
177 | |
178 delete profile_; | |
179 }; | |
180 | |
181 TEST_F(BluetoothAdapterProfileChromeOSTest, BlackHole) { | |
182 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid); | |
183 BluetoothProfileManagerClient::Options options; | |
184 | |
185 options.require_authentication.reset(new bool(false)); | |
186 | |
187 profile_ = BluetoothAdapterProfileChromeOS::Register( | |
188 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()), uuid, options, | |
189 base::Bind( | |
190 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback, | |
191 base::Unretained(this)), | |
192 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
193 base::Unretained(this))); | |
194 | |
195 message_loop_.RunUntilIdle(); | |
196 | |
197 EXPECT_TRUE(profile_); | |
198 EXPECT_EQ(1U, success_callback_count_); | |
199 EXPECT_EQ(0U, error_callback_count_); | |
200 | |
201 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
202 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath), | |
203 FakeBluetoothProfileManagerClient::kRfcommUuid, | |
204 base::Bind( | |
205 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback, | |
206 base::Unretained(this)), | |
207 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
208 base::Unretained(this))); | |
209 | |
210 message_loop_.RunUntilIdle(); | |
211 | |
212 EXPECT_EQ(1U, success_callback_count_); | |
213 EXPECT_EQ(1U, error_callback_count_); | |
214 | |
215 EXPECT_EQ(0U, fake_delegate_paired_.connections_); | |
216 | |
217 delete profile_; | |
218 }; | |
219 | |
220 TEST_F(BluetoothAdapterProfileChromeOSTest, Routing) { | |
221 BluetoothUUID uuid(FakeBluetoothProfileManagerClient::kRfcommUuid); | |
222 BluetoothProfileManagerClient::Options options; | |
223 | |
224 options.require_authentication.reset(new bool(false)); | |
225 | |
226 profile_ = BluetoothAdapterProfileChromeOS::Register( | |
227 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()), uuid, options, | |
228 base::Bind( | |
229 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback, | |
230 base::Unretained(this)), | |
231 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
232 base::Unretained(this))); | |
233 | |
234 message_loop_.RunUntilIdle(); | |
235 | |
236 ASSERT_TRUE(profile_); | |
237 | |
238 profile_->SetDelegate(fake_delegate_paired_.device_path_, | |
239 &fake_delegate_paired_); | |
240 profile_->SetDelegate(fake_delegate_autopair_.device_path_, | |
241 &fake_delegate_autopair_); | |
242 profile_->SetDelegate(fake_delegate_listen_.device_path_, | |
243 &fake_delegate_listen_); | |
244 | |
245 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
246 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath), | |
247 FakeBluetoothProfileManagerClient::kRfcommUuid, | |
248 base::Bind( | |
249 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback, | |
250 base::Unretained(this)), | |
251 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
252 base::Unretained(this))); | |
253 | |
254 message_loop_.RunUntilIdle(); | |
255 | |
256 EXPECT_EQ(2U, success_callback_count_); | |
257 EXPECT_EQ(0U, error_callback_count_); | |
258 | |
259 EXPECT_EQ(1U, fake_delegate_paired_.connections_); | |
260 | |
261 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
262 dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath), | |
263 FakeBluetoothProfileManagerClient::kRfcommUuid, | |
264 base::Bind( | |
265 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback, | |
266 base::Unretained(this)), | |
267 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
268 base::Unretained(this))); | |
269 | |
270 message_loop_.RunUntilIdle(); | |
271 | |
272 EXPECT_EQ(3U, success_callback_count_); | |
273 EXPECT_EQ(0U, error_callback_count_); | |
274 | |
275 EXPECT_EQ(1U, fake_delegate_autopair_.connections_); | |
276 | |
277 // Incoming connections look the same from BlueZ. | |
278 DBusThreadManager::Get()->GetBluetoothDeviceClient()->ConnectProfile( | |
279 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath), | |
280 FakeBluetoothProfileManagerClient::kRfcommUuid, | |
281 base::Bind( | |
282 &BluetoothAdapterProfileChromeOSTest::DBusConnectSuccessCallback, | |
283 base::Unretained(this)), | |
284 base::Bind(&BluetoothAdapterProfileChromeOSTest::DBusErrorCallback, | |
285 base::Unretained(this))); | |
286 | |
287 message_loop_.RunUntilIdle(); | |
288 | |
289 EXPECT_EQ(4U, success_callback_count_); | |
290 EXPECT_EQ(0U, error_callback_count_); | |
291 | |
292 EXPECT_EQ(1U, fake_delegate_listen_.connections_); | |
293 | |
294 delete profile_; | |
295 }; | |
296 | |
297 } // namespace chromeos | |
OLD | NEW |