OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_api.h" | 6 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_api.h" |
7 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_event_router.h" | 7 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_event_router.h" |
8 #include "chrome/browser/extensions/extension_apitest.h" | 8 #include "chrome/browser/extensions/extension_apitest.h" |
9 #include "chrome/browser/extensions/extension_function_test_utils.h" | 9 #include "chrome/browser/extensions/extension_function_test_utils.h" |
10 #include "chrome/browser/extensions/extension_test_message_listener.h" | 10 #include "chrome/browser/extensions/extension_test_message_listener.h" |
11 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 11 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
12 #include "device/bluetooth/test/mock_bluetooth_device.h" | 12 #include "device/bluetooth/test/mock_bluetooth_device.h" |
| 13 #include "device/bluetooth/test/mock_bluetooth_gatt_characteristic.h" |
13 #include "device/bluetooth/test/mock_bluetooth_gatt_service.h" | 14 #include "device/bluetooth/test/mock_bluetooth_gatt_service.h" |
14 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
15 | 16 |
16 using device::BluetoothUUID; | 17 using device::BluetoothUUID; |
17 using device::BluetoothAdapter; | 18 using device::BluetoothAdapter; |
18 using device::BluetoothDevice; | 19 using device::BluetoothDevice; |
| 20 using device::BluetoothGattCharacteristic; |
19 using device::BluetoothGattService; | 21 using device::BluetoothGattService; |
20 using device::MockBluetoothAdapter; | 22 using device::MockBluetoothAdapter; |
21 using device::MockBluetoothDevice; | 23 using device::MockBluetoothDevice; |
| 24 using device::MockBluetoothGattCharacteristic; |
22 using device::MockBluetoothGattService; | 25 using device::MockBluetoothGattService; |
23 using testing::Return; | 26 using testing::Return; |
| 27 using testing::ReturnRefOfCopy; |
24 using testing::_; | 28 using testing::_; |
25 | 29 |
26 namespace utils = extension_function_test_utils; | 30 namespace utils = extension_function_test_utils; |
27 | 31 |
28 namespace { | 32 namespace { |
29 | 33 |
30 const char kTestLeDeviceAddress[] = "11:22:33:44:55:66"; | 34 const char kTestLeDeviceAddress[] = "11:22:33:44:55:66"; |
31 const char kTestLeDeviceName[] = "Test LE Device"; | 35 const char kTestLeDeviceName[] = "Test LE Device"; |
32 | 36 |
33 const char kTestServiceId0[] = "service_id0"; | 37 const char kTestServiceId0[] = "service_id0"; |
34 const char kTestServiceUuid0[] = "1234"; | 38 const char kTestServiceUuid0[] = "1234"; |
| 39 |
35 const char kTestServiceId1[] = "service_id1"; | 40 const char kTestServiceId1[] = "service_id1"; |
36 const char kTestServiceUuid1[] = "5678"; | 41 const char kTestServiceUuid1[] = "5678"; |
37 | 42 |
| 43 const char kTestCharacteristicId0[] = "char_id0"; |
| 44 const char kTestCharacteristicUuid0[] = "1211"; |
| 45 const BluetoothGattCharacteristic::Properties kTestCharacteristicProperties0 = |
| 46 BluetoothGattCharacteristic::kPropertyBroadcast | |
| 47 BluetoothGattCharacteristic::kPropertyRead | |
| 48 BluetoothGattCharacteristic::kPropertyWriteWithoutResponse | |
| 49 BluetoothGattCharacteristic::kPropertyIndicate; |
| 50 const uint8 kTestCharacteristicDefaultValue0[] = {0x01, 0x02, 0x03, 0x04, 0x05}; |
| 51 |
| 52 const char kTestCharacteristicId1[] = "char_id1"; |
| 53 const char kTestCharacteristicUuid1[] = "1212"; |
| 54 const BluetoothGattCharacteristic::Properties kTestCharacteristicProperties1 = |
| 55 BluetoothGattCharacteristic::kPropertyRead | |
| 56 BluetoothGattCharacteristic::kPropertyWrite | |
| 57 BluetoothGattCharacteristic::kPropertyNotify; |
| 58 const uint8 kTestCharacteristicDefaultValue1[] = {0x06, 0x07, 0x08}; |
| 59 |
| 60 const char kTestCharacteristicId2[] = "char_id1"; |
| 61 const char kTestCharacteristicUuid2[] = "1213"; |
| 62 const BluetoothGattCharacteristic::Properties kTestCharacteristicProperties2 = |
| 63 BluetoothGattCharacteristic::kPropertyNone; |
| 64 |
38 class BluetoothLowEnergyApiTest : public ExtensionApiTest { | 65 class BluetoothLowEnergyApiTest : public ExtensionApiTest { |
39 public: | 66 public: |
40 BluetoothLowEnergyApiTest() {} | 67 BluetoothLowEnergyApiTest() {} |
41 | 68 |
42 virtual ~BluetoothLowEnergyApiTest() {} | 69 virtual ~BluetoothLowEnergyApiTest() {} |
43 | 70 |
44 virtual void SetUpOnMainThread() OVERRIDE { | 71 virtual void SetUpOnMainThread() OVERRIDE { |
45 ExtensionApiTest::SetUpOnMainThread(); | 72 ExtensionApiTest::SetUpOnMainThread(); |
46 empty_extension_ = utils::CreateEmptyExtension(); | 73 empty_extension_ = utils::CreateEmptyExtension(); |
47 SetUpMocks(); | 74 SetUpMocks(); |
(...skipping 24 matching lines...) Expand all Loading... |
72 BluetoothUUID(kTestServiceUuid0), | 99 BluetoothUUID(kTestServiceUuid0), |
73 true /* is_primary */, | 100 true /* is_primary */, |
74 false /* is_local */)); | 101 false /* is_local */)); |
75 | 102 |
76 service1_.reset(new testing::NiceMock<MockBluetoothGattService>( | 103 service1_.reset(new testing::NiceMock<MockBluetoothGattService>( |
77 device_.get(), | 104 device_.get(), |
78 kTestServiceId1, | 105 kTestServiceId1, |
79 BluetoothUUID(kTestServiceUuid1), | 106 BluetoothUUID(kTestServiceUuid1), |
80 false /* is_primary */, | 107 false /* is_primary */, |
81 false /* is_local */)); | 108 false /* is_local */)); |
| 109 |
| 110 // Assign characteristics some random properties and permissions. They don't |
| 111 // need to reflect what the characteristic is actually capable of, since |
| 112 // the JS API just passes values through from |
| 113 // device::BluetoothGattCharacteristic. |
| 114 std::vector<uint8> default_value; |
| 115 chrc0_.reset(new testing::NiceMock<MockBluetoothGattCharacteristic>( |
| 116 service0_.get(), |
| 117 kTestCharacteristicId0, |
| 118 BluetoothUUID(kTestCharacteristicUuid0), |
| 119 false /* is_local */, |
| 120 kTestCharacteristicProperties0, |
| 121 BluetoothGattCharacteristic::kPermissionNone)); |
| 122 default_value.assign(kTestCharacteristicDefaultValue0, |
| 123 (kTestCharacteristicDefaultValue0 + |
| 124 sizeof(kTestCharacteristicDefaultValue0))); |
| 125 ON_CALL(*chrc0_, GetValue()).WillByDefault(ReturnRefOfCopy(default_value)); |
| 126 |
| 127 chrc1_.reset(new testing::NiceMock<MockBluetoothGattCharacteristic>( |
| 128 service0_.get(), |
| 129 kTestCharacteristicId1, |
| 130 BluetoothUUID(kTestCharacteristicUuid1), |
| 131 false /* is_local */, |
| 132 kTestCharacteristicProperties1, |
| 133 BluetoothGattCharacteristic::kPermissionNone)); |
| 134 default_value.assign(kTestCharacteristicDefaultValue1, |
| 135 (kTestCharacteristicDefaultValue1 + |
| 136 sizeof(kTestCharacteristicDefaultValue1))); |
| 137 ON_CALL(*chrc1_, GetValue()).WillByDefault(ReturnRefOfCopy(default_value)); |
| 138 |
| 139 chrc2_.reset(new testing::NiceMock<MockBluetoothGattCharacteristic>( |
| 140 service1_.get(), |
| 141 kTestCharacteristicId2, |
| 142 BluetoothUUID(kTestCharacteristicUuid2), |
| 143 false /* is_local */, |
| 144 kTestCharacteristicProperties2, |
| 145 BluetoothGattCharacteristic::kPermissionNone)); |
82 } | 146 } |
83 | 147 |
84 protected: | 148 protected: |
85 extensions::BluetoothLowEnergyEventRouter* event_router() { | 149 extensions::BluetoothLowEnergyEventRouter* event_router() { |
86 return extensions::BluetoothLowEnergyAPI::Get(browser()->profile()) | 150 return extensions::BluetoothLowEnergyAPI::Get(browser()->profile()) |
87 ->event_router(); | 151 ->event_router(); |
88 } | 152 } |
89 | 153 |
90 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_; | 154 testing::StrictMock<MockBluetoothAdapter>* mock_adapter_; |
91 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device_; | 155 scoped_ptr<testing::NiceMock<MockBluetoothDevice> > device_; |
92 scoped_ptr<testing::NiceMock<MockBluetoothGattService> > service0_; | 156 scoped_ptr<testing::NiceMock<MockBluetoothGattService> > service0_; |
93 scoped_ptr<testing::NiceMock<MockBluetoothGattService> > service1_; | 157 scoped_ptr<testing::NiceMock<MockBluetoothGattService> > service1_; |
| 158 scoped_ptr<testing::NiceMock<MockBluetoothGattCharacteristic> > chrc0_; |
| 159 scoped_ptr<testing::NiceMock<MockBluetoothGattCharacteristic> > chrc1_; |
| 160 scoped_ptr<testing::NiceMock<MockBluetoothGattCharacteristic> > chrc2_; |
94 | 161 |
95 private: | 162 private: |
96 scoped_refptr<extensions::Extension> empty_extension_; | 163 scoped_refptr<extensions::Extension> empty_extension_; |
97 }; | 164 }; |
98 | 165 |
99 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetServices) { | 166 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetServices) { |
100 ResultCatcher catcher; | 167 ResultCatcher catcher; |
101 catcher.RestrictToProfile(browser()->profile()); | 168 catcher.RestrictToProfile(browser()->profile()); |
102 | 169 |
103 std::vector<BluetoothGattService*> services; | 170 std::vector<BluetoothGattService*> services; |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 EXPECT_CALL(*device_, GetGattService(kTestServiceId0)) | 316 EXPECT_CALL(*device_, GetGattService(kTestServiceId0)) |
250 .Times(2) | 317 .Times(2) |
251 .WillRepeatedly(Return(service0_.get())); | 318 .WillRepeatedly(Return(service0_.get())); |
252 EXPECT_CALL(*service0_, GetIncludedServices()) | 319 EXPECT_CALL(*service0_, GetIncludedServices()) |
253 .Times(2) | 320 .Times(2) |
254 .WillOnce(Return(std::vector<BluetoothGattService*>())) | 321 .WillOnce(Return(std::vector<BluetoothGattService*>())) |
255 .WillOnce(Return(includes)); | 322 .WillOnce(Return(includes)); |
256 | 323 |
257 listener.Reply("go"); | 324 listener.Reply("go"); |
258 listener.Reset(); | 325 listener.Reset(); |
| 326 |
259 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 327 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
260 | 328 |
261 listener.Reply("go"); | 329 listener.Reply("go"); |
| 330 |
| 331 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 332 event_router()->GattServiceRemoved(device_.get(), service0_.get()); |
| 333 event_router()->DeviceRemoved(mock_adapter_, device_.get()); |
| 334 } |
| 335 |
| 336 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetCharacteristics) { |
| 337 ResultCatcher catcher; |
| 338 catcher.RestrictToProfile(browser()->profile()); |
| 339 |
| 340 std::vector<BluetoothGattCharacteristic*> characteristics; |
| 341 characteristics.push_back(chrc0_.get()); |
| 342 characteristics.push_back(chrc1_.get()); |
| 343 |
| 344 event_router()->DeviceAdded(mock_adapter_, device_.get()); |
| 345 event_router()->GattServiceAdded(device_.get(), service0_.get()); |
| 346 |
| 347 EXPECT_CALL(*mock_adapter_, GetDevice(_)).Times(3).WillRepeatedly( |
| 348 Return(device_.get())); |
| 349 EXPECT_CALL(*device_, GetGattService(kTestServiceId0)) |
| 350 .Times(3) |
| 351 .WillOnce(Return(static_cast<BluetoothGattService*>(NULL))) |
| 352 .WillRepeatedly(Return(service0_.get())); |
| 353 EXPECT_CALL(*service0_, GetCharacteristics()) |
| 354 .Times(2) |
| 355 .WillOnce(Return(std::vector<BluetoothGattCharacteristic*>())) |
| 356 .WillOnce(Return(characteristics)); |
| 357 |
| 358 ExtensionTestMessageListener listener("ready", true); |
| 359 ASSERT_TRUE(LoadExtension( |
| 360 test_data_dir_.AppendASCII("bluetooth_low_energy/get_characteristics"))); |
| 361 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 362 |
| 363 listener.Reply("go"); |
262 | 364 |
263 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 365 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
264 event_router()->GattServiceRemoved(device_.get(), service0_.get()); | 366 event_router()->GattServiceRemoved(device_.get(), service0_.get()); |
265 event_router()->DeviceRemoved(mock_adapter_, device_.get()); | 367 event_router()->DeviceRemoved(mock_adapter_, device_.get()); |
266 } | 368 } |
267 | 369 |
268 } // namespace | 370 } // namespace |
OLD | NEW |