Bluetooth: extend JavaScript Device object
[chromium-blink-merge.git] / chrome / browser / extensions / api / bluetooth / bluetooth_apitest.cc
blobcbd4043d6344bb2266996b192009d498054d05ee
1 // Copyright (c) 2012 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 <string.h>
7 #include "base/strings/stringprintf.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h"
10 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h"
11 #include "chrome/browser/extensions/extension_apitest.h"
12 #include "chrome/browser/extensions/extension_function_test_utils.h"
13 #include "chrome/browser/extensions/extension_service.h"
14 #include "chrome/browser/extensions/extension_test_message_listener.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/test/base/ui_test_utils.h"
17 #include "device/bluetooth/bluetooth_adapter.h"
18 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h"
19 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
20 #include "device/bluetooth/test/mock_bluetooth_device.h"
21 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h"
22 #include "device/bluetooth/test/mock_bluetooth_profile.h"
23 #include "device/bluetooth/test/mock_bluetooth_socket.h"
24 #include "testing/gmock/include/gmock/gmock.h"
26 using device::BluetoothAdapter;
27 using device::BluetoothDevice;
28 using device::BluetoothDiscoverySession;
29 using device::BluetoothOutOfBandPairingData;
30 using device::BluetoothProfile;
31 using device::MockBluetoothAdapter;
32 using device::MockBluetoothDevice;
33 using device::MockBluetoothDiscoverySession;
34 using device::MockBluetoothProfile;
35 using extensions::Extension;
37 namespace utils = extension_function_test_utils;
38 namespace api = extensions::api;
40 namespace {
42 static const char* kAdapterAddress = "A1:A2:A3:A4:A5:A6";
43 static const char* kName = "whatsinaname";
45 class BluetoothApiTest : public ExtensionApiTest {
46 public:
47 BluetoothApiTest() : empty_extension_(utils::CreateEmptyExtension()) {}
49 virtual void SetUpOnMainThread() OVERRIDE {
50 SetUpMockAdapter();
51 profile1_.reset(new testing::NiceMock<MockBluetoothProfile>());
52 profile2_.reset(new testing::NiceMock<MockBluetoothProfile>());
55 virtual void CleanUpOnMainThread() OVERRIDE {
56 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_));
59 void SetUpMockAdapter() {
60 // The browser will clean this up when it is torn down
61 mock_adapter_ = new testing::StrictMock<MockBluetoothAdapter>();
62 event_router()->SetAdapterForTest(mock_adapter_);
64 device1_.reset(new testing::NiceMock<MockBluetoothDevice>(
65 mock_adapter_, 0, "d1", "11:12:13:14:15:16",
66 true /* paired */, true /* connected */));
67 device2_.reset(new testing::NiceMock<MockBluetoothDevice>(
68 mock_adapter_, 0, "d2", "21:22:23:24:25:26",
69 false /* paired */, false /* connected */));
70 device3_.reset(new testing::NiceMock<MockBluetoothDevice>(
71 mock_adapter_, 0, "d3", "31:32:33:34:35:36",
72 false /* paired */, false /* connected */));
76 void DiscoverySessionCallback(
77 const BluetoothAdapter::DiscoverySessionCallback& callback,
78 const BluetoothAdapter::ErrorCallback& error_callback) {
79 if (mock_session_.get()) {
80 callback.Run(
81 scoped_ptr<BluetoothDiscoverySession>(mock_session_.release()));
82 return;
84 error_callback.Run();
87 template <class T>
88 T* setupFunction(T* function) {
89 function->set_extension(empty_extension_.get());
90 function->set_has_callback(true);
91 return function;
94 protected:
95 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_;
96 scoped_ptr<testing::NiceMock<MockBluetoothDiscoverySession> > mock_session_;
97 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device1_;
98 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device2_;
99 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device3_;
100 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile1_;
101 scoped_ptr<testing::NiceMock<MockBluetoothProfile> > profile2_;
103 extensions::ExtensionBluetoothEventRouter* event_router() {
104 return extensions::BluetoothAPI::Get(browser()->profile())
105 ->bluetooth_event_router();
108 private:
109 scoped_refptr<Extension> empty_extension_;
112 class TestBluetoothAddProfileFunction
113 : public api::BluetoothAddProfileFunction {
114 public:
115 explicit TestBluetoothAddProfileFunction(BluetoothProfile* profile)
116 : BluetoothAddProfileFunction(), profile_(profile) {
119 protected:
120 virtual ~TestBluetoothAddProfileFunction() {
123 // BluetoothAddProfileFunction override.
124 virtual void RegisterProfile(
125 const device::BluetoothProfile::Options& options,
126 const device::BluetoothProfile::ProfileCallback& callback) OVERRIDE {
127 callback.Run(profile_);
130 private:
131 // TestBluetoothAddProfileFunction does not own |profile_|.
132 BluetoothProfile* profile_;
135 // This is the canonical UUID for the short UUID 0010.
136 static const char kOutOfBandPairingDataHash[] = "0123456789ABCDEh";
137 static const char kOutOfBandPairingDataRandomizer[] = "0123456789ABCDEr";
139 static BluetoothOutOfBandPairingData GetOutOfBandPairingData() {
140 BluetoothOutOfBandPairingData data;
141 memcpy(&(data.hash), kOutOfBandPairingDataHash,
142 device::kBluetoothOutOfBandPairingDataSize);
143 memcpy(&(data.randomizer), kOutOfBandPairingDataRandomizer,
144 device::kBluetoothOutOfBandPairingDataSize);
145 return data;
148 static bool CallClosure(const base::Closure& callback) {
149 callback.Run();
150 return true;
153 static void StopDiscoverySessionCallback(const base::Closure& callback,
154 const base::Closure& error_callback) {
155 callback.Run();
158 static void CallOutOfBandPairingDataCallback(
159 const BluetoothAdapter::BluetoothOutOfBandPairingDataCallback& callback,
160 const BluetoothAdapter::ErrorCallback& error_callback) {
161 callback.Run(GetOutOfBandPairingData());
164 } // namespace
166 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Profiles) {
167 // Run in context of an extension that has permissions for the profiles
168 // we intend to register.
169 scoped_refptr<const Extension> extension(
170 LoadExtension(test_data_dir_.AppendASCII("bluetooth/profiles")));
171 ASSERT_TRUE(extension.get());
173 EXPECT_CALL(*profile1_, SetConnectionCallback(testing::_));
174 scoped_refptr<TestBluetoothAddProfileFunction> add_profile_function;
175 add_profile_function = new TestBluetoothAddProfileFunction(profile1_.get());
176 add_profile_function->set_extension(extension.get());
177 add_profile_function->set_has_callback(true);
178 std::string error(utils::RunFunctionAndReturnError(
179 add_profile_function.get(), "[{\"uuid\": \"1234\"}]", browser()));
180 ASSERT_TRUE(error.empty());
182 // Registering the profile for the same uuid again will throw an error.
183 add_profile_function = new TestBluetoothAddProfileFunction(profile2_.get());
184 add_profile_function->set_extension(extension.get());
185 add_profile_function->set_has_callback(true);
186 error = utils::RunFunctionAndReturnError(
187 add_profile_function.get(), "[{\"uuid\": \"1234\"}]", browser());
188 ASSERT_FALSE(error.empty());
190 add_profile_function = new TestBluetoothAddProfileFunction(profile2_.get());
191 add_profile_function->set_extension(extension.get());
192 add_profile_function->set_has_callback(true);
193 error = utils::RunFunctionAndReturnError(
194 add_profile_function.get(), "[{\"uuid\": \"5678\"}]", browser());
195 ASSERT_TRUE(error.empty());
197 scoped_refptr<api::BluetoothRemoveProfileFunction> remove_profile_function;
198 remove_profile_function = new api::BluetoothRemoveProfileFunction();
199 remove_profile_function->set_extension(extension.get());
200 remove_profile_function->set_has_callback(true);
201 error = utils::RunFunctionAndReturnError(
202 remove_profile_function.get(), "[{\"uuid\": \"1234\"}]", browser());
203 ASSERT_TRUE(error.empty());
205 remove_profile_function = new api::BluetoothRemoveProfileFunction();
206 remove_profile_function->set_extension(extension.get());
207 remove_profile_function->set_has_callback(true);
208 error = utils::RunFunctionAndReturnError(
209 remove_profile_function.get(), "[{\"uuid\": \"5678\"}]", browser());
210 ASSERT_TRUE(error.empty());
212 // Removing the same profile again will throw an error.
213 remove_profile_function = new api::BluetoothRemoveProfileFunction();
214 remove_profile_function->set_extension(extension.get());
215 remove_profile_function->set_has_callback(true);
216 error = utils::RunFunctionAndReturnError(
217 remove_profile_function.get(), "[{\"uuid\": \"5678\"}]", browser());
218 ASSERT_FALSE(error.empty());
220 // Registering a profile we don't have permission for will throw an error.
221 add_profile_function = new TestBluetoothAddProfileFunction(profile1_.get());
222 add_profile_function->set_extension(extension.get());
223 add_profile_function->set_has_callback(true);
224 error = utils::RunFunctionAndReturnError(
225 add_profile_function.get(), "[{\"uuid\": \"9999\"}]", browser());
226 ASSERT_FALSE(error.empty());
229 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetAdapterState) {
230 EXPECT_CALL(*mock_adapter_, GetAddress())
231 .WillOnce(testing::Return(kAdapterAddress));
232 EXPECT_CALL(*mock_adapter_, GetName())
233 .WillOnce(testing::Return(kName));
234 EXPECT_CALL(*mock_adapter_, IsPresent())
235 .WillOnce(testing::Return(false));
236 EXPECT_CALL(*mock_adapter_, IsPowered())
237 .WillOnce(testing::Return(true));
238 EXPECT_CALL(*mock_adapter_, IsDiscovering())
239 .WillOnce(testing::Return(false));
241 scoped_refptr<api::BluetoothGetAdapterStateFunction> get_adapter_state;
242 get_adapter_state = setupFunction(new api::BluetoothGetAdapterStateFunction);
244 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult(
245 get_adapter_state.get(), "[]", browser()));
246 ASSERT_TRUE(result.get() != NULL);
247 api::bluetooth::AdapterState state;
248 ASSERT_TRUE(api::bluetooth::AdapterState::Populate(*result, &state));
250 EXPECT_FALSE(state.available);
251 EXPECT_TRUE(state.powered);
252 EXPECT_FALSE(state.discovering);
253 EXPECT_EQ(kName, state.name);
254 EXPECT_EQ(kAdapterAddress, state.address);
257 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetLocalOutOfBandPairingData) {
258 EXPECT_CALL(*mock_adapter_,
259 ReadLocalOutOfBandPairingData(testing::_, testing::_))
260 .WillOnce(testing::Invoke(CallOutOfBandPairingDataCallback));
262 scoped_refptr<api::BluetoothGetLocalOutOfBandPairingDataFunction>
263 get_oob_function(setupFunction(
264 new api::BluetoothGetLocalOutOfBandPairingDataFunction));
266 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnSingleResult(
267 get_oob_function.get(), "[]", browser()));
269 base::DictionaryValue* dict;
270 EXPECT_TRUE(result->GetAsDictionary(&dict));
272 base::BinaryValue* binary_value;
273 EXPECT_TRUE(dict->GetBinary("hash", &binary_value));
274 EXPECT_STREQ(kOutOfBandPairingDataHash,
275 std::string(binary_value->GetBuffer(), binary_value->GetSize()).c_str());
276 EXPECT_TRUE(dict->GetBinary("randomizer", &binary_value));
277 EXPECT_STREQ(kOutOfBandPairingDataRandomizer,
278 std::string(binary_value->GetBuffer(), binary_value->GetSize()).c_str());
280 // Try again with an error
281 testing::Mock::VerifyAndClearExpectations(mock_adapter_);
282 EXPECT_CALL(*mock_adapter_,
283 ReadLocalOutOfBandPairingData(
284 testing::_,
285 testing::Truly(CallClosure)));
287 get_oob_function =
288 setupFunction(new api::BluetoothGetLocalOutOfBandPairingDataFunction);
290 std::string error(utils::RunFunctionAndReturnError(
291 get_oob_function.get(), "[]", browser()));
292 EXPECT_FALSE(error.empty());
295 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) {
296 EXPECT_CALL(*mock_adapter_, GetDevice(device1_->GetAddress()))
297 .WillOnce(testing::Return(device1_.get()));
298 EXPECT_CALL(*device1_,
299 ClearOutOfBandPairingData(testing::Truly(CallClosure),
300 testing::_));
302 std::string params = base::StringPrintf(
303 "[{\"deviceAddress\":\"%s\"}]", device1_->GetAddress().c_str());
305 scoped_refptr<api::BluetoothSetOutOfBandPairingDataFunction> set_oob_function;
306 set_oob_function = setupFunction(
307 new api::BluetoothSetOutOfBandPairingDataFunction);
308 // There isn't actually a result.
309 (void) utils::RunFunctionAndReturnSingleResult(
310 set_oob_function.get(), params, browser());
312 // Try again with an error
313 testing::Mock::VerifyAndClearExpectations(mock_adapter_);
314 testing::Mock::VerifyAndClearExpectations(device1_.get());
315 EXPECT_CALL(*mock_adapter_, GetDevice(device1_->GetAddress()))
316 .WillOnce(testing::Return(device1_.get()));
317 EXPECT_CALL(*device1_,
318 ClearOutOfBandPairingData(testing::_,
319 testing::Truly(CallClosure)));
321 set_oob_function = setupFunction(
322 new api::BluetoothSetOutOfBandPairingDataFunction);
323 std::string error(utils::RunFunctionAndReturnError(
324 set_oob_function.get(), params, browser()));
325 EXPECT_FALSE(error.empty());
327 // TODO(bryeung): Also test setting the data when there is support for
328 // ArrayBuffers in the arguments to the RunFunctionAnd* methods.
329 // crbug.com/132796
332 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DeviceEvents) {
333 ResultCatcher catcher;
334 catcher.RestrictToProfile(browser()->profile());
336 ASSERT_TRUE(LoadExtension(
337 test_data_dir_.AppendASCII("bluetooth/device_events")));
339 ExtensionTestMessageListener events_received("ready", true);
340 event_router()->DeviceAdded(mock_adapter_, device1_.get());
341 event_router()->DeviceAdded(mock_adapter_, device2_.get());
343 EXPECT_CALL(*device2_.get(), GetDeviceName())
344 .WillRepeatedly(testing::Return("the real d2"));
345 EXPECT_CALL(*device2_.get(), GetName())
346 .WillRepeatedly(testing::Return(base::UTF8ToUTF16("the real d2")));
347 event_router()->DeviceChanged(mock_adapter_, device2_.get());
349 event_router()->DeviceAdded(mock_adapter_, device3_.get());
350 event_router()->DeviceRemoved(mock_adapter_, device1_.get());
351 EXPECT_TRUE(events_received.WaitUntilSatisfied());
352 events_received.Reply("go");
354 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
357 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) {
358 // Try with a failure to start. This will return an error as we haven't
359 // initialied a session object.
360 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_))
361 .WillOnce(
362 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback));
364 // StartDiscovery failure will not reference the adapter.
365 scoped_refptr<api::BluetoothStartDiscoveryFunction> start_function;
366 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction);
367 std::string error(
368 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser()));
369 ASSERT_FALSE(error.empty());
371 // Reset the adapter and initiate a discovery session. The ownership of the
372 // mock session will be passed to the event router.
373 ASSERT_FALSE(mock_session_.get());
374 SetUpMockAdapter();
376 // Create a mock session to be returned as a result. Get a handle to it as
377 // its ownership will be passed and |mock_session_| will be reset.
378 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>());
379 MockBluetoothDiscoverySession* session = mock_session_.get();
380 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_))
381 .WillOnce(
382 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback));
383 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction);
384 (void)
385 utils::RunFunctionAndReturnError(start_function.get(), "[]", browser());
387 // End the discovery session. The StopDiscovery function should succeed.
388 testing::Mock::VerifyAndClearExpectations(mock_adapter_);
389 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true));
390 EXPECT_CALL(*session, Stop(testing::_, testing::_))
391 .WillOnce(testing::Invoke(StopDiscoverySessionCallback));
393 // StopDiscovery success will remove the session object, unreferencing the
394 // adapter.
395 scoped_refptr<api::BluetoothStopDiscoveryFunction> stop_function;
396 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction);
397 (void) utils::RunFunctionAndReturnSingleResult(
398 stop_function.get(), "[]", browser());
400 // Reset the adapter. Simulate failure for stop discovery. The event router
401 // still owns the session. Make it appear inactive.
402 SetUpMockAdapter();
403 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(false));
404 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction);
405 error =
406 utils::RunFunctionAndReturnError(stop_function.get(), "[]", browser());
407 ASSERT_FALSE(error.empty());
408 SetUpMockAdapter();
411 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryCallback) {
412 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>());
413 MockBluetoothDiscoverySession* session = mock_session_.get();
414 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_))
415 .WillOnce(
416 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback));
417 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true));
418 EXPECT_CALL(*session, Stop(testing::_, testing::_))
419 .WillOnce(testing::Invoke(StopDiscoverySessionCallback));
421 ResultCatcher catcher;
422 catcher.RestrictToProfile(browser()->profile());
424 ExtensionTestMessageListener discovery_started("ready", true);
425 ASSERT_TRUE(LoadExtension(
426 test_data_dir_.AppendASCII("bluetooth/discovery_callback")));
427 EXPECT_TRUE(discovery_started.WaitUntilSatisfied());
429 event_router()->DeviceAdded(mock_adapter_, device1_.get());
431 discovery_started.Reply("go");
432 ExtensionTestMessageListener discovery_stopped("ready", true);
433 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_));
434 EXPECT_TRUE(discovery_stopped.WaitUntilSatisfied());
436 SetUpMockAdapter();
437 event_router()->DeviceAdded(mock_adapter_, device2_.get());
438 discovery_stopped.Reply("go");
440 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
443 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryInProgress) {
444 EXPECT_CALL(*mock_adapter_, GetAddress())
445 .WillOnce(testing::Return(kAdapterAddress));
446 EXPECT_CALL(*mock_adapter_, GetName())
447 .WillOnce(testing::Return(kName));
448 EXPECT_CALL(*mock_adapter_, IsPresent())
449 .WillOnce(testing::Return(true));
450 EXPECT_CALL(*mock_adapter_, IsPowered())
451 .WillOnce(testing::Return(true));
453 // Fake that the adapter is discovering
454 EXPECT_CALL(*mock_adapter_, IsDiscovering())
455 .WillOnce(testing::Return(true));
456 event_router()->AdapterDiscoveringChanged(mock_adapter_, true);
458 // Cache a device before the extension starts discovering
459 event_router()->DeviceAdded(mock_adapter_, device1_.get());
461 ResultCatcher catcher;
462 catcher.RestrictToProfile(browser()->profile());
464 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>());
465 MockBluetoothDiscoverySession* session = mock_session_.get();
466 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_))
467 .WillOnce(
468 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback));
469 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true));
470 EXPECT_CALL(*session, Stop(testing::_, testing::_))
471 .WillOnce(testing::Invoke(StopDiscoverySessionCallback));
473 ExtensionTestMessageListener discovery_started("ready", true);
474 ASSERT_TRUE(LoadExtension(
475 test_data_dir_.AppendASCII("bluetooth/discovery_in_progress")));
476 EXPECT_TRUE(discovery_started.WaitUntilSatisfied());
478 // Only this should be received. No additional notification should be sent for
479 // devices discovered before the discovery session started.
480 event_router()->DeviceAdded(mock_adapter_, device2_.get());
482 discovery_started.Reply("go");
483 ExtensionTestMessageListener discovery_stopped("ready", true);
484 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_));
485 EXPECT_TRUE(discovery_stopped.WaitUntilSatisfied());
487 SetUpMockAdapter();
488 // This should never be received.
489 event_router()->DeviceAdded(mock_adapter_, device2_.get());
490 discovery_stopped.Reply("go");
492 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
495 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, OnAdapterStateChanged) {
496 ResultCatcher catcher;
497 catcher.RestrictToProfile(browser()->profile());
499 // Load and wait for setup
500 ExtensionTestMessageListener listener("ready", true);
501 ASSERT_TRUE(
502 LoadExtension(
503 test_data_dir_.AppendASCII("bluetooth/on_adapter_state_changed")));
504 EXPECT_TRUE(listener.WaitUntilSatisfied());
506 EXPECT_CALL(*mock_adapter_, GetAddress())
507 .WillOnce(testing::Return(kAdapterAddress));
508 EXPECT_CALL(*mock_adapter_, GetName())
509 .WillOnce(testing::Return(kName));
510 EXPECT_CALL(*mock_adapter_, IsPresent())
511 .WillOnce(testing::Return(false));
512 EXPECT_CALL(*mock_adapter_, IsPowered())
513 .WillOnce(testing::Return(false));
514 EXPECT_CALL(*mock_adapter_, IsDiscovering())
515 .WillOnce(testing::Return(false));
516 event_router()->AdapterPoweredChanged(mock_adapter_, false);
518 EXPECT_CALL(*mock_adapter_, GetAddress())
519 .WillOnce(testing::Return(kAdapterAddress));
520 EXPECT_CALL(*mock_adapter_, GetName())
521 .WillOnce(testing::Return(kName));
522 EXPECT_CALL(*mock_adapter_, IsPresent())
523 .WillOnce(testing::Return(true));
524 EXPECT_CALL(*mock_adapter_, IsPowered())
525 .WillOnce(testing::Return(true));
526 EXPECT_CALL(*mock_adapter_, IsDiscovering())
527 .WillOnce(testing::Return(true));
528 event_router()->AdapterPresentChanged(mock_adapter_, true);
530 EXPECT_CALL(*mock_adapter_, GetAddress())
531 .WillOnce(testing::Return(kAdapterAddress));
532 EXPECT_CALL(*mock_adapter_, GetName())
533 .WillOnce(testing::Return(kName));
534 EXPECT_CALL(*mock_adapter_, IsPresent())
535 .WillOnce(testing::Return(true));
536 EXPECT_CALL(*mock_adapter_, IsPowered())
537 .WillOnce(testing::Return(true));
538 EXPECT_CALL(*mock_adapter_, IsDiscovering())
539 .WillOnce(testing::Return(true));
540 event_router()->AdapterDiscoveringChanged(mock_adapter_, true);
542 listener.Reply("go");
544 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
547 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, OnConnection) {
548 ResultCatcher catcher;
549 catcher.RestrictToProfile(browser()->profile());
551 // Load and wait for setup
552 ExtensionTestMessageListener listener("ready", true);
553 scoped_refptr<const Extension> extension(
554 LoadExtension(test_data_dir_.AppendASCII("bluetooth/on_connection")));
555 ASSERT_TRUE(extension.get());
556 EXPECT_TRUE(listener.WaitUntilSatisfied());
558 scoped_refptr<device::MockBluetoothSocket> socket =
559 new device::MockBluetoothSocket();
561 event_router()->AddProfile("1234", extension->id(), profile1_.get());
562 event_router()->DispatchConnectionEvent(
563 extension->id(), "1234", device1_.get(), socket);
565 listener.Reply("go");
566 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
567 event_router()->RemoveProfile("1234");
570 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetProfiles) {
571 ResultCatcher catcher;
572 catcher.RestrictToProfile(browser()->profile());
574 BluetoothDevice::ServiceList service_list;
575 service_list.push_back("1234");
576 service_list.push_back("5678");
578 EXPECT_CALL(*device1_, GetServices())
579 .WillOnce(testing::Return(service_list));
581 EXPECT_CALL(*mock_adapter_, GetDevice(device1_->GetAddress()))
582 .WillOnce(testing::Return(device1_.get()));
584 // Load and wait for setup
585 ExtensionTestMessageListener listener("ready", true);
586 ASSERT_TRUE(
587 LoadExtension(test_data_dir_.AppendASCII("bluetooth/get_profiles")));
588 EXPECT_TRUE(listener.WaitUntilSatisfied());
590 listener.Reply("go");
592 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
595 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) {
596 ResultCatcher catcher;
597 catcher.RestrictToProfile(browser()->profile());
599 BluetoothAdapter::ConstDeviceList devices;
600 devices.push_back(device1_.get());
601 devices.push_back(device2_.get());
603 EXPECT_CALL(*mock_adapter_, GetDevices())
604 .Times(1)
605 .WillRepeatedly(testing::Return(devices));
607 // Load and wait for setup
608 ExtensionTestMessageListener listener("ready", true);
609 ASSERT_TRUE(
610 LoadExtension(test_data_dir_.AppendASCII("bluetooth/get_devices")));
611 EXPECT_TRUE(listener.WaitUntilSatisfied());
613 listener.Reply("go");
615 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
618 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DeviceInfo) {
619 ResultCatcher catcher;
620 catcher.RestrictToProfile(browser()->profile());
622 // Set up the first device object to reflect a real-world device.
623 BluetoothAdapter::ConstDeviceList devices;
625 EXPECT_CALL(*device1_.get(), GetAddress())
626 .WillRepeatedly(testing::Return("A4:17:31:00:00:00"));
627 EXPECT_CALL(*device1_.get(), GetDeviceName())
628 .WillRepeatedly(testing::Return("Chromebook Pixel"));
629 EXPECT_CALL(*device1_.get(), GetName())
630 .WillRepeatedly(testing::Return(base::UTF8ToUTF16("Chromebook Pixel")));
631 EXPECT_CALL(*device1_.get(), GetBluetoothClass())
632 .WillRepeatedly(testing::Return(0x080104));
633 EXPECT_CALL(*device1_.get(), GetDeviceType())
634 .WillRepeatedly(testing::Return(BluetoothDevice::DEVICE_COMPUTER));
635 EXPECT_CALL(*device1_.get(), GetVendorIDSource())
636 .WillRepeatedly(testing::Return(BluetoothDevice::VENDOR_ID_BLUETOOTH));
637 EXPECT_CALL(*device1_.get(), GetVendorID())
638 .WillRepeatedly(testing::Return(0x00E0));
639 EXPECT_CALL(*device1_.get(), GetProductID())
640 .WillRepeatedly(testing::Return(0x240A));
641 EXPECT_CALL(*device1_.get(), GetDeviceID())
642 .WillRepeatedly(testing::Return(0x0400));
644 devices.push_back(device1_.get());
646 // Leave the second largely empty so we can check a device without
647 // available information.
648 devices.push_back(device2_.get());
650 EXPECT_CALL(*mock_adapter_, GetDevices())
651 .Times(1)
652 .WillRepeatedly(testing::Return(devices));
654 // Load and wait for setup
655 ExtensionTestMessageListener listener("ready", true);
656 ASSERT_TRUE(
657 LoadExtension(test_data_dir_.AppendASCII("bluetooth/device_info")));
658 EXPECT_TRUE(listener.WaitUntilSatisfied());
660 listener.Reply("go");
662 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();