summaryrefslogtreecommitdiffstats
path: root/device/bluetooth/bluetooth_profile_chromeos_unittest.cc
blob: 620116c26ec2eb31163c091596f7a72003e34da7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/message_loop.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/dbus/fake_bluetooth_device_client.h"
#include "chromeos/dbus/fake_bluetooth_profile_manager_client.h"
#include "chromeos/dbus/fake_bluetooth_profile_service_provider.h"
#include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_experimental_chromeos.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_device_experimental_chromeos.h"
#include "device/bluetooth/bluetooth_profile.h"
#include "device/bluetooth/bluetooth_profile_experimental_chromeos.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "device/bluetooth/bluetooth_socket_experimental_chromeos.h"
#include "net/base/io_buffer.h"
#include "testing/gtest/include/gtest/gtest.h"

using device::BluetoothAdapter;
using device::BluetoothDevice;
using device::BluetoothProfile;
using device::BluetoothSocket;

namespace chromeos {

class BluetoothProfileChromeOSTest : public testing::Test {
 public:
  BluetoothProfileChromeOSTest()
      : message_loop_(MessageLoop::TYPE_IO),
        callback_count_(0),
        error_callback_count_(0),
        profile_callback_count_(0),
        connection_callback_count_(0),
        last_profile_(NULL),
        last_device_(NULL) {}

  virtual void SetUp() {
    if (!CommandLine::ForCurrentProcess()->HasSwitch(
            chromeos::switches::kEnableExperimentalBluetooth))
      CommandLine::ForCurrentProcess()->AppendSwitch(
          chromeos::switches::kEnableExperimentalBluetooth);

    mock_dbus_thread_manager_ =
        new MockDBusThreadManagerWithoutGMock();
    DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_);

    fake_bluetooth_profile_manager_client_ =
      mock_dbus_thread_manager_->fake_bluetooth_profile_manager_client();

    device::BluetoothAdapterFactory::GetAdapter(
        base::Bind(&BluetoothProfileChromeOSTest::AdapterCallback,
                   base::Unretained(this)));
    ASSERT_TRUE(adapter_ != NULL);
    ASSERT_TRUE(adapter_->IsInitialized());
    ASSERT_TRUE(adapter_->IsPresent());

    adapter_->SetPowered(
        true,
        base::Bind(&base::DoNothing),
        base::Bind(&base::DoNothing));
    ASSERT_TRUE(adapter_->IsPowered());
  }

  virtual void TearDown() {
    adapter_ = NULL;
    DBusThreadManager::Shutdown();
  }

  void AdapterCallback(scoped_refptr<BluetoothAdapter> adapter) {
    adapter_ = adapter;
  }

  void Callback() {
    ++callback_count_;
  }

  void ErrorCallback() {
    ++error_callback_count_;

    message_loop_.Quit();
  }

  void ProfileCallback(BluetoothProfile* profile) {
    ++profile_callback_count_;
    last_profile_ = profile;
  }

  void ConnectionCallback(const BluetoothDevice *device,
                          scoped_refptr<BluetoothSocket> socket) {
    ++connection_callback_count_;
    last_device_ = device;
    last_socket_ = socket;

    message_loop_.Quit();
  }

 protected:
  base::MessageLoop message_loop_;

  FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client_;
  MockDBusThreadManagerWithoutGMock* mock_dbus_thread_manager_;
  scoped_refptr<BluetoothAdapter> adapter_;

  unsigned int callback_count_;
  unsigned int error_callback_count_;
  unsigned int profile_callback_count_;
  unsigned int connection_callback_count_;
  BluetoothProfile* last_profile_;
  const BluetoothDevice* last_device_;
  scoped_refptr<BluetoothSocket> last_socket_;
};

