Cleanup: Pass std::string as const reference from pdf/
[chromium-blink-merge.git] / components / proximity_auth / throttled_bluetooth_connection_finder_unittest.cc
blobcf2ca7bc05ce7e55eebe43333f7b829bfa232a28
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/throttled_bluetooth_connection_finder.h"
7 #include "base/bind.h"
8 #include "base/test/test_simple_task_runner.h"
9 #include "base/time/time.h"
10 #include "components/proximity_auth/bluetooth_connection_finder.h"
11 #include "components/proximity_auth/bluetooth_throttler.h"
12 #include "components/proximity_auth/connection.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/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
19 using testing::NiceMock;
20 using testing::Return;
21 using testing::_;
23 namespace proximity_auth {
24 namespace {
26 const int kPollingIntervalSeconds = 7;
27 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF";
29 // A callback that stores a found |connection| into |out|.
30 void SaveConnection(scoped_ptr<Connection>* out,
31 scoped_ptr<Connection> connection) {
32 *out = connection.Pass();
35 class StubConnection : public Connection {
36 public:
37 StubConnection() : Connection(RemoteDevice()) {}
38 ~StubConnection() override {}
40 void Connect() override {}
41 void Disconnect() override {}
42 void SendMessageImpl(scoped_ptr<WireMessage> message) override {}
44 private:
45 DISALLOW_COPY_AND_ASSIGN(StubConnection);
48 class MockBluetoothThrottler : public BluetoothThrottler {
49 public:
50 MockBluetoothThrottler() {}
51 ~MockBluetoothThrottler() override {}
53 MOCK_CONST_METHOD0(GetDelay, base::TimeDelta());
54 MOCK_METHOD1(OnConnection, void(Connection* connection));
56 private:
57 DISALLOW_COPY_AND_ASSIGN(MockBluetoothThrottler);
60 class FakeBluetoothConnectionFinder : public BluetoothConnectionFinder {
61 public:
62 FakeBluetoothConnectionFinder()
63 : BluetoothConnectionFinder(
64 RemoteDevice(),
65 device::BluetoothUUID(kUuid),
66 base::TimeDelta::FromSeconds(kPollingIntervalSeconds)) {}
67 ~FakeBluetoothConnectionFinder() override {}
69 void Find(const ConnectionCallback& connection_callback) override {
70 connection_callback.Run(make_scoped_ptr(new StubConnection));
73 private:
74 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothConnectionFinder);
77 } // namespace
79 class ProximityAuthThrottledBluetoothConnectionFinderTest
80 : public testing::Test {
81 public:
82 ProximityAuthThrottledBluetoothConnectionFinderTest()
83 : task_runner_(new base::TestSimpleTaskRunner) {}
85 protected:
86 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
87 NiceMock<MockBluetoothThrottler> throttler_;
90 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest,
91 Find_ExecutesImmediatelyWhenUnthrottled) {
92 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta()));
94 ThrottledBluetoothConnectionFinder connection_finder(
95 make_scoped_ptr(new FakeBluetoothConnectionFinder), task_runner_,
96 &throttler_);
97 scoped_ptr<Connection> connection;
98 connection_finder.Find(base::Bind(&SaveConnection, &connection));
99 EXPECT_TRUE(connection);
102 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest,
103 Find_ExecutesAfterADelayWhenThrottled) {
104 ON_CALL(throttler_, GetDelay())
105 .WillByDefault(Return(base::TimeDelta::FromSeconds(1)));
107 ThrottledBluetoothConnectionFinder connection_finder(
108 make_scoped_ptr(new FakeBluetoothConnectionFinder), task_runner_,
109 &throttler_);
110 scoped_ptr<Connection> connection;
111 connection_finder.Find(base::Bind(&SaveConnection, &connection));
112 EXPECT_FALSE(connection);
114 // The connection should be found once the throttling period has elapsed.
115 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta()));
116 task_runner_->RunUntilIdle();
117 EXPECT_TRUE(connection);
120 TEST_F(ProximityAuthThrottledBluetoothConnectionFinderTest,
121 OnConnection_ForwardsNotificationToThrottler) {
122 ON_CALL(throttler_, GetDelay()).WillByDefault(Return(base::TimeDelta()));
124 ThrottledBluetoothConnectionFinder connection_finder(
125 make_scoped_ptr(new FakeBluetoothConnectionFinder), task_runner_,
126 &throttler_);
127 scoped_ptr<Connection> connection;
128 EXPECT_CALL(throttler_, OnConnection(_));
129 connection_finder.Find(base::Bind(&SaveConnection, &connection));
130 EXPECT_TRUE(connection);
133 } // namespace proximity_auth