Implement nacl_irt_memory for non-sfi mode.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_profile_chromeos_unittest.cc
blobdd7c9457adb61d391574f77aaae7a20800e40a37
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.
5 #include "base/message_loop/message_loop.h"
6 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
7 #include "chromeos/dbus/fake_bluetooth_device_client.h"
8 #include "chromeos/dbus/fake_bluetooth_input_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/fake_dbus_thread_manager.h"
12 #include "device/bluetooth/bluetooth_adapter.h"
13 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
14 #include "device/bluetooth/bluetooth_adapter_factory.h"
15 #include "device/bluetooth/bluetooth_device.h"
16 #include "device/bluetooth/bluetooth_device_chromeos.h"
17 #include "device/bluetooth/bluetooth_profile.h"
18 #include "device/bluetooth/bluetooth_profile_chromeos.h"
19 #include "device/bluetooth/bluetooth_socket.h"
20 #include "device/bluetooth/bluetooth_socket_chromeos.h"
21 #include "net/base/io_buffer.h"
22 #include "testing/gtest/include/gtest/gtest.h"
24 using device::BluetoothAdapter;
25 using device::BluetoothDevice;
26 using device::BluetoothProfile;
27 using device::BluetoothSocket;
29 namespace chromeos {
31 class BluetoothProfileChromeOSTest : public testing::Test {
32 public:
33 BluetoothProfileChromeOSTest()
34 : callback_count_(0),
35 error_callback_count_(0),
36 profile_callback_count_(0),
37 connection_callback_count_(0),
38 last_profile_(NULL),
39 last_device_(NULL) {}
41 virtual void SetUp() {
42 FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager;
43 fake_bluetooth_profile_manager_client_ =
44 new FakeBluetoothProfileManagerClient;
45 fake_dbus_thread_manager->SetBluetoothProfileManagerClient(
46 scoped_ptr<BluetoothProfileManagerClient>(
47 fake_bluetooth_profile_manager_client_));
48 fake_dbus_thread_manager->SetBluetoothAdapterClient(
49 scoped_ptr<BluetoothAdapterClient>(new FakeBluetoothAdapterClient));
50 fake_dbus_thread_manager->SetBluetoothDeviceClient(
51 scoped_ptr<BluetoothDeviceClient>(new FakeBluetoothDeviceClient));
52 fake_dbus_thread_manager->SetBluetoothInputClient(
53 scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
54 DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager);
56 device::BluetoothAdapterFactory::GetAdapter(
57 base::Bind(&BluetoothProfileChromeOSTest::AdapterCallback,
58 base::Unretained(this)));
59 ASSERT_TRUE(adapter_.get() != NULL);
60 ASSERT_TRUE(adapter_->IsInitialized());
61 ASSERT_TRUE(adapter_->IsPresent());
63 adapter_->SetPowered(
64 true,
65 base::Bind(&base::DoNothing),
66 base::Bind(&base::DoNothing));
67 ASSERT_TRUE(adapter_->IsPowered());
70 virtual void TearDown() {
71 adapter_ = NULL;
72 DBusThreadManager::Shutdown();
75 void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
76 adapter_ = adapter;
79 void Callback() {
80 ++callback_count_;
83 void ErrorCallback() {
84 ++error_callback_count_;
86 message_loop_.Quit();
89 void ProfileCallback(BluetoothProfile* profile) {
90 ++profile_callback_count_;
91 last_profile_ = profile;
94 void ConnectionCallback(const BluetoothDevice *device,
95 scoped_refptr<BluetoothSocket> socket) {
96 ++connection_callback_count_;
97 last_device_ = device;
98 last_socket_ = socket;
100 message_loop_.Quit();
103 protected:
104 base::MessageLoopForIO message_loop_;
106 FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client_;
107 scoped_refptr<BluetoothAdapter> adapter_;
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_;
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)));
128 EXPECT_EQ(1U, profile_callback_count_);
129 EXPECT_TRUE(last_profile_ != NULL);
130 BluetoothProfile* profile = last_profile_;
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);
138 // Register the connection callback.
139 profile->SetConnectionCallback(
140 base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback,
141 base::Unretained(this)));
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);
150 device->ConnectToProfile(
151 profile,
152 base::Bind(&BluetoothProfileChromeOSTest::Callback,
153 base::Unretained(this)),
154 base::Bind(&BluetoothProfileChromeOSTest::ErrorCallback,
155 base::Unretained(this)));
157 message_loop_.Run();
159 EXPECT_EQ(1U, callback_count_);
160 EXPECT_EQ(0U, error_callback_count_);
162 EXPECT_EQ(1U, connection_callback_count_);
163 EXPECT_EQ(device, last_device_);
164 EXPECT_TRUE(last_socket_.get() != NULL);
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;
171 bool success;
172 scoped_refptr<net::GrowableIOBuffer> read_buffer;
174 scoped_refptr<net::StringIOBuffer> base_buffer(
175 new net::StringIOBuffer("test"));
176 scoped_refptr<net::DrainableIOBuffer> write_buffer;
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.get());
182 EXPECT_TRUE(success);
183 EXPECT_EQ(0, read_buffer->capacity());
184 EXPECT_EQ(0, read_buffer->offset());
185 EXPECT_EQ("", socket->GetLastErrorMessage());
187 // Write data to the socket; the data should be consumed and no bytes should
188 // be remaining.
189 write_buffer =
190 new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size());
191 success = socket->Send(write_buffer.get());
192 EXPECT_TRUE(success);
193 EXPECT_EQ(base_buffer->size(), write_buffer->BytesConsumed());
194 EXPECT_EQ(0, write_buffer->BytesRemaining());
195 EXPECT_EQ("", socket->GetLastErrorMessage());
197 // Read data from the socket; this should match the data we sent since the
198 // server just echoes us. We have to spin here until there is actually data
199 // to read.
200 read_buffer = new net::GrowableIOBuffer;
201 do {
202 success = socket->Receive(read_buffer.get());
203 } while (success && read_buffer->offset() == 0);
204 EXPECT_TRUE(success);
205 EXPECT_NE(0, read_buffer->capacity());
206 EXPECT_EQ(base_buffer->size(), read_buffer->offset());
207 EXPECT_EQ("", socket->GetLastErrorMessage());
209 std::string data = std::string(read_buffer->StartOfBuffer(),
210 read_buffer->offset());
211 EXPECT_EQ("test", data);
213 // Write data to the socket; since the socket is closed, this should return
214 // an error without writing the data and "Disconnected" as the message.
215 write_buffer =
216 new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size());
217 success = socket->Send(write_buffer.get());
218 EXPECT_FALSE(success);
219 EXPECT_EQ(0, write_buffer->BytesConsumed());
220 EXPECT_EQ(base_buffer->size(), write_buffer->BytesRemaining());
221 EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());
223 // Read data from the socket; since the socket is closed, this should return
224 // an error with "Disconnected" as the last message.
225 read_buffer = new net::GrowableIOBuffer;
226 success = socket->Receive(read_buffer.get());
227 EXPECT_FALSE(success);
228 EXPECT_EQ(0, read_buffer->capacity());
229 EXPECT_EQ(0, read_buffer->offset());
230 EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());
232 // Close our end of the socket.
233 socket = NULL;
235 // Unregister the profile, make sure it's no longer registered.
236 last_profile_->Unregister();
238 profile_service_provider =
239 fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
240 FakeBluetoothProfileManagerClient::kL2capUuid);
241 EXPECT_TRUE(profile_service_provider == NULL);
244 TEST_F(BluetoothProfileChromeOSTest, RfcommEndToEnd) {
245 // Register the profile and expect the profile object to be passed to the
246 // callback.
247 BluetoothProfile::Options options;
248 BluetoothProfile::Register(
249 FakeBluetoothProfileManagerClient::kRfcommUuid,
250 options,
251 base::Bind(&BluetoothProfileChromeOSTest::ProfileCallback,
252 base::Unretained(this)));
254 EXPECT_EQ(1U, profile_callback_count_);
255 EXPECT_TRUE(last_profile_ != NULL);
256 BluetoothProfile* profile = last_profile_;
258 // Make sure we have a profile service provider for it.
259 FakeBluetoothProfileServiceProvider* profile_service_provider =
260 fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
261 FakeBluetoothProfileManagerClient::kRfcommUuid);
262 EXPECT_TRUE(profile_service_provider != NULL);
264 // Register the connection callback.
265 profile->SetConnectionCallback(
266 base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback,
267 base::Unretained(this)));
269 // Connect to the device, expect the success callback to be called and the
270 // connection callback to be called with the device we passed and a new
271 // socket instance.
272 BluetoothDevice* device = adapter_->GetDevice(
273 FakeBluetoothDeviceClient::kPairedDeviceAddress);
274 ASSERT_TRUE(device != NULL);
276 device->ConnectToProfile(
277 profile,
278 base::Bind(&BluetoothProfileChromeOSTest::Callback,
279 base::Unretained(this)),
280 base::Bind(&BluetoothProfileChromeOSTest::ErrorCallback,
281 base::Unretained(this)));
283 message_loop_.Run();
285 EXPECT_EQ(1U, callback_count_);
286 EXPECT_EQ(0U, error_callback_count_);
288 EXPECT_EQ(1U, connection_callback_count_);
289 EXPECT_EQ(device, last_device_);
290 EXPECT_TRUE(last_socket_.get() != NULL);
292 // Take the ownership of the socket for the remainder of the test and set
293 // up buffers for read/write tests.
294 scoped_refptr<BluetoothSocket> socket = last_socket_;
295 last_socket_ = NULL;
297 bool success;
298 scoped_refptr<net::GrowableIOBuffer> read_buffer;
300 scoped_refptr<net::StringIOBuffer> base_buffer(
301 new net::StringIOBuffer("test"));
302 scoped_refptr<net::DrainableIOBuffer> write_buffer;
304 // Read data from the socket; since no data should be waiting, this should
305 // return success but no data.
306 read_buffer = new net::GrowableIOBuffer;
307 success = socket->Receive(read_buffer.get());
308 EXPECT_TRUE(success);
309 EXPECT_EQ(0, read_buffer->offset());
310 EXPECT_EQ("", socket->GetLastErrorMessage());
312 // Write data to the socket; the data should be consumed and no bytes should
313 // be remaining.
314 write_buffer =
315 new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size());
316 success = socket->Send(write_buffer.get());
317 EXPECT_TRUE(success);
318 EXPECT_EQ(base_buffer->size(), write_buffer->BytesConsumed());
319 EXPECT_EQ(0, write_buffer->BytesRemaining());
320 EXPECT_EQ("", socket->GetLastErrorMessage());
322 // Read data from the socket; this should match the data we sent since the
323 // server just echoes us. We have to spin here until there is actually data
324 // to read.
325 read_buffer = new net::GrowableIOBuffer;
326 do {
327 success = socket->Receive(read_buffer.get());
328 } while (success && read_buffer->offset() == 0);
329 EXPECT_TRUE(success);
330 EXPECT_NE(0, read_buffer->capacity());
331 EXPECT_EQ(base_buffer->size(), read_buffer->offset());
332 EXPECT_EQ("", socket->GetLastErrorMessage());
334 std::string data = std::string(read_buffer->StartOfBuffer(),
335 read_buffer->offset());
336 EXPECT_EQ("test", data);
338 // Write data to the socket; since the socket is closed, this should return
339 // an error without writing the data and "Disconnected" as the message.
340 write_buffer =
341 new net::DrainableIOBuffer(base_buffer.get(), base_buffer->size());
342 success = socket->Send(write_buffer.get());
343 EXPECT_FALSE(success);
344 EXPECT_EQ(0, write_buffer->BytesConsumed());
345 EXPECT_EQ(base_buffer->size(), write_buffer->BytesRemaining());
346 EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());
348 // Read data from the socket; since the socket is closed, this should return
349 // an error with "Disconnected" as the last message.
350 read_buffer = new net::GrowableIOBuffer;
351 success = socket->Receive(read_buffer.get());
352 EXPECT_FALSE(success);
353 EXPECT_EQ(0, read_buffer->offset());
354 EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());
356 // Close our end of the socket.
357 socket = NULL;
359 // Unregister the profile, make sure it's no longer registered.
360 last_profile_->Unregister();
362 profile_service_provider =
363 fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
364 FakeBluetoothProfileManagerClient::kRfcommUuid);
365 EXPECT_TRUE(profile_service_provider == NULL);
368 } // namespace chromeos