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" |
11 #include "google/cacheinvalidation/v2/system-resources.h" | 12 #include "google/cacheinvalidation/v2/system-resources.h" |
12 #include "jingle/notifier/base/fake_base_task.h" | 13 #include "jingle/notifier/base/fake_base_task.h" |
| 14 #include "jingle/notifier/listener/notification_defines.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 #include "talk/base/task.h" | 17 #include "talk/base/task.h" |
16 #include "talk/xmpp/asyncsocket.h" | 18 #include "talk/xmpp/asyncsocket.h" |
17 | 19 |
18 namespace sync_notifier { | 20 namespace sync_notifier { |
19 | 21 |
20 using ::testing::_; | 22 using ::testing::_; |
21 using ::testing::Return; | 23 using ::testing::Return; |
22 | 24 |
23 class MockMessageCallback { | 25 class MockMessageCallback { |
24 public: | 26 public: |
25 void StoreMessage(const std::string& message) { | 27 void StoreMessage(const std::string& message) { |
26 last_message = message; | 28 last_message = message; |
27 } | 29 } |
28 | 30 |
29 std::string last_message; | 31 std::string last_message; |
30 }; | 32 }; |
31 | 33 |
32 class CacheInvalidationPacketHandlerTest : public testing::Test { | 34 class CacheInvalidationPacketHandlerTest : public testing::Test { |
33 public: | 35 public: |
34 virtual ~CacheInvalidationPacketHandlerTest() {} | 36 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 } |
35 }; | 44 }; |
36 | 45 |
37 TEST_F(CacheInvalidationPacketHandlerTest, Basic) { | 46 TEST_F(CacheInvalidationPacketHandlerTest, Basic) { |
38 MessageLoop message_loop; | 47 MessageLoop message_loop; |
39 | 48 |
40 notifier::FakeBaseTask fake_base_task; | 49 notifier::FakeBaseTask fake_base_task; |
41 | 50 |
42 std::string last_message; | 51 std::string last_message; |
43 MockMessageCallback callback; | 52 MockMessageCallback callback; |
44 invalidation::MessageCallback* mock_message_callback = | 53 invalidation::MessageCallback* mock_message_callback = |
45 invalidation::NewPermanentCallback( | 54 invalidation::NewPermanentCallback( |
46 &callback, &MockMessageCallback::StoreMessage); | 55 &callback, &MockMessageCallback::StoreMessage); |
47 | 56 |
48 const char kInboundMessage[] = "non-bogus"; | 57 const char kInboundMessage[] = "non-bogus"; |
| 58 ipc::invalidation::ClientGatewayMessage envelope; |
| 59 envelope.set_network_message(kInboundMessage); |
| 60 std::string serialized; |
| 61 envelope.SerializeToString(&serialized); |
49 { | 62 { |
50 CacheInvalidationPacketHandler handler(fake_base_task.AsWeakPtr()); | 63 CacheInvalidationPacketHandler handler(fake_base_task.AsWeakPtr()); |
51 handler.SetMessageReceiver(mock_message_callback); | 64 handler.SetMessageReceiver(mock_message_callback); |
52 | 65 |
53 // Take care of any tasks posted by the constructor. | 66 // Take care of any tasks posted by the constructor. |
54 message_loop.RunAllPending(); | 67 message_loop.RunAllPending(); |
55 | 68 |
56 { | 69 { |
57 handler.HandleInboundPacket("bogus"); | 70 handler.OnNotificationReceived(MakeNotification("bogus")); |
58 std::string inbound_message_encoded; | 71 handler.OnNotificationReceived(MakeNotification(serialized)); |
59 EXPECT_TRUE( | |
60 base::Base64Encode(kInboundMessage, &inbound_message_encoded)); | |
61 handler.HandleInboundPacket(inbound_message_encoded); | |
62 } | 72 } |
63 | 73 |
64 // Take care of any tasks posted by HandleOutboundPacket(). | 74 // Take care of any tasks posted by HandleOutboundPacket(). |
65 message_loop.RunAllPending(); | 75 message_loop.RunAllPending(); |
66 | 76 |
67 EXPECT_EQ(callback.last_message, kInboundMessage); | 77 EXPECT_EQ(callback.last_message, kInboundMessage); |
68 } | 78 } |
69 } | 79 } |
70 | 80 |
71 } // namespace sync_notifier | 81 } // namespace sync_notifier |
OLD | NEW |