| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/proximity_auth/bluetooth_throttler_impl.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/macros.h" | |
| 10 #include "base/memory/ptr_util.h" | |
| 11 #include "base/test/simple_test_tick_clock.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "components/proximity_auth/fake_connection.h" | |
| 14 #include "components/proximity_auth/wire_message.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 namespace proximity_auth { | |
| 18 namespace { | |
| 19 | |
| 20 class TestBluetoothThrottler : public BluetoothThrottlerImpl { | |
| 21 public: | |
| 22 explicit TestBluetoothThrottler(std::unique_ptr<base::TickClock> clock) | |
| 23 : BluetoothThrottlerImpl(std::move(clock)) {} | |
| 24 ~TestBluetoothThrottler() override {} | |
| 25 | |
| 26 // Increase visibility for testing. | |
| 27 using BluetoothThrottlerImpl::GetCooldownTimeDelta; | |
| 28 | |
| 29 private: | |
| 30 DISALLOW_COPY_AND_ASSIGN(TestBluetoothThrottler); | |
| 31 }; | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 class ProximityAuthBluetoothThrottlerImplTest : public testing::Test { | |
| 36 public: | |
| 37 ProximityAuthBluetoothThrottlerImplTest() | |
| 38 : clock_(new base::SimpleTestTickClock), | |
| 39 throttler_(base::WrapUnique(clock_)) { | |
| 40 // The throttler treats null times as special, so start with a non-null | |
| 41 // time. | |
| 42 clock_->Advance(base::TimeDelta::FromSeconds(1)); | |
| 43 } | |
| 44 | |
| 45 void PerformConnectionStateTransition(Connection::Status old_status, | |
| 46 Connection::Status new_status) { | |
| 47 FakeConnection connection((cryptauth::RemoteDevice())); | |
| 48 throttler_.OnConnection(&connection); | |
| 49 static_cast<ConnectionObserver*>(&throttler_) | |
| 50 ->OnConnectionStatusChanged(&connection, old_status, new_status); | |
| 51 } | |
| 52 | |
| 53 protected: | |
| 54 // The clock is owned by the |throttler_|. | |
| 55 base::SimpleTestTickClock* clock_; | |
| 56 TestBluetoothThrottler throttler_; | |
| 57 }; | |
| 58 | |
| 59 TEST_F(ProximityAuthBluetoothThrottlerImplTest, | |
| 60 GetDelay_FirstConnectionIsNotThrottled) { | |
| 61 EXPECT_EQ(base::TimeDelta(), throttler_.GetDelay()); | |
| 62 } | |
| 63 | |
| 64 TEST_F(ProximityAuthBluetoothThrottlerImplTest, | |
| 65 GetDelay_ConnectionAfterDisconnectIsThrottled) { | |
| 66 // Simulate a connection followed by a disconnection. | |
| 67 PerformConnectionStateTransition(Connection::CONNECTED, | |
| 68 Connection::DISCONNECTED); | |
| 69 EXPECT_GT(throttler_.GetDelay(), base::TimeDelta()); | |
| 70 } | |
| 71 | |
| 72 TEST_F(ProximityAuthBluetoothThrottlerImplTest, | |
| 73 GetDelay_ConnectionAfterIsProgressDisconnectIsThrottled) { | |
| 74 // Simulate an attempt to connect (in progress connection) followed by a | |
| 75 // disconnection. | |
| 76 PerformConnectionStateTransition(Connection::IN_PROGRESS, | |
| 77 Connection::DISCONNECTED); | |
| 78 EXPECT_GT(throttler_.GetDelay(), base::TimeDelta()); | |
| 79 } | |
| 80 | |
| 81 TEST_F(ProximityAuthBluetoothThrottlerImplTest, | |
| 82 GetDelay_DelayedConnectionAfterDisconnectIsNotThrottled) { | |
| 83 // Simulate a connection followed by a disconnection, then allow the cooldown | |
| 84 // period to elapse. | |
| 85 PerformConnectionStateTransition(Connection::CONNECTED, | |
| 86 Connection::DISCONNECTED); | |
| 87 clock_->Advance(throttler_.GetCooldownTimeDelta()); | |
| 88 EXPECT_EQ(base::TimeDelta(), throttler_.GetDelay()); | |
| 89 } | |
| 90 | |
| 91 TEST_F(ProximityAuthBluetoothThrottlerImplTest, | |
| 92 GetDelay_DelayedConnectionAfterInProgressDisconnectIsNotThrottled) { | |
| 93 // Simulate an attempt to connect (in progress connection) followed by a | |
| 94 // disconnection, then allow the cooldown period to elapse. | |
| 95 PerformConnectionStateTransition(Connection::IN_PROGRESS, | |
| 96 Connection::DISCONNECTED); | |
| 97 clock_->Advance(throttler_.GetCooldownTimeDelta()); | |
| 98 EXPECT_EQ(base::TimeDelta(), throttler_.GetDelay()); | |
| 99 } | |
| 100 | |
| 101 } // namespace proximity_auth | |
| OLD | NEW |