Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: sync/notifier/non_blocking_invalidator_unittest.cc

Issue 10874096: [Sync] Rework Invalidator-related unit tests (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // Needed by WaitForInvalidator().
28 using ::testing::StrictMock; 27 void DoNothing() {}
29 28
30 class NonBlockingInvalidatorTest : public testing::Test { 29 class NonBlockingInvalidatorTestDelegate {
31 public: 30 public:
32 NonBlockingInvalidatorTest() : io_thread_("Test IO thread") {} 31 NonBlockingInvalidatorTestDelegate() : io_thread_("IO thread") {}
33 32
34 protected: 33 ~NonBlockingInvalidatorTestDelegate() {
35 virtual void SetUp() { 34 DestroyInvalidator();
35 }
36
37 void CreateInvalidator(
38 const std::string& initial_state,
39 const base::WeakPtr<InvalidationStateTracker>&
40 invalidation_state_tracker) {
41 DCHECK(!invalidator_.get());
36 base::Thread::Options options; 42 base::Thread::Options options;
37 options.message_loop_type = MessageLoop::TYPE_IO; 43 options.message_loop_type = MessageLoop::TYPE_IO;
38 io_thread_.StartWithOptions(options); 44 io_thread_.StartWithOptions(options);
39 request_context_getter_ = 45 request_context_getter_ =
40 new TestURLRequestContextGetter(io_thread_.message_loop_proxy()); 46 new TestURLRequestContextGetter(io_thread_.message_loop_proxy());
41 notifier::NotifierOptions notifier_options; 47 notifier::NotifierOptions invalidator_options;
42 notifier_options.request_context_getter = request_context_getter_; 48 invalidator_options.request_context_getter = request_context_getter_;
43 invalidation_notifier_.reset( 49 invalidator_.reset(
44 new NonBlockingInvalidator( 50 new NonBlockingInvalidator(
45 notifier_options, 51 invalidator_options,
46 InvalidationVersionMap(), 52 InvalidationVersionMap(),
47 std::string(), // initial_invalidation_state 53 initial_state,
48 MakeWeakHandle(base::WeakPtr<InvalidationStateTracker>()), 54 MakeWeakHandle(invalidation_state_tracker),
49 "fake_client_info")); 55 "fake_client_info"));
50 invalidation_notifier_->RegisterHandler(&fake_handler_);
51 } 56 }
52 57
53 virtual void TearDown() { 58 Invalidator* GetInvalidator() {
54 invalidation_notifier_->UnregisterHandler(&fake_handler_); 59 return invalidator_.get();
55 invalidation_notifier_.reset(); 60 }
61
62 void DestroyInvalidator() {
63 invalidator_.reset();
56 request_context_getter_ = NULL; 64 request_context_getter_ = NULL;
57 io_thread_.Stop(); 65 io_thread_.Stop();
58 ui_loop_.RunAllPending(); 66 message_loop_.RunAllPending();
59 } 67 }
60 68
61 MessageLoop ui_loop_; 69 void WaitForInvalidator() {
70 base::RunLoop run_loop;
71 ASSERT_TRUE(
72 io_thread_.message_loop_proxy()->PostTaskAndReply(
73 FROM_HERE,
74 base::Bind(&DoNothing),
75 run_loop.QuitClosure()));
76 run_loop.Run();
77 }
78
79 void TriggerOnNotificationsEnabled() {
80 invalidator_->OnNotificationsEnabled();
81 }
82
83 void TriggerOnIncomingNotification(const ObjectIdStateMap& id_state_map,
84 IncomingNotificationSource source) {
85 invalidator_->OnIncomingNotification(id_state_map, source);
86 }
87
88 void TriggerOnNotificationsDisabled(NotificationsDisabledReason reason) {
89 invalidator_->OnNotificationsDisabled(reason);
90 }
91
92 static bool InvalidatorHandlesDeprecatedState() {
93 return true;
94 }
95
96 private:
97 MessageLoop message_loop_;
62 base::Thread io_thread_; 98 base::Thread io_thread_;
63 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 99 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
64 scoped_ptr<NonBlockingInvalidator> invalidation_notifier_; 100 scoped_ptr<NonBlockingInvalidator> invalidator_;
65 FakeInvalidationHandler fake_handler_;
66 notifier::FakeBaseTask fake_base_task_;
67 }; 101 };
68 102
69 // TODO(akalin): Add real unit tests (http://crbug.com/140410). 103 INSTANTIATE_TYPED_TEST_CASE_P(
70 104 NonBlockingInvalidatorTest, InvalidatorTest,
71 TEST_F(NonBlockingInvalidatorTest, Basic) { 105 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 106
107 } // namespace 107 } // namespace
108 108
109 } // namespace syncer 109 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/notifier/non_blocking_invalidator.cc ('k') | sync/notifier/notifications_disabled_reason.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698