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" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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(mock_tracker_.AsWeakPtr()), |
| 52 "fake_client_info")); | 52 "fake_client_info")); |
| 53 invalidation_notifier_->AddObserver(&mock_observer_); | |
| 54 } | 53 } |
| 55 | 54 |
| 56 void ResetNotifier() { | 55 void ResetNotifier() { |
| 57 invalidation_notifier_->RemoveObserver(&mock_observer_); | 56 invalidation_notifier_->UpdateRegisteredIds(&mock_observer_, ObjectIdSet()); |
| 58 // Stopping the invalidation notifier stops its scheduler, which deletes any | 57 // Stopping the invalidation notifier stops its scheduler, which deletes any |
| 59 // pending tasks without running them. Some tasks "run and delete" another | 58 // 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. | 59 // 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 | 60 // Stopping does not schedule any tasks, so it's both necessary and |
| 62 // sufficient to drain the task queue before stopping the notifier. | 61 // sufficient to drain the task queue before stopping the notifier. |
| 63 message_loop_.RunAllPending(); | 62 message_loop_.RunAllPending(); |
| 64 invalidation_notifier_.reset(); | 63 invalidation_notifier_.reset(); |
| 65 } | 64 } |
| 66 | 65 |
| 67 MessageLoopForIO message_loop_; | 66 MessageLoopForIO message_loop_; |
| 68 scoped_ptr<InvalidationNotifier> invalidation_notifier_; | 67 scoped_ptr<InvalidationNotifier> invalidation_notifier_; |
| 69 StrictMock<MockInvalidationStateTracker> mock_tracker_; | 68 StrictMock<MockInvalidationStateTracker> mock_tracker_; |
| 70 StrictMock<MockSyncNotifierObserver> mock_observer_; | 69 StrictMock<MockSyncNotifierObserver> mock_observer_; |
| 71 notifier::FakeBaseTask fake_base_task_; | 70 notifier::FakeBaseTask fake_base_task_; |
| 72 }; | 71 }; |
| 73 | 72 |
| 74 TEST_F(InvalidationNotifierTest, Basic) { | 73 TEST_F(InvalidationNotifierTest, Basic) { |
| 75 CreateAndObserveNotifier("fake_state"); | 74 CreateAndObserveNotifier("fake_state"); |
| 76 InSequence dummy; | 75 InSequence dummy; |
| 77 | 76 |
| 78 ModelTypePayloadMap type_payloads; | 77 ModelTypeSet models(PREFERENCES, |
|
akalin
2012/07/21 01:09:47
fit on one line?
dcheng
2012/07/21 14:06:53
Done.
| |
| 79 type_payloads[PREFERENCES] = "payload"; | 78 BOOKMARKS, |
| 80 type_payloads[BOOKMARKS] = "payload"; | 79 AUTOFILL); |
| 81 type_payloads[AUTOFILL] = "payload"; | 80 invalidation_notifier_->UpdateRegisteredIds( |
| 81 &mock_observer_, ModelTypeSetToObjectIdSet(models)); | |
| 82 | 82 |
| 83 ModelTypePayloadMap type_payloads = | |
|
akalin
2012/07/21 01:09:47
const ref
dcheng
2012/07/21 14:06:53
Done.
| |
| 84 ModelTypePayloadMapFromEnumSet(models, "payload"); | |
| 83 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); | 85 EXPECT_CALL(mock_observer_, OnNotificationsEnabled()); |
| 84 EXPECT_CALL(mock_observer_, | 86 EXPECT_CALL(mock_observer_, OnIncomingNotification( |
| 85 OnIncomingNotification(type_payloads, | 87 ModelTypePayloadMapToObjectIdPayloadMap(type_payloads), |
| 86 REMOTE_NOTIFICATION)); | 88 REMOTE_NOTIFICATION)); |
| 87 EXPECT_CALL(mock_observer_, | 89 EXPECT_CALL(mock_observer_, |
| 88 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); | 90 OnNotificationsDisabled(TRANSIENT_NOTIFICATION_ERROR)); |
| 89 EXPECT_CALL(mock_observer_, | 91 EXPECT_CALL(mock_observer_, |
| 90 OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED)); | 92 OnNotificationsDisabled(NOTIFICATION_CREDENTIALS_REJECTED)); |
| 91 // Note no expectation on mock_tracker_, as we initialized with | 93 // Note no expectation on mock_tracker_, as we initialized with |
| 92 // non-empty initial_invalidation_state above. | 94 // non-empty initial_invalidation_state above. |
| 93 | 95 |
| 94 // TODO(tim): This call should be a no-op, Remove once bug 124140 and | 96 // TODO(tim): This call should be a no-op, Remove once bug 124140 and |
| 95 // associated issues are fixed. | 97 // associated issues are fixed. |
| 96 invalidation_notifier_->SetStateDeprecated("fake_state"); | 98 invalidation_notifier_->SetStateDeprecated("fake_state"); |
| 97 invalidation_notifier_->SetUniqueId("fake_id"); | 99 invalidation_notifier_->SetUniqueId("fake_id"); |
| 98 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 100 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
| 99 | 101 |
| 100 invalidation_notifier_->OnNotificationsEnabled(); | 102 invalidation_notifier_->OnNotificationsEnabled(); |
| 101 | 103 |
| 102 ObjectIdPayloadMap id_payloads; | 104 invalidation_notifier_->OnInvalidate( |
| 103 for (ModelTypePayloadMap::const_iterator it = type_payloads.begin(); | 105 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 | 106 |
| 111 invalidation_notifier_->OnNotificationsDisabled( | 107 invalidation_notifier_->OnNotificationsDisabled( |
| 112 TRANSIENT_NOTIFICATION_ERROR); | 108 TRANSIENT_NOTIFICATION_ERROR); |
| 113 invalidation_notifier_->OnNotificationsDisabled( | 109 invalidation_notifier_->OnNotificationsDisabled( |
| 114 NOTIFICATION_CREDENTIALS_REJECTED); | 110 NOTIFICATION_CREDENTIALS_REJECTED); |
| 115 } | 111 } |
| 116 | 112 |
| 117 TEST_F(InvalidationNotifierTest, MigrateState) { | 113 TEST_F(InvalidationNotifierTest, MigrateState) { |
| 118 CreateAndObserveNotifier(std::string()); | 114 CreateAndObserveNotifier(std::string()); |
| 119 InSequence dummy; | 115 InSequence dummy; |
| 120 | 116 |
| 121 EXPECT_CALL(mock_tracker_, SetInvalidationState("fake_state")); | 117 EXPECT_CALL(mock_tracker_, SetInvalidationState("fake_state")); |
| 122 invalidation_notifier_->SetStateDeprecated("fake_state"); | 118 invalidation_notifier_->SetStateDeprecated("fake_state"); |
| 123 | 119 |
| 124 // Should do nothing. | 120 // Should do nothing. |
| 125 invalidation_notifier_->SetStateDeprecated("spurious_fake_state"); | 121 invalidation_notifier_->SetStateDeprecated("spurious_fake_state"); |
| 126 | 122 |
| 127 // Pretend Chrome shut down. | 123 // Pretend Chrome shut down. |
| 128 ResetNotifier(); | 124 ResetNotifier(); |
| 129 | 125 |
| 130 CreateAndObserveNotifier("fake_state"); | 126 CreateAndObserveNotifier("fake_state"); |
| 131 // Should do nothing. | 127 // Should do nothing. |
| 132 invalidation_notifier_->SetStateDeprecated("more_spurious_fake_state"); | 128 invalidation_notifier_->SetStateDeprecated("more_spurious_fake_state"); |
| 133 } | 129 } |
| 134 | 130 |
| 135 } // namespace | 131 } // namespace |
| 136 | 132 |
| 137 } // namespace syncer | 133 } // namespace syncer |
| OLD | NEW |