1 // Copyright 2014 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/memory/scoped_ptr.h"
6 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.h"
7 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_event_router.h"
8 #include "chrome/browser/extensions/extension_apitest.h"
9 #include "chrome/browser/extensions/extension_function_test_utils.h"
10 #include "chrome/browser/extensions/extension_test_message_listener.h"
11 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
12 #include "device/bluetooth/test/mock_bluetooth_device.h"
13 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h"
14 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h"
15 #include "device/bluetooth/test/mock_bluetooth_gatt_descriptor.h"
16 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h"
17 #include "device/bluetooth/test/mock_bluetooth_gatt_service.h"
18 #include "testing/gmock/include/gmock/gmock.h"
20 using device::BluetoothUUID;
21 using device::BluetoothAdapter;
22 using device::BluetoothDevice;
23 using device::BluetoothGattCharacteristic;
24 using device::BluetoothGattConnection;
25 using device::BluetoothGattDescriptor;
26 using device::BluetoothGattService;
27 using device::BluetoothGattNotifySession;
28 using device::MockBluetoothAdapter;
29 using device::MockBluetoothDevice;
30 using device::MockBluetoothGattCharacteristic;
31 using device::MockBluetoothGattConnection;
32 using device::MockBluetoothGattDescriptor;
33 using device::MockBluetoothGattService;
34 using device::MockBluetoothGattNotifySession;
35 using extensions::BluetoothLowEnergyEventRouter;
36 using testing::Invoke;
37 using testing::Return;
38 using testing::ReturnRef;
39 using testing::ReturnRefOfCopy;
40 using testing::SaveArg;
43 namespace utils = extension_function_test_utils;
47 // Test service constants.
48 const char kTestLeDeviceAddress0[] = "11:22:33:44:55:66";
49 const char kTestLeDeviceName0[] = "Test LE Device 0";
51 const char kTestLeDeviceAddress1[] = "77:88:99:AA:BB:CC";
52 const char kTestLeDeviceName1[] = "Test LE Device 1";
54 const char kTestServiceId0[] = "service_id0";
55 const char kTestServiceUuid0[] = "1234";
57 const char kTestServiceId1[] = "service_id1";
58 const char kTestServiceUuid1[] = "5678";
60 // Test characteristic constants.
61 const char kTestCharacteristicId0[] = "char_id0";
62 const char kTestCharacteristicUuid0[] = "1211";
63 const BluetoothGattCharacteristic::Properties kTestCharacteristicProperties0 =
64 BluetoothGattCharacteristic::kPropertyBroadcast |
65 BluetoothGattCharacteristic::kPropertyRead |
66 BluetoothGattCharacteristic::kPropertyWriteWithoutResponse |
67 BluetoothGattCharacteristic::kPropertyIndicate;
68 const uint8 kTestCharacteristicDefaultValue0[] = {0x01, 0x02, 0x03, 0x04, 0x05};
70 const char kTestCharacteristicId1[] = "char_id1";
71 const char kTestCharacteristicUuid1[] = "1212";
72 const BluetoothGattCharacteristic::Properties kTestCharacteristicProperties1 =
73 BluetoothGattCharacteristic::kPropertyRead |
74 BluetoothGattCharacteristic::kPropertyWrite |
75 BluetoothGattCharacteristic::kPropertyNotify;
76 const uint8 kTestCharacteristicDefaultValue1[] = {0x06, 0x07, 0x08};
78 const char kTestCharacteristicId2[] = "char_id2";
79 const char kTestCharacteristicUuid2[] = "1213";
80 const BluetoothGattCharacteristic::Properties kTestCharacteristicProperties2 =
81 BluetoothGattCharacteristic::kPropertyNone;
83 // Test descriptor constants.
84 const char kTestDescriptorId0[] = "desc_id0";
85 const char kTestDescriptorUuid0[] = "1221";
86 const uint8 kTestDescriptorDefaultValue0[] = {0x01, 0x02, 0x03};
88 const char kTestDescriptorId1[] = "desc_id1";
89 const char kTestDescriptorUuid1[] = "1222";
90 const uint8 kTestDescriptorDefaultValue1[] = {0x04, 0x05};
92 class BluetoothLowEnergyApiTest : public ExtensionApiTest {
94 BluetoothLowEnergyApiTest() {}
96 virtual ~BluetoothLowEnergyApiTest() {}
98 virtual void SetUpOnMainThread() OVERRIDE {
99 ExtensionApiTest::SetUpOnMainThread();
100 empty_extension_ = utils::CreateEmptyExtension();
104 virtual void CleanUpOnMainThread() OVERRIDE {
105 EXPECT_CALL(*mock_adapter_, RemoveObserver(_));
109 mock_adapter_ = new testing::StrictMock<MockBluetoothAdapter>();
110 EXPECT_CALL(*mock_adapter_, GetDevices())
111 .WillOnce(Return(BluetoothAdapter::ConstDeviceList()));
113 event_router()->SetAdapterForTesting(mock_adapter_);
116 new testing::NiceMock<MockBluetoothDevice>(mock_adapter_,
119 kTestLeDeviceAddress0,
121 true /* connected */));
124 new testing::NiceMock<MockBluetoothDevice>(mock_adapter_,
127 kTestLeDeviceAddress1,
129 false /* connected */));
131 service0_.reset(new testing::NiceMock<MockBluetoothGattService>(
134 BluetoothUUID(kTestServiceUuid0),
135 true /* is_primary */,
136 false /* is_local */));
138 service1_.reset(new testing::NiceMock<MockBluetoothGattService>(
141 BluetoothUUID(kTestServiceUuid1),
142 false /* is_primary */,
143 false /* is_local */));
145 // Assign characteristics some random properties and permissions. They don't
146 // need to reflect what the characteristic is actually capable of, since
147 // the JS API just passes values through from
148 // device::BluetoothGattCharacteristic.
149 std::vector<uint8> default_value;
150 chrc0_.reset(new testing::NiceMock<MockBluetoothGattCharacteristic>(
152 kTestCharacteristicId0,
153 BluetoothUUID(kTestCharacteristicUuid0),
154 false /* is_local */,
155 kTestCharacteristicProperties0,
156 BluetoothGattCharacteristic::kPermissionNone));
157 default_value.assign(kTestCharacteristicDefaultValue0,
158 (kTestCharacteristicDefaultValue0 +
159 sizeof(kTestCharacteristicDefaultValue0)));
160 ON_CALL(*chrc0_, GetValue()).WillByDefault(ReturnRefOfCopy(default_value));
162 chrc1_.reset(new testing::NiceMock<MockBluetoothGattCharacteristic>(
164 kTestCharacteristicId1,
165 BluetoothUUID(kTestCharacteristicUuid1),
166 false /* is_local */,
167 kTestCharacteristicProperties1,
168 BluetoothGattCharacteristic::kPermissionNone));
169 default_value.assign(kTestCharacteristicDefaultValue1,
170 (kTestCharacteristicDefaultValue1 +
171 sizeof(kTestCharacteristicDefaultValue1)));
172 ON_CALL(*chrc1_, GetValue()).WillByDefault(ReturnRefOfCopy(default_value));
174 chrc2_.reset(new testing::NiceMock<MockBluetoothGattCharacteristic>(
176 kTestCharacteristicId2,
177 BluetoothUUID(kTestCharacteristicUuid2),
178 false /* is_local */,
179 kTestCharacteristicProperties2,
180 BluetoothGattCharacteristic::kPermissionNone));
182 desc0_.reset(new testing::NiceMock<MockBluetoothGattDescriptor>(
185 BluetoothUUID(kTestDescriptorUuid0),
186 false /* is_local */,
187 BluetoothGattCharacteristic::kPermissionNone));
188 default_value.assign(
189 kTestDescriptorDefaultValue0,
190 (kTestDescriptorDefaultValue0 + sizeof(kTestDescriptorDefaultValue0)));
191 ON_CALL(*desc0_, GetValue()).WillByDefault(ReturnRefOfCopy(default_value));
193 desc1_.reset(new testing::NiceMock<MockBluetoothGattDescriptor>(
196 BluetoothUUID(kTestDescriptorUuid1),
197 false /* is_local */,
198 BluetoothGattCharacteristic::kPermissionNone));
199 default_value.assign(
200 kTestDescriptorDefaultValue1,
201 (kTestDescriptorDefaultValue1 + sizeof(kTestDescriptorDefaultValue1)));
202 ON_CALL(*desc1_, GetValue()).WillByDefault(ReturnRefOfCopy(default_value));
206 BluetoothLowEnergyEventRouter* event_router() {
207 return extensions::BluetoothLowEnergyAPI::Get(browser()->profile())
211 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_;
212 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device0_;
213 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device1_;
214 scoped_ptr<testing::NiceMock<MockBluetoothGattService> > service0_;
215 scoped_ptr<testing::NiceMock<MockBluetoothGattService> > service1_;
216 scoped_ptr<testing::NiceMock<MockBluetoothGattCharacteristic> > chrc0_;
217 scoped_ptr<testing::NiceMock<MockBluetoothGattCharacteristic> > chrc1_;
218 scoped_ptr<testing::NiceMock<MockBluetoothGattCharacteristic> > chrc2_;
219 scoped_ptr<testing::NiceMock<MockBluetoothGattDescriptor> > desc0_;
220 scoped_ptr<testing::NiceMock<MockBluetoothGattDescriptor> > desc1_;
223 scoped_refptr<extensions::Extension> empty_extension_;
226 ACTION_TEMPLATE(InvokeCallbackArgument,
227 HAS_1_TEMPLATE_PARAMS(int, k),
228 AND_0_VALUE_PARAMS()) {
229 ::std::tr1::get<k>(args).Run();
232 ACTION_TEMPLATE(InvokeCallbackArgument,
233 HAS_1_TEMPLATE_PARAMS(int, k),
234 AND_1_VALUE_PARAMS(p0)) {
235 ::std::tr1::get<k>(args).Run(p0);
238 ACTION_TEMPLATE(InvokeCallbackWithScopedPtrArg,
239 HAS_2_TEMPLATE_PARAMS(int, k, typename, T),
240 AND_1_VALUE_PARAMS(p0)) {
241 ::std::tr1::get<k>(args).Run(scoped_ptr<T>(p0));
244 BluetoothGattConnection* CreateGattConnection(
245 const std::string& device_address,
246 bool expect_disconnect) {
247 testing::NiceMock<MockBluetoothGattConnection>* conn =
248 new testing::NiceMock<MockBluetoothGattConnection>(device_address);
250 if (expect_disconnect) {
251 EXPECT_CALL(*conn, Disconnect(_))
253 .WillOnce(InvokeCallbackArgument<0>());
255 EXPECT_CALL(*conn, Disconnect(_)).Times(0);
261 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetServices) {
262 ResultCatcher catcher;
263 catcher.RestrictToProfile(browser()->profile());
265 std::vector<BluetoothGattService*> services;
266 services.push_back(service0_.get());
267 services.push_back(service1_.get());
269 EXPECT_CALL(*mock_adapter_, GetDevice(_))
271 .WillOnce(Return(static_cast<BluetoothDevice*>(NULL)))
272 .WillRepeatedly(Return(device0_.get()));
274 EXPECT_CALL(*device0_, GetGattServices())
276 .WillOnce(Return(std::vector<BluetoothGattService*>()))
277 .WillOnce(Return(services));
279 // Load and wait for setup.
280 ExtensionTestMessageListener listener("ready", true);
281 ASSERT_TRUE(LoadExtension(
282 test_data_dir_.AppendASCII("bluetooth_low_energy/get_services")));
283 EXPECT_TRUE(listener.WaitUntilSatisfied());
285 listener.Reply("go");
287 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
290 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetService) {
291 ResultCatcher catcher;
292 catcher.RestrictToProfile(browser()->profile());
294 event_router()->DeviceAdded(mock_adapter_, device0_.get());
295 event_router()->GattServiceAdded(device0_.get(), service0_.get());
297 EXPECT_CALL(*mock_adapter_, GetDevice(_))
299 .WillOnce(Return(static_cast<BluetoothDevice*>(NULL)))
300 .WillRepeatedly(Return(device0_.get()));
302 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
304 .WillOnce(Return(static_cast<BluetoothGattService*>(NULL)))
305 .WillOnce(Return(service0_.get()));
307 // Load and wait for setup.
308 ExtensionTestMessageListener listener("ready", true);
309 ASSERT_TRUE(LoadExtension(
310 test_data_dir_.AppendASCII("bluetooth_low_energy/get_service")));
311 EXPECT_TRUE(listener.WaitUntilSatisfied());
313 listener.Reply("go");
315 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
317 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
318 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
321 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, ServiceEvents) {
322 ResultCatcher catcher;
323 catcher.RestrictToProfile(browser()->profile());
325 // Load the extension and let it set up.
326 ExtensionTestMessageListener listener("ready", true);
327 ASSERT_TRUE(LoadExtension(
328 test_data_dir_.AppendASCII("bluetooth_low_energy/service_events")));
330 // Cause events to be sent to the extension.
331 event_router()->DeviceAdded(mock_adapter_, device0_.get());
333 event_router()->GattServiceAdded(device0_.get(), service0_.get());
334 event_router()->GattServiceAdded(device0_.get(), service1_.get());
335 event_router()->GattServiceChanged(service1_.get());
336 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
338 EXPECT_TRUE(listener.WaitUntilSatisfied());
339 listener.Reply("go");
341 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
342 event_router()->GattServiceRemoved(device0_.get(), service1_.get());
343 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
346 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetRemovedService) {
347 ResultCatcher catcher;
348 catcher.RestrictToProfile(browser()->profile());
350 // Load the extension and let it set up.
351 ASSERT_TRUE(LoadExtension(
352 test_data_dir_.AppendASCII("bluetooth_low_energy/get_removed_service")));
354 // 1. getService success.
355 EXPECT_CALL(*mock_adapter_, GetDevice(_))
357 .WillOnce(Return(device0_.get()));
358 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
360 .WillOnce(Return(service0_.get()));
362 event_router()->DeviceAdded(mock_adapter_, device0_.get());
363 event_router()->GattServiceAdded(device0_.get(), service0_.get());
365 ExtensionTestMessageListener get_service_success_listener("getServiceSuccess",
367 EXPECT_TRUE(get_service_success_listener.WaitUntilSatisfied());
368 testing::Mock::VerifyAndClearExpectations(mock_adapter_);
369 testing::Mock::VerifyAndClearExpectations(device0_.get());
371 // 2. getService fail.
372 EXPECT_CALL(*mock_adapter_, GetDevice(_)).Times(0);
373 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0)).Times(0);
375 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
377 ExtensionTestMessageListener get_service_fail_listener("getServiceFail",
379 EXPECT_TRUE(get_service_fail_listener.WaitUntilSatisfied());
380 testing::Mock::VerifyAndClearExpectations(mock_adapter_);
381 testing::Mock::VerifyAndClearExpectations(device0_.get());
383 get_service_fail_listener.Reply("go");
385 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
386 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
389 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetIncludedServices) {
390 ResultCatcher catcher;
391 catcher.RestrictToProfile(browser()->profile());
393 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
394 "bluetooth_low_energy/get_included_services")));
396 // Wait for initial call to end with failure as there is no mapping.
397 ExtensionTestMessageListener listener("ready", true);
398 EXPECT_TRUE(listener.WaitUntilSatisfied());
400 // Set up for the rest of the calls before replying. Included services can be
401 // returned even if there is no instance ID mapping for them yet, so no need
402 // to call GattServiceAdded for |service1_| here.
403 event_router()->DeviceAdded(mock_adapter_, device0_.get());
404 event_router()->GattServiceAdded(device0_.get(), service0_.get());
406 std::vector<BluetoothGattService*> includes;
407 includes.push_back(service1_.get());
408 EXPECT_CALL(*mock_adapter_, GetDevice(kTestLeDeviceAddress0))
410 .WillRepeatedly(Return(device0_.get()));
411 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
413 .WillRepeatedly(Return(service0_.get()));
414 EXPECT_CALL(*service0_, GetIncludedServices())
416 .WillOnce(Return(std::vector<BluetoothGattService*>()))
417 .WillOnce(Return(includes));
419 listener.Reply("go");
422 EXPECT_TRUE(listener.WaitUntilSatisfied());
424 listener.Reply("go");
426 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
427 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
428 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
431 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetCharacteristics) {
432 ResultCatcher catcher;
433 catcher.RestrictToProfile(browser()->profile());
435 std::vector<BluetoothGattCharacteristic*> characteristics;
436 characteristics.push_back(chrc0_.get());
437 characteristics.push_back(chrc1_.get());
439 event_router()->DeviceAdded(mock_adapter_, device0_.get());
440 event_router()->GattServiceAdded(device0_.get(), service0_.get());
442 EXPECT_CALL(*mock_adapter_, GetDevice(_)).Times(3).WillRepeatedly(
443 Return(device0_.get()));
444 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
446 .WillOnce(Return(static_cast<BluetoothGattService*>(NULL)))
447 .WillRepeatedly(Return(service0_.get()));
448 EXPECT_CALL(*service0_, GetCharacteristics())
450 .WillOnce(Return(std::vector<BluetoothGattCharacteristic*>()))
451 .WillOnce(Return(characteristics));
453 ExtensionTestMessageListener listener("ready", true);
454 ASSERT_TRUE(LoadExtension(
455 test_data_dir_.AppendASCII("bluetooth_low_energy/get_characteristics")));
456 EXPECT_TRUE(listener.WaitUntilSatisfied());
458 listener.Reply("go");
460 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
461 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
462 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
465 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetCharacteristic) {
466 ResultCatcher catcher;
467 catcher.RestrictToProfile(browser()->profile());
469 event_router()->DeviceAdded(mock_adapter_, device0_.get());
470 event_router()->GattServiceAdded(device0_.get(), service0_.get());
471 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
473 EXPECT_CALL(*mock_adapter_, GetDevice(_))
475 .WillOnce(Return(static_cast<BluetoothDevice*>(NULL)))
476 .WillRepeatedly(Return(device0_.get()));
478 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
480 .WillOnce(Return(static_cast<BluetoothGattService*>(NULL)))
481 .WillRepeatedly(Return(service0_.get()));
483 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
485 .WillOnce(Return(static_cast<BluetoothGattCharacteristic*>(NULL)))
486 .WillOnce(Return(chrc0_.get()));
488 // Load the extension and wait for first test.
489 ExtensionTestMessageListener listener("ready", true);
490 ASSERT_TRUE(LoadExtension(
491 test_data_dir_.AppendASCII("bluetooth_low_energy/get_characteristic")));
492 EXPECT_TRUE(listener.WaitUntilSatisfied());
494 listener.Reply("go");
496 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
498 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
499 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
500 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
503 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, CharacteristicProperties) {
504 ResultCatcher catcher;
505 catcher.RestrictToProfile(browser()->profile());
507 event_router()->DeviceAdded(mock_adapter_, device0_.get());
508 event_router()->GattServiceAdded(device0_.get(), service0_.get());
509 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
511 EXPECT_CALL(*mock_adapter_, GetDevice(_))
513 .WillRepeatedly(Return(device0_.get()));
514 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
516 .WillRepeatedly(Return(service0_.get()));
517 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
519 .WillRepeatedly(Return(chrc0_.get()));
520 EXPECT_CALL(*chrc0_, GetProperties())
522 .WillOnce(Return(BluetoothGattCharacteristic::kPropertyNone))
523 .WillOnce(Return(BluetoothGattCharacteristic::kPropertyBroadcast))
524 .WillOnce(Return(BluetoothGattCharacteristic::kPropertyRead))
526 Return(BluetoothGattCharacteristic::kPropertyWriteWithoutResponse))
527 .WillOnce(Return(BluetoothGattCharacteristic::kPropertyWrite))
528 .WillOnce(Return(BluetoothGattCharacteristic::kPropertyNotify))
529 .WillOnce(Return(BluetoothGattCharacteristic::kPropertyIndicate))
531 BluetoothGattCharacteristic::kPropertyAuthenticatedSignedWrites))
533 Return(BluetoothGattCharacteristic::kPropertyExtendedProperties))
534 .WillOnce(Return(BluetoothGattCharacteristic::kPropertyReliableWrite))
536 Return(BluetoothGattCharacteristic::kPropertyWritableAuxiliaries))
538 BluetoothGattCharacteristic::kPropertyBroadcast |
539 BluetoothGattCharacteristic::kPropertyRead |
540 BluetoothGattCharacteristic::kPropertyWriteWithoutResponse |
541 BluetoothGattCharacteristic::kPropertyWrite |
542 BluetoothGattCharacteristic::kPropertyNotify |
543 BluetoothGattCharacteristic::kPropertyIndicate |
544 BluetoothGattCharacteristic::kPropertyAuthenticatedSignedWrites |
545 BluetoothGattCharacteristic::kPropertyExtendedProperties |
546 BluetoothGattCharacteristic::kPropertyReliableWrite |
547 BluetoothGattCharacteristic::kPropertyWritableAuxiliaries));
549 ExtensionTestMessageListener listener("ready", true);
550 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
551 "bluetooth_low_energy/characteristic_properties")));
552 EXPECT_TRUE(listener.WaitUntilSatisfied());
554 listener.Reply("go");
556 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
558 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
559 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
560 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
563 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetRemovedCharacteristic) {
564 ResultCatcher catcher;
565 catcher.RestrictToProfile(browser()->profile());
567 EXPECT_CALL(*mock_adapter_, GetDevice(_))
569 .WillOnce(Return(device0_.get()));
570 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
572 .WillOnce(Return(service0_.get()));
573 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
575 .WillOnce(Return(chrc0_.get()));
577 event_router()->DeviceAdded(mock_adapter_, device0_.get());
578 event_router()->GattServiceAdded(device0_.get(), service0_.get());
579 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
581 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
582 "bluetooth_low_energy/get_removed_characteristic")));
584 ExtensionTestMessageListener listener("ready", true);
585 EXPECT_TRUE(listener.WaitUntilSatisfied());
586 testing::Mock::VerifyAndClearExpectations(mock_adapter_);
587 testing::Mock::VerifyAndClearExpectations(device0_.get());
588 testing::Mock::VerifyAndClearExpectations(service0_.get());
590 EXPECT_CALL(*mock_adapter_, GetDevice(_)).Times(0);
591 EXPECT_CALL(*device0_, GetGattService(_)).Times(0);
592 EXPECT_CALL(*service0_, GetCharacteristic(_)).Times(0);
594 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
596 listener.Reply("go");
598 EXPECT_TRUE(listener.WaitUntilSatisfied());
600 listener.Reply("go");
602 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
603 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
604 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
607 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, CharacteristicValueChanged) {
608 ResultCatcher catcher;
609 catcher.RestrictToProfile(browser()->profile());
611 // Cause events to be sent to the extension.
612 event_router()->DeviceAdded(mock_adapter_, device0_.get());
613 event_router()->GattServiceAdded(device0_.get(), service0_.get());
614 event_router()->GattServiceAdded(device0_.get(), service1_.get());
615 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
616 event_router()->GattCharacteristicAdded(service1_.get(), chrc2_.get());
618 EXPECT_CALL(*mock_adapter_, GetDevice(_))
620 .WillRepeatedly(Return(device0_.get()));
621 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
623 .WillOnce(Return(service0_.get()));
624 EXPECT_CALL(*device0_, GetGattService(kTestServiceId1))
626 .WillOnce(Return(service1_.get()));
627 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
629 .WillOnce(Return(chrc0_.get()));
630 EXPECT_CALL(*service1_, GetCharacteristic(kTestCharacteristicId2))
632 .WillOnce(Return(chrc2_.get()));
634 BluetoothGattNotifySession* session0 =
635 new testing::NiceMock<MockBluetoothGattNotifySession>(
636 kTestCharacteristicId0);
637 BluetoothGattNotifySession* session1 =
638 new testing::NiceMock<MockBluetoothGattNotifySession>(
639 kTestCharacteristicId2);
641 EXPECT_CALL(*chrc0_, StartNotifySession(_, _))
644 InvokeCallbackWithScopedPtrArg<0, BluetoothGattNotifySession>(
646 EXPECT_CALL(*chrc2_, StartNotifySession(_, _))
649 InvokeCallbackWithScopedPtrArg<0, BluetoothGattNotifySession>(
652 ExtensionTestMessageListener listener("ready", true);
653 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
654 "bluetooth_low_energy/characteristic_value_changed")));
656 EXPECT_TRUE(listener.WaitUntilSatisfied());
658 std::vector<uint8> value;
659 event_router()->GattCharacteristicValueChanged(
660 service0_.get(), chrc0_.get(), value);
661 event_router()->GattCharacteristicValueChanged(
662 service1_.get(), chrc2_.get(), value);
664 listener.Reply("go");
666 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
667 event_router()->GattCharacteristicRemoved(service1_.get(), chrc2_.get());
668 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
669 event_router()->GattServiceRemoved(device0_.get(), service1_.get());
670 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
671 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
674 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, ReadCharacteristicValue) {
675 ResultCatcher catcher;
676 catcher.RestrictToProfile(browser()->profile());
678 event_router()->DeviceAdded(mock_adapter_, device0_.get());
679 event_router()->GattServiceAdded(device0_.get(), service0_.get());
680 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
682 EXPECT_CALL(*mock_adapter_, GetDevice(_))
684 .WillRepeatedly(Return(device0_.get()));
686 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
688 .WillRepeatedly(Return(service0_.get()));
690 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
692 .WillRepeatedly(Return(chrc0_.get()));
694 std::vector<uint8> value;
695 EXPECT_CALL(*chrc0_, ReadRemoteCharacteristic(_, _))
697 .WillOnce(InvokeCallbackArgument<1>())
698 .WillOnce(InvokeCallbackArgument<0>(value));
700 ExtensionTestMessageListener listener("ready", true);
701 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
702 "bluetooth_low_energy/read_characteristic_value")));
703 EXPECT_TRUE(listener.WaitUntilSatisfied());
705 listener.Reply("go");
707 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
709 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
710 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
711 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
714 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, WriteCharacteristicValue) {
715 ResultCatcher catcher;
716 catcher.RestrictToProfile(browser()->profile());
718 event_router()->DeviceAdded(mock_adapter_, device0_.get());
719 event_router()->GattServiceAdded(device0_.get(), service0_.get());
720 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
722 EXPECT_CALL(*mock_adapter_, GetDevice(_))
724 .WillRepeatedly(Return(device0_.get()));
726 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
728 .WillRepeatedly(Return(service0_.get()));
730 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
732 .WillRepeatedly(Return(chrc0_.get()));
734 std::vector<uint8> write_value;
735 EXPECT_CALL(*chrc0_, WriteRemoteCharacteristic(_, _, _))
737 .WillOnce(InvokeCallbackArgument<2>())
738 .WillOnce(DoAll(SaveArg<0>(&write_value), InvokeCallbackArgument<1>()));
740 EXPECT_CALL(*chrc0_, GetValue()).Times(1).WillOnce(ReturnRef(write_value));
742 ExtensionTestMessageListener listener("ready", true);
743 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
744 "bluetooth_low_energy/write_characteristic_value")));
745 EXPECT_TRUE(listener.WaitUntilSatisfied());
747 listener.Reply("go");
749 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
751 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
752 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
753 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
756 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetDescriptors) {
757 ResultCatcher catcher;
758 catcher.RestrictToProfile(browser()->profile());
760 std::vector<BluetoothGattDescriptor*> descriptors;
761 descriptors.push_back(desc0_.get());
762 descriptors.push_back(desc1_.get());
764 event_router()->DeviceAdded(mock_adapter_, device0_.get());
765 event_router()->GattServiceAdded(device0_.get(), service0_.get());
766 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
768 EXPECT_CALL(*mock_adapter_, GetDevice(_))
770 .WillRepeatedly(Return(device0_.get()));
771 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
773 .WillRepeatedly(Return(service0_.get()));
774 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
776 .WillOnce(Return(static_cast<BluetoothGattCharacteristic*>(NULL)))
777 .WillRepeatedly(Return(chrc0_.get()));
778 EXPECT_CALL(*chrc0_, GetDescriptors())
780 .WillOnce(Return(std::vector<BluetoothGattDescriptor*>()))
781 .WillOnce(Return(descriptors));
783 ExtensionTestMessageListener listener("ready", true);
784 ASSERT_TRUE(LoadExtension(
785 test_data_dir_.AppendASCII("bluetooth_low_energy/get_descriptors")));
786 EXPECT_TRUE(listener.WaitUntilSatisfied());
788 listener.Reply("go");
790 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
792 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
793 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
794 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
797 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetDescriptor) {
798 ResultCatcher catcher;
799 catcher.RestrictToProfile(browser()->profile());
801 event_router()->DeviceAdded(mock_adapter_, device0_.get());
802 event_router()->GattServiceAdded(device0_.get(), service0_.get());
803 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
804 event_router()->GattDescriptorAdded(chrc0_.get(), desc0_.get());
806 EXPECT_CALL(*mock_adapter_, GetDevice(_))
808 .WillOnce(Return(static_cast<BluetoothDevice*>(NULL)))
809 .WillRepeatedly(Return(device0_.get()));
811 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
813 .WillOnce(Return(static_cast<BluetoothGattService*>(NULL)))
814 .WillRepeatedly(Return(service0_.get()));
816 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
818 .WillOnce(Return(static_cast<BluetoothGattCharacteristic*>(NULL)))
819 .WillRepeatedly(Return(chrc0_.get()));
821 EXPECT_CALL(*chrc0_, GetDescriptor(kTestDescriptorId0))
823 .WillOnce(Return(static_cast<BluetoothGattDescriptor*>(NULL)))
824 .WillOnce(Return(desc0_.get()));
826 // Load the extension and wait for first test.
827 ExtensionTestMessageListener listener("ready", true);
828 ASSERT_TRUE(LoadExtension(
829 test_data_dir_.AppendASCII("bluetooth_low_energy/get_descriptor")));
830 EXPECT_TRUE(listener.WaitUntilSatisfied());
832 listener.Reply("go");
834 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
836 event_router()->GattDescriptorRemoved(chrc0_.get(), desc0_.get());
837 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
838 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
839 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
842 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetRemovedDescriptor) {
843 ResultCatcher catcher;
844 catcher.RestrictToProfile(browser()->profile());
846 EXPECT_CALL(*mock_adapter_, GetDevice(_))
848 .WillOnce(Return(device0_.get()));
849 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
851 .WillOnce(Return(service0_.get()));
852 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
854 .WillOnce(Return(chrc0_.get()));
855 EXPECT_CALL(*chrc0_, GetDescriptor(kTestDescriptorId0))
857 .WillOnce(Return(desc0_.get()));
859 event_router()->DeviceAdded(mock_adapter_, device0_.get());
860 event_router()->GattServiceAdded(device0_.get(), service0_.get());
861 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
862 event_router()->GattDescriptorAdded(chrc0_.get(), desc0_.get());
864 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
865 "bluetooth_low_energy/get_removed_descriptor")));
867 ExtensionTestMessageListener listener("ready", true);
868 EXPECT_TRUE(listener.WaitUntilSatisfied());
869 testing::Mock::VerifyAndClearExpectations(mock_adapter_);
870 testing::Mock::VerifyAndClearExpectations(device0_.get());
871 testing::Mock::VerifyAndClearExpectations(service0_.get());
872 testing::Mock::VerifyAndClearExpectations(chrc0_.get());
874 EXPECT_CALL(*mock_adapter_, GetDevice(_)).Times(0);
875 EXPECT_CALL(*device0_, GetGattService(_)).Times(0);
876 EXPECT_CALL(*service0_, GetCharacteristic(_)).Times(0);
877 EXPECT_CALL(*chrc0_, GetDescriptor(_)).Times(0);
879 event_router()->GattDescriptorRemoved(chrc0_.get(), desc0_.get());
881 listener.Reply("go");
883 EXPECT_TRUE(listener.WaitUntilSatisfied());
885 listener.Reply("go");
887 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
888 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
889 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
890 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
893 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, DescriptorValueChanged) {
894 ResultCatcher catcher;
895 catcher.RestrictToProfile(browser()->profile());
897 event_router()->DeviceAdded(mock_adapter_, device0_.get());
898 event_router()->GattServiceAdded(device0_.get(), service0_.get());
899 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
900 event_router()->GattDescriptorAdded(chrc0_.get(), desc0_.get());
901 event_router()->GattDescriptorAdded(chrc0_.get(), desc1_.get());
903 // Load the extension and let it set up.
904 ExtensionTestMessageListener listener("ready", true);
905 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
906 "bluetooth_low_energy/descriptor_value_changed")));
908 // Cause events to be sent to the extension.
909 std::vector<uint8> value;
910 event_router()->GattDescriptorValueChanged(chrc0_.get(), desc0_.get(), value);
911 event_router()->GattDescriptorValueChanged(chrc0_.get(), desc1_.get(), value);
913 EXPECT_TRUE(listener.WaitUntilSatisfied());
914 listener.Reply("go");
916 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
917 event_router()->GattDescriptorRemoved(chrc0_.get(), desc1_.get());
918 event_router()->GattDescriptorRemoved(chrc0_.get(), desc0_.get());
919 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
920 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
921 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
924 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, ReadDescriptorValue) {
925 ResultCatcher catcher;
926 catcher.RestrictToProfile(browser()->profile());
928 event_router()->DeviceAdded(mock_adapter_, device0_.get());
929 event_router()->GattServiceAdded(device0_.get(), service0_.get());
930 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
931 event_router()->GattDescriptorAdded(chrc0_.get(), desc0_.get());
933 EXPECT_CALL(*mock_adapter_, GetDevice(_))
935 .WillRepeatedly(Return(device0_.get()));
937 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
939 .WillRepeatedly(Return(service0_.get()));
941 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
943 .WillRepeatedly(Return(chrc0_.get()));
945 EXPECT_CALL(*chrc0_, GetDescriptor(kTestDescriptorId0))
947 .WillRepeatedly(Return(desc0_.get()));
949 std::vector<uint8> value;
950 EXPECT_CALL(*desc0_, ReadRemoteDescriptor(_, _))
952 .WillOnce(InvokeCallbackArgument<1>())
953 .WillOnce(InvokeCallbackArgument<0>(value));
955 ExtensionTestMessageListener listener("ready", true);
956 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
957 "bluetooth_low_energy/read_descriptor_value")));
958 EXPECT_TRUE(listener.WaitUntilSatisfied());
960 listener.Reply("go");
962 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
964 event_router()->GattDescriptorRemoved(chrc0_.get(), desc0_.get());
965 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
966 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
967 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
970 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, WriteDescriptorValue) {
971 ResultCatcher catcher;
972 catcher.RestrictToProfile(browser()->profile());
974 event_router()->DeviceAdded(mock_adapter_, device0_.get());
975 event_router()->GattServiceAdded(device0_.get(), service0_.get());
976 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
977 event_router()->GattDescriptorAdded(chrc0_.get(), desc0_.get());
979 EXPECT_CALL(*mock_adapter_, GetDevice(_))
981 .WillRepeatedly(Return(device0_.get()));
983 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
985 .WillRepeatedly(Return(service0_.get()));
987 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
989 .WillRepeatedly(Return(chrc0_.get()));
991 EXPECT_CALL(*chrc0_, GetDescriptor(kTestDescriptorId0))
993 .WillRepeatedly(Return(desc0_.get()));
995 std::vector<uint8> write_value;
996 EXPECT_CALL(*desc0_, WriteRemoteDescriptor(_, _, _))
998 .WillOnce(InvokeCallbackArgument<2>())
999 .WillOnce(DoAll(SaveArg<0>(&write_value), InvokeCallbackArgument<1>()));
1001 EXPECT_CALL(*desc0_, GetValue()).Times(1).WillOnce(ReturnRef(write_value));
1003 ExtensionTestMessageListener listener("ready", true);
1004 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1005 "bluetooth_low_energy/write_descriptor_value")));
1006 EXPECT_TRUE(listener.WaitUntilSatisfied());
1008 listener.Reply("go");
1010 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1012 event_router()->GattDescriptorRemoved(chrc0_.get(), desc0_.get());
1013 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
1014 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
1015 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
1018 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, PermissionDenied) {
1019 ResultCatcher catcher;
1020 catcher.RestrictToProfile(browser()->profile());
1022 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1023 "bluetooth_low_energy/permission_denied")));
1024 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1027 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, UuidPermissionMethods) {
1028 ResultCatcher catcher;
1029 catcher.RestrictToProfile(browser()->profile());
1031 event_router()->DeviceAdded(mock_adapter_, device0_.get());
1032 event_router()->GattServiceAdded(device0_.get(), service0_.get());
1033 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
1034 event_router()->GattDescriptorAdded(chrc0_.get(), desc0_.get());
1036 std::vector<BluetoothGattService*> services;
1037 services.push_back(service0_.get());
1039 EXPECT_CALL(*mock_adapter_, GetDevice(_))
1040 .WillRepeatedly(Return(device0_.get()));
1041 EXPECT_CALL(*device0_, GetGattServices()).WillOnce(Return(services));
1042 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
1043 .WillRepeatedly(Return(service0_.get()));
1044 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
1045 .WillRepeatedly(Return(chrc0_.get()));
1046 EXPECT_CALL(*chrc0_, GetDescriptor(kTestDescriptorId0))
1047 .WillRepeatedly(Return(desc0_.get()));
1049 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1050 "bluetooth_low_energy/uuid_permission_methods")));
1051 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1053 event_router()->GattDescriptorRemoved(chrc0_.get(), desc0_.get());
1054 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
1055 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
1056 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
1059 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, UuidPermissionEvents) {
1060 ResultCatcher catcher;
1061 catcher.RestrictToProfile(browser()->profile());
1063 ExtensionTestMessageListener listener("ready", true);
1064 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1065 "bluetooth_low_energy/uuid_permission_events")));
1067 // Cause events to be sent to the extension.
1068 event_router()->DeviceAdded(mock_adapter_, device0_.get());
1069 event_router()->GattServiceAdded(device0_.get(), service0_.get());
1070 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
1071 event_router()->GattDescriptorAdded(chrc0_.get(), desc0_.get());
1073 std::vector<uint8> value;
1074 event_router()->GattCharacteristicValueChanged(
1075 service0_.get(), chrc0_.get(), value);
1076 event_router()->GattDescriptorValueChanged(chrc0_.get(), desc0_.get(), value);
1077 event_router()->GattServiceChanged(service0_.get());
1079 EXPECT_TRUE(listener.WaitUntilSatisfied());
1080 listener.Reply("go");
1081 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1083 event_router()->GattDescriptorRemoved(chrc0_.get(), desc0_.get());
1084 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
1085 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
1086 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
1089 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GattConnection) {
1090 ResultCatcher catcher;
1091 catcher.RestrictToProfile(browser()->profile());
1093 event_router()->DeviceAdded(mock_adapter_, device0_.get());
1094 event_router()->DeviceAdded(mock_adapter_, device1_.get());
1096 EXPECT_CALL(*mock_adapter_, GetDevice(_))
1097 .WillRepeatedly(Return(static_cast<BluetoothDevice*>(NULL)));
1098 EXPECT_CALL(*mock_adapter_, GetDevice(kTestLeDeviceAddress0))
1099 .WillRepeatedly(Return(device0_.get()));
1100 EXPECT_CALL(*mock_adapter_, GetDevice(kTestLeDeviceAddress1))
1101 .WillRepeatedly(Return(device1_.get()));
1102 EXPECT_CALL(*device0_, CreateGattConnection(_, _))
1104 .WillOnce(InvokeCallbackArgument<1>(BluetoothDevice::ERROR_FAILED))
1105 .WillOnce(InvokeCallbackWithScopedPtrArg<0, BluetoothGattConnection>(
1106 CreateGattConnection(kTestLeDeviceAddress0,
1107 true /* expect_disconnect */)))
1108 .WillOnce(InvokeCallbackWithScopedPtrArg<0, BluetoothGattConnection>(
1109 CreateGattConnection(kTestLeDeviceAddress0,
1110 false /* expect_disconnect */)));
1111 EXPECT_CALL(*device1_, CreateGattConnection(_, _))
1113 .WillOnce(InvokeCallbackWithScopedPtrArg<0, BluetoothGattConnection>(
1114 CreateGattConnection(kTestLeDeviceAddress1,
1115 true /* expect_disconnect */)));
1117 ASSERT_TRUE(LoadExtension(
1118 test_data_dir_.AppendASCII("bluetooth_low_energy/gatt_connection")));
1119 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1121 event_router()->DeviceRemoved(mock_adapter_, device1_.get());
1122 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
1125 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, ReconnectAfterDisconnected) {
1126 ResultCatcher catcher;
1127 catcher.RestrictToProfile(browser()->profile());
1129 event_router()->DeviceAdded(mock_adapter_, device0_.get());
1131 EXPECT_CALL(*mock_adapter_, GetDevice(kTestLeDeviceAddress0))
1132 .WillRepeatedly(Return(device0_.get()));
1134 MockBluetoothGattConnection* first_conn =
1135 static_cast<MockBluetoothGattConnection*>(CreateGattConnection(
1136 kTestLeDeviceAddress0, false /* expect_disconnect */));
1137 EXPECT_CALL(*first_conn, IsConnected())
1139 .WillOnce(Return(true))
1140 .WillOnce(Return(false));
1142 EXPECT_CALL(*device0_, CreateGattConnection(_, _))
1144 .WillOnce(InvokeCallbackWithScopedPtrArg<0, BluetoothGattConnection>(
1146 .WillOnce(InvokeCallbackWithScopedPtrArg<0, BluetoothGattConnection>(
1147 CreateGattConnection(kTestLeDeviceAddress0,
1148 false /* expect_disconnect */)));
1150 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1151 "bluetooth_low_energy/reconnect_after_disconnected")));
1152 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1154 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
1157 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, ConnectInProgress) {
1158 ResultCatcher catcher;
1159 catcher.RestrictToProfile(browser()->profile());
1161 event_router()->DeviceAdded(mock_adapter_, device0_.get());
1163 EXPECT_CALL(*mock_adapter_, GetDevice(kTestLeDeviceAddress0))
1164 .WillRepeatedly(Return(device0_.get()));
1166 BluetoothDevice::GattConnectionCallback connect_callback;
1167 base::Closure disconnect_callback;
1169 testing::NiceMock<MockBluetoothGattConnection>* conn =
1170 new testing::NiceMock<MockBluetoothGattConnection>(
1171 kTestLeDeviceAddress0);
1172 scoped_ptr<BluetoothGattConnection> conn_ptr(conn);
1173 EXPECT_CALL(*conn, Disconnect(_))
1175 .WillOnce(SaveArg<0>(&disconnect_callback));
1177 EXPECT_CALL(*device0_, CreateGattConnection(_, _))
1179 .WillOnce(SaveArg<0>(&connect_callback));
1181 ExtensionTestMessageListener listener("ready", true);
1182 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1183 "bluetooth_low_energy/connect_in_progress")));
1185 listener.WaitUntilSatisfied();
1186 connect_callback.Run(conn_ptr.Pass());
1189 listener.WaitUntilSatisfied();
1190 disconnect_callback.Run();
1192 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1194 event_router()->DeviceRemoved(mock_adapter_, device0_.get());
1197 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, StartStopNotifications) {
1198 ResultCatcher catcher;
1199 catcher.RestrictToProfile(browser()->profile());
1201 event_router()->DeviceAdded(mock_adapter_, device0_.get());
1202 event_router()->GattServiceAdded(device0_.get(), service0_.get());
1203 event_router()->GattServiceAdded(device0_.get(), service1_.get());
1204 event_router()->GattCharacteristicAdded(service0_.get(), chrc0_.get());
1205 event_router()->GattCharacteristicAdded(service0_.get(), chrc1_.get());
1206 event_router()->GattCharacteristicAdded(service1_.get(), chrc2_.get());
1208 EXPECT_CALL(*mock_adapter_, GetDevice(_))
1209 .WillRepeatedly(Return(device0_.get()));
1210 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
1211 .WillRepeatedly(Return(service0_.get()));
1212 EXPECT_CALL(*device0_, GetGattService(kTestServiceId1))
1213 .WillRepeatedly(Return(service1_.get()));
1214 EXPECT_CALL(*service1_, GetCharacteristic(kTestCharacteristicId2))
1216 .WillOnce(Return(chrc2_.get()));
1217 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
1219 .WillRepeatedly(Return(chrc0_.get()));
1220 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId1))
1222 .WillOnce(Return(chrc1_.get()));
1224 BluetoothGattNotifySession* session0 =
1225 new testing::NiceMock<MockBluetoothGattNotifySession>(
1226 kTestCharacteristicId0);
1227 MockBluetoothGattNotifySession* session1 =
1228 new testing::NiceMock<MockBluetoothGattNotifySession>(
1229 kTestCharacteristicId1);
1231 EXPECT_CALL(*session1, Stop(_))
1233 .WillOnce(InvokeCallbackArgument<0>());
1235 EXPECT_CALL(*chrc0_, StartNotifySession(_, _))
1237 .WillOnce(InvokeCallbackArgument<1>())
1239 InvokeCallbackWithScopedPtrArg<0, BluetoothGattNotifySession>(
1241 EXPECT_CALL(*chrc1_, StartNotifySession(_, _))
1244 InvokeCallbackWithScopedPtrArg<0, BluetoothGattNotifySession>(
1247 ExtensionTestMessageListener listener("ready", true);
1248 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1249 "bluetooth_low_energy/start_stop_notifications")));
1251 EXPECT_TRUE(listener.WaitUntilSatisfied());
1253 std::vector<uint8> value;
1254 event_router()->GattCharacteristicValueChanged(
1255 service0_.get(), chrc0_.get(), value);
1256 event_router()->GattCharacteristicValueChanged(
1257 service0_.get(), chrc1_.get(), value);
1258 event_router()->GattCharacteristicValueChanged(
1259 service1_.get(), chrc2_.get(), value);
1261 listener.Reply("go");
1263 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1264 event_router()->GattCharacteristicRemoved(service1_.get(), chrc2_.get());
1265 event_router()->GattCharacteristicRemoved(service0_.get(), chrc1_.get());
1266 event_router()->GattCharacteristicRemoved(service0_.get(), chrc0_.get());
1267 event_router()->GattServiceRemoved(device0_.get(), service1_.get());
1268 event_router()->GattServiceRemoved(device0_.get(), service0_.get());
1269 event_router()->DeviceRemoved(mock_adapter_, device0_.get());