Chromium Code Reviews| 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 <memory> | |
| 7 #include <string> | 8 #include <string> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" | |
| 12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 15 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" | 19 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h" |
| 19 #include "components/proximity_auth/fake_connection.h" | 20 #include "components/proximity_auth/fake_connection.h" |
| 20 #include "components/proximity_auth/proximity_auth_test_util.h" | 21 #include "components/proximity_auth/proximity_auth_test_util.h" |
| 21 #include "components/proximity_auth/remote_device.h" | 22 #include "components/proximity_auth/remote_device.h" |
| 22 #include "components/proximity_auth/wire_message.h" | 23 #include "components/proximity_auth/wire_message.h" |
| 23 #include "device/bluetooth/bluetooth_adapter_factory.h" | 24 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 FinderStrategy finder_strategy) | 63 FinderStrategy finder_strategy) |
| 63 : BluetoothLowEnergyConnectionFinder(CreateLERemoteDeviceForTest(), | 64 : BluetoothLowEnergyConnectionFinder(CreateLERemoteDeviceForTest(), |
| 64 kServiceUUID, | 65 kServiceUUID, |
| 65 finder_strategy, | 66 finder_strategy, |
| 66 device_whitelist, | 67 device_whitelist, |
| 67 nullptr, | 68 nullptr, |
| 68 kMaxNumberOfAttempts) {} | 69 kMaxNumberOfAttempts) {} |
| 69 | 70 |
| 70 ~MockBluetoothLowEnergyConnectionFinder() override {} | 71 ~MockBluetoothLowEnergyConnectionFinder() override {} |
| 71 | 72 |
| 72 // Mock methods don't support return type scoped_ptr<>. This is a possible | 73 // Mock methods don't support return type std::unique_ptr<>. This is a |
| 74 // possible | |
| 73 // workaround: mock a proxy method to be called by the target overrided method | 75 // workaround: mock a proxy method to be called by the target overrided method |
| 74 // (CreateConnection). | 76 // (CreateConnection). |
|
Ilya Sherman
2016/04/20 22:08:56
Also, is this comment still true?
Ilya Sherman
2016/04/20 22:08:56
nit: Please rewrap
dcheng
2016/04/20 22:13:52
Done.
(Also, yes, this comment is still true.)
| |
| 75 MOCK_METHOD0(CreateConnectionProxy, Connection*()); | 77 MOCK_METHOD0(CreateConnectionProxy, Connection*()); |
| 76 | 78 |
| 77 // Creates a mock connection and sets an expectation that the mock connection | 79 // Creates a mock connection and sets an expectation that the mock connection |
| 78 // finder's CreateConnection() method will be called and will return the | 80 // finder's CreateConnection() method will be called and will return the |
| 79 // created connection. Returns a reference to the created connection. | 81 // created connection. Returns a reference to the created connection. |
| 80 // NOTE: The returned connection's lifetime is managed by the connection | 82 // NOTE: The returned connection's lifetime is managed by the connection |
| 81 // finder. | 83 // finder. |
| 82 FakeConnection* ExpectCreateConnection() { | 84 FakeConnection* ExpectCreateConnection() { |
| 83 scoped_ptr<FakeConnection> connection( | 85 std::unique_ptr<FakeConnection> connection( |
| 84 new FakeConnection(CreateLERemoteDeviceForTest())); | 86 new FakeConnection(CreateLERemoteDeviceForTest())); |
| 85 FakeConnection* connection_alias = connection.get(); | 87 FakeConnection* connection_alias = connection.get(); |
| 86 EXPECT_CALL(*this, CreateConnectionProxy()) | 88 EXPECT_CALL(*this, CreateConnectionProxy()) |
| 87 .WillOnce(Return(connection.release())); | 89 .WillOnce(Return(connection.release())); |
| 88 return connection_alias; | 90 return connection_alias; |
| 89 } | 91 } |
| 90 | 92 |
| 91 MOCK_METHOD0(CloseGattConnectionProxy, void(void)); | 93 MOCK_METHOD0(CloseGattConnectionProxy, void(void)); |
| 92 | 94 |
| 93 protected: | 95 protected: |
| 94 scoped_ptr<Connection> CreateConnection( | 96 std::unique_ptr<Connection> CreateConnection( |
| 95 const std::string& device_address) override { | 97 const std::string& device_address) override { |
| 96 return make_scoped_ptr(CreateConnectionProxy()); | 98 return base::WrapUnique(CreateConnectionProxy()); |
| 97 } | 99 } |
| 98 | 100 |
| 99 private: | 101 private: |
| 100 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnectionFinder); | 102 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnectionFinder); |
| 101 }; | 103 }; |
| 102 | 104 |
| 103 } // namespace | 105 } // namespace |
| 104 | 106 |
| 105 class ProximityAuthBluetoothLowEnergyConnectionFinderTest | 107 class ProximityAuthBluetoothLowEnergyConnectionFinderTest |
| 106 : public testing::Test { | 108 : public testing::Test { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 125 std::vector<const device::BluetoothDevice*> devices; | 127 std::vector<const device::BluetoothDevice*> devices; |
| 126 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); | 128 ON_CALL(*adapter_, GetDevices()).WillByDefault(Return(devices)); |
| 127 | 129 |
| 128 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | 130 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); |
| 129 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | 131 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); |
| 130 | 132 |
| 131 ON_CALL(*device_whitelist_, HasDeviceWithAddress(_)) | 133 ON_CALL(*device_whitelist_, HasDeviceWithAddress(_)) |
| 132 .WillByDefault(Return(false)); | 134 .WillByDefault(Return(false)); |
| 133 } | 135 } |
| 134 | 136 |
| 135 void OnConnectionFound(scoped_ptr<Connection> connection) { | 137 void OnConnectionFound(std::unique_ptr<Connection> connection) { |
| 136 last_found_connection_ = std::move(connection); | 138 last_found_connection_ = std::move(connection); |
| 137 } | 139 } |
| 138 | 140 |
| 139 void FindAndExpectStartDiscovery( | 141 void FindAndExpectStartDiscovery( |
| 140 BluetoothLowEnergyConnectionFinder& connection_finder) { | 142 BluetoothLowEnergyConnectionFinder& connection_finder) { |
| 141 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; | 143 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; |
| 142 scoped_ptr<device::MockBluetoothDiscoverySession> discovery_session( | 144 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( |
| 143 new NiceMock<device::MockBluetoothDiscoverySession>()); | 145 new NiceMock<device::MockBluetoothDiscoverySession>()); |
| 144 last_discovery_session_alias_ = discovery_session.get(); | 146 last_discovery_session_alias_ = discovery_session.get(); |
| 145 | 147 |
| 146 // Starting a discovery session. StartDiscoveryWithFilterRaw is a proxy for | 148 // Starting a discovery session. StartDiscoveryWithFilterRaw is a proxy for |
| 147 // StartDiscoveryWithFilter. | 149 // StartDiscoveryWithFilter. |
| 148 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | 150 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 149 .WillOnce(SaveArg<1>(&discovery_callback)); | 151 .WillOnce(SaveArg<1>(&discovery_callback)); |
| 150 EXPECT_CALL(*adapter_, AddObserver(_)); | 152 EXPECT_CALL(*adapter_, AddObserver(_)); |
| 151 ON_CALL(*last_discovery_session_alias_, IsActive()) | 153 ON_CALL(*last_discovery_session_alias_, IsActive()) |
| 152 .WillByDefault(Return(true)); | 154 .WillByDefault(Return(true)); |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 165 bool paired) { | 167 bool paired) { |
| 166 std::vector<device::BluetoothUUID> uuids; | 168 std::vector<device::BluetoothUUID> uuids; |
| 167 uuids.push_back(device::BluetoothUUID(uuid)); | 169 uuids.push_back(device::BluetoothUUID(uuid)); |
| 168 ON_CALL(*device_, GetUUIDs()).WillByDefault(Return(uuids)); | 170 ON_CALL(*device_, GetUUIDs()).WillByDefault(Return(uuids)); |
| 169 ON_CALL(*device_, GetAddress()).WillByDefault(Return(address)); | 171 ON_CALL(*device_, GetAddress()).WillByDefault(Return(address)); |
| 170 ON_CALL(*device_, IsPaired()).WillByDefault(Return(paired)); | 172 ON_CALL(*device_, IsPaired()).WillByDefault(Return(paired)); |
| 171 } | 173 } |
| 172 | 174 |
| 173 scoped_refptr<device::MockBluetoothAdapter> adapter_; | 175 scoped_refptr<device::MockBluetoothAdapter> adapter_; |
| 174 ConnectionFinder::ConnectionCallback connection_callback_; | 176 ConnectionFinder::ConnectionCallback connection_callback_; |
| 175 scoped_ptr<device::MockBluetoothDevice> device_; | 177 std::unique_ptr<device::MockBluetoothDevice> device_; |
| 176 scoped_ptr<Connection> last_found_connection_; | 178 std::unique_ptr<Connection> last_found_connection_; |
| 177 scoped_ptr<MockBluetoothLowEnergyDeviceWhitelist> device_whitelist_; | 179 std::unique_ptr<MockBluetoothLowEnergyDeviceWhitelist> device_whitelist_; |
| 178 device::MockBluetoothDiscoverySession* last_discovery_session_alias_; | 180 device::MockBluetoothDiscoverySession* last_discovery_session_alias_; |
| 179 | 181 |
| 180 private: | 182 private: |
| 181 base::MessageLoop message_loop_; | 183 base::MessageLoop message_loop_; |
| 182 }; | 184 }; |
| 183 | 185 |
| 184 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | 186 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, |
| 185 ConstructAndDestroyDoesntCrash) { | 187 ConstructAndDestroyDoesntCrash) { |
| 186 // Destroying a BluetoothConnectionFinder for which Find() has not been called | 188 // Destroying a BluetoothConnectionFinder for which Find() has not been called |
| 187 // should not crash. | 189 // should not crash. |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 204 } | 206 } |
| 205 | 207 |
| 206 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, | 208 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest, |
| 207 Find_StopsDiscoverySessionBeforeDestroying) { | 209 Find_StopsDiscoverySessionBeforeDestroying) { |
| 208 BluetoothLowEnergyConnectionFinder connection_finder( | 210 BluetoothLowEnergyConnectionFinder connection_finder( |
| 209 CreateLERemoteDeviceForTest(), kServiceUUID, | 211 CreateLERemoteDeviceForTest(), kServiceUUID, |
| 210 BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE, | 212 BluetoothLowEnergyConnectionFinder::FIND_PAIRED_DEVICE, |
| 211 device_whitelist_.get(), nullptr, kMaxNumberOfAttempts); | 213 device_whitelist_.get(), nullptr, kMaxNumberOfAttempts); |
| 212 | 214 |
| 213 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; | 215 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; |
| 214 scoped_ptr<device::MockBluetoothDiscoverySession> discovery_session( | 216 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( |
| 215 new NiceMock<device::MockBluetoothDiscoverySession>()); | 217 new NiceMock<device::MockBluetoothDiscoverySession>()); |
| 216 device::MockBluetoothDiscoverySession* discovery_session_alias = | 218 device::MockBluetoothDiscoverySession* discovery_session_alias = |
| 217 discovery_session.get(); | 219 discovery_session.get(); |
| 218 | 220 |
| 219 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | 221 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 220 .WillOnce(SaveArg<1>(&discovery_callback)); | 222 .WillOnce(SaveArg<1>(&discovery_callback)); |
| 221 ON_CALL(*discovery_session_alias, IsActive()).WillByDefault(Return(true)); | 223 ON_CALL(*discovery_session_alias, IsActive()).WillByDefault(Return(true)); |
| 222 EXPECT_CALL(*adapter_, AddObserver(_)); | 224 EXPECT_CALL(*adapter_, AddObserver(_)); |
| 223 connection_finder.Find(connection_callback_); | 225 connection_finder.Find(connection_callback_); |
| 224 | 226 |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 406 .WillOnce(SaveArg<1>(&discovery_callback)); | 408 .WillOnce(SaveArg<1>(&discovery_callback)); |
| 407 | 409 |
| 408 // Connection fails. | 410 // Connection fails. |
| 409 { | 411 { |
| 410 base::RunLoop run_loop; | 412 base::RunLoop run_loop; |
| 411 connection->SetStatus(Connection::DISCONNECTED); | 413 connection->SetStatus(Connection::DISCONNECTED); |
| 412 run_loop.RunUntilIdle(); | 414 run_loop.RunUntilIdle(); |
| 413 } | 415 } |
| 414 | 416 |
| 415 // Restarting the discovery session. | 417 // Restarting the discovery session. |
| 416 scoped_ptr<device::MockBluetoothDiscoverySession> discovery_session( | 418 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( |
| 417 new NiceMock<device::MockBluetoothDiscoverySession>()); | 419 new NiceMock<device::MockBluetoothDiscoverySession>()); |
| 418 last_discovery_session_alias_ = discovery_session.get(); | 420 last_discovery_session_alias_ = discovery_session.get(); |
| 419 ON_CALL(*last_discovery_session_alias_, IsActive()) | 421 ON_CALL(*last_discovery_session_alias_, IsActive()) |
| 420 .WillByDefault(Return(true)); | 422 .WillByDefault(Return(true)); |
| 421 ASSERT_FALSE(discovery_callback.is_null()); | 423 ASSERT_FALSE(discovery_callback.is_null()); |
| 422 discovery_callback.Run(std::move(discovery_session)); | 424 discovery_callback.Run(std::move(discovery_session)); |
| 423 | 425 |
| 424 // Preparing to create a GATT connection to the right device. | 426 // Preparing to create a GATT connection to the right device. |
| 425 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true); | 427 PrepareDevice(kServiceUUID, kTestRemoteDeviceBluetoothAddress, true); |
| 426 connection = connection_finder.ExpectCreateConnection(); | 428 connection = connection_finder.ExpectCreateConnection(); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 453 ON_CALL(*last_discovery_session_alias_, IsActive()) | 455 ON_CALL(*last_discovery_session_alias_, IsActive()) |
| 454 .WillByDefault(Return(false)); | 456 .WillByDefault(Return(false)); |
| 455 connection_finder.AdapterPoweredChanged(adapter_.get(), false); | 457 connection_finder.AdapterPoweredChanged(adapter_.get(), false); |
| 456 connection_finder.AdapterPresentChanged(adapter_.get(), false); | 458 connection_finder.AdapterPresentChanged(adapter_.get(), false); |
| 457 | 459 |
| 458 // Adding the adapter. | 460 // Adding the adapter. |
| 459 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | 461 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); |
| 460 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | 462 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); |
| 461 | 463 |
| 462 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; | 464 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback; |
| 463 scoped_ptr<device::MockBluetoothDiscoverySession> discovery_session( | 465 std::unique_ptr<device::MockBluetoothDiscoverySession> discovery_session( |
| 464 new NiceMock<device::MockBluetoothDiscoverySession>()); | 466 new NiceMock<device::MockBluetoothDiscoverySession>()); |
| 465 last_discovery_session_alias_ = discovery_session.get(); | 467 last_discovery_session_alias_ = discovery_session.get(); |
| 466 | 468 |
| 467 // Restarting the discovery session. | 469 // Restarting the discovery session. |
| 468 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) | 470 EXPECT_CALL(*adapter_, StartDiscoverySessionWithFilterRaw(_, _, _)) |
| 469 .WillOnce(SaveArg<1>(&discovery_callback)); | 471 .WillOnce(SaveArg<1>(&discovery_callback)); |
| 470 connection_finder.AdapterPresentChanged(adapter_.get(), true); | 472 connection_finder.AdapterPresentChanged(adapter_.get(), true); |
| 471 connection_finder.AdapterPoweredChanged(adapter_.get(), true); | 473 connection_finder.AdapterPoweredChanged(adapter_.get(), true); |
| 472 ON_CALL(*last_discovery_session_alias_, IsActive()) | 474 ON_CALL(*last_discovery_session_alias_, IsActive()) |
| 473 .WillByDefault(Return(true)); | 475 .WillByDefault(Return(true)); |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 485 // Completing the connection. | 487 // Completing the connection. |
| 486 base::RunLoop run_loop; | 488 base::RunLoop run_loop; |
| 487 ASSERT_FALSE(last_found_connection_); | 489 ASSERT_FALSE(last_found_connection_); |
| 488 connection->SetStatus(Connection::IN_PROGRESS); | 490 connection->SetStatus(Connection::IN_PROGRESS); |
| 489 connection->SetStatus(Connection::CONNECTED); | 491 connection->SetStatus(Connection::CONNECTED); |
| 490 run_loop.RunUntilIdle(); | 492 run_loop.RunUntilIdle(); |
| 491 EXPECT_TRUE(last_found_connection_); | 493 EXPECT_TRUE(last_found_connection_); |
| 492 } | 494 } |
| 493 | 495 |
| 494 } // namespace proximity_auth | 496 } // namespace proximity_auth |
| OLD | NEW |