Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Unified Diff: components/proximity_auth/throttled_bluetooth_connection_finder_unittest.cc

Issue 663693002: [Easy Unlock] Port the BluetoothThrottler class to native code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: cleanup Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/proximity_auth/throttled_bluetooth_connection_finder_unittest.cc
diff --git a/components/proximity_auth/throttled_bluetooth_connection_finder_unittest.cc b/components/proximity_auth/throttled_bluetooth_connection_finder_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..39aec4e03bb8ad38c52aa722a985b85e1ed3aa00
--- /dev/null
+++ b/components/proximity_auth/throttled_bluetooth_connection_finder_unittest.cc
@@ -0,0 +1,136 @@
+// 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/throttled_bluetooth_connection_finder.h"
+
+#include "base/bind.h"
+#include "base/test/test_simple_task_runner.h"
+#include "base/time/time.h"
+#include "components/proximity_auth/bluetooth_connection_finder.h"
+#include "components/proximity_auth/bluetooth_throttler.h"
+#include "components/proximity_auth/connection.h"
+#include "components/proximity_auth/remote_device.h"
+#include "device/bluetooth/bluetooth_uuid.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using testing::NiceMock;
+using testing::Return;
+
+namespace proximity_auth {
+namespace {
+
+const int kPollingIntervalSeconds = 7;
+const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF";
+
+// A callback that stores a found |connection| into |out|.
+void SaveConnection(scoped_ptr<Connection>* out,
+ scoped_ptr<Connection> connection) {
+ *out = connection.Pass();
+}
+
+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 MockBluetoothThrottler : public BluetoothThrottler {
+ public:
+ MockBluetoothThrottler() {}
+ ~MockBluetoothThrottler() override {}
+
+ MOCK_CONST_METHOD0(GetDelay, base::TimeDelta());
+ MOCK_METHOD0(OnConnectionClosed, void());
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler);
+};
+
+class FakeBluetoothConnectionFinder : public BluetoothConnectionFinder {
+ public:
+ FakeBluetoothConnectionFinder()
+ : BluetoothConnectionFinder(
+ RemoteDevice(),
+ device::BluetoothUUID(kUuid),
+ base::TimeDelta::FromSeconds(kPollingIntervalSeconds)) {}
+ ~FakeBluetoothConnectionFinder() override {}
+
+ void Find(const ConnectionCallback& connection_callback) override {
+ connection_callback.Run(make_scoped_ptr(new StubConnection));
+ }
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(FakeBluetoothConnectionFinder);
+};
+
+} // namespace
+
+class ProximityAuthThrottledBluetoothConnectionFinderTest
+ : public testing::Test {
+ public:
+ ProximityAuthThrottledBluetoothConnectionFinderTest()
+ : task_runner_(new base::TestSimpleTaskRunner) {}
+
+ protected:
+ scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
+ NiceMock<MockBluetoothThrottler> throttler_;
+};
+
+TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest,
+ Find_ExecutesImmediatelyWhenUnthrottled) {
+ ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta()));
+
+ ThrottledBluetoothConnectionFinder connection_finder(
+ make_scoped_ptr(new FakeBluetoothConnectionFinder), task_runner_,
+ &throttler_);
+ scoped_ptr<Connection> connection;
+ connection_finder.Find(base::Bind(&SaveConnection, &connection));
+ EXPECT_TRUE(connection);
+}
+
+TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest,
+ Find_ExecutesAfterADelyaWhenThrottled) {
+ ON_CALL(throttler_, GetDelay())
+ .WillByDefault(Return(base::TimeDelta::FromSeconds(1)));
+
+ ThrottledBluetoothConnectionFinder connection_finder(
+ make_scoped_ptr(new FakeBluetoothConnectionFinder), task_runner_,
+ &throttler_);
+ scoped_ptr<Connection> connection;
+ connection_finder.Find(base::Bind(&SaveConnection, &connection));
+ EXPECT_FALSE(connection);
+
+ // The connection should be found once the throttling period has elapsed.
+ ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta()));
+ task_runner_->RunUntilIdle();
+ EXPECT_TRUE(connection);
+}
+
+TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest,
+ OnConnectionStatusChanged_NotifiesThrottlerWhenConnectionDisconnects) {
+ ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta()));
+
+ ThrottledBluetoothConnectionFinder connection_finder(
+ make_scoped_ptr(new FakeBluetoothConnectionFinder), task_runner_,
+ &throttler_);
+ scoped_ptr<Connection> connection;
+ connection_finder.Find(base::Bind(&SaveConnection, &connection));
+ ASSERT_TRUE(connection);
+
+ // Simulate the connection disconnecting.
+ EXPECT_CALL(throttler_, OnConnectionClosed());
+ static_cast<ConnectionObserver*>(&connection_finder)
+ ->OnConnectionStatusChanged(*connection, Connection::CONNECTED,
+ Connection::DISCONNECTED);
+}
+
+} // namespace proximity_auth

Powered by Google App Engine
This is Rietveld 408576698