Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / google_apis / gcm / engine / fake_connection_handler.cc
blob243c28068492a345ccab779dec14d2161891d128
1 // Copyright 2013 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 "google_apis/gcm/engine/fake_connection_handler.h"
7 #include "base/logging.h"
8 #include "google_apis/gcm/base/mcs_util.h"
9 #include "net/socket/stream_socket.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace gcm {
14 namespace {
16 // Build a basic login response.
17 scoped_ptr<google::protobuf::MessageLite> BuildLoginResponse(bool fail_login) {
18 scoped_ptr<mcs_proto::LoginResponse> login_response(
19 new mcs_proto::LoginResponse());
20 login_response->set_id("id");
21 if (fail_login)
22 login_response->mutable_error()->set_code(1);
23 return login_response.Pass();
26 } // namespace
28 FakeConnectionHandler::FakeConnectionHandler(
29 const ConnectionHandler::ProtoReceivedCallback& read_callback,
30 const ConnectionHandler::ProtoSentCallback& write_callback)
31 : read_callback_(read_callback),
32 write_callback_(write_callback),
33 fail_login_(false),
34 fail_send_(false),
35 initialized_(false),
36 had_error_(false) {
39 FakeConnectionHandler::~FakeConnectionHandler() {
42 void FakeConnectionHandler::Init(const mcs_proto::LoginRequest& login_request,
43 net::StreamSocket* socket) {
44 ASSERT_GE(expected_outgoing_messages_.size(), 1U);
45 EXPECT_EQ(expected_outgoing_messages_.front().SerializeAsString(),
46 login_request.SerializeAsString());
47 expected_outgoing_messages_.pop_front();
48 DVLOG(1) << "Received init call.";
49 read_callback_.Run(BuildLoginResponse(fail_login_));
50 initialized_ = !fail_login_;
53 void FakeConnectionHandler::Reset() {
54 initialized_ = false;
55 had_error_ = false;
58 bool FakeConnectionHandler::CanSendMessage() const {
59 return initialized_ && !had_error_;
62 void FakeConnectionHandler::SendMessage(
63 const google::protobuf::MessageLite& message) {
64 if (expected_outgoing_messages_.empty())
65 FAIL() << "Unexpected message sent.";
66 EXPECT_EQ(expected_outgoing_messages_.front().SerializeAsString(),
67 message.SerializeAsString());
68 expected_outgoing_messages_.pop_front();
69 DVLOG(1) << "Received message, "
70 << (fail_send_ ? " failing send." : "calling back.");
71 if (!fail_send_)
72 write_callback_.Run();
73 else
74 initialized_ = false; // Prevent future messages until reconnect.
77 void FakeConnectionHandler::ExpectOutgoingMessage(const MCSMessage& message) {
78 expected_outgoing_messages_.push_back(message);
81 void FakeConnectionHandler::ResetOutgoingMessageExpectations() {
82 expected_outgoing_messages_.clear();
85 bool FakeConnectionHandler::AllOutgoingMessagesReceived() const {
86 return expected_outgoing_messages_.empty();
89 void FakeConnectionHandler::ReceiveMessage(const MCSMessage& message) {
90 read_callback_.Run(message.CloneProtobuf());
93 } // namespace gcm