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

Side by Side Diff: chrome/browser/sync/profile_sync_service_session_unittest.cc

Issue 3133022: sync: take two for: "Added classes to enable session sync... (Closed)
Patch Set: Created 10 years, 4 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
OLDNEW
(Empty)
1 // Copyright (c) 2010 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 <map>
6 #include <string>
7
8 #include "base/message_loop.h"
9 #include "base/scoped_ptr.h"
10 #include "base/scoped_temp_dir.h"
11 #include "base/stl_util-inl.h"
12 #include "base/task.h"
13 #include "chrome/browser/sessions/session_service.h"
14 #include "chrome/browser/sessions/session_service_test_helper.h"
15 #include "chrome/browser/sync/abstract_profile_sync_service_test.h"
16 #include "chrome/browser/sync/engine/syncapi.h"
17 #include "chrome/browser/sync/glue/session_change_processor.h"
18 #include "chrome/browser/sync/glue/session_data_type_controller.h"
19 #include "chrome/browser/sync/glue/session_model_associator.h"
20 #include "chrome/browser/sync/glue/sync_backend_host.h"
21 #include "chrome/browser/sync/profile_sync_test_util.h"
22 #include "chrome/browser/sync/profile_sync_factory_mock.h"
23 #include "chrome/browser/sync/protocol/session_specifics.pb.h"
24 #include "chrome/browser/sync/protocol/sync.pb.h"
25 #include "chrome/browser/sync/syncable/directory_manager.h"
26 #include "chrome/browser/sync/syncable/model_type.h"
27 #include "chrome/browser/sync/syncable/syncable.h"
28 #include "chrome/browser/sync/test_profile_sync_service.h"
29 #include "chrome/common/notification_observer.h"
30 #include "chrome/common/notification_registrar.h"
31 #include "chrome/common/notification_service.h"
32 #include "chrome/common/pref_names.h"
33 #include "chrome/test/browser_with_test_window_test.h"
34 #include "chrome/test/file_test_utils.h"
35 #include "chrome/test/profile_mock.h"
36 #include "chrome/test/testing_profile.h"
37 #include "chrome/test/sync/engine/test_id_factory.h"
38 #include "testing/gmock/include/gmock/gmock.h"
39 #include "testing/gtest/include/gtest/gtest.h"
40
41 using browser_sync::SessionChangeProcessor;
42 using browser_sync::SessionDataTypeController;
43 using browser_sync::SessionModelAssociator;
44 using browser_sync::SyncBackendHost;
45 using sync_api::SyncManager;
46 using testing::_;
47 using testing::Return;
48 using browser_sync::TestIdFactory;
49
50 namespace browser_sync {
51
52 class ProfileSyncServiceSessionTest
53 : public BrowserWithTestWindowTest,
54 public NotificationObserver {
55 public:
56 ProfileSyncServiceSessionTest()
57 : window_bounds_(0, 1, 2, 3),
58 notified_of_update_(false),
59 notification_sync_id_(0) {}
60
61 ProfileSyncService* sync_service() { return sync_service_.get(); }
62
63 TestIdFactory* ids() { return &ids_; }
64
65 protected:
66 SessionService* service() { return helper_.service(); }
67
68 virtual void SetUp() {
69 BrowserWithTestWindowTest::SetUp();
70 profile()->set_has_history_service(true);
71 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
72 SessionService* session_service = new SessionService(temp_dir_.path());
73 helper_.set_service(session_service);
74 service()->SetWindowType(window_id_, Browser::TYPE_NORMAL);
75 service()->SetWindowBounds(window_id_, window_bounds_, false);
76 registrar_.Add(this, NotificationType::FOREIGN_SESSION_UPDATED,
77 NotificationService::AllSources());
78 registrar_.Add(this, NotificationType::FOREIGN_SESSION_DELETED,
79 NotificationService::AllSources());
80 }
81
82 void Observe(NotificationType type,
83 const NotificationSource& source,
84 const NotificationDetails& details) {
85 switch (type.value) {
86 case NotificationType::FOREIGN_SESSION_UPDATED: {
87 notified_of_update_ = true;
88 notification_sync_id_ = *Details<int64>(details).ptr();
89 break;
90 }
91 case NotificationType::FOREIGN_SESSION_DELETED: {
92 notified_of_update_ = true;
93 notification_sync_id_ = -1;
94 break;
95 }
96 default:
97 NOTREACHED();
98 break;
99 }
100 }
101
102 virtual void TearDown() {
103 helper_.set_service(NULL);
104 profile()->set_session_service(NULL);
105 sync_service_.reset();
106 }
107
108 bool StartSyncService(Task* task, bool will_fail_association) {
109 if (sync_service_.get())
110 return false;
111
112 sync_service_.reset(new TestProfileSyncService(
113 &factory_, profile(), false, false, task));
114 profile()->set_session_service(helper_.service());
115
116 // Register the session data type.
117 model_associator_ =
118 new SessionModelAssociator(sync_service_.get());
119 change_processor_ = new SessionChangeProcessor(
120 sync_service_.get(), model_associator_);
121 EXPECT_CALL(factory_, CreateSessionSyncComponents(_, _)).
122 WillOnce(Return(ProfileSyncFactory::SyncComponents(
123 model_associator_, change_processor_)));
124 EXPECT_CALL(factory_, CreateDataTypeManager(_, _)).
125 WillOnce(ReturnNewDataTypeManager());
126 sync_service_->set_num_expected_resumes(will_fail_association ? 0 : 1);
127 sync_service_->RegisterDataTypeController(
128 new SessionDataTypeController(&factory_, sync_service_.get()));
129 sync_service_->Initialize();
130 MessageLoop::current()->Run();
131 return true;
132 }
133
134 SyncBackendHost* backend() { return sync_service_->backend(); }
135
136 // Path used in testing.
137 ScopedTempDir temp_dir_;
138 SessionServiceTestHelper helper_;
139 SessionModelAssociator* model_associator_;
140 SessionChangeProcessor* change_processor_;
141 SessionID window_id_;
142 ProfileSyncFactoryMock factory_;
143 scoped_ptr<TestProfileSyncService> sync_service_;
144 TestIdFactory ids_;
145 const gfx::Rect window_bounds_;
146 bool notified_of_update_;
147 int64 notification_sync_id_;
148 NotificationRegistrar registrar_;
149 };
150
151 class CreateRootTask : public Task {
152 public:
153 explicit CreateRootTask(ProfileSyncServiceSessionTest* test)
154 : test_(test), success_(false) {
155 }
156
157 virtual ~CreateRootTask() {}
158 virtual void Run() {
159 success_ = ProfileSyncServiceTestHelper::CreateRoot(syncable::SESSIONS,
160 test_->sync_service(), test_->ids());
161 }
162
163 bool success() { return success_; }
164
165 private:
166 ProfileSyncServiceSessionTest* test_;
167 bool success_;
168 };
169
170 // Test that we can write this machine's session to a node and retrieve it.
171 TEST_F(ProfileSyncServiceSessionTest, WriteSessionToNode) {
172 CreateRootTask task(this);
173 ASSERT_TRUE(StartSyncService(&task, false));
174 ASSERT_TRUE(task.success());
175 ASSERT_EQ(model_associator_->GetSessionService(), helper_.service());
176
177 // Check that the DataTypeController associated the models.
178 bool has_nodes;
179 ASSERT_TRUE(model_associator_->SyncModelHasUserCreatedNodes(&has_nodes));
180 ASSERT_TRUE(has_nodes);
181 ASSERT_TRUE(model_associator_->ChromeModelHasUserCreatedNodes(&has_nodes));
182 ASSERT_TRUE(has_nodes);
183 std::string machine_tag = model_associator_->GetCurrentMachineTag();
184 int64 sync_id;
185 ASSERT_TRUE(model_associator_->GetSyncIdForTaggedNode(&machine_tag,
186 &sync_id));
187 ASSERT_EQ(model_associator_->GetSyncIdFromChromeId(machine_tag), sync_id);
188 scoped_ptr<const sync_pb::SessionSpecifics> sync_specifics(
189 model_associator_->GetChromeNodeFromSyncId(sync_id));
190 ASSERT_TRUE(sync_specifics != NULL);
191
192 // Check that we can get the correct session specifics back from the node.
193 sync_api::ReadTransaction trans(sync_service_->
194 backend()->GetUserShareHandle());
195 sync_api::ReadNode node(&trans);
196 ASSERT_TRUE(node.InitByClientTagLookup(syncable::SESSIONS,
197 machine_tag));
198 const sync_pb::SessionSpecifics& specifics(node.GetSessionSpecifics());
199 ASSERT_EQ(sync_specifics->session_tag(), specifics.session_tag());
200 ASSERT_EQ(machine_tag, specifics.session_tag());
201 }
202
203 // Test that we can fill this machine's session, write it to a node,
204 // and then retrieve it.
205 TEST_F(ProfileSyncServiceSessionTest, WriteFilledSessionToNode) {
206 CreateRootTask task(this);
207 ASSERT_TRUE(StartSyncService(&task, false));
208 ASSERT_TRUE(task.success());
209
210 // Check that the DataTypeController associated the models.
211 bool has_nodes;
212 ASSERT_TRUE(model_associator_->SyncModelHasUserCreatedNodes(&has_nodes));
213 ASSERT_TRUE(has_nodes);
214 AddTab(browser(), GURL("http://foo/1"));
215 NavigateAndCommitActiveTab(GURL("http://foo/2"));
216 AddTab(browser(), GURL("http://bar/1"));
217 NavigateAndCommitActiveTab(GURL("http://bar/2"));
218
219 // Report a saved session, thus causing the ChangeProcessor to write to a
220 // node.
221 NotificationService::current()->Notify(
222 NotificationType::SESSION_SERVICE_SAVED,
223 Source<Profile>(profile()),
224 NotificationService::NoDetails());
225 std::string machine_tag = model_associator_->GetCurrentMachineTag();
226 int64 sync_id;
227 ASSERT_TRUE(model_associator_->GetSyncIdForTaggedNode(&machine_tag,
228 &sync_id));
229 ASSERT_EQ(model_associator_->GetSyncIdFromChromeId(machine_tag), sync_id);
230 scoped_ptr<const sync_pb::SessionSpecifics> sync_specifics(
231 model_associator_->GetChromeNodeFromSyncId(sync_id));
232 ASSERT_TRUE(sync_specifics != NULL);
233
234 // Check that this machine's data is not included in the foreign windows.
235 std::vector<ForeignSession*> foreign_sessions;
236 model_associator_->GetSessionDataFromSyncModel(&foreign_sessions);
237 ASSERT_EQ(foreign_sessions.size(), 0U);
238
239 // Get the windows for this machine from the node and check that they were
240 // filled.
241 sync_api::ReadTransaction trans(sync_service_->
242 backend()->GetUserShareHandle());
243 sync_api::ReadNode node(&trans);
244 ASSERT_TRUE(node.InitByClientTagLookup(syncable::SESSIONS,
245 machine_tag));
246 model_associator_->AppendForeignSessionWithID(sync_id, &foreign_sessions,
247 &trans);
248 ASSERT_EQ(foreign_sessions.size(), 1U);
249 ASSERT_EQ(1U, foreign_sessions[0]->windows.size());
250 ASSERT_EQ(2U, foreign_sessions[0]->windows[0]->tabs.size());
251 ASSERT_EQ(2U, foreign_sessions[0]->windows[0]->tabs[0]->navigations.size());
252 ASSERT_EQ(GURL("http://bar/1"),
253 foreign_sessions[0]->windows[0]->tabs[0]->navigations[0].virtual_url());
254 ASSERT_EQ(GURL("http://bar/2"),
255 foreign_sessions[0]->windows[0]->tabs[0]->navigations[1].virtual_url());
256 ASSERT_EQ(2U, foreign_sessions[0]->windows[0]->tabs[1]->navigations.size());
257 ASSERT_EQ(GURL("http://foo/1"),
258 foreign_sessions[0]->windows[0]->tabs[1]->navigations[0].virtual_url());
259 ASSERT_EQ(GURL("http://foo/2"),
260 foreign_sessions[0]->windows[0]->tabs[1]->navigations[1].virtual_url());
261 const sync_pb::SessionSpecifics& specifics(node.GetSessionSpecifics());
262 ASSERT_EQ(sync_specifics->session_tag(), specifics.session_tag());
263 ASSERT_EQ(machine_tag, specifics.session_tag());
264 }
265
266 // Test that we fail on a failed model association.
267 TEST_F(ProfileSyncServiceSessionTest, FailModelAssociation) {
268 ASSERT_TRUE(StartSyncService(NULL, true));
269 ASSERT_TRUE(sync_service_->unrecoverable_error_detected());
270 }
271
272 // Write a foreign session to a node, and then retrieve it.
273 TEST_F(ProfileSyncServiceSessionTest, WriteForeignSessionToNode) {
274 CreateRootTask task(this);
275 ASSERT_TRUE(StartSyncService(&task, false));
276 ASSERT_TRUE(task.success());
277
278 // Check that the DataTypeController associated the models.
279 bool has_nodes;
280 ASSERT_TRUE(model_associator_->SyncModelHasUserCreatedNodes(&has_nodes));
281 ASSERT_TRUE(has_nodes);
282
283 // Fill an instance of session specifics with a foreign session's data.
284 sync_pb::SessionSpecifics specifics;
285 std::string machine_tag = "session_sync123";
286 specifics.set_session_tag(machine_tag);
287 sync_pb::SessionWindow* window = specifics.add_session_window();
288 window->set_selected_tab_index(1);
289 window->set_browser_type(sync_pb::SessionWindow_BrowserType_TYPE_NORMAL);
290 sync_pb::SessionTab* tab = window->add_session_tab();
291 tab->set_tab_visual_index(13);
292 tab->set_current_navigation_index(3);
293 tab->set_pinned(true);
294 tab->set_extension_app_id("app_id");
295 sync_pb::TabNavigation* navigation = tab->add_navigation();
296 navigation->set_index(12);
297 navigation->set_virtual_url("http://foo/1");
298 navigation->set_referrer("referrer");
299 navigation->set_title("title");
300 navigation->set_page_transition(sync_pb::TabNavigation_PageTransition_TYPED);
301
302 // Update the server with the session specifics.
303 {
304 sync_api::WriteTransaction trans(sync_service_->
305 backend()->GetUserShareHandle());
306 sync_api::ReadNode root(&trans);
307 ASSERT_TRUE(root.InitByTagLookup(kSessionsTag));
308 model_associator_->UpdateSyncModel(&specifics, &trans, &root);
309 }
310
311 // Check that the foreign session was written to a node and retrieve the data.
312 int64 sync_id;
313 ASSERT_TRUE(model_associator_->GetSyncIdForTaggedNode(&machine_tag,
314 &sync_id));
315 ASSERT_EQ(model_associator_->GetSyncIdFromChromeId(machine_tag), sync_id);
316 scoped_ptr<const sync_pb::SessionSpecifics> sync_specifics(
317 model_associator_->GetChromeNodeFromSyncId(sync_id));
318 ASSERT_TRUE(sync_specifics != NULL);
319 std::vector<ForeignSession*> foreign_sessions;
320 model_associator_->GetSessionDataFromSyncModel(&foreign_sessions);
321 ASSERT_EQ(foreign_sessions.size(), 1U);
322 ASSERT_EQ(1U, foreign_sessions[0]->windows.size());
323 ASSERT_EQ(1U, foreign_sessions[0]->windows[0]->tabs.size());
324 ASSERT_EQ(1U, foreign_sessions[0]->windows[0]->tabs[0]->navigations.size());
325 ASSERT_EQ(foreign_sessions[0]->foreign_tession_tag, machine_tag);
326 ASSERT_EQ(1, foreign_sessions[0]->windows[0]->selected_tab_index);
327 ASSERT_EQ(1, foreign_sessions[0]->windows[0]->type);
328 ASSERT_EQ(13, foreign_sessions[0]->windows[0]->tabs[0]->tab_visual_index);
329 ASSERT_EQ(3,
330 foreign_sessions[0]->windows[0]->tabs[0]->current_navigation_index);
331 ASSERT_TRUE(foreign_sessions[0]->windows[0]->tabs[0]->pinned);
332 ASSERT_EQ("app_id",
333 foreign_sessions[0]->windows[0]->tabs[0]->extension_app_id);
334 ASSERT_EQ(12,
335 foreign_sessions[0]->windows[0]->tabs[0]->navigations[0].index());
336 ASSERT_EQ(GURL("referrer"),
337 foreign_sessions[0]->windows[0]->tabs[0]->navigations[0].referrer());
338 ASSERT_EQ(string16(ASCIIToUTF16("title")),
339 foreign_sessions[0]->windows[0]->tabs[0]->navigations[0].title());
340 ASSERT_EQ(PageTransition::TYPED,
341 foreign_sessions[0]->windows[0]->tabs[0]->navigations[0].transition());
342 ASSERT_EQ(GURL("http://foo/1"),
343 foreign_sessions[0]->windows[0]->tabs[0]->navigations[0].virtual_url());
344 sync_api::WriteTransaction trans(sync_service_->
345 backend()->GetUserShareHandle());
346 sync_api::ReadNode node(&trans);
347 ASSERT_TRUE(node.InitByClientTagLookup(syncable::SESSIONS,
348 machine_tag));
349 const sync_pb::SessionSpecifics& specifics_(node.GetSessionSpecifics());
350 ASSERT_EQ(sync_specifics->session_tag(), specifics_.session_tag());
351 ASSERT_EQ(machine_tag, specifics_.session_tag());
352 }
353
354 // Test the DataTypeController on update.
355 TEST_F(ProfileSyncServiceSessionTest, UpdatedSyncNodeActionUpdate) {
356 CreateRootTask task(this);
357 ASSERT_TRUE(StartSyncService(&task, false));
358 ASSERT_TRUE(task.success());
359 int64 node_id = model_associator_->GetSyncIdFromChromeId(
360 model_associator_->GetCurrentMachineTag());
361 scoped_ptr<SyncManager::ChangeRecord> record(new SyncManager::ChangeRecord);
362 record->action = SyncManager::ChangeRecord::ACTION_UPDATE;
363 record->id = node_id;
364 ASSERT_EQ(notification_sync_id_, 0);
365 ASSERT_FALSE(notified_of_update_);
366 {
367 sync_api::WriteTransaction trans(backend()->GetUserShareHandle());
368 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1);
369 }
370 ASSERT_EQ(notification_sync_id_, node_id);
371 ASSERT_TRUE(notified_of_update_);
372 }
373
374 // Test the DataTypeController on add.
375 TEST_F(ProfileSyncServiceSessionTest, UpdatedSyncNodeActionAdd) {
376 CreateRootTask task(this);
377 ASSERT_TRUE(StartSyncService(&task, false));
378 ASSERT_TRUE(task.success());
379
380 int64 node_id = model_associator_->GetSyncIdFromChromeId(
381 model_associator_->GetCurrentMachineTag());
382 scoped_ptr<SyncManager::ChangeRecord> record(new SyncManager::ChangeRecord);
383 record->action = SyncManager::ChangeRecord::ACTION_ADD;
384 record->id = node_id;
385 ASSERT_EQ(notification_sync_id_, 0);
386 ASSERT_FALSE(notified_of_update_);
387 {
388 sync_api::WriteTransaction trans(backend()->GetUserShareHandle());
389 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1);
390 }
391 ASSERT_EQ(notification_sync_id_, node_id);
392 ASSERT_TRUE(notified_of_update_);
393 }
394
395 // Test the DataTypeController on delete.
396 TEST_F(ProfileSyncServiceSessionTest, UpdatedSyncNodeActionDelete) {
397 CreateRootTask task(this);
398 ASSERT_TRUE(StartSyncService(&task, false));
399 ASSERT_TRUE(task.success());
400
401 int64 node_id = model_associator_->GetSyncIdFromChromeId(
402 model_associator_->GetCurrentMachineTag());
403 scoped_ptr<SyncManager::ChangeRecord> record(new SyncManager::ChangeRecord);
404 record->action = SyncManager::ChangeRecord::ACTION_DELETE;
405 record->id = node_id;
406 ASSERT_EQ(notification_sync_id_, 0);
407 ASSERT_FALSE(notified_of_update_);
408 {
409 sync_api::WriteTransaction trans(backend()->GetUserShareHandle());
410 change_processor_->ApplyChangesFromSyncModel(&trans, record.get(), 1);
411 }
412 ASSERT_EQ(notification_sync_id_, -1);
413 ASSERT_TRUE(notified_of_update_);
414 }
415
416 } // namespace browser_sync
417
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698