| 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/bluetooth_throttler_impl.h" | 5 #include "components/proximity_auth/bluetooth_throttler_impl.h" |
| 6 | 6 |
| 7 #include "base/test/simple_test_tick_clock.h" | 7 #include "base/test/simple_test_tick_clock.h" |
| 8 #include "base/time/time.h" | 8 #include "base/time/time.h" |
| 9 #include "components/proximity_auth/fake_connection.h" |
| 9 #include "components/proximity_auth/wire_message.h" | 10 #include "components/proximity_auth/wire_message.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 namespace proximity_auth { | 13 namespace proximity_auth { |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 class StubConnection : public Connection { | |
| 16 public: | |
| 17 StubConnection() : Connection(RemoteDevice()) {} | |
| 18 ~StubConnection() override {} | |
| 19 | |
| 20 void Connect() override {} | |
| 21 void Disconnect() override {} | |
| 22 void SendMessageImpl(scoped_ptr<WireMessage> message) override {} | |
| 23 | |
| 24 private: | |
| 25 DISALLOW_COPY_AND_ASSIGN(StubConnection); | |
| 26 }; | |
| 27 | |
| 28 class TestBluetoothThrottler : public BluetoothThrottlerImpl { | 16 class TestBluetoothThrottler : public BluetoothThrottlerImpl { |
| 29 public: | 17 public: |
| 30 explicit TestBluetoothThrottler(scoped_ptr<base::TickClock> clock) | 18 explicit TestBluetoothThrottler(scoped_ptr<base::TickClock> clock) |
| 31 : BluetoothThrottlerImpl(clock.Pass()) {} | 19 : BluetoothThrottlerImpl(clock.Pass()) {} |
| 32 ~TestBluetoothThrottler() override {} | 20 ~TestBluetoothThrottler() override {} |
| 33 | 21 |
| 34 // Increase visibility for testing. | 22 // Increase visibility for testing. |
| 35 using BluetoothThrottlerImpl::GetCooldownTimeDelta; | 23 using BluetoothThrottlerImpl::GetCooldownTimeDelta; |
| 36 | 24 |
| 37 private: | 25 private: |
| 38 DISALLOW_COPY_AND_ASSIGN(TestBluetoothThrottler); | 26 DISALLOW_COPY_AND_ASSIGN(TestBluetoothThrottler); |
| 39 }; | 27 }; |
| 40 | 28 |
| 41 } // namespace | 29 } // namespace |
| 42 | 30 |
| 43 class ProximityAuthBluetoothThrottlerImplTest : public testing::Test { | 31 class ProximityAuthBluetoothThrottlerImplTest : public testing::Test { |
| 44 public: | 32 public: |
| 45 ProximityAuthBluetoothThrottlerImplTest() | 33 ProximityAuthBluetoothThrottlerImplTest() |
| 46 : clock_(new base::SimpleTestTickClock), | 34 : clock_(new base::SimpleTestTickClock), |
| 47 throttler_(make_scoped_ptr(clock_)) { | 35 throttler_(make_scoped_ptr(clock_)) { |
| 48 // The throttler treats null times as special, so start with a non-null | 36 // The throttler treats null times as special, so start with a non-null |
| 49 // time. | 37 // time. |
| 50 clock_->Advance(base::TimeDelta::FromSeconds(1)); | 38 clock_->Advance(base::TimeDelta::FromSeconds(1)); |
| 51 } | 39 } |
| 52 | 40 |
| 53 void PerformConnectionStateTransition(Connection::Status old_status, | 41 void PerformConnectionStateTransition(Connection::Status old_status, |
| 54 Connection::Status new_status) { | 42 Connection::Status new_status) { |
| 55 StubConnection connection; | 43 FakeConnection connection((RemoteDevice())); |
| 56 throttler_.OnConnection(&connection); | 44 throttler_.OnConnection(&connection); |
| 57 static_cast<ConnectionObserver*>(&throttler_) | 45 static_cast<ConnectionObserver*>(&throttler_) |
| 58 ->OnConnectionStatusChanged(&connection, old_status, new_status); | 46 ->OnConnectionStatusChanged(&connection, old_status, new_status); |
| 59 } | 47 } |
| 60 | 48 |
| 61 protected: | 49 protected: |
| 62 // The clock is owned by the |throttler_|. | 50 // The clock is owned by the |throttler_|. |
| 63 base::SimpleTestTickClock* clock_; | 51 base::SimpleTestTickClock* clock_; |
| 64 TestBluetoothThrottler throttler_; | 52 TestBluetoothThrottler throttler_; |
| 65 }; | 53 }; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 GetDelay_DelayedConnectionAfterInProgressDisconnectIsNotThrottled) { | 88 GetDelay_DelayedConnectionAfterInProgressDisconnectIsNotThrottled) { |
| 101 // Simulate an attempt to connect (in progress connection) followed by a | 89 // Simulate an attempt to connect (in progress connection) followed by a |
| 102 // disconnection, then allow the cooldown period to elapse. | 90 // disconnection, then allow the cooldown period to elapse. |
| 103 PerformConnectionStateTransition(Connection::IN_PROGRESS, | 91 PerformConnectionStateTransition(Connection::IN_PROGRESS, |
| 104 Connection::DISCONNECTED); | 92 Connection::DISCONNECTED); |
| 105 clock_->Advance(throttler_.GetCooldownTimeDelta()); | 93 clock_->Advance(throttler_.GetCooldownTimeDelta()); |
| 106 EXPECT_EQ(base::TimeDelta(), throttler_.GetDelay()); | 94 EXPECT_EQ(base::TimeDelta(), throttler_.GetDelay()); |
| 107 } | 95 } |
| 108 | 96 |
| 109 } // namespace proximity_auth | 97 } // namespace proximity_auth |
| OLD | NEW |