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/non_blocking_invalidator.h" | 5 #include "sync/notifier/non_blocking_invalidator.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/run_loop.h" | |
| 10 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
| 11 #include "google/cacheinvalidation/types.pb.h" | 12 #include "google/cacheinvalidation/types.pb.h" |
| 12 #include "jingle/notifier/base/fake_base_task.h" | 13 #include "jingle/notifier/base/fake_base_task.h" |
| 13 #include "net/url_request/url_request_test_util.h" | 14 #include "net/url_request/url_request_test_util.h" |
| 14 #include "sync/internal_api/public/base/model_type.h" | |
| 15 #include "sync/internal_api/public/base/model_type_state_map.h" | |
| 16 #include "sync/internal_api/public/util/weak_handle.h" | 15 #include "sync/internal_api/public/util/weak_handle.h" |
| 17 #include "sync/notifier/fake_invalidation_handler.h" | 16 #include "sync/notifier/fake_invalidation_handler.h" |
| 18 #include "sync/notifier/invalidation_state_tracker.h" | 17 #include "sync/notifier/invalidation_state_tracker.h" |
| 18 #include "sync/notifier/invalidator_test_template.h" | |
| 19 #include "sync/notifier/object_id_state_map_test_util.h" | 19 #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" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 21 |
| 23 namespace syncer { | 22 namespace syncer { |
| 24 | 23 |
| 25 namespace { | 24 namespace { |
| 26 | 25 |
| 27 using ::testing::InSequence; | 26 void DoNothing() {} |
|
Nicolas Zea
2012/08/31 17:44:25
Comment that this is needed to block sync thread.
akalin
2012/08/31 21:06:33
Done.
| |
| 28 using ::testing::StrictMock; | |
| 29 | 27 |
| 30 class NonBlockingInvalidatorTest : public testing::Test { | 28 class NonBlockingInvalidatorTestDelegate { |
| 31 public: | 29 public: |
| 32 NonBlockingInvalidatorTest() : io_thread_("Test IO thread") {} | 30 NonBlockingInvalidatorTestDelegate() : io_thread_("IO thread") {} |
| 33 | 31 |
| 34 protected: | 32 ~NonBlockingInvalidatorTestDelegate() { |
| 35 virtual void SetUp() { | 33 DestroyInvalidator(); |
| 34 } | |
| 35 | |
| 36 void CreateInvalidator( | |
| 37 const std::string& initial_state, | |
| 38 const base::WeakPtr<InvalidationStateTracker>& | |
| 39 invalidation_state_tracker) { | |
| 40 DCHECK(!invalidator_.get()); | |
| 36 base::Thread::Options options; | 41 base::Thread::Options options; |
| 37 options.message_loop_type = MessageLoop::TYPE_IO; | 42 options.message_loop_type = MessageLoop::TYPE_IO; |
| 38 io_thread_.StartWithOptions(options); | 43 io_thread_.StartWithOptions(options); |
| 39 request_context_getter_ = | 44 request_context_getter_ = |
| 40 new TestURLRequestContextGetter(io_thread_.message_loop_proxy()); | 45 new TestURLRequestContextGetter(io_thread_.message_loop_proxy()); |
| 41 notifier::NotifierOptions notifier_options; | 46 notifier::NotifierOptions invalidator_options; |
| 42 notifier_options.request_context_getter = request_context_getter_; | 47 invalidator_options.request_context_getter = request_context_getter_; |
| 43 invalidation_notifier_.reset( | 48 invalidator_.reset( |
| 44 new NonBlockingInvalidator( | 49 new NonBlockingInvalidator( |
| 45 notifier_options, | 50 invalidator_options, |
| 46 InvalidationVersionMap(), | 51 InvalidationVersionMap(), |
| 47 std::string(), // initial_invalidation_state | 52 initial_state, |
| 48 MakeWeakHandle(base::WeakPtr<InvalidationStateTracker>()), | 53 MakeWeakHandle(invalidation_state_tracker), |
| 49 "fake_client_info")); | 54 "fake_client_info")); |
| 50 invalidation_notifier_->RegisterHandler(&fake_handler_); | |
| 51 } | 55 } |
| 52 | 56 |
| 53 virtual void TearDown() { | 57 Invalidator* GetInvalidator() { |
| 54 invalidation_notifier_->UnregisterHandler(&fake_handler_); | 58 return invalidator_.get(); |
| 55 invalidation_notifier_.reset(); | 59 } |
| 60 | |
| 61 void DestroyInvalidator() { | |
| 62 invalidator_.reset(); | |
| 56 request_context_getter_ = NULL; | 63 request_context_getter_ = NULL; |
| 57 io_thread_.Stop(); | 64 io_thread_.Stop(); |
| 58 ui_loop_.RunAllPending(); | 65 message_loop_.RunAllPending(); |
| 59 } | 66 } |
| 60 | 67 |
| 61 MessageLoop ui_loop_; | 68 void WaitForInvalidator() { |
| 69 base::RunLoop run_loop; | |
| 70 ASSERT_TRUE( | |
| 71 io_thread_.message_loop_proxy()->PostTaskAndReply( | |
| 72 FROM_HERE, | |
| 73 base::Bind(&DoNothing), | |
| 74 run_loop.QuitClosure())); | |
| 75 run_loop.Run(); | |
| 76 } | |
| 77 | |
| 78 void TriggerOnNotificationsEnabled() { | |
| 79 invalidator_->OnNotificationsEnabled(); | |
| 80 } | |
| 81 | |
| 82 void TriggerOnIncomingNotification(const ObjectIdStateMap& id_state_map, | |
| 83 IncomingNotificationSource source) { | |
| 84 invalidator_->OnIncomingNotification(id_state_map, source); | |
| 85 } | |
| 86 | |
| 87 void TriggerOnNotificationsDisabled(NotificationsDisabledReason reason) { | |
| 88 invalidator_->OnNotificationsDisabled(reason); | |
| 89 } | |
| 90 | |
| 91 static bool InvalidatorHandlesDeprecatedState() { | |
| 92 return true; | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 MessageLoop message_loop_; | |
| 62 base::Thread io_thread_; | 97 base::Thread io_thread_; |
| 63 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 98 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 64 scoped_ptr<NonBlockingInvalidator> invalidation_notifier_; | 99 scoped_ptr<NonBlockingInvalidator> invalidator_; |
| 65 FakeInvalidationHandler fake_handler_; | |
| 66 notifier::FakeBaseTask fake_base_task_; | |
| 67 }; | 100 }; |
| 68 | 101 |
| 69 // TODO(akalin): Add real unit tests (http://crbug.com/140410). | 102 INSTANTIATE_TYPED_TEST_CASE_P( |
| 70 | 103 NonBlockingInvalidatorTest, InvalidatorTest, |
| 71 TEST_F(NonBlockingInvalidatorTest, Basic) { | 104 NonBlockingInvalidatorTestDelegate); |
| 72 const ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL); | |
| 73 const ObjectIdStateMap& id_state_map = | |
| 74 ModelTypeStateMapToObjectIdStateMap( | |
| 75 ModelTypeSetToStateMap(models, "payload")); | |
| 76 | |
| 77 invalidation_notifier_->UpdateRegisteredIds( | |
| 78 &fake_handler_, ModelTypeSetToObjectIdSet(models)); | |
| 79 | |
| 80 invalidation_notifier_->SetStateDeprecated("fake_state"); | |
| 81 invalidation_notifier_->SetUniqueId("fake_id"); | |
| 82 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | |
| 83 | |
| 84 invalidation_notifier_->OnNotificationsEnabled(); | |
| 85 EXPECT_EQ(NO_NOTIFICATION_ERROR, | |
| 86 fake_handler_.GetNotificationsDisabledReason()); | |
| 87 | |
| 88 invalidation_notifier_->OnIncomingNotification( | |
| 89 id_state_map, REMOTE_NOTIFICATION); | |
| 90 EXPECT_THAT(id_state_map, | |
| 91 Eq(fake_handler_.GetLastNotificationIdStateMap())); | |
| 92 EXPECT_EQ(REMOTE_NOTIFICATION, fake_handler_.GetLastNotificationSource()); | |
| 93 | |
| 94 invalidation_notifier_->OnNotificationsDisabled( | |
| 95 TRANSIENT_NOTIFICATION_ERROR); | |
| 96 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, | |
| 97 fake_handler_.GetNotificationsDisabledReason()); | |
| 98 | |
| 99 invalidation_notifier_->OnNotificationsDisabled( | |
| 100 NOTIFICATION_CREDENTIALS_REJECTED); | |
| 101 EXPECT_EQ(NOTIFICATION_CREDENTIALS_REJECTED, | |
| 102 fake_handler_.GetNotificationsDisabledReason()); | |
| 103 | |
| 104 ui_loop_.RunAllPending(); | |
| 105 } | |
| 106 | 105 |
| 107 } // namespace | 106 } // namespace |
| 108 | 107 |
| 109 } // namespace syncer | 108 } // namespace syncer |
| OLD | NEW |