TEST_F(BluetoothProfileChromeOSTest, L2capEndToEnd) {
  // Register the profile and expect the profile object to be passed to the
  // callback.
  BluetoothProfile::Options options;
  BluetoothProfile::Register(
      FakeBluetoothProfileManagerClient::kL2capUuid,
      options,
      base::Bind(&BluetoothProfileChromeOSTest::ProfileCallback,
                 base::Unretained(this)));

  EXPECT_EQ(1U, profile_callback_count_);
  EXPECT_TRUE(last_profile_ != NULL);
  BluetoothProfile* profile = last_profile_;

  // Make sure we have a profile service provider for it.
  FakeBluetoothProfileServiceProvider* profile_service_provider =
      fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
          FakeBluetoothProfileManagerClient::kL2capUuid);
  EXPECT_TRUE(profile_service_provider != NULL);

  // Register the connection callback.
  profile->SetConnectionCallback(
      base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback,
                 base::Unretained(this)));

  // Connect to the device, expect the success callback to be called and the
  // connection callback to be called with the device we passed and a new
  // socket instance.
  BluetoothDevice* device = adapter_->GetDevice(
      FakeBluetoothDeviceClient::kPairedDeviceAddress);
  ASSERT_TRUE(device != NULL);

  device->ConnectToProfile(
      profile,
      base::Bind(&BluetoothProfileChromeOSTest::Callback,
                 base::Unretained(this)),
      base::Bind(&BluetoothProfileChromeOSTest::ErrorCallback,
                 base::Unretained(this)));

  message_loop_.Run();

  EXPECT_EQ(1U, callback_count_);
  EXPECT_EQ(0U, error_callback_count_);

  EXPECT_EQ(1U, connection_callback_count_);
  EXPECT_EQ(device, last_device_);
  EXPECT_TRUE(last_socket_.get() != NULL);

  // Take the ownership of the socket for the remainder of the test and set
  // up buffers for read/write tests.
  scoped_refptr<BluetoothSocket> socket = last_socket_;
  last_socket_ = NULL;

  bool success;
  scoped_refptr<net::GrowableIOBuffer> read_buffer;

  scoped_refptr<net::StringIOBuffer> base_buffer(
      new net::StringIOBuffer("test"));
  scoped_refptr<net::DrainableIOBuffer> write_buffer;

  // Read data from the socket; since no data should be waiting, this should
  // return success but no data.
  read_buffer = new net::GrowableIOBuffer;
  success = socket->Receive(read_buffer);
  EXPECT_TRUE(success);
  EXPECT_EQ(0, read_buffer->capacity());
  EXPECT_EQ(0, read_buffer->offset());
  EXPECT_EQ("", socket->GetLastErrorMessage());

  // Write data to the socket; the data should be consumed and no bytes should
  // be remaining.
  write_buffer = new net::DrainableIOBuffer(base_buffer, base_buffer->size());
  success = socket->Send(write_buffer);
  EXPECT_TRUE(success);
  EXPECT_EQ(base_buffer->size(), write_buffer->BytesConsumed());
  EXPECT_EQ(0, write_buffer->BytesRemaining());
  EXPECT_EQ("", socket->GetLastErrorMessage());

  // Read data from the socket; this should match the data we sent since the
  // server just echoes us. We have to spin here until there is actually data
  // to read.
  read_buffer = new net::GrowableIOBuffer;
  do {
    success = socket->Receive(read_buffer);
  } while (success && read_buffer->offset() == 0);
  EXPECT_TRUE(success);
  EXPECT_NE(0, read_buffer->capacity());
  EXPECT_EQ(base_buffer->size(), read_buffer->offset());
  EXPECT_EQ("", socket->GetLastErrorMessage());

  std::string data = std::string(read_buffer->StartOfBuffer(),
                                 read_buffer->offset());
  EXPECT_EQ("test", data);

  // Write data to the socket; since the socket is closed, this should return
  // an error without writing the data and "Disconnected" as the message.
  write_buffer = new net::DrainableIOBuffer(base_buffer, base_buffer->size());
  success = socket->Send(write_buffer);
  EXPECT_FALSE(success);
  EXPECT_EQ(0, write_buffer->BytesConsumed());
  EXPECT_EQ(base_buffer->size(), write_buffer->BytesRemaining());
  EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());

  // Read data from the socket; since the socket is closed, this should return
  // an error with "Disconnected" as the last message.
  read_buffer = new net::GrowableIOBuffer;
  success = socket->Receive(read_buffer);
  EXPECT_FALSE(success);
  EXPECT_EQ(0, read_buffer->capacity());
  EXPECT_EQ(0, read_buffer->offset());
  EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());

  // Close our end of the socket.
  socket = NULL;

  // Unregister the profile, make sure it's no longer registered.
  last_profile_->Unregister();

  profile_service_provider =
      fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
          FakeBluetoothProfileManagerClient::kL2capUuid);
  EXPECT_TRUE(profile_service_provider == NULL);
}

