blob: 1bca940fa858f5960786f6e35eaf568a23e2eb1c [file] [log] [blame]
ishermanfd7c5342015-04-24 00:43:531// 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 "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"
11
12namespace proximity_auth {
13namespace {
14
15class 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
28class TestBluetoothThrottler : public BluetoothThrottlerImpl {
29 public:
30 explicit TestBluetoothThrottler(scoped_ptr<base::TickClock> clock)
31 : BluetoothThrottlerImpl(clock.Pass()) {}
32 ~TestBluetoothThrottler() override {}
33
34 // Increase visibility for testing.
35 using BluetoothThrottlerImpl::GetCooldownTimeDelta;
36
37 private:
38 DISALLOW_COPY_AND_ASSIGN(TestBluetoothThrottler);
39};
40
41} // namespace
42
43class ProximityAuthBluetoothThrottlerImplTest : public testing::Test {
44 public:
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
49 // time.
50 clock_->Advance(base::TimeDelta::FromSeconds(1));
51 }
52
53 protected:
54 // The clock is owned by the |throttler_|.
55 base::SimpleTestTickClock* clock_;
56 TestBluetoothThrottler throttler_;
57};
58
59TEST_F(ProximityAuthBluetoothThrottlerImplTest,
60 GetDelay_FirstConnectionIsNotThrottled) {
61 EXPECT_EQ(base::TimeDelta(), throttler_.GetDelay());
62}
63
64TEST_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());
73}
74
75TEST_F(ProximityAuthBluetoothThrottlerImplTest,
76 GetDelay_DelayedConnectionAfterDisconnectIsNotThrottled) {
77 // Simulate a connection followed by a disconnection, then allow the cooldown
78 // period to elapse.
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());
86}
87
88} // namespace proximity_auth