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.
5 #include "components/proximity_auth/bluetooth_throttler_impl.h"
7 #include "base/test/simple_test_tick_clock.h"
8 #include "base/time/time.h"
9 #include "components/proximity_auth/wire_message.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace proximity_auth
{
15 class StubConnection
: public Connection
{
17 StubConnection() : Connection(RemoteDevice()) {}
18 ~StubConnection() override
{}
20 void Connect() override
{}
21 void Disconnect() override
{}
22 void SendMessageImpl(scoped_ptr
<WireMessage
> message
) override
{}
25 DISALLOW_COPY_AND_ASSIGN(StubConnection
);
28 class TestBluetoothThrottler
: public BluetoothThrottlerImpl
{
30 explicit TestBluetoothThrottler(scoped_ptr
<base::TickClock
> clock
)
31 : BluetoothThrottlerImpl(clock
.Pass()) {}
32 ~TestBluetoothThrottler() override
{}
34 // Increase visibility for testing.
35 using BluetoothThrottlerImpl::GetCooldownTimeDelta
;
38 DISALLOW_COPY_AND_ASSIGN(TestBluetoothThrottler
);
43 class ProximityAuthBluetoothThrottlerImplTest
: public testing::Test
{
45 ProximityAuthBluetoothThrottlerImplTest()
46 : clock_(new base::SimpleTestTickClock
),
47 throttler_(make_scoped_ptr(clock_
)) {
48 // The throttler treats null times as special, so start with a non-null
50 clock_
->Advance(base::TimeDelta::FromSeconds(1));
54 // The clock is owned by the |throttler_|.
55 base::SimpleTestTickClock
* clock_
;
56 TestBluetoothThrottler throttler_
;
59 TEST_F(ProximityAuthBluetoothThrottlerImplTest
,
60 GetDelay_FirstConnectionIsNotThrottled
) {
61 EXPECT_EQ(base::TimeDelta(), throttler_
.GetDelay());
64 TEST_F(ProximityAuthBluetoothThrottlerImplTest
,
65 GetDelay_ConnectionAfterDisconnectIsThrottled
) {
66 // Simulate a connection followed by a disconnection.
67 StubConnection connection
;
68 throttler_
.OnConnection(&connection
);
69 static_cast<ConnectionObserver
*>(&throttler_
)
70 ->OnConnectionStatusChanged(&connection
, Connection::CONNECTED
,
71 Connection::DISCONNECTED
);
72 EXPECT_GT(throttler_
.GetDelay(), base::TimeDelta());
75 TEST_F(ProximityAuthBluetoothThrottlerImplTest
,
76 GetDelay_DelayedConnectionAfterDisconnectIsNotThrottled
) {
77 // Simulate a connection followed by a disconnection, then allow the cooldown
79 StubConnection connection
;
80 throttler_
.OnConnection(&connection
);
81 static_cast<ConnectionObserver
*>(&throttler_
)
82 ->OnConnectionStatusChanged(&connection
, Connection::CONNECTED
,
83 Connection::DISCONNECTED
);
84 clock_
->Advance(throttler_
.GetCooldownTimeDelta());
85 EXPECT_EQ(base::TimeDelta(), throttler_
.GetDelay());
88 } // namespace proximity_auth