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