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_state_map.h" | 14 #include "sync/internal_api/public/base/model_type_state_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_handler.h" |
16 #include "sync/notifier/fake_invalidation_state_tracker.h" | 17 #include "sync/notifier/fake_invalidation_state_tracker.h" |
17 #include "sync/notifier/fake_sync_notifier_observer.h" | |
18 #include "sync/notifier/invalidation_state_tracker.h" | 18 #include "sync/notifier/invalidation_state_tracker.h" |
19 #include "sync/notifier/object_id_state_map_test_util.h" | 19 #include "sync/notifier/object_id_state_map_test_util.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 | 22 |
23 namespace syncer { | 23 namespace syncer { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 using ::testing::InSequence; | 27 using ::testing::InSequence; |
28 using ::testing::StrictMock; | 28 using ::testing::StrictMock; |
29 | 29 |
30 class InvalidationNotifierTest : public testing::Test { | 30 class InvalidationNotifierTest : public testing::Test { |
31 protected: | 31 protected: |
32 virtual void TearDown() { | 32 virtual void TearDown() { |
33 if (invalidation_notifier_.get()) | 33 if (invalidation_notifier_.get()) |
34 ResetNotifier(); | 34 ResetNotifier(); |
35 } | 35 } |
36 | 36 |
37 // Constructs an InvalidationNotifier, places it in |invalidation_notifier_|, | 37 // Constructs an InvalidationNotifier, places it in |invalidation_notifier_|, |
38 // and registers |fake_observer_| as a handler. This remains in place until | 38 // and registers |fake_handler_| as a handler. This remains in place until |
39 // either TearDown (automatic) or ResetNotifier (manual) is called. | 39 // either TearDown (automatic) or ResetNotifier (manual) is called. |
40 void CreateNotifier( | 40 void CreateNotifier( |
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(fake_tracker_.AsWeakPtr()), | 51 MakeWeakHandle(fake_tracker_.AsWeakPtr()), |
52 "fake_client_info")); | 52 "fake_client_info")); |
53 invalidation_notifier_->RegisterHandler(&fake_observer_); | 53 invalidation_notifier_->RegisterHandler(&fake_handler_); |
54 } | 54 } |
55 | 55 |
56 void ResetNotifier() { | 56 void ResetNotifier() { |
57 invalidation_notifier_->UnregisterHandler(&fake_observer_); | 57 invalidation_notifier_->UnregisterHandler(&fake_handler_); |
58 // Stopping the invalidation notifier stops its scheduler, which deletes any | 58 // Stopping the invalidation notifier stops its scheduler, which deletes any |
59 // pending tasks without running them. Some tasks "run and delete" another | 59 // pending tasks without running them. Some tasks "run and delete" another |
60 // task, so they must be run in order to avoid leaking the inner task. | 60 // task, so they must be run in order to avoid leaking the inner task. |
61 // Stopping does not schedule any tasks, so it's both necessary and | 61 // Stopping does not schedule any tasks, so it's both necessary and |
62 // sufficient to drain the task queue before stopping the notifier. | 62 // sufficient to drain the task queue before stopping the notifier. |
63 message_loop_.RunAllPending(); | 63 message_loop_.RunAllPending(); |
64 invalidation_notifier_.reset(); | 64 invalidation_notifier_.reset(); |
65 } | 65 } |
66 | 66 |
67 void SetStateDeprecated(const std::string& new_state) { | 67 void SetStateDeprecated(const std::string& new_state) { |
68 invalidation_notifier_->SetStateDeprecated(new_state); | 68 invalidation_notifier_->SetStateDeprecated(new_state); |
69 message_loop_.RunAllPending(); | 69 message_loop_.RunAllPending(); |
70 } | 70 } |
71 | 71 |
72 private: | 72 private: |
73 MessageLoopForIO message_loop_; | 73 MessageLoopForIO message_loop_; |
74 notifier::FakeBaseTask fake_base_task_; | 74 notifier::FakeBaseTask fake_base_task_; |
75 | 75 |
76 protected: | 76 protected: |
77 scoped_ptr<InvalidationNotifier> invalidation_notifier_; | 77 scoped_ptr<InvalidationNotifier> invalidation_notifier_; |
78 FakeInvalidationStateTracker fake_tracker_; | 78 FakeInvalidationStateTracker fake_tracker_; |
79 FakeSyncNotifierObserver fake_observer_; | 79 FakeInvalidationHandler fake_handler_; |
80 }; | 80 }; |
81 | 81 |
82 TEST_F(InvalidationNotifierTest, Basic) { | 82 TEST_F(InvalidationNotifierTest, Basic) { |
83 CreateNotifier("fake_state"); | 83 CreateNotifier("fake_state"); |
84 | 84 |
85 const ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL); | 85 const ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL); |
86 const ObjectIdStateMap& id_state_map = | 86 const ObjectIdStateMap& id_state_map = |
87 ModelTypeStateMapToObjectIdStateMap( | 87 ModelTypeStateMapToObjectIdStateMap( |
88 ModelTypeSetToStateMap(models, "payload")); | 88 ModelTypeSetToStateMap(models, "payload")); |
89 | 89 |
90 invalidation_notifier_->UpdateRegisteredIds( | 90 invalidation_notifier_->UpdateRegisteredIds( |
91 &fake_observer_, ModelTypeSetToObjectIdSet(models)); | 91 &fake_handler_, ModelTypeSetToObjectIdSet(models)); |
92 | 92 |
93 // TODO(tim): This call should be a no-op, Remove once bug 124140 and | 93 // TODO(tim): This call should be a no-op, Remove once bug 124140 and |
94 // associated issues are fixed. | 94 // associated issues are fixed. |
95 invalidation_notifier_->SetStateDeprecated("fake_state"); | 95 invalidation_notifier_->SetStateDeprecated("fake_state"); |
96 // We don't expect |fake_tracker_|'s state to change, as we | 96 // We don't expect |fake_tracker_|'s state to change, as we |
97 // initialized with non-empty initial_invalidation_state above. | 97 // initialized with non-empty initial_invalidation_state above. |
98 EXPECT_TRUE(fake_tracker_.GetInvalidationState().empty()); | 98 EXPECT_TRUE(fake_tracker_.GetInvalidationState().empty()); |
99 invalidation_notifier_->SetUniqueId("fake_id"); | 99 invalidation_notifier_->SetUniqueId("fake_id"); |
100 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 100 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
101 | 101 |
102 invalidation_notifier_->OnNotificationsEnabled(); | 102 invalidation_notifier_->OnNotificationsEnabled(); |
103 EXPECT_EQ(NO_NOTIFICATION_ERROR, | 103 EXPECT_EQ(NO_NOTIFICATION_ERROR, |
104 fake_observer_.GetNotificationsDisabledReason()); | 104 fake_handler_.GetNotificationsDisabledReason()); |
105 | 105 |
106 invalidation_notifier_->OnInvalidate(id_state_map); | 106 invalidation_notifier_->OnInvalidate(id_state_map); |
107 EXPECT_THAT(id_state_map, | 107 EXPECT_THAT(id_state_map, |
108 Eq(fake_observer_.GetLastNotificationIdStateMap())); | 108 Eq(fake_handler_.GetLastNotificationIdStateMap())); |
109 EXPECT_EQ(REMOTE_NOTIFICATION, fake_observer_.GetLastNotificationSource()); | 109 EXPECT_EQ(REMOTE_NOTIFICATION, fake_handler_.GetLastNotificationSource()); |
110 | 110 |
111 invalidation_notifier_->OnNotificationsDisabled( | 111 invalidation_notifier_->OnNotificationsDisabled( |
112 TRANSIENT_NOTIFICATION_ERROR); | 112 TRANSIENT_NOTIFICATION_ERROR); |
113 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, | 113 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, |
114 fake_observer_.GetNotificationsDisabledReason()); | 114 fake_handler_.GetNotificationsDisabledReason()); |
115 | 115 |
116 invalidation_notifier_->OnNotificationsDisabled( | 116 invalidation_notifier_->OnNotificationsDisabled( |
117 NOTIFICATION_CREDENTIALS_REJECTED); | 117 NOTIFICATION_CREDENTIALS_REJECTED); |
118 EXPECT_EQ(NOTIFICATION_CREDENTIALS_REJECTED, | 118 EXPECT_EQ(NOTIFICATION_CREDENTIALS_REJECTED, |
119 fake_observer_.GetNotificationsDisabledReason()); | 119 fake_handler_.GetNotificationsDisabledReason()); |
120 } | 120 } |
121 | 121 |
122 TEST_F(InvalidationNotifierTest, MigrateState) { | 122 TEST_F(InvalidationNotifierTest, MigrateState) { |
123 CreateNotifier(std::string()); | 123 CreateNotifier(std::string()); |
124 | 124 |
125 SetStateDeprecated("fake_state"); | 125 SetStateDeprecated("fake_state"); |
126 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); | 126 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); |
127 | 127 |
128 // Should do nothing. | 128 // Should do nothing. |
129 SetStateDeprecated("spurious_fake_state"); | 129 SetStateDeprecated("spurious_fake_state"); |
130 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); | 130 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); |
131 | 131 |
132 // Pretend Chrome shut down. | 132 // Pretend Chrome shut down. |
133 ResetNotifier(); | 133 ResetNotifier(); |
134 | 134 |
135 CreateNotifier("fake_state"); | 135 CreateNotifier("fake_state"); |
136 // Should do nothing. | 136 // Should do nothing. |
137 SetStateDeprecated("more_spurious_fake_state"); | 137 SetStateDeprecated("more_spurious_fake_state"); |
138 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); | 138 EXPECT_EQ("fake_state", fake_tracker_.GetInvalidationState()); |
139 } | 139 } |
140 | 140 |
141 } // namespace | 141 } // namespace |
142 | 142 |
143 } // namespace syncer | 143 } // namespace syncer |
OLD | NEW |