mandoline: Reland "Sandbox mojo:browser and mojo:omnibox."
[chromium-blink-merge.git] / components / proximity_auth / bluetooth_throttler_impl_unittest.cc
blob8b439419f5a1b342a6fe260f9f36c88b5fd13701
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 {
13 namespace {
15 class StubConnection : public Connection {
16 public:
17 StubConnection() : Connection(RemoteDevice()) {}
18 ~StubConnection() override {}
20 void Connect() override {}
21 void Disconnect() override {}
22 void SendMessageImpl(scoped_ptr<WireMessage> message) override {}
24 private:
25 DISALLOW_COPY_AND_ASSIGN(StubConnection);
28 class TestBluetoothThrottler : public BluetoothThrottlerImpl {
29 public:
30 explicit TestBluetoothThrottler(scoped_ptr<base::TickClock> clock)
31 : BluetoothThrottlerImpl(clock.Pass()) {}
32 ~TestBluetoothThrottler() override {}
34 // Increase visibility for testing.
35 using BluetoothThrottlerImpl::GetCooldownTimeDelta;
37 private:
38 DISALLOW_COPY_AND_ASSIGN(TestBluetoothThrottler);
41 } // namespace
43 class 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));
53 void PerformConnectionStateTransition(Connection::Status old_status,
54 Connection::Status new_status) {
55 StubConnection connection;
56 throttler_.OnConnection(&connection);
57 static_cast<ConnectionObserver*>(&throttler_)
58 ->OnConnectionStatusChanged(&connection, old_status, new_status);
61 protected:
62 // The clock is owned by the |throttler_|.
63 base::SimpleTestTickClock* clock_;
64 TestBluetoothThrottler throttler_;
67 TEST_F(ProximityAuthBluetoothThrottlerImplTest,
68 GetDelay_FirstConnectionIsNotThrottled) {
69 EXPECT_EQ(base::TimeDelta(), throttler_.GetDelay());
72 TEST_F(ProximityAuthBluetoothThrottlerImplTest,
73 GetDelay_ConnectionAfterDisconnectIsThrottled) {
74 // Simulate a connection followed by a disconnection.
75 PerformConnectionStateTransition(Connection::CONNECTED,
76 Connection::DISCONNECTED);
77 EXPECT_GT(throttler_.GetDelay(), base::TimeDelta());
80 TEST_F(ProximityAuthBluetoothThrottlerImplTest,
81 GetDelay_ConnectionAfterIsProgressDisconnectIsThrottled) {
82 // Simulate an attempt to connect (in progress connection) followed by a
83 // disconnection.
84 PerformConnectionStateTransition(Connection::IN_PROGRESS,
85 Connection::DISCONNECTED);
86 EXPECT_GT(throttler_.GetDelay(), base::TimeDelta());
89 TEST_F(ProximityAuthBluetoothThrottlerImplTest,
90 GetDelay_DelayedConnectionAfterDisconnectIsNotThrottled) {
91 // Simulate a connection followed by a disconnection, then allow the cooldown
92 // period to elapse.
93 PerformConnectionStateTransition(Connection::CONNECTED,
94 Connection::DISCONNECTED);
95 clock_->Advance(throttler_.GetCooldownTimeDelta());
96 EXPECT_EQ(base::TimeDelta(), throttler_.GetDelay());
99 TEST_F(ProximityAuthBluetoothThrottlerImplTest,
100 GetDelay_DelayedConnectionAfterInProgressDisconnectIsNotThrottled) {
101 // Simulate an attempt to connect (in progress connection) followed by a
102 // disconnection, then allow the cooldown period to elapse.
103 PerformConnectionStateTransition(Connection::IN_PROGRESS,
104 Connection::DISCONNECTED);
105 clock_->Advance(throttler_.GetCooldownTimeDelta());
106 EXPECT_EQ(base::TimeDelta(), throttler_.GetDelay());
109 } // namespace proximity_auth