Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Side by Side Diff: device/bluetooth/bluetooth_profile_chromeos_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698