| OLD | NEW |
| 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 "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h
" | 5 #include "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h
" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" | 18 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); | 126 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); |
| 126 | 127 |
| 127 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | 128 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); |
| 128 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | 129 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); |
| 129 | 130 |
| 130 ON_CALL(*device_whitelist_, HasDeviceWithAddress(_)) | 131 ON_CALL(*device_whitelist_, HasDeviceWithAddress(_)) |
| 131 .WillByDefault(Return(false)); | 132 .WillByDefault(Return(false)); |
| 132 } | 133 } |
| 133 | 134 |
| 134 void OnConnectionFound(scoped_ptr<Connection> connection) { | 135 void OnConnectionFound(scoped_ptr<Connection> connection) { |
| 135 last_found_connection_ = connection.Pass(); | 136 last_found_connection_ = std::move(connection); |
| 136 } | 137 } |
| 137 | 138 |
| 138 void FindAndExpectStartDiscovery( | 139 void FindAndExpectStartDiscovery( |
| 139 BluetoothLowEnergyConnectionFinder& connection_finder) { | 140 BluetoothLowEnergyConnectionFinder& connection_finder) { |
| 140 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; | 141 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; |
| 141 scoped_ptr<device::MockBluetoothDiscoverySession> discovery_session( | 142 scoped_ptr<device::MockBluetoothDiscoverySession> discovery_session( |
| 142 new NiceMock<device::MockBluetoothDiscoverySession>()); | 143 new NiceMock<device::MockBluetoothDiscoverySession>()); |
| 143 last_discovery_session_alias_ = discovery_session.get(); | 144 last_discovery_session_alias_ = discovery_session.get(); |
| 144 | 145 |
| 145 // Starting a discovery session. StartDiscoveryWithFilterRaw is a proxy for | 146 // Starting a discovery session. StartDiscoveryWithFilterRaw is a proxy for |
| 146 // StartDiscoveryWithFilter. | 147 // StartDiscoveryWithFilter. |
| 147 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | 148 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 148 .WillOnce(SaveArg<1>(&discovery_callback)); | 149 .WillOnce(SaveArg<1>(&discovery_callback)); |
| 149 EXPECT_CALL(*adapter_, AddObserver(_)); | 150 EXPECT_CALL(*adapter_, AddObserver(_)); |
| 150 ON_CALL(*last_discovery_session_alias_, IsActive()) | 151 ON_CALL(*last_discovery_session_alias_, IsActive()) |
| 151 .WillByDefault(Return(true)); | 152 .WillByDefault(Return(true)); |
| 152 connection_finder.Find(connection_callback_); | 153 connection_finder.Find(connection_callback_); |
| 153 ASSERT_FALSE(discovery_callback.is_null()); | 154 ASSERT_FALSE(discovery_callback.is_null()); |
| 154 discovery_callback.Run(discovery_session.Pass()); | 155 discovery_callback.Run(std::move(discovery_session)); |
| 155 } | 156 } |
| 156 | 157 |
| 157 void ExpectRemoveObserver() { | 158 void ExpectRemoveObserver() { |
| 158 EXPECT_CALL(*adapter_, RemoveObserver(_)).Times(AtLeast(1)); | 159 EXPECT_CALL(*adapter_, RemoveObserver(_)).Times(AtLeast(1)); |
| 159 } | 160 } |
| 160 | 161 |
| 161 // Prepare |device_| with |uuid|. | 162 // Prepare |device_| with |uuid|. |
| 162 void PrepareDevice(const std::string& uuid, | 163 void PrepareDevice(const std::string& uuid, |
| 163 const std::string& address, | 164 const std::string& address, |
| 164 bool paired) { | 165 bool paired) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 device::MockBluetoothDiscoverySession* discovery_session_alias = | 216 device::MockBluetoothDiscoverySession* discovery_session_alias = |
| 216 discovery_session.get(); | 217 discovery_session.get(); |
| 217 | 218 |
| 218 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | 219 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 219 .WillOnce(SaveArg<1>(&discovery_callback)); | 220 .WillOnce(SaveArg<1>(&discovery_callback)); |
| 220 ON_CALL(*discovery_session_alias, IsActive()).WillByDefault(Return(true)); | 221 ON_CALL(*discovery_session_alias, IsActive()).WillByDefault(Return(true)); |
| 221 EXPECT_CALL(*adapter_, AddObserver(_)); | 222 EXPECT_CALL(*adapter_, AddObserver(_)); |
| 222 connection_finder.Find(connection_callback_); | 223 connection_finder.Find(connection_callback_); |
| 223 | 224 |
| 224 ASSERT_FALSE(discovery_callback.is_null()); | 225 ASSERT_FALSE(discovery_callback.is_null()); |
| 225 discovery_callback.Run(discovery_session.Pass()); | 226 discovery_callback.Run(std::move(discovery_session)); |
| 226 | 227 |
| 227 EXPECT_CALL(*adapter_, RemoveObserver(_)); | 228 EXPECT_CALL(*adapter_, RemoveObserver(_)); |
| 228 } | 229 } |
| 229 | 230 |
| 230 // TODO(sacomoto): Remove it when ProximityAuthBleSystem is not needed anymore. | 231 // TODO(sacomoto): Remove it when ProximityAuthBleSystem is not needed anymore. |
| 231 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | 232 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, |
| 232 Find_CreatesConnectionWhenWhitelistedDeviceIsAdded) { | 233 Find_CreatesConnectionWhenWhitelistedDeviceIsAdded) { |
| 233 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( | 234 StrictMock<MockBluetoothLowEnergyConnectionFinder> connection_finder( |
| 234 device_whitelist_.get(), | 235 device_whitelist_.get(), |
| 235 BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE); | 236 BluetoothLowEnergyConnectionFinder::FIND_ANY_DEVICE); |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 run_loop.RunUntilIdle(); | 412 run_loop.RunUntilIdle(); |
| 412 } | 413 } |
| 413 | 414 |
| 414 // Restarting the discovery session. | 415 // Restarting the discovery session. |
| 415 scoped_ptr<device::MockBluetoothDiscoverySession> discovery_session( | 416 scoped_ptr<device::MockBluetoothDiscoverySession> discovery_session( |
| 416 new NiceMock<device::MockBluetoothDiscoverySession>()); | 417 new NiceMock<device::MockBluetoothDiscoverySession>()); |
| 417 last_discovery_session_alias_ = discovery_session.get(); | 418 last_discovery_session_alias_ = discovery_session.get(); |
| 418 ON_CALL(*last_discovery_session_alias_, IsActive()) | 419 ON_CALL(*last_discovery_session_alias_, IsActive()) |
| 419 .WillByDefault(Return(true)); | 420 .WillByDefault(Return(true)); |
| 420 ASSERT_FALSE(discovery_callback.is_null()); | 421 ASSERT_FALSE(discovery_callback.is_null()); |
| 421 discovery_callback.Run(discovery_session.Pass()); | 422 discovery_callback.Run(std::move(discovery_session)); |
| 422 | 423 |
| 423 // Preparing to create a GATT connection to the right device. | 424 // Preparing to create a GATT connection to the right device. |
| 424 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true); | 425 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true); |
| 425 connection = connection_finder.ExpectCreateConnection(); | 426 connection = connection_finder.ExpectCreateConnection(); |
| 426 | 427 |
| 427 // Trying to create a connection. | 428 // Trying to create a connection. |
| 428 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | 429 connection_finder.DeviceAdded(adapter_.get(), device_.get()); |
| 429 | 430 |
| 430 // Completing the connection. | 431 // Completing the connection. |
| 431 { | 432 { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 | 466 |
| 466 // Restarting the discovery session. | 467 // Restarting the discovery session. |
| 467 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | 468 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 468 .WillOnce(SaveArg<1>(&discovery_callback)); | 469 .WillOnce(SaveArg<1>(&discovery_callback)); |
| 469 connection_finder.AdapterPresentChanged(adapter_.get(), true); | 470 connection_finder.AdapterPresentChanged(adapter_.get(), true); |
| 470 connection_finder.AdapterPoweredChanged(adapter_.get(), true); | 471 connection_finder.AdapterPoweredChanged(adapter_.get(), true); |
| 471 ON_CALL(*last_discovery_session_alias_, IsActive()) | 472 ON_CALL(*last_discovery_session_alias_, IsActive()) |
| 472 .WillByDefault(Return(true)); | 473 .WillByDefault(Return(true)); |
| 473 | 474 |
| 474 ASSERT_FALSE(discovery_callback.is_null()); | 475 ASSERT_FALSE(discovery_callback.is_null()); |
| 475 discovery_callback.Run(discovery_session.Pass()); | 476 discovery_callback.Run(std::move(discovery_session)); |
| 476 | 477 |
| 477 // Preparing to create a GATT connection to the right device. | 478 // Preparing to create a GATT connection to the right device. |
| 478 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true); | 479 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true); |
| 479 FakeConnection* connection = connection_finder.ExpectCreateConnection(); | 480 FakeConnection* connection = connection_finder.ExpectCreateConnection(); |
| 480 | 481 |
| 481 // Trying to create a connection. | 482 // Trying to create a connection. |
| 482 connection_finder.DeviceAdded(adapter_.get(), device_.get()); | 483 connection_finder.DeviceAdded(adapter_.get(), device_.get()); |
| 483 | 484 |
| 484 // Completing the connection. | 485 // Completing the connection. |
| 485 base::RunLoop run_loop; | 486 base::RunLoop run_loop; |
| 486 ASSERT_FALSE(last_found_connection_); | 487 ASSERT_FALSE(last_found_connection_); |
| 487 connection->SetStatus(Connection::IN_PROGRESS); | 488 connection->SetStatus(Connection::IN_PROGRESS); |
| 488 connection->SetStatus(Connection::CONNECTED); | 489 connection->SetStatus(Connection::CONNECTED); |
| 489 run_loop.RunUntilIdle(); | 490 run_loop.RunUntilIdle(); |
| 490 EXPECT_TRUE(last_found_connection_); | 491 EXPECT_TRUE(last_found_connection_); |
| 491 } | 492 } |
| 492 | 493 |
| 493 } // namespace proximity_auth | 494 } // namespace proximity_auth |
| OLD | NEW |