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
365
366
367
368
369
370
|
// Copyright (c) 2012 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 <string.h>
#include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h"
#include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h"
#include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_device.h"
#include "chrome/browser/chromeos/extensions/bluetooth_event_router.h"
#include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_function_test_utils.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_test_message_listener.h"
#include "chrome/browser/ui/browser.h"
#include "chromeos/dbus/bluetooth_out_of_band_client.h"
#include "chrome/test/base/ui_test_utils.h"
#include "testing/gmock/include/gmock/gmock.h"
using extensions::Extension;
namespace utils = extension_function_test_utils;
namespace api = extensions::api;
namespace {
class BluetoothApiTest : public PlatformAppApiTest {
public:
BluetoothApiTest() : empty_extension_(utils::CreateEmptyExtension()) {}
virtual void SetUpOnMainThread() OVERRIDE {
// The browser will clean this up when it is torn down
mock_adapter_ = new testing::StrictMock<chromeos::MockBluetoothAdapter>;
event_router()->SetAdapterForTest(mock_adapter_);
device1_.reset(new testing::NiceMock<chromeos::MockBluetoothDevice>(
mock_adapter_, "d1", "11:12:13:14:15:16",
true /* paired */, false /* bonded */, true /* connected */));
device2_.reset(new testing::NiceMock<chromeos::MockBluetoothDevice>(
mock_adapter_, "d2", "21:22:23:24:25:26",
false /* paired */, true /* bonded */, false /* connected */));
}
void expectBooleanResult(bool expected,
UIThreadExtensionFunction* function,
const std::string& args) {
scoped_ptr<base::Value> result(
utils::RunFunctionAndReturnResult(function, args, browser()));
ASSERT_TRUE(result.get() != NULL);
ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType());
bool boolean_value;
result->GetAsBoolean(&boolean_value);
EXPECT_EQ(expected, boolean_value);
}
template <class T>
T* setupFunction(T* function) {
function->set_extension(empty_extension_.get());
function->set_has_callback(true);
return function;
}
protected:
testing::StrictMock<chromeos::MockBluetoothAdapter>* mock_adapter_;
scoped_ptr<testing::NiceMock<chromeos::MockBluetoothDevice> > device1_;
scoped_ptr<testing::NiceMock<chromeos::MockBluetoothDevice> > device2_;
chromeos::ExtensionBluetoothEventRouter* event_router() {
return browser()->profile()->GetExtensionService()->
bluetooth_event_router();
}
private:
scoped_refptr<Extension> empty_extension_;
};
static const char kOutOfBandPairingDataHash[] = "0123456789ABCDEh";
static const char kOutOfBandPairingDataRandomizer[] = "0123456789ABCDEr";
static chromeos::BluetoothOutOfBandPairingData GetOutOfBandPairingData() {
chromeos::BluetoothOutOfBandPairingData data;
memcpy(&(data.hash), kOutOfBandPairingDataHash,
chromeos::kBluetoothOutOfBandPairingDataSize);
memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer,
chromeos::kBluetoothOutOfBandPairingDataSize);
return data;
}
static bool CallClosure(const base::Closure& callback) {
callback.Run();
return true;
}
static bool CallOutOfBandPairingDataCallback(
const chromeos::BluetoothAdapter::BluetoothOutOfBandPairingDataCallback&
callback) {
callback.Run(GetOutOfBandPairingData());
return true;
}
} // namespace
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, IsAvailable) {
EXPECT_CALL(*mock_adapter_, IsPresent())
.WillOnce(testing::Return(false));
scoped_refptr<api::BluetoothIsAvailableFunction> is_available;
is_available = setupFunction(new api::BluetoothIsAvailableFunction);
expectBooleanResult(false, is_available, "[]");
testing::Mock::VerifyAndClearExpectations(mock_adapter_);
EXPECT_CALL(*mock_adapter_, IsPresent())
.WillOnce(testing::Return(true));
is_available = setupFunction(new api::BluetoothIsAvailableFunction);
expectBooleanResult(true, is_available, "[]");
}
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, IsPowered) {
EXPECT_CALL(*mock_adapter_, IsPowered())
.WillOnce(testing::Return(false));
scoped_refptr<api::BluetoothIsPoweredFunction> is_powered;
is_powered = setupFunction(new api::BluetoothIsPoweredFunction);
expectBooleanResult(false, is_powered, "[]");
testing::Mock::VerifyAndClearExpectations(mock_adapter_);
EXPECT_CALL(*mock_adapter_, IsPowered())
.WillOnce(testing::Return(true));
is_powered = setupFunction(new api::BluetoothIsPoweredFunction);
expectBooleanResult(true, is_powered, "[]");
}
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) {
chromeos::BluetoothAdapter::ConstDeviceList devices;
devices.push_back(device1_.get());
devices.push_back(device2_.get());
EXPECT_CALL(*device1_, ProvidesServiceWithUUID("foo"))
.WillOnce(testing::Return(false));
EXPECT_CALL(*device2_, ProvidesServiceWithUUID("foo"))
.WillOnce(testing::Return(true));
EXPECT_CALL(*mock_adapter_, GetDevices())
.WillOnce(testing::Return(devices));
scoped_refptr<api::BluetoothGetDevicesFunction> get_devices;
get_devices = setupFunction(new api::BluetoothGetDevicesFunction);
scoped_ptr<base::Value> result(utils::RunFunctionAndReturnResult(
get_devices,
"[{\"uuid\":\"foo\"}]",
browser()));
ASSERT_EQ(base::Value::TYPE_LIST, result->GetType());
base::ListValue* list;
ASSERT_TRUE(result->GetAsList(&list));
EXPECT_EQ(1u, list->GetSize());
base::Value* device_value;
EXPECT_TRUE(list->Get(0, &device_value));
EXPECT_EQ(base::Value::TYPE_DICTIONARY, device_value->GetType());
base::DictionaryValue* device;
ASSERT_TRUE(device_value->GetAsDictionary(&device));
std::string name;
ASSERT_TRUE(device->GetString("name", &name));
EXPECT_EQ("d2", name);
std::string address;
ASSERT_TRUE(device->GetString("address", &address));
EXPECT_EQ("21:22:23:24:25:26", address);
bool paired;
ASSERT_TRUE(device->GetBoolean("paired", &paired));
EXPECT_FALSE(paired);
bool bonded;
ASSERT_TRUE(device->GetBoolean("bonded", &bonded));
EXPECT_TRUE(bonded);
bool connected;
ASSERT_TRUE(device->GetBoolean("connected", &connected));
EXPECT_FALSE(connected);
// Try again with no options
testing::Mock::VerifyAndClearExpectations(mock_adapter_);
EXPECT_CALL(*mock_adapter_, GetDevices())
.WillOnce(testing::Return(devices));
get_devices = setupFunction(new api::BluetoothGetDevicesFunction);
result.reset(
utils::RunFunctionAndReturnResult(get_devices, "[{}]", browser()));
ASSERT_EQ(base::Value::TYPE_LIST, result->GetType());
ASSERT_TRUE(result->GetAsList(&list));
EXPECT_EQ(2u, list->GetSize());
}
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetLocalOutOfBandPairingData) {
EXPECT_CALL(*mock_adapter_,
ReadLocalOutOfBandPairingData(
testing::Truly(CallOutOfBandPairingDataCallback),
testing::_));
scoped_refptr<api::BluetoothGetLocalOutOfBandPairingDataFunction>
get_oob_function(setupFunction(
new api::BluetoothGetLocalOutOfBandPairingDataFunction));
scoped_ptr<base::Value> result(
utils::RunFunctionAndReturnResult(get_oob_function, "[]", browser()));
base::DictionaryValue* dict;
EXPECT_TRUE(result->GetAsDictionary(&dict));
base::BinaryValue* binary_value;
EXPECT_TRUE(dict->GetBinary("hash", &binary_value));
EXPECT_STREQ(kOutOfBandPairingDataHash,
std::string(binary_value->GetBuffer(), binary_value->GetSize()).c_str());
EXPECT_TRUE(dict->GetBinary("randomizer", &binary_value));
EXPECT_STREQ(kOutOfBandPairingDataRandomizer,
std::string(binary_value->GetBuffer(), binary_value->GetSize()).c_str());
// Try again with an error
testing::Mock::VerifyAndClearExpectations(mock_adapter_);
EXPECT_CALL(*mock_adapter_,
ReadLocalOutOfBandPairingData(
testing::_,
testing::Truly(CallClosure)));
get_oob_function =
setupFunction(new api::BluetoothGetLocalOutOfBandPairingDataFunction);
std::string error(
utils::RunFunctionAndReturnError(get_oob_function, "[]", browser()));
EXPECT_FALSE(error.empty());
}
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) {
std::string device_address("11:12:13:14:15:16");
EXPECT_CALL(*mock_adapter_, GetDevice(device_address))
.WillOnce(testing::Return(device1_.get()));
EXPECT_CALL(*device1_,
ClearOutOfBandPairingData(testing::Truly(CallClosure),
testing::_));
char buf[64];
snprintf(buf, sizeof(buf),
"[{\"deviceAddress\":\"%s\"}]", device_address.c_str());
std::string params(buf);
scoped_refptr<api::BluetoothSetOutOfBandPairingDataFunction> set_oob_function;
set_oob_function = setupFunction(
new api::BluetoothSetOutOfBandPairingDataFunction);
// There isn't actually a result.
(void)utils::RunFunctionAndReturnResult(set_oob_function, params, browser());
// Try again with an error
testing::Mock::VerifyAndClearExpectations(mock_adapter_);
testing::Mock::VerifyAndClearExpectations(device1_.get());
EXPECT_CALL(*mock_adapter_, GetDevice(device_address))
.WillOnce(testing::Return(device1_.get()));
EXPECT_CALL(*device1_,
ClearOutOfBandPairingData(testing::_,
testing::Truly(CallClosure)));
set_oob_function = setupFunction(
new api::BluetoothSetOutOfBandPairingDataFunction);
std::string error(
utils::RunFunctionAndReturnError(set_oob_function, params, browser()));
EXPECT_FALSE(error.empty());
// TODO(bryeung): Also test setting the data when there is support for
// ArrayBuffers in the arguments to the RunFunctionAnd* methods.
// crbug.com/132796
}
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) {
// Try with a failure to start
EXPECT_CALL(*mock_adapter_,
SetDiscovering(true,
testing::_,
testing::Truly(CallClosure)));
scoped_refptr<api::BluetoothStartDiscoveryFunction> start_function;
start_function = setupFunction(new api::BluetoothStartDiscoveryFunction);
std::string error(
utils::RunFunctionAndReturnError(start_function, "[]", browser()));
ASSERT_TRUE(!error.empty());
// Reset for a successful start
testing::Mock::VerifyAndClearExpectations(mock_adapter_);
EXPECT_CALL(*mock_adapter_,
SetDiscovering(true,
testing::Truly(CallClosure),
testing::_));
start_function = setupFunction(new api::BluetoothStartDiscoveryFunction);
(void)utils::RunFunctionAndReturnError(start_function, "[]", browser());
// Reset to try stopping
testing::Mock::VerifyAndClearExpectations(mock_adapter_);
EXPECT_CALL(*mock_adapter_,
SetDiscovering(false,
testing::Truly(CallClosure),
testing::_));
scoped_refptr<api::BluetoothStopDiscoveryFunction> stop_function;
stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction);
(void)utils::RunFunctionAndReturnResult(stop_function, "[]", browser());
// Reset to try stopping with an error
testing::Mock::VerifyAndClearExpectations(mock_adapter_);
EXPECT_CALL(*mock_adapter_,
SetDiscovering(false,
testing::_,
testing::Truly(CallClosure)));
stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction);
error = utils::RunFunctionAndReturnError(stop_function, "[]", browser());
ASSERT_TRUE(!error.empty());
}
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryCallback) {
EXPECT_CALL(*mock_adapter_,
SetDiscovering(true, testing::Truly(CallClosure), testing::_));
EXPECT_CALL(*mock_adapter_,
SetDiscovering(false, testing::Truly(CallClosure), testing::_));
ResultCatcher catcher;
catcher.RestrictToProfile(browser()->profile());
ExtensionTestMessageListener discovery_started("ready", true);
const extensions::Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("bluetooth"));
GURL page_url = extension->GetResourceURL("test_discovery.html");
ui_test_utils::NavigateToURL(browser(), page_url);
EXPECT_TRUE(discovery_started.WaitUntilSatisfied());
event_router()->DeviceAdded(mock_adapter_, device1_.get());
discovery_started.Reply("go");
ExtensionTestMessageListener discovery_stopped("ready", true);
EXPECT_TRUE(discovery_stopped.WaitUntilSatisfied());
event_router()->DeviceAdded(mock_adapter_, device2_.get());
discovery_stopped.Reply("go");
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}
IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Events) {
ResultCatcher catcher;
catcher.RestrictToProfile(browser()->profile());
// Load and wait for setup
ExtensionTestMessageListener listener("ready", true);
const extensions::Extension* extension =
LoadExtension(test_data_dir_.AppendASCII("bluetooth"));
GURL page_url = extension->GetResourceURL("test_events.html");
ui_test_utils::NavigateToURL(browser(), page_url);
EXPECT_TRUE(listener.WaitUntilSatisfied());
event_router()->AdapterPoweredChanged(mock_adapter_, true);
event_router()->AdapterPoweredChanged(mock_adapter_, false);
event_router()->AdapterPresentChanged(mock_adapter_, true);
event_router()->AdapterPresentChanged(mock_adapter_, false);
listener.Reply("go");
EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
}
|