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" |
11 #include "jingle/notifier/listener/fake_push_client.h" | 11 #include "jingle/notifier/listener/fake_push_client.h" |
12 #include "net/url_request/url_request_test_util.h" | 12 #include "net/url_request/url_request_test_util.h" |
13 #include "sync/internal_api/public/base/model_type.h" | 13 #include "sync/internal_api/public/base/model_type.h" |
14 #include "sync/internal_api/public/base/model_type_payload_map.h" | 14 #include "sync/internal_api/public/base/model_type_payload_map.h" |
15 #include "sync/internal_api/public/util/weak_handle.h" | 15 #include "sync/internal_api/public/util/weak_handle.h" |
| 16 #include "sync/notifier/fake_invalidation_state_tracker.h" |
16 #include "sync/notifier/invalidation_state_tracker.h" | 17 #include "sync/notifier/invalidation_state_tracker.h" |
17 #include "sync/notifier/mock_invalidation_state_tracker.h" | |
18 #include "sync/notifier/mock_sync_notifier_observer.h" | 18 #include "sync/notifier/mock_sync_notifier_observer.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 | 21 |
22 namespace syncer { | 22 namespace syncer { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 using ::testing::InSequence; | 26 using ::testing::InSequence; |
27 using ::testing::StrictMock; | 27 using ::testing::StrictMock; |
(...skipping 13 matching lines...) Expand all Loading... |
41 const std::string& initial_invalidation_state) { | 41 const std::string& initial_invalidation_state) { |
42 notifier::NotifierOptions notifier_options; | 42 notifier::NotifierOptions notifier_options; |
43 // Note: URLRequestContextGetters are ref-counted. | 43 // Note: URLRequestContextGetters are ref-counted. |
44 notifier_options.request_context_getter = | 44 notifier_options.request_context_getter = |
45 new TestURLRequestContextGetter(message_loop_.message_loop_proxy()); | 45 new TestURLRequestContextGetter(message_loop_.message_loop_proxy()); |
46 invalidation_notifier_.reset( | 46 invalidation_notifier_.reset( |
47 new InvalidationNotifier( | 47 new InvalidationNotifier( |
48 scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()), | 48 scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()), |
49 InvalidationVersionMap(), | 49 InvalidationVersionMap(), |
50 initial_invalidation_state, | 50 initial_invalidation_state, |
51 MakeWeakHandle(mock_tracker_.AsWeakPtr()), | 51 MakeWeakHandle(fake_tracker_.AsWeakPtr()), |
52 "fake_client_info")); | 52 "fake_client_info")); |
53 } | 53 } |
54 | 54 |
55 void ResetNotifier() { | 55 void ResetNotifier() { |
56 invalidation_notifier_->UpdateRegisteredIds(&mock_observer_, ObjectIdSet()); | 56 invalidation_notifier_->UpdateRegisteredIds(&mock_observer_, ObjectIdSet()); |
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) { |
| 67 invalidation_notifier_->SetStateDeprecated(new_state); |
| 68 message_loop_.RunAllPending(); |
| 69 } |
| 70 |
| 71 private: |
66 MessageLoopForIO message_loop_; | 72 MessageLoopForIO message_loop_; |
| 73 notifier::FakeBaseTask fake_base_task_; |
| 74 |
| 75 protected: |
67 scoped_ptr<InvalidationNotifier> invalidation_notifier_; | 76 scoped_ptr<InvalidationNotifier> invalidation_notifier_; |
68 StrictMock<MockInvalidationStateTracker> mock_tracker_; | 77 FakeInvalidationStateTracker fake_tracker_; |
69 StrictMock<MockSyncNotifierObserver> mock_observer_; | 78 StrictMock<MockSyncNotifierObserver> mock_observer_; |
70 notifier::FakeBaseTask fake_base_task_; | |
71 }; | 79 }; |
72 | 80 |
73 TEST_F(InvalidationNotifierTest, Basic) { | 81 TEST_F(InvalidationNotifierTest, Basic) { |
74 CreateAndObserveNotifier("fake_state"); | 82 CreateAndObserveNotifier("fake_state"); |
75 InSequence dummy; | 83 InSequence dummy; |
76 | 84 |
77 ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL); | 85 ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL); |
78 invalidation_notifier_->UpdateRegisteredIds( | 86 invalidation_notifier_->UpdateRegisteredIds( |
79 &mock_observer_, ModelTypeSetToObjectIdSet(models)); | 87 &mock_observer_, ModelTypeSetToObjectIdSet(models)); |
80 | 88 |
81 const ModelTypePayloadMap& type_payloads = | 89 const ModelTypePayloadMap& type_payloads = |
82 ModelTypePayloadMapFromEnumSet(models, "payload"); | 90 ModelTypePayloadMapFromEnumSet(models, "payload"); |
83 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 91 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
84 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 92 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
85 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), | 93 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), |
86 REMOTE_NOTIFICATION)); | 94 REMOTE_NOTIFICATION)); |
87 EXPECT_CALL(mock_observer_, | 95 EXPECT_CALL(mock_observer_, |
88 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); | 96 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); |
89 EXPECT_CALL(mock_observer_, | 97 EXPECT_CALL(mock_observer_, |
90 OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED)); | 98 OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED)); |
91 // Note no expectation on mock_tracker_, as we initialized with | |
92 // non-empty initial_invalidation_state above. | |
93 | 99 |
94 // TODO(tim): This call should be a no-op, Remove once bug 124140 and | 100 // TODO(tim): This call should be a no-op, Remove once bug 124140 and |
95 // associated issues are fixed. | 101 // associated issues are fixed. |
96 invalidation_notifier_->SetStateDeprecated("fake_state"); | 102 invalidation_notifier_->SetStateDeprecated("fake_state"); |
| 103 // We don't expect |fake_tracker_|'s state to change, as we |
| 104 // initialized with non-empty initial_invalidation_state above. |
| 105 EXPECT_TRUE(fake_tracker_.GetInvalidationState().empty()); |
97 invalidation_notifier_->SetUniqueId("fake_id"); | 106 invalidation_notifier_->SetUniqueId("fake_id"); |
98 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 107 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
99 | 108 |
100 invalidation_notifier_->OnNotificationsEnabled(); | 109 invalidation_notifier_->OnNotificationsEnabled(); |
101 | 110 |
102 invalidation_notifier_->OnInvalidate( | 111 invalidation_notifier_->OnInvalidate( |
103 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads)); | 112 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads)); |
104 | 113 |
105 invalidation_notifier_->OnNotificationsDisabled( | 114 invalidation_notifier_->OnNotificationsDisabled( |
106 TRANSIENT_NOTIFICATION_ERROR); | 115 TRANSIENT_NOTIFICATION_ERROR); |
107 invalidation_notifier_->OnNotificationsDisabled( | 116 invalidation_notifier_->OnNotificationsDisabled( |
108 NOTIFICATION_CREDENTIALS_REJECTED); | 117 NOTIFICATION_CREDENTIALS_REJECTED); |
109 } | 118 } |
110 | 119 |
111 TEST_F(InvalidationNotifierTest, MigrateState) { | 120 TEST_F(InvalidationNotifierTest, MigrateState) { |
112 CreateAndObserveNotifier(std::string()); | 121 CreateAndObserveNotifier(std::string()); |
113 InSequence dummy; | |
114 | 122 |
115 EXPECT_CALL(mock_tracker_, SetInvalidationState("fake_state")); | 123 SetStateDeprecated("fake_state"); |
116 invalidation_notifier_->SetStateDeprecated("fake_state"); | 124 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); |
117 | 125 |
118 // Should do nothing. | 126 // Should do nothing. |
119 invalidation_notifier_->SetStateDeprecated("spurious_fake_state"); | 127 SetStateDeprecated("spurious_fake_state"); |
| 128 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); |
120 | 129 |
121 // Pretend Chrome shut down. | 130 // Pretend Chrome shut down. |
122 ResetNotifier(); | 131 ResetNotifier(); |
123 | 132 |
124 CreateAndObserveNotifier("fake_state"); | 133 CreateAndObserveNotifier("fake_state"); |
125 // Should do nothing. | 134 // Should do nothing. |
126 invalidation_notifier_->SetStateDeprecated("more_spurious_fake_state"); | 135 SetStateDeprecated("more_spurious_fake_state"); |
| 136 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); |
127 } | 137 } |
128 | 138 |
129 } // namespace | 139 } // namespace |
130 | 140 |
131 } // namespace syncer | 141 } // namespace syncer |
OLD | NEW |