Index: components/proximity_auth/bluetooth_throttler_unittest.cc |
diff --git a/components/proximity_auth/bluetooth_throttler_unittest.cc b/components/proximity_auth/bluetooth_throttler_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2518ca67ab49da49fd971ad0216e40c11bcfb4ba |
--- /dev/null |
+++ b/components/proximity_auth/bluetooth_throttler_unittest.cc |
@@ -0,0 +1,188 @@ |
+// Copyright 2014 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.h" |
+ |
+#include "base/bind.h" |
+#include "base/macros.h" |
+#include "base/test/simple_test_tick_clock.h" |
+#include "base/test/test_simple_task_runner.h" |
+#include "components/proximity_auth/connection.h" |
+#include "components/proximity_auth/connection_finder.h" |
+#include "components/proximity_auth/remote_device.h" |
+#include "components/proximity_auth/wire_message.h" |
+#include "device/bluetooth/bluetooth_uuid.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace proximity_auth { |
+namespace { |
+ |
+const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF"; |
+ |
+class StubConnection : public Connection { |
+ public: |
+ StubConnection() : Connection(RemoteDevice()) {} |
+ virtual ~StubConnection() {} |
+ |
+ virtual void Connect() override {} |
+ virtual void Disconnect() override {} |
+ virtual void SendMessageImpl(scoped_ptr<WireMessage> message) override {} |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(StubConnection); |
+}; |
+ |
+class FakeConnectionFinder : public ConnectionFinder { |
+ public: |
+ FakeConnectionFinder() {} |
+ virtual ~FakeConnectionFinder() {} |
+ |
+ void Find(const ConnectionCallback& connection_callback) override { |
+ connection_callback.Run(make_scoped_ptr(new StubConnection)); |
+ } |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(FakeConnectionFinder); |
+}; |
+ |
+class TestBluetoothThrottler : public BluetoothThrottler { |
+ public: |
+ TestBluetoothThrottler() |
+ : BluetoothThrottler(RemoteDevice()), |
+ task_runner_(new base::TestSimpleTaskRunner) { |
+ // The throttler treats null times as special, so start with a non-null |
+ // time. |
+ clock_.Advance(base::TimeDelta::FromSeconds(1)); |
+ } |
+ virtual ~TestBluetoothThrottler() {} |
+ |
+ virtual scoped_ptr<ConnectionFinder> CreateConnectionFinder( |
+ const device::BluetoothUUID& uuid) override { |
+ return make_scoped_ptr(new FakeConnectionFinder()); |
+ } |
+ |
+ virtual scoped_refptr<base::TaskRunner> GetTaskRunner() const override { |
+ return task_runner_; |
+ } |
+ |
+ virtual base::TickClock* GetTickClock() override { return &clock_; } |
+ |
+ scoped_refptr<base::TestSimpleTaskRunner> task_runner() { |
+ return task_runner_; |
+ } |
+ |
+ base::SimpleTestTickClock* clock() { return &clock_; } |
+ |
+ using BluetoothThrottler::GetCooldownTimeDelta; |
+ |
+ private: |
+ scoped_refptr<base::TestSimpleTaskRunner> task_runner_; |
+ base::SimpleTestTickClock clock_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(TestBluetoothThrottler); |
+}; |
+ |
+} // namespace |
+ |
+class ProximityAuthBluetoothThrottlerTest : public testing::Test { |
+ public: |
+ ProximityAuthBluetoothThrottlerTest() |
+ : connection_callback_( |
+ base::Bind(&ProximityAuthBluetoothThrottlerTest::OnConnection, |
+ base::Unretained(this))) {} |
+ |
+ void OnConnection(scoped_ptr<Connection> connection) { |
+ last_connection_ = connection.Pass(); |
+ } |
+ |
+ protected: |
+ // Callback for capturing the last connected connection. |
+ ConnectionFinder::ConnectionCallback connection_callback_; |
+ |
+ // The last seen connection. |
+ scoped_ptr<Connection> last_connection_; |
+}; |
+ |
+TEST_F(ProximityAuthBluetoothThrottlerTest, |
+ FindConnection_FirstConnectionIsNotThrottled) { |
+ TestBluetoothThrottler throttler; |
+ |
+ throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
+ EXPECT_TRUE(last_connection_); |
+} |
+ |
+TEST_F(ProximityAuthBluetoothThrottlerTest, |
+ FindConnection_ConnectionAfterDisconnectIsThrottled) { |
+ TestBluetoothThrottler throttler; |
+ |
+ // Search for a connection. |
+ throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
+ EXPECT_TRUE(last_connection_); |
+ |
+ // Simulate that connection connecting, then disconnecting. |
+ static_cast<ConnectionObserver*>(&throttler) |
+ ->OnConnectionStatusChanged( |
+ *last_connection_, Connection::CONNECTED, Connection::DISCONNECTED); |
+ last_connection_.reset(); |
+ |
+ // Immediately issue a second search. The connection should be throttled. |
+ throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
+ EXPECT_FALSE(last_connection_); |
+ |
+ // The throttled task should eventually run. |
+ throttler.clock()->Advance(throttler.GetCooldownTimeDelta()); |
+ throttler.task_runner()->RunUntilIdle(); |
+ EXPECT_TRUE(last_connection_); |
+} |
+ |
+TEST_F(ProximityAuthBluetoothThrottlerTest, |
+ FindConnection_DelayedConnectionAfterDisconnectIsNotThrottled) { |
+ TestBluetoothThrottler throttler; |
+ |
+ // Search for a connection. |
+ throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
+ EXPECT_TRUE(last_connection_); |
+ |
+ // Simulate that connection connecting, then disconnecting. |
+ static_cast<ConnectionObserver*>(&throttler) |
+ ->OnConnectionStatusChanged( |
+ *last_connection_, Connection::CONNECTED, Connection::DISCONNECTED); |
+ last_connection_.reset(); |
+ |
+ // Allow the cooldown period to elapse. |
+ throttler.clock()->Advance(throttler.GetCooldownTimeDelta()); |
+ |
+ // Now issue a second search. The connection should not be throttled. |
+ throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
+ EXPECT_TRUE(last_connection_); |
+} |
+ |
+TEST_F(ProximityAuthBluetoothThrottlerTest, |
+ StopFindingConnections_CancelsThrottledSearches) { |
+ TestBluetoothThrottler throttler; |
+ |
+ // Search for a connection. |
+ throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
+ EXPECT_TRUE(last_connection_); |
+ |
+ // Simulate that connection connecting, then disconnecting. |
+ static_cast<ConnectionObserver*>(&throttler) |
+ ->OnConnectionStatusChanged( |
+ *last_connection_, Connection::CONNECTED, Connection::DISCONNECTED); |
+ last_connection_.reset(); |
+ |
+ // Immediately issue a second search. The connection should be throttled. |
+ throttler.FindConnection(device::BluetoothUUID(kUuid), connection_callback_); |
+ EXPECT_FALSE(last_connection_); |
+ |
+ // While the connection is throttled, stop finding connections. |
+ throttler.StopFindingConnections(); |
+ |
+ // The throttled task should have been canceled. |
+ throttler.clock()->Advance(throttler.GetCooldownTimeDelta()); |
+ throttler.task_runner()->RunUntilIdle(); |
+ EXPECT_FALSE(last_connection_); |
+} |
+ |
+} // namespace proximity_auth |