| 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/throttled_bluetooth_connection_finder.h" | 5 #include "components/proximity_auth/throttled_bluetooth_connection_finder.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/test/test_simple_task_runner.h" | 12 #include "base/test/test_simple_task_runner.h" |
| 12 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 13 #include "components/proximity_auth/bluetooth_connection_finder.h" | 14 #include "components/proximity_auth/bluetooth_connection_finder.h" |
| 14 #include "components/proximity_auth/bluetooth_throttler.h" | 15 #include "components/proximity_auth/bluetooth_throttler.h" |
| 15 #include "components/proximity_auth/fake_connection.h" | 16 #include "components/proximity_auth/fake_connection.h" |
| 16 #include "components/proximity_auth/remote_device.h" | 17 #include "components/proximity_auth/remote_device.h" |
| 17 #include "components/proximity_auth/wire_message.h" | 18 #include "components/proximity_auth/wire_message.h" |
| 18 #include "device/bluetooth/bluetooth_uuid.h" | 19 #include "device/bluetooth/bluetooth_uuid.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| 22 using testing::NiceMock; | 23 using testing::NiceMock; |
| 23 using testing::Return; | 24 using testing::Return; |
| 24 using testing::_; | 25 using testing::_; |
| 25 | 26 |
| 26 namespace proximity_auth { | 27 namespace proximity_auth { |
| 27 namespace { | 28 namespace { |
| 28 | 29 |
| 29 const int kPollingIntervalSeconds = 7; | 30 const int kPollingIntervalSeconds = 7; |
| 30 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF"; | 31 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF"; |
| 31 | 32 |
| 32 // A callback that stores a found |connection| into |out|. | 33 // A callback that stores a found |connection| into |out|. |
| 33 void SaveConnection(scoped_ptr<Connection>* out, | 34 void SaveConnection(std::unique_ptr<Connection>* out, |
| 34 scoped_ptr<Connection> connection) { | 35 std::unique_ptr<Connection> connection) { |
| 35 *out = std::move(connection); | 36 *out = std::move(connection); |
| 36 } | 37 } |
| 37 | 38 |
| 38 class MockBluetoothThrottler : public BluetoothThrottler { | 39 class MockBluetoothThrottler : public BluetoothThrottler { |
| 39 public: | 40 public: |
| 40 MockBluetoothThrottler() {} | 41 MockBluetoothThrottler() {} |
| 41 ~MockBluetoothThrottler() override {} | 42 ~MockBluetoothThrottler() override {} |
| 42 | 43 |
| 43 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); | 44 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta()); |
| 44 MOCK_METHOD1(OnConnection, void(Connection* connection)); | 45 MOCK_METHOD1(OnConnection, void(Connection* connection)); |
| 45 | 46 |
| 46 private: | 47 private: |
| 47 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); | 48 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler); |
| 48 }; | 49 }; |
| 49 | 50 |
| 50 class FakeBluetoothConnectionFinder : public BluetoothConnectionFinder { | 51 class FakeBluetoothConnectionFinder : public BluetoothConnectionFinder { |
| 51 public: | 52 public: |
| 52 FakeBluetoothConnectionFinder() | 53 FakeBluetoothConnectionFinder() |
| 53 : BluetoothConnectionFinder( | 54 : BluetoothConnectionFinder( |
| 54 RemoteDevice(), | 55 RemoteDevice(), |
| 55 device::BluetoothUUID(kUuid), | 56 device::BluetoothUUID(kUuid), |
| 56 base::TimeDelta::FromSeconds(kPollingIntervalSeconds)) {} | 57 base::TimeDelta::FromSeconds(kPollingIntervalSeconds)) {} |
| 57 ~FakeBluetoothConnectionFinder() override {} | 58 ~FakeBluetoothConnectionFinder() override {} |
| 58 | 59 |
| 59 void Find(const ConnectionCallback& connection_callback) override { | 60 void Find(const ConnectionCallback& connection_callback) override { |
| 60 connection_callback.Run( | 61 connection_callback.Run( |
| 61 make_scoped_ptr(new FakeConnection(RemoteDevice()))); | 62 base::WrapUnique(new FakeConnection(RemoteDevice()))); |
| 62 } | 63 } |
| 63 | 64 |
| 64 private: | 65 private: |
| 65 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothConnectionFinder); | 66 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothConnectionFinder); |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 } // namespace | 69 } // namespace |
| 69 | 70 |
| 70 class ProximityAuthThrottledBluetoothConnectionFinderTest | 71 class ProximityAuthThrottledBluetoothConnectionFinderTest |
| 71 : public testing::Test { | 72 : public testing::Test { |
| 72 public: | 73 public: |
| 73 ProximityAuthThrottledBluetoothConnectionFinderTest() | 74 ProximityAuthThrottledBluetoothConnectionFinderTest() |
| 74 : task_runner_(new base::TestSimpleTaskRunner) {} | 75 : task_runner_(new base::TestSimpleTaskRunner) {} |
| 75 | 76 |
| 76 protected: | 77 protected: |
| 77 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; | 78 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 78 NiceMock<MockBluetoothThrottler> throttler_; | 79 NiceMock<MockBluetoothThrottler> throttler_; |
| 79 }; | 80 }; |
| 80 | 81 |
| 81 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, | 82 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, |
| 82 Find_ExecutesImmediatelyWhenUnthrottled) { | 83 Find_ExecutesImmediatelyWhenUnthrottled) { |
| 83 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); | 84 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); |
| 84 | 85 |
| 85 ThrottledBluetoothConnectionFinder connection_finder( | 86 ThrottledBluetoothConnectionFinder connection_finder( |
| 86 make_scoped_ptr(new FakeBluetoothConnectionFinder), task_runner_, | 87 base::WrapUnique(new FakeBluetoothConnectionFinder), task_runner_, |
| 87 &throttler_); | 88 &throttler_); |
| 88 scoped_ptr<Connection> connection; | 89 std::unique_ptr<Connection> connection; |
| 89 connection_finder.Find(base::Bind(&SaveConnection, &connection)); | 90 connection_finder.Find(base::Bind(&SaveConnection, &connection)); |
| 90 EXPECT_TRUE(connection); | 91 EXPECT_TRUE(connection); |
| 91 } | 92 } |
| 92 | 93 |
| 93 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, | 94 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, |
| 94 Find_ExecutesAfterADelayWhenThrottled) { | 95 Find_ExecutesAfterADelayWhenThrottled) { |
| 95 ON_CALL(throttler_, GetDelay()) | 96 ON_CALL(throttler_, GetDelay()) |
| 96 .WillByDefault(Return(base::TimeDelta::FromSeconds(1))); | 97 .WillByDefault(Return(base::TimeDelta::FromSeconds(1))); |
| 97 | 98 |
| 98 ThrottledBluetoothConnectionFinder connection_finder( | 99 ThrottledBluetoothConnectionFinder connection_finder( |
| 99 make_scoped_ptr(new FakeBluetoothConnectionFinder), task_runner_, | 100 base::WrapUnique(new FakeBluetoothConnectionFinder), task_runner_, |
| 100 &throttler_); | 101 &throttler_); |
| 101 scoped_ptr<Connection> connection; | 102 std::unique_ptr<Connection> connection; |
| 102 connection_finder.Find(base::Bind(&SaveConnection, &connection)); | 103 connection_finder.Find(base::Bind(&SaveConnection, &connection)); |
| 103 EXPECT_FALSE(connection); | 104 EXPECT_FALSE(connection); |
| 104 | 105 |
| 105 // The connection should be found once the throttling period has elapsed. | 106 // The connection should be found once the throttling period has elapsed. |
| 106 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); | 107 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); |
| 107 task_runner_->RunUntilIdle(); | 108 task_runner_->RunUntilIdle(); |
| 108 EXPECT_TRUE(connection); | 109 EXPECT_TRUE(connection); |
| 109 } | 110 } |
| 110 | 111 |
| 111 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, | 112 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest, |
| 112 OnConnection_ForwardsNotificationToThrottler) { | 113 OnConnection_ForwardsNotificationToThrottler) { |
| 113 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); | 114 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta())); |
| 114 | 115 |
| 115 ThrottledBluetoothConnectionFinder connection_finder( | 116 ThrottledBluetoothConnectionFinder connection_finder( |
| 116 make_scoped_ptr(new FakeBluetoothConnectionFinder), task_runner_, | 117 base::WrapUnique(new FakeBluetoothConnectionFinder), task_runner_, |
| 117 &throttler_); | 118 &throttler_); |
| 118 scoped_ptr<Connection> connection; | 119 std::unique_ptr<Connection> connection; |
| 119 EXPECT_CALL(throttler_, OnConnection(_)); | 120 EXPECT_CALL(throttler_, OnConnection(_)); |
| 120 connection_finder.Find(base::Bind(&SaveConnection, &connection)); | 121 connection_finder.Find(base::Bind(&SaveConnection, &connection)); |
| 121 EXPECT_TRUE(connection); | 122 EXPECT_TRUE(connection); |
| 122 } | 123 } |
| 123 | 124 |
| 124 } // namespace proximity_auth | 125 } // namespace proximity_auth |
| OLD | NEW |