OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 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 <string.h> | 5 #include <string.h> |
6 | 6 |
7 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 7 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" |
8 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h" | 8 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_adapter.h" |
9 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_device.h" | 9 #include "chrome/browser/chromeos/bluetooth/test/mock_bluetooth_device.h" |
10 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" | 10 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 namespace { | 26 namespace { |
27 | 27 |
28 class BluetoothApiTest : public PlatformAppApiTest { | 28 class BluetoothApiTest : public PlatformAppApiTest { |
29 public: | 29 public: |
30 BluetoothApiTest() : empty_extension_(utils::CreateEmptyExtension()) {} | 30 BluetoothApiTest() : empty_extension_(utils::CreateEmptyExtension()) {} |
31 | 31 |
32 virtual void SetUpOnMainThread() OVERRIDE { | 32 virtual void SetUpOnMainThread() OVERRIDE { |
33 // The browser will clean this up when it is torn down | 33 // The browser will clean this up when it is torn down |
34 mock_adapter_ = new testing::StrictMock<chromeos::MockBluetoothAdapter>; | 34 mock_adapter_ = new testing::StrictMock<chromeos::MockBluetoothAdapter>; |
35 event_router()->SetAdapterForTest(mock_adapter_); | 35 event_router()->SetAdapterForTest(mock_adapter_); |
| 36 |
| 37 device1_.reset(new testing::NiceMock<chromeos::MockBluetoothDevice>( |
| 38 mock_adapter_, "d1", "11:12:13:14:15:16", |
| 39 true /* paired */, false /* bonded */, true /* connected */)); |
| 40 device2_.reset(new testing::NiceMock<chromeos::MockBluetoothDevice>( |
| 41 mock_adapter_, "d2", "21:22:23:24:25:26", |
| 42 false /* paired */, true /* bonded */, false /* connected */)); |
36 } | 43 } |
37 | 44 |
38 void expectBooleanResult(bool expected, | 45 void expectBooleanResult(bool expected, |
39 UIThreadExtensionFunction* function, | 46 UIThreadExtensionFunction* function, |
40 const std::string& args) { | 47 const std::string& args) { |
41 scoped_ptr<base::Value> result( | 48 scoped_ptr<base::Value> result( |
42 utils::RunFunctionAndReturnResult(function, args, browser())); | 49 utils::RunFunctionAndReturnResult(function, args, browser())); |
43 ASSERT_TRUE(result.get() != NULL); | 50 ASSERT_TRUE(result.get() != NULL); |
44 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); | 51 ASSERT_EQ(base::Value::TYPE_BOOLEAN, result->GetType()); |
45 bool boolean_value; | 52 bool boolean_value; |
46 result->GetAsBoolean(&boolean_value); | 53 result->GetAsBoolean(&boolean_value); |
47 EXPECT_EQ(expected, boolean_value); | 54 EXPECT_EQ(expected, boolean_value); |
48 } | 55 } |
49 | 56 |
50 template <class T> | 57 template <class T> |
51 T* setupFunction(T* function) { | 58 T* setupFunction(T* function) { |
52 function->set_extension(empty_extension_.get()); | 59 function->set_extension(empty_extension_.get()); |
53 function->set_has_callback(true); | 60 function->set_has_callback(true); |
54 return function; | 61 return function; |
55 } | 62 } |
56 | 63 |
57 protected: | 64 protected: |
58 testing::StrictMock<chromeos::MockBluetoothAdapter>* mock_adapter_; | 65 testing::StrictMock<chromeos::MockBluetoothAdapter>* mock_adapter_; |
| 66 scoped_ptr<testing::NiceMock<chromeos::MockBluetoothDevice> > device1_; |
| 67 scoped_ptr<testing::NiceMock<chromeos::MockBluetoothDevice> > device2_; |
59 | 68 |
60 chromeos::ExtensionBluetoothEventRouter* event_router() { | 69 chromeos::ExtensionBluetoothEventRouter* event_router() { |
61 return browser()->profile()->GetExtensionService()-> | 70 return browser()->profile()->GetExtensionService()-> |
62 bluetooth_event_router(); | 71 bluetooth_event_router(); |
63 } | 72 } |
64 | 73 |
65 private: | 74 private: |
66 scoped_refptr<Extension> empty_extension_; | 75 scoped_refptr<Extension> empty_extension_; |
67 }; | 76 }; |
68 | 77 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 | 129 |
121 testing::Mock::VerifyAndClearExpectations(mock_adapter_); | 130 testing::Mock::VerifyAndClearExpectations(mock_adapter_); |
122 EXPECT_CALL(*mock_adapter_, IsPowered()) | 131 EXPECT_CALL(*mock_adapter_, IsPowered()) |
123 .WillOnce(testing::Return(true)); | 132 .WillOnce(testing::Return(true)); |
124 | 133 |
125 is_powered = setupFunction(new api::BluetoothIsPoweredFunction); | 134 is_powered = setupFunction(new api::BluetoothIsPoweredFunction); |
126 expectBooleanResult(true, is_powered, "[]"); | 135 expectBooleanResult(true, is_powered, "[]"); |
127 } | 136 } |
128 | 137 |
129 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { | 138 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { |
130 testing::NiceMock<chromeos::MockBluetoothDevice> device1( | |
131 mock_adapter_, "d1", "11:12:13:14:15:16", | |
132 true /* paired */, false /* bonded */, true /* connected */); | |
133 testing::NiceMock<chromeos::MockBluetoothDevice> device2( | |
134 mock_adapter_, "d2", "21:22:23:24:25:26", | |
135 false /* paired */, true /* bonded */, false /* connected */); | |
136 chromeos::BluetoothAdapter::ConstDeviceList devices; | 139 chromeos::BluetoothAdapter::ConstDeviceList devices; |
137 devices.push_back(&device1); | 140 devices.push_back(device1_.get()); |
138 devices.push_back(&device2); | 141 devices.push_back(device2_.get()); |
139 | 142 |
140 EXPECT_CALL(device1, ProvidesServiceWithUUID("foo")) | 143 EXPECT_CALL(*device1_, ProvidesServiceWithUUID("foo")) |
141 .WillOnce(testing::Return(false)); | 144 .WillOnce(testing::Return(false)); |
142 EXPECT_CALL(device2, ProvidesServiceWithUUID("foo")) | 145 EXPECT_CALL(*device2_, ProvidesServiceWithUUID("foo")) |
143 .WillOnce(testing::Return(true)); | 146 .WillOnce(testing::Return(true)); |
144 | 147 |
145 EXPECT_CALL(*mock_adapter_, GetDevices()) | 148 EXPECT_CALL(*mock_adapter_, GetDevices()) |
146 .WillOnce(testing::Return(devices)); | 149 .WillOnce(testing::Return(devices)); |
147 | 150 |
148 scoped_refptr<api::BluetoothGetDevicesFunction> get_devices; | 151 scoped_refptr<api::BluetoothGetDevicesFunction> get_devices; |
149 | 152 |
150 get_devices = setupFunction(new api::BluetoothGetDevicesFunction); | 153 get_devices = setupFunction(new api::BluetoothGetDevicesFunction); |
151 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnResult( | 154 scoped_ptr<base::Value> result(utils::RunFunctionAndReturnResult( |
152 get_devices, | 155 get_devices, |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 get_oob_function = | 232 get_oob_function = |
230 setupFunction(new api::BluetoothGetLocalOutOfBandPairingDataFunction); | 233 setupFunction(new api::BluetoothGetLocalOutOfBandPairingDataFunction); |
231 | 234 |
232 std::string error( | 235 std::string error( |
233 utils::RunFunctionAndReturnError(get_oob_function, "[]", browser())); | 236 utils::RunFunctionAndReturnError(get_oob_function, "[]", browser())); |
234 EXPECT_FALSE(error.empty()); | 237 EXPECT_FALSE(error.empty()); |
235 } | 238 } |
236 | 239 |
237 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) { | 240 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) { |
238 std::string device_address("11:12:13:14:15:16"); | 241 std::string device_address("11:12:13:14:15:16"); |
239 testing::NiceMock<chromeos::MockBluetoothDevice> device( | |
240 mock_adapter_, "d1", device_address, | |
241 true /* paired */, false /* bonded */, true /* connected */); | |
242 EXPECT_CALL(*mock_adapter_, GetDevice(device_address)) | 242 EXPECT_CALL(*mock_adapter_, GetDevice(device_address)) |
243 .WillOnce(testing::Return(&device)); | 243 .WillOnce(testing::Return(device1_.get())); |
244 EXPECT_CALL(device, | 244 EXPECT_CALL(*device1_, |
245 ClearOutOfBandPairingData(testing::Truly(CallClosure), | 245 ClearOutOfBandPairingData(testing::Truly(CallClosure), |
246 testing::_)); | 246 testing::_)); |
247 | 247 |
248 char buf[64]; | 248 char buf[64]; |
249 snprintf(buf, sizeof(buf), | 249 snprintf(buf, sizeof(buf), |
250 "[{\"deviceAddress\":\"%s\"}]", device_address.c_str()); | 250 "[{\"deviceAddress\":\"%s\"}]", device_address.c_str()); |
251 std::string params(buf); | 251 std::string params(buf); |
252 | 252 |
253 scoped_refptr<api::BluetoothSetOutOfBandPairingDataFunction> set_oob_function; | 253 scoped_refptr<api::BluetoothSetOutOfBandPairingDataFunction> set_oob_function; |
254 set_oob_function = setupFunction( | 254 set_oob_function = setupFunction( |
255 new api::BluetoothSetOutOfBandPairingDataFunction); | 255 new api::BluetoothSetOutOfBandPairingDataFunction); |
256 // There isn't actually a result. | 256 // There isn't actually a result. |
257 (void)utils::RunFunctionAndReturnResult(set_oob_function, params, browser()); | 257 (void)utils::RunFunctionAndReturnResult(set_oob_function, params, browser()); |
258 | 258 |
259 // Try again with an error | 259 // Try again with an error |
260 testing::Mock::VerifyAndClearExpectations(mock_adapter_); | 260 testing::Mock::VerifyAndClearExpectations(mock_adapter_); |
261 testing::Mock::VerifyAndClearExpectations(&device); | 261 testing::Mock::VerifyAndClearExpectations(device1_.get()); |
262 EXPECT_CALL(*mock_adapter_, GetDevice(device_address)) | 262 EXPECT_CALL(*mock_adapter_, GetDevice(device_address)) |
263 .WillOnce(testing::Return(&device)); | 263 .WillOnce(testing::Return(device1_.get())); |
264 EXPECT_CALL(device, | 264 EXPECT_CALL(*device1_, |
265 ClearOutOfBandPairingData(testing::_, | 265 ClearOutOfBandPairingData(testing::_, |
266 testing::Truly(CallClosure))); | 266 testing::Truly(CallClosure))); |
267 | 267 |
268 set_oob_function = setupFunction( | 268 set_oob_function = setupFunction( |
269 new api::BluetoothSetOutOfBandPairingDataFunction); | 269 new api::BluetoothSetOutOfBandPairingDataFunction); |
270 std::string error( | 270 std::string error( |
271 utils::RunFunctionAndReturnError(set_oob_function, params, browser())); | 271 utils::RunFunctionAndReturnError(set_oob_function, params, browser())); |
272 EXPECT_FALSE(error.empty()); | 272 EXPECT_FALSE(error.empty()); |
273 | 273 |
274 // TODO(bryeung): Also test setting the data when there is support for | 274 // TODO(bryeung): Also test setting the data when there is support for |
275 // ArrayBuffers in the arguments to the RunFunctionAnd* methods. | 275 // ArrayBuffers in the arguments to the RunFunctionAnd* methods. |
276 // crbug.com/132796 | 276 // crbug.com/132796 |
277 } | 277 } |
278 | 278 |
279 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) { | 279 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) { |
280 // TODO(bryeung): test that no events are sent now (crbug.com/132616) | |
281 | |
282 // Try with a failure to start | 280 // Try with a failure to start |
283 EXPECT_CALL(*mock_adapter_, | 281 EXPECT_CALL(*mock_adapter_, |
284 SetDiscovering(true, | 282 SetDiscovering(true, |
285 testing::_, | 283 testing::_, |
286 testing::Truly(CallClosure))); | 284 testing::Truly(CallClosure))); |
287 scoped_refptr<api::BluetoothStartDiscoveryFunction> start_function; | 285 scoped_refptr<api::BluetoothStartDiscoveryFunction> start_function; |
288 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); | 286 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); |
289 std::string error( | 287 std::string error( |
290 utils::RunFunctionAndReturnError(start_function, "[]", browser())); | 288 utils::RunFunctionAndReturnError(start_function, "[]", browser())); |
291 ASSERT_TRUE(!error.empty()); | 289 ASSERT_TRUE(!error.empty()); |
292 | 290 |
293 // Reset for a successful start | 291 // Reset for a successful start |
294 testing::Mock::VerifyAndClearExpectations(mock_adapter_); | 292 testing::Mock::VerifyAndClearExpectations(mock_adapter_); |
295 EXPECT_CALL(*mock_adapter_, | 293 EXPECT_CALL(*mock_adapter_, |
296 SetDiscovering(true, | 294 SetDiscovering(true, |
297 testing::Truly(CallClosure), | 295 testing::Truly(CallClosure), |
298 testing::_)); | 296 testing::_)); |
299 | 297 |
300 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); | 298 start_function = setupFunction(new api::BluetoothStartDiscoveryFunction); |
301 (void)utils::RunFunctionAndReturnError(start_function, "[]", browser()); | 299 (void)utils::RunFunctionAndReturnError(start_function, "[]", browser()); |
302 | 300 |
303 // TODO(bryeung): test that events are sent now (crbug.com/132616) | |
304 | |
305 // Reset to try stopping | 301 // Reset to try stopping |
306 testing::Mock::VerifyAndClearExpectations(mock_adapter_); | 302 testing::Mock::VerifyAndClearExpectations(mock_adapter_); |
307 EXPECT_CALL(*mock_adapter_, | 303 EXPECT_CALL(*mock_adapter_, |
308 SetDiscovering(false, | 304 SetDiscovering(false, |
309 testing::Truly(CallClosure), | 305 testing::Truly(CallClosure), |
310 testing::_)); | 306 testing::_)); |
311 scoped_refptr<api::BluetoothStopDiscoveryFunction> stop_function; | 307 scoped_refptr<api::BluetoothStopDiscoveryFunction> stop_function; |
312 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); | 308 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); |
313 (void)utils::RunFunctionAndReturnResult(stop_function, "[]", browser()); | 309 (void)utils::RunFunctionAndReturnResult(stop_function, "[]", browser()); |
314 | 310 |
315 // TODO(bryeung): test that no events are sent now (crbug.com/132616) | |
316 | |
317 // Reset to try stopping with an error | 311 // Reset to try stopping with an error |
318 testing::Mock::VerifyAndClearExpectations(mock_adapter_); | 312 testing::Mock::VerifyAndClearExpectations(mock_adapter_); |
319 EXPECT_CALL(*mock_adapter_, | 313 EXPECT_CALL(*mock_adapter_, |
320 SetDiscovering(false, | 314 SetDiscovering(false, |
321 testing::_, | 315 testing::_, |
322 testing::Truly(CallClosure))); | 316 testing::Truly(CallClosure))); |
323 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); | 317 stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction); |
324 error = utils::RunFunctionAndReturnError(stop_function, "[]", browser()); | 318 error = utils::RunFunctionAndReturnError(stop_function, "[]", browser()); |
325 ASSERT_TRUE(!error.empty()); | 319 ASSERT_TRUE(!error.empty()); |
326 } | 320 } |
327 | 321 |
| 322 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DiscoveryCallback) { |
| 323 EXPECT_CALL(*mock_adapter_, |
| 324 SetDiscovering(true, testing::Truly(CallClosure), testing::_)); |
| 325 EXPECT_CALL(*mock_adapter_, |
| 326 SetDiscovering(false, testing::Truly(CallClosure), testing::_)); |
| 327 |
| 328 ResultCatcher catcher; |
| 329 catcher.RestrictToProfile(browser()->profile()); |
| 330 |
| 331 ExtensionTestMessageListener discovery_started("ready", true); |
| 332 const extensions::Extension* extension = |
| 333 LoadExtension(test_data_dir_.AppendASCII("bluetooth")); |
| 334 GURL page_url = extension->GetResourceURL("test_discovery.html"); |
| 335 ui_test_utils::NavigateToURL(browser(), page_url); |
| 336 EXPECT_TRUE(discovery_started.WaitUntilSatisfied()); |
| 337 |
| 338 event_router()->DeviceAdded(mock_adapter_, device1_.get()); |
| 339 |
| 340 discovery_started.Reply("go"); |
| 341 ExtensionTestMessageListener discovery_stopped("ready", true); |
| 342 EXPECT_TRUE(discovery_stopped.WaitUntilSatisfied()); |
| 343 |
| 344 event_router()->DeviceAdded(mock_adapter_, device2_.get()); |
| 345 discovery_stopped.Reply("go"); |
| 346 |
| 347 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
| 348 } |
| 349 |
328 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Events) { | 350 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Events) { |
329 ResultCatcher catcher; | 351 ResultCatcher catcher; |
330 catcher.RestrictToProfile(browser()->profile()); | 352 catcher.RestrictToProfile(browser()->profile()); |
331 | 353 |
332 // Load and wait for setup | 354 // Load and wait for setup |
333 ExtensionTestMessageListener listener("ready", true); | 355 ExtensionTestMessageListener listener("ready", true); |
334 const extensions::Extension* extension = | 356 const extensions::Extension* extension = |
335 LoadExtension(test_data_dir_.AppendASCII("bluetooth")); | 357 LoadExtension(test_data_dir_.AppendASCII("bluetooth")); |
336 GURL page_url = extension->GetResourceURL("test_events.html"); | 358 GURL page_url = extension->GetResourceURL("test_events.html"); |
337 ui_test_utils::NavigateToURL(browser(), page_url); | 359 ui_test_utils::NavigateToURL(browser(), page_url); |
338 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 360 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
339 | 361 |
340 event_router()->AdapterPoweredChanged(mock_adapter_, true); | 362 event_router()->AdapterPoweredChanged(mock_adapter_, true); |
341 event_router()->AdapterPoweredChanged(mock_adapter_, false); | 363 event_router()->AdapterPoweredChanged(mock_adapter_, false); |
342 event_router()->AdapterPresentChanged(mock_adapter_, true); | 364 event_router()->AdapterPresentChanged(mock_adapter_, true); |
343 event_router()->AdapterPresentChanged(mock_adapter_, false); | 365 event_router()->AdapterPresentChanged(mock_adapter_, false); |
344 | 366 |
345 listener.Reply("go"); | 367 listener.Reply("go"); |
346 | 368 |
347 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 369 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
348 } | 370 } |
OLD | NEW |