| 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 and places it in |
| 37 // |invalidation_notifier_|, and adds |mock_observer_| as an observer. This | 37 // |invalidation_notifier_|. This remains in place until either TearDown |
| 38 // remains in place until either TearDown (automatic) or ResetNotifier | 38 // (automatic) or ResetNotifier (manual) is called. |
| 39 // (manual) is called. | 39 void CreateNotifier(const std::string& initial_invalidation_state) { |
| 40 void CreateAndObserveNotifier( | |
| 41 const std::string& initial_invalidation_state) { | |
| 42 notifier::NotifierOptions notifier_options; | 40 notifier::NotifierOptions notifier_options; |
| 43 // Note: URLRequestContextGetters are ref-counted. | 41 // Note: URLRequestContextGetters are ref-counted. |
| 44 notifier_options.request_context_getter = | 42 notifier_options.request_context_getter = |
| 45 new TestURLRequestContextGetter(message_loop_.message_loop_proxy()); | 43 new TestURLRequestContextGetter(message_loop_.message_loop_proxy()); |
| 46 invalidation_notifier_.reset( | 44 invalidation_notifier_.reset( |
| 47 new InvalidationNotifier( | 45 new InvalidationNotifier( |
| 48 scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()), | 46 scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()), |
| 49 InvalidationVersionMap(), | 47 InvalidationVersionMap(), |
| 50 initial_invalidation_state, | 48 initial_invalidation_state, |
| 51 MakeWeakHandle(mock_tracker_.AsWeakPtr()), | 49 MakeWeakHandle(mock_tracker_.AsWeakPtr()), |
| 52 "fake_client_info")); | 50 "fake_client_info")); |
| 51 invalidation_notifier_->RegisterHandler(&mock_observer_); |
| 53 } | 52 } |
| 54 | 53 |
| 55 void ResetNotifier() { | 54 void ResetNotifier() { |
| 56 invalidation_notifier_->UpdateRegisteredIds(&mock_observer_, ObjectIdSet()); | 55 invalidation_notifier_->UnregisterHandler(&mock_observer_); |
| 57 // Stopping the invalidation notifier stops its scheduler, which deletes any | 56 // Stopping the invalidation notifier stops its scheduler, which deletes any |
| 58 // pending tasks without running them. Some tasks "run and delete" another | 57 // 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. | 58 // 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 | 59 // Stopping does not schedule any tasks, so it's both necessary and |
| 61 // sufficient to drain the task queue before stopping the notifier. | 60 // sufficient to drain the task queue before stopping the notifier. |
| 62 message_loop_.RunAllPending(); | 61 message_loop_.RunAllPending(); |
| 63 invalidation_notifier_.reset(); | 62 invalidation_notifier_.reset(); |
| 64 } | 63 } |
| 65 | 64 |
| 66 MessageLoopForIO message_loop_; | 65 MessageLoopForIO message_loop_; |
| 67 scoped_ptr<InvalidationNotifier> invalidation_notifier_; | 66 scoped_ptr<InvalidationNotifier> invalidation_notifier_; |
| 68 StrictMock<MockInvalidationStateTracker> mock_tracker_; | 67 StrictMock<MockInvalidationStateTracker> mock_tracker_; |
| 69 StrictMock<MockSyncNotifierObserver> mock_observer_; | 68 StrictMock<MockSyncNotifierObserver> mock_observer_; |
| 70 notifier::FakeBaseTask fake_base_task_; | 69 notifier::FakeBaseTask fake_base_task_; |
| 71 }; | 70 }; |
| 72 | 71 |
| 73 TEST_F(InvalidationNotifierTest, Basic) { | 72 TEST_F(InvalidationNotifierTest, Basic) { |
| 74 CreateAndObserveNotifier("fake_state"); | |
| 75 InSequence dummy; | 73 InSequence dummy; |
| 76 | 74 |
| 77 ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL); | 75 CreateNotifier("fake_state"); |
| 78 invalidation_notifier_->UpdateRegisteredIds( | |
| 79 &mock_observer_, ModelTypeSetToObjectIdSet(models)); | |
| 80 | 76 |
| 77 const ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL); |
| 81 const ModelTypePayloadMap& type_payloads = | 78 const ModelTypePayloadMap& type_payloads = |
| 82 ModelTypePayloadMapFromEnumSet(models, "payload"); | 79 ModelTypePayloadMapFromEnumSet(models, "payload"); |
| 83 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 80 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
| 84 EXPECT_CALL(mock_observer_, OnIncomingNotification( | 81 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
| 85 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), | 82 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), |
| 86 REMOTE_NOTIFICATION)); | 83 REMOTE_NOTIFICATION)); |
| 87 EXPECT_CALL(mock_observer_, | 84 EXPECT_CALL(mock_observer_, |
| 88 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); | 85 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); |
| 89 EXPECT_CALL(mock_observer_, | 86 EXPECT_CALL(mock_observer_, |
| 90 OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED)); | 87 OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED)); |
| 88 |
| 89 invalidation_notifier_->UpdateRegisteredIds( |
| 90 &mock_observer_, ModelTypeSetToObjectIdSet(models)); |
| 91 |
| 91 // Note no expectation on mock_tracker_, as we initialized with | 92 // Note no expectation on mock_tracker_, as we initialized with |
| 92 // non-empty initial_invalidation_state above. | 93 // non-empty initial_invalidation_state above. |
| 93 | 94 |
| 94 // TODO(tim): This call should be a no-op, Remove once bug 124140 and | 95 // TODO(tim): This call should be a no-op, Remove once bug 124140 and |
| 95 // associated issues are fixed. | 96 // associated issues are fixed. |
| 96 invalidation_notifier_->SetStateDeprecated("fake_state"); | 97 invalidation_notifier_->SetStateDeprecated("fake_state"); |
| 97 invalidation_notifier_->SetUniqueId("fake_id"); | 98 invalidation_notifier_->SetUniqueId("fake_id"); |
| 98 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 99 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
| 99 | 100 |
| 100 invalidation_notifier_->OnNotificationsEnabled(); | 101 invalidation_notifier_->OnNotificationsEnabled(); |
| 101 | 102 |
| 102 invalidation_notifier_->OnInvalidate( | 103 invalidation_notifier_->OnInvalidate( |
| 103 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads)); | 104 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads)); |
| 104 | 105 |
| 105 invalidation_notifier_->OnNotificationsDisabled( | 106 invalidation_notifier_->OnNotificationsDisabled( |
| 106 TRANSIENT_NOTIFICATION_ERROR); | 107 TRANSIENT_NOTIFICATION_ERROR); |
| 107 invalidation_notifier_->OnNotificationsDisabled( | 108 invalidation_notifier_->OnNotificationsDisabled( |
| 108 NOTIFICATION_CREDENTIALS_REJECTED); | 109 NOTIFICATION_CREDENTIALS_REJECTED); |
| 109 } | 110 } |
| 110 | 111 |
| 111 TEST_F(InvalidationNotifierTest, MigrateState) { | 112 TEST_F(InvalidationNotifierTest, MigrateState) { |
| 112 CreateAndObserveNotifier(std::string()); | |
| 113 InSequence dummy; | 113 InSequence dummy; |
| 114 | 114 |
| 115 CreateNotifier(std::string()); |
| 116 |
| 115 EXPECT_CALL(mock_tracker_, SetInvalidationState("fake_state")); | 117 EXPECT_CALL(mock_tracker_, SetInvalidationState("fake_state")); |
| 116 invalidation_notifier_->SetStateDeprecated("fake_state"); | 118 invalidation_notifier_->SetStateDeprecated("fake_state"); |
| 117 | 119 |
| 118 // Should do nothing. | 120 // Should do nothing. |
| 119 invalidation_notifier_->SetStateDeprecated("spurious_fake_state"); | 121 invalidation_notifier_->SetStateDeprecated("spurious_fake_state"); |
| 120 | 122 |
| 121 // Pretend Chrome shut down. | 123 // Pretend Chrome shut down. |
| 122 ResetNotifier(); | 124 ResetNotifier(); |
| 123 | 125 |
| 124 CreateAndObserveNotifier("fake_state"); | 126 CreateNotifier("fake_state"); |
| 125 // Should do nothing. | 127 // Should do nothing. |
| 126 invalidation_notifier_->SetStateDeprecated("more_spurious_fake_state"); | 128 invalidation_notifier_->SetStateDeprecated("more_spurious_fake_state"); |
| 127 } | 129 } |
| 128 | 130 |
| 129 } // namespace | 131 } // namespace |
| 130 | 132 |
| 131 } // namespace syncer | 133 } // namespace syncer |
| OLD | NEW |