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 using NiceMockBluetoothAdapter = | |
| 25 testing::NiceMock<device::MockBluetoothAdapter>; | |
| 26 using NiceMockBluetoothDevice = testing::NiceMock<device::MockBluetoothDevice>; | |
| 27 using NiceMockBluetoothGattService = | |
| 28 testing::NiceMock<device::MockBluetoothGattService>; | |
| 29 using NiceMockBluetoothGattConnection = | |
| 30 testing::NiceMock<device::MockBluetoothGattConnection>; | |
| 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 class BluetoothInterfaceDeviceTest : public testing::Test { | |
| 43 public: | |
| 44 enum class Call { EXPECTED, NOT_EXPECTED }; | |
| 45 | |
| 46 BluetoothInterfaceDeviceTest() | |
| 47 : adapter_(new NiceMockBluetoothAdapter), | |
| 48 device_(adapter_.get(), | |
| 49 0, | |
| 50 kTestLeDeviceName0, | |
| 51 kTestLeDeviceAddress0, | |
| 52 false, | |
| 53 true), | |
| 54 weak_factory_(this) { | |
| 55 ON_CALL(*adapter_, GetDevice(kTestLeDeviceAddress0)) | |
| 56 .WillByDefault(Return(&device_)); | |
| 57 | |
| 58 auto service1 = base::MakeUnique<NiceMockBluetoothGattService>( | |
| 59 &device_, kTestServiceId0, device::BluetoothUUID(kTestServiceUuid0), | |
| 60 true /* is_primary */, false /* is_local */); | |
| 61 auto service2 = base::MakeUnique<NiceMockBluetoothGattService>( | |
| 62 &device_, kTestServiceId1, device::BluetoothUUID(kTestServiceUuid1), | |
| 63 true /* is_primary */, false /* is_local */); | |
| 64 | |
| 65 device_.AddMockService(std::move(service1)); | |
| 66 device_.AddMockService(std::move(service2)); | |
| 67 | |
| 68 EXPECT_CALL(device_, GetGattServices()) | |
| 69 .WillRepeatedly( | |
| 70 Invoke(&device_, &device::MockBluetoothDevice::GetMockServices)); | |
| 71 | |
| 72 auto connection = base::MakeUnique<NiceMockBluetoothGattConnection>( | |
| 73 adapter_, device_.GetAddress()); | |
| 74 | |
| 75 device_service_ = new Device(adapter_, std::move(connection)); | |
| 76 binding_ptr_ = mojo::MakeStrongBinding(base::WrapUnique(device_service_), | |
| 77 mojo::GetProxy(&proxy_)); | |
| 78 device_service_->SetStrongBindingPtr(binding_ptr_); | |
| 79 } | |
| 80 | |
| 81 void TearDown() override { | |
| 82 EXPECT_EQ(expected_success_callback_calls_, actual_success_callback_calls_); | |
| 83 EXPECT_EQ(message_pipe_closed_, expect_device_service_deleted_); | |
| 84 proxy_.reset(); | |
| 85 } | |
| 86 | |
| 87 void OnConnectionError(const base::Closure& continuation) { | |
| 88 message_pipe_closed_ = true; | |
| 89 continuation.Run(); | |
|
ortuno
2016/11/16 04:53:17
Can you use base::RunLoop to simplify some of this
mbrunson
2016/11/16 22:17:04
Done.
| |
| 90 } | |
| 91 | |
| 92 protected: | |
| 93 void GetServicesCheckForPrecedingCalls( | |
| 94 Call expected, | |
| 95 size_t expected_service_count, | |
| 96 int num_of_preceding_calls, | |
| 97 const base::Closure& continuation, | |
| 98 std::vector<mojom::ServiceInfoPtr> services) { | |
| 99 EXPECT_EQ(num_of_preceding_calls, callback_count_); | |
| 100 ++callback_count_; | |
| 101 | |
| 102 if (expected == Call::EXPECTED) | |
| 103 ++actual_success_callback_calls_; | |
| 104 | |
| 105 EXPECT_EQ(expected_service_count, services.size()); | |
| 106 | |
| 107 continuation.Run(); | |
| 108 } | |
| 109 | |
| 110 Device::GetServicesCallback GetGetServicesCheckForPrecedingCalls( | |
| 111 Call expected, | |
| 112 int num_of_preceding_calls, | |
| 113 const base::Closure& continuation) { | |
| 114 if (expected == Call::EXPECTED) | |
| 115 ++expected_success_callback_calls_; | |
| 116 | |
| 117 return base::Bind( | |
| 118 &BluetoothInterfaceDeviceTest::GetServicesCheckForPrecedingCalls, | |
| 119 weak_factory_.GetWeakPtr(), expected, 2 /* expected_service_count */, | |
| 120 num_of_preceding_calls, continuation); | |
| 121 } | |
| 122 | |
| 123 scoped_refptr<NiceMockBluetoothAdapter> adapter_; | |
| 124 NiceMockBluetoothDevice device_; | |
| 125 Device* device_service_; | |
| 126 base::MessageLoop message_loop_; | |
| 127 mojom::DevicePtr proxy_; | |
| 128 mojo::StrongBindingPtr<mojom::Device> binding_ptr_; | |
| 129 | |
| 130 bool message_pipe_closed_ = false; | |
| 131 bool expect_device_service_deleted_ = false; | |
| 132 int expected_success_callback_calls_ = 0; | |
| 133 int actual_success_callback_calls_ = 0; | |
| 134 int callback_count_ = 0; | |
| 135 | |
| 136 base::WeakPtrFactory<BluetoothInterfaceDeviceTest> weak_factory_; | |
| 137 }; | |
| 138 } // namespace | |
| 139 | |
| 140 TEST_F(BluetoothInterfaceDeviceTest, GetServices) { | |
| 141 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
| 142 .WillRepeatedly(Return(true)); | |
| 143 | |
| 144 base::RunLoop loop; | |
| 145 | |
| 146 proxy_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
| 147 Call::EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
| 148 | |
| 149 loop.Run(); | |
| 150 } | |
| 151 | |
| 152 TEST_F(BluetoothInterfaceDeviceTest, GetServicesNotDiscovered) { | |
| 153 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
| 154 .WillOnce(Return(false)) | |
| 155 .WillOnce(Return(false)) | |
| 156 .WillRepeatedly(Return(true)); | |
| 157 | |
| 158 base::RunLoop loop; | |
| 159 | |
| 160 // Client: Sends multiple requests for services. | |
| 161 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
| 162 Call::EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
| 163 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
| 164 Call::EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure())); | |
| 165 | |
| 166 // Simulate: GattServicesDiscovered. | |
| 167 device_service_->GattServicesDiscovered(nullptr /* adapter */, &device_); | |
| 168 | |
| 169 // No more GetServices calls will complete. | |
| 170 device_service_->GattServicesDiscovered(nullptr /* adapter */, &device_); | |
| 171 | |
| 172 // Client: Sends more requests which run immediately. | |
| 173 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
| 174 Call::EXPECTED, 2 /* num_of_preceding_calls */, loop.QuitClosure())); | |
| 175 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
| 176 Call::EXPECTED, 3 /* num_of_preceding_calls */, loop.QuitClosure())); | |
| 177 | |
| 178 // No more GetServices calls will complete. | |
| 179 device_service_->GattServicesDiscovered(nullptr /* adapter */, &device_); | |
| 180 | |
| 181 loop.Run(); | |
| 182 } | |
| 183 | |
| 184 TEST_F(BluetoothInterfaceDeviceTest, | |
| 185 GetServicesLostConnectionWithPendingRequests) { | |
| 186 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
| 187 .WillRepeatedly(Return(false)); | |
| 188 // Client: Sends multiple requests for services. | |
| 189 base::RunLoop loop; | |
| 190 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
| 191 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
| 192 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
| 193 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure())); | |
| 194 EXPECT_EQ(0, callback_count_); | |
| 195 | |
| 196 base::RunLoop loop2; | |
| 197 proxy_.set_connection_error_handler( | |
|
ortuno
2016/11/16 04:53:17
I think you can do this when initializing proxy_ i
mbrunson
2016/11/16 22:17:04
Done.
| |
| 198 base::Bind(&BluetoothInterfaceDeviceTest::OnConnectionError, | |
| 199 weak_factory_.GetWeakPtr(), loop2.QuitClosure())); | |
| 200 | |
| 201 // Simulate connection loss. | |
| 202 device_.SetConnected(false); | |
| 203 device_service_->DeviceChanged(nullptr /* adapter */, &device_); | |
| 204 expect_device_service_deleted_ = true; | |
|
ortuno
2016/11/16 04:53:17
Why do you test this in TearDown rather than in th
mbrunson
2016/11/16 22:17:04
I wanted to verify the values after each test was
ortuno
2016/11/17 03:54:44
We are loosing some clarity by doing it in TearDow
| |
| 205 loop2.Run(); | |
| 206 } | |
| 207 | |
| 208 TEST_F(BluetoothInterfaceDeviceTest, | |
| 209 GetServicesForcedDisconnectionWithPendingRequests) { | |
| 210 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
| 211 .WillRepeatedly(Return(false)); | |
| 212 | |
| 213 // Client: Sends multiple requests for services. | |
| 214 base::RunLoop loop; | |
| 215 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
| 216 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
| 217 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
| 218 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure())); | |
| 219 EXPECT_EQ(0, callback_count_); | |
| 220 | |
| 221 base::RunLoop loop2; | |
| 222 proxy_.set_connection_error_handler( | |
| 223 base::Bind(&BluetoothInterfaceDeviceTest::OnConnectionError, | |
| 224 weak_factory_.GetWeakPtr(), loop2.QuitClosure())); | |
| 225 | |
| 226 // Simulate connection loss. | |
| 227 device_service_->Disconnect(); | |
| 228 expect_device_service_deleted_ = true; | |
| 229 loop2.Run(); | |
| 230 } | |
| 231 | |
| 232 TEST_F(BluetoothInterfaceDeviceTest, GetServicesPipeClosedWithPendingRequests) { | |
| 233 EXPECT_CALL(device_, IsGattServicesDiscoveryComplete()) | |
| 234 .WillRepeatedly(Return(false)); | |
| 235 // Client: Sends multiple requests for services. | |
| 236 base::RunLoop loop; | |
| 237 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
| 238 Call::NOT_EXPECTED, 0 /* num_of_preceding_calls */, loop.QuitClosure())); | |
| 239 device_service_->GetServices(GetGetServicesCheckForPrecedingCalls( | |
| 240 Call::NOT_EXPECTED, 1 /* num_of_preceding_calls */, loop.QuitClosure())); | |
| 241 EXPECT_EQ(0, callback_count_); | |
| 242 | |
| 243 base::RunLoop loop2; | |
| 244 | |
| 245 // Bind | |
| 246 binding_ptr_->set_connection_error_handler( | |
| 247 base::Bind(&BluetoothInterfaceDeviceTest::OnConnectionError, | |
| 248 weak_factory_.GetWeakPtr(), loop2.QuitClosure())); | |
| 249 | |
| 250 // Simulate message pipe error. | |
| 251 proxy_.reset(); | |
| 252 expect_device_service_deleted_ = true; | |
| 253 | |
| 254 loop2.Run(); | |
| 255 } | |
| 256 | |
| 257 } // namespace bluetooth | |
| OLD | NEW |