Home | History | Annotate | Download | only in notifier
      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
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/sync/notifier/invalidation_notifier.h"
      6 
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/message_loop.h"
      9 #include "chrome/browser/sync/notifier/mock_sync_notifier_observer.h"
     10 #include "chrome/browser/sync/syncable/model_type.h"
     11 #include "chrome/browser/sync/syncable/model_type_payload_map.h"
     12 #include "chrome/test/test_url_request_context_getter.h"
     13 #include "content/browser/browser_thread.h"
     14 #include "jingle/notifier/base/fake_base_task.h"
     15 #include "net/base/cert_verifier.h"
     16 #include "net/base/host_resolver.h"
     17 #include "testing/gmock/include/gmock/gmock.h"
     18 #include "testing/gtest/include/gtest/gtest.h"
     19 
     20 namespace sync_notifier {
     21 
     22 namespace {
     23 
     24 using ::testing::InSequence;
     25 using ::testing::StrictMock;
     26 
     27 class InvalidationNotifierTest : public testing::Test {
     28  public:
     29   InvalidationNotifierTest() : io_thread_(BrowserThread::IO, &message_loop_) {}
     30 
     31  protected:
     32   virtual void SetUp() {
     33     request_context_getter_ = new TestURLRequestContextGetter;
     34     notifier::NotifierOptions notifier_options;
     35     notifier_options.request_context_getter = request_context_getter_;
     36     invalidation_notifier_.reset(new InvalidationNotifier(notifier_options,
     37                                                           "fake_client_info"));
     38     invalidation_notifier_->AddObserver(&mock_observer_);
     39   }
     40 
     41   virtual void TearDown() {
     42     invalidation_notifier_->RemoveObserver(&mock_observer_);
     43     invalidation_notifier_.reset();
     44     request_context_getter_ = NULL;
     45   }
     46 
     47   MessageLoop message_loop_;
     48   scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
     49   scoped_ptr<InvalidationNotifier> invalidation_notifier_;
     50   StrictMock<MockSyncNotifierObserver> mock_observer_;
     51   notifier::FakeBaseTask fake_base_task_;
     52   // Since this test calls HostResolver code, we need an IO thread.
     53   BrowserThread io_thread_;
     54 };
     55 
     56 TEST_F(InvalidationNotifierTest, Basic) {
     57   InSequence dummy;
     58 
     59   syncable::ModelTypePayloadMap type_payloads;
     60   type_payloads[syncable::PREFERENCES] = "payload";
     61   type_payloads[syncable::BOOKMARKS] = "";
     62   type_payloads[syncable::AUTOFILL] = "";
     63 
     64   EXPECT_CALL(mock_observer_, OnNotificationStateChange(true));
     65   EXPECT_CALL(mock_observer_, StoreState("new_fake_state"));
     66   EXPECT_CALL(mock_observer_, OnIncomingNotification(type_payloads));
     67   EXPECT_CALL(mock_observer_, OnNotificationStateChange(false));
     68 
     69   invalidation_notifier_->SetState("fake_state");
     70   invalidation_notifier_->UpdateCredentials("foo (at) bar.com", "fake_token");
     71 
     72   invalidation_notifier_->OnConnect(fake_base_task_.AsWeakPtr());
     73   invalidation_notifier_->OnSessionStatusChanged(true);
     74 
     75   invalidation_notifier_->WriteState("new_fake_state");
     76 
     77   invalidation_notifier_->OnInvalidate(type_payloads);
     78 
     79   // Shouldn't trigger notification state change.
     80   invalidation_notifier_->OnDisconnect();
     81   invalidation_notifier_->OnConnect(fake_base_task_.AsWeakPtr());
     82 
     83   invalidation_notifier_->OnSessionStatusChanged(false);
     84 }
     85 
     86 }  // namespace
     87 
     88 }  // namespace sync_notifier
     89