• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "sync/notifier/push_client_channel.h"
6 
7 #include <cstddef>
8 #include <string>
9 
10 #include "base/compiler_specific.h"
11 #include "jingle/notifier/listener/fake_push_client.h"
12 #include "jingle/notifier/listener/notification_defines.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace syncer {
16 namespace {
17 
18 class PushClientChannelTest
19     : public ::testing::Test,
20       public SyncNetworkChannel::Observer {
21  protected:
PushClientChannelTest()22   PushClientChannelTest()
23       : fake_push_client_(new notifier::FakePushClient()),
24         push_client_channel_(
25             scoped_ptr<notifier::PushClient>(fake_push_client_)),
26         last_invalidator_state_(DEFAULT_INVALIDATION_ERROR) {
27     push_client_channel_.AddObserver(this);
28     push_client_channel_.SetMessageReceiver(
29         invalidation::NewPermanentCallback(
30             this, &PushClientChannelTest::OnIncomingMessage));
31     push_client_channel_.SetSystemResources(NULL);
32   }
33 
~PushClientChannelTest()34   virtual ~PushClientChannelTest() {
35     push_client_channel_.RemoveObserver(this);
36   }
37 
OnNetworkChannelStateChanged(InvalidatorState invalidator_state)38   virtual void OnNetworkChannelStateChanged(
39       InvalidatorState invalidator_state) OVERRIDE {
40     last_invalidator_state_ = invalidator_state;
41   }
42 
OnIncomingMessage(std::string incoming_message)43   void OnIncomingMessage(std::string incoming_message) {
44     last_message_ = incoming_message;
45   }
46 
47   notifier::FakePushClient* fake_push_client_;
48   PushClientChannel push_client_channel_;
49   std::string last_message_;
50   InvalidatorState last_invalidator_state_;
51 };
52 
53 const char kMessage[] = "message";
54 const char kServiceContext[] = "service context";
55 const int64 kSchedulingHash = 100;
56 
57 // Make sure the channel subscribes to the correct notifications
58 // channel on construction.
TEST_F(PushClientChannelTest,Subscriptions)59 TEST_F(PushClientChannelTest, Subscriptions) {
60   notifier::Subscription expected_subscription;
61   expected_subscription.channel = "tango_raw";
62   EXPECT_TRUE(notifier::SubscriptionListsEqual(
63       fake_push_client_->subscriptions(),
64       notifier::SubscriptionList(1, expected_subscription)));
65 }
66 
67 // Call UpdateCredentials on the channel.  It should propagate it to
68 // the push client.
TEST_F(PushClientChannelTest,UpdateCredentials)69 TEST_F(PushClientChannelTest, UpdateCredentials) {
70   const char kEmail[] = "foo@bar.com";
71   const char kToken[] = "token";
72   EXPECT_TRUE(fake_push_client_->email().empty());
73   EXPECT_TRUE(fake_push_client_->token().empty());
74   push_client_channel_.UpdateCredentials(kEmail, kToken);
75   EXPECT_EQ(kEmail, fake_push_client_->email());
76   EXPECT_EQ(kToken, fake_push_client_->token());
77 }
78 
79 // Simulate push client state changes on the push client.  It should
80 // propagate to the channel.
TEST_F(PushClientChannelTest,OnPushClientStateChange)81 TEST_F(PushClientChannelTest, OnPushClientStateChange) {
82   EXPECT_EQ(DEFAULT_INVALIDATION_ERROR, last_invalidator_state_);
83   fake_push_client_->EnableNotifications();
84   EXPECT_EQ(INVALIDATIONS_ENABLED, last_invalidator_state_);
85   fake_push_client_->DisableNotifications(
86       notifier::TRANSIENT_NOTIFICATION_ERROR);
87   EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, last_invalidator_state_);
88   fake_push_client_->DisableNotifications(
89       notifier::NOTIFICATION_CREDENTIALS_REJECTED);
90   EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED, last_invalidator_state_);
91 }
92 
93 // Call SendMessage on the channel.  It should propagate it to the
94 // push client.
TEST_F(PushClientChannelTest,SendMessage)95 TEST_F(PushClientChannelTest, SendMessage) {
96   EXPECT_TRUE(fake_push_client_->sent_notifications().empty());
97   push_client_channel_.SendMessage(kMessage);
98   ASSERT_EQ(1u, fake_push_client_->sent_notifications().size());
99 }
100 
101 // Simulate an incoming notification.  It should be decoded properly
102 // by the channel.
TEST_F(PushClientChannelTest,OnIncomingNotification)103 TEST_F(PushClientChannelTest, OnIncomingNotification) {
104   notifier::Notification notification;
105   notification.data =
106       PushClientChannel::EncodeMessageForTest(
107           kMessage, kServiceContext, kSchedulingHash);
108 
109   fake_push_client_->SimulateIncomingNotification(notification);
110   EXPECT_EQ(kServiceContext,
111             push_client_channel_.GetServiceContextForTest());
112   EXPECT_EQ(kSchedulingHash,
113             push_client_channel_.GetSchedulingHashForTest());
114   EXPECT_EQ(kMessage, last_message_);
115 }
116 
117 }  // namespace
118 }  // namespace syncer
119