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 "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/extensions/extension_apitest.h" | 9 #include "chrome/browser/extensions/extension_apitest.h" |
10 #include "chrome/browser/extensions/extension_function_test_utils.h" | 10 #include "chrome/browser/extensions/extension_function_test_utils.h" |
11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
12 #include "chrome/browser/extensions/extension_test_message_listener.h" | 12 #include "chrome/browser/extensions/extension_test_message_listener.h" |
13 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
14 #include "chrome/test/base/ui_test_utils.h" | 14 #include "chrome/test/base/ui_test_utils.h" |
15 #include "device/bluetooth/bluetooth_adapter.h" | 15 #include "device/bluetooth/bluetooth_adapter.h" |
16 #include "device/bluetooth/bluetooth_uuid.h" | 16 #include "device/bluetooth/bluetooth_uuid.h" |
17 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 17 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
18 #include "device/bluetooth/test/mock_bluetooth_device.h" | 18 #include "device/bluetooth/test/mock_bluetooth_device.h" |
19 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" | 19 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h" |
20 #include "extensions/browser/api/bluetooth/bluetooth_api.h" | 20 #include "extensions/browser/api/bluetooth/bluetooth_api.h" |
21 #include "extensions/browser/api/bluetooth/bluetooth_event_router.h" | 21 #include "extensions/browser/api/bluetooth/bluetooth_event_router.h" |
| 22 #include "extensions/test/result_catcher.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
23 | 24 |
24 using device::BluetoothAdapter; | 25 using device::BluetoothAdapter; |
25 using device::BluetoothDevice; | 26 using device::BluetoothDevice; |
26 using device::BluetoothDiscoverySession; | 27 using device::BluetoothDiscoverySession; |
27 using device::BluetoothUUID; | 28 using device::BluetoothUUID; |
28 using device::MockBluetoothAdapter; | 29 using device::MockBluetoothAdapter; |
29 using device::MockBluetoothDevice; | 30 using device::MockBluetoothDevice; |
30 using device::MockBluetoothDiscoverySession; | 31 using device::MockBluetoothDiscoverySession; |
31 using extensions::Extension; | 32 using extensions::Extension; |
| 33 using extensions::ResultCatcher; |
32 | 34 |
33 namespace utils = extension_function_test_utils; | 35 namespace utils = extension_function_test_utils; |
34 namespace api = extensions::core_api; | 36 namespace api = extensions::core_api; |
35 | 37 |
36 namespace { | 38 namespace { |
37 | 39 |
38 static const char* kAdapterAddress = "A1:A2:A3:A4:A5:A6"; | 40 static const char* kAdapterAddress = "A1:A2:A3:A4:A5:A6"; |
39 static const char* kName = "whatsinaname"; | 41 static const char* kName = "whatsinaname"; |
40 | 42 |
41 class BluetoothApiTest : public ExtensionApiTest { | 43 class BluetoothApiTest : public ExtensionApiTest { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 | 137 |
136 EXPECT_FALSE(state.available); | 138 EXPECT_FALSE(state.available); |
137 EXPECT_TRUE(state.powered); | 139 EXPECT_TRUE(state.powered); |
138 EXPECT_FALSE(state.discovering); | 140 EXPECT_FALSE(state.discovering); |
139 EXPECT_EQ(kName, state.name); | 141 EXPECT_EQ(kName, state.name); |
140 EXPECT_EQ(kAdapterAddress, state.address); | 142 EXPECT_EQ(kAdapterAddress, state.address); |
141 } | 143 } |
142 | 144 |
143 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DeviceEvents) { | 145 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DeviceEvents) { |
144 ResultCatcher catcher; | 146 ResultCatcher catcher; |
145 catcher.RestrictToProfile(browser()->profile()); | 147 catcher.RestrictToBrowserContext(browser()->profile()); |
146 | 148 |
147 ASSERT_TRUE(LoadExtension( | 149 ASSERT_TRUE(LoadExtension( |
148 test_data_dir_.AppendASCII("bluetooth/device_events"))); | 150 test_data_dir_.AppendASCII("bluetooth/device_events"))); |
149 | 151 |
150 ExtensionTestMessageListener events_received("ready", true); | 152 ExtensionTestMessageListener events_received("ready", true); |
151 event_router()->DeviceAdded(mock_adapter_, device1_.get()); | 153 event_router()->DeviceAdded(mock_adapter_, device1_.get()); |
152 event_router()->DeviceAdded(mock_adapter_, device2_.get()); | 154 event_router()->DeviceAdded(mock_adapter_, device2_.get()); |
153 | 155 |
154 EXPECT_CALL(*device2_.get(), GetDeviceName()) | 156 EXPECT_CALL(*device2_.get(), GetDeviceName()) |
155 .WillRepeatedly(testing::Return("the real d2")); | 157 .WillRepeatedly(testing::Return("the real d2")); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>()); | 225 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>()); |
224 MockBluetoothDiscoverySession* session = mock_session_.get(); | 226 MockBluetoothDiscoverySession* session = mock_session_.get(); |
225 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_)) | 227 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_)) |
226 .WillOnce( | 228 .WillOnce( |
227 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback)); | 229 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback)); |
228 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true)); | 230 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true)); |
229 EXPECT_CALL(*session, Stop(testing::_, testing::_)) | 231 EXPECT_CALL(*session, Stop(testing::_, testing::_)) |
230 .WillOnce(testing::Invoke(StopDiscoverySessionCallback)); | 232 .WillOnce(testing::Invoke(StopDiscoverySessionCallback)); |
231 | 233 |
232 ResultCatcher catcher; | 234 ResultCatcher catcher; |
233 catcher.RestrictToProfile(browser()->profile()); | 235 catcher.RestrictToBrowserContext(browser()->profile()); |
234 | 236 |
235 ExtensionTestMessageListener discovery_started("ready", true); | 237 ExtensionTestMessageListener discovery_started("ready", true); |
236 ASSERT_TRUE(LoadExtension( | 238 ASSERT_TRUE(LoadExtension( |
237 test_data_dir_.AppendASCII("bluetooth/discovery_callback"))); | 239 test_data_dir_.AppendASCII("bluetooth/discovery_callback"))); |
238 EXPECT_TRUE(discovery_started.WaitUntilSatisfied()); | 240 EXPECT_TRUE(discovery_started.WaitUntilSatisfied()); |
239 | 241 |
240 event_router()->DeviceAdded(mock_adapter_, device1_.get()); | 242 event_router()->DeviceAdded(mock_adapter_, device1_.get()); |
241 | 243 |
242 discovery_started.Reply("go"); | 244 discovery_started.Reply("go"); |
243 ExtensionTestMessageListener discovery_stopped("ready", true); | 245 ExtensionTestMessageListener discovery_stopped("ready", true); |
(...skipping 19 matching lines...) Expand all Loading... |
263 | 265 |
264 // Fake that the adapter is discovering | 266 // Fake that the adapter is discovering |
265 EXPECT_CALL(*mock_adapter_, IsDiscovering()) | 267 EXPECT_CALL(*mock_adapter_, IsDiscovering()) |
266 .WillOnce(testing::Return(true)); | 268 .WillOnce(testing::Return(true)); |
267 event_router()->AdapterDiscoveringChanged(mock_adapter_, true); | 269 event_router()->AdapterDiscoveringChanged(mock_adapter_, true); |
268 | 270 |
269 // Cache a device before the extension starts discovering | 271 // Cache a device before the extension starts discovering |
270 event_router()->DeviceAdded(mock_adapter_, device1_.get()); | 272 event_router()->DeviceAdded(mock_adapter_, device1_.get()); |
271 | 273 |
272 ResultCatcher catcher; | 274 ResultCatcher catcher; |
273 catcher.RestrictToProfile(browser()->profile()); | 275 catcher.RestrictToBrowserContext(browser()->profile()); |
274 | 276 |
275 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>()); | 277 mock_session_.reset(new testing::NiceMock<MockBluetoothDiscoverySession>()); |
276 MockBluetoothDiscoverySession* session = mock_session_.get(); | 278 MockBluetoothDiscoverySession* session = mock_session_.get(); |
277 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_)) | 279 EXPECT_CALL(*mock_adapter_, StartDiscoverySession(testing::_, testing::_)) |
278 .WillOnce( | 280 .WillOnce( |
279 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback)); | 281 testing::Invoke(this, &BluetoothApiTest::DiscoverySessionCallback)); |
280 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true)); | 282 EXPECT_CALL(*session, IsActive()).WillOnce(testing::Return(true)); |
281 EXPECT_CALL(*session, Stop(testing::_, testing::_)) | 283 EXPECT_CALL(*session, Stop(testing::_, testing::_)) |
282 .WillOnce(testing::Invoke(StopDiscoverySessionCallback)); | 284 .WillOnce(testing::Invoke(StopDiscoverySessionCallback)); |
283 | 285 |
(...skipping 14 matching lines...) Expand all Loading... |
298 SetUpMockAdapter(); | 300 SetUpMockAdapter(); |
299 // This should never be received. | 301 // This should never be received. |
300 event_router()->DeviceAdded(mock_adapter_, device2_.get()); | 302 event_router()->DeviceAdded(mock_adapter_, device2_.get()); |
301 discovery_stopped.Reply("go"); | 303 discovery_stopped.Reply("go"); |
302 | 304 |
303 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 305 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
304 } | 306 } |
305 | 307 |
306 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, OnAdapterStateChanged) { | 308 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, OnAdapterStateChanged) { |
307 ResultCatcher catcher; | 309 ResultCatcher catcher; |
308 catcher.RestrictToProfile(browser()->profile()); | 310 catcher.RestrictToBrowserContext(browser()->profile()); |
309 | 311 |
310 // Load and wait for setup | 312 // Load and wait for setup |
311 ExtensionTestMessageListener listener("ready", true); | 313 ExtensionTestMessageListener listener("ready", true); |
312 ASSERT_TRUE( | 314 ASSERT_TRUE( |
313 LoadExtension( | 315 LoadExtension( |
314 test_data_dir_.AppendASCII("bluetooth/on_adapter_state_changed"))); | 316 test_data_dir_.AppendASCII("bluetooth/on_adapter_state_changed"))); |
315 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 317 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
316 | 318 |
317 EXPECT_CALL(*mock_adapter_, GetAddress()) | 319 EXPECT_CALL(*mock_adapter_, GetAddress()) |
318 .WillOnce(testing::Return(kAdapterAddress)); | 320 .WillOnce(testing::Return(kAdapterAddress)); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 .WillOnce(testing::Return(true)); | 352 .WillOnce(testing::Return(true)); |
351 event_router()->AdapterDiscoveringChanged(mock_adapter_, true); | 353 event_router()->AdapterDiscoveringChanged(mock_adapter_, true); |
352 | 354 |
353 listener.Reply("go"); | 355 listener.Reply("go"); |
354 | 356 |
355 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 357 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
356 } | 358 } |
357 | 359 |
358 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { | 360 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { |
359 ResultCatcher catcher; | 361 ResultCatcher catcher; |
360 catcher.RestrictToProfile(browser()->profile()); | 362 catcher.RestrictToBrowserContext(browser()->profile()); |
361 | 363 |
362 BluetoothAdapter::ConstDeviceList devices; | 364 BluetoothAdapter::ConstDeviceList devices; |
363 devices.push_back(device1_.get()); | 365 devices.push_back(device1_.get()); |
364 devices.push_back(device2_.get()); | 366 devices.push_back(device2_.get()); |
365 | 367 |
366 EXPECT_CALL(*mock_adapter_, GetDevices()) | 368 EXPECT_CALL(*mock_adapter_, GetDevices()) |
367 .Times(1) | 369 .Times(1) |
368 .WillRepeatedly(testing::Return(devices)); | 370 .WillRepeatedly(testing::Return(devices)); |
369 | 371 |
370 // Load and wait for setup | 372 // Load and wait for setup |
371 ExtensionTestMessageListener listener("ready", true); | 373 ExtensionTestMessageListener listener("ready", true); |
372 ASSERT_TRUE( | 374 ASSERT_TRUE( |
373 LoadExtension(test_data_dir_.AppendASCII("bluetooth/get_devices"))); | 375 LoadExtension(test_data_dir_.AppendASCII("bluetooth/get_devices"))); |
374 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 376 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
375 | 377 |
376 listener.Reply("go"); | 378 listener.Reply("go"); |
377 | 379 |
378 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 380 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
379 } | 381 } |
380 | 382 |
381 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevice) { | 383 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevice) { |
382 ResultCatcher catcher; | 384 ResultCatcher catcher; |
383 catcher.RestrictToProfile(browser()->profile()); | 385 catcher.RestrictToBrowserContext(browser()->profile()); |
384 | 386 |
385 EXPECT_CALL(*mock_adapter_, GetDevice(device1_->GetAddress())) | 387 EXPECT_CALL(*mock_adapter_, GetDevice(device1_->GetAddress())) |
386 .WillOnce(testing::Return(device1_.get())); | 388 .WillOnce(testing::Return(device1_.get())); |
387 EXPECT_CALL(*mock_adapter_, GetDevice(device2_->GetAddress())) | 389 EXPECT_CALL(*mock_adapter_, GetDevice(device2_->GetAddress())) |
388 .Times(1) | 390 .Times(1) |
389 .WillRepeatedly(testing::Return(static_cast<BluetoothDevice*>(NULL))); | 391 .WillRepeatedly(testing::Return(static_cast<BluetoothDevice*>(NULL))); |
390 | 392 |
391 // Load and wait for setup | 393 // Load and wait for setup |
392 ExtensionTestMessageListener listener("ready", true); | 394 ExtensionTestMessageListener listener("ready", true); |
393 ASSERT_TRUE( | 395 ASSERT_TRUE( |
394 LoadExtension(test_data_dir_.AppendASCII("bluetooth/get_device"))); | 396 LoadExtension(test_data_dir_.AppendASCII("bluetooth/get_device"))); |
395 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 397 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
396 | 398 |
397 listener.Reply("go"); | 399 listener.Reply("go"); |
398 | 400 |
399 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 401 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
400 } | 402 } |
401 | 403 |
402 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DeviceInfo) { | 404 IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DeviceInfo) { |
403 ResultCatcher catcher; | 405 ResultCatcher catcher; |
404 catcher.RestrictToProfile(browser()->profile()); | 406 catcher.RestrictToBrowserContext(browser()->profile()); |
405 | 407 |
406 // Set up the first device object to reflect a real-world device. | 408 // Set up the first device object to reflect a real-world device. |
407 BluetoothAdapter::ConstDeviceList devices; | 409 BluetoothAdapter::ConstDeviceList devices; |
408 | 410 |
409 EXPECT_CALL(*device1_.get(), GetAddress()) | 411 EXPECT_CALL(*device1_.get(), GetAddress()) |
410 .WillRepeatedly(testing::Return("A4:17:31:00:00:00")); | 412 .WillRepeatedly(testing::Return("A4:17:31:00:00:00")); |
411 EXPECT_CALL(*device1_.get(), GetDeviceName()) | 413 EXPECT_CALL(*device1_.get(), GetDeviceName()) |
412 .WillRepeatedly(testing::Return("Chromebook Pixel")); | 414 .WillRepeatedly(testing::Return("Chromebook Pixel")); |
413 EXPECT_CALL(*device1_.get(), GetName()) | 415 EXPECT_CALL(*device1_.get(), GetName()) |
414 .WillRepeatedly(testing::Return(base::UTF8ToUTF16("Chromebook Pixel"))); | 416 .WillRepeatedly(testing::Return(base::UTF8ToUTF16("Chromebook Pixel"))); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
450 // Load and wait for setup | 452 // Load and wait for setup |
451 ExtensionTestMessageListener listener("ready", true); | 453 ExtensionTestMessageListener listener("ready", true); |
452 ASSERT_TRUE( | 454 ASSERT_TRUE( |
453 LoadExtension(test_data_dir_.AppendASCII("bluetooth/device_info"))); | 455 LoadExtension(test_data_dir_.AppendASCII("bluetooth/device_info"))); |
454 EXPECT_TRUE(listener.WaitUntilSatisfied()); | 456 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
455 | 457 |
456 listener.Reply("go"); | 458 listener.Reply("go"); |
457 | 459 |
458 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); | 460 EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); |
459 } | 461 } |
OLD | NEW |