OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/callback.h" | 10 #include "google/cacheinvalidation/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 void InitNotification( | |
akalin
2012/01/12 23:06:44
seems cleaner to just have InitNotification return
ghc
2012/01/13 01:23:07
Done.
| |
39 const std::string& data, notifier::Notification* notification) { | |
40 notification->channel = "tango_raw"; | |
41 notification->data = data; | |
42 } | |
35 }; | 43 }; |
36 | 44 |
37 TEST_F(CacheInvalidationPacketHandlerTest, Basic) { | 45 TEST_F(CacheInvalidationPacketHandlerTest, Basic) { |
38 MessageLoop message_loop; | 46 MessageLoop message_loop; |
39 | 47 |
40 notifier::FakeBaseTask fake_base_task; | 48 notifier::FakeBaseTask fake_base_task; |
41 | 49 |
42 std::string last_message; | 50 std::string last_message; |
43 MockMessageCallback callback; | 51 MockMessageCallback callback; |
44 invalidation::MessageCallback* mock_message_callback = | 52 invalidation::MessageCallback* mock_message_callback = |
45 invalidation::NewPermanentCallback( | 53 invalidation::NewPermanentCallback( |
46 &callback, &MockMessageCallback::StoreMessage); | 54 &callback, &MockMessageCallback::StoreMessage); |
47 | 55 |
48 const char kInboundMessage[] = "non-bogus"; | 56 const char kInboundMessage[] = "non-bogus"; |
57 ipc::invalidation::ClientGatewayMessage envelope; | |
58 envelope.set_network_message(kInboundMessage); | |
59 std::string serialized; | |
60 envelope.SerializeToString(&serialized); | |
49 { | 61 { |
50 CacheInvalidationPacketHandler handler(fake_base_task.AsWeakPtr()); | 62 CacheInvalidationPacketHandler handler(fake_base_task.AsWeakPtr()); |
51 handler.SetMessageReceiver(mock_message_callback); | 63 handler.SetMessageReceiver(mock_message_callback); |
52 | 64 |
53 // Take care of any tasks posted by the constructor. | 65 // Take care of any tasks posted by the constructor. |
54 message_loop.RunAllPending(); | 66 message_loop.RunAllPending(); |
55 | 67 |
56 { | 68 { |
57 handler.HandleInboundPacket("bogus"); | 69 notifier::Notification notification; |
58 std::string inbound_message_encoded; | 70 InitNotification("bogus", ¬ification); |
59 EXPECT_TRUE( | 71 handler.OnNotificationReceived(notification); |
60 base::Base64Encode(kInboundMessage, &inbound_message_encoded)); | 72 InitNotification(serialized, ¬ification); |
61 handler.HandleInboundPacket(inbound_message_encoded); | 73 handler.OnNotificationReceived(notification); |
62 } | 74 } |
63 | 75 |
64 // Take care of any tasks posted by HandleOutboundPacket(). | 76 // Take care of any tasks posted by HandleOutboundPacket(). |
65 message_loop.RunAllPending(); | 77 message_loop.RunAllPending(); |
66 | 78 |
67 EXPECT_EQ(callback.last_message, kInboundMessage); | 79 EXPECT_EQ(callback.last_message, kInboundMessage); |
68 } | 80 } |
69 } | 81 } |
70 | 82 |
71 } // namespace sync_notifier | 83 } // namespace sync_notifier |
OLD | NEW |