Blink roll a6d61dbff0a4bce30fce5b29c2efb77a94ddc3ee:a11635f657e0f1ce75179fd7336a309a6...
[chromium-blink-merge.git] / components / proximity_auth / connection_unittest.cc
blob2f9fbdb78b56a97f924e5c5e306db706a5eab758
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/wire_message.h"
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 using testing::_;
13 using testing::NiceMock;
14 using testing::Return;
15 using testing::StrictMock;
17 namespace proximity_auth {
18 namespace {
20 class MockConnection : public Connection {
21 public:
22 MockConnection() : Connection(RemoteDevice()) {}
23 ~MockConnection() {}
25 MOCK_METHOD1(SetPaused, void(bool paused));
26 MOCK_METHOD0(Connect, void());
27 MOCK_METHOD0(Disconnect, void());
28 MOCK_METHOD0(CancelConnectionAttempt, void());
29 MOCK_METHOD1(SendMessageImplProxy, void(WireMessage* message));
30 MOCK_METHOD0(HasReceivedCompleteMessage, bool());
31 MOCK_METHOD0(DeserializeWireMessageProxy, WireMessage*());
33 // Gmock only supports copyable types, so create simple wrapper methods for
34 // ease of mocking.
35 virtual void SendMessageImpl(scoped_ptr<WireMessage> message) OVERRIDE {
36 SendMessageImplProxy(message.get());
39 virtual scoped_ptr<WireMessage> DeserializeWireMessage() OVERRIDE {
40 return make_scoped_ptr(DeserializeWireMessageProxy());
43 using Connection::status;
44 using Connection::SetStatus;
45 using Connection::OnDidSendMessage;
46 using Connection::OnBytesReceived;
48 private:
49 DISALLOW_COPY_AND_ASSIGN(MockConnection);
52 class MockConnectionObserver : public ConnectionObserver {
53 public:
54 MockConnectionObserver() {}
55 virtual ~MockConnectionObserver() {}
57 MOCK_METHOD3(OnConnectionStatusChanged,
58 void(const Connection& connection,
59 Connection::Status old_status,
60 Connection::Status new_status));
61 MOCK_METHOD2(OnMessageReceived,
62 void(const Connection& connection, const WireMessage& message));
63 MOCK_METHOD3(OnSendCompleted,
64 void(const Connection& connection,
65 const WireMessage& message,
66 bool success));
68 private:
69 DISALLOW_COPY_AND_ASSIGN(MockConnectionObserver);
72 // Unlike WireMessage, offers a public constructor.
73 class TestWireMessage : public WireMessage {
74 public:
75 TestWireMessage() {}
76 virtual ~TestWireMessage() {}
78 private:
79 DISALLOW_COPY_AND_ASSIGN(TestWireMessage);
82 } // namespace
84 TEST(ProximityAuthConnectionTest, IsConnected) {
85 StrictMock<MockConnection> connection;
86 EXPECT_FALSE(connection.IsConnected());
88 connection.SetStatus(Connection::CONNECTED);
89 EXPECT_TRUE(connection.IsConnected());
91 connection.SetStatus(Connection::DISCONNECTED);
92 EXPECT_FALSE(connection.IsConnected());
94 connection.SetStatus(Connection::IN_PROGRESS);
95 EXPECT_FALSE(connection.IsConnected());
98 TEST(ProximityAuthConnectionTest, SendMessage_FailsWhenNotConnected) {
99 StrictMock<MockConnection> connection;
100 connection.SetStatus(Connection::IN_PROGRESS);
102 EXPECT_CALL(connection, SendMessageImplProxy(_)).Times(0);
103 connection.SendMessage(scoped_ptr<WireMessage>());
106 TEST(ProximityAuthConnectionTest,
107 SendMessage_FailsWhenAnotherMessageSendIsInProgress) {
108 NiceMock<MockConnection> connection;
109 connection.SetStatus(Connection::CONNECTED);
110 connection.SendMessage(scoped_ptr<WireMessage>());
112 EXPECT_CALL(connection, SendMessageImplProxy(_)).Times(0);
113 connection.SendMessage(scoped_ptr<WireMessage>());
116 TEST(ProximityAuthConnectionTest, SendMessage_SucceedsWhenConnected) {
117 StrictMock<MockConnection> connection;
118 connection.SetStatus(Connection::CONNECTED);
120 EXPECT_CALL(connection, SendMessageImplProxy(_));
121 connection.SendMessage(scoped_ptr<WireMessage>());
124 TEST(ProximityAuthConnectionTest,
125 SendMessage_SucceedsAfterPreviousMessageSendCompletes) {
126 NiceMock<MockConnection> connection;
127 connection.SetStatus(Connection::CONNECTED);
128 connection.SendMessage(scoped_ptr<WireMessage>());
129 connection.OnDidSendMessage(TestWireMessage(), true /* success */);
131 EXPECT_CALL(connection, SendMessageImplProxy(_));
132 connection.SendMessage(scoped_ptr<WireMessage>());
135 TEST(ProximityAuthConnectionTest, SetStatus_NotifiesObserversOfStatusChange) {
136 StrictMock<MockConnection> connection;
137 EXPECT_EQ(Connection::DISCONNECTED, connection.status());
139 StrictMock<MockConnectionObserver> observer;
140 connection.AddObserver(&observer);
142 EXPECT_CALL(
143 observer,
144 OnConnectionStatusChanged(
145 Ref(connection), Connection::DISCONNECTED, Connection::CONNECTED));
146 connection.SetStatus(Connection::CONNECTED);
149 TEST(ProximityAuthConnectionTest,
150 SetStatus_DoesntNotifyObserversIfStatusUnchanged) {
151 StrictMock<MockConnection> connection;
152 EXPECT_EQ(Connection::DISCONNECTED, connection.status());
154 StrictMock<MockConnectionObserver> observer;
155 connection.AddObserver(&observer);
157 EXPECT_CALL(observer, OnConnectionStatusChanged(_, _, _)).Times(0);
158 connection.SetStatus(Connection::DISCONNECTED);
161 TEST(ProximityAuthConnectionTest,
162 OnDidSendMessage_NotifiesObserversIfMessageSendInProgress) {
163 NiceMock<MockConnection> connection;
164 connection.SetStatus(Connection::CONNECTED);
165 connection.SendMessage(scoped_ptr<WireMessage>());
167 StrictMock<MockConnectionObserver> observer;
168 connection.AddObserver(&observer);
170 EXPECT_CALL(observer, OnSendCompleted(Ref(connection), _, true));
171 connection.OnDidSendMessage(TestWireMessage(), true /* success */);
174 TEST(ProximityAuthConnectionTest,
175 OnDidSendMessage_DoesntNotifyObserversIfNoMessageSendInProgress) {
176 NiceMock<MockConnection> connection;
177 connection.SetStatus(Connection::CONNECTED);
179 StrictMock<MockConnectionObserver> observer;
180 connection.AddObserver(&observer);
182 EXPECT_CALL(observer, OnSendCompleted(_, _, _)).Times(0);
183 connection.OnDidSendMessage(TestWireMessage(), true /* success */);
186 TEST(ProximityAuthConnectionTest,
187 OnBytesReceived_NotifiesObserversOnValidMessage) {
188 NiceMock<MockConnection> connection;
189 connection.SetStatus(Connection::CONNECTED);
191 StrictMock<MockConnectionObserver> observer;
192 connection.AddObserver(&observer);
194 ON_CALL(connection, HasReceivedCompleteMessage()).WillByDefault(Return(true));
195 ON_CALL(connection, DeserializeWireMessageProxy())
196 .WillByDefault(Return(new TestWireMessage));
197 EXPECT_CALL(observer, OnMessageReceived(Ref(connection), _));
198 connection.OnBytesReceived(std::string());
201 TEST(ProximityAuthConnectionTest,
202 OnBytesReceived_DoesntNotifyObserversIfNotConnected) {
203 StrictMock<MockConnection> connection;
204 connection.SetStatus(Connection::IN_PROGRESS);
206 StrictMock<MockConnectionObserver> observer;
207 connection.AddObserver(&observer);
209 EXPECT_CALL(observer, OnMessageReceived(_, _)).Times(0);
210 connection.OnBytesReceived(std::string());
213 TEST(ProximityAuthConnectionTest,
214 OnBytesReceived_DoesntNotifyObserversIfMessageIsIncomplete) {
215 NiceMock<MockConnection> connection;
216 connection.SetStatus(Connection::CONNECTED);
218 StrictMock<MockConnectionObserver> observer;
219 connection.AddObserver(&observer);
221 ON_CALL(connection, HasReceivedCompleteMessage())
222 .WillByDefault(Return(false));
223 EXPECT_CALL(observer, OnMessageReceived(_, _)).Times(0);
224 connection.OnBytesReceived(std::string());
227 TEST(ProximityAuthConnectionTest,
228 OnBytesReceived_DoesntNotifyObserversIfMessageIsInvalid) {
229 NiceMock<MockConnection> connection;
230 connection.SetStatus(Connection::CONNECTED);
232 StrictMock<MockConnectionObserver> observer;
233 connection.AddObserver(&observer);
235 ON_CALL(connection, HasReceivedCompleteMessage()).WillByDefault(Return(true));
236 ON_CALL(connection, DeserializeWireMessageProxy())
237 .WillByDefault(Return(static_cast<WireMessage*>(NULL)));
238 EXPECT_CALL(observer, OnMessageReceived(_, _)).Times(0);
239 connection.OnBytesReceived(std::string());
242 } // namespace proximity_auth