OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/test/simple_test_tick_clock.h" |
| 10 #include "base/test/test_simple_task_runner.h" |
| 11 #include "components/proximity_auth/connection.h" |
| 12 #include "components/proximity_auth/connection_finder.h" |
| 13 #include "components/proximity_auth/remote_device.h" |
| 14 #include "components/proximity_auth/wire_message.h" |
| 15 #include "device/bluetooth/bluetooth_uuid.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace proximity_auth { |
| 19 namespace { |
| 20 |
| 21 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF"; |
| 22 |
| 23 class StubConnection : public Connection { |
| 24 public: |
| 25 StubConnection() : Connection(RemoteDevice()) {} |
| 26 virtual ~StubConnection() {} |
| 27 |
| 28 virtual void Connect() override {} |
| 29 virtual void Disconnect() override {} |
| 30 virtual void SendMessageImpl(scoped_ptr<WireMessage> message) override {} |
| 31 |
| 32 private: |
| 33 DISALLOW_COPY_AND_ASSIGN(StubConnection); |
| 34 }; |
| 35 |
| 36 class FakeConnectionFinder : public ConnectionFinder { |
| 37 public: |
| 38 FakeConnectionFinder() {} |
| 39 virtual ~FakeConnectionFinder() {} |
| 40 |
| 41 void Find(const ConnectionCallback& connection_callback) override { |
| 42 connection_callback.Run(make_scoped_ptr(new StubConnection)); |
| 43 } |
| 44 |
| 45 private: |
| 46 DISALLOW_COPY_AND_ASSIGN(FakeConnectionFinder); |
| 47 }; |
| 48 |
| 49 class TestBluetoothThrottler : public BluetoothThrottler { |
| 50 public: |
| 51 TestBluetoothThrottler() |
| 52 : BluetoothThrottler(RemoteDevice()), |
| 53 task_runner_(new base::TestSimpleTaskRunner) { |
| 54 // The throttler treats null times as special, so start with a non-null |
| 55 // time. |
| 56 clock_.Advance(base::TimeDelta::FromSeconds(1)); |
| 57 } |
| 58 virtual ~TestBluetoothThrottler() {} |
| 59 |
| 60 virtual scoped_ptr<ConnectionFinder> CreateConnectionFinder( |
| 61 const device::BluetoothUUID& uuid) override { |
| 62 return make_scoped_ptr(new FakeConnectionFinder()); |
| 63 } |
| 64 |
| 65 virtual scoped_refptr<base::TaskRunner> GetTaskRunner() const override { |
| 66 return task_runner_; |
| 67 } |
| 68 |
| 69 virtual base::TickClock* GetTickClock() override { return &clock_; } |
| 70 |
| 71 scoped_refptr<base::TestSimpleTaskRunner> task_runner() { |
| 72 return task_runner_; |
| 73 } |
| 74 |
| 75 base::SimpleTestTickClock* clock() { return &clock_; } |
| 76 |
| 77 using BluetoothThrottler::GetCooldownTimeDelta; |
| 78 |
| 79 private: |
| 80 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
| 81 base::SimpleTestTickClock clock_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(TestBluetoothThrottler); |
| 84 }; |
| 85 |
| 86 } // namespace |
| 87 |
| 88 class ProximityAuthBluetoothThrottlerTest : public testing::Test { |
| 89 public: |
| 90 ProximityAuthBluetoothThrottlerTest() |
| 91 : connection_callback_( |
| 92 base::Bind(&ProximityAuthBluetoothThrottlerTest::OnConnection, |
| 93 base::Unretained(this))) {} |
| 94 |
| 95 void OnConnection(scoped_ptr<Connection> connection) { |
| 96 last_connection_ = connection.Pass(); |
| 97 } |
| 98 |
| 99 protected: |
| 100 // Callback for capturing the last connected connection. |
| 101 ConnectionFinder::ConnectionCallback connection_callback_; |
| 102 |
| 103 // The last seen connection. |
| 104 scoped_ptr<Connection> last_connection_; |
| 105 }; |
| 106 |
| 107 TEST_F(ProximityAuthBluetoothThrottlerTest, |
| 108 FindConnection_FirstConnectionIsNotThrottled) { |
| 109 TestBluetoothThrottler throttler; |
| 110 |
| 111 throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
| 112 EXPECT_TRUE(last_connection_); |
| 113 } |
| 114 |
| 115 TEST_F(ProximityAuthBluetoothThrottlerTest, |
| 116 FindConnection_ConnectionAfterDisconnectIsThrottled) { |
| 117 TestBluetoothThrottler throttler; |
| 118 |
| 119 // Search for a connection. |
| 120 throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
| 121 EXPECT_TRUE(last_connection_); |
| 122 |
| 123 // Simulate that connection connecting, then disconnecting. |
| 124 static_cast<ConnectionObserver*>(&throttler) |
| 125 ->OnConnectionStatusChanged( |
| 126 *last_connection_, Connection::CONNECTED, Connection::DISCONNECTED); |
| 127 last_connection_.reset(); |
| 128 |
| 129 // Immediately issue a second search. The connection should be throttled. |
| 130 throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
| 131 EXPECT_FALSE(last_connection_); |
| 132 |
| 133 // The throttled task should eventually run. |
| 134 throttler.clock()->Advance(throttler.GetCooldownTimeDelta()); |
| 135 throttler.task_runner()->RunUntilIdle(); |
| 136 EXPECT_TRUE(last_connection_); |
| 137 } |
| 138 |
| 139 TEST_F(ProximityAuthBluetoothThrottlerTest, |
| 140 FindConnection_DelayedConnectionAfterDisconnectIsNotThrottled) { |
| 141 TestBluetoothThrottler throttler; |
| 142 |
| 143 // Search for a connection. |
| 144 throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
| 145 EXPECT_TRUE(last_connection_); |
| 146 |
| 147 // Simulate that connection connecting, then disconnecting. |
| 148 static_cast<ConnectionObserver*>(&throttler) |
| 149 ->OnConnectionStatusChanged( |
| 150 *last_connection_, Connection::CONNECTED, Connection::DISCONNECTED); |
| 151 last_connection_.reset(); |
| 152 |
| 153 // Allow the cooldown period to elapse. |
| 154 throttler.clock()->Advance(throttler.GetCooldownTimeDelta()); |
| 155 |
| 156 // Now issue a second search. The connection should not be throttled. |
| 157 throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
| 158 EXPECT_TRUE(last_connection_); |
| 159 } |
| 160 |
| 161 TEST_F(ProximityAuthBluetoothThrottlerTest, |
| 162 StopFindingConnections_CancelsThrottledSearches) { |
| 163 TestBluetoothThrottler throttler; |
| 164 |
| 165 // Search for a connection. |
| 166 throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
| 167 EXPECT_TRUE(last_connection_); |
| 168 |
| 169 // Simulate that connection connecting, then disconnecting. |
| 170 static_cast<ConnectionObserver*>(&throttler) |
| 171 ->OnConnectionStatusChanged( |
| 172 *last_connection_, Connection::CONNECTED, Connection::DISCONNECTED); |
| 173 last_connection_.reset(); |
| 174 |
| 175 // Immediately issue a second search. The connection should be throttled. |
| 176 throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
| 177 EXPECT_FALSE(last_connection_); |
| 178 |
| 179 // While the connection is throttled, stop finding connections. |
| 180 throttler.StopFindingConnections(); |
| 181 |
| 182 // The throttled task should have been canceled. |
| 183 throttler.clock()->Advance(throttler.GetCooldownTimeDelta()); |
| 184 throttler.task_runner()->RunUntilIdle(); |
| 185 EXPECT_FALSE(last_connection_); |
| 186 } |
| 187 |
| 188 } // namespace proximity_auth |
OLD | NEW |