summaryrefslogtreecommitdiffstats
path: root/sync/notifier/invalidation_notifier_unittest.cc
blob: 973c26f95a394d5a21b1819808c0ea481e6845a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sync/notifier/invalidation_notifier.h"

#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "jingle/notifier/base/fake_base_task.h"
#include "jingle/notifier/base/notifier_options.h"
#include "jingle/notifier/listener/fake_push_client.h"
#include "net/url_request/url_request_test_util.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/internal_api/public/base/model_type_payload_map.h"
#include "sync/internal_api/public/util/weak_handle.h"
#include "sync/notifier/fake_invalidation_state_tracker.h"
#include "sync/notifier/invalidation_state_tracker.h"
#include "sync/notifier/mock_sync_notifier_observer.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace syncer {

namespace {

using ::testing::InSequence;
using ::testing::StrictMock;

class InvalidationNotifierTest : public testing::Test {
 protected:
  virtual void TearDown() {
    if (invalidation_notifier_.get())
      ResetNotifier();
  }

  // Constructs an InvalidationNotifier, places it in |invalidation_notifier_|,
  // and registers |mock_observer_| as a handler. This remains in place until
  // either TearDown (automatic) or ResetNotifier (manual) is called.
  void CreateNotifier(
      const std::string& initial_invalidation_state) {
    notifier::NotifierOptions notifier_options;
    // Note: URLRequestContextGetters are ref-counted.
    notifier_options.request_context_getter =
        new TestURLRequestContextGetter(message_loop_.message_loop_proxy());
    invalidation_notifier_.reset(
        new InvalidationNotifier(
            scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()),
            InvalidationVersionMap(),
            initial_invalidation_state,
            MakeWeakHandle(fake_tracker_.AsWeakPtr()),
            "fake_client_info"));
    invalidation_notifier_->RegisterHandler(&mock_observer_);
  }

  void ResetNotifier() {
    invalidation_notifier_->UnregisterHandler(&mock_observer_);
    // Stopping the invalidation notifier stops its scheduler, which deletes any
    // pending tasks without running them.  Some tasks "run and delete" another
    // task, so they must be run in order to avoid leaking the inner task.
    // Stopping does not schedule any tasks, so it's both necessary and
    // sufficient to drain the task queue before stopping the notifier.
    message_loop_.RunAllPending();
    invalidation_notifier_.reset();
  }

  void SetStateDeprecated(const std::string& new_state) {
    invalidation_notifier_->SetStateDeprecated(new_state);
    message_loop_.RunAllPending();
  }

 private:
  MessageLoopForIO message_loop_;
  notifier::FakeBaseTask fake_base_task_;

 protected:
  scoped_ptr<InvalidationNotifier> invalidation_notifier_;
  FakeInvalidationStateTracker fake_tracker_;
  StrictMock<MockSyncNotifierObserver> mock_observer_;
};

TEST_F(InvalidationNotifierTest, Basic) {
  InSequence dummy;

  CreateNotifier("fake_state");

  const ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL);
  const ModelTypePayloadMap& type_payloads =
      ModelTypePayloadMapFromEnumSet(models, "payload");
  EXPECT_CALL(mock_observer_, OnNotificationsEnabled());
  EXPECT_CALL(mock_observer_, OnIncomingNotification(
      ModelTypePayloadMapToObjectIdPayloadMap(type_payloads),
      REMOTE_NOTIFICATION));
  EXPECT_CALL(mock_observer_,
              OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR));
  EXPECT_CALL(mock_observer_,
              OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED));

  invalidation_notifier_->UpdateRegisteredIds(
      &mock_observer_, ModelTypeSetToObjectIdSet(models));

  // TODO(tim): This call should be a no-op, Remove once bug 124140 and
  // associated issues are fixed.
  invalidation_notifier_->SetStateDeprecated("fake_state");
  // We don't expect |fake_tracker_|'s state to change, as we
  // initialized with non-empty initial_invalidation_state above.
  EXPECT_TRUE(fake_tracker_.GetInvalidationState().empty());
  invalidation_notifier_->SetUniqueId("fake_id");
  invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token");

  invalidation_notifier_->OnNotificationsEnabled();

  invalidation_notifier_->OnInvalidate(
      ModelTypePayloadMapToObjectIdPayloadMap(type_payloads));

  invalidation_notifier_->OnNotificationsDisabled(
      TRANSIENT_NOTIFICATION_ERROR);
  invalidation_notifier_->OnNotificationsDisabled(
      NOTIFICATION_CREDENTIALS_REJECTED);
}

TEST_F(InvalidationNotifierTest, MigrateState) {
  CreateNotifier(std::string());

  SetStateDeprecated("fake_state");
  EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState());

  // Should do nothing.
  SetStateDeprecated("spurious_fake_state");
  EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState());

  // Pretend Chrome shut down.
  ResetNotifier();

  CreateNotifier("fake_state");
  // Should do nothing.
  SetStateDeprecated("more_spurious_fake_state");
  EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState());
}

}  // namespace

}  // namespace syncer