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

Side by Side Diff: content/shell/browser/layout_test/layout_test_bluetooth_adapter_provider.cc

Issue 1334763002: bluetooth: Subscribe to notifications (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-origin
Patch Set: Fix global interface test Created 5 years, 2 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provid er.h" 5 #include "content/shell/browser/layout_test/layout_test_bluetooth_adapter_provid er.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/strings/stringprintf.h" 11 #include "base/strings/stringprintf.h"
12 #include "base/thread_task_runner_handle.h" 12 #include "base/thread_task_runner_handle.h"
13 #include "device/bluetooth/bluetooth_adapter.h" 13 #include "device/bluetooth/bluetooth_adapter.h"
14 #include "device/bluetooth/bluetooth_device.h" 14 #include "device/bluetooth/bluetooth_device.h"
15 #include "device/bluetooth/bluetooth_discovery_session.h" 15 #include "device/bluetooth/bluetooth_discovery_session.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_discovery_session.h" 18 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h"
19 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" 19 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h"
20 #include "device/bluetooth/test/mock_bluetooth_gatt_notify_session.h"
20 #include "testing/gmock/include/gmock/gmock.h" 21 #include "testing/gmock/include/gmock/gmock.h"
21 22
22 using device::BluetoothAdapter; 23 using device::BluetoothAdapter;
23 using device::BluetoothDevice; 24 using device::BluetoothDevice;
25 using device::BluetoothGattCharacteristic;
24 using device::BluetoothGattService; 26 using device::BluetoothGattService;
25 using device::BluetoothUUID; 27 using device::BluetoothUUID;
26 using device::MockBluetoothAdapter; 28 using device::MockBluetoothAdapter;
27 using device::MockBluetoothDevice; 29 using device::MockBluetoothDevice;
28 using device::MockBluetoothDiscoverySession; 30 using device::MockBluetoothDiscoverySession;
29 using device::MockBluetoothGattCharacteristic; 31 using device::MockBluetoothGattCharacteristic;
30 using device::MockBluetoothGattConnection; 32 using device::MockBluetoothGattConnection;
33 using device::MockBluetoothGattNotifySession;
31 using device::MockBluetoothGattService; 34 using device::MockBluetoothGattService;
32 using testing::ElementsAre; 35 using testing::ElementsAre;
33 using testing::Invoke; 36 using testing::Invoke;
34 using testing::ResultOf; 37 using testing::ResultOf;
35 using testing::Return; 38 using testing::Return;
36 using testing::_; 39 using testing::_;
37 40
38 typedef testing::NiceMock<MockBluetoothAdapter> NiceMockBluetoothAdapter; 41 typedef testing::NiceMock<MockBluetoothAdapter> NiceMockBluetoothAdapter;
39 typedef testing::NiceMock<MockBluetoothDevice> NiceMockBluetoothDevice; 42 typedef testing::NiceMock<MockBluetoothDevice> NiceMockBluetoothDevice;
40 typedef testing::NiceMock<MockBluetoothDiscoverySession> 43 typedef testing::NiceMock<MockBluetoothDiscoverySession>
41 NiceMockBluetoothDiscoverySession; 44 NiceMockBluetoothDiscoverySession;
42 typedef testing::NiceMock<MockBluetoothGattCharacteristic> 45 typedef testing::NiceMock<MockBluetoothGattCharacteristic>
43 NiceMockBluetoothGattCharacteristic; 46 NiceMockBluetoothGattCharacteristic;
44 typedef testing::NiceMock<MockBluetoothGattConnection> 47 typedef testing::NiceMock<MockBluetoothGattConnection>
45 NiceMockBluetoothGattConnection; 48 NiceMockBluetoothGattConnection;
46 typedef testing::NiceMock<MockBluetoothGattService> 49 typedef testing::NiceMock<MockBluetoothGattService>
47 NiceMockBluetoothGattService; 50 NiceMockBluetoothGattService;
51 typedef testing::NiceMock<MockBluetoothGattNotifySession>
52 NiceMockBluetoothGattNotifySession;
48 53
49 namespace { 54 namespace {
50 // Bluetooth UUIDs suitable to pass to BluetoothUUID(). 55 // Bluetooth UUIDs suitable to pass to BluetoothUUID().
51 const char kBatteryServiceUUID[] = "180f"; 56 const char kBatteryServiceUUID[] = "180f";
52 const char kGenericAccessServiceUUID[] = "1800"; 57 const char kGenericAccessServiceUUID[] = "1800";
53 const char kGlucoseServiceUUID[] = "1808"; 58 const char kGlucoseServiceUUID[] = "1808";
54 const char kHeartRateServiceUUID[] = "180d"; 59 const char kHeartRateServiceUUID[] = "180d";
60 const char kHeartRateMeasurementUUID[] = "2a37";
55 const char kDeviceNameUUID[] = "2a00"; 61 const char kDeviceNameUUID[] = "2a00";
56 62
57 // Invokes Run() on the k-th argument of the function with no arguments. 63 // Invokes Run() on the k-th argument of the function with no arguments.
58 ACTION_TEMPLATE(RunCallback, 64 ACTION_TEMPLATE(RunCallback,
59 HAS_1_TEMPLATE_PARAMS(int, k), 65 HAS_1_TEMPLATE_PARAMS(int, k),
60 AND_0_VALUE_PARAMS()) { 66 AND_0_VALUE_PARAMS()) {
61 return ::testing::get<k>(args).Run(); 67 return ::testing::get<k>(args).Run();
62 } 68 }
63 69
64 // Invokes Run() on the k-th argument of the function with 1 argument. 70 // Invokes Run() on the k-th argument of the function with 1 argument.
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 else if (fake_adapter_name == "FailStartDiscoveryAdapter") 121 else if (fake_adapter_name == "FailStartDiscoveryAdapter")
116 return GetFailStartDiscoveryAdapter(); 122 return GetFailStartDiscoveryAdapter();
117 else if (fake_adapter_name == "GlucoseHeartRateAdapter") 123 else if (fake_adapter_name == "GlucoseHeartRateAdapter")
118 return GetGlucoseHeartRateAdapter(); 124 return GetGlucoseHeartRateAdapter();
119 else if (fake_adapter_name == "MissingServiceGenericAccessAdapter") 125 else if (fake_adapter_name == "MissingServiceGenericAccessAdapter")
120 return GetMissingServiceGenericAccessAdapter(); 126 return GetMissingServiceGenericAccessAdapter();
121 else if (fake_adapter_name == "MissingCharacteristicGenericAccessAdapter") 127 else if (fake_adapter_name == "MissingCharacteristicGenericAccessAdapter")
122 return GetMissingCharacteristicGenericAccessAdapter(); 128 return GetMissingCharacteristicGenericAccessAdapter();
123 else if (fake_adapter_name == "GenericAccessAdapter") 129 else if (fake_adapter_name == "GenericAccessAdapter")
124 return GetGenericAccessAdapter(); 130 return GetGenericAccessAdapter();
131 else if (fake_adapter_name == "HeartRateAdapter")
132 return GetHeartRateAdapter();
125 else if (fake_adapter_name == "FailingConnectionsAdapter") 133 else if (fake_adapter_name == "FailingConnectionsAdapter")
126 return GetFailingConnectionsAdapter(); 134 return GetFailingConnectionsAdapter();
127 else if (fake_adapter_name == "FailingGATTOperationsAdapter") 135 else if (fake_adapter_name == "FailingGATTOperationsAdapter")
128 return GetFailingGATTOperationsAdapter(); 136 return GetFailingGATTOperationsAdapter();
129 else if (fake_adapter_name == "SecondDiscoveryFindsHeartRateAdapter") 137 else if (fake_adapter_name == "SecondDiscoveryFindsHeartRateAdapter")
130 return GetSecondDiscoveryFindsHeartRateAdapter(); 138 return GetSecondDiscoveryFindsHeartRateAdapter();
131 else if (fake_adapter_name == "") 139 else if (fake_adapter_name == "")
132 return NULL; 140 return NULL;
133 141
134 NOTREACHED() << fake_adapter_name; 142 NOTREACHED() << fake_adapter_name;
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 354
347 generic_access->AddMockCharacteristic(device_name.Pass()); 355 generic_access->AddMockCharacteristic(device_name.Pass());
348 device->AddMockService(generic_access.Pass()); 356 device->AddMockService(generic_access.Pass());
349 adapter->AddMockDevice(device.Pass()); 357 adapter->AddMockDevice(device.Pass());
350 358
351 return adapter.Pass(); 359 return adapter.Pass();
352 } 360 }
353 361
354 // static 362 // static
355 scoped_refptr<NiceMockBluetoothAdapter> 363 scoped_refptr<NiceMockBluetoothAdapter>
364 LayoutTestBluetoothAdapterProvider::GetHeartRateAdapter() {
365 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter());
366 scoped_ptr<NiceMockBluetoothDevice> device(GetHeartRateDevice(adapter.get()));
367 scoped_ptr<NiceMockBluetoothGattService> heart_rate(
368 GetBaseGATTService(device.get(), kHeartRateServiceUUID));
369
370 // TODO(ortuno): Implement the rest of the service's characteristics
371 // See: http://crbug.com/529975
372 scoped_ptr<NiceMockBluetoothGattCharacteristic> heart_rate_measurement(
373 GetBaseGATTCharacteristic(heart_rate.get(), kHeartRateMeasurementUUID));
374 BluetoothGattCharacteristic* measurement_ptr = heart_rate_measurement.get();
375 ON_CALL(*heart_rate_measurement, StartNotifySession(_, _))
376 .WillByDefault(
377 RunCallbackWithResult<0 /* success_callback */>([measurement_ptr]() {
378 return GetBaseGATTNotifySession(measurement_ptr->GetIdentifier());
379 }));
380
381 heart_rate->AddMockCharacteristic(heart_rate_measurement.Pass());
382 device->AddMockService(heart_rate.Pass());
383 adapter->AddMockDevice(device.Pass());
384
385 return adapter.Pass();
386 }
387
388 // static
389 scoped_refptr<NiceMockBluetoothAdapter>
356 LayoutTestBluetoothAdapterProvider::GetFailingConnectionsAdapter() { 390 LayoutTestBluetoothAdapterProvider::GetFailingConnectionsAdapter() {
357 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); 391 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter());
358 392
359 for (int error = BluetoothDevice::ERROR_UNKNOWN; 393 for (int error = BluetoothDevice::ERROR_UNKNOWN;
360 error <= BluetoothDevice::ERROR_UNSUPPORTED_DEVICE; error++) { 394 error <= BluetoothDevice::ERROR_UNSUPPORTED_DEVICE; error++) {
361 adapter->AddMockDevice(GetUnconnectableDevice( 395 adapter->AddMockDevice(GetUnconnectableDevice(
362 adapter.get(), static_cast<BluetoothDevice::ConnectErrorCode>(error))); 396 adapter.get(), static_cast<BluetoothDevice::ConnectErrorCode>(error)));
363 } 397 }
398
364 return adapter.Pass(); 399 return adapter.Pass();
365 } 400 }
366 401
367 // static 402 // static
368 scoped_refptr<NiceMockBluetoothAdapter> 403 scoped_refptr<NiceMockBluetoothAdapter>
369 LayoutTestBluetoothAdapterProvider::GetFailingGATTOperationsAdapter() { 404 LayoutTestBluetoothAdapterProvider::GetFailingGATTOperationsAdapter() {
370 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter()); 405 scoped_refptr<NiceMockBluetoothAdapter> adapter(GetEmptyAdapter());
371 406
372 const std::string errorsServiceUUID = errorUUID(0xA0); 407 const std::string errorsServiceUUID = errorUUID(0xA0);
373 408
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 } 503 }
469 504
470 // static 505 // static
471 scoped_ptr<NiceMockBluetoothDevice> 506 scoped_ptr<NiceMockBluetoothDevice>
472 LayoutTestBluetoothAdapterProvider::GetHeartRateDevice( 507 LayoutTestBluetoothAdapterProvider::GetHeartRateDevice(
473 MockBluetoothAdapter* adapter) { 508 MockBluetoothAdapter* adapter) {
474 BluetoothDevice::UUIDList uuids; 509 BluetoothDevice::UUIDList uuids;
475 uuids.push_back(BluetoothUUID(kGenericAccessServiceUUID)); 510 uuids.push_back(BluetoothUUID(kGenericAccessServiceUUID));
476 uuids.push_back(BluetoothUUID(kHeartRateServiceUUID)); 511 uuids.push_back(BluetoothUUID(kHeartRateServiceUUID));
477 512
478 return GetBaseDevice(adapter, "Heart Rate Device", uuids, 513 return GetConnectableDevice(adapter, "Heart Rate Device", uuids,
479 makeMACAddress(0x3)); 514 makeMACAddress(0x3));
480 } 515 }
481 516
482 // static 517 // static
483 scoped_ptr<NiceMockBluetoothDevice> 518 scoped_ptr<NiceMockBluetoothDevice>
484 LayoutTestBluetoothAdapterProvider::GetConnectableDevice( 519 LayoutTestBluetoothAdapterProvider::GetConnectableDevice(
485 device::MockBluetoothAdapter* adapter, 520 device::MockBluetoothAdapter* adapter,
486 const std::string& device_name, 521 const std::string& device_name,
487 BluetoothDevice::UUIDList uuids) { 522 BluetoothDevice::UUIDList uuids,
523 const std::string& address) {
488 scoped_ptr<NiceMockBluetoothDevice> device( 524 scoped_ptr<NiceMockBluetoothDevice> device(
489 GetBaseDevice(adapter, device_name, uuids)); 525 GetBaseDevice(adapter, device_name, uuids, address));
490 526
491 BluetoothDevice* device_ptr = device.get(); 527 BluetoothDevice* device_ptr = device.get();
492 528
493 ON_CALL(*device, CreateGattConnection(_, _)) 529 ON_CALL(*device, CreateGattConnection(_, _))
494 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>( 530 .WillByDefault(RunCallbackWithResult<0 /* success_callback */>(
495 [adapter, device_ptr]() { 531 [adapter, device_ptr]() {
496 return make_scoped_ptr(new NiceMockBluetoothGattConnection( 532 return make_scoped_ptr(new NiceMockBluetoothGattConnection(
497 adapter, device_ptr->GetAddress())); 533 adapter, device_ptr->GetAddress()));
498 })); 534 }));
499 535
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
574 GetBaseGATTCharacteristic(service, errorUUID(error_alias))); 610 GetBaseGATTCharacteristic(service, errorUUID(error_alias)));
575 611
576 // Read response. 612 // Read response.
577 ON_CALL(*characteristic, ReadRemoteCharacteristic(_, _)) 613 ON_CALL(*characteristic, ReadRemoteCharacteristic(_, _))
578 .WillByDefault(RunCallback<1 /* error_callback */>(error_code)); 614 .WillByDefault(RunCallback<1 /* error_callback */>(error_code));
579 615
580 // Write response. 616 // Write response.
581 ON_CALL(*characteristic, WriteRemoteCharacteristic(_, _, _)) 617 ON_CALL(*characteristic, WriteRemoteCharacteristic(_, _, _))
582 .WillByDefault(RunCallback<2 /* error_callback */>(error_code)); 618 .WillByDefault(RunCallback<2 /* error_callback */>(error_code));
583 619
620 // StartNotifySession response
621 ON_CALL(*characteristic, StartNotifySession(_, _))
622 .WillByDefault(RunCallback<1 /* error_callback */>(error_code));
623
584 return characteristic.Pass(); 624 return characteristic.Pass();
585 } 625 }
586 626
627 // Notify sessions
628
629 // static
630 scoped_ptr<NiceMockBluetoothGattNotifySession>
631 LayoutTestBluetoothAdapterProvider::GetBaseGATTNotifySession(
632 const std::string& characteristic_identifier) {
633 scoped_ptr<NiceMockBluetoothGattNotifySession> session(
634 new NiceMockBluetoothGattNotifySession(characteristic_identifier));
635
636 ON_CALL(*session, Stop(_)).WillByDefault(RunCallback<0>());
637
638 return session.Pass();
639 }
640
587 // Helper functions 641 // Helper functions
588 642
589 // static 643 // static
590 std::string LayoutTestBluetoothAdapterProvider::errorUUID(uint32_t alias) { 644 std::string LayoutTestBluetoothAdapterProvider::errorUUID(uint32_t alias) {
591 return base::StringPrintf("%08x-97e5-4cd7-b9f1-f5a427670c59", alias); 645 return base::StringPrintf("%08x-97e5-4cd7-b9f1-f5a427670c59", alias);
592 } 646 }
593 647
594 // static 648 // static
595 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) { 649 std::string LayoutTestBluetoothAdapterProvider::makeMACAddress(uint64_t addr) {
596 return BluetoothDevice::CanonicalizeAddress( 650 return BluetoothDevice::CanonicalizeAddress(
597 base::StringPrintf("%012" PRIx64, addr)); 651 base::StringPrintf("%012" PRIx64, addr));
598 } 652 }
599 653
600 } // namespace content 654 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698