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