Chromium Code Reviews| Index: sync/notifier/non_blocking_invalidator_unittest.cc |
| diff --git a/sync/notifier/non_blocking_invalidator_unittest.cc b/sync/notifier/non_blocking_invalidator_unittest.cc |
| index 8704cd2cc7f1268bffa10050a9a10032b690a3e4..2c245698fc9a30e3df273a72e206b14ae6728316 100644 |
| --- a/sync/notifier/non_blocking_invalidator_unittest.cc |
| +++ b/sync/notifier/non_blocking_invalidator_unittest.cc |
| @@ -7,102 +7,101 @@ |
| #include "base/memory/ref_counted.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/message_loop.h" |
| +#include "base/run_loop.h" |
| #include "base/threading/thread.h" |
| #include "google/cacheinvalidation/types.pb.h" |
| #include "jingle/notifier/base/fake_base_task.h" |
| #include "net/url_request/url_request_test_util.h" |
| -#include "sync/internal_api/public/base/model_type.h" |
| -#include "sync/internal_api/public/base/model_type_state_map.h" |
| #include "sync/internal_api/public/util/weak_handle.h" |
| #include "sync/notifier/fake_invalidation_handler.h" |
| #include "sync/notifier/invalidation_state_tracker.h" |
| +#include "sync/notifier/invalidator_test_template.h" |
| #include "sync/notifier/object_id_state_map_test_util.h" |
| -#include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace syncer { |
| namespace { |
| -using ::testing::InSequence; |
| -using ::testing::StrictMock; |
| +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.
|
| -class NonBlockingInvalidatorTest : public testing::Test { |
| +class NonBlockingInvalidatorTestDelegate { |
| public: |
| - NonBlockingInvalidatorTest() : io_thread_("Test IO thread") {} |
| + NonBlockingInvalidatorTestDelegate() : io_thread_("IO thread") {} |
| - protected: |
| - virtual void SetUp() { |
| + ~NonBlockingInvalidatorTestDelegate() { |
| + DestroyInvalidator(); |
| + } |
| + |
| + void CreateInvalidator( |
| + const std::string& initial_state, |
| + const base::WeakPtr<InvalidationStateTracker>& |
| + invalidation_state_tracker) { |
| + DCHECK(!invalidator_.get()); |
| base::Thread::Options options; |
| options.message_loop_type = MessageLoop::TYPE_IO; |
| io_thread_.StartWithOptions(options); |
| request_context_getter_ = |
| new TestURLRequestContextGetter(io_thread_.message_loop_proxy()); |
| - notifier::NotifierOptions notifier_options; |
| - notifier_options.request_context_getter = request_context_getter_; |
| - invalidation_notifier_.reset( |
| + notifier::NotifierOptions invalidator_options; |
| + invalidator_options.request_context_getter = request_context_getter_; |
| + invalidator_.reset( |
| new NonBlockingInvalidator( |
| - notifier_options, |
| + invalidator_options, |
| InvalidationVersionMap(), |
| - std::string(), // initial_invalidation_state |
| - MakeWeakHandle(base::WeakPtr<InvalidationStateTracker>()), |
| + initial_state, |
| + MakeWeakHandle(invalidation_state_tracker), |
| "fake_client_info")); |
| - invalidation_notifier_->RegisterHandler(&fake_handler_); |
| } |
| - virtual void TearDown() { |
| - invalidation_notifier_->UnregisterHandler(&fake_handler_); |
| - invalidation_notifier_.reset(); |
| + Invalidator* GetInvalidator() { |
| + return invalidator_.get(); |
| + } |
| + |
| + void DestroyInvalidator() { |
| + invalidator_.reset(); |
| request_context_getter_ = NULL; |
| io_thread_.Stop(); |
| - ui_loop_.RunAllPending(); |
| + message_loop_.RunAllPending(); |
| } |
| - MessageLoop ui_loop_; |
| - base::Thread io_thread_; |
| - scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| - scoped_ptr<NonBlockingInvalidator> invalidation_notifier_; |
| - FakeInvalidationHandler fake_handler_; |
| - notifier::FakeBaseTask fake_base_task_; |
| -}; |
| - |
| -// TODO(akalin): Add real unit tests (http://crbug.com/140410). |
| - |
| -TEST_F(NonBlockingInvalidatorTest, Basic) { |
| - const ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL); |
| - const ObjectIdStateMap& id_state_map = |
| - ModelTypeStateMapToObjectIdStateMap( |
| - ModelTypeSetToStateMap(models, "payload")); |
| - |
| - invalidation_notifier_->UpdateRegisteredIds( |
| - &fake_handler_, ModelTypeSetToObjectIdSet(models)); |
| + void WaitForInvalidator() { |
| + base::RunLoop run_loop; |
| + ASSERT_TRUE( |
| + io_thread_.message_loop_proxy()->PostTaskAndReply( |
| + FROM_HERE, |
| + base::Bind(&DoNothing), |
| + run_loop.QuitClosure())); |
| + run_loop.Run(); |
| + } |
| - invalidation_notifier_->SetStateDeprecated("fake_state"); |
| - invalidation_notifier_->SetUniqueId("fake_id"); |
| - invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
| + void TriggerOnNotificationsEnabled() { |
| + invalidator_->OnNotificationsEnabled(); |
| + } |
| - invalidation_notifier_->OnNotificationsEnabled(); |
| - EXPECT_EQ(NO_NOTIFICATION_ERROR, |
| - fake_handler_.GetNotificationsDisabledReason()); |
| + void TriggerOnIncomingNotification(const ObjectIdStateMap& id_state_map, |
| + IncomingNotificationSource source) { |
| + invalidator_->OnIncomingNotification(id_state_map, source); |
| + } |
| - invalidation_notifier_->OnIncomingNotification( |
| - id_state_map, REMOTE_NOTIFICATION); |
| - EXPECT_THAT(id_state_map, |
| - Eq(fake_handler_.GetLastNotificationIdStateMap())); |
| - EXPECT_EQ(REMOTE_NOTIFICATION, fake_handler_.GetLastNotificationSource()); |
| + void TriggerOnNotificationsDisabled(NotificationsDisabledReason reason) { |
| + invalidator_->OnNotificationsDisabled(reason); |
| + } |
| - invalidation_notifier_->OnNotificationsDisabled( |
| - TRANSIENT_NOTIFICATION_ERROR); |
| - EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, |
| - fake_handler_.GetNotificationsDisabledReason()); |
| + static bool InvalidatorHandlesDeprecatedState() { |
| + return true; |
| + } |
| - invalidation_notifier_->OnNotificationsDisabled( |
| - NOTIFICATION_CREDENTIALS_REJECTED); |
| - EXPECT_EQ(NOTIFICATION_CREDENTIALS_REJECTED, |
| - fake_handler_.GetNotificationsDisabledReason()); |
| + private: |
| + MessageLoop message_loop_; |
| + base::Thread io_thread_; |
| + scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| + scoped_ptr<NonBlockingInvalidator> invalidator_; |
| +}; |
| - ui_loop_.RunAllPending(); |
| -} |
| +INSTANTIATE_TYPED_TEST_CASE_P( |
| + NonBlockingInvalidatorTest, InvalidatorTest, |
| + NonBlockingInvalidatorTestDelegate); |
| } // namespace |