1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/proximity_auth/bluetooth_throttler_impl.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/time/time.h"
#include "components/proximity_auth/wire_message.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace proximity_auth {
namespace {
class StubConnection : public Connection {
public:
StubConnection() : Connection(RemoteDevice()) {}
~StubConnection() override {}
void Connect() override {}
void Disconnect() override {}
void SendMessageImpl(scoped_ptr<WireMessage> message) override {}
private:
DISALLOW_COPY_AND_ASSIGN(StubConnection);
};
class TestBluetoothThrottler : public BluetoothThrottlerImpl {
public:
explicit TestBluetoothThrottler(scoped_ptr<base::TickClock> clock)
: BluetoothThrottlerImpl(clock.Pass()) {}
~TestBluetoothThrottler() override {}
// Increase visibility for testing.
using BluetoothThrottlerImpl::GetCooldownTimeDelta;
private:
DISALLOW_COPY_AND_ASSIGN(TestBluetoothThrottler);
};
} // namespace
class ProximityAuthBluetoothThrottlerImplTest : public testing::Test {
public:
ProximityAuthBluetoothThrottlerImplTest()
: clock_(new base::SimpleTestTickClock),
throttler_(make_scoped_ptr(clock_)) {
// The throttler treats null times as special, so start with a non-null
// time.
clock_->Advance(base::TimeDelta::FromSeconds(1));
}
void PerformConnectionStateTransition(Connection::Status old_status,
Connection::Status new_status) {
StubConnection connection;
throttler_.OnConnection(&connection);
static_cast<ConnectionObserver*>(&throttler_)
->OnConnectionStatusChanged(&connection, old_status, new_status);
}
protected:
// The clock is owned by the |throttler_|.
base::SimpleTestTickClock* clock_;
TestBluetoothThrottler throttler_;
};
TEST_F(ProximityAuthBluetoothThrottlerImplTest,
GetDelay_FirstConnectionIsNotThrottled) {
EXPECT_EQ(base::TimeDelta(), throttler_.GetDelay());
}
TEST_F(ProximityAuthBluetoothThrottlerImplTest,
GetDelay_ConnectionAfterDisconnectIsThrottled) {
// Simulate a connection followed by a disconnection.
PerformConnectionStateTransition(Connection::CONNECTED,
Connection::DISCONNECTED);
EXPECT_GT(throttler_.GetDelay(), base::TimeDelta());
}
TEST_F(ProximityAuthBluetoothThrottlerImplTest,
GetDelay_ConnectionAfterIsProgressDisconnectIsThrottled) {
// Simulate an attempt to connect (in progress connection) followed by a
// disconnection.
PerformConnectionStateTransition(Connection::IN_PROGRESS,
Connection::DISCONNECTED);
EXPECT_GT(throttler_.GetDelay(), base::TimeDelta());
}
TEST_F(ProximityAuthBluetoothThrottlerImplTest,
GetDelay_DelayedConnectionAfterDisconnectIsNotThrottled) {
// Simulate a connection followed by a disconnection, then allow the cooldown
// period to elapse.
PerformConnectionStateTransition(Connection::CONNECTED,
Connection::DISCONNECTED);
clock_->Advance(throttler_.GetCooldownTimeDelta());
EXPECT_EQ(base::TimeDelta(), throttler_.GetDelay());
}
TEST_F(ProximityAuthBluetoothThrottlerImplTest,
GetDelay_DelayedConnectionAfterInProgressDisconnectIsNotThrottled) {
// Simulate an attempt to connect (in progress connection) followed by a
// disconnection, then allow the cooldown period to elapse.
PerformConnectionStateTransition(Connection::IN_PROGRESS,
Connection::DISCONNECTED);
clock_->Advance(throttler_.GetCooldownTimeDelta());
EXPECT_EQ(base::TimeDelta(), throttler_.GetDelay());
}
} // namespace proximity_auth
|