OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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/message_loop/message_loop.h" | |
6 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" | |
7 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h" | |
8 #include "chromeos/dbus/fake_bluetooth_device_client.h" | |
9 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h" | |
10 #include "chromeos/dbus/fake_bluetooth_input_client.h" | |
11 #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h" | |
12 #include "chromeos/dbus/fake_bluetooth_profile_service_provider.h" | |
13 #include "chromeos/dbus/fake_dbus_thread_manager.h" | |
14 #include "device/bluetooth/bluetooth_adapter.h" | |
15 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | |
16 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
17 #include "device/bluetooth/bluetooth_device.h" | |
18 #include "device/bluetooth/bluetooth_device_chromeos.h" | |
19 #include "device/bluetooth/bluetooth_profile.h" | |
20 #include "device/bluetooth/bluetooth_profile_chromeos.h" | |
21 #include "device/bluetooth/bluetooth_socket.h" | |
22 #include "device/bluetooth/bluetooth_socket_chromeos.h" | |
23 #include "device/bluetooth/bluetooth_uuid.h" | |
24 #include "net/base/io_buffer.h" | |
25 #include "testing/gtest/include/gtest/gtest.h" | |
26 | |
27 using device::BluetoothAdapter; | |
28 using device::BluetoothDevice; | |
29 using device::BluetoothProfile; | |
30 using device::BluetoothSocket; | |
31 using device::BluetoothUUID; | |
32 | |
33 namespace chromeos { | |
34 | |
35 class BluetoothProfileChromeOSTest : public testing::Test { | |
36 public: | |
37 BluetoothProfileChromeOSTest() | |
38 : callback_count_(0), | |
39 error_callback_count_(0), | |
40 profile_callback_count_(0), | |
41 connection_callback_count_(0), | |
42 last_profile_(NULL), | |
43 last_device_(NULL) {} | |
44 | |
45 virtual void SetUp() { | |
46 FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager; | |
47 fake_bluetooth_profile_manager_client_ = | |
48 new FakeBluetoothProfileManagerClient; | |
49 fake_dbus_thread_manager->SetBluetoothProfileManagerClient( | |
50 scoped_ptr<BluetoothProfileManagerClient>( | |
51 fake_bluetooth_profile_manager_client_)); | |
52 fake_dbus_thread_manager->SetBluetoothAgentManagerClient( | |
53 scoped_ptr<BluetoothAgentManagerClient>( | |
54 new FakeBluetoothAgentManagerClient)); | |
55 fake_dbus_thread_manager->SetBluetoothAdapterClient( | |
56 scoped_ptr<BluetoothAdapterClient>(new FakeBluetoothAdapterClient)); | |
57 fake_dbus_thread_manager->SetBluetoothDeviceClient( | |
58 scoped_ptr<BluetoothDeviceClient>(new FakeBluetoothDeviceClient)); | |
59 fake_dbus_thread_manager->SetBluetoothInputClient( | |
60 scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient)); | |
61 fake_dbus_thread_manager->SetBluetoothGattServiceClient( | |
62 scoped_ptr<BluetoothGattServiceClient>( | |
63 new FakeBluetoothGattServiceClient)); | |
64 DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager); | |
65 | |
66 device::BluetoothAdapterFactory::GetAdapter( | |
67 base::Bind(&BluetoothProfileChromeOSTest::AdapterCallback, | |
68 base::Unretained(this))); | |
69 ASSERT_TRUE(adapter_.get() != NULL); | |
70 ASSERT_TRUE(adapter_->IsInitialized()); | |
71 ASSERT_TRUE(adapter_->IsPresent()); | |
72 | |
73 adapter_->SetPowered( | |
74 true, | |
75 base::Bind(&base::DoNothing), | |
76 base::Bind(&base::DoNothing)); | |
77 ASSERT_TRUE(adapter_->IsPowered()); | |
78 } | |
79 | |
80 virtual void TearDown() { | |
81 adapter_ = NULL; | |
82 DBusThreadManager::Shutdown(); | |
83 } | |
84 | |
85 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) { | |
86 adapter_ = adapter; | |
87 } | |
88 | |
89 void Callback() { | |
90 ++callback_count_; | |
91 } | |
92 | |
93 void ErrorCallback() { | |
94 ++error_callback_count_; | |
95 | |
96 message_loop_.Quit(); | |
97 } | |
98 | |
99 void ConnectToProfileErrorCallback(const std::string& error) { | |
100 ErrorCallback(); | |
101 } | |
102 | |
103 void ProfileCallback(BluetoothProfile* profile) { | |
104 ++profile_callback_count_; | |
105 last_profile_ = profile; | |
106 } | |
107 | |
108 void ConnectionCallback(const BluetoothDevice *device, | |
109 scoped_refptr<BluetoothSocket> socket) { | |
110 ++connection_callback_count_; | |
111 last_device_ = device; | |
112 last_socket_ = socket; | |
113 | |
114 message_loop_.Quit(); | |
115 } | |
116 | |
117 protected: | |
118 base::MessageLoopForIO message_loop_; | |
119 | |
120 FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client_; | |
121 scoped_refptr<BluetoothAdapter> adapter_; | |
122 | |
123 unsigned int callback_count_; | |
124 unsigned int error_callback_count_; | |
125 unsigned int profile_callback_count_; | |
126 unsigned int connection_callback_count_; | |
127 BluetoothProfile* last_profile_; | |
128 const BluetoothDevice* last_device_; | |
129 scoped_refptr<BluetoothSocket> last_socket_; | |
130 }; | |
131 | |
132 TEST_F(BluetoothProfileChromeOSTest, L2capEndToEnd) { | |
133 // TODO(rpaquay): Implement this test once the ChromeOS implementation of | |
134 // BluetoothSocket is done. | |
135 #if 0 | |
136 // Register the profile and expect the profile object to be passed to the | |
137 // callback. | |
138 BluetoothProfile::Options options; | |
139 BluetoothProfile::Register( | |
140 BluetoothUUID(FakeBluetoothProfileManagerClient::kL2capUuid), | |
141 options, | |
142 base::Bind(&BluetoothProfileChromeOSTest::ProfileCallback, | |
143 base::Unretained(this))); | |
144 | |
145 EXPECT_EQ(1U, profile_callback_count_); | |
146 EXPECT_TRUE(last_profile_ != NULL); | |
147 BluetoothProfile* profile = last_profile_; | |
148 | |
149 // Make sure we have a profile service provider for it. | |
150 FakeBluetoothProfileServiceProvider* profile_service_provider = | |
151 fake_bluetooth_profile_manager_client_->GetProfileServiceProvider( | |
152 FakeBluetoothProfileManagerClient::kL2capUuid); | |
153 EXPECT_TRUE(profile_service_provider != NULL); | |
154 | |
155 // Register the connection callback. | |
156 profile->SetConnectionCallback( | |
157 base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback, | |
158 base::Unretained(this))); | |
159 | |
160 // Connect to the device, expect the success callback to be called and the | |
161 // connection callback to be called with the device we passed and a new | |
162 // socket instance. | |
163 BluetoothDevice* device = adapter_->GetDevice( | |
164 FakeBluetoothDeviceClient::kPairedDeviceAddress); | |
165 ASSERT_TRUE(device != NULL); | |
166 | |
167 device->ConnectToProfile( | |
168 profile, | |
169 base::Bind(&BluetoothProfileChromeOSTest::Callback, | |
170 base::Unretained(this)), | |
171 base::Bind(&BluetoothProfileChromeOSTest::ConnectToProfileErrorCallback, | |
172 base::Unretained(this))); | |
173 | |
174 message_loop_.Run(); | |
175 | |
176 EXPECT_EQ(1U, callback_count_); | |
177 EXPECT_EQ(0U, error_callback_count_); | |
178 | |
179 EXPECT_EQ(1U, connection_callback_count_); | |
180 EXPECT_EQ(device, last_device_); | |
181 EXPECT_TRUE(last_socket_.get() != NULL); | |
182 | |
183 // Take the ownership of the socket for the remainder of the test and set | |
184 // up buffers for read/write tests. | |
185 scoped_refptr<BluetoothSocket> socket = last_socket_; | |
186 last_socket_ = NULL; | |
187 | |
188 bool success; | |
189 scoped_refptr<net::GrowableIOBuffer> read_buffer; | |
190 | |
191 scoped_refptr<net::StringIOBuffer> base_buffer( | |
192 new net::StringIOBuffer("test")); | |
193 scoped_refptr<net::DrainableIOBuffer> write_buffer; | |
194 | |
195 // Read data from the socket; since no data should be waiting, this should | |
196 // return success but no data. | |
197 read_buffer = new net::GrowableIOBuffer; | |
198 success = socket->Receive(read_buffer.get()); | |
199 EXPECT_TRUE(success); | |
200 EXPECT_EQ(0, read_buffer->capacity()); | |
201 EXPECT_EQ(0, read_buffer->offset()); | |
202 EXPECT_EQ("", socket->GetLastErrorMessage()); | |
203 | |
204 // Write data to the socket; the data should be consumed and no bytes should | |
205 // be remaining. | |
206 write_buffer = | |
207 new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size()); | |
208 success = socket->Send(write_buffer.get()); | |
209 EXPECT_TRUE(success); | |
210 EXPECT_EQ(base_buffer->size(), write_buffer->BytesConsumed()); | |
211 EXPECT_EQ(0, write_buffer->BytesRemaining()); | |
212 EXPECT_EQ("", socket->GetLastErrorMessage()); | |
213 | |
214 // Read data from the socket; this should match the data we sent since the | |
215 // server just echoes us. We have to spin here until there is actually data | |
216 // to read. | |
217 read_buffer = new net::GrowableIOBuffer; | |
218 do { | |
219 success = socket->Receive(read_buffer.get()); | |
220 } while (success && read_buffer->offset() == 0); | |
221 EXPECT_TRUE(success); | |
222 EXPECT_NE(0, read_buffer->capacity()); | |
223 EXPECT_EQ(base_buffer->size(), read_buffer->offset()); | |
224 EXPECT_EQ("", socket->GetLastErrorMessage()); | |
225 | |
226 std::string data = std::string(read_buffer->StartOfBuffer(), | |
227 read_buffer->offset()); | |
228 EXPECT_EQ("test", data); | |
229 | |
230 // Write data to the socket; since the socket is closed, this should return | |
231 // an error without writing the data and "Disconnected" as the message. | |
232 write_buffer = | |
233 new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size()); | |
234 success = socket->Send(write_buffer.get()); | |
235 EXPECT_FALSE(success); | |
236 EXPECT_EQ(0, write_buffer->BytesConsumed()); | |
237 EXPECT_EQ(base_buffer->size(), write_buffer->BytesRemaining()); | |
238 EXPECT_EQ("Disconnected", socket->GetLastErrorMessage()); | |
239 | |
240 // Read data from the socket; since the socket is closed, this should return | |
241 // an error with "Disconnected" as the last message. | |
242 read_buffer = new net::GrowableIOBuffer; | |
243 success = socket->Receive(read_buffer.get()); | |
244 EXPECT_FALSE(success); | |
245 EXPECT_EQ(0, read_buffer->capacity()); | |
246 EXPECT_EQ(0, read_buffer->offset()); | |
247 EXPECT_EQ("Disconnected", socket->GetLastErrorMessage()); | |
248 | |
249 // Close our end of the socket. | |
250 socket = NULL; | |
251 | |
252 // Unregister the profile, make sure it's no longer registered. | |
253 last_profile_->Unregister(); | |
254 | |
255 profile_service_provider = | |
256 fake_bluetooth_profile_manager_client_->GetProfileServiceProvider( | |
257 FakeBluetoothProfileManagerClient::kL2capUuid); | |
258 EXPECT_TRUE(profile_service_provider == NULL); | |
259 #endif | |
260 } | |
261 | |
262 TEST_F(BluetoothProfileChromeOSTest, RfcommEndToEnd) { | |
263 // TODO(rpaquay): Implement this test once the ChromeOS implementation of | |
264 // BluetoothSocket is done. | |
265 #if 0 | |
266 // Register the profile and expect the profile object to be passed to the | |
267 // callback. | |
268 BluetoothProfile::Options options; | |
269 BluetoothProfile::Register( | |
270 BluetoothUUID(FakeBluetoothProfileManagerClient::kRfcommUuid), | |
271 options, | |
272 base::Bind(&BluetoothProfileChromeOSTest::ProfileCallback, | |
273 base::Unretained(this))); | |
274 | |
275 EXPECT_EQ(1U, profile_callback_count_); | |
276 EXPECT_TRUE(last_profile_ != NULL); | |
277 BluetoothProfile* profile = last_profile_; | |
278 | |
279 // Make sure we have a profile service provider for it. | |
280 FakeBluetoothProfileServiceProvider* profile_service_provider = | |
281 fake_bluetooth_profile_manager_client_->GetProfileServiceProvider( | |
282 FakeBluetoothProfileManagerClient::kRfcommUuid); | |
283 EXPECT_TRUE(profile_service_provider != NULL); | |
284 | |
285 // Register the connection callback. | |
286 profile->SetConnectionCallback( | |
287 base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback, | |
288 base::Unretained(this))); | |
289 | |
290 // Connect to the device, expect the success callback to be called and the | |
291 // connection callback to be called with the device we passed and a new | |
292 // socket instance. | |
293 BluetoothDevice* device = adapter_->GetDevice( | |
294 FakeBluetoothDeviceClient::kPairedDeviceAddress); | |
295 ASSERT_TRUE(device != NULL); | |
296 | |
297 device->ConnectToProfile( | |
298 profile, | |
299 base::Bind(&BluetoothProfileChromeOSTest::Callback, | |
300 base::Unretained(this)), | |
301 base::Bind(&BluetoothProfileChromeOSTest::ConnectToProfileErrorCallback, | |
302 base::Unretained(this))); | |
303 | |
304 message_loop_.Run(); | |
305 | |
306 EXPECT_EQ(1U, callback_count_); | |
307 EXPECT_EQ(0U, error_callback_count_); | |
308 | |
309 EXPECT_EQ(1U, connection_callback_count_); | |
310 EXPECT_EQ(device, last_device_); | |
311 EXPECT_TRUE(last_socket_.get() != NULL); | |
312 | |
313 // Take the ownership of the socket for the remainder of the test and set | |
314 // up buffers for read/write tests. | |
315 scoped_refptr<BluetoothSocket> socket = last_socket_; | |
316 last_socket_ = NULL; | |
317 | |
318 bool success; | |
319 scoped_refptr<net::GrowableIOBuffer> read_buffer; | |
320 | |
321 scoped_refptr<net::StringIOBuffer> base_buffer( | |
322 new net::StringIOBuffer("test")); | |
323 scoped_refptr<net::DrainableIOBuffer> write_buffer; | |
324 | |
325 // Read data from the socket; since no data should be waiting, this should | |
326 // return success but no data. | |
327 read_buffer = new net::GrowableIOBuffer; | |
328 success = socket->Receive(read_buffer.get()); | |
329 EXPECT_TRUE(success); | |
330 EXPECT_EQ(0, read_buffer->offset()); | |
331 EXPECT_EQ("", socket->GetLastErrorMessage()); | |
332 | |
333 // Write data to the socket; the data should be consumed and no bytes should | |
334 // be remaining. | |
335 write_buffer = | |
336 new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size()); | |
337 success = socket->Send(write_buffer.get()); | |
338 EXPECT_TRUE(success); | |
339 EXPECT_EQ(base_buffer->size(), write_buffer->BytesConsumed()); | |
340 EXPECT_EQ(0, write_buffer->BytesRemaining()); | |
341 EXPECT_EQ("", socket->GetLastErrorMessage()); | |
342 | |
343 // Read data from the socket; this should match the data we sent since the | |
344 // server just echoes us. We have to spin here until there is actually data | |
345 // to read. | |
346 read_buffer = new net::GrowableIOBuffer; | |
347 do { | |
348 success = socket->Receive(read_buffer.get()); | |
349 } while (success && read_buffer->offset() == 0); | |
350 EXPECT_TRUE(success); | |
351 EXPECT_NE(0, read_buffer->capacity()); | |
352 EXPECT_EQ(base_buffer->size(), read_buffer->offset()); | |
353 EXPECT_EQ("", socket->GetLastErrorMessage()); | |
354 | |
355 std::string data = std::string(read_buffer->StartOfBuffer(), | |
356 read_buffer->offset()); | |
357 EXPECT_EQ("test", data); | |
358 | |
359 // Write data to the socket; since the socket is closed, this should return | |
360 // an error without writing the data and "Disconnected" as the message. | |
361 write_buffer = | |
362 new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size()); | |
363 success = socket->Send(write_buffer.get()); | |
364 EXPECT_FALSE(success); | |
365 EXPECT_EQ(0, write_buffer->BytesConsumed()); | |
366 EXPECT_EQ(base_buffer->size(), write_buffer->BytesRemaining()); | |
367 EXPECT_EQ("Disconnected", socket->GetLastErrorMessage()); | |
368 | |
369 // Read data from the socket; since the socket is closed, this should return | |
370 // an error with "Disconnected" as the last message. | |
371 read_buffer = new net::GrowableIOBuffer; | |
372 success = socket->Receive(read_buffer.get()); | |
373 EXPECT_FALSE(success); | |
374 EXPECT_EQ(0, read_buffer->offset()); | |
375 EXPECT_EQ("Disconnected", socket->GetLastErrorMessage()); | |
376 | |
377 // Close our end of the socket. | |
378 socket = NULL; | |
379 | |
380 // Unregister the profile, make sure it's no longer registered. | |
381 last_profile_->Unregister(); | |
382 | |
383 profile_service_provider = | |
384 fake_bluetooth_profile_manager_client_->GetProfileServiceProvider( | |
385 FakeBluetoothProfileManagerClient::kRfcommUuid); | |
386 EXPECT_TRUE(profile_service_provider == NULL); | |
387 #endif | |
388 } | |
389 | |
390 } // namespace chromeos | |
OLD | NEW |