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. 4 5 #ifndef GOOGLE_APIS_GCM_ENGINE_FAKE_CONNECTION_HANDLER_H_ 6 #define GOOGLE_APIS_GCM_ENGINE_FAKE_CONNECTION_HANDLER_H_ 7 8 #include <list> 9 10 #include "google_apis/gcm/base/mcs_message.h" 11 #include "google_apis/gcm/engine/connection_handler.h" 12 13 namespace gcm { 14 15 // A fake implementation of a ConnectionHandler that can arbitrarily receive 16 // messages and verify expectations for outgoing messages. 17 class FakeConnectionHandler : public ConnectionHandler { 18 public: 19 FakeConnectionHandler( 20 const ConnectionHandler::ProtoReceivedCallback& read_callback, 21 const ConnectionHandler::ProtoSentCallback& write_callback); 22 virtual ~FakeConnectionHandler(); 23 24 // ConnectionHandler implementation. 25 virtual void Init(const mcs_proto::LoginRequest& login_request, 26 net::StreamSocket* socket) OVERRIDE; 27 virtual void Reset() OVERRIDE; 28 virtual bool CanSendMessage() const OVERRIDE; 29 virtual void SendMessage(const google::protobuf::MessageLite& message) 30 OVERRIDE; 31 32 // EXPECT's receipt of |message| via SendMessage(..). 33 void ExpectOutgoingMessage(const MCSMessage& message); 34 35 // Reset the expected outgoing messages. 36 void ResetOutgoingMessageExpectations(); 37 38 // Whether all expected outgoing messages have been received; 39 bool AllOutgoingMessagesReceived() const; 40 41 // Passes on |message| to |write_callback_|. 42 void ReceiveMessage(const MCSMessage& message); 43 44 // Whether to return an error with the next login response. set_fail_login(bool fail_login)45 void set_fail_login(bool fail_login) { 46 fail_login_ = fail_login; 47 } 48 49 // Whether to invoke the write callback on the next send attempt or fake a 50 // connection error instead. set_fail_send(bool fail_send)51 void set_fail_send(bool fail_send) { 52 fail_send_ = fail_send; 53 } 54 55 private: 56 ConnectionHandler::ProtoReceivedCallback read_callback_; 57 ConnectionHandler::ProtoSentCallback write_callback_; 58 59 std::list<MCSMessage> expected_outgoing_messages_; 60 61 // Whether to fail the login or not. 62 bool fail_login_; 63 64 // Whether to fail a SendMessage call or not. 65 bool fail_send_; 66 67 // Whether a successful login has completed. 68 bool initialized_; 69 70 DISALLOW_COPY_AND_ASSIGN(FakeConnectionHandler); 71 }; 72 73 } // namespace gcm 74 75 #endif // GOOGLE_APIS_GCM_ENGINE_FAKE_CONNECTION_HANDLER_H_ 76