Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "device/bluetooth/device.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "base/memory/ptr_util.h" | |
| 13 #include "base/memory/weak_ptr.h" | |
| 14 #include "base/run_loop.h" | |
| 15 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | |
| 16 #include "device/bluetooth/test/mock_bluetooth_device.h" | |
| 17 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 using ::testing::Return; | |
| 21 | |
| 22 namespace bluetooth { | |
| 23 | |
| 24 typedef testing::NiceMock<device::MockBluetoothAdapter> | |
| 25 NiceMockBluetoothAdapter; | |
| 26 typedef testing::NiceMock<device::MockBluetoothDevice> NiceMockBluetoothDevice; | |
| 27 typedef testing::NiceMock<device::MockBluetoothGattService> | |
| 28 NiceMockBluetoothGattService; | |
| 29 typedef testing::NiceMock<device::MockBluetoothGattConnection> | |
| 30 NiceMockBluetoothGattConnection; | |
| 31 | |
| 32 namespace { | |
| 33 const char kTestLeDeviceAddress0[] = "11:22:33:44:55:66"; | |
| 34 const char kTestLeDeviceName0[] = "Test LE Device 0"; | |
| 35 | |
| 36 const char kTestServiceId0[] = "service_id0"; | |
| 37 const char kTestServiceUuid0[] = "1234"; | |
| 38 | |
| 39 const char kTestServiceId1[] = "service_id1"; | |
| 40 const char kTestServiceUuid1[] = "5678"; | |
| 41 | |
| 42 void ExpectServices(size_t expected_length, | |
| 43 const base::Closure continuation, | |
| 44 std::vector<mojom::ServiceInfoPtr> services) { | |
| 45 EXPECT_EQ(expected_length, services.size()); | |
| 46 continuation.Run(); | |
| 47 } | |
| 48 | |
| 49 class DeviceTest : public testing::Test { | |
| 50 public: | |
| 51 DeviceTest() | |
| 52 : adapter_(new NiceMockBluetoothAdapter), | |
| 53 message_loop_(new base::MessageLoop) { | |
| 54 device_.reset( | |
| 55 new NiceMockBluetoothDevice(adapter_.get(), 0, kTestLeDeviceName0, | |
| 56 kTestLeDeviceAddress0, false, true)); | |
| 57 | |
| 58 ON_CALL(*adapter_, GetDevice(kTestLeDeviceAddress0)) | |
| 59 .WillByDefault(Return(device_.get())); | |
| 60 | |
| 61 std::unique_ptr<NiceMockBluetoothGattService> service1( | |
| 62 new NiceMockBluetoothGattService( | |
| 63 device_.get(), kTestServiceId0, | |
| 64 device::BluetoothUUID(kTestServiceUuid0), true /* is_primary */, | |
| 65 false /* is_local */)); | |
| 66 std::unique_ptr<NiceMockBluetoothGattService> service2( | |
| 67 new NiceMockBluetoothGattService( | |
| 68 device_.get(), kTestServiceId1, | |
| 69 device::BluetoothUUID(kTestServiceUuid1), true /* is_primary */, | |
| 70 false /* is_local */)); | |
| 71 | |
| 72 device_->AddMockService(std::move(service1)); | |
| 73 device_->AddMockService(std::move(service2)); | |
| 74 | |
| 75 std::unique_ptr<NiceMockBluetoothGattConnection> connection( | |
| 76 new NiceMockBluetoothGattConnection(adapter_, device_->GetAddress())); | |
| 77 | |
| 78 mojom::DevicePtr proxy; | |
| 79 | |
| 80 // Owns itself. | |
| 81 device_service_.reset( | |
| 82 new Device(adapter_, std::move(connection), mojo::GetProxy(&proxy))); | |
| 83 } | |
| 84 | |
| 85 protected: | |
| 86 void ExpectGetGattServicesCalls(int expected_calls) { | |
| 87 EXPECT_CALL(*device_, GetGattServices()) | |
| 88 .Times(expected_calls) | |
| 89 .WillRepeatedly(Invoke(device_.get(), | |
| 90 &device::MockBluetoothDevice::GetMockServices)); | |
| 91 } | |
| 92 | |
| 93 scoped_refptr<NiceMockBluetoothAdapter> adapter_; | |
| 94 std::unique_ptr<NiceMockBluetoothDevice> device_; | |
| 95 std::unique_ptr<Device> device_service_; | |
| 96 std::unique_ptr<base::MessageLoop> message_loop_; | |
| 97 }; | |
| 98 } // namespace | |
| 99 | |
| 100 TEST_F(DeviceTest, GetServices) { | |
| 101 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) | |
| 102 .WillRepeatedly(Return(true)); | |
| 103 | |
| 104 ExpectGetGattServicesCalls(1); | |
| 105 | |
| 106 base::RunLoop loop; | |
| 107 | |
| 108 device_service_->set_connection_error_handler(loop.QuitClosure()); | |
| 109 | |
| 110 device_service_->GetServices( | |
| 111 base::Bind(&ExpectServices, 2, loop.QuitClosure())); | |
| 112 loop.Run(); | |
| 113 } | |
| 114 | |
| 115 TEST_F(DeviceTest, GetServicesNotDiscovered) { | |
| 116 EXPECT_CALL(*device_, IsGattServicesDiscoveryComplete()) | |
| 117 .WillOnce(Return(false)) | |
| 118 .WillOnce(Return(false)) | |
| 119 .WillRepeatedly(Return(true)); | |
| 120 | |
| 121 ExpectGetGattServicesCalls(4); | |
| 122 | |
| 123 base::RunLoop loop; | |
| 124 | |
| 125 device_service_->set_connection_error_handler(loop.QuitClosure()); | |
| 126 | |
| 127 // Add more than one request to queue. | |
| 128 device_service_->GetServices( | |
| 129 base::Bind(&ExpectServices, 2, loop.QuitClosure())); | |
| 130 device_service_->GetServices( | |
| 131 base::Bind(&ExpectServices, 2, loop.QuitClosure())); | |
| 132 | |
| 133 // Run then flush the queue. Calls GetGattServices twice. | |
|
scheib
2016/10/31 20:46:17
Completes 2 queued GetServices tasks.
mbrunson
2016/10/31 21:39:25
Done.
| |
| 134 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get()); | |
| 135 | |
| 136 // Queue should be empty so no more calls to GetGattServices. | |
|
scheib
2016/10/31 20:46:17
"no more GetServices calls will complete".
Let's f
mbrunson
2016/10/31 21:39:25
Done.
| |
| 137 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get()); | |
| 138 | |
| 139 // These should run immediately. | |
| 140 device_service_->GetServices( | |
| 141 base::Bind(&ExpectServices, 2, loop.QuitClosure())); | |
| 142 device_service_->GetServices( | |
| 143 base::Bind(&ExpectServices, 2, loop.QuitClosure())); | |
| 144 | |
| 145 // Queue should be empty so no more calls to GetGattServices. | |
| 146 device_service_->GattServicesDiscovered(nullptr /* adapter */, device_.get()); | |
| 147 | |
| 148 loop.Run(); | |
| 149 } | |
| 150 | |
| 151 } // namespace bluetooth | |
| OLD | NEW |