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

Side by Side Diff: chrome/browser/sync/glue/bridged_invalidator_unittest.cc

Issue 12022041: Separate local and remote sync invalidations (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 7 years, 10 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/sync/glue/bridged_invalidator.h"
6
7 #include <string>
8
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/thread.h"
13 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h"
14 #include "chrome/test/base/profile_mock.h"
15 #include "content/public/test/test_browser_thread.h"
16 #include "google/cacheinvalidation/include/types.h"
17 #include "sync/internal_api/public/base/model_type.h"
18 #include "sync/internal_api/public/base/model_type_test_util.h"
19 #include "sync/notifier/fake_invalidation_handler.h"
20 #include "sync/notifier/fake_invalidator.h"
21 #include "sync/notifier/invalidator_test_template.h"
22 #include "sync/notifier/object_id_invalidation_map_test_util.h"
23 #include "testing/gmock/include/gmock/gmock.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25
26 namespace syncer {
27 class InvalidationStateTracker;
28 } // namespace syncer
29
30 namespace browser_sync {
31
32 // BridgedInvalidatorTestDelegate has to be visible from the syncer
33 // namespace (where InvalidatorTest lives).
34 class BridgedInvalidatorTestDelegate {
35 public:
36 BridgedInvalidatorTestDelegate()
37 : ui_thread_(content::BrowserThread::UI, &ui_loop_),
38 bridge_(&mock_profile_, ui_loop_.message_loop_proxy()),
39 fake_delegate_(NULL) {
40 // Pump |ui_loop_| to fully initialize |bridge_|.
41 ui_loop_.RunUntilIdle();
42 }
43
44 ~BridgedInvalidatorTestDelegate() {
45 DestroyInvalidator();
46 }
47
48 void CreateInvalidator(
49 const std::string& /* initial_state (unused) */,
50 const base::WeakPtr<syncer::InvalidationStateTracker>&
51 /* invalidation_state_tracker (unused) */) {
52 DCHECK(!fake_delegate_);
53 DCHECK(!invalidator_.get());
54 fake_delegate_ = new syncer::FakeInvalidator();
55 invalidator_.reset(new BridgedInvalidator(
56 &bridge_, fake_delegate_, syncer::DEFAULT_INVALIDATION_ERROR));
57 }
58
59 BridgedInvalidator* GetInvalidator() {
60 return invalidator_.get();
61 }
62
63 ChromeSyncNotificationBridge* GetBridge() {
64 return &bridge_;
65 }
66
67 syncer::FakeInvalidator* GetFakeDelegate() {
68 return fake_delegate_;
69 }
70
71 void DestroyInvalidator() {
72 invalidator_.reset();
73 fake_delegate_ = NULL;
74 bridge_.StopForShutdown();
75 ui_loop_.RunUntilIdle();
76 }
77
78 void WaitForInvalidator() {
79 // Do nothing.
80 }
81
82 void TriggerOnInvalidatorStateChange(syncer::InvalidatorState state) {
83 fake_delegate_->EmitOnInvalidatorStateChange(state);
84 }
85
86 void TriggerOnIncomingInvalidation(
87 const syncer::ObjectIdInvalidationMap& invalidation_map,
88 syncer::IncomingInvalidationSource source) {
89 fake_delegate_->EmitOnIncomingInvalidation(invalidation_map, source);
90 }
91
92 private:
93 MessageLoop ui_loop_;
94 content::TestBrowserThread ui_thread_;
95 ::testing::NiceMock<ProfileMock> mock_profile_;
96 ChromeSyncNotificationBridge bridge_;
97 // Owned by |invalidator_|.
98 syncer::FakeInvalidator* fake_delegate_;
99 scoped_ptr<BridgedInvalidator> invalidator_;
100 };
101
102 namespace {
103
104 // All tests just verify that each call is passed through to the delegate, with
105 // the exception of RegisterHandler, UnregisterHandler, and
106 // UpdateRegisteredIds, which also verifies the call is forwarded to the
107 // bridge.
108 class BridgedInvalidatorTest : public testing::Test {
109 public:
110 BridgedInvalidatorTest() {
111 delegate_.CreateInvalidator(
112 "fake_state", base::WeakPtr<syncer::InvalidationStateTracker>());
113 }
114
115 virtual ~BridgedInvalidatorTest() {}
116
117 protected:
118 BridgedInvalidatorTestDelegate delegate_;
119 };
120
121 TEST_F(BridgedInvalidatorTest, HandlerMethods) {
122 syncer::ObjectIdSet ids;
123 ids.insert(invalidation::ObjectId(1, "id1"));
124
125 syncer::FakeInvalidationHandler handler;
126
127 delegate_.GetInvalidator()->RegisterHandler(&handler);
128 EXPECT_TRUE(delegate_.GetFakeDelegate()->IsHandlerRegistered(&handler));
129 EXPECT_TRUE(delegate_.GetBridge()->IsHandlerRegisteredForTest(&handler));
130
131 delegate_.GetInvalidator()->UpdateRegisteredIds(&handler, ids);
132 EXPECT_EQ(ids, delegate_.GetFakeDelegate()->GetRegisteredIds(&handler));
133 EXPECT_EQ(ids, delegate_.GetBridge()->GetRegisteredIdsForTest(&handler));
134
135 delegate_.GetInvalidator()->UnregisterHandler(&handler);
136 EXPECT_FALSE(delegate_.GetFakeDelegate()->IsHandlerRegistered(&handler));
137 EXPECT_FALSE(delegate_.GetBridge()->IsHandlerRegisteredForTest(&handler));
138 }
139
140 TEST_F(BridgedInvalidatorTest, GetInvalidatorState) {
141 EXPECT_EQ(syncer::DEFAULT_INVALIDATION_ERROR,
142 delegate_.GetInvalidator()->GetInvalidatorState());
143 }
144
145 TEST_F(BridgedInvalidatorTest, SetUniqueId) {
146 const std::string& unique_id = "unique id";
147 delegate_.GetInvalidator()->SetUniqueId(unique_id);
148 EXPECT_EQ(unique_id, delegate_.GetFakeDelegate()->GetUniqueId());
149 }
150
151 TEST_F(BridgedInvalidatorTest, UpdateCredentials) {
152 const std::string& email = "email";
153 const std::string& token = "token";
154 delegate_.GetInvalidator()->UpdateCredentials(email, token);
155 EXPECT_EQ(email, delegate_.GetFakeDelegate()->GetCredentialsEmail());
156 EXPECT_EQ(token, delegate_.GetFakeDelegate()->GetCredentialsToken());
157 }
158
159 TEST_F(BridgedInvalidatorTest, SendInvalidation) {
160 syncer::ObjectIdSet ids;
161 ids.insert(invalidation::ObjectId(1, "id1"));
162 ids.insert(invalidation::ObjectId(2, "id2"));
163 const syncer::ObjectIdInvalidationMap& invalidation_map =
164 syncer::ObjectIdSetToInvalidationMap(ids, "payload");
165 delegate_.GetInvalidator()->SendInvalidation(invalidation_map);
166 EXPECT_THAT(invalidation_map,
167 Eq(delegate_.GetFakeDelegate()->GetLastSentInvalidationMap()));
168 }
169
170 } // namespace
171 } // namespace browser_sync
172
173 namespace syncer {
174 namespace {
175
176 INSTANTIATE_TYPED_TEST_CASE_P(
177 BridgedInvalidatorTest, InvalidatorTest,
178 ::browser_sync::BridgedInvalidatorTestDelegate);
179
180 } // namespace
181 } // namespace syncer
OLDNEW
« no previous file with comments | « chrome/browser/sync/glue/bridged_invalidator.cc ('k') | chrome/browser/sync/glue/chrome_sync_notification_bridge.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698