Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / bluetooth_low_energy / bluetooth_low_energy_apitest.cc
blobacf885ea0b385002b987afd49442b4ee06b65e15
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 "device/bluetooth/test/mock_bluetooth_adapter.h"
10 #include "device/bluetooth/test/mock_bluetooth_device.h"
11 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h"
12 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h"
13 #include "device/bluetooth/test/mock_bluetooth_gatt_descriptor.h"
14 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h"
15 #include "device/bluetooth/test/mock_bluetooth_gatt_service.h"
16 #include "extensions/common/test_util.h"
17 #include "extensions/test/extension_test_message_listener.h"
18 #include "extensions/test/result_catcher.h"
19 #include "testing/gmock/include/gmock/gmock.h"
21 using device::BluetoothUUID;
22 using device::BluetoothAdapter;
23 using device::BluetoothDevice;
24 using device::BluetoothGattCharacteristic;
25 using device::BluetoothGattConnection;
26 using device::BluetoothGattDescriptor;
27 using device::BluetoothGattService;
28 using device::BluetoothGattNotifySession;
29 using device::MockBluetoothAdapter;
30 using device::MockBluetoothDevice;
31 using device::MockBluetoothGattCharacteristic;
32 using device::MockBluetoothGattConnection;
33 using device::MockBluetoothGattDescriptor;
34 using device::MockBluetoothGattService;
35 using device::MockBluetoothGattNotifySession;
36 using extensions::BluetoothLowEnergyEventRouter;
37 using extensions::ResultCatcher;
38 using testing::Invoke;
39 using testing::Return;
40 using testing::ReturnRef;
41 using testing::ReturnRefOfCopy;
42 using testing::SaveArg;
43 using testing::_;
45 namespace {
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::PROPERTY_BROADCAST |
65 BluetoothGattCharacteristic::PROPERTY_READ |
66 BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE |
67 BluetoothGattCharacteristic::PROPERTY_INDICATE;
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::PROPERTY_READ |
74 BluetoothGattCharacteristic::PROPERTY_WRITE |
75 BluetoothGattCharacteristic::PROPERTY_NOTIFY;
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::PROPERTY_NONE;
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 {
93 public:
94 BluetoothLowEnergyApiTest() {}
96 ~BluetoothLowEnergyApiTest() override {}
98 void SetUpOnMainThread() override {
99 ExtensionApiTest::SetUpOnMainThread();
100 empty_extension_ = extensions::test_util::CreateEmptyExtension();
101 SetUpMocks();
104 void TearDownOnMainThread() override {
105 EXPECT_CALL(*mock_adapter_, RemoveObserver(_));
108 void SetUpMocks() {
109 mock_adapter_ = new testing::StrictMock<MockBluetoothAdapter>();
110 EXPECT_CALL(*mock_adapter_, GetDevices())
111 .WillOnce(Return(BluetoothAdapter::ConstDeviceList()));
113 event_router()->SetAdapterForTesting(mock_adapter_);
115 device0_.reset(
116 new testing::NiceMock<MockBluetoothDevice>(mock_adapter_,
118 kTestLeDeviceName0,
119 kTestLeDeviceAddress0,
120 false /* paired */,
121 true /* connected */));
123 device1_.reset(
124 new testing::NiceMock<MockBluetoothDevice>(mock_adapter_,
126 kTestLeDeviceName1,
127 kTestLeDeviceAddress1,
128 false /* paired */,
129 false /* connected */));
131 service0_.reset(new testing::NiceMock<MockBluetoothGattService>(
132 device0_.get(),
133 kTestServiceId0,
134 BluetoothUUID(kTestServiceUuid0),
135 true /* is_primary */,
136 false /* is_local */));
138 service1_.reset(new testing::NiceMock<MockBluetoothGattService>(
139 device0_.get(),
140 kTestServiceId1,
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>(
151 service0_.get(),
152 kTestCharacteristicId0,
153 BluetoothUUID(kTestCharacteristicUuid0),
154 false /* is_local */,
155 kTestCharacteristicProperties0,
156 BluetoothGattCharacteristic::PERMISSION_NONE));
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>(
163 service0_.get(),
164 kTestCharacteristicId1,
165 BluetoothUUID(kTestCharacteristicUuid1),
166 false /* is_local */,
167 kTestCharacteristicProperties1,
168 BluetoothGattCharacteristic::PERMISSION_NONE));
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>(
175 service1_.get(),
176 kTestCharacteristicId2,
177 BluetoothUUID(kTestCharacteristicUuid2),
178 false /* is_local */,
179 kTestCharacteristicProperties2,
180 BluetoothGattCharacteristic::PERMISSION_NONE));
182 desc0_.reset(new testing::NiceMock<MockBluetoothGattDescriptor>(
183 chrc0_.get(),
184 kTestDescriptorId0,
185 BluetoothUUID(kTestDescriptorUuid0),
186 false /* is_local */,
187 BluetoothGattCharacteristic::PERMISSION_NONE));
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>(
194 chrc0_.get(),
195 kTestDescriptorId1,
196 BluetoothUUID(kTestDescriptorUuid1),
197 false /* is_local */,
198 BluetoothGattCharacteristic::PERMISSION_NONE));
199 default_value.assign(
200 kTestDescriptorDefaultValue1,
201 (kTestDescriptorDefaultValue1 + sizeof(kTestDescriptorDefaultValue1)));
202 ON_CALL(*desc1_, GetValue()).WillByDefault(ReturnRefOfCopy(default_value));
205 protected:
206 BluetoothLowEnergyEventRouter* event_router() {
207 return extensions::BluetoothLowEnergyAPI::Get(browser()->profile())
208 ->event_router();
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_;
222 private:
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 scoped_refptr<device::BluetoothAdapter> adapter,
246 const std::string& device_address,
247 bool expect_disconnect) {
248 testing::NiceMock<MockBluetoothGattConnection>* conn =
249 new testing::NiceMock<MockBluetoothGattConnection>(adapter,
250 device_address);
251 EXPECT_CALL(*conn, Disconnect()).Times(expect_disconnect ? 1 : 0);
252 return conn;
255 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetServices) {
256 ResultCatcher catcher;
257 catcher.RestrictToBrowserContext(browser()->profile());
259 std::vector<BluetoothGattService*> services;
260 services.push_back(service0_.get());
261 services.push_back(service1_.get());
263 EXPECT_CALL(*mock_adapter_, GetDevice(_))
264 .Times(3)
265 .WillOnce(Return(static_cast<BluetoothDevice*>(NULL)))
266 .WillRepeatedly(Return(device0_.get()));
268 EXPECT_CALL(*device0_, GetGattServices())
269 .Times(2)
270 .WillOnce(Return(std::vector<BluetoothGattService*>()))
271 .WillOnce(Return(services));
273 // Load and wait for setup.
274 ExtensionTestMessageListener listener("ready", true);
275 listener.set_failure_message("fail");
276 ASSERT_TRUE(LoadExtension(
277 test_data_dir_.AppendASCII("bluetooth_low_energy/get_services")));
278 EXPECT_TRUE(listener.WaitUntilSatisfied());
280 listener.Reply("go");
282 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
285 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetService) {
286 ResultCatcher catcher;
287 catcher.RestrictToBrowserContext(browser()->profile());
289 event_router()->GattServiceAdded(
290 mock_adapter_, device0_.get(), service0_.get());
292 EXPECT_CALL(*mock_adapter_, GetDevice(_))
293 .Times(3)
294 .WillOnce(Return(static_cast<BluetoothDevice*>(NULL)))
295 .WillRepeatedly(Return(device0_.get()));
297 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
298 .Times(2)
299 .WillOnce(Return(static_cast<BluetoothGattService*>(NULL)))
300 .WillOnce(Return(service0_.get()));
302 // Load and wait for setup.
303 ExtensionTestMessageListener listener("ready", true);
304 listener.set_failure_message("fail");
305 ASSERT_TRUE(LoadExtension(
306 test_data_dir_.AppendASCII("bluetooth_low_energy/get_service")));
307 EXPECT_TRUE(listener.WaitUntilSatisfied());
309 listener.Reply("go");
311 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
313 event_router()->GattServiceRemoved(
314 mock_adapter_, device0_.get(), service0_.get());
317 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, ServiceEvents) {
318 ResultCatcher catcher;
319 catcher.RestrictToBrowserContext(browser()->profile());
321 // Load the extension and let it set up.
322 ExtensionTestMessageListener listener(true);
323 ASSERT_TRUE(LoadExtension(
324 test_data_dir_.AppendASCII("bluetooth_low_energy/service_events")));
326 // These will create the identifier mappings.
327 event_router()->GattServiceAdded(
328 mock_adapter_, device0_.get(), service0_.get());
329 event_router()->GattServiceAdded(
330 mock_adapter_, device0_.get(), service1_.get());
332 // These will send the onServiceAdded event to apps.
333 event_router()->GattDiscoveryCompleteForService(mock_adapter_,
334 service0_.get());
335 event_router()->GattDiscoveryCompleteForService(mock_adapter_,
336 service1_.get());
338 // This will send the onServiceChanged event to apps.
339 event_router()->GattServiceChanged(mock_adapter_, service1_.get());
341 // This will send the onServiceRemoved event to apps.
342 event_router()->GattServiceRemoved(
343 mock_adapter_, device0_.get(), service0_.get());
345 EXPECT_TRUE(listener.WaitUntilSatisfied());
346 ASSERT_EQ("ready", listener.message()) << listener.message();
347 listener.Reply("go");
349 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
350 event_router()->GattServiceRemoved(
351 mock_adapter_, device0_.get(), service1_.get());
354 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetRemovedService) {
355 ResultCatcher catcher;
356 catcher.RestrictToBrowserContext(browser()->profile());
358 // Load the extension and let it set up.
359 ASSERT_TRUE(LoadExtension(
360 test_data_dir_.AppendASCII("bluetooth_low_energy/get_removed_service")));
362 // 1. getService success.
363 EXPECT_CALL(*mock_adapter_, GetDevice(_))
364 .Times(1)
365 .WillOnce(Return(device0_.get()));
366 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
367 .Times(1)
368 .WillOnce(Return(service0_.get()));
370 event_router()->GattServiceAdded(
371 mock_adapter_, device0_.get(), service0_.get());
372 event_router()->GattDiscoveryCompleteForService(mock_adapter_,
373 service0_.get());
375 ExtensionTestMessageListener get_service_success_listener(true);
376 EXPECT_TRUE(get_service_success_listener.WaitUntilSatisfied());
377 ASSERT_EQ("getServiceSuccess", get_service_success_listener.message())
378 << get_service_success_listener.message();
379 testing::Mock::VerifyAndClearExpectations(mock_adapter_);
380 testing::Mock::VerifyAndClearExpectations(device0_.get());
382 // 2. getService fail.
383 EXPECT_CALL(*mock_adapter_, GetDevice(_)).Times(0);
384 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0)).Times(0);
386 event_router()->GattServiceRemoved(
387 mock_adapter_, device0_.get(), service0_.get());
389 ExtensionTestMessageListener get_service_fail_listener(true);
390 EXPECT_TRUE(get_service_fail_listener.WaitUntilSatisfied());
391 ASSERT_EQ("getServiceFail", get_service_fail_listener.message())
392 << get_service_fail_listener.message();
393 testing::Mock::VerifyAndClearExpectations(mock_adapter_);
394 testing::Mock::VerifyAndClearExpectations(device0_.get());
396 get_service_fail_listener.Reply("go");
398 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
401 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetIncludedServices) {
402 ResultCatcher catcher;
403 catcher.RestrictToBrowserContext(browser()->profile());
405 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
406 "bluetooth_low_energy/get_included_services")));
408 // Wait for initial call to end with failure as there is no mapping.
409 ExtensionTestMessageListener listener("ready", true);
410 listener.set_failure_message("fail");
411 EXPECT_TRUE(listener.WaitUntilSatisfied());
413 // Set up for the rest of the calls before replying. Included services can be
414 // returned even if there is no instance ID mapping for them yet, so no need
415 // to call GattServiceAdded for |service1_| here.
416 event_router()->GattServiceAdded(
417 mock_adapter_, device0_.get(), service0_.get());
419 std::vector<BluetoothGattService*> includes;
420 includes.push_back(service1_.get());
421 EXPECT_CALL(*mock_adapter_, GetDevice(kTestLeDeviceAddress0))
422 .Times(2)
423 .WillRepeatedly(Return(device0_.get()));
424 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
425 .Times(2)
426 .WillRepeatedly(Return(service0_.get()));
427 EXPECT_CALL(*service0_, GetIncludedServices())
428 .Times(2)
429 .WillOnce(Return(std::vector<BluetoothGattService*>()))
430 .WillOnce(Return(includes));
432 listener.Reply("go");
433 listener.Reset();
435 EXPECT_TRUE(listener.WaitUntilSatisfied());
437 listener.Reply("go");
439 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
440 event_router()->GattServiceRemoved(
441 mock_adapter_, device0_.get(), service0_.get());
444 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetCharacteristics) {
445 ResultCatcher catcher;
446 catcher.RestrictToBrowserContext(browser()->profile());
448 std::vector<BluetoothGattCharacteristic*> characteristics;
449 characteristics.push_back(chrc0_.get());
450 characteristics.push_back(chrc1_.get());
452 event_router()->GattServiceAdded(
453 mock_adapter_, device0_.get(), service0_.get());
455 EXPECT_CALL(*mock_adapter_, GetDevice(_)).Times(3).WillRepeatedly(
456 Return(device0_.get()));
457 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
458 .Times(3)
459 .WillOnce(Return(static_cast<BluetoothGattService*>(NULL)))
460 .WillRepeatedly(Return(service0_.get()));
461 EXPECT_CALL(*service0_, GetCharacteristics())
462 .Times(2)
463 .WillOnce(Return(std::vector<BluetoothGattCharacteristic*>()))
464 .WillOnce(Return(characteristics));
466 ExtensionTestMessageListener listener("ready", true);
467 ASSERT_TRUE(LoadExtension(
468 test_data_dir_.AppendASCII("bluetooth_low_energy/get_characteristics")));
469 EXPECT_TRUE(listener.WaitUntilSatisfied());
471 listener.Reply("go");
473 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
474 event_router()->GattServiceRemoved(
475 mock_adapter_, device0_.get(), service0_.get());
478 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetCharacteristic) {
479 ResultCatcher catcher;
480 catcher.RestrictToBrowserContext(browser()->profile());
482 event_router()->GattServiceAdded(
483 mock_adapter_, device0_.get(), service0_.get());
484 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
486 EXPECT_CALL(*mock_adapter_, GetDevice(_))
487 .Times(4)
488 .WillOnce(Return(static_cast<BluetoothDevice*>(NULL)))
489 .WillRepeatedly(Return(device0_.get()));
491 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
492 .Times(3)
493 .WillOnce(Return(static_cast<BluetoothGattService*>(NULL)))
494 .WillRepeatedly(Return(service0_.get()));
496 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
497 .Times(2)
498 .WillOnce(Return(static_cast<BluetoothGattCharacteristic*>(NULL)))
499 .WillOnce(Return(chrc0_.get()));
501 // Load the extension and wait for first test.
502 ExtensionTestMessageListener listener("ready", true);
503 listener.set_failure_message("fail");
504 ASSERT_TRUE(LoadExtension(
505 test_data_dir_.AppendASCII("bluetooth_low_energy/get_characteristic")));
506 EXPECT_TRUE(listener.WaitUntilSatisfied());
508 listener.Reply("go");
510 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
512 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
513 event_router()->GattServiceRemoved(
514 mock_adapter_, device0_.get(), service0_.get());
517 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, CharacteristicProperties) {
518 ResultCatcher catcher;
519 catcher.RestrictToBrowserContext(browser()->profile());
521 event_router()->GattServiceAdded(
522 mock_adapter_, device0_.get(), service0_.get());
523 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
525 EXPECT_CALL(*mock_adapter_, GetDevice(_))
526 .Times(12)
527 .WillRepeatedly(Return(device0_.get()));
528 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
529 .Times(12)
530 .WillRepeatedly(Return(service0_.get()));
531 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
532 .Times(12)
533 .WillRepeatedly(Return(chrc0_.get()));
534 EXPECT_CALL(*chrc0_, GetProperties())
535 .Times(12)
536 .WillOnce(Return(BluetoothGattCharacteristic::PROPERTY_NONE))
537 .WillOnce(Return(BluetoothGattCharacteristic::PROPERTY_BROADCAST))
538 .WillOnce(Return(BluetoothGattCharacteristic::PROPERTY_READ))
539 .WillOnce(
540 Return(BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE))
541 .WillOnce(Return(BluetoothGattCharacteristic::PROPERTY_WRITE))
542 .WillOnce(Return(BluetoothGattCharacteristic::PROPERTY_NOTIFY))
543 .WillOnce(Return(BluetoothGattCharacteristic::PROPERTY_INDICATE))
544 .WillOnce(Return(
545 BluetoothGattCharacteristic::PROPERTY_AUTHENTICATED_SIGNED_WRITES))
546 .WillOnce(
547 Return(BluetoothGattCharacteristic::PROPERTY_EXTENDED_PROPERTIES))
548 .WillOnce(Return(BluetoothGattCharacteristic::PROPERTY_RELIABLE_WRITE))
549 .WillOnce(
550 Return(BluetoothGattCharacteristic::PROPERTY_WRITABLE_AUXILIARIES))
551 .WillOnce(Return(
552 BluetoothGattCharacteristic::PROPERTY_BROADCAST |
553 BluetoothGattCharacteristic::PROPERTY_READ |
554 BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE |
555 BluetoothGattCharacteristic::PROPERTY_WRITE |
556 BluetoothGattCharacteristic::PROPERTY_NOTIFY |
557 BluetoothGattCharacteristic::PROPERTY_INDICATE |
558 BluetoothGattCharacteristic::PROPERTY_AUTHENTICATED_SIGNED_WRITES |
559 BluetoothGattCharacteristic::PROPERTY_EXTENDED_PROPERTIES |
560 BluetoothGattCharacteristic::PROPERTY_RELIABLE_WRITE |
561 BluetoothGattCharacteristic::PROPERTY_WRITABLE_AUXILIARIES));
563 ExtensionTestMessageListener listener("ready", true);
564 listener.set_failure_message("fail");
565 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
566 "bluetooth_low_energy/characteristic_properties")));
567 EXPECT_TRUE(listener.WaitUntilSatisfied());
569 listener.Reply("go");
571 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
573 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
574 event_router()->GattServiceRemoved(
575 mock_adapter_, device0_.get(), service0_.get());
578 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetRemovedCharacteristic) {
579 ResultCatcher catcher;
580 catcher.RestrictToBrowserContext(browser()->profile());
582 EXPECT_CALL(*mock_adapter_, GetDevice(_))
583 .Times(1)
584 .WillOnce(Return(device0_.get()));
585 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
586 .Times(1)
587 .WillOnce(Return(service0_.get()));
588 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
589 .Times(1)
590 .WillOnce(Return(chrc0_.get()));
592 event_router()->GattServiceAdded(
593 mock_adapter_, device0_.get(), service0_.get());
594 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
596 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
597 "bluetooth_low_energy/get_removed_characteristic")));
599 ExtensionTestMessageListener listener(true);
600 EXPECT_TRUE(listener.WaitUntilSatisfied());
601 ASSERT_EQ("ready", listener.message()) << listener.message();
602 testing::Mock::VerifyAndClearExpectations(mock_adapter_);
603 testing::Mock::VerifyAndClearExpectations(device0_.get());
604 testing::Mock::VerifyAndClearExpectations(service0_.get());
606 EXPECT_CALL(*mock_adapter_, GetDevice(_)).Times(0);
607 EXPECT_CALL(*device0_, GetGattService(_)).Times(0);
608 EXPECT_CALL(*service0_, GetCharacteristic(_)).Times(0);
610 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
612 listener.Reply("go");
613 listener.Reset();
614 EXPECT_TRUE(listener.WaitUntilSatisfied());
615 ASSERT_EQ("ready", listener.message()) << listener.message();
617 listener.Reply("go");
619 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
620 event_router()->GattServiceRemoved(
621 mock_adapter_, device0_.get(), service0_.get());
624 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, CharacteristicValueChanged) {
625 ResultCatcher catcher;
626 catcher.RestrictToBrowserContext(browser()->profile());
628 // Cause events to be sent to the extension.
629 event_router()->GattServiceAdded(
630 mock_adapter_, device0_.get(), service0_.get());
631 event_router()->GattServiceAdded(
632 mock_adapter_, device0_.get(), service1_.get());
633 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
634 event_router()->GattCharacteristicAdded(mock_adapter_, chrc2_.get());
636 EXPECT_CALL(*mock_adapter_, GetDevice(_))
637 .Times(2)
638 .WillRepeatedly(Return(device0_.get()));
639 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
640 .Times(1)
641 .WillOnce(Return(service0_.get()));
642 EXPECT_CALL(*device0_, GetGattService(kTestServiceId1))
643 .Times(1)
644 .WillOnce(Return(service1_.get()));
645 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
646 .Times(1)
647 .WillOnce(Return(chrc0_.get()));
648 EXPECT_CALL(*service1_, GetCharacteristic(kTestCharacteristicId2))
649 .Times(1)
650 .WillOnce(Return(chrc2_.get()));
652 BluetoothGattNotifySession* session0 =
653 new testing::NiceMock<MockBluetoothGattNotifySession>(
654 kTestCharacteristicId0);
655 BluetoothGattNotifySession* session1 =
656 new testing::NiceMock<MockBluetoothGattNotifySession>(
657 kTestCharacteristicId2);
659 EXPECT_CALL(*chrc0_, StartNotifySession(_, _))
660 .Times(1)
661 .WillOnce(
662 InvokeCallbackWithScopedPtrArg<0, BluetoothGattNotifySession>(
663 session0));
664 EXPECT_CALL(*chrc2_, StartNotifySession(_, _))
665 .Times(1)
666 .WillOnce(
667 InvokeCallbackWithScopedPtrArg<0, BluetoothGattNotifySession>(
668 session1));
670 ExtensionTestMessageListener listener("ready", true);
671 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
672 "bluetooth_low_energy/characteristic_value_changed")));
674 EXPECT_TRUE(listener.WaitUntilSatisfied());
676 std::vector<uint8> value;
677 event_router()->GattCharacteristicValueChanged(
678 mock_adapter_, chrc0_.get(), value);
679 event_router()->GattCharacteristicValueChanged(
680 mock_adapter_, chrc2_.get(), value);
682 listener.Reply("go");
684 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
685 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc2_.get());
686 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
687 event_router()->GattServiceRemoved(
688 mock_adapter_, device0_.get(), service1_.get());
689 event_router()->GattServiceRemoved(
690 mock_adapter_, device0_.get(), service0_.get());
693 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, ReadCharacteristicValue) {
694 ResultCatcher catcher;
695 catcher.RestrictToBrowserContext(browser()->profile());
697 event_router()->GattServiceAdded(
698 mock_adapter_, device0_.get(), service0_.get());
699 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
701 EXPECT_CALL(*mock_adapter_, GetDevice(_))
702 .Times(3)
703 .WillRepeatedly(Return(device0_.get()));
705 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
706 .Times(3)
707 .WillRepeatedly(Return(service0_.get()));
709 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
710 .Times(3)
711 .WillRepeatedly(Return(chrc0_.get()));
713 std::vector<uint8> value;
714 EXPECT_CALL(*chrc0_, ReadRemoteCharacteristic(_, _))
715 .Times(2)
716 .WillOnce(
717 InvokeCallbackArgument<1>(BluetoothGattService::GATT_ERROR_FAILED))
718 .WillOnce(InvokeCallbackArgument<0>(value));
720 ExtensionTestMessageListener listener("ready", true);
721 listener.set_failure_message("fail");
722 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
723 "bluetooth_low_energy/read_characteristic_value")));
724 listener.WaitUntilSatisfied();
726 listener.Reply("go");
728 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
730 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
731 event_router()->GattServiceRemoved(
732 mock_adapter_, device0_.get(), service0_.get());
735 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, WriteCharacteristicValue) {
736 ResultCatcher catcher;
737 catcher.RestrictToBrowserContext(browser()->profile());
739 event_router()->GattServiceAdded(
740 mock_adapter_, device0_.get(), service0_.get());
741 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
743 EXPECT_CALL(*mock_adapter_, GetDevice(_))
744 .Times(3)
745 .WillRepeatedly(Return(device0_.get()));
747 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
748 .Times(3)
749 .WillRepeatedly(Return(service0_.get()));
751 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
752 .Times(3)
753 .WillRepeatedly(Return(chrc0_.get()));
755 std::vector<uint8> write_value;
756 EXPECT_CALL(*chrc0_, WriteRemoteCharacteristic(_, _, _))
757 .Times(2)
758 .WillOnce(
759 InvokeCallbackArgument<2>(BluetoothGattService::GATT_ERROR_FAILED))
760 .WillOnce(DoAll(SaveArg<0>(&write_value), InvokeCallbackArgument<1>()));
762 EXPECT_CALL(*chrc0_, GetValue()).Times(1).WillOnce(ReturnRef(write_value));
764 ExtensionTestMessageListener listener("ready", true);
765 listener.set_failure_message("fail");
766 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
767 "bluetooth_low_energy/write_characteristic_value")));
768 EXPECT_TRUE(listener.WaitUntilSatisfied());
770 listener.Reply("go");
772 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
774 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
775 event_router()->GattServiceRemoved(
776 mock_adapter_, device0_.get(), service0_.get());
779 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetDescriptors) {
780 ResultCatcher catcher;
781 catcher.RestrictToBrowserContext(browser()->profile());
783 std::vector<BluetoothGattDescriptor*> descriptors;
784 descriptors.push_back(desc0_.get());
785 descriptors.push_back(desc1_.get());
787 event_router()->GattServiceAdded(
788 mock_adapter_, device0_.get(), service0_.get());
789 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
791 EXPECT_CALL(*mock_adapter_, GetDevice(_))
792 .Times(3)
793 .WillRepeatedly(Return(device0_.get()));
794 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
795 .Times(3)
796 .WillRepeatedly(Return(service0_.get()));
797 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
798 .Times(3)
799 .WillOnce(Return(static_cast<BluetoothGattCharacteristic*>(NULL)))
800 .WillRepeatedly(Return(chrc0_.get()));
801 EXPECT_CALL(*chrc0_, GetDescriptors())
802 .Times(2)
803 .WillOnce(Return(std::vector<BluetoothGattDescriptor*>()))
804 .WillOnce(Return(descriptors));
806 ExtensionTestMessageListener listener("ready", true);
807 listener.set_failure_message("fail");
808 ASSERT_TRUE(LoadExtension(
809 test_data_dir_.AppendASCII("bluetooth_low_energy/get_descriptors")));
810 EXPECT_TRUE(listener.WaitUntilSatisfied());
812 listener.Reply("go");
814 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
816 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
817 event_router()->GattServiceRemoved(
818 mock_adapter_, device0_.get(), service0_.get());
821 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetDescriptor) {
822 ResultCatcher catcher;
823 catcher.RestrictToBrowserContext(browser()->profile());
825 event_router()->GattServiceAdded(
826 mock_adapter_, device0_.get(), service0_.get());
827 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
828 event_router()->GattDescriptorAdded(mock_adapter_, desc0_.get());
830 EXPECT_CALL(*mock_adapter_, GetDevice(_))
831 .Times(5)
832 .WillOnce(Return(static_cast<BluetoothDevice*>(NULL)))
833 .WillRepeatedly(Return(device0_.get()));
835 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
836 .Times(4)
837 .WillOnce(Return(static_cast<BluetoothGattService*>(NULL)))
838 .WillRepeatedly(Return(service0_.get()));
840 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
841 .Times(3)
842 .WillOnce(Return(static_cast<BluetoothGattCharacteristic*>(NULL)))
843 .WillRepeatedly(Return(chrc0_.get()));
845 EXPECT_CALL(*chrc0_, GetDescriptor(kTestDescriptorId0))
846 .Times(2)
847 .WillOnce(Return(static_cast<BluetoothGattDescriptor*>(NULL)))
848 .WillOnce(Return(desc0_.get()));
850 // Load the extension and wait for first test.
851 ExtensionTestMessageListener listener("ready", true);
852 listener.set_failure_message("fail");
853 ASSERT_TRUE(LoadExtension(
854 test_data_dir_.AppendASCII("bluetooth_low_energy/get_descriptor")));
855 EXPECT_TRUE(listener.WaitUntilSatisfied());
857 listener.Reply("go");
859 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
861 event_router()->GattDescriptorRemoved(mock_adapter_, desc0_.get());
862 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
863 event_router()->GattServiceRemoved(
864 mock_adapter_, device0_.get(), service0_.get());
867 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetRemovedDescriptor) {
868 ResultCatcher catcher;
869 catcher.RestrictToBrowserContext(browser()->profile());
871 EXPECT_CALL(*mock_adapter_, GetDevice(_))
872 .Times(1)
873 .WillOnce(Return(device0_.get()));
874 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
875 .Times(1)
876 .WillOnce(Return(service0_.get()));
877 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
878 .Times(1)
879 .WillOnce(Return(chrc0_.get()));
880 EXPECT_CALL(*chrc0_, GetDescriptor(kTestDescriptorId0))
881 .Times(1)
882 .WillOnce(Return(desc0_.get()));
884 event_router()->GattServiceAdded(
885 mock_adapter_, device0_.get(), service0_.get());
886 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
887 event_router()->GattDescriptorAdded(mock_adapter_, desc0_.get());
889 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
890 "bluetooth_low_energy/get_removed_descriptor")));
892 ExtensionTestMessageListener listener(true);
893 EXPECT_TRUE(listener.WaitUntilSatisfied());
894 ASSERT_EQ("ready", listener.message()) << listener.message();
895 testing::Mock::VerifyAndClearExpectations(mock_adapter_);
896 testing::Mock::VerifyAndClearExpectations(device0_.get());
897 testing::Mock::VerifyAndClearExpectations(service0_.get());
898 testing::Mock::VerifyAndClearExpectations(chrc0_.get());
900 EXPECT_CALL(*mock_adapter_, GetDevice(_)).Times(0);
901 EXPECT_CALL(*device0_, GetGattService(_)).Times(0);
902 EXPECT_CALL(*service0_, GetCharacteristic(_)).Times(0);
903 EXPECT_CALL(*chrc0_, GetDescriptor(_)).Times(0);
905 event_router()->GattDescriptorRemoved(mock_adapter_, desc0_.get());
907 listener.Reply("go");
908 listener.Reset();
909 EXPECT_TRUE(listener.WaitUntilSatisfied());
910 ASSERT_EQ("ready", listener.message()) << listener.message();
912 listener.Reply("go");
914 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
915 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
916 event_router()->GattServiceRemoved(
917 mock_adapter_, device0_.get(), service0_.get());
920 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, DescriptorValueChanged) {
921 ResultCatcher catcher;
922 catcher.RestrictToBrowserContext(browser()->profile());
924 event_router()->GattServiceAdded(
925 mock_adapter_, device0_.get(), service0_.get());
926 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
927 event_router()->GattDescriptorAdded(mock_adapter_, desc0_.get());
928 event_router()->GattDescriptorAdded(mock_adapter_, desc1_.get());
930 // Load the extension and let it set up.
931 ExtensionTestMessageListener listener("ready", true);
932 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
933 "bluetooth_low_energy/descriptor_value_changed")));
935 // Cause events to be sent to the extension.
936 std::vector<uint8> value;
937 event_router()->GattDescriptorValueChanged(
938 mock_adapter_, desc0_.get(), value);
939 event_router()->GattDescriptorValueChanged(
940 mock_adapter_, desc1_.get(), value);
942 EXPECT_TRUE(listener.WaitUntilSatisfied());
943 listener.Reply("go");
945 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
946 event_router()->GattDescriptorRemoved(mock_adapter_, desc1_.get());
947 event_router()->GattDescriptorRemoved(mock_adapter_, desc0_.get());
948 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
949 event_router()->GattServiceRemoved(
950 mock_adapter_, device0_.get(), service0_.get());
953 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, ReadDescriptorValue) {
954 ResultCatcher catcher;
955 catcher.RestrictToBrowserContext(browser()->profile());
957 event_router()->GattServiceAdded(
958 mock_adapter_, device0_.get(), service0_.get());
959 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
960 event_router()->GattDescriptorAdded(mock_adapter_, desc0_.get());
962 EXPECT_CALL(*mock_adapter_, GetDevice(_))
963 .Times(9)
964 .WillRepeatedly(Return(device0_.get()));
966 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
967 .Times(9)
968 .WillRepeatedly(Return(service0_.get()));
970 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
971 .Times(9)
972 .WillRepeatedly(Return(chrc0_.get()));
974 EXPECT_CALL(*chrc0_, GetDescriptor(kTestDescriptorId0))
975 .Times(9)
976 .WillRepeatedly(Return(desc0_.get()));
978 std::vector<uint8> value;
979 EXPECT_CALL(*desc0_, ReadRemoteDescriptor(_, _))
980 .Times(8)
981 .WillOnce(
982 InvokeCallbackArgument<1>(BluetoothGattService::GATT_ERROR_FAILED))
983 .WillOnce(InvokeCallbackArgument<1>(
984 BluetoothGattService::GATT_ERROR_INVALID_LENGTH))
985 .WillOnce(InvokeCallbackArgument<1>(
986 BluetoothGattService::GATT_ERROR_NOT_PERMITTED))
987 .WillOnce(InvokeCallbackArgument<1>(
988 BluetoothGattService::GATT_ERROR_NOT_AUTHORIZED))
989 .WillOnce(InvokeCallbackArgument<1>(
990 BluetoothGattService::GATT_ERROR_NOT_PAIRED))
991 .WillOnce(InvokeCallbackArgument<1>(
992 BluetoothGattService::GATT_ERROR_NOT_SUPPORTED))
993 .WillOnce(InvokeCallbackArgument<1>(
994 BluetoothGattService::GATT_ERROR_IN_PROGRESS))
995 .WillOnce(InvokeCallbackArgument<0>(value));
997 ExtensionTestMessageListener listener("ready", true);
998 listener.set_failure_message("fail");
999 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1000 "bluetooth_low_energy/read_descriptor_value")));
1001 EXPECT_TRUE(listener.WaitUntilSatisfied());
1003 listener.Reply("go");
1005 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1007 event_router()->GattDescriptorRemoved(mock_adapter_, desc0_.get());
1008 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
1009 event_router()->GattServiceRemoved(
1010 mock_adapter_, device0_.get(), service0_.get());
1013 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, WriteDescriptorValue) {
1014 ResultCatcher catcher;
1015 catcher.RestrictToBrowserContext(browser()->profile());
1017 event_router()->GattServiceAdded(
1018 mock_adapter_, device0_.get(), service0_.get());
1019 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
1020 event_router()->GattDescriptorAdded(mock_adapter_, desc0_.get());
1022 EXPECT_CALL(*mock_adapter_, GetDevice(_))
1023 .Times(3)
1024 .WillRepeatedly(Return(device0_.get()));
1026 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
1027 .Times(3)
1028 .WillRepeatedly(Return(service0_.get()));
1030 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
1031 .Times(3)
1032 .WillRepeatedly(Return(chrc0_.get()));
1034 EXPECT_CALL(*chrc0_, GetDescriptor(kTestDescriptorId0))
1035 .Times(3)
1036 .WillRepeatedly(Return(desc0_.get()));
1038 std::vector<uint8> write_value;
1039 EXPECT_CALL(*desc0_, WriteRemoteDescriptor(_, _, _))
1040 .Times(2)
1041 .WillOnce(
1042 InvokeCallbackArgument<2>(BluetoothGattService::GATT_ERROR_FAILED))
1043 .WillOnce(DoAll(SaveArg<0>(&write_value), InvokeCallbackArgument<1>()));
1045 EXPECT_CALL(*desc0_, GetValue()).Times(1).WillOnce(ReturnRef(write_value));
1047 ExtensionTestMessageListener listener("ready", true);
1048 listener.set_failure_message("fail");
1049 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1050 "bluetooth_low_energy/write_descriptor_value")));
1051 EXPECT_TRUE(listener.WaitUntilSatisfied());
1053 listener.Reply("go");
1055 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1057 event_router()->GattDescriptorRemoved(mock_adapter_, desc0_.get());
1058 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
1059 event_router()->GattServiceRemoved(
1060 mock_adapter_, device0_.get(), service0_.get());
1063 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, PermissionDenied) {
1064 ResultCatcher catcher;
1065 catcher.RestrictToBrowserContext(browser()->profile());
1067 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1068 "bluetooth_low_energy/permission_denied")));
1069 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1072 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, UuidPermissionMethods) {
1073 ResultCatcher catcher;
1074 catcher.RestrictToBrowserContext(browser()->profile());
1076 event_router()->GattServiceAdded(
1077 mock_adapter_, device0_.get(), service0_.get());
1078 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
1079 event_router()->GattDescriptorAdded(mock_adapter_, desc0_.get());
1081 std::vector<BluetoothGattService*> services;
1082 services.push_back(service0_.get());
1084 EXPECT_CALL(*mock_adapter_, GetDevice(_))
1085 .WillRepeatedly(Return(device0_.get()));
1086 EXPECT_CALL(*device0_, GetGattServices()).WillOnce(Return(services));
1087 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
1088 .WillRepeatedly(Return(service0_.get()));
1089 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
1090 .WillRepeatedly(Return(chrc0_.get()));
1091 EXPECT_CALL(*chrc0_, GetDescriptor(kTestDescriptorId0))
1092 .WillRepeatedly(Return(desc0_.get()));
1094 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1095 "bluetooth_low_energy/uuid_permission_methods")));
1096 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1098 event_router()->GattDescriptorRemoved(mock_adapter_, desc0_.get());
1099 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
1100 event_router()->GattServiceRemoved(
1101 mock_adapter_, device0_.get(), service0_.get());
1104 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, UuidPermissionEvents) {
1105 ResultCatcher catcher;
1106 catcher.RestrictToBrowserContext(browser()->profile());
1108 ExtensionTestMessageListener listener(true);
1109 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1110 "bluetooth_low_energy/uuid_permission_events")));
1112 // Cause events to be sent to the extension.
1113 event_router()->GattServiceAdded(
1114 mock_adapter_, device0_.get(), service0_.get());
1115 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
1116 event_router()->GattDescriptorAdded(mock_adapter_, desc0_.get());
1118 std::vector<uint8> value;
1119 event_router()->GattCharacteristicValueChanged(
1120 mock_adapter_, chrc0_.get(), value);
1121 event_router()->GattDescriptorValueChanged(
1122 mock_adapter_, desc0_.get(), value);
1123 event_router()->GattServiceChanged(mock_adapter_, service0_.get());
1125 EXPECT_TRUE(listener.WaitUntilSatisfied());
1126 listener.Reply("go");
1127 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1128 ASSERT_EQ("ready", listener.message()) << listener.message();
1130 event_router()->GattDescriptorRemoved(mock_adapter_, desc0_.get());
1131 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
1132 event_router()->GattServiceRemoved(
1133 mock_adapter_, device0_.get(), service0_.get());
1136 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GattConnection) {
1137 ResultCatcher catcher;
1138 catcher.RestrictToBrowserContext(browser()->profile());
1140 EXPECT_CALL(*mock_adapter_, GetDevice(_))
1141 .WillRepeatedly(Return(static_cast<BluetoothDevice*>(NULL)));
1142 EXPECT_CALL(*mock_adapter_, GetDevice(kTestLeDeviceAddress0))
1143 .WillRepeatedly(Return(device0_.get()));
1144 EXPECT_CALL(*mock_adapter_, GetDevice(kTestLeDeviceAddress1))
1145 .WillRepeatedly(Return(device1_.get()));
1146 EXPECT_CALL(*device0_, CreateGattConnection(_, _))
1147 .Times(9)
1148 .WillOnce(InvokeCallbackArgument<1>(BluetoothDevice::ERROR_FAILED))
1149 .WillOnce(InvokeCallbackArgument<1>(BluetoothDevice::ERROR_INPROGRESS))
1150 .WillOnce(InvokeCallbackArgument<1>(BluetoothDevice::ERROR_AUTH_FAILED))
1151 .WillOnce(InvokeCallbackArgument<1>(BluetoothDevice::ERROR_AUTH_REJECTED))
1152 .WillOnce(InvokeCallbackArgument<1>(BluetoothDevice::ERROR_AUTH_CANCELED))
1153 .WillOnce(InvokeCallbackArgument<1>(BluetoothDevice::ERROR_AUTH_TIMEOUT))
1154 .WillOnce(
1155 InvokeCallbackArgument<1>(BluetoothDevice::ERROR_UNSUPPORTED_DEVICE))
1156 .WillOnce(InvokeCallbackWithScopedPtrArg<0, BluetoothGattConnection>(
1157 CreateGattConnection(mock_adapter_, kTestLeDeviceAddress0,
1158 true /* expect_disconnect */)))
1159 .WillOnce(InvokeCallbackWithScopedPtrArg<0, BluetoothGattConnection>(
1160 CreateGattConnection(mock_adapter_, kTestLeDeviceAddress0,
1161 false /* expect_disconnect */)));
1162 EXPECT_CALL(*device1_, CreateGattConnection(_, _))
1163 .Times(1)
1164 .WillOnce(InvokeCallbackWithScopedPtrArg<0, BluetoothGattConnection>(
1165 CreateGattConnection(mock_adapter_, kTestLeDeviceAddress1,
1166 true /* expect_disconnect */)));
1168 ASSERT_TRUE(LoadExtension(
1169 test_data_dir_.AppendASCII("bluetooth_low_energy/gatt_connection")));
1170 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1173 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, ReconnectAfterDisconnected) {
1174 ResultCatcher catcher;
1175 catcher.RestrictToBrowserContext(browser()->profile());
1177 EXPECT_CALL(*mock_adapter_, GetDevice(kTestLeDeviceAddress0))
1178 .WillRepeatedly(Return(device0_.get()));
1180 MockBluetoothGattConnection* first_conn =
1181 static_cast<MockBluetoothGattConnection*>(CreateGattConnection(
1182 mock_adapter_, kTestLeDeviceAddress0, false /* expect_disconnect */));
1183 EXPECT_CALL(*first_conn, IsConnected())
1184 .Times(2)
1185 .WillOnce(Return(true))
1186 .WillOnce(Return(false));
1188 EXPECT_CALL(*device0_, CreateGattConnection(_, _))
1189 .Times(2)
1190 .WillOnce(InvokeCallbackWithScopedPtrArg<0, BluetoothGattConnection>(
1191 first_conn))
1192 .WillOnce(InvokeCallbackWithScopedPtrArg<0, BluetoothGattConnection>(
1193 CreateGattConnection(mock_adapter_, kTestLeDeviceAddress0,
1194 false /* expect_disconnect */)));
1196 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1197 "bluetooth_low_energy/reconnect_after_disconnected")));
1198 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1201 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, ConnectInProgress) {
1202 ResultCatcher catcher;
1203 catcher.RestrictToBrowserContext(browser()->profile());
1205 EXPECT_CALL(*mock_adapter_, GetDevice(kTestLeDeviceAddress0))
1206 .WillRepeatedly(Return(device0_.get()));
1208 BluetoothDevice::GattConnectionCallback connect_callback;
1210 testing::NiceMock<MockBluetoothGattConnection>* conn =
1211 new testing::NiceMock<MockBluetoothGattConnection>(mock_adapter_,
1212 kTestLeDeviceAddress0);
1213 scoped_ptr<BluetoothGattConnection> conn_ptr(conn);
1214 EXPECT_CALL(*conn, Disconnect()).Times(1);
1216 EXPECT_CALL(*device0_, CreateGattConnection(_, _))
1217 .Times(1)
1218 .WillOnce(SaveArg<0>(&connect_callback));
1220 ExtensionTestMessageListener listener(true);
1221 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1222 "bluetooth_low_energy/connect_in_progress")));
1224 EXPECT_TRUE(listener.WaitUntilSatisfied());
1225 ASSERT_EQ("After 2nd connect fails due to 1st connect being in progress.",
1226 listener.message())
1227 << listener.message();
1228 listener.Reset();
1230 connect_callback.Run(conn_ptr.Pass());
1231 EXPECT_TRUE(listener.WaitUntilSatisfied());
1232 ASSERT_EQ("After 2nd call to disconnect.", listener.message())
1233 << listener.message();
1235 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1238 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, StartStopNotifications) {
1239 ResultCatcher catcher;
1240 catcher.RestrictToBrowserContext(browser()->profile());
1242 event_router()->GattServiceAdded(
1243 mock_adapter_, device0_.get(), service0_.get());
1244 event_router()->GattServiceAdded(
1245 mock_adapter_, device0_.get(), service1_.get());
1246 event_router()->GattCharacteristicAdded(mock_adapter_, chrc0_.get());
1247 event_router()->GattCharacteristicAdded(mock_adapter_, chrc1_.get());
1248 event_router()->GattCharacteristicAdded(mock_adapter_, chrc2_.get());
1250 EXPECT_CALL(*mock_adapter_, GetDevice(_))
1251 .WillRepeatedly(Return(device0_.get()));
1252 EXPECT_CALL(*device0_, GetGattService(kTestServiceId0))
1253 .WillRepeatedly(Return(service0_.get()));
1254 EXPECT_CALL(*device0_, GetGattService(kTestServiceId1))
1255 .WillRepeatedly(Return(service1_.get()));
1256 EXPECT_CALL(*service1_, GetCharacteristic(kTestCharacteristicId2))
1257 .Times(1)
1258 .WillOnce(Return(chrc2_.get()));
1259 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId0))
1260 .Times(2)
1261 .WillRepeatedly(Return(chrc0_.get()));
1262 EXPECT_CALL(*service0_, GetCharacteristic(kTestCharacteristicId1))
1263 .Times(1)
1264 .WillOnce(Return(chrc1_.get()));
1266 BluetoothGattNotifySession* session0 =
1267 new testing::NiceMock<MockBluetoothGattNotifySession>(
1268 kTestCharacteristicId0);
1269 MockBluetoothGattNotifySession* session1 =
1270 new testing::NiceMock<MockBluetoothGattNotifySession>(
1271 kTestCharacteristicId1);
1273 EXPECT_CALL(*session1, Stop(_))
1274 .Times(1)
1275 .WillOnce(InvokeCallbackArgument<0>());
1277 EXPECT_CALL(*chrc0_, StartNotifySession(_, _))
1278 .Times(2)
1279 .WillOnce(
1280 InvokeCallbackArgument<1>(BluetoothGattService::GATT_ERROR_FAILED))
1281 .WillOnce(InvokeCallbackWithScopedPtrArg<0, BluetoothGattNotifySession>(
1282 session0));
1283 EXPECT_CALL(*chrc1_, StartNotifySession(_, _))
1284 .Times(1)
1285 .WillOnce(
1286 InvokeCallbackWithScopedPtrArg<0, BluetoothGattNotifySession>(
1287 session1));
1289 ExtensionTestMessageListener listener("ready", true);
1290 listener.set_failure_message("fail");
1291 ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII(
1292 "bluetooth_low_energy/start_stop_notifications")));
1294 EXPECT_TRUE(listener.WaitUntilSatisfied());
1296 std::vector<uint8> value;
1297 event_router()->GattCharacteristicValueChanged(
1298 mock_adapter_, chrc0_.get(), value);
1299 event_router()->GattCharacteristicValueChanged(
1300 mock_adapter_, chrc1_.get(), value);
1301 event_router()->GattCharacteristicValueChanged(
1302 mock_adapter_, chrc2_.get(), value);
1304 listener.Reply("go");
1306 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
1307 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc2_.get());
1308 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc1_.get());
1309 event_router()->GattCharacteristicRemoved(mock_adapter_, chrc0_.get());
1310 event_router()->GattServiceRemoved(
1311 mock_adapter_, device0_.get(), service1_.get());
1312 event_router()->GattServiceRemoved(
1313 mock_adapter_, device0_.get(), service0_.get());
1316 } // namespace