| 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_invalidation_notifier.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/threading/thread.h" | 10 #include "base/threading/thread.h" |
| 11 #include "google/cacheinvalidation/types.pb.h" | 11 #include "google/cacheinvalidation/types.pb.h" |
| 12 #include "jingle/notifier/base/fake_base_task.h" | 12 #include "jingle/notifier/base/fake_base_task.h" |
| 13 #include "net/url_request/url_request_test_util.h" | 13 #include "net/url_request/url_request_test_util.h" |
| 14 #include "sync/internal_api/public/base/model_type.h" | 14 #include "sync/internal_api/public/base/model_type.h" |
| 15 #include "sync/internal_api/public/base/model_type_state_map.h" | 15 #include "sync/internal_api/public/base/model_type_state_map.h" |
| 16 #include "sync/internal_api/public/util/weak_handle.h" | 16 #include "sync/internal_api/public/util/weak_handle.h" |
| 17 #include "sync/notifier/fake_sync_notifier_observer.h" | 17 #include "sync/notifier/fake_invalidation_handler.h" |
| 18 #include "sync/notifier/invalidation_state_tracker.h" | 18 #include "sync/notifier/invalidation_state_tracker.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" | 20 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 22 |
| 23 namespace syncer { | 23 namespace syncer { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 using ::testing::InSequence; | 27 using ::testing::InSequence; |
| 28 using ::testing::StrictMock; | 28 using ::testing::StrictMock; |
| 29 | 29 |
| 30 class NonBlockingInvalidationNotifierTest : public testing::Test { | 30 class NonBlockingInvalidatorTest : public testing::Test { |
| 31 public: | 31 public: |
| 32 NonBlockingInvalidationNotifierTest() : io_thread_("Test IO thread") {} | 32 NonBlockingInvalidatorTest() : io_thread_("Test IO thread") {} |
| 33 | 33 |
| 34 protected: | 34 protected: |
| 35 virtual void SetUp() { | 35 virtual void SetUp() { |
| 36 base::Thread::Options options; | 36 base::Thread::Options options; |
| 37 options.message_loop_type = MessageLoop::TYPE_IO; | 37 options.message_loop_type = MessageLoop::TYPE_IO; |
| 38 io_thread_.StartWithOptions(options); | 38 io_thread_.StartWithOptions(options); |
| 39 request_context_getter_ = | 39 request_context_getter_ = |
| 40 new TestURLRequestContextGetter(io_thread_.message_loop_proxy()); | 40 new TestURLRequestContextGetter(io_thread_.message_loop_proxy()); |
| 41 notifier::NotifierOptions notifier_options; | 41 notifier::NotifierOptions notifier_options; |
| 42 notifier_options.request_context_getter = request_context_getter_; | 42 notifier_options.request_context_getter = request_context_getter_; |
| 43 invalidation_notifier_.reset( | 43 invalidation_notifier_.reset( |
| 44 new NonBlockingInvalidationNotifier( | 44 new NonBlockingInvalidator( |
| 45 notifier_options, | 45 notifier_options, |
| 46 InvalidationVersionMap(), | 46 InvalidationVersionMap(), |
| 47 std::string(), // initial_invalidation_state | 47 std::string(), // initial_invalidation_state |
| 48 MakeWeakHandle(base::WeakPtr<InvalidationStateTracker>()), | 48 MakeWeakHandle(base::WeakPtr<InvalidationStateTracker>()), |
| 49 "fake_client_info")); | 49 "fake_client_info")); |
| 50 invalidation_notifier_->RegisterHandler(&fake_observer_); | 50 invalidation_notifier_->RegisterHandler(&fake_handler_); |
| 51 } | 51 } |
| 52 | 52 |
| 53 virtual void TearDown() { | 53 virtual void TearDown() { |
| 54 invalidation_notifier_->UnregisterHandler(&fake_observer_); | 54 invalidation_notifier_->UnregisterHandler(&fake_handler_); |
| 55 invalidation_notifier_.reset(); | 55 invalidation_notifier_.reset(); |
| 56 request_context_getter_ = NULL; | 56 request_context_getter_ = NULL; |
| 57 io_thread_.Stop(); | 57 io_thread_.Stop(); |
| 58 ui_loop_.RunAllPending(); | 58 ui_loop_.RunAllPending(); |
| 59 } | 59 } |
| 60 | 60 |
| 61 MessageLoop ui_loop_; | 61 MessageLoop ui_loop_; |
| 62 base::Thread io_thread_; | 62 base::Thread io_thread_; |
| 63 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 63 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 64 scoped_ptr<NonBlockingInvalidationNotifier> invalidation_notifier_; | 64 scoped_ptr<NonBlockingInvalidator> invalidation_notifier_; |
| 65 FakeSyncNotifierObserver fake_observer_; | 65 FakeInvalidationHandler fake_handler_; |
| 66 notifier::FakeBaseTask fake_base_task_; | 66 notifier::FakeBaseTask fake_base_task_; |
| 67 }; | 67 }; |
| 68 | 68 |
| 69 // TODO(akalin): Add real unit tests (http://crbug.com/140410). | 69 // TODO(akalin): Add real unit tests (http://crbug.com/140410). |
| 70 | 70 |
| 71 TEST_F(NonBlockingInvalidationNotifierTest, Basic) { | 71 TEST_F(NonBlockingInvalidatorTest, Basic) { |
| 72 const ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL); | 72 const ModelTypeSet models(PREFERENCES, BOOKMARKS, AUTOFILL); |
| 73 const ObjectIdStateMap& id_state_map = | 73 const ObjectIdStateMap& id_state_map = |
| 74 ModelTypeStateMapToObjectIdStateMap( | 74 ModelTypeStateMapToObjectIdStateMap( |
| 75 ModelTypeSetToStateMap(models, "payload")); | 75 ModelTypeSetToStateMap(models, "payload")); |
| 76 | 76 |
| 77 invalidation_notifier_->UpdateRegisteredIds( | 77 invalidation_notifier_->UpdateRegisteredIds( |
| 78 &fake_observer_, ModelTypeSetToObjectIdSet(models)); | 78 &fake_handler_, ModelTypeSetToObjectIdSet(models)); |
| 79 | 79 |
| 80 invalidation_notifier_->SetStateDeprecated("fake_state"); | 80 invalidation_notifier_->SetStateDeprecated("fake_state"); |
| 81 invalidation_notifier_->SetUniqueId("fake_id"); | 81 invalidation_notifier_->SetUniqueId("fake_id"); |
| 82 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); | 82 invalidation_notifier_->UpdateCredentials("foo@bar.com", "fake_token"); |
| 83 | 83 |
| 84 invalidation_notifier_->OnNotificationsEnabled(); | 84 invalidation_notifier_->OnNotificationsEnabled(); |
| 85 EXPECT_EQ(NO_NOTIFICATION_ERROR, | 85 EXPECT_EQ(NO_NOTIFICATION_ERROR, |
| 86 fake_observer_.GetNotificationsDisabledReason()); | 86 fake_handler_.GetNotificationsDisabledReason()); |
| 87 | 87 |
| 88 invalidation_notifier_->OnIncomingNotification( | 88 invalidation_notifier_->OnIncomingNotification( |
| 89 id_state_map, REMOTE_NOTIFICATION); | 89 id_state_map, REMOTE_NOTIFICATION); |
| 90 EXPECT_THAT(id_state_map, | 90 EXPECT_THAT(id_state_map, |
| 91 Eq(fake_observer_.GetLastNotificationIdStateMap())); | 91 Eq(fake_handler_.GetLastNotificationIdStateMap())); |
| 92 EXPECT_EQ(REMOTE_NOTIFICATION, fake_observer_.GetLastNotificationSource()); | 92 EXPECT_EQ(REMOTE_NOTIFICATION, fake_handler_.GetLastNotificationSource()); |
| 93 | 93 |
| 94 invalidation_notifier_->OnNotificationsDisabled( | 94 invalidation_notifier_->OnNotificationsDisabled( |
| 95 TRANSIENT_NOTIFICATION_ERROR); | 95 TRANSIENT_NOTIFICATION_ERROR); |
| 96 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, | 96 EXPECT_EQ(TRANSIENT_NOTIFICATION_ERROR, |
| 97 fake_observer_.GetNotificationsDisabledReason()); | 97 fake_handler_.GetNotificationsDisabledReason()); |
| 98 | 98 |
| 99 invalidation_notifier_->OnNotificationsDisabled( | 99 invalidation_notifier_->OnNotificationsDisabled( |
| 100 NOTIFICATION_CREDENTIALS_REJECTED); | 100 NOTIFICATION_CREDENTIALS_REJECTED); |
| 101 EXPECT_EQ(NOTIFICATION_CREDENTIALS_REJECTED, | 101 EXPECT_EQ(NOTIFICATION_CREDENTIALS_REJECTED, |
| 102 fake_observer_.GetNotificationsDisabledReason()); | 102 fake_handler_.GetNotificationsDisabledReason()); |
| 103 | 103 |
| 104 ui_loop_.RunAllPending(); | 104 ui_loop_.RunAllPending(); |
| 105 } | 105 } |
| 106 | 106 |
| 107 } // namespace | 107 } // namespace |
| 108 | 108 |
| 109 } // namespace syncer | 109 } // namespace syncer |
| OLD | NEW |