Chromium Code Reviews| Index: components/proximity_auth/bluetooth_connection_finder_unittest.cc |
| diff --git a/components/proximity_auth/bluetooth_connection_finder_unittest.cc b/components/proximity_auth/bluetooth_connection_finder_unittest.cc |
| index ac6b38c3006a89e07b93b4afa216a70a76fdd812..5b708d83fdba4d8a946872479cda37152cb0113d 100644 |
| --- a/components/proximity_auth/bluetooth_connection_finder_unittest.cc |
| +++ b/components/proximity_auth/bluetooth_connection_finder_unittest.cc |
| @@ -15,6 +15,7 @@ |
| #include "device/bluetooth/bluetooth_adapter_factory.h" |
| #include "device/bluetooth/bluetooth_uuid.h" |
| #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| +#include "device/bluetooth/test/mock_bluetooth_device.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -80,12 +81,35 @@ class MockBluetoothConnectionFinder : public BluetoothConnectionFinder { |
| using BluetoothConnectionFinder::AdapterPresentChanged; |
| using BluetoothConnectionFinder::AdapterPoweredChanged; |
| + void ClearSeekCallbacks() { |
| + seek_callback_ = base::Closure(); |
| + seek_error_callback_ = bluetooth_util::ErrorCallback(); |
| + } |
| + |
| + const base::Closure& seek_callback() { return seek_callback_; } |
| + const bluetooth_util::ErrorCallback& seek_error_callback() { |
| + return seek_error_callback_; |
| + } |
| + |
| protected: |
| + // BluetoothConnectionFinder: |
| scoped_ptr<Connection> CreateConnection() override { |
| return make_scoped_ptr(CreateConnectionProxy()); |
| } |
| + void SeekDeviceByAddress( |
| + const std::string& bluetooth_address, |
| + const base::Closure& callback, |
| + const bluetooth_util::ErrorCallback& error_callback) override { |
| + EXPECT_EQ(kBluetoothAddress, bluetooth_address); |
| + seek_callback_ = callback; |
| + seek_error_callback_ = error_callback; |
| + } |
| + |
| private: |
| + base::Closure seek_callback_; |
| + bluetooth_util::ErrorCallback seek_error_callback_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnectionFinder); |
| }; |
| @@ -95,6 +119,13 @@ class ProximityAuthBluetoothConnectionFinderTest : public testing::Test { |
| protected: |
| ProximityAuthBluetoothConnectionFinderTest() |
| : adapter_(new NiceMock<device::MockBluetoothAdapter>), |
| + bluetooth_device_(new NiceMock<device::MockBluetoothDevice>( |
| + adapter_.get(), |
| + device::BluetoothDevice::DEVICE_PHONE, |
| + kDeviceName, |
| + kBluetoothAddress, |
| + true, |
| + false)), |
| connection_callback_(base::Bind( |
| &ProximityAuthBluetoothConnectionFinderTest::OnConnectionFound, |
| base::Unretained(this))) { |
| @@ -104,6 +135,8 @@ class ProximityAuthBluetoothConnectionFinderTest : public testing::Test { |
| // can override this as needed. |
| ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); |
| ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); |
| + ON_CALL(*adapter_, GetDevice(kBluetoothAddress)) |
| + .WillByDefault(Return(bluetooth_device_.get())); |
|
sacomoto
2015/08/10 19:40:28
nit: add a comment that by default the device is k
Tim Song
2015/08/10 22:17:55
Done.
|
| } |
| MOCK_METHOD1(OnConnectionFoundProxy, void(Connection* connection)); |
| @@ -113,6 +146,7 @@ class ProximityAuthBluetoothConnectionFinderTest : public testing::Test { |
| } |
| scoped_refptr<device::MockBluetoothAdapter> adapter_; |
| + scoped_ptr<device::MockBluetoothDevice> bluetooth_device_; |
| ConnectionFinder::ConnectionCallback connection_callback_; |
| private: |
| @@ -164,11 +198,15 @@ TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_ConnectionSucceeds) { |
| MockConnection* connection = connection_finder.ExpectCreateConnection(); |
| connection_finder.Find(connection_callback_); |
| + EXPECT_TRUE(connection_finder.seek_callback().is_null()); |
| + EXPECT_TRUE(connection_finder.seek_error_callback().is_null()); |
| connection->SetStatus(Connection::IN_PROGRESS); |
| + base::RunLoop run_loop; |
| EXPECT_CALL(*this, OnConnectionFoundProxy(_)); |
| connection->SetStatus(Connection::CONNECTED); |
| + run_loop.RunUntilIdle(); |
| } |
| TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
| @@ -180,15 +218,24 @@ TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
| connection->SetStatus(Connection::IN_PROGRESS); |
| - EXPECT_CALL(*this, OnConnectionFoundProxy(_)); |
| - EXPECT_CALL(*adapter_, RemoveObserver(&connection_finder)); |
| - connection->SetStatus(Connection::CONNECTED); |
| + { |
| + base::RunLoop run_loop; |
| + EXPECT_CALL(*this, OnConnectionFoundProxy(_)); |
| + EXPECT_CALL(*adapter_, RemoveObserver(&connection_finder)); |
| + connection->SetStatus(Connection::CONNECTED); |
| + run_loop.RunUntilIdle(); |
| + } |
| - // If for some reason the connection sends more status updates, they should be |
| - // ignored. |
| - EXPECT_CALL(*this, OnConnectionFoundProxy(_)).Times(0); |
| - connection->SetStatus(Connection::IN_PROGRESS); |
| - connection->SetStatus(Connection::CONNECTED); |
| + { |
| + // If for some reason the connection sends more status updates, they should |
| + // be |
|
sacomoto
2015/08/10 19:40:28
nit: kill the extra line.
Tim Song
2015/08/10 22:17:55
Done.
|
| + // ignored. |
| + base::RunLoop run_loop; |
| + EXPECT_CALL(*this, OnConnectionFoundProxy(_)).Times(0); |
| + connection->SetStatus(Connection::IN_PROGRESS); |
| + connection->SetStatus(Connection::CONNECTED); |
| + run_loop.RunUntilIdle(); |
| + } |
| } |
| TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
| @@ -279,4 +326,69 @@ TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
| connection_finder.AdapterPresentChanged(adapter_.get(), true); |
| } |
| +TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
| + Find_DeviceNotKnown_SeekDeviceSucceeds) { |
| + // If the BluetoothDevice is not known by the adapter, |connection_finder| |
| + // will call SeekDeviceByAddress() first to make it known. |
| + ON_CALL(*adapter_, GetDevice(kBluetoothAddress)) |
| + .WillByDefault(Return(nullptr)); |
| + StrictMock<MockBluetoothConnectionFinder> connection_finder; |
| + connection_finder.Find(connection_callback_); |
| + ASSERT_FALSE(connection_finder.seek_callback().is_null()); |
| + EXPECT_FALSE(connection_finder.seek_error_callback().is_null()); |
| + |
| + // After seeking is successful, the normal flow should resume. |
|
sacomoto
2015/08/10 19:40:28
optional: you could refactor this in a helper meth
Tim Song
2015/08/10 22:17:55
Done.
|
| + ON_CALL(*adapter_, GetDevice(kBluetoothAddress)) |
| + .WillByDefault(Return(bluetooth_device_.get())); |
| + MockConnection* connection = connection_finder.ExpectCreateConnection(); |
| + connection_finder.seek_callback().Run(); |
| + |
| + connection->SetStatus(Connection::IN_PROGRESS); |
| + base::RunLoop run_loop; |
| + EXPECT_CALL(*this, OnConnectionFoundProxy(_)); |
| + connection->SetStatus(Connection::CONNECTED); |
| + run_loop.RunUntilIdle(); |
| +} |
| + |
| +TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
| + Find_DeviceNotKnown_SeekDeviceFailThenSucceeds) { |
| + // If the BluetoothDevice is not known by the adapter, |connection_finder| |
| + // will call SeekDeviceByAddress() first to make it known. |
| + ON_CALL(*adapter_, GetDevice(kBluetoothAddress)) |
| + .WillByDefault(Return(nullptr)); |
| + StrictMock<MockBluetoothConnectionFinder> connection_finder; |
| + connection_finder.Find(connection_callback_); |
| + EXPECT_FALSE(connection_finder.seek_callback().is_null()); |
| + ASSERT_FALSE(connection_finder.seek_error_callback().is_null()); |
| + |
| + // If the seek fails, then |connection_finder| will post a delayed poll to |
| + // reattempt the seek. |
| + connection_finder.seek_error_callback().Run("Seek failed for test."); |
| + connection_finder.ClearSeekCallbacks(); |
| + EXPECT_TRUE(connection_finder.seek_callback().is_null()); |
| + EXPECT_TRUE(connection_finder.seek_error_callback().is_null()); |
| + |
| + // Check that seek is reattempted. |
| + { |
| + base::RunLoop run_loop; |
| + run_loop.RunUntilIdle(); |
| + ASSERT_FALSE(connection_finder.seek_callback().is_null()); |
| + EXPECT_FALSE(connection_finder.seek_error_callback().is_null()); |
| + } |
| + |
| + // Successfully connect to the Bluetooth device. |
| + ON_CALL(*adapter_, GetDevice(kBluetoothAddress)) |
| + .WillByDefault(Return(bluetooth_device_.get())); |
| + MockConnection* connection = connection_finder.ExpectCreateConnection(); |
| + connection_finder.seek_callback().Run(); |
| + |
| + connection->SetStatus(Connection::IN_PROGRESS); |
| + { |
| + base::RunLoop run_loop; |
| + EXPECT_CALL(*this, OnConnectionFoundProxy(_)); |
| + connection->SetStatus(Connection::CONNECTED); |
| + run_loop.RunUntilIdle(); |
| + } |
| +} |
| + |
| } // namespace proximity_auth |