Home | History | Annotate | Download | only in notifier
      1 // Copyright (c) 2011 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 #include "chrome/browser/sync/notifier/cache_invalidation_packet_handler.h"
      6 
      7 #include "base/base64.h"
      8 #include "base/memory/weak_ptr.h"
      9 #include "base/message_loop.h"
     10 #include "google/cacheinvalidation/invalidation-client.h"
     11 #include "jingle/notifier/base/fake_base_task.h"
     12 #include "testing/gmock/include/gmock/gmock.h"
     13 #include "testing/gtest/include/gtest/gtest.h"
     14 #include "talk/base/task.h"
     15 #include "talk/xmpp/asyncsocket.h"
     16 
     17 namespace sync_notifier {
     18 
     19 using ::testing::_;
     20 using ::testing::Return;
     21 
     22 class MockNetworkEndpoint : public invalidation::NetworkEndpoint {
     23  public:
     24   MOCK_METHOD1(RegisterOutboundListener,
     25                void(invalidation::NetworkCallback*));
     26   MOCK_METHOD1(HandleInboundMessage, void(const invalidation::string&));
     27   MOCK_METHOD1(TakeOutboundMessage, void(invalidation::string*));
     28   MOCK_METHOD1(AdviseNetworkStatus, void(bool));
     29 };
     30 
     31 class MockInvalidationClient : public invalidation::InvalidationClient {
     32  public:
     33   MOCK_METHOD1(Start, void(const std::string& str));
     34   MOCK_METHOD1(Register, void(const invalidation::ObjectId&));
     35   MOCK_METHOD1(Unregister, void(const invalidation::ObjectId&));
     36   MOCK_METHOD0(network_endpoint, invalidation::NetworkEndpoint*());
     37 };
     38 
     39 class CacheInvalidationPacketHandlerTest : public testing::Test {
     40  public:
     41   virtual ~CacheInvalidationPacketHandlerTest() {}
     42 };
     43 
     44 TEST_F(CacheInvalidationPacketHandlerTest, Basic) {
     45   MessageLoop message_loop;
     46 
     47   notifier::FakeBaseTask fake_base_task;
     48 
     49   MockNetworkEndpoint mock_network_endpoint;
     50   MockInvalidationClient mock_invalidation_client;
     51 
     52   EXPECT_CALL(mock_invalidation_client, network_endpoint()).
     53       WillRepeatedly(Return(&mock_network_endpoint));
     54 
     55   const char kInboundMessage[] = "non-bogus";
     56   EXPECT_CALL(mock_network_endpoint,
     57               HandleInboundMessage(kInboundMessage)).Times(1);
     58   EXPECT_CALL(mock_network_endpoint, TakeOutboundMessage(_)).Times(1);
     59 
     60   {
     61     CacheInvalidationPacketHandler handler(
     62         fake_base_task.AsWeakPtr(), &mock_invalidation_client);
     63     // Take care of any tasks posted by the constructor.
     64     message_loop.RunAllPending();
     65 
     66     {
     67       handler.HandleInboundPacket("bogus");
     68       std::string inbound_message_encoded;
     69       EXPECT_TRUE(
     70           base::Base64Encode(kInboundMessage, &inbound_message_encoded));
     71       handler.HandleInboundPacket(inbound_message_encoded);
     72     }
     73 
     74     handler.HandleOutboundPacket(&mock_network_endpoint);
     75     // Take care of any tasks posted by HandleOutboundPacket().
     76     message_loop.RunAllPending();
     77   }
     78 }
     79 
     80 }  // namespace sync_notifier
     81