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

Side by Side Diff: components/sync_sessions/sessions_sync_manager_unittest.cc

Issue 2712743006: Reland v5 of Sessions Refactor (Closed)
Patch Set: Fix typo Created 3 years, 9 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/sync_sessions/sessions_sync_manager.h" 5 #include "components/sync_sessions/sessions_sync_manager.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 [&change_string](const SyncChange& change) { 122 [&change_string](const SyncChange& change) {
123 change_string += change.ToString(); 123 change_string += change.ToString();
124 }); 124 });
125 return testing::AssertionFailure() 125 return testing::AssertionFailure()
126 << "Change type mismatch: " << type_string << " vs " 126 << "Change type mismatch: " << type_string << " vs "
127 << change_string; 127 << change_string;
128 } 128 }
129 return testing::AssertionSuccess(); 129 return testing::AssertionSuccess();
130 } 130 }
131 131
132 // Creates a field trial with the specified |trial_name| and |group_name| and
133 // registers an associated |variation_id| for it for the given |service|.
134 void CreateAndActivateFieldTrial(const std::string& trial_name,
135 const std::string& group_name,
136 variations::VariationID variation_id,
137 variations::IDCollectionKey service) {
138 base::FieldTrialList::CreateFieldTrial(trial_name, group_name);
139 variations::AssociateGoogleVariationID(service, trial_name, group_name,
140 variation_id);
141 // Access the trial to activate it.
142 base::FieldTrialList::FindFullName(trial_name);
143 }
144
145 class SessionNotificationObserver { 132 class SessionNotificationObserver {
146 public: 133 public:
147 SessionNotificationObserver() 134 SessionNotificationObserver()
148 : notified_of_update_(false), notified_of_refresh_(false) {} 135 : notified_of_update_(false), notified_of_refresh_(false) {}
149 void NotifyOfUpdate() { notified_of_update_ = true; } 136 void NotifyOfUpdate() { notified_of_update_ = true; }
150 void NotifyOfRefresh() { notified_of_refresh_ = true; } 137 void NotifyOfRefresh() { notified_of_refresh_ = true; }
151 138
152 bool notified_of_update() const { return notified_of_update_; } 139 bool notified_of_update() const { return notified_of_update_; }
153 bool notified_of_refresh() const { return notified_of_refresh_; } 140 bool notified_of_refresh() const { return notified_of_refresh_; }
154 141
(...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 SessionID window_id_; 383 SessionID window_id_;
397 sync_pb::SessionWindow_BrowserType window_type_ = 384 sync_pb::SessionWindow_BrowserType window_type_ =
398 sync_pb::SessionWindow_BrowserType_TYPE_TABBED; 385 sync_pb::SessionWindow_BrowserType_TYPE_TABBED;
399 }; 386 };
400 387
401 class TestSyncedWindowDelegatesGetter : public SyncedWindowDelegatesGetter { 388 class TestSyncedWindowDelegatesGetter : public SyncedWindowDelegatesGetter {
402 public: 389 public:
403 TestSyncedWindowDelegatesGetter() {} 390 TestSyncedWindowDelegatesGetter() {}
404 ~TestSyncedWindowDelegatesGetter() override {} 391 ~TestSyncedWindowDelegatesGetter() override {}
405 392
406 std::set<const SyncedWindowDelegate*> GetSyncedWindowDelegates() override { 393 SyncedWindowDelegateMap GetSyncedWindowDelegates() override {
407 return delegates_; 394 return delegates_;
408 } 395 }
409 396
410 const SyncedWindowDelegate* FindById(SessionID::id_type id) override { 397 const SyncedWindowDelegate* FindById(SessionID::id_type id) override {
411 for (auto* window : delegates_) { 398 for (auto window_iter_pair : delegates_) {
412 if (window->GetSessionId() == id) 399 if (window_iter_pair.second->GetSessionId() == id)
413 return window; 400 return window_iter_pair.second;
414 } 401 }
415 return nullptr; 402 return nullptr;
416 } 403 }
417 404
418 void AddSyncedWindowDelegate(const SyncedWindowDelegate* delegate) { 405 void AddSyncedWindowDelegate(const SyncedWindowDelegate* delegate) {
419 delegates_.insert(delegate); 406 delegates_[delegate->GetSessionId()] = delegate;
420 } 407 }
421 408
422 private: 409 private:
423 std::set<const SyncedWindowDelegate*> delegates_; 410 SyncedWindowDelegateMap delegates_;
424 }; 411 };
425 412
426 class TestSyncChangeProcessor : public syncer::SyncChangeProcessor { 413 class TestSyncChangeProcessor : public syncer::SyncChangeProcessor {
427 public: 414 public:
428 explicit TestSyncChangeProcessor(SyncChangeList* output) : output_(output) {} 415 explicit TestSyncChangeProcessor(SyncChangeList* output) : output_(output) {}
429 SyncError ProcessSyncChanges(const tracked_objects::Location& from_here, 416 SyncError ProcessSyncChanges(const tracked_objects::Location& from_here,
430 const SyncChangeList& change_list) override { 417 const SyncChangeList& change_list) override {
431 if (error_.IsSet()) { 418 if (error_.IsSet()) {
432 SyncError error = error_; 419 SyncError error = error_;
433 error_ = SyncError(); 420 error_ = SyncError();
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
507 } 494 }
508 495
509 private: 496 private:
510 SyncedWindowDelegatesGetter* synced_window_getter_; 497 SyncedWindowDelegatesGetter* synced_window_getter_;
511 }; 498 };
512 499
513 } // namespace 500 } // namespace
514 501
515 class SessionsSyncManagerTest : public testing::Test { 502 class SessionsSyncManagerTest : public testing::Test {
516 protected: 503 protected:
517 SessionsSyncManagerTest() : test_processor_(nullptr) { 504 SessionsSyncManagerTest() {}
505
506 void SetUp() override {
518 local_device_ = base::MakeUnique<LocalDeviceInfoProviderMock>( 507 local_device_ = base::MakeUnique<LocalDeviceInfoProviderMock>(
519 "cache_guid", "Wayne Gretzky's Hacking Box", "Chromium 10k", 508 "cache_guid", "Wayne Gretzky's Hacking Box", "Chromium 10k",
520 "Chrome 10k", sync_pb::SyncEnums_DeviceType_TYPE_LINUX, "device_id"); 509 "Chrome 10k", sync_pb::SyncEnums_DeviceType_TYPE_LINUX, "device_id");
521 }
522
523 void SetUp() override {
524 sync_client_ = base::MakeUnique<syncer::FakeSyncClient>(); 510 sync_client_ = base::MakeUnique<syncer::FakeSyncClient>();
525 sessions_client_shim_ = 511 sessions_client_shim_ =
526 base::MakeUnique<SyncSessionsClientShim>(&window_getter_); 512 base::MakeUnique<SyncSessionsClientShim>(&window_getter_);
527 sync_prefs_ = 513 sync_prefs_ =
528 base::MakeUnique<syncer::SyncPrefs>(sync_client_->GetPrefService()); 514 base::MakeUnique<syncer::SyncPrefs>(sync_client_->GetPrefService());
529 manager_ = base::MakeUnique<SessionsSyncManager>( 515 manager_ = base::MakeUnique<SessionsSyncManager>(
530 sessions_client_shim(), sync_prefs_.get(), local_device_.get(), 516 sessions_client_shim(), sync_prefs_.get(), local_device_.get(),
531 std::unique_ptr<LocalSessionEventRouter>(NewDummyRouter()), 517 std::unique_ptr<LocalSessionEventRouter>(NewDummyRouter()),
532 base::Bind(&SessionNotificationObserver::NotifyOfUpdate, 518 base::Bind(&SessionNotificationObserver::NotifyOfUpdate,
533 base::Unretained(&observer_)), 519 base::Unretained(&observer_)),
534 base::Bind(&SessionNotificationObserver::NotifyOfRefresh, 520 base::Bind(&SessionNotificationObserver::NotifyOfRefresh,
535 base::Unretained(&observer_))); 521 base::Unretained(&observer_)));
536 } 522 }
537 523
538 void TearDown() override { 524 void TearDown() override {
539 test_processor_ = nullptr; 525 test_processor_ = nullptr;
540 helper()->Reset(); 526 helper()->Reset();
541 sync_prefs_.reset(); 527 sync_prefs_.reset();
542 manager_.reset(); 528 manager_.reset();
543 } 529 }
544 530
545 const DeviceInfo* GetLocalDeviceInfo() { 531 const DeviceInfo* GetLocalDeviceInfo() {
546 return local_device_->GetLocalDeviceInfo(); 532 return local_device_->GetLocalDeviceInfo();
547 } 533 }
548 534
535 TabNodePool* GetTabPool() {
536 return &manager()->session_tracker_.local_tab_pool_;
537 }
538
549 SessionsSyncManager* manager() { return manager_.get(); } 539 SessionsSyncManager* manager() { return manager_.get(); }
550 SessionSyncTestHelper* helper() { return &helper_; } 540 SessionSyncTestHelper* helper() { return &helper_; }
551 LocalDeviceInfoProvider* local_device() { return local_device_.get(); } 541 LocalDeviceInfoProvider* local_device() { return local_device_.get(); }
552 SessionNotificationObserver* observer() { return &observer_; } 542 SessionNotificationObserver* observer() { return &observer_; }
553 syncer::SyncPrefs* sync_prefs() { return sync_prefs_.get(); } 543 syncer::SyncPrefs* sync_prefs() { return sync_prefs_.get(); }
554 SyncSessionsClient* sessions_client_shim() { 544 SyncSessionsClient* sessions_client_shim() {
555 return sessions_client_shim_.get(); 545 return sessions_client_shim_.get();
556 } 546 }
557 SyncedWindowDelegatesGetter* window_getter() { return &window_getter_; } 547 SyncedWindowDelegatesGetter* window_getter() { return &window_getter_; }
558 548
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
680 670
681 SyncData CreateRemoteData(const sync_pb::EntitySpecifics& entity, 671 SyncData CreateRemoteData(const sync_pb::EntitySpecifics& entity,
682 base::Time mtime = base::Time()) const { 672 base::Time mtime = base::Time()) const {
683 // The server ID is never relevant to these tests, so just use 1. 673 // The server ID is never relevant to these tests, so just use 1.
684 return SyncData::CreateRemoteData( 674 return SyncData::CreateRemoteData(
685 1, entity, mtime, syncer::AttachmentIdList(), 675 1, entity, mtime, syncer::AttachmentIdList(),
686 syncer::AttachmentServiceProxyForTest::Create(), 676 syncer::AttachmentServiceProxyForTest::Create(),
687 SessionsSyncManager::TagHashFromSpecifics(entity.session())); 677 SessionsSyncManager::TagHashFromSpecifics(entity.session()));
688 } 678 }
689 679
690 TestSyncedWindowDelegate* GetWindowAtIndex(size_t index) {
691 if (index >= windows_.size())
692 return nullptr;
693 return windows_[index].get();
694 }
695
696 // Creates a new tab within the window specified by |window_id|, and points it 680 // Creates a new tab within the window specified by |window_id|, and points it
697 // at |url|. Returns the newly created TestSyncedTabDelegate (not owned). 681 // at |url|. Returns the newly created TestSyncedTabDelegate (not owned).
698 TestSyncedTabDelegate* AddTab(SessionID::id_type window_id, 682 TestSyncedTabDelegate* AddTab(SessionID::id_type window_id,
699 const std::string& url, 683 const std::string& url,
700 base::Time time) { 684 base::Time time) {
701 tabs_.push_back(base::MakeUnique<TestSyncedTabDelegate>()); 685 tabs_.push_back(base::MakeUnique<TestSyncedTabDelegate>());
702 for (auto& window : windows_) { 686 for (auto& window : windows_) {
703 if (window->GetSessionId() == window_id) { 687 if (window->GetSessionId() == window_id) {
704 int tab_index = window->GetTabCount(); 688 int tab_index = window->GetTabCount();
705 window->OverrideTabAt(tab_index, tabs_.back().get()); 689 window->OverrideTabAt(tab_index, tabs_.back().get());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
737 windows_.push_back(base::MakeUnique<TestSyncedWindowDelegate>()); 721 windows_.push_back(base::MakeUnique<TestSyncedWindowDelegate>());
738 window_getter_.AddSyncedWindowDelegate(windows_.back().get()); 722 window_getter_.AddSyncedWindowDelegate(windows_.back().get());
739 return windows_.back().get(); 723 return windows_.back().get();
740 } 724 }
741 725
742 private: 726 private:
743 std::unique_ptr<syncer::FakeSyncClient> sync_client_; 727 std::unique_ptr<syncer::FakeSyncClient> sync_client_;
744 std::unique_ptr<SyncSessionsClientShim> sessions_client_shim_; 728 std::unique_ptr<SyncSessionsClientShim> sessions_client_shim_;
745 std::unique_ptr<syncer::SyncPrefs> sync_prefs_; 729 std::unique_ptr<syncer::SyncPrefs> sync_prefs_;
746 SessionNotificationObserver observer_; 730 SessionNotificationObserver observer_;
747 DummyRouter* router_; 731 DummyRouter* router_ = nullptr;
748 std::unique_ptr<SessionsSyncManager> manager_; 732 std::unique_ptr<SessionsSyncManager> manager_;
749 SessionSyncTestHelper helper_; 733 SessionSyncTestHelper helper_;
750 TestSyncChangeProcessor* test_processor_; 734 TestSyncChangeProcessor* test_processor_ = nullptr;
751 TestSyncedWindowDelegatesGetter window_getter_; 735 TestSyncedWindowDelegatesGetter window_getter_;
752 std::vector<std::unique_ptr<TestSyncedWindowDelegate>> windows_; 736 std::vector<std::unique_ptr<TestSyncedWindowDelegate>> windows_;
753 std::vector<std::unique_ptr<TestSyncedTabDelegate>> tabs_; 737 std::vector<std::unique_ptr<TestSyncedTabDelegate>> tabs_;
754 std::unique_ptr<LocalDeviceInfoProviderMock> local_device_; 738 std::unique_ptr<LocalDeviceInfoProviderMock> local_device_;
755 }; 739 };
756 740
757 // Test that the SyncSessionManager can properly fill in a SessionHeader. 741 // Test that the SyncSessionManager can properly fill in a SessionHeader.
758 TEST_F(SessionsSyncManagerTest, PopulateSessionHeader) { 742 TEST_F(SessionsSyncManagerTest, PopulateSessionHeader) {
759 sync_pb::SessionHeader header_s; 743 sync_pb::SessionHeader header_s;
760 header_s.set_client_name("Client 1"); 744 header_s.set_client_name("Client 1");
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
869 NavigateTab(tab, kBar2, kTime3); 853 NavigateTab(tab, kBar2, kTime3);
870 tab->set_current_entry_index(1); 854 tab->set_current_entry_index(1);
871 855
872 sessions::SessionTab session_tab; 856 sessions::SessionTab session_tab;
873 manager()->SetSessionTabFromDelegate(*tab, kTime9, &session_tab); 857 manager()->SetSessionTabFromDelegate(*tab, kTime9, &session_tab);
874 858
875 EXPECT_EQ(2, session_tab.current_navigation_index); 859 EXPECT_EQ(2, session_tab.current_navigation_index);
876 ASSERT_EQ(3u, session_tab.navigations.size()); 860 ASSERT_EQ(3u, session_tab.navigations.size());
877 } 861 }
878 862
879 // Tests that variation ids are set correctly.
880 TEST_F(SessionsSyncManagerTest, SetVariationIds) {
881 // Create two trials with a group which has a variation id for Chrome Sync
882 // and one with a variation id for another service.
883 const variations::VariationID kVariationId1 = 3300200;
884 const variations::VariationID kVariationId2 = 3300300;
885 const variations::VariationID kVariationId3 = 3300400;
886
887 base::FieldTrialList field_trial_list(nullptr);
888 CreateAndActivateFieldTrial("trial name 1", "group name", kVariationId1,
889 variations::CHROME_SYNC_SERVICE);
890 CreateAndActivateFieldTrial("trial name 2", "group name", kVariationId2,
891 variations::CHROME_SYNC_SERVICE);
892 CreateAndActivateFieldTrial("trial name 3", "group name", kVariationId3,
893 variations::GOOGLE_WEB_PROPERTIES);
894
895 sessions::SessionTab session_tab;
896 manager()->SetVariationIds(&session_tab);
897
898 ASSERT_EQ(2u, session_tab.variation_ids.size());
899 EXPECT_EQ(kVariationId1, session_tab.variation_ids[0]);
900 EXPECT_EQ(kVariationId2, session_tab.variation_ids[1]);
901 }
902
903 // Tests that for supervised users blocked navigations are recorded and marked 863 // Tests that for supervised users blocked navigations are recorded and marked
904 // as such, while regular navigations are marked as allowed. 864 // as such, while regular navigations are marked as allowed.
905 TEST_F(SessionsSyncManagerTest, BlockedNavigations) { 865 TEST_F(SessionsSyncManagerTest, BlockedNavigations) {
906 TestSyncedTabDelegate* tab = 866 TestSyncedTabDelegate* tab =
907 AddTab(AddWindow()->GetSessionId(), kFoo1, kTime1); 867 AddTab(AddWindow()->GetSessionId(), kFoo1, kTime1);
908 868
909 auto entry2 = base::MakeUnique<sessions::SerializedNavigationEntry>(); 869 auto entry2 = base::MakeUnique<sessions::SerializedNavigationEntry>();
910 GURL url2("http://blocked.com/foo"); 870 GURL url2("http://blocked.com/foo");
911 SerializedNavigationEntryTestHelper::SetVirtualURL(GURL(url2), entry2.get()); 871 SerializedNavigationEntryTestHelper::SetVirtualURL(GURL(url2), entry2.get());
912 SerializedNavigationEntryTestHelper::SetTimestamp(kTime2, entry2.get()); 872 SerializedNavigationEntryTestHelper::SetTimestamp(kTime2, entry2.get());
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
976 // Header creation + update. 936 // Header creation + update.
977 ASSERT_TRUE(ChangeTypeMatches( 937 ASSERT_TRUE(ChangeTypeMatches(
978 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE})); 938 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE}));
979 EXPECT_EQ(out.size(), 939 EXPECT_EQ(out.size(),
980 CountIfTagMatches(out, manager()->current_machine_tag())); 940 CountIfTagMatches(out, manager()->current_machine_tag()));
981 VerifyLocalHeaderChange(out[0], 0, 0); 941 VerifyLocalHeaderChange(out[0], 0, 0);
982 VerifyLocalHeaderChange(out[1], 0, 0); 942 VerifyLocalHeaderChange(out[1], 0, 0);
983 943
984 // Now take that header node and feed it in as input. 944 // Now take that header node and feed it in as input.
985 SyncData d = CreateRemoteData(out[1].sync_data().GetSpecifics()); 945 SyncData d = CreateRemoteData(out[1].sync_data().GetSpecifics());
986 SyncDataList in(&d, &d + 1); 946 SyncDataList in = {d};
987 out.clear(); 947 out.clear();
988 manager()->StopSyncing(syncer::SESSIONS); 948 manager()->StopSyncing(syncer::SESSIONS);
989 InitWithSyncDataTakeOutput(in, &out); 949 InitWithSyncDataTakeOutput(in, &out);
990 950
991 ASSERT_TRUE(ChangeTypeMatches(out, {SyncChange::ACTION_UPDATE})); 951 ASSERT_TRUE(ChangeTypeMatches(out, {SyncChange::ACTION_UPDATE}));
992 EXPECT_TRUE(out[0].sync_data().GetSpecifics().session().has_header()); 952 EXPECT_TRUE(out[0].sync_data().GetSpecifics().session().has_header());
993 } 953 }
994 954
995 // Tests MergeDataAndStartSyncing with sync data but no local data. 955 // Tests MergeDataAndStartSyncing with sync data but no local data.
996 TEST_F(SessionsSyncManagerTest, MergeWithInitialForeignSession) { 956 TEST_F(SessionsSyncManagerTest, MergeWithInitialForeignSession) {
(...skipping 26 matching lines...) Expand all
1023 ASSERT_EQ(1U, foreign_sessions.size()); 983 ASSERT_EQ(1U, foreign_sessions.size());
1024 std::vector<std::vector<SessionID::id_type>> session_reference; 984 std::vector<std::vector<SessionID::id_type>> session_reference;
1025 session_reference.push_back(tab_list1); 985 session_reference.push_back(tab_list1);
1026 session_reference.push_back(tab_list2); 986 session_reference.push_back(tab_list2);
1027 helper()->VerifySyncedSession(kTag1, session_reference, 987 helper()->VerifySyncedSession(kTag1, session_reference,
1028 *(foreign_sessions[0])); 988 *(foreign_sessions[0]));
1029 } 989 }
1030 990
1031 // Ensure model association associates the pre-existing tabs. 991 // Ensure model association associates the pre-existing tabs.
1032 TEST_F(SessionsSyncManagerTest, MergeLocalSessionExistingTabs) { 992 TEST_F(SessionsSyncManagerTest, MergeLocalSessionExistingTabs) {
1033 SessionID::id_type window_id = AddWindow()->GetSessionId(); 993 TestSyncedWindowDelegate* window = AddWindow();
994 SessionID::id_type window_id = window->GetSessionId();
1034 TestSyncedTabDelegate* tab = AddTab(window_id, kFoo1); 995 TestSyncedTabDelegate* tab = AddTab(window_id, kFoo1);
1035 NavigateTab(tab, kBar1); // Adds back entry. 996 NavigateTab(tab, kBar1); // Adds back entry.
1036 NavigateTab(tab, kBaz1); // Adds back entry. 997 NavigateTab(tab, kBaz1); // Adds back entry.
1037 TestSyncedTabDelegate* tab2 = AddTab(window_id, kFoo2); 998 TestSyncedTabDelegate* tab2 = AddTab(window_id, kFoo2);
1038 NavigateTab(tab2, kBar2); // Adds back entry. 999 NavigateTab(tab2, kBar2); // Adds back entry.
1039 1000
1040 SyncChangeList out; 1001 SyncChangeList out;
1041 InitWithSyncDataTakeOutput(SyncDataList(), &out); 1002 InitWithSyncDataTakeOutput(SyncDataList(), &out);
1042 // Header creation, add two tabs (and update them), header update. 1003 // Header creation, add two tabs, header update.
1043 ASSERT_TRUE(ChangeTypeMatches( 1004 ASSERT_TRUE(
1044 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_ADD, 1005 ChangeTypeMatches(out,
1045 SyncChange::ACTION_UPDATE, SyncChange::ACTION_ADD, 1006 {SyncChange::ACTION_ADD, SyncChange::ACTION_ADD,
1046 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE})); 1007 SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE}));
1047 EXPECT_EQ(out.size(), 1008 EXPECT_EQ(out.size(),
1048 CountIfTagMatches(out, manager()->current_machine_tag())); 1009 CountIfTagMatches(out, manager()->current_machine_tag()));
1049 1010
1050 // Check that this machine's data is not included in the foreign windows. 1011 // Check that this machine's data is not included in the foreign windows.
1051 std::vector<const SyncedSession*> foreign_sessions; 1012 std::vector<const SyncedSession*> foreign_sessions;
1052 ASSERT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); 1013 ASSERT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions));
1053 1014
1054 VerifyLocalHeaderChange(out[0], 0, 0); 1015 VerifyLocalHeaderChange(out[0], 0, 0);
1055 VerifyLocalTabChange(out[2], tab->GetEntryCount(), kBaz1); 1016 VerifyLocalTabChange(out[1], tab->GetEntryCount(), kBaz1);
1056 VerifyLocalTabChange(out[4], tab2->GetEntryCount(), kBar2); 1017 VerifyLocalTabChange(out[2], tab2->GetEntryCount(), kBar2);
1057 VerifyLocalHeaderChange(out[5], 1, 2); 1018 VerifyLocalHeaderChange(out[3], 1, 2);
1058 1019
1059 // Verify tab delegates have Sync ids. 1020 // Verify tab delegates have Sync ids.
1060 std::set<const SyncedWindowDelegate*> window_delegates = 1021 EXPECT_EQ(0, window->GetTabAt(0)->GetSyncId());
1061 window_getter()->GetSyncedWindowDelegates(); 1022 EXPECT_EQ(1, window->GetTabAt(1)->GetSyncId());
1062 // Sync ids are in same order as tabs because the association happens after
1063 // the tabs are opened (and therefore iterates through same order).
1064 EXPECT_EQ(0, (*window_delegates.begin())->GetTabAt(0)->GetSyncId());
1065 EXPECT_EQ(1, (*window_delegates.begin())->GetTabAt(1)->GetSyncId());
1066 } 1023 }
1067 1024
1068 // This is a combination of MergeWithInitialForeignSession and 1025 // This is a combination of MergeWithInitialForeignSession and
1069 // MergeLocalSessionExistingTabs. We repeat some checks performed in each of 1026 // MergeLocalSessionExistingTabs. We repeat some checks performed in each of
1070 // those tests to ensure the common mixed scenario works. 1027 // those tests to ensure the common mixed scenario works.
1071 TEST_F(SessionsSyncManagerTest, MergeWithLocalAndForeignTabs) { 1028 TEST_F(SessionsSyncManagerTest, MergeWithLocalAndForeignTabs) {
1072 // Local. 1029 // Local.
1073 TestSyncedTabDelegate* tab = AddTab(AddWindow()->GetSessionId(), kFoo1); 1030 TestSyncedTabDelegate* tab = AddTab(AddWindow()->GetSessionId(), kFoo1);
1074 NavigateTab(tab, kFoo2); 1031 NavigateTab(tab, kFoo2);
1075 1032
1076 // Foreign. 1033 // Foreign.
1077 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1), 1034 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1),
1078 std::end(kTabIds1)); 1035 std::end(kTabIds1));
1079 std::vector<sync_pb::SessionSpecifics> tabs1; 1036 std::vector<sync_pb::SessionSpecifics> tabs1;
1080 sync_pb::SessionSpecifics meta( 1037 sync_pb::SessionSpecifics meta(
1081 helper()->BuildForeignSession(kTag1, tab_list1, &tabs1)); 1038 helper()->BuildForeignSession(kTag1, tab_list1, &tabs1));
1082 SyncDataList foreign_data; 1039 SyncDataList foreign_data;
1083 foreign_data.push_back(CreateRemoteData(meta)); 1040 foreign_data.push_back(CreateRemoteData(meta));
1084 AddTabsToSyncDataList(tabs1, &foreign_data); 1041 AddTabsToSyncDataList(tabs1, &foreign_data);
1085 1042
1086 SyncChangeList out; 1043 SyncChangeList out;
1087 InitWithSyncDataTakeOutput(foreign_data, &out); 1044 InitWithSyncDataTakeOutput(foreign_data, &out);
1088 // Should be one header add, 1 tab add (and update), and one header update.
1089 1045
1090 ASSERT_TRUE(ChangeTypeMatches( 1046 // Should be one header add, 1 tab add, and one header update.
1091 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_ADD, 1047 ASSERT_TRUE(ChangeTypeMatches(out,
1092 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE})); 1048 {SyncChange::ACTION_ADD, SyncChange::ACTION_ADD,
1049 SyncChange::ACTION_UPDATE}));
1093 EXPECT_EQ(out.size(), 1050 EXPECT_EQ(out.size(),
1094 CountIfTagMatches(out, manager()->current_machine_tag())); 1051 CountIfTagMatches(out, manager()->current_machine_tag()));
1095 1052
1096 // Verify local data. 1053 // Verify local data.
1097 VerifyLocalHeaderChange(out[0], 0, 0); 1054 VerifyLocalHeaderChange(out[0], 0, 0);
1098 VerifyLocalTabChange(out[2], tab->GetEntryCount(), kFoo2); 1055 VerifyLocalTabChange(out[1], tab->GetEntryCount(), kFoo2);
1099 VerifyLocalHeaderChange(out[3], 1, 1); 1056 VerifyLocalHeaderChange(out[2], 1, 1);
1100 1057
1101 // Verify foreign data. 1058 // Verify foreign data.
1102 std::vector<const SyncedSession*> foreign_sessions; 1059 std::vector<const SyncedSession*> foreign_sessions;
1103 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); 1060 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions));
1104 std::vector<std::vector<SessionID::id_type>> session_reference; 1061 std::vector<std::vector<SessionID::id_type>> session_reference;
1105 session_reference.push_back(tab_list1); 1062 session_reference.push_back(tab_list1);
1106 helper()->VerifySyncedSession(kTag1, session_reference, 1063 helper()->VerifySyncedSession(kTag1, session_reference,
1107 *(foreign_sessions[0])); 1064 *(foreign_sessions[0]));
1108 // There should be one and only one foreign session. If VerifySyncedSession 1065 // There should be one and only one foreign session. If VerifySyncedSession
1109 // was successful above this EXPECT call ensures the local session didn't 1066 // was successful above this EXPECT call ensures the local session didn't
(...skipping 18 matching lines...) Expand all
1128 meta1_reference.push_back(tab_list1); 1085 meta1_reference.push_back(tab_list1);
1129 std::vector<sync_pb::SessionSpecifics> tabs1; 1086 std::vector<sync_pb::SessionSpecifics> tabs1;
1130 meta1 = helper()->BuildForeignSession(kTag1, tab_list1, &tabs1); 1087 meta1 = helper()->BuildForeignSession(kTag1, tab_list1, &tabs1);
1131 foreign_data1.push_back(CreateRemoteData(meta1)); 1088 foreign_data1.push_back(CreateRemoteData(meta1));
1132 AddTabsToSyncDataList(tabs1, &foreign_data1); 1089 AddTabsToSyncDataList(tabs1, &foreign_data1);
1133 1090
1134 SyncChangeList out; 1091 SyncChangeList out;
1135 InitWithSyncDataTakeOutput(foreign_data1, &out); 1092 InitWithSyncDataTakeOutput(foreign_data1, &out);
1136 1093
1137 // 1 header add, two tab adds, one header update. 1094 // 1 header add, two tab adds, one header update.
1138 ASSERT_TRUE(ChangeTypeMatches( 1095 ASSERT_TRUE(
1139 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_ADD, 1096 ChangeTypeMatches(out,
1140 SyncChange::ACTION_UPDATE, SyncChange::ACTION_ADD, 1097 {SyncChange::ACTION_ADD, SyncChange::ACTION_ADD,
1141 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE})); 1098 SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE}));
1142 EXPECT_EQ(out.size(), 1099 EXPECT_EQ(out.size(),
1143 CountIfTagMatches(out, manager()->current_machine_tag())); 1100 CountIfTagMatches(out, manager()->current_machine_tag()));
1144 VerifyLocalHeaderChange(out[5], 2, 2); 1101 VerifyLocalHeaderChange(out[3], 2, 2);
1145 1102
1146 // Add a second window to the foreign session. 1103 // Add a second window to the foreign session.
1147 std::vector<SessionID::id_type> tab_list2(std::begin(kTabIds2), 1104 std::vector<SessionID::id_type> tab_list2(std::begin(kTabIds2),
1148 std::end(kTabIds2)); 1105 std::end(kTabIds2));
1149 meta1_reference.push_back(tab_list2); 1106 meta1_reference.push_back(tab_list2);
1150 helper()->AddWindowSpecifics(1, tab_list2, &meta1); 1107 helper()->AddWindowSpecifics(1, tab_list2, &meta1);
1151 std::vector<sync_pb::SessionSpecifics> tabs2; 1108 std::vector<sync_pb::SessionSpecifics> tabs2;
1152 tabs2.resize(tab_list2.size()); 1109 tabs2.resize(tab_list2.size());
1153 for (size_t i = 0; i < tab_list2.size(); ++i) { 1110 for (size_t i = 0; i < tab_list2.size(); ++i) {
1154 helper()->BuildTabSpecifics(kTag1, 0, tab_list2[i], &tabs2[i]); 1111 helper()->BuildTabSpecifics(kTag1, 0, tab_list2[i], &tabs2[i]);
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
1220 EXPECT_TRUE(changes.empty()); 1177 EXPECT_TRUE(changes.empty());
1221 1178
1222 // Fill an instance of session specifics with a foreign session's data. 1179 // Fill an instance of session specifics with a foreign session's data.
1223 std::vector<sync_pb::SessionSpecifics> tabs; 1180 std::vector<sync_pb::SessionSpecifics> tabs;
1224 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1), 1181 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1),
1225 std::end(kTabIds1)); 1182 std::end(kTabIds1));
1226 sync_pb::SessionSpecifics meta( 1183 sync_pb::SessionSpecifics meta(
1227 helper()->BuildForeignSession(kTag1, tab_list1, &tabs)); 1184 helper()->BuildForeignSession(kTag1, tab_list1, &tabs));
1228 1185
1229 // Update associator with the session's meta node, window, and tabs. 1186 // Update associator with the session's meta node, window, and tabs.
1230 manager()->UpdateTrackerWithForeignSession(meta, base::Time()); 1187 manager()->UpdateTrackerWithSpecifics(meta, base::Time());
1231 for (std::vector<sync_pb::SessionSpecifics>::iterator iter = tabs.begin(); 1188 for (std::vector<sync_pb::SessionSpecifics>::iterator iter = tabs.begin();
1232 iter != tabs.end(); ++iter) { 1189 iter != tabs.end(); ++iter) {
1233 manager()->UpdateTrackerWithForeignSession(*iter, base::Time()); 1190 manager()->UpdateTrackerWithSpecifics(*iter, base::Time());
1234 } 1191 }
1235 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); 1192 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions));
1236 ASSERT_EQ(1U, foreign_sessions.size()); 1193 ASSERT_EQ(1U, foreign_sessions.size());
1237 1194
1238 // Now delete the foreign session. 1195 // Now delete the foreign session.
1239 manager()->DeleteForeignSessionInternal(kTag1, &changes); 1196 manager()->DeleteForeignSessionInternal(kTag1, &changes);
1240 EXPECT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); 1197 EXPECT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions));
1241 1198
1242 EXPECT_EQ(5U, changes.size()); 1199 EXPECT_EQ(5U, changes.size());
1243 ASSERT_TRUE(AllOfChangesAreType(changes, SyncChange::ACTION_DELETE)); 1200 ASSERT_TRUE(AllOfChangesAreType(changes, SyncChange::ACTION_DELETE));
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1350 out.clear(); 1307 out.clear();
1351 manager()->ProcessSyncChanges(FROM_HERE, changes); 1308 manager()->ProcessSyncChanges(FROM_HERE, changes);
1352 EXPECT_TRUE(manager()->local_tab_pool_out_of_sync_); 1309 EXPECT_TRUE(manager()->local_tab_pool_out_of_sync_);
1353 EXPECT_TRUE(out.empty()); // ChangeProcessor shouldn't see any activity. 1310 EXPECT_TRUE(out.empty()); // ChangeProcessor shouldn't see any activity.
1354 1311
1355 // This should trigger repair of the TabNodePool. 1312 // This should trigger repair of the TabNodePool.
1356 AddTab(window_id, kFoo1); 1313 AddTab(window_id, kFoo1);
1357 EXPECT_FALSE(manager()->local_tab_pool_out_of_sync_); 1314 EXPECT_FALSE(manager()->local_tab_pool_out_of_sync_);
1358 1315
1359 // Rebuilding associations will trigger an initial header add and update, 1316 // Rebuilding associations will trigger an initial header add and update,
1360 // coupled with the tab creation/update and the header update to reflect the 1317 // coupled with the tab creation and the header update to reflect the new tab.
1361 // new tab. In total, that means four changes. 1318 // In total, that means four changes.
1362 ASSERT_TRUE( 1319 ASSERT_TRUE(
1363 ChangeTypeMatches(out, {SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE, 1320 ChangeTypeMatches(out,
1364 SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE, 1321 {SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE,
1365 SyncChange::ACTION_UPDATE})); 1322 SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE}));
1366 1323
1367 // Verify the actual content. 1324 // Verify the actual content.
1368 VerifyLocalTabChange(out[3], 1, kFoo1); 1325 VerifyLocalTabChange(out[2], 1, kFoo1);
1369 VerifyLocalHeaderChange(out[4], 1, 1); 1326 VerifyLocalHeaderChange(out[3], 1, 1);
1370 1327
1371 // Verify TabLinks. 1328 // Verify TabLinks.
1372 SessionsSyncManager::TabLinksMap tab_map = manager()->local_tab_map_; 1329 EXPECT_EQ(1U, GetTabPool()->Capacity());
1373 ASSERT_EQ(1U, tab_map.size()); 1330 EXPECT_TRUE(GetTabPool()->Empty());
1374 int tab_node_id = out[3].sync_data().GetSpecifics().session().tab_node_id(); 1331 int tab_node_id = out[2].sync_data().GetSpecifics().session().tab_node_id();
1375 int tab_id = out[3].sync_data().GetSpecifics().session().tab().tab_id(); 1332 int tab_id = out[2].sync_data().GetSpecifics().session().tab().tab_id();
1376 EXPECT_EQ(tab_node_id, tab_map.find(tab_id)->second->tab_node_id()); 1333 EXPECT_EQ(tab_id, GetTabPool()->GetTabIdFromTabNodeId(tab_node_id));
1377 } 1334 }
1378 1335
1379 // Test that receiving a session delete from sync removes the session 1336 // Test that receiving a session delete from sync removes the session
1380 // from tracking. 1337 // from tracking.
1381 TEST_F(SessionsSyncManagerTest, ProcessForeignDelete) { 1338 TEST_F(SessionsSyncManagerTest, ProcessForeignDelete) {
1382 InitWithNoSyncData(); 1339 InitWithNoSyncData();
1383 std::vector<sync_pb::SessionSpecifics> tabs1; 1340 std::vector<sync_pb::SessionSpecifics> tabs1;
1384 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1), 1341 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1),
1385 std::end(kTabIds1)); 1342 std::end(kTabIds1));
1386 sync_pb::SessionSpecifics meta( 1343 sync_pb::SessionSpecifics meta(
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
1510 output.clear(); 1467 output.clear();
1511 1468
1512 // Verify that cleanup post-merge cleanup correctly removes all tabs objects. 1469 // Verify that cleanup post-merge cleanup correctly removes all tabs objects.
1513 const sessions::SessionTab* tab; 1470 const sessions::SessionTab* tab;
1514 ASSERT_FALSE( 1471 ASSERT_FALSE(
1515 manager()->session_tracker_.LookupSessionTab(session_tag, 1, &tab)); 1472 manager()->session_tracker_.LookupSessionTab(session_tag, 1, &tab));
1516 ASSERT_FALSE( 1473 ASSERT_FALSE(
1517 manager()->session_tracker_.LookupSessionTab(session_tag, 2, &tab)); 1474 manager()->session_tracker_.LookupSessionTab(session_tag, 2, &tab));
1518 1475
1519 std::set<int> tab_node_ids; 1476 std::set<int> tab_node_ids;
1520 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); 1477 manager()->session_tracker_.LookupForeignTabNodeIds(session_tag,
1478 &tab_node_ids);
1521 EXPECT_EQ(6U, tab_node_ids.size()); 1479 EXPECT_EQ(6U, tab_node_ids.size());
1522 EXPECT_TRUE(tab_node_ids.find(tab1A.tab_node_id()) != tab_node_ids.end()); 1480 EXPECT_TRUE(tab_node_ids.find(tab1A.tab_node_id()) != tab_node_ids.end());
1523 EXPECT_TRUE(tab_node_ids.find(tab1B.tab_node_id()) != tab_node_ids.end()); 1481 EXPECT_TRUE(tab_node_ids.find(tab1B.tab_node_id()) != tab_node_ids.end());
1524 EXPECT_TRUE(tab_node_ids.find(tab1C.tab_node_id()) != tab_node_ids.end()); 1482 EXPECT_TRUE(tab_node_ids.find(tab1C.tab_node_id()) != tab_node_ids.end());
1525 EXPECT_TRUE(tab_node_ids.find(tab2A.tab_node_id()) != tab_node_ids.end()); 1483 EXPECT_TRUE(tab_node_ids.find(tab2A.tab_node_id()) != tab_node_ids.end());
1526 EXPECT_TRUE(tab_node_ids.find(tab2B.tab_node_id()) != tab_node_ids.end()); 1484 EXPECT_TRUE(tab_node_ids.find(tab2B.tab_node_id()) != tab_node_ids.end());
1527 EXPECT_TRUE(tab_node_ids.find(tab2C.tab_node_id()) != tab_node_ids.end()); 1485 EXPECT_TRUE(tab_node_ids.find(tab2C.tab_node_id()) != tab_node_ids.end());
1528 1486
1529 SyncChangeList changes; 1487 SyncChangeList changes;
1530 changes.push_back(MakeRemoteChange(tab1A, SyncChange::ACTION_DELETE)); 1488 changes.push_back(MakeRemoteChange(tab1A, SyncChange::ACTION_DELETE));
1531 changes.push_back(MakeRemoteChange(tab1B, SyncChange::ACTION_DELETE)); 1489 changes.push_back(MakeRemoteChange(tab1B, SyncChange::ACTION_DELETE));
1532 changes.push_back(MakeRemoteChange(tab2C, SyncChange::ACTION_DELETE)); 1490 changes.push_back(MakeRemoteChange(tab2C, SyncChange::ACTION_DELETE));
1533 manager()->ProcessSyncChanges(FROM_HERE, changes); 1491 manager()->ProcessSyncChanges(FROM_HERE, changes);
1534 1492
1535 tab_node_ids.clear(); 1493 tab_node_ids.clear();
1536 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); 1494 manager()->session_tracker_.LookupForeignTabNodeIds(session_tag,
1495 &tab_node_ids);
1537 EXPECT_EQ(3U, tab_node_ids.size()); 1496 EXPECT_EQ(3U, tab_node_ids.size());
1538 EXPECT_TRUE(tab_node_ids.find(tab1C.tab_node_id()) != tab_node_ids.end()); 1497 EXPECT_TRUE(tab_node_ids.find(tab1C.tab_node_id()) != tab_node_ids.end());
1539 EXPECT_TRUE(tab_node_ids.find(tab2A.tab_node_id()) != tab_node_ids.end()); 1498 EXPECT_TRUE(tab_node_ids.find(tab2A.tab_node_id()) != tab_node_ids.end());
1540 EXPECT_TRUE(tab_node_ids.find(tab2B.tab_node_id()) != tab_node_ids.end()); 1499 EXPECT_TRUE(tab_node_ids.find(tab2B.tab_node_id()) != tab_node_ids.end());
1541 1500
1542 manager()->DoGarbageCollection(); 1501 manager()->DoGarbageCollection();
1543 ASSERT_EQ(3U, output.size()); 1502 ASSERT_EQ(3U, output.size());
1544 } 1503 }
1545 1504
1546 TEST_F(SessionsSyncManagerTest, ProcessForeignDeleteTabsWithReusedNodeIds) { 1505 TEST_F(SessionsSyncManagerTest, ProcessForeignDeleteTabsWithReusedNodeIds) {
(...skipping 18 matching lines...) Expand all
1565 AddToSyncDataList(tab2A, &foreign_data, 1524 AddToSyncDataList(tab2A, &foreign_data,
1566 stale_mtime + base::TimeDelta::FromMinutes(1)); 1525 stale_mtime + base::TimeDelta::FromMinutes(1));
1567 1526
1568 AddWindow(); 1527 AddWindow();
1569 SyncChangeList output; 1528 SyncChangeList output;
1570 InitWithSyncDataTakeOutput(foreign_data, &output); 1529 InitWithSyncDataTakeOutput(foreign_data, &output);
1571 ASSERT_EQ(2U, output.size()); 1530 ASSERT_EQ(2U, output.size());
1572 output.clear(); 1531 output.clear();
1573 1532
1574 std::set<int> tab_node_ids; 1533 std::set<int> tab_node_ids;
1575 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); 1534 manager()->session_tracker_.LookupForeignTabNodeIds(session_tag,
1535 &tab_node_ids);
1576 EXPECT_EQ(2U, tab_node_ids.size()); 1536 EXPECT_EQ(2U, tab_node_ids.size());
1577 EXPECT_TRUE(tab_node_ids.find(tab_node_id_shared) != tab_node_ids.end()); 1537 EXPECT_TRUE(tab_node_ids.find(tab_node_id_shared) != tab_node_ids.end());
1578 EXPECT_TRUE(tab_node_ids.find(tab_node_id_unique) != tab_node_ids.end()); 1538 EXPECT_TRUE(tab_node_ids.find(tab_node_id_unique) != tab_node_ids.end());
1579 1539
1580 SyncChangeList changes; 1540 SyncChangeList changes;
1581 changes.push_back(MakeRemoteChange(tab1A, SyncChange::ACTION_DELETE)); 1541 changes.push_back(MakeRemoteChange(tab1A, SyncChange::ACTION_DELETE));
1582 manager()->ProcessSyncChanges(FROM_HERE, changes); 1542 manager()->ProcessSyncChanges(FROM_HERE, changes);
1583 1543
1584 tab_node_ids.clear(); 1544 tab_node_ids.clear();
1585 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); 1545 manager()->session_tracker_.LookupForeignTabNodeIds(session_tag,
1546 &tab_node_ids);
1586 EXPECT_EQ(1U, tab_node_ids.size()); 1547 EXPECT_EQ(1U, tab_node_ids.size());
1587 EXPECT_TRUE(tab_node_ids.find(tab_node_id_unique) != tab_node_ids.end()); 1548 EXPECT_TRUE(tab_node_ids.find(tab_node_id_unique) != tab_node_ids.end());
1588 1549
1589 manager()->DoGarbageCollection(); 1550 manager()->DoGarbageCollection();
1590 EXPECT_EQ(1U, output.size()); 1551 EXPECT_EQ(1U, output.size());
1591 } 1552 }
1592 1553
1593 TEST_F(SessionsSyncManagerTest, AssociationReusesNodes) { 1554 TEST_F(SessionsSyncManagerTest, AssociationReusesNodes) {
1594 SyncChangeList changes; 1555 SyncChangeList changes;
1595 AddTab(AddWindow()->GetSessionId(), kFoo1); 1556 AddTab(AddWindow()->GetSessionId(), kFoo1);
1596 InitWithSyncDataTakeOutput(SyncDataList(), &changes); 1557 InitWithSyncDataTakeOutput(SyncDataList(), &changes);
1597 ASSERT_TRUE(ChangeTypeMatches( 1558 ASSERT_TRUE(ChangeTypeMatches(changes,
1598 changes, {SyncChange::ACTION_ADD, SyncChange::ACTION_ADD, 1559 {SyncChange::ACTION_ADD, SyncChange::ACTION_ADD,
1599 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE})); 1560 SyncChange::ACTION_UPDATE}));
1600 ASSERT_TRUE(changes[2].sync_data().GetSpecifics().session().has_tab()); 1561 ASSERT_TRUE(changes[1].sync_data().GetSpecifics().session().has_tab());
1601 int tab_node_id = 1562 int tab_node_id =
1602 changes[1].sync_data().GetSpecifics().session().tab_node_id(); 1563 changes[1].sync_data().GetSpecifics().session().tab_node_id();
1603 1564
1604 // Pass back the previous tab and header nodes at association, along with a 1565 // Pass back the previous tab and header nodes at association, along with a
1605 // second tab node (with a rewritten tab node id). 1566 // second tab node (with a rewritten tab node id).
1606 SyncDataList in; 1567 SyncDataList in;
1607 in.push_back( 1568 in.push_back(
1608 CreateRemoteData(changes[3].sync_data().GetSpecifics())); // Header node. 1569 CreateRemoteData(changes[2].sync_data().GetSpecifics())); // Header node.
1609 sync_pb::SessionSpecifics new_tab( 1570 sync_pb::SessionSpecifics new_tab(
1610 changes[2].sync_data().GetSpecifics().session()); 1571 changes[1].sync_data().GetSpecifics().session());
1611 new_tab.set_tab_node_id(tab_node_id + 1); 1572 new_tab.set_tab_node_id(tab_node_id + 1);
1612 in.push_back(CreateRemoteData(new_tab)); // New tab node. 1573 in.push_back(CreateRemoteData(new_tab)); // New tab node.
1613 in.push_back(CreateRemoteData( 1574 in.push_back(CreateRemoteData(
1614 changes[2].sync_data().GetSpecifics())); // Old tab node. 1575 changes[1].sync_data().GetSpecifics())); // Old tab node.
1615 changes.clear(); 1576 changes.clear();
1616 1577
1617 // Reassociate (with the same single tab/window open). 1578 // Reassociate (with the same single tab/window open).
1618 manager()->StopSyncing(syncer::SESSIONS); 1579 manager()->StopSyncing(syncer::SESSIONS);
1619 InitWithSyncDataTakeOutput(in, &changes); 1580 InitWithSyncDataTakeOutput(in, &changes);
1620 1581
1621 // No tab entities should be deleted. The original (lower) tab node id should 1582 // No tab entities should be deleted. The original (lower) tab node id should
1622 // be reused for association. 1583 // be reused for association.
1623 FilterOutLocalHeaderChanges(&changes); 1584 FilterOutLocalHeaderChanges(&changes);
1624 ASSERT_TRUE(ChangeTypeMatches(changes, {SyncChange::ACTION_UPDATE})); 1585 ASSERT_TRUE(ChangeTypeMatches(changes, {SyncChange::ACTION_UPDATE}));
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1703 AddTab(AddWindow()->GetSessionId(), "chrome://preferences/"); 1664 AddTab(AddWindow()->GetSessionId(), "chrome://preferences/");
1704 InitWithSyncDataTakeOutput(SyncDataList(), &out); 1665 InitWithSyncDataTakeOutput(SyncDataList(), &out);
1705 ASSERT_TRUE(ChangeTypeMatches( 1666 ASSERT_TRUE(ChangeTypeMatches(
1706 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE})); 1667 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE}));
1707 VerifyLocalHeaderChange(out[1], 0, 0); 1668 VerifyLocalHeaderChange(out[1], 0, 0);
1708 out.clear(); 1669 out.clear();
1709 1670
1710 // Go to a sync-interesting URL. 1671 // Go to a sync-interesting URL.
1711 NavigateTab(tab, kFoo1); 1672 NavigateTab(tab, kFoo1);
1712 1673
1713 // The tab should be created/updated, coupled with a header update. 1674 // The tab should be created, coupled with a header update.
1714 ASSERT_TRUE( 1675 ASSERT_TRUE(ChangeTypeMatches(
1715 ChangeTypeMatches(out, {SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE, 1676 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE}));
1716 SyncChange::ACTION_UPDATE})); 1677 VerifyLocalTabChange(out[0], 2, kFoo1);
1717 VerifyLocalTabChange(out[1], 2, kFoo1); 1678 VerifyLocalHeaderChange(out[1], 1, 1);
1718 VerifyLocalHeaderChange(out[2], 1, 1);
1719 } 1679 }
1720 1680
1721 // Tests that the SyncSessionManager responds to local tab events properly. 1681 // Tests that the SyncSessionManager responds to local tab events properly.
1722 TEST_F(SessionsSyncManagerTest, OnLocalTabModified) { 1682 TEST_F(SessionsSyncManagerTest, OnLocalTabModified) {
1723 SyncChangeList out; 1683 SyncChangeList out;
1724 // Init with no local data, relies on MergeLocalSessionNoTabs. 1684 // Init with no local data, relies on MergeLocalSessionNoTabs.
1725 SessionID::id_type window_id = AddWindow()->GetSessionId(); 1685 TestSyncedWindowDelegate* window = AddWindow();
1686 SessionID::id_type window_id = window->GetSessionId();
1726 InitWithSyncDataTakeOutput(SyncDataList(), &out); 1687 InitWithSyncDataTakeOutput(SyncDataList(), &out);
1727 ASSERT_FALSE(manager()->current_machine_tag().empty()); 1688 ASSERT_FALSE(manager()->current_machine_tag().empty());
1728 ASSERT_EQ(2U, out.size()); 1689 ASSERT_EQ(2U, out.size());
1729 1690
1730 // Copy the original header. 1691 // Copy the original header.
1731 sync_pb::EntitySpecifics header(out[0].sync_data().GetSpecifics()); 1692 sync_pb::EntitySpecifics header(out[0].sync_data().GetSpecifics());
1732 out.clear(); 1693 out.clear();
1733 1694
1734 NavigateTab(AddTab(window_id, kFoo1), kFoo2); 1695 NavigateTab(AddTab(window_id, kFoo1), kFoo2);
1735 NavigateTab(AddTab(window_id, kBar1), kBar2); 1696 NavigateTab(AddTab(window_id, kBar1), kBar2);
1736 std::vector<std::string> urls = {kFoo1, kFoo2, kBar1, kBar2}; 1697 std::vector<std::string> urls = {kFoo1, kFoo2, kBar1, kBar2};
1737 1698
1738 // Change type breakdown: 1699 // Change type breakdown:
1739 // 1 tab add/update + 2 header updates. 1700 // 1 tab add + 2 header updates.
1740 const size_t kChangesPerTabCreation = 4; 1701 const size_t kChangesPerTabCreation = 3;
1741 // 1 tab update + 1 header update. 1702 // 1 tab update + 1 header update.
1742 const size_t kChangesPerTabNav = 2; 1703 const size_t kChangesPerTabNav = 2;
1743 const size_t kChangesPerTab = kChangesPerTabNav + kChangesPerTabCreation; 1704 const size_t kChangesPerTab = kChangesPerTabNav + kChangesPerTabCreation;
1744 const size_t kNumTabs = 2; 1705 const size_t kNumTabs = 2;
1745 const size_t kTotalUpdates = kChangesPerTab * kNumTabs; 1706 const size_t kTotalUpdates = kChangesPerTab * kNumTabs;
1746 1707
1747 std::vector<SyncChange::SyncChangeType> types = { 1708 std::vector<SyncChange::SyncChangeType> types = {
1748 // Tab 1 1709 // Tab 1
1749 SyncChange::ACTION_UPDATE, SyncChange::ACTION_ADD, 1710 SyncChange::ACTION_UPDATE, SyncChange::ACTION_ADD,
1750 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE, 1711 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE,
1751 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE, 1712 SyncChange::ACTION_UPDATE,
1752 // Tab 2 1713 // Tab 2
1753 SyncChange::ACTION_UPDATE, SyncChange::ACTION_ADD, 1714 SyncChange::ACTION_UPDATE, SyncChange::ACTION_ADD,
1754 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE, 1715 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE,
1755 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE}; 1716 SyncChange::ACTION_UPDATE};
1756 ASSERT_EQ(kTotalUpdates, types.size()); 1717 ASSERT_EQ(kTotalUpdates, types.size());
1757 1718
1758 // Verify the tab node creations and updates to ensure the SyncProcessor sees 1719 // Verify the tab node creations and updates to ensure the SyncProcessor sees
1759 // the right operations. Do this by inspecting the set of changes for each 1720 // the right operations. Do this by inspecting the set of changes for each
1760 // tab separately by iterating through the tabs. 1721 // tab separately by iterating through the tabs.
1761 ASSERT_TRUE(ChangeTypeMatches(out, types)); 1722 ASSERT_TRUE(ChangeTypeMatches(out, types));
1762 for (size_t i = 0; i < kNumTabs; ++i) { 1723 for (size_t i = 0; i < kNumTabs; ++i) {
1763 int index = kChangesPerTab * i; 1724 int index = kChangesPerTab * i;
1764 int nav_per_tab_count = 0; 1725 int nav_per_tab_count = 0;
1765 { 1726 {
1766 SCOPED_TRACE(index); 1727 SCOPED_TRACE(index);
1767 // The initial tab parent event triggers a header update (which is in 1728 // The initial tab parent event triggers a header update (which is in
1768 // effect a no-op). 1729 // effect a no-op).
1769 VerifyLocalHeaderChange(out[index++], (i == 0 ? 0 : 1), i); 1730 VerifyLocalHeaderChange(out[index++], (i == 0 ? 0 : 1), i);
1770 } 1731 }
1771 index++; // Ignore the tab add, which has no useful data.
1772 { 1732 {
1773 SCOPED_TRACE(index); 1733 SCOPED_TRACE(index);
1774 nav_per_tab_count++; 1734 nav_per_tab_count++;
1775 // Tab update after initial creation.. 1735 // Tab update after initial creation..
1776 VerifyLocalTabChange(out[index++], nav_per_tab_count, 1736 VerifyLocalTabChange(out[index++], nav_per_tab_count,
1777 urls[i * kChangesPerTabNav + nav_per_tab_count - 1]); 1737 urls[i * kChangesPerTabNav + nav_per_tab_count - 1]);
1778 } 1738 }
1779 { 1739 {
1780 SCOPED_TRACE(index); 1740 SCOPED_TRACE(index);
1781 // The associate windows after the tab creation. 1741 // The associate windows after the tab creation.
1782 VerifyLocalHeaderChange(out[index++], 1, i + 1); 1742 VerifyLocalHeaderChange(out[index++], 1, i + 1);
1783 } 1743 }
1784 { 1744 {
1785 SCOPED_TRACE(index); 1745 SCOPED_TRACE(index);
1786 nav_per_tab_count++; 1746 nav_per_tab_count++;
1787 // Tab navigation. 1747 // Tab navigation.
1788 VerifyLocalTabChange(out[index++], nav_per_tab_count, 1748 VerifyLocalTabChange(out[index++], nav_per_tab_count,
1789 urls[i * kChangesPerTabNav + nav_per_tab_count - 1]); 1749 urls[i * kChangesPerTabNav + nav_per_tab_count - 1]);
1790 } 1750 }
1791 { 1751 {
1792 SCOPED_TRACE(index); 1752 SCOPED_TRACE(index);
1793 // The associate windows after the tab navigation. 1753 // The associate windows after the tab navigation.
1794 VerifyLocalHeaderChange(out[index++], 1, i + 1); 1754 VerifyLocalHeaderChange(out[index++], 1, i + 1);
1795 } 1755 }
1796 } 1756 }
1797 1757
1798 // Verify tab delegates have Sync ids. 1758 // Verify tab delegates have Sync ids.
1799 std::set<const SyncedWindowDelegate*> window_delegates = 1759 EXPECT_EQ(0, window->GetTabAt(0)->GetSyncId());
1800 window_getter()->GetSyncedWindowDelegates(); 1760 EXPECT_EQ(1, window->GetTabAt(1)->GetSyncId());
1801 EXPECT_EQ(0, (*window_delegates.begin())->GetTabAt(0)->GetSyncId());
1802 EXPECT_EQ(1, (*window_delegates.begin())->GetTabAt(1)->GetSyncId());
1803 } 1761 }
1804 1762
1805 TEST_F(SessionsSyncManagerTest, ForeignSessionModifiedTime) { 1763 TEST_F(SessionsSyncManagerTest, ForeignSessionModifiedTime) {
1806 SyncDataList foreign_data; 1764 SyncDataList foreign_data;
1807 base::Time newest_time = base::Time::Now() - base::TimeDelta::FromDays(1); 1765 base::Time newest_time = base::Time::Now() - base::TimeDelta::FromDays(1);
1808 base::Time middle_time = base::Time::Now() - base::TimeDelta::FromDays(2); 1766 base::Time middle_time = base::Time::Now() - base::TimeDelta::FromDays(2);
1809 base::Time oldest_time = base::Time::Now() - base::TimeDelta::FromDays(3); 1767 base::Time oldest_time = base::Time::Now() - base::TimeDelta::FromDays(3);
1810 1768
1811 { 1769 {
1812 std::string session_tag = "tag1"; 1770 std::string session_tag = "tag1";
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after
2080 EXPECT_TRUE(observer()->notified_of_refresh()); 2038 EXPECT_TRUE(observer()->notified_of_refresh());
2081 } 2039 }
2082 2040
2083 // Tests receipt of duplicate tab IDs in the same window. This should never 2041 // Tests receipt of duplicate tab IDs in the same window. This should never
2084 // happen, but we want to make sure the client won't do anything bad if it does 2042 // happen, but we want to make sure the client won't do anything bad if it does
2085 // receive such garbage input data. 2043 // receive such garbage input data.
2086 TEST_F(SessionsSyncManagerTest, ReceiveDuplicateTabInSameWindow) { 2044 TEST_F(SessionsSyncManagerTest, ReceiveDuplicateTabInSameWindow) {
2087 std::string tag = "tag1"; 2045 std::string tag = "tag1";
2088 2046
2089 // Reuse tab ID 10 in an attempt to trigger bad behavior. 2047 // Reuse tab ID 10 in an attempt to trigger bad behavior.
2090 std::vector<SessionID::id_type> tab_list1 = {5, 10, 10, 17}; 2048 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1),
2049 std::end(kTabIds1));
2091 std::vector<sync_pb::SessionSpecifics> tabs1; 2050 std::vector<sync_pb::SessionSpecifics> tabs1;
2092 sync_pb::SessionSpecifics meta( 2051 sync_pb::SessionSpecifics meta(
2093 helper()->BuildForeignSession(tag, tab_list1, &tabs1)); 2052 helper()->BuildForeignSession(tag, tab_list1, &tabs1));
2094 2053
2095 // Set up initial data. 2054 // Set up initial data.
2096 SyncDataList initial_data; 2055 SyncDataList initial_data;
2097 sync_pb::EntitySpecifics entity; 2056 sync_pb::EntitySpecifics entity;
2098 entity.mutable_session()->CopyFrom(meta); 2057 entity.mutable_session()->CopyFrom(meta);
2099 initial_data.push_back(CreateRemoteData(entity)); 2058 initial_data.push_back(CreateRemoteData(entity));
2100 AddTabsToSyncDataList(tabs1, &initial_data); 2059 AddTabsToSyncDataList(tabs1, &initial_data);
2101 2060
2102 SyncChangeList output; 2061 SyncChangeList output;
2103 InitWithSyncDataTakeOutput(initial_data, &output); 2062 InitWithSyncDataTakeOutput(initial_data, &output);
2104 } 2063 }
2105 2064
2106 // Tests receipt of duplicate tab IDs for the same session. The duplicate tab 2065 // Tests receipt of duplicate tab IDs for the same session. The duplicate tab
2107 // ID is present in two different windows. A client can't be expected to do 2066 // ID is present in two different windows. A client can't be expected to do
2108 // anything reasonable with this input, but we can expect that it doesn't 2067 // anything reasonable with this input, but we can expect that it doesn't
2109 // crash. 2068 // crash.
2110 TEST_F(SessionsSyncManagerTest, ReceiveDuplicateTabInOtherWindow) { 2069 TEST_F(SessionsSyncManagerTest, ReceiveDuplicateTabInOtherWindow) {
2111 std::vector<SessionID::id_type> tab_list1 = {5, 10, 17}; 2070 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1),
2071 std::end(kTabIds1));
2112 std::vector<sync_pb::SessionSpecifics> tabs1; 2072 std::vector<sync_pb::SessionSpecifics> tabs1;
2113 sync_pb::SessionSpecifics meta( 2073 sync_pb::SessionSpecifics meta(
2114 helper()->BuildForeignSession(kTag1, tab_list1, &tabs1)); 2074 helper()->BuildForeignSession(kTag1, tab_list1, &tabs1));
2115 2075
2116 // Add a second window. Tab ID 10 is a duplicate. 2076 // Add a second window. Tab ID 10 is a duplicate.
2117 std::vector<SessionID::id_type> tab_list2 = {10, 18, 20}; 2077 std::vector<SessionID::id_type> tab_list2(std::begin(kTabIds2),
2078 std::end(kTabIds2));
2118 helper()->AddWindowSpecifics(1, tab_list2, &meta); 2079 helper()->AddWindowSpecifics(1, tab_list2, &meta);
2119 2080
2120 // Set up initial data. 2081 // Set up initial data.
2121 SyncDataList initial_data; 2082 SyncDataList initial_data;
2122 sync_pb::EntitySpecifics entity; 2083 sync_pb::EntitySpecifics entity;
2123 entity.mutable_session()->CopyFrom(meta); 2084 entity.mutable_session()->CopyFrom(meta);
2124 initial_data.push_back(CreateRemoteData(entity)); 2085 initial_data.push_back(CreateRemoteData(entity));
2125 AddTabsToSyncDataList(tabs1, &initial_data); 2086 AddTabsToSyncDataList(tabs1, &initial_data);
2126 2087
2127 for (size_t i = 0; i < tab_list2.size(); ++i) { 2088 for (size_t i = 0; i < tab_list2.size(); ++i) {
2128 sync_pb::EntitySpecifics entity; 2089 sync_pb::EntitySpecifics entity;
2129 helper()->BuildTabSpecifics(kTag1, 0, tab_list2[i], 2090 helper()->BuildTabSpecifics(kTag1, 0, tab_list2[i],
2130 entity.mutable_session()); 2091 entity.mutable_session());
2131 initial_data.push_back(CreateRemoteData(entity)); 2092 initial_data.push_back(CreateRemoteData(entity));
2132 } 2093 }
2133 2094
2134 SyncChangeList output; 2095 SyncChangeList output;
2135 InitWithSyncDataTakeOutput(initial_data, &output); 2096 InitWithSyncDataTakeOutput(initial_data, &output);
2136 } 2097 }
2137 2098
2138 // Tests receipt of multiple unassociated tabs and makes sure that 2099 // Tests receipt of multiple unassociated tabs and makes sure that
2139 // the ones with later timestamp win 2100 // the ones with later timestamp win
2140 TEST_F(SessionsSyncManagerTest, ReceiveDuplicateUnassociatedTabs) { 2101 TEST_F(SessionsSyncManagerTest, ReceiveDuplicateUnassociatedTabs) {
2141 std::vector<SessionID::id_type> tab_list1 = {5, 10, 17}; 2102 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1),
2103 std::end(kTabIds1));
2142 std::vector<sync_pb::SessionSpecifics> tabs1; 2104 std::vector<sync_pb::SessionSpecifics> tabs1;
2143 sync_pb::SessionSpecifics meta( 2105 sync_pb::SessionSpecifics meta(
2144 helper()->BuildForeignSession(kTag1, tab_list1, &tabs1)); 2106 helper()->BuildForeignSession(kTag1, tab_list1, &tabs1));
2145 2107
2146 // Set up initial data. 2108 // Set up initial data.
2147 SyncDataList initial_data; 2109 SyncDataList initial_data;
2148 initial_data.push_back(CreateRemoteData(meta)); 2110 initial_data.push_back(CreateRemoteData(meta));
2149 2111
2150 sync_pb::EntitySpecifics entity; 2112 sync_pb::EntitySpecifics entity;
2151 2113
2152 for (size_t i = 0; i < tabs1.size(); ++i) { 2114 for (size_t i = 0; i < tabs1.size(); ++i) {
2153 entity.mutable_session()->CopyFrom(tabs1[i]); 2115 entity.mutable_session()->CopyFrom(tabs1[i]);
2154 initial_data.push_back( 2116 initial_data.push_back(
2155 CreateRemoteData(entity, base::Time::FromDoubleT(2000))); 2117 CreateRemoteData(entity, base::Time::FromDoubleT(2000)));
2156 } 2118 }
2157 2119
2158 // Add two more tabs with duplicating IDs but with different modification 2120 // Add two more tabs with duplicating IDs but with different modification
2159 // times, one before and one after the tabs above. 2121 // times, one before and one after the tabs above.
2160 // These two tabs get a different visual indices to distinguish them from the 2122 // These two tabs get a different visual indices to distinguish them from the
2161 // tabs above that get visual index 1 by default. 2123 // tabs above that get visual index 1 by default.
2162 sync_pb::SessionSpecifics duplicating_tab1; 2124 sync_pb::SessionSpecifics duplicating_tab1;
2163 helper()->BuildTabSpecifics(kTag1, 0, 10, &duplicating_tab1); 2125 helper()->BuildTabSpecifics(kTag1, 0, kTabIds1[1], &duplicating_tab1);
2164 duplicating_tab1.mutable_tab()->set_tab_visual_index(2); 2126 duplicating_tab1.mutable_tab()->set_tab_visual_index(2);
2165 entity.mutable_session()->CopyFrom(duplicating_tab1); 2127 entity.mutable_session()->CopyFrom(duplicating_tab1);
2166 initial_data.push_back( 2128 initial_data.push_back(
2167 CreateRemoteData(entity, base::Time::FromDoubleT(1000))); 2129 CreateRemoteData(entity, base::Time::FromDoubleT(1000)));
2168 2130
2169 sync_pb::SessionSpecifics duplicating_tab2; 2131 sync_pb::SessionSpecifics duplicating_tab2;
2170 helper()->BuildTabSpecifics(kTag1, 0, 17, &duplicating_tab2); 2132 helper()->BuildTabSpecifics(kTag1, 0, kTabIds1[2], &duplicating_tab2);
2171 duplicating_tab2.mutable_tab()->set_tab_visual_index(3); 2133 duplicating_tab2.mutable_tab()->set_tab_visual_index(3);
2172 entity.mutable_session()->CopyFrom(duplicating_tab2); 2134 entity.mutable_session()->CopyFrom(duplicating_tab2);
2173 initial_data.push_back( 2135 initial_data.push_back(
2174 CreateRemoteData(entity, base::Time::FromDoubleT(3000))); 2136 CreateRemoteData(entity, base::Time::FromDoubleT(3000)));
2175 2137
2176 SyncChangeList output; 2138 SyncChangeList output;
2177 InitWithSyncDataTakeOutput(initial_data, &output); 2139 InitWithSyncDataTakeOutput(initial_data, &output);
2178 2140
2179 std::vector<const SyncedSession*> foreign_sessions; 2141 std::vector<const SyncedSession*> foreign_sessions;
2180 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); 2142 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions));
2181 2143
2182 const std::vector<std::unique_ptr<sessions::SessionTab>>& window_tabs = 2144 const std::vector<std::unique_ptr<sessions::SessionTab>>& window_tabs =
2183 foreign_sessions[0]->windows.find(0)->second->tabs; 2145 foreign_sessions[0]->windows.find(0)->second->tabs;
2184 ASSERT_EQ(3U, window_tabs.size()); 2146 ASSERT_EQ(4U, window_tabs.size());
2185 // The first one is from the original set of tabs. 2147 // The first one is from the original set of tabs.
2186 ASSERT_EQ(1, window_tabs[0]->tab_visual_index); 2148 ASSERT_EQ(1, window_tabs[0]->tab_visual_index);
2187 // The one from the original set of tabs wins over duplicating_tab1. 2149 // The one from the original set of tabs wins over duplicating_tab1.
2188 ASSERT_EQ(1, window_tabs[1]->tab_visual_index); 2150 ASSERT_EQ(1, window_tabs[1]->tab_visual_index);
2189 // duplicating_tab2 wins due to the later timestamp. 2151 // duplicating_tab2 wins due to the later timestamp.
2190 ASSERT_EQ(3, window_tabs[2]->tab_visual_index); 2152 ASSERT_EQ(3, window_tabs[2]->tab_visual_index);
2191 } 2153 }
2192 2154
2193 // Verify that GetAllForeignSessions returns all sessions sorted by recency. 2155 // Verify that GetAllForeignSessions returns all sessions sorted by recency.
2194 TEST_F(SessionsSyncManagerTest, GetAllForeignSessions) { 2156 TEST_F(SessionsSyncManagerTest, GetAllForeignSessions) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
2280 TestSyncedTabDelegate* tab2 = AddTab(window->GetSessionId(), kBar1); 2242 TestSyncedTabDelegate* tab2 = AddTab(window->GetSessionId(), kBar1);
2281 NavigateTab(tab2, kBar2); 2243 NavigateTab(tab2, kBar2);
2282 TestSyncedTabDelegate* tab3 = AddTab(window->GetSessionId(), kBaz1); 2244 TestSyncedTabDelegate* tab3 = AddTab(window->GetSessionId(), kBaz1);
2283 NavigateTab(tab3, kBaz2); 2245 NavigateTab(tab3, kBaz2);
2284 2246
2285 SyncDataList in; 2247 SyncDataList in;
2286 SyncChangeList out; 2248 SyncChangeList out;
2287 InitWithSyncDataTakeOutput(in, &out); 2249 InitWithSyncDataTakeOutput(in, &out);
2288 2250
2289 // Should be one header add, 3 tab adds/updates, one header update. 2251 // Should be one header add, 3 tab adds/updates, one header update.
2290 ASSERT_EQ(8U, out.size()); 2252 ASSERT_EQ(5U, out.size());
2291 2253
2292 // Now update the sync data to be: 2254 // Now update the sync data to be:
2293 // * one "normal" fully loaded tab 2255 // * one "normal" fully loaded tab
2294 // * one placeholder tab with no WebContents and a tab_id change 2256 // * one placeholder tab with no WebContents and a tab_id change
2295 // * one placeholder tab with no WebContents and no tab_id change 2257 // * one placeholder tab with no WebContents and no tab_id change
2296 sync_pb::EntitySpecifics t0_entity = out[2].sync_data().GetSpecifics(); 2258 sync_pb::EntitySpecifics t0_entity = out[1].sync_data().GetSpecifics();
2297 sync_pb::EntitySpecifics t1_entity = out[4].sync_data().GetSpecifics(); 2259 sync_pb::EntitySpecifics t1_entity = out[2].sync_data().GetSpecifics();
2298 t1_entity.mutable_session()->mutable_tab()->set_tab_id(kRestoredTabId); 2260 t1_entity.mutable_session()->mutable_tab()->set_tab_id(kRestoredTabId);
2299 sync_pb::EntitySpecifics t2_entity = out[6].sync_data().GetSpecifics(); 2261 sync_pb::EntitySpecifics t2_entity = out[3].sync_data().GetSpecifics();
2300 in.push_back(CreateRemoteData(t0_entity)); 2262 in.push_back(CreateRemoteData(t0_entity));
2301 in.push_back(CreateRemoteData(t1_entity)); 2263 in.push_back(CreateRemoteData(t1_entity));
2302 in.push_back(CreateRemoteData(t2_entity)); 2264 in.push_back(CreateRemoteData(t2_entity));
2303 out.clear(); 2265 out.clear();
2304 manager()->StopSyncing(syncer::SESSIONS); 2266 manager()->StopSyncing(syncer::SESSIONS);
2305 2267
2306 PlaceholderTabDelegate t1_override(kNewTabId, 1); 2268 PlaceholderTabDelegate t1_override(kNewTabId, 1);
2307 PlaceholderTabDelegate t2_override(t2_entity.session().tab().tab_id(), 2); 2269 PlaceholderTabDelegate t2_override(t2_entity.session().tab().tab_id(), 2);
2308 window->OverrideTabAt(1, &t1_override); 2270 window->OverrideTabAt(1, &t1_override);
2309 window->OverrideTabAt(2, &t2_override); 2271 window->OverrideTabAt(2, &t2_override);
(...skipping 18 matching lines...) Expand all
2328 TEST_F(SessionsSyncManagerTest, WindowIdUpdatedOnRestore) { 2290 TEST_F(SessionsSyncManagerTest, WindowIdUpdatedOnRestore) {
2329 const int kNewWindowId = 1337; 2291 const int kNewWindowId = 1337;
2330 SyncDataList in; 2292 SyncDataList in;
2331 SyncChangeList out; 2293 SyncChangeList out;
2332 2294
2333 // Set up one tab and start sync with it. 2295 // Set up one tab and start sync with it.
2334 TestSyncedWindowDelegate* window = AddWindow(); 2296 TestSyncedWindowDelegate* window = AddWindow();
2335 AddTab(window->GetSessionId(), kFoo1); 2297 AddTab(window->GetSessionId(), kFoo1);
2336 InitWithSyncDataTakeOutput(in, &out); 2298 InitWithSyncDataTakeOutput(in, &out);
2337 2299
2338 // Should be one header add, 1 tab add/update, and one header update. 2300 // Should be one header add, 1 tab add, and one header update.
2339 ASSERT_EQ(4U, out.size()); 2301 ASSERT_EQ(3U, out.size());
2340 const sync_pb::EntitySpecifics t0_entity = out[2].sync_data().GetSpecifics(); 2302 const sync_pb::EntitySpecifics t0_entity = out[1].sync_data().GetSpecifics();
2341 ASSERT_TRUE(t0_entity.session().has_tab()); 2303 ASSERT_TRUE(t0_entity.session().has_tab());
2342 2304
2343 in.push_back(CreateRemoteData(t0_entity)); 2305 in.push_back(CreateRemoteData(t0_entity));
2344 out.clear(); 2306 out.clear();
2345 manager()->StopSyncing(syncer::SESSIONS); 2307 manager()->StopSyncing(syncer::SESSIONS);
2346 2308
2347 // Override the tab with a placeholder tab delegate. 2309 // Override the tab with a placeholder tab delegate.
2348 PlaceholderTabDelegate t0_override(t0_entity.session().tab().tab_id(), 2310 PlaceholderTabDelegate t0_override(t0_entity.session().tab().tab_id(),
2349 t0_entity.session().tab_node_id()); 2311 t0_entity.session().tab_node_id());
2350 2312
2351 // Set up the window override with the new window ID and placeholder tab. 2313 // Set up the window override with the new window ID and placeholder tab.
2352 window->OverrideTabAt(0, &t0_override); 2314 window->OverrideTabAt(0, &t0_override);
2353 window->OverrideWindowId(kNewWindowId); 2315 window->OverrideWindowId(kNewWindowId);
2354 InitWithSyncDataTakeOutput(in, &out); 2316 InitWithSyncDataTakeOutput(in, &out);
2355 2317
2356 // There should be one change for t0's window ID update. 2318 // There should be one change for t0's window ID update.
2357 ASSERT_EQ(1U, FilterOutLocalHeaderChanges(&out)->size()); 2319 ASSERT_EQ(1U, FilterOutLocalHeaderChanges(&out)->size());
2358 VerifyLocalTabChange(out[0], 1, kFoo1); 2320 VerifyLocalTabChange(out[0], 1, kFoo1);
2359 EXPECT_EQ(kNewWindowId, 2321 EXPECT_EQ(kNewWindowId,
2360 out[0].sync_data().GetSpecifics().session().tab().window_id()); 2322 out[0].sync_data().GetSpecifics().session().tab().window_id());
2361 } 2323 }
2362 2324
2325 // Ensure that the manager properly ignores a restored placeholder that refers
2326 // to a tab node that doesn't exist
2327 TEST_F(SessionsSyncManagerTest, RestoredPlacholderTabNodeDeleted) {
2328 syncer::SyncDataList in;
2329 syncer::SyncChangeList out;
2330
2331 // Set up one tab and start sync with it.
2332 TestSyncedWindowDelegate* window = AddWindow();
2333 AddTab(window->GetSessionId(), kFoo1);
2334 InitWithSyncDataTakeOutput(in, &out);
2335
2336 // Should be one header add, 1 tab add, and one header update.
2337 ASSERT_EQ(3U, out.size());
2338 const sync_pb::EntitySpecifics t0_entity = out[1].sync_data().GetSpecifics();
2339 ASSERT_TRUE(t0_entity.session().has_tab());
2340
2341 out.clear();
2342 manager()->StopSyncing(syncer::SESSIONS);
2343
2344 // Override the tab with a placeholder tab delegate.
2345 PlaceholderTabDelegate t0_override(t0_entity.session().tab().tab_id(),
2346 t0_entity.session().tab_node_id());
2347
2348 // Override the tab with a placeholder whose sync entity won't exist.
2349 window->OverrideTabAt(0, &t0_override);
2350 InitWithSyncDataTakeOutput(in, &out);
2351
2352 // Because no entities were passed in at associate time, there should be no
2353 // tab changes.
2354 ASSERT_EQ(0U, FilterOutLocalHeaderChanges(&out)->size());
2355 }
2356
2357 // Check the behavior for a placeholder tab in one window being mapped to the
2358 // same sync entity as a tab in another window. If the placeholder is associated
2359 // last, the original tab should be unmapped from the first window, and reused
2360 // by the placeholder in the new window..
2361 TEST_F(SessionsSyncManagerTest, PlaceholderConflictAcrossWindows) {
2362 syncer::SyncDataList in;
2363 syncer::SyncChangeList out;
2364
2365 // First sync with one tab and one window.
2366 TestSyncedWindowDelegate* window = AddWindow();
2367 TestSyncedTabDelegate* tab1 = AddTab(window->GetSessionId(), kFoo1);
2368 InitWithSyncDataTakeOutput(in, &out);
2369 ASSERT_TRUE(out[1].sync_data().GetSpecifics().session().has_tab());
2370 manager()->StopSyncing(syncer::SESSIONS);
2371
2372 // Now create a second window with a placeholder that has the same sync id,
2373 // but a different tab id.
2374 TestSyncedWindowDelegate* window2 = AddWindow();
2375 int sync_id = out[1].sync_data().GetSpecifics().session().tab_node_id();
2376 PlaceholderTabDelegate tab2(SessionID().id(), sync_id);
2377 window2->OverrideTabAt(0, &tab2);
2378
2379 // Resync, reusing the old sync data.
2380 in.push_back(CreateRemoteData(out[0].sync_data().GetSpecifics()));
2381 in.push_back(CreateRemoteData(out[1].sync_data().GetSpecifics()));
2382 out.clear();
2383 InitWithSyncDataTakeOutput(in, &out);
2384
2385 // The tab entity will be overwritten twice. Once with the information for
2386 // tab 1 and then again with the information for tab 2. This will be followed
2387 // by a header change reflecting both tabs.
2388 ASSERT_TRUE(
2389 ChangeTypeMatches(out,
2390 {SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE,
2391 SyncChange::ACTION_UPDATE}));
2392 VerifyLocalHeaderChange(out[2], 2, 2);
2393 VerifyLocalTabChange(out[0], 1, kFoo1);
2394 EXPECT_EQ(sync_id, out[0].sync_data().GetSpecifics().session().tab_node_id());
2395 EXPECT_EQ(tab1->GetSessionId(),
2396 out[0].sync_data().GetSpecifics().session().tab().tab_id());
2397 // Because tab 2 is a placeholder, tab 1's URL will be preserved.
2398 VerifyLocalTabChange(out[1], 1, kFoo1);
2399 EXPECT_EQ(sync_id, out[1].sync_data().GetSpecifics().session().tab_node_id());
2400 EXPECT_EQ(tab2.GetSessionId(),
2401 out[1].sync_data().GetSpecifics().session().tab().tab_id());
2402 EXPECT_EQ(window2->GetSessionId(),
2403 out[1].sync_data().GetSpecifics().session().tab().window_id());
2404 }
2405
2363 } // namespace sync_sessions 2406 } // namespace sync_sessions
OLDNEW
« no previous file with comments | « components/sync_sessions/sessions_sync_manager.cc ('k') | components/sync_sessions/synced_session_tracker.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698