base: Use scoped_ptr for ownership of pointers in unittests.
[chromium-blink-merge.git] / components / proximity_auth / connection_unittest.cc
blob1e298cf6a9cc4504332dfaba8b04754e0a9371f3
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.
5 #include "components/proximity_auth/connection.h"
7 #include "components/proximity_auth/connection_observer.h"
8 #include "components/proximity_auth/remote_device.h"
9 #include "components/proximity_auth/wire_message.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 using testing::_;
14 using testing::DoAll;
15 using testing::NiceMock;
16 using testing::Return;
17 using testing::SetArgPointee;
18 using testing::StrictMock;
20 namespace proximity_auth {
21 namespace {
23 class MockConnection : public Connection {
24 public:
25 MockConnection() : Connection(RemoteDevice()) {}
26 ~MockConnection() {}
28 MOCK_METHOD1(SetPaused, void(bool paused));
29 MOCK_METHOD0(Connect, void());
30 MOCK_METHOD0(Disconnect, void());
31 MOCK_METHOD0(CancelConnectionAttempt, void());
32 MOCK_METHOD1(SendMessageImplProxy, void(WireMessage* message));
33 MOCK_METHOD1(DeserializeWireMessageProxy,
34 WireMessage*(bool* is_incomplete_message));
36 // Gmock only supports copyable types, so create simple wrapper methods for
37 // ease of mocking.
38 void SendMessageImpl(scoped_ptr<WireMessage> message) override {
39 SendMessageImplProxy(message.get());
42 scoped_ptr<WireMessage> DeserializeWireMessage(
43 bool* is_incomplete_message) override {
44 return make_scoped_ptr(DeserializeWireMessageProxy(is_incomplete_message));
47 using Connection::status;
48 using Connection::SetStatus;
49 using Connection::OnDidSendMessage;
50 using Connection::OnBytesReceived;
52 private:
53 DISALLOW_COPY_AND_ASSIGN(MockConnection);
56 class MockConnectionObserver : public ConnectionObserver {
57 public:
58 MockConnectionObserver() {}
59 virtual ~MockConnectionObserver() {}
61 MOCK_METHOD3(OnConnectionStatusChanged,
62 void(Connection* connection,
63 Connection::Status old_status,
64 Connection::Status new_status));
65 MOCK_METHOD2(OnMessageReceived,
66 void(const Connection& connection, const WireMessage& message));
67 MOCK_METHOD3(OnSendCompleted,
68 void(const Connection& connection,
69 const WireMessage& message,
70 bool success));
72 private:
73 DISALLOW_COPY_AND_ASSIGN(MockConnectionObserver);
76 // Unlike WireMessage, offers a public constructor.
77 class TestWireMessage : public WireMessage {
78 public:
79 TestWireMessage() : WireMessage(std::string(), std::string()) {}
80 ~TestWireMessage() override {}
82 private:
83 DISALLOW_COPY_AND_ASSIGN(TestWireMessage);
86 } // namespace
88 TEST(ProximityAuthConnectionTest, IsConnected) {
89 StrictMock<MockConnection> connection;
90 EXPECT_FALSE(connection.IsConnected());
92 connection.SetStatus(Connection::CONNECTED);
93 EXPECT_TRUE(connection.IsConnected());
95 connection.SetStatus(Connection::DISCONNECTED);
96 EXPECT_FALSE(connection.IsConnected());
98 connection.SetStatus(Connection::IN_PROGRESS);
99 EXPECT_FALSE(connection.IsConnected());
102 TEST(ProximityAuthConnectionTest, SendMessage_FailsWhenNotConnected) {
103 StrictMock<MockConnection> connection;
104 connection.SetStatus(Connection::IN_PROGRESS);
106 EXPECT_CALL(connection, SendMessageImplProxy(_)).Times(0);
107 connection.SendMessage(scoped_ptr<WireMessage>());
110 TEST(ProximityAuthConnectionTest,
111 SendMessage_FailsWhenAnotherMessageSendIsInProgress) {
112 NiceMock<MockConnection> connection;
113 connection.SetStatus(Connection::CONNECTED);
114 connection.SendMessage(scoped_ptr<WireMessage>());
116 EXPECT_CALL(connection, SendMessageImplProxy(_)).Times(0);
117 connection.SendMessage(scoped_ptr<WireMessage>());
120 TEST(ProximityAuthConnectionTest, SendMessage_SucceedsWhenConnected) {
121 StrictMock<MockConnection> connection;
122 connection.SetStatus(Connection::CONNECTED);
124 EXPECT_CALL(connection, SendMessageImplProxy(_));
125 connection.SendMessage(scoped_ptr<WireMessage>());
128 TEST(ProximityAuthConnectionTest,
129 SendMessage_SucceedsAfterPreviousMessageSendCompletes) {
130 NiceMock<MockConnection> connection;
131 connection.SetStatus(Connection::CONNECTED);
132 connection.SendMessage(scoped_ptr<WireMessage>());
133 connection.OnDidSendMessage(TestWireMessage(), true /* success */);
135 EXPECT_CALL(connection, SendMessageImplProxy(_));
136 connection.SendMessage(scoped_ptr<WireMessage>());
139 TEST(ProximityAuthConnectionTest, SetStatus_NotifiesObserversOfStatusChange) {
140 StrictMock<MockConnection> connection;
141 EXPECT_EQ(Connection::DISCONNECTED, connection.status());
143 StrictMock<MockConnectionObserver> observer;
144 connection.AddObserver(&observer);
146 EXPECT_CALL(observer,
147 OnConnectionStatusChanged(&connection, Connection::DISCONNECTED,
148 Connection::CONNECTED));
149 connection.SetStatus(Connection::CONNECTED);
152 TEST(ProximityAuthConnectionTest,
153 SetStatus_DoesntNotifyObserversIfStatusUnchanged) {
154 StrictMock<MockConnection> connection;
155 EXPECT_EQ(Connection::DISCONNECTED, connection.status());
157 StrictMock<MockConnectionObserver> observer;
158 connection.AddObserver(&observer);
160 EXPECT_CALL(observer, OnConnectionStatusChanged(_, _, _)).Times(0);
161 connection.SetStatus(Connection::DISCONNECTED);
164 TEST(ProximityAuthConnectionTest,
165 OnDidSendMessage_NotifiesObserversIfMessageSendInProgress) {
166 NiceMock<MockConnection> connection;
167 connection.SetStatus(Connection::CONNECTED);
168 connection.SendMessage(scoped_ptr<WireMessage>());
170 StrictMock<MockConnectionObserver> observer;
171 connection.AddObserver(&observer);
173 EXPECT_CALL(observer, OnSendCompleted(Ref(connection), _, true));
174 connection.OnDidSendMessage(TestWireMessage(), true /* success */);
177 TEST(ProximityAuthConnectionTest,
178 OnDidSendMessage_DoesntNotifyObserversIfNoMessageSendInProgress) {
179 NiceMock<MockConnection> connection;
180 connection.SetStatus(Connection::CONNECTED);
182 StrictMock<MockConnectionObserver> observer;
183 connection.AddObserver(&observer);
185 EXPECT_CALL(observer, OnSendCompleted(_, _, _)).Times(0);
186 connection.OnDidSendMessage(TestWireMessage(), true /* success */);
189 TEST(ProximityAuthConnectionTest,
190 OnBytesReceived_NotifiesObserversOnValidMessage) {
191 NiceMock<MockConnection> connection;
192 connection.SetStatus(Connection::CONNECTED);
194 StrictMock<MockConnectionObserver> observer;
195 connection.AddObserver(&observer);
197 ON_CALL(connection, DeserializeWireMessageProxy(_))
198 .WillByDefault(
199 DoAll(SetArgPointee<0>(false), Return(new TestWireMessage)));
200 EXPECT_CALL(observer, OnMessageReceived(Ref(connection), _));
201 connection.OnBytesReceived(std::string());
204 TEST(ProximityAuthConnectionTest,
205 OnBytesReceived_DoesntNotifyObserversIfNotConnected) {
206 StrictMock<MockConnection> connection;
207 connection.SetStatus(Connection::IN_PROGRESS);
209 StrictMock<MockConnectionObserver> observer;
210 connection.AddObserver(&observer);
212 EXPECT_CALL(observer, OnMessageReceived(_, _)).Times(0);
213 connection.OnBytesReceived(std::string());
216 TEST(ProximityAuthConnectionTest,
217 OnBytesReceived_DoesntNotifyObserversIfMessageIsIncomplete) {
218 NiceMock<MockConnection> connection;
219 connection.SetStatus(Connection::CONNECTED);
221 StrictMock<MockConnectionObserver> observer;
222 connection.AddObserver(&observer);
224 ON_CALL(connection, DeserializeWireMessageProxy(_))
225 .WillByDefault(DoAll(SetArgPointee<0>(true),
226 Return(static_cast<WireMessage*>(NULL))));
227 EXPECT_CALL(observer, OnMessageReceived(_, _)).Times(0);
228 connection.OnBytesReceived(std::string());
231 TEST(ProximityAuthConnectionTest,
232 OnBytesReceived_DoesntNotifyObserversIfMessageIsInvalid) {
233 NiceMock<MockConnection> connection;
234 connection.SetStatus(Connection::CONNECTED);
236 StrictMock<MockConnectionObserver> observer;
237 connection.AddObserver(&observer);
239 ON_CALL(connection, DeserializeWireMessageProxy(_))
240 .WillByDefault(DoAll(SetArgPointee<0>(false),
241 Return(static_cast<WireMessage*>(NULL))));
242 EXPECT_CALL(observer, OnMessageReceived(_, _)).Times(0);
243 connection.OnBytesReceived(std::string());
246 } // namespace proximity_auth