Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(537)

Side by Side Diff: chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_apitest.cc

Issue 255053002: chrome.bluetoothLowEnergy: Implement getCharacteristics. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 EXPECT_TRUE(listener.WaitUntilSatisfied()); 216 EXPECT_TRUE(listener.WaitUntilSatisfied());
150 217
151 listener.Reply("go"); 218 listener.Reply("go");
152 219
153 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); 220 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
154 221
155 event_router()->GattServiceRemoved(device_.get(), service0_.get()); 222 event_router()->GattServiceRemoved(device_.get(), service0_.get());
156 event_router()->DeviceRemoved(mock_adapter_, device_.get()); 223 event_router()->DeviceRemoved(mock_adapter_, device_.get());
157 } 224 }
158 225
226 IN_PROC_BROWSER_TEST_F(BluetoothLowEnergyApiTest, GetCharacteristics) {
227 ResultCatcher catcher;
228 catcher.RestrictToProfile(browser()->profile());
229
230 std::vector<BluetoothGattCharacteristic*> characteristics;
231 characteristics.push_back(chrc0_.get());
232 characteristics.push_back(chrc1_.get());
233
234 event_router()->DeviceAdded(mock_adapter_, device_.get());
235 event_router()->GattServiceAdded(device_.get(), service0_.get());
236
237 EXPECT_CALL(*mock_adapter_, GetDevice(_)).Times(3).WillRepeatedly(
238 Return(device_.get()));
239 EXPECT_CALL(*device_, GetGattService(kTestServiceId0))
240 .Times(3)
241 .WillOnce(Return(static_cast<BluetoothGattService*>(NULL)))
242 .WillRepeatedly(Return(service0_.get()));
243 EXPECT_CALL(*service0_, GetCharacteristics())
244 .Times(2)
245 .WillOnce(Return(std::vector<BluetoothGattCharacteristic*>()))
246 .WillOnce(Return(characteristics));
247
248 ExtensionTestMessageListener listener("ready", true);
249 ASSERT_TRUE(LoadExtension(
250 test_data_dir_.AppendASCII("bluetooth_low_energy/get_characteristics")));
251 EXPECT_TRUE(listener.WaitUntilSatisfied());
252
253 listener.Reply("go");
254
255 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message();
256
257 event_router()->GattServiceRemoved(device_.get(), service0_.get());
258 event_router()->DeviceRemoved(mock_adapter_, device_.get());
259 }
260
159 } // namespace 261 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698