OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "sync/notifier/invalidation_notifier.h" | 5 #include "sync/notifier/invalidation_notifier.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "jingle/notifier/base/fake_base_task.h" | 9 #include "jingle/notifier/base/fake_base_task.h" |
10 #include "jingle/notifier/base/notifier_options.h" | 10 #include "jingle/notifier/base/notifier_options.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 using ::testing::InSequence; | 26 using ::testing::InSequence; |
27 using ::testing::StrictMock; | 27 using ::testing::StrictMock; |
28 | 28 |
29 class InvalidationNotifierTest : public testing::Test { | 29 class InvalidationNotifierTest : public testing::Test { |
30 protected: | 30 protected: |
31 virtual void TearDown() { | 31 virtual void TearDown() { |
32 if (invalidation_notifier_.get()) | 32 if (invalidation_notifier_.get()) |
33 ResetNotifier(); | 33 ResetNotifier(); |
34 } | 34 } |
35 | 35 |
36 // Constructs an InvalidationNotifier, places it in | 36 // Constructs an InvalidationNotifier, places it in |invalidation_notifier_|, |
37 // |invalidation_notifier_|, and adds |mock_observer_| as an observer. This | 37 // and registers |mock_observer_| as a handler. This remains in place until |
38 // remains in place until either TearDown (automatic) or ResetNotifier | 38 // either TearDown (automatic) or ResetNotifier (manual) is called. |
39 // (manual) is called. | 39 void CreateNotifier( |
40 void CreateAndObserveNotifier( | |
41 const std::string& initial_invalidation_state) { | 40 const std::string& initial_invalidation_state) { |
42 notifier::NotifierOptions notifier_options; | 41 notifier::NotifierOptions notifier_options; |
43 // Note: URLRequestContextGetters are ref-counted. | 42 // Note: URLRequestContextGetters are ref-counted. |
44 notifier_options.request_context_getter = | 43 notifier_options.request_context_getter = |
45 new TestURLRequestContextGetter(message_loop_.message_loop_proxy()); | 44 new TestURLRequestContextGetter(message_loop_.message_loop_proxy()); |
46 invalidation_notifier_.reset( | 45 invalidation_notifier_.reset( |
47 new InvalidationNotifier( | 46 new InvalidationNotifier( |
48 scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()), | 47 scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()), |
49 InvalidationVersionMap(), | 48 InvalidationVersionMap(), |
50 initial_invalidation_state, | 49 initial_invalidation_state, |
51 MakeWeakHandle(fake_tracker_.AsWeakPtr()), | 50 MakeWeakHandle(fake_tracker_.AsWeakPtr()), |
52 "fake_client_info")); | 51 "fake_client_info")); |
| 52 invalidation_notifier_->RegisterHandler(&mock_observer_); |
53 } | 53 } |
54 | 54 |
55 void ResetNotifier() { | 55 void ResetNotifier() { |
56 invalidation_notifier_->UpdateRegisteredIds(&mock_observer_, ObjectIdSet()); | 56 invalidation_notifier_->UnregisterHandler(&mock_observer_); |
57 // Stopping the invalidation notifier stops its scheduler, which deletes any | 57 // Stopping the invalidation notifier stops its scheduler, which deletes any |
58 // pending tasks without running them. Some tasks "run and delete" another | 58 // pending tasks without running them. Some tasks "run and delete" another |
59 // task, so they must be run in order to avoid leaking the inner task. | 59 // task, so they must be run in order to avoid leaking the inner task. |
60 // Stopping does not schedule any tasks, so it's both necessary and | 60 // Stopping does not schedule any tasks, so it's both necessary and |
61 // sufficient to drain the task queue before stopping the notifier. | 61 // sufficient to drain the task queue before stopping the notifier. |
62 message_loop_.RunAllPending(); | 62 message_loop_.RunAllPending(); |
63 invalidation_notifier_.reset(); | 63 invalidation_notifier_.reset(); |
64 } | 64 } |
65 | 65 |
66 void SetStateDeprecated(const std::string& new_state) { | 66 void SetStateDeprecated(const std::string& new_state) { |
67 invalidation_notifier_->SetStateDeprecated(new_state); | 67 invalidation_notifier_->SetStateDeprecated(new_state); |
68 message_loop_.RunAllPending(); | 68 message_loop_.RunAllPending(); |
69 } | 69 } |
70 | 70 |
71 private: | 71 private: |
72 MessageLoopForIO message_loop_; | 72 MessageLoopForIO message_loop_; |
73 notifier::FakeBaseTask fake_base_task_; | 73 notifier::FakeBaseTask fake_base_task_; |
74 | 74 |
75 protected: | 75 protected: |
76 scoped_ptr<InvalidationNotifier> invalidation_notifier_; | 76 scoped_ptr<InvalidationNotifier> invalidation_notifier_; |
77 FakeInvalidationStateTracker fake_tracker_; | 77 FakeInvalidationStateTracker fake_tracker_; |
78 StrictMock<MockSyncNotifierObserver> mock_observer_; | 78 StrictMock<MockSyncNotifierObserver> mock_observer_; |
79 }; | 79 }; |
80 | 80 |
81 TEST_F(InvalidationNotifierTest, Basic) { | 81 TEST_F(InvalidationNotifierTest, Basic) { |
82 CreateAndObserveNotifier("fake_state"); | |
83 InSequence dummy; | 82 InSequence dummy; |
84 | 83 |
85 ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL); | 84 CreateNotifier("fake_state"); |
86 invalidation_notifier_->UpdateRegisteredIds( | |
87 &mock_observer_, ModelTypeSetToObjectIdSet(models)); | |
88 | 85 |
| 86 const ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL); |
89 const ModelTypePayloadMap& type_payloads = | 87 const ModelTypePayloadMap& type_payloads = |
90 ModelTypePayloadMapFromEnumSet(models, "payload"); | 88 ModelTypePayloadMapFromEnumSet(models, "payload"); |
91 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 89 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
92 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 90 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
93 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), | 91 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), |
94 REMOTE_NOTIFICATION)); | 92 REMOTE_NOTIFICATION)); |
95 EXPECT_CALL(mock_observer_, | 93 EXPECT_CALL(mock_observer_, |
96 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); | 94 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); |
97 EXPECT_CALL(mock_observer_, | 95 EXPECT_CALL(mock_observer_, |
98 OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED)); | 96 OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED)); |
99 | 97 |
| 98 invalidation_notifier_->UpdateRegisteredIds( |
| 99 &mock_observer_, ModelTypeSetToObjectIdSet(models)); |
| 100 |
100 // TODO(tim): This call should be a no-op, Remove once bug 124140 and | 101 // TODO(tim): This call should be a no-op, Remove once bug 124140 and |
101 // associated issues are fixed. | 102 // associated issues are fixed. |
102 invalidation_notifier_->SetStateDeprecated("fake_state"); | 103 invalidation_notifier_->SetStateDeprecated("fake_state"); |
103 // We don't expect |fake_tracker_|'s state to change, as we | 104 // We don't expect |fake_tracker_|'s state to change, as we |
104 // initialized with non-empty initial_invalidation_state above. | 105 // initialized with non-empty initial_invalidation_state above. |
105 EXPECT_TRUE(fake_tracker_.GetInvalidationState().empty()); | 106 EXPECT_TRUE(fake_tracker_.GetInvalidationState().empty()); |
106 invalidation_notifier_->SetUniqueId("fake_id"); | 107 invalidation_notifier_->SetUniqueId("fake_id"); |
107 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 108 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
108 | 109 |
109 invalidation_notifier_->OnNotificationsEnabled(); | 110 invalidation_notifier_->OnNotificationsEnabled(); |
110 | 111 |
111 invalidation_notifier_->OnInvalidate( | 112 invalidation_notifier_->OnInvalidate( |
112 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads)); | 113 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads)); |
113 | 114 |
114 invalidation_notifier_->OnNotificationsDisabled( | 115 invalidation_notifier_->OnNotificationsDisabled( |
115 TRANSIENT_NOTIFICATION_ERROR); | 116 TRANSIENT_NOTIFICATION_ERROR); |
116 invalidation_notifier_->OnNotificationsDisabled( | 117 invalidation_notifier_->OnNotificationsDisabled( |
117 NOTIFICATION_CREDENTIALS_REJECTED); | 118 NOTIFICATION_CREDENTIALS_REJECTED); |
118 } | 119 } |
119 | 120 |
120 TEST_F(InvalidationNotifierTest, MigrateState) { | 121 TEST_F(InvalidationNotifierTest, MigrateState) { |
121 CreateAndObserveNotifier(std::string()); | 122 CreateNotifier(std::string()); |
122 | 123 |
123 SetStateDeprecated("fake_state"); | 124 SetStateDeprecated("fake_state"); |
124 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); | 125 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); |
125 | 126 |
126 // Should do nothing. | 127 // Should do nothing. |
127 SetStateDeprecated("spurious_fake_state"); | 128 SetStateDeprecated("spurious_fake_state"); |
128 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); | 129 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); |
129 | 130 |
130 // Pretend Chrome shut down. | 131 // Pretend Chrome shut down. |
131 ResetNotifier(); | 132 ResetNotifier(); |
132 | 133 |
133 CreateAndObserveNotifier("fake_state"); | 134 CreateNotifier("fake_state"); |
134 // Should do nothing. | 135 // Should do nothing. |
135 SetStateDeprecated("more_spurious_fake_state"); | 136 SetStateDeprecated("more_spurious_fake_state"); |
136 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); | 137 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); |
137 } | 138 } |
138 | 139 |
139 } // namespace | 140 } // namespace |
140 | 141 |
141 } // namespace syncer | 142 } // namespace syncer |
OLD | NEW |