Chromium Code Reviews| 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/invalidation_state_tracker.h" | 16 #include "sync/notifier/invalidation_state_tracker.h" |
| 17 #include "sync/notifier/mock_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::Return; | |
| 27 using ::testing::StrictMock; | 28 using ::testing::StrictMock; |
| 28 | 29 |
| 29 class InvalidationNotifierTest : public testing::Test { | 30 class InvalidationNotifierTest : public testing::Test { |
| 30 protected: | 31 protected: |
| 31 virtual void TearDown() { | 32 virtual void TearDown() { |
| 32 if (invalidation_notifier_.get()) | 33 if (invalidation_notifier_.get()) |
| 33 ResetNotifier(); | 34 ResetNotifier(); |
| 34 } | 35 } |
| 35 | 36 |
| 36 // Constructs an InvalidationNotifier, places it in | 37 // Constructs an InvalidationNotifier, places it in |
| 37 // |invalidation_notifier_|, and adds |mock_observer_| as an observer. This | 38 // |invalidation_notifier_|, and adds |mock_observer_| as an observer. This |
| 38 // remains in place until either TearDown (automatic) or ResetNotifier | 39 // remains in place until either TearDown (automatic) or ResetNotifier |
| 39 // (manual) is called. | 40 // (manual) is called. |
| 40 void CreateAndObserveNotifier( | 41 void CreateAndObserveNotifier( |
| 41 const std::string& initial_invalidation_state) { | 42 const std::string& initial_invalidation_state) { |
| 42 notifier::NotifierOptions notifier_options; | 43 notifier::NotifierOptions notifier_options; |
| 43 // Note: URLRequestContextGetters are ref-counted. | 44 // Note: URLRequestContextGetters are ref-counted. |
| 44 notifier_options.request_context_getter = | 45 notifier_options.request_context_getter = |
| 45 new TestURLRequestContextGetter(message_loop_.message_loop_proxy()); | 46 new TestURLRequestContextGetter(message_loop_.message_loop_proxy()); |
| 46 invalidation_notifier_.reset( | 47 invalidation_notifier_.reset( |
| 47 new InvalidationNotifier( | 48 new InvalidationNotifier( |
| 48 scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()), | 49 scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()), |
| 49 InvalidationVersionMap(), | 50 InvalidationVersionMap(), |
| 50 initial_invalidation_state, | 51 initial_invalidation_state, |
| 51 syncer::MakeWeakHandle(mock_tracker_.AsWeakPtr()), | 52 syncer::MakeWeakHandle(mock_tracker_.AsWeakPtr()), |
| 52 "fake_client_info")); | 53 "fake_client_info")); |
| 53 invalidation_notifier_->AddObserver(&mock_observer_); | 54 invalidation_notifier_->AddHandler(&mock_observer_); |
| 54 } | 55 } |
| 55 | 56 |
| 56 void ResetNotifier() { | 57 void ResetNotifier() { |
| 57 invalidation_notifier_->RemoveObserver(&mock_observer_); | 58 invalidation_notifier_->RemoveHandler(&mock_observer_); |
| 58 // Stopping the invalidation notifier stops its scheduler, which deletes any | 59 // Stopping the invalidation notifier stops its scheduler, which deletes any |
| 59 // pending tasks without running them. Some tasks "run and delete" another | 60 // 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. | 61 // 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 | 62 // Stopping does not schedule any tasks, so it's both necessary and |
| 62 // sufficient to drain the task queue before stopping the notifier. | 63 // sufficient to drain the task queue before stopping the notifier. |
| 63 message_loop_.RunAllPending(); | 64 message_loop_.RunAllPending(); |
| 64 invalidation_notifier_.reset(); | 65 invalidation_notifier_.reset(); |
| 65 } | 66 } |
| 66 | 67 |
| 67 MessageLoopForIO message_loop_; | 68 MessageLoopForIO message_loop_; |
| 68 scoped_ptr<InvalidationNotifier> invalidation_notifier_; | 69 scoped_ptr<InvalidationNotifier> invalidation_notifier_; |
| 69 StrictMock<MockInvalidationStateTracker> mock_tracker_; | 70 StrictMock<MockInvalidationStateTracker> mock_tracker_; |
| 70 StrictMock<MockSyncNotifierObserver> mock_observer_; | 71 StrictMock<MockSyncNotifierObserver> mock_observer_; |
| 71 notifier::FakeBaseTask fake_base_task_; | 72 notifier::FakeBaseTask fake_base_task_; |
| 72 }; | 73 }; |
| 73 | 74 |
| 75 // TODO(dcheng): Need to add test cases testing attempts to add observers | |
| 76 // which register for the same object IDs as well as removing an observer | |
| 77 // clearing its object IDs. | |
|
dcheng
2012/07/10 19:15:25
I expect to put these new test cases in Invalidati
| |
| 78 | |
| 74 TEST_F(InvalidationNotifierTest, Basic) { | 79 TEST_F(InvalidationNotifierTest, Basic) { |
| 75 CreateAndObserveNotifier("fake_state"); | 80 CreateAndObserveNotifier("fake_state"); |
| 76 InSequence dummy; | 81 InSequence dummy; |
| 77 | 82 |
| 78 syncer::ModelTypePayloadMap type_payloads; | 83 ModelTypeSet models(syncer::PREFERENCES, |
| 79 type_payloads[syncer::PREFERENCES] = "payload"; | 84 syncer::BOOKMARKS, |
| 80 type_payloads[syncer::BOOKMARKS] = "payload"; | 85 syncer::AUTOFILL); |
| 81 type_payloads[syncer::AUTOFILL] = "payload"; | 86 ModelTypePayloadMap type_payloads = |
| 87 ModelTypePayloadMapFromEnumSet(models, "payload"); | |
| 88 | |
| 89 EXPECT_CALL(mock_observer_, GetHandledIds()) | |
| 90 .WillOnce(Return(ModelTypeSetToObjectIdSet(models))); | |
| 91 invalidation_notifier_->ReloadHandlers(); | |
| 82 | 92 |
| 83 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 93 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
| 84 EXPECT_CALL(mock_observer_, | 94 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
| 85 OnIncomingNotification(type_payloads, | 95 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), |
| 86 REMOTE_NOTIFICATION)); | 96 REMOTE_NOTIFICATION)); |
| 87 EXPECT_CALL(mock_observer_, | 97 EXPECT_CALL(mock_observer_, |
| 88 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); | 98 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); |
| 89 EXPECT_CALL(mock_observer_, | 99 EXPECT_CALL(mock_observer_, |
| 90 OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED)); | 100 OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED)); |
| 91 // Note no expectation on mock_tracker_, as we initialized with | 101 // Note no expectation on mock_tracker_, as we initialized with |
| 92 // non-empty initial_invalidation_state above. | 102 // non-empty initial_invalidation_state above. |
| 93 | 103 |
| 94 // TODO(tim): This call should be a no-op, Remove once bug 124140 and | 104 // TODO(tim): This call should be a no-op, Remove once bug 124140 and |
| 95 // associated issues are fixed. | 105 // associated issues are fixed. |
| 96 invalidation_notifier_->SetStateDeprecated("fake_state"); | 106 invalidation_notifier_->SetStateDeprecated("fake_state"); |
| 97 invalidation_notifier_->SetUniqueId("fake_id"); | 107 invalidation_notifier_->SetUniqueId("fake_id"); |
| 98 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 108 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
| 99 | 109 |
| 100 invalidation_notifier_->OnNotificationsEnabled(); | 110 invalidation_notifier_->OnNotificationsEnabled(); |
| 101 | 111 |
| 102 ObjectIdPayloadMap id_payloads; | 112 invalidation_notifier_->OnInvalidate( |
| 103 for (syncer::ModelTypePayloadMap::const_iterator it = type_payloads.begin(); | 113 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads)); |
| 104 it != type_payloads.end(); ++it) { | |
| 105 invalidation::ObjectId id; | |
| 106 ASSERT_TRUE(RealModelTypeToObjectId(it->first, &id)); | |
| 107 id_payloads[id] = "payload"; | |
| 108 } | |
| 109 invalidation_notifier_->OnInvalidate(id_payloads); | |
| 110 | 114 |
| 111 invalidation_notifier_->OnNotificationsDisabled( | 115 invalidation_notifier_->OnNotificationsDisabled( |
| 112 TRANSIENT_NOTIFICATION_ERROR); | 116 TRANSIENT_NOTIFICATION_ERROR); |
| 113 invalidation_notifier_->OnNotificationsDisabled( | 117 invalidation_notifier_->OnNotificationsDisabled( |
| 114 NOTIFICATION_CREDENTIALS_REJECTED); | 118 NOTIFICATION_CREDENTIALS_REJECTED); |
| 115 } | 119 } |
| 116 | 120 |
| 117 TEST_F(InvalidationNotifierTest, MigrateState) { | 121 TEST_F(InvalidationNotifierTest, MigrateState) { |
| 118 CreateAndObserveNotifier(std::string()); | 122 CreateAndObserveNotifier(std::string()); |
| 119 InSequence dummy; | 123 InSequence dummy; |
| 120 | 124 |
| 121 EXPECT_CALL(mock_tracker_, SetInvalidationState("fake_state")); | 125 EXPECT_CALL(mock_tracker_, SetInvalidationState("fake_state")); |
| 122 invalidation_notifier_->SetStateDeprecated("fake_state"); | 126 invalidation_notifier_->SetStateDeprecated("fake_state"); |
| 123 | 127 |
| 124 // Should do nothing. | 128 // Should do nothing. |
| 125 invalidation_notifier_->SetStateDeprecated("spurious_fake_state"); | 129 invalidation_notifier_->SetStateDeprecated("spurious_fake_state"); |
| 126 | 130 |
| 127 // Pretend Chrome shut down. | 131 // Pretend Chrome shut down. |
| 128 ResetNotifier(); | 132 ResetNotifier(); |
| 129 | 133 |
| 130 CreateAndObserveNotifier("fake_state"); | 134 CreateAndObserveNotifier("fake_state"); |
| 131 // Should do nothing. | 135 // Should do nothing. |
| 132 invalidation_notifier_->SetStateDeprecated("more_spurious_fake_state"); | 136 invalidation_notifier_->SetStateDeprecated("more_spurious_fake_state"); |
| 133 } | 137 } |
| 134 | 138 |
| 135 } // namespace | 139 } // namespace |
| 136 | 140 |
| 137 } // namespace syncer | 141 } // namespace syncer |
| OLD | NEW |