15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sync/notifier/push_client_channel.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <cstddef>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "jingle/notifier/listener/fake_push_client.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "jingle/notifier/listener/notification_defines.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "testing/gtest/include/gtest/gtest.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace syncer {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
18a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)class PushClientChannelTest
19a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    : public ::testing::Test,
20a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      public SyncNetworkChannel::Observer {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PushClientChannelTest()
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      : fake_push_client_(new notifier::FakePushClient()),
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        push_client_channel_(
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            scoped_ptr<notifier::PushClient>(fake_push_client_)),
26a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)        last_invalidator_state_(DEFAULT_INVALIDATION_ERROR) {
27a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    push_client_channel_.AddObserver(this);
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    push_client_channel_.SetMessageReceiver(
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        invalidation::NewPermanentCallback(
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            this, &PushClientChannelTest::OnIncomingMessage));
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    push_client_channel_.SetSystemResources(NULL);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
34a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual ~PushClientChannelTest() {
35a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    push_client_channel_.RemoveObserver(this);
36a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
38a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual void OnNetworkChannelStateChanged(
39a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      InvalidatorState invalidator_state) OVERRIDE {
40a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    last_invalidator_state_ = invalidator_state;
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
43a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void OnIncomingMessage(std::string incoming_message) {
44a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    last_message_ = incoming_message;
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  notifier::FakePushClient* fake_push_client_;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PushClientChannel push_client_channel_;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string last_message_;
50a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  InvalidatorState last_invalidator_state_;
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kMessage[] = "message";
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kServiceContext[] = "service context";
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int64 kSchedulingHash = 100;
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Make sure the channel subscribes to the correct notifications
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// channel on construction.
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(PushClientChannelTest, Subscriptions) {
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  notifier::Subscription expected_subscription;
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  expected_subscription.channel = "tango_raw";
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(notifier::SubscriptionListsEqual(
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      fake_push_client_->subscriptions(),
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      notifier::SubscriptionList(1, expected_subscription)));
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Call UpdateCredentials on the channel.  It should propagate it to
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the push client.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(PushClientChannelTest, UpdateCredentials) {
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char kEmail[] = "foo@bar.com";
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const char kToken[] = "token";
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(fake_push_client_->email().empty());
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_TRUE(fake_push_client_->token().empty());
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  push_client_channel_.UpdateCredentials(kEmail, kToken);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(kEmail, fake_push_client_->email());
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(kToken, fake_push_client_->token());
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Simulate push client state changes on the push client.  It should
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// propagate to the channel.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(PushClientChannelTest, OnPushClientStateChange) {
82a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_EQ(DEFAULT_INVALIDATION_ERROR, last_invalidator_state_);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_push_client_->EnableNotifications();
84a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_EQ(INVALIDATIONS_ENABLED, last_invalidator_state_);
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_push_client_->DisableNotifications(
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      notifier::TRANSIENT_NOTIFICATION_ERROR);
87a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_EQ(TRANSIENT_INVALIDATION_ERROR, last_invalidator_state_);
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_push_client_->DisableNotifications(
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      notifier::NOTIFICATION_CREDENTIALS_REJECTED);
90a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_EQ(INVALIDATION_CREDENTIALS_REJECTED, last_invalidator_state_);
91a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
92a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
93a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Call SendMessage on the channel.  It should propagate it to the
94a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// push client.
95a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)TEST_F(PushClientChannelTest, SendMessage) {
96a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  EXPECT_TRUE(fake_push_client_->sent_notifications().empty());
97a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  push_client_channel_.SendMessage(kMessage);
98a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ASSERT_EQ(1u, fake_push_client_->sent_notifications().size());
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Simulate an incoming notification.  It should be decoded properly
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// by the channel.
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TEST_F(PushClientChannelTest, OnIncomingNotification) {
104a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  notifier::Notification notification;
105a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  notification.data =
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      PushClientChannel::EncodeMessageForTest(
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          kMessage, kServiceContext, kSchedulingHash);
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  fake_push_client_->SimulateIncomingNotification(notification);
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(kServiceContext,
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            push_client_channel_.GetServiceContextForTest());
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(kSchedulingHash,
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            push_client_channel_.GetSchedulingHashForTest());
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  EXPECT_EQ(kMessage, last_message_);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace syncer
119