TEST_F(BluetoothProfileChromeOSTest, RfcommEndToEnd) {
  // Register the profile and expect the profile object to be passed to the
  // callback.
  BluetoothProfile::Options options;
  BluetoothProfile::Register(
      FakeBluetoothProfileManagerClient::kRfcommUuid,
      options,
      base::Bind(&BluetoothProfileChromeOSTest::ProfileCallback,
                 base::Unretained(this)));

  EXPECT_EQ(1U, profile_callback_count_);
  EXPECT_TRUE(last_profile_ != NULL);
  BluetoothProfile* profile = last_profile_;

  // Make sure we have a profile service provider for it.
  FakeBluetoothProfileServiceProvider* profile_service_provider =
      fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
          FakeBluetoothProfileManagerClient::kRfcommUuid);
  EXPECT_TRUE(profile_service_provider != NULL);

  // Register the connection callback.
  profile->SetConnectionCallback(
      base::Bind(&BluetoothProfileChromeOSTest::ConnectionCallback,
                 base::Unretained(this)));

  // Connect to the device, expect the success callback to be called and the
  // connection callback to be called with the device we passed and a new
  // socket instance.
  BluetoothDevice* device = adapter_->GetDevice(
      FakeBluetoothDeviceClient::kPairedDeviceAddress);
  ASSERT_TRUE(device != NULL);

  device->ConnectToProfile(
      profile,
      base::Bind(&BluetoothProfileChromeOSTest::Callback,
                 base::Unretained(this)),
      base::Bind(&BluetoothProfileChromeOSTest::ErrorCallback,
                 base::Unretained(this)));

  message_loop_.Run();

  EXPECT_EQ(1U, callback_count_);
  EXPECT_EQ(0U, error_callback_count_);

  EXPECT_EQ(1U, connection_callback_count_);
  EXPECT_EQ(device, last_device_);
  EXPECT_TRUE(last_socket_.get() != NULL);

  // Take the ownership of the socket for the remainder of the test and set
  // up buffers for read/write tests.
  scoped_refptr<BluetoothSocket> socket = last_socket_;
  last_socket_ = NULL;

  bool success;
  scoped_refptr<net::GrowableIOBuffer> read_buffer;

  scoped_refptr<net::StringIOBuffer> base_buffer(
      new net::StringIOBuffer("test"));
  scoped_refptr<net::DrainableIOBuffer> write_buffer;

  // Read data from the socket; since no data should be waiting, this should
  // return success but no data.
  read_buffer = new net::GrowableIOBuffer;
  success = socket->Receive(read_buffer);
  EXPECT_TRUE(success);
  EXPECT_EQ(0, read_buffer->offset());
  EXPECT_EQ("", socket->GetLastErrorMessage());

  // Write data to the socket; the data should be consumed and no bytes should
  // be remaining.
  write_buffer = new net::DrainableIOBuffer(base_buffer, base_buffer->size());
  success = socket->Send(write_buffer);
  EXPECT_TRUE(success);
  EXPECT_EQ(base_buffer->size(), write_buffer->BytesConsumed());
  EXPECT_EQ(0, write_buffer->BytesRemaining());
  EXPECT_EQ("", socket->GetLastErrorMessage());

  // Read data from the socket; this should match the data we sent since the
  // server just echoes us. We have to spin here until there is actually data
  // to read.
  read_buffer = new net::GrowableIOBuffer;
  do {
    success = socket->Receive(read_buffer);
  } while (success && read_buffer->offset() == 0);
  EXPECT_TRUE(success);
  EXPECT_NE(0, read_buffer->capacity());
  EXPECT_EQ(base_buffer->size(), read_buffer->offset());
  EXPECT_EQ("", socket->GetLastErrorMessage());

  std::string data = std::string(read_buffer->StartOfBuffer(),
                                 read_buffer->offset());
  EXPECT_EQ("test", data);

  // Write data to the socket; since the socket is closed, this should return
  // an error without writing the data and "Disconnected" as the message.
  write_buffer = new net::DrainableIOBuffer(base_buffer, base_buffer->size());
  success = socket->Send(write_buffer);
  EXPECT_FALSE(success);
  EXPECT_EQ(0, write_buffer->BytesConsumed());
  EXPECT_EQ(base_buffer->size(), write_buffer->BytesRemaining());
  EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());

  // Read data from the socket; since the socket is closed, this should return
  // an error with "Disconnected" as the last message.
  read_buffer = new net::GrowableIOBuffer;
  success = socket->Receive(read_buffer);
  EXPECT_FALSE(success);
  EXPECT_EQ(0, read_buffer->offset());
  EXPECT_EQ("Disconnected", socket->GetLastErrorMessage());

  // Close our end of the socket.
  socket = NULL;

  // Unregister the profile, make sure it's no longer registered.
  last_profile_->Unregister();

  profile_service_provider =
      fake_bluetooth_profile_manager_client_->GetProfileServiceProvider(
          FakeBluetoothProfileManagerClient::kRfcommUuid);
  EXPECT_TRUE(profile_service_provider == NULL);
}

}  // namespace chromeos