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