| 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/notifier/invalidation_state_tracker.h" | 13 #include "sync/notifier/invalidation_state_tracker.h" |
| 14 #include "sync/notifier/mock_invalidation_state_tracker.h" |
| 14 #include "sync/notifier/mock_sync_notifier_observer.h" | 15 #include "sync/notifier/mock_sync_notifier_observer.h" |
| 15 #include "sync/syncable/model_type.h" | 16 #include "sync/syncable/model_type.h" |
| 16 #include "sync/syncable/model_type_payload_map.h" | 17 #include "sync/syncable/model_type_payload_map.h" |
| 17 #include "sync/util/weak_handle.h" | 18 #include "sync/util/weak_handle.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 21 |
| 21 namespace sync_notifier { | 22 namespace sync_notifier { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 using ::testing::InSequence; | 26 using ::testing::InSequence; |
| 26 using ::testing::StrictMock; | 27 using ::testing::StrictMock; |
| 27 | 28 |
| 28 class InvalidationNotifierTest : public testing::Test { | 29 class InvalidationNotifierTest : public testing::Test { |
| 29 protected: | 30 protected: |
| 30 virtual void SetUp() { | 31 virtual void TearDown() { |
| 32 if (invalidation_notifier_.get()) |
| 33 ResetNotifier(); |
| 34 } |
| 35 |
| 36 // Constructs an InvalidationNotifier, places it in |
| 37 // |invalidation_notifier_|, and adds |mock_observer_| as an observer. This |
| 38 // remains in place until either TearDown (automatic) or ResetNotifier |
| 39 // (manual) is called. |
| 40 void CreateAndObserveNotifier( |
| 41 const std::string& initial_invalidation_state) { |
| 31 notifier::NotifierOptions notifier_options; | 42 notifier::NotifierOptions notifier_options; |
| 32 // Note: URLRequestContextGetters are ref-counted. | 43 // Note: URLRequestContextGetters are ref-counted. |
| 33 notifier_options.request_context_getter = | 44 notifier_options.request_context_getter = |
| 34 new TestURLRequestContextGetter(message_loop_.message_loop_proxy()); | 45 new TestURLRequestContextGetter(message_loop_.message_loop_proxy()); |
| 35 invalidation_notifier_.reset( | 46 invalidation_notifier_.reset( |
| 36 new InvalidationNotifier( | 47 new InvalidationNotifier( |
| 37 scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()), | 48 scoped_ptr<notifier::PushClient>(new notifier::FakePushClient()), |
| 38 InvalidationVersionMap(), | 49 InvalidationVersionMap(), |
| 39 browser_sync::MakeWeakHandle( | 50 initial_invalidation_state, |
| 40 base::WeakPtr<InvalidationStateTracker>()), | 51 browser_sync::MakeWeakHandle(mock_tracker_.AsWeakPtr()), |
| 41 "fake_client_info")); | 52 "fake_client_info")); |
| 42 invalidation_notifier_->AddObserver(&mock_observer_); | 53 invalidation_notifier_->AddObserver(&mock_observer_); |
| 43 } | 54 } |
| 44 | 55 |
| 45 virtual void TearDown() { | 56 void ResetNotifier() { |
| 46 invalidation_notifier_->RemoveObserver(&mock_observer_); | 57 invalidation_notifier_->RemoveObserver(&mock_observer_); |
| 47 // Stopping the invalidation notifier stops its scheduler, which deletes any | 58 // Stopping the invalidation notifier stops its scheduler, which deletes any |
| 48 // pending tasks without running them. Some tasks "run and delete" another | 59 // pending tasks without running them. Some tasks "run and delete" another |
| 49 // 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. |
| 50 // 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 |
| 51 // sufficient to drain the task queue before stopping the notifier. | 62 // sufficient to drain the task queue before stopping the notifier. |
| 52 message_loop_.RunAllPending(); | 63 message_loop_.RunAllPending(); |
| 53 invalidation_notifier_.reset(); | 64 invalidation_notifier_.reset(); |
| 54 } | 65 } |
| 55 | 66 |
| 56 MessageLoopForIO message_loop_; | 67 MessageLoopForIO message_loop_; |
| 57 scoped_ptr<InvalidationNotifier> invalidation_notifier_; | 68 scoped_ptr<InvalidationNotifier> invalidation_notifier_; |
| 69 StrictMock<MockInvalidationStateTracker> mock_tracker_; |
| 58 StrictMock<MockSyncNotifierObserver> mock_observer_; | 70 StrictMock<MockSyncNotifierObserver> mock_observer_; |
| 59 notifier::FakeBaseTask fake_base_task_; | 71 notifier::FakeBaseTask fake_base_task_; |
| 60 }; | 72 }; |
| 61 | 73 |
| 62 TEST_F(InvalidationNotifierTest, Basic) { | 74 TEST_F(InvalidationNotifierTest, Basic) { |
| 75 CreateAndObserveNotifier("fake_state"); |
| 63 InSequence dummy; | 76 InSequence dummy; |
| 64 | 77 |
| 65 syncable::ModelTypePayloadMap type_payloads; | 78 syncable::ModelTypePayloadMap type_payloads; |
| 66 type_payloads[syncable::PREFERENCES] = "payload"; | 79 type_payloads[syncable::PREFERENCES] = "payload"; |
| 67 type_payloads[syncable::BOOKMARKS] = ""; | 80 type_payloads[syncable::BOOKMARKS] = ""; |
| 68 type_payloads[syncable::AUTOFILL] = ""; | 81 type_payloads[syncable::AUTOFILL] = ""; |
| 69 | 82 |
| 70 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); | 83 EXPECT_CALL(mock_observer_, OnNotificationStateChange(true)); |
| 71 EXPECT_CALL(mock_observer_, StoreState("new_fake_state")); | |
| 72 EXPECT_CALL(mock_observer_, | 84 EXPECT_CALL(mock_observer_, |
| 73 OnIncomingNotification(type_payloads, | 85 OnIncomingNotification(type_payloads, |
| 74 REMOTE_NOTIFICATION)); | 86 REMOTE_NOTIFICATION)); |
| 75 EXPECT_CALL(mock_observer_, OnNotificationStateChange(false)); | 87 EXPECT_CALL(mock_observer_, OnNotificationStateChange(false)); |
| 88 // Note no expectation on mock_tracker_, as we initialized with |
| 89 // non-empty initial_invalidation_state above. |
| 76 | 90 |
| 77 invalidation_notifier_->SetState("fake_state"); | 91 // TODO(tim): This call should be a no-op, Remove once bug 124140 and |
| 92 // associated issues are fixed. |
| 93 invalidation_notifier_->SetStateDeprecated("fake_state"); |
| 78 invalidation_notifier_->SetUniqueId("fake_id"); | 94 invalidation_notifier_->SetUniqueId("fake_id"); |
| 79 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 95 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
| 80 | 96 |
| 81 invalidation_notifier_->OnSessionStatusChanged(true); | 97 invalidation_notifier_->OnSessionStatusChanged(true); |
| 82 | 98 |
| 83 invalidation_notifier_->WriteState("new_fake_state"); | |
| 84 | |
| 85 invalidation_notifier_->OnInvalidate(type_payloads); | 99 invalidation_notifier_->OnInvalidate(type_payloads); |
| 86 | 100 |
| 87 invalidation_notifier_->OnSessionStatusChanged(false); | 101 invalidation_notifier_->OnSessionStatusChanged(false); |
| 88 } | 102 } |
| 89 | 103 |
| 104 TEST_F(InvalidationNotifierTest, MigrateState) { |
| 105 CreateAndObserveNotifier(std::string()); |
| 106 InSequence dummy; |
| 107 |
| 108 EXPECT_CALL(mock_tracker_, SetInvalidationState("fake_state")); |
| 109 invalidation_notifier_->SetStateDeprecated("fake_state"); |
| 110 |
| 111 // Should do nothing. |
| 112 invalidation_notifier_->SetStateDeprecated("spurious_fake_state"); |
| 113 |
| 114 // Pretend Chrome shut down. |
| 115 ResetNotifier(); |
| 116 |
| 117 CreateAndObserveNotifier("fake_state"); |
| 118 // Should do nothing. |
| 119 invalidation_notifier_->SetStateDeprecated("more_spurious_fake_state"); |
| 120 } |
| 121 |
| 90 } // namespace | 122 } // namespace |
| 91 | 123 |
| 92 } // namespace sync_notifier | 124 } // namespace sync_notifier |
| OLD | NEW |