OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/notifier/cache_invalidation_packet_handler.h" | 5 #include "chrome/browser/sync/notifier/cache_invalidation_packet_handler.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "google/cacheinvalidation/v2/callback.h" | 10 #include "google/cacheinvalidation/v2/callback.h" |
11 #include "google/cacheinvalidation/v2/client_gateway.pb.h" | |
12 #include "google/cacheinvalidation/v2/system-resources.h" | 11 #include "google/cacheinvalidation/v2/system-resources.h" |
13 #include "jingle/notifier/base/fake_base_task.h" | 12 #include "jingle/notifier/base/fake_base_task.h" |
14 #include "jingle/notifier/listener/notification_defines.h" | |
15 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
17 #include "talk/base/task.h" | 15 #include "talk/base/task.h" |
18 #include "talk/xmpp/asyncsocket.h" | 16 #include "talk/xmpp/asyncsocket.h" |
19 | 17 |
20 namespace sync_notifier { | 18 namespace sync_notifier { |
21 | 19 |
22 using ::testing::_; | 20 using ::testing::_; |
23 using ::testing::Return; | 21 using ::testing::Return; |
24 | 22 |
25 class MockMessageCallback { | 23 class MockMessageCallback { |
26 public: | 24 public: |
27 void StoreMessage(const std::string& message) { | 25 void StoreMessage(const std::string& message) { |
28 last_message = message; | 26 last_message = message; |
29 } | 27 } |
30 | 28 |
31 std::string last_message; | 29 std::string last_message; |
32 }; | 30 }; |
33 | 31 |
34 class CacheInvalidationPacketHandlerTest : public testing::Test { | 32 class CacheInvalidationPacketHandlerTest : public testing::Test { |
35 public: | 33 public: |
36 virtual ~CacheInvalidationPacketHandlerTest() {} | 34 virtual ~CacheInvalidationPacketHandlerTest() {} |
37 | |
38 notifier::Notification MakeNotification(const std::string& data) { | |
39 notifier::Notification notification; | |
40 notification.channel = "tango_raw"; | |
41 notification.data = data; | |
42 return notification; | |
43 } | |
44 }; | 35 }; |
45 | 36 |
46 TEST_F(CacheInvalidationPacketHandlerTest, Basic) { | 37 TEST_F(CacheInvalidationPacketHandlerTest, Basic) { |
47 MessageLoop message_loop; | 38 MessageLoop message_loop; |
48 | 39 |
49 notifier::FakeBaseTask fake_base_task; | 40 notifier::FakeBaseTask fake_base_task; |
50 | 41 |
51 std::string last_message; | 42 std::string last_message; |
52 MockMessageCallback callback; | 43 MockMessageCallback callback; |
53 invalidation::MessageCallback* mock_message_callback = | 44 invalidation::MessageCallback* mock_message_callback = |
54 invalidation::NewPermanentCallback( | 45 invalidation::NewPermanentCallback( |
55 &callback, &MockMessageCallback::StoreMessage); | 46 &callback, &MockMessageCallback::StoreMessage); |
56 | 47 |
57 const char kInboundMessage[] = "non-bogus"; | 48 const char kInboundMessage[] = "non-bogus"; |
58 ipc::invalidation::ClientGatewayMessage envelope; | |
59 envelope.set_network_message(kInboundMessage); | |
60 std::string serialized; | |
61 envelope.SerializeToString(&serialized); | |
62 { | 49 { |
63 CacheInvalidationPacketHandler handler(fake_base_task.AsWeakPtr()); | 50 CacheInvalidationPacketHandler handler(fake_base_task.AsWeakPtr()); |
64 handler.SetMessageReceiver(mock_message_callback); | 51 handler.SetMessageReceiver(mock_message_callback); |
65 | 52 |
66 // Take care of any tasks posted by the constructor. | 53 // Take care of any tasks posted by the constructor. |
67 message_loop.RunAllPending(); | 54 message_loop.RunAllPending(); |
68 | 55 |
69 { | 56 { |
70 handler.OnNotificationReceived(MakeNotification("bogus")); | 57 handler.HandleInboundPacket("bogus"); |
71 handler.OnNotificationReceived(MakeNotification(serialized)); | 58 std::string inbound_message_encoded; |
| 59 EXPECT_TRUE( |
| 60 base::Base64Encode(kInboundMessage, &inbound_message_encoded)); |
| 61 handler.HandleInboundPacket(inbound_message_encoded); |
72 } | 62 } |
73 | 63 |
74 // Take care of any tasks posted by HandleOutboundPacket(). | 64 // Take care of any tasks posted by HandleOutboundPacket(). |
75 message_loop.RunAllPending(); | 65 message_loop.RunAllPending(); |
76 | 66 |
77 EXPECT_EQ(callback.last_message, kInboundMessage); | 67 EXPECT_EQ(callback.last_message, kInboundMessage); |
78 } | 68 } |
79 } | 69 } |
80 | 70 |
81 } // namespace sync_notifier | 71 } // namespace sync_notifier |
OLD | NEW |