| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/sync_sessions/sessions_sync_manager.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "build/build_config.h" | |
| 14 #include "chrome/browser/sessions/session_tab_helper.h" | |
| 15 #include "chrome/browser/sync/chrome_sync_client.h" | |
| 16 #include "chrome/browser/sync/sessions/notification_service_sessions_router.h" | |
| 17 #include "chrome/browser/ui/sync/browser_synced_window_delegates_getter.h" | |
| 18 #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h" | |
| 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 20 #include "chrome/test/base/browser_with_test_window_test.h" | |
| 21 #include "components/sessions/content/content_serialized_navigation_builder.h" | |
| 22 #include "components/sessions/core/serialized_navigation_entry_test_helper.h" | |
| 23 #include "components/sync/device_info/local_device_info_provider_mock.h" | |
| 24 #include "components/sync/driver/sync_api_component_factory.h" | |
| 25 #include "components/sync/model/attachments/attachment_id.h" | |
| 26 #include "components/sync/model/attachments/attachment_service_proxy_for_test.h" | |
| 27 #include "components/sync/model/sync_error_factory_mock.h" | |
| 28 #include "components/sync_sessions/session_sync_test_helper.h" | |
| 29 #include "components/sync_sessions/sync_sessions_client.h" | |
| 30 #include "components/sync_sessions/synced_tab_delegate.h" | |
| 31 #include "components/sync_sessions/synced_window_delegate.h" | |
| 32 #include "components/sync_sessions/synced_window_delegates_getter.h" | |
| 33 #include "content/public/browser/navigation_entry.h" | |
| 34 #include "content/public/browser/web_contents.h" | |
| 35 #include "testing/gmock/include/gmock/gmock.h" | |
| 36 #include "testing/gtest/include/gtest/gtest.h" | |
| 37 | |
| 38 using content::WebContents; | |
| 39 using sessions::SerializedNavigationEntry; | |
| 40 using sessions::SerializedNavigationEntryTestHelper; | |
| 41 using syncer::DeviceInfo; | |
| 42 using syncer::LocalDeviceInfoProvider; | |
| 43 using syncer::LocalDeviceInfoProviderMock; | |
| 44 using syncer::SyncChange; | |
| 45 using syncer::SyncData; | |
| 46 | |
| 47 namespace sync_sessions { | |
| 48 | |
| 49 namespace { | |
| 50 | |
| 51 class SessionNotificationObserver { | |
| 52 public: | |
| 53 SessionNotificationObserver() | |
| 54 : notified_of_update_(false), notified_of_refresh_(false) {} | |
| 55 void NotifyOfUpdate() { notified_of_update_ = true; } | |
| 56 void NotifyOfRefresh() { notified_of_refresh_ = true; } | |
| 57 | |
| 58 bool notified_of_update() const { return notified_of_update_; } | |
| 59 bool notified_of_refresh() const { return notified_of_refresh_; } | |
| 60 | |
| 61 void Reset() { | |
| 62 notified_of_update_ = false; | |
| 63 notified_of_refresh_ = false; | |
| 64 } | |
| 65 | |
| 66 private: | |
| 67 bool notified_of_update_; | |
| 68 bool notified_of_refresh_; | |
| 69 }; | |
| 70 | |
| 71 class SyncedWindowDelegateOverride : public SyncedWindowDelegate { | |
| 72 public: | |
| 73 explicit SyncedWindowDelegateOverride(const SyncedWindowDelegate* wrapped) | |
| 74 : wrapped_(wrapped) { | |
| 75 } | |
| 76 ~SyncedWindowDelegateOverride() override {} | |
| 77 | |
| 78 bool HasWindow() const override { return wrapped_->HasWindow(); } | |
| 79 | |
| 80 SessionID::id_type GetSessionId() const override { | |
| 81 return session_id_override_ >= 0 ? session_id_override_ | |
| 82 : wrapped_->GetSessionId(); | |
| 83 } | |
| 84 | |
| 85 int GetTabCount() const override { return wrapped_->GetTabCount(); } | |
| 86 | |
| 87 int GetActiveIndex() const override { return wrapped_->GetActiveIndex(); } | |
| 88 | |
| 89 bool IsApp() const override { return wrapped_->IsApp(); } | |
| 90 | |
| 91 bool IsTypeTabbed() const override { return wrapped_->IsTypeTabbed(); } | |
| 92 | |
| 93 bool IsTypePopup() const override { return wrapped_->IsTypePopup(); } | |
| 94 | |
| 95 bool IsTabPinned(const SyncedTabDelegate* tab) const override { | |
| 96 return wrapped_->IsTabPinned(tab); | |
| 97 } | |
| 98 | |
| 99 SyncedTabDelegate* GetTabAt(int index) const override { | |
| 100 if (tab_overrides_.find(index) != tab_overrides_.end()) | |
| 101 return tab_overrides_.find(index)->second; | |
| 102 | |
| 103 return wrapped_->GetTabAt(index); | |
| 104 } | |
| 105 | |
| 106 void OverrideSessionId(SessionID::id_type id) { session_id_override_ = id; } | |
| 107 | |
| 108 void OverrideTabAt(int index, | |
| 109 SyncedTabDelegate* delegate, | |
| 110 SessionID::id_type tab_id) { | |
| 111 tab_overrides_[index] = delegate; | |
| 112 tab_id_overrides_[index] = tab_id; | |
| 113 } | |
| 114 | |
| 115 SessionID::id_type GetTabIdAt(int index) const override { | |
| 116 if (tab_id_overrides_.find(index) != tab_id_overrides_.end()) | |
| 117 return tab_id_overrides_.find(index)->second; | |
| 118 return wrapped_->GetTabIdAt(index); | |
| 119 } | |
| 120 | |
| 121 bool IsSessionRestoreInProgress() const override { | |
| 122 return wrapped_->IsSessionRestoreInProgress(); | |
| 123 } | |
| 124 | |
| 125 bool ShouldSync() const override { return wrapped_->ShouldSync(); } | |
| 126 | |
| 127 private: | |
| 128 std::map<int, SyncedTabDelegate*> tab_overrides_; | |
| 129 std::map<int, SessionID::id_type> tab_id_overrides_; | |
| 130 const SyncedWindowDelegate* const wrapped_; | |
| 131 SessionID::id_type session_id_override_ = -1; | |
| 132 }; | |
| 133 | |
| 134 class TestSyncedWindowDelegatesGetter : public SyncedWindowDelegatesGetter { | |
| 135 public: | |
| 136 TestSyncedWindowDelegatesGetter( | |
| 137 const std::set<const SyncedWindowDelegate*>& delegates) | |
| 138 : delegates_(delegates) {} | |
| 139 | |
| 140 std::set<const SyncedWindowDelegate*> GetSyncedWindowDelegates() override { | |
| 141 return delegates_; | |
| 142 } | |
| 143 | |
| 144 const SyncedWindowDelegate* FindById(SessionID::id_type id) override { | |
| 145 for (auto* window : delegates_) { | |
| 146 if (window->GetSessionId() == id) | |
| 147 return window; | |
| 148 } | |
| 149 return nullptr; | |
| 150 } | |
| 151 | |
| 152 private: | |
| 153 const std::set<const SyncedWindowDelegate*> delegates_; | |
| 154 }; | |
| 155 | |
| 156 class TestSyncProcessorStub : public syncer::SyncChangeProcessor { | |
| 157 public: | |
| 158 explicit TestSyncProcessorStub(syncer::SyncChangeList* output) | |
| 159 : output_(output) {} | |
| 160 syncer::SyncError ProcessSyncChanges( | |
| 161 const tracked_objects::Location& from_here, | |
| 162 const syncer::SyncChangeList& change_list) override { | |
| 163 if (error_.IsSet()) { | |
| 164 syncer::SyncError error = error_; | |
| 165 error_ = syncer::SyncError(); | |
| 166 return error; | |
| 167 } | |
| 168 | |
| 169 if (output_) | |
| 170 output_->insert(output_->end(), change_list.begin(), change_list.end()); | |
| 171 NotifyLocalChangeObservers(); | |
| 172 | |
| 173 return syncer::SyncError(); | |
| 174 } | |
| 175 | |
| 176 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override { | |
| 177 return sync_data_to_return_; | |
| 178 } | |
| 179 | |
| 180 void AddLocalChangeObserver(syncer::LocalChangeObserver* observer) override { | |
| 181 local_change_observers_.AddObserver(observer); | |
| 182 } | |
| 183 void RemoveLocalChangeObserver( | |
| 184 syncer::LocalChangeObserver* observer) override { | |
| 185 local_change_observers_.RemoveObserver(observer); | |
| 186 } | |
| 187 | |
| 188 void NotifyLocalChangeObservers() { | |
| 189 const syncer::SyncChange empty_change; | |
| 190 for (syncer::LocalChangeObserver& observer : local_change_observers_) | |
| 191 observer.OnLocalChange(nullptr, empty_change); | |
| 192 } | |
| 193 | |
| 194 void FailProcessSyncChangesWith(const syncer::SyncError& error) { | |
| 195 error_ = error; | |
| 196 } | |
| 197 | |
| 198 void SetSyncDataToReturn(const syncer::SyncDataList& data) { | |
| 199 sync_data_to_return_ = data; | |
| 200 } | |
| 201 | |
| 202 private: | |
| 203 syncer::SyncError error_; | |
| 204 syncer::SyncChangeList* output_; | |
| 205 syncer::SyncDataList sync_data_to_return_; | |
| 206 base::ObserverList<syncer::LocalChangeObserver> local_change_observers_; | |
| 207 }; | |
| 208 | |
| 209 void ExpectAllOfChangesType(const syncer::SyncChangeList& changes, | |
| 210 const syncer::SyncChange::SyncChangeType type) { | |
| 211 for (const syncer::SyncChange& change : changes) { | |
| 212 EXPECT_EQ(type, change.change_type()); | |
| 213 } | |
| 214 } | |
| 215 | |
| 216 int CountIfTagMatches(const syncer::SyncChangeList& changes, | |
| 217 const std::string& tag) { | |
| 218 return std::count_if( | |
| 219 changes.begin(), changes.end(), [&tag](const syncer::SyncChange& change) { | |
| 220 return change.sync_data().GetSpecifics().session().session_tag() == tag; | |
| 221 }); | |
| 222 } | |
| 223 | |
| 224 int CountIfTagMatches(const std::vector<const SyncedSession*>& sessions, | |
| 225 const std::string& tag) { | |
| 226 return std::count_if(sessions.begin(), sessions.end(), | |
| 227 [&tag](const SyncedSession* session) { | |
| 228 return session->session_tag == tag; | |
| 229 }); | |
| 230 } | |
| 231 | |
| 232 // Creates a field trial with the specified |trial_name| and |group_name| and | |
| 233 // registers an associated |variation_id| for it for the given |service|. | |
| 234 void CreateAndActivateFieldTrial(const std::string& trial_name, | |
| 235 const std::string& group_name, | |
| 236 variations::VariationID variation_id, | |
| 237 variations::IDCollectionKey service) { | |
| 238 base::FieldTrialList::CreateFieldTrial(trial_name, group_name); | |
| 239 variations::AssociateGoogleVariationID(service, trial_name, group_name, | |
| 240 variation_id); | |
| 241 // Access the trial to activate it. | |
| 242 base::FieldTrialList::FindFullName(trial_name); | |
| 243 } | |
| 244 | |
| 245 class DummyRouter : public LocalSessionEventRouter { | |
| 246 public: | |
| 247 ~DummyRouter() override {} | |
| 248 void StartRoutingTo(LocalSessionEventHandler* handler) override {} | |
| 249 void Stop() override {} | |
| 250 }; | |
| 251 | |
| 252 std::unique_ptr<LocalSessionEventRouter> NewDummyRouter() { | |
| 253 return std::unique_ptr<LocalSessionEventRouter>(new DummyRouter()); | |
| 254 } | |
| 255 | |
| 256 // Provides ability to override SyncedWindowDelegatesGetter. | |
| 257 // All other calls are passed through to the original SyncSessionsClient. | |
| 258 class SyncSessionsClientShim : public SyncSessionsClient { | |
| 259 public: | |
| 260 explicit SyncSessionsClientShim(SyncSessionsClient* sync_sessions_client) | |
| 261 : sync_sessions_client_(sync_sessions_client), | |
| 262 synced_window_getter_(nullptr) {} | |
| 263 ~SyncSessionsClientShim() override {} | |
| 264 | |
| 265 bookmarks::BookmarkModel* GetBookmarkModel() override { | |
| 266 return sync_sessions_client_->GetBookmarkModel(); | |
| 267 } | |
| 268 | |
| 269 favicon::FaviconService* GetFaviconService() override { | |
| 270 return sync_sessions_client_->GetFaviconService(); | |
| 271 } | |
| 272 | |
| 273 history::HistoryService* GetHistoryService() override { | |
| 274 return sync_sessions_client_->GetHistoryService(); | |
| 275 } | |
| 276 | |
| 277 bool ShouldSyncURL(const GURL& url) const override { | |
| 278 return sync_sessions_client_->ShouldSyncURL(url); | |
| 279 } | |
| 280 | |
| 281 SyncedWindowDelegatesGetter* GetSyncedWindowDelegatesGetter() override { | |
| 282 // The idea here is to allow the test code override the default | |
| 283 // SyncedWindowDelegatesGetter provided by |sync_sessions_client_|. | |
| 284 // If |synced_window_getter_| is explicitly set, return it; otherwise return | |
| 285 // the default one provided by |sync_sessions_client_|. | |
| 286 return synced_window_getter_ | |
| 287 ? synced_window_getter_ | |
| 288 : sync_sessions_client_->GetSyncedWindowDelegatesGetter(); | |
| 289 } | |
| 290 | |
| 291 std::unique_ptr<LocalSessionEventRouter> GetLocalSessionEventRouter() | |
| 292 override { | |
| 293 return sync_sessions_client_->GetLocalSessionEventRouter(); | |
| 294 } | |
| 295 | |
| 296 void set_synced_window_getter( | |
| 297 SyncedWindowDelegatesGetter* synced_window_getter) { | |
| 298 synced_window_getter_ = synced_window_getter; | |
| 299 } | |
| 300 | |
| 301 private: | |
| 302 SyncSessionsClient* const sync_sessions_client_; | |
| 303 SyncedWindowDelegatesGetter* synced_window_getter_; | |
| 304 }; | |
| 305 | |
| 306 } // namespace | |
| 307 | |
| 308 class SessionsSyncManagerTest | |
| 309 : public BrowserWithTestWindowTest { | |
| 310 protected: | |
| 311 SessionsSyncManagerTest() : test_processor_(nullptr) { | |
| 312 local_device_ = base::MakeUnique<LocalDeviceInfoProviderMock>( | |
| 313 "cache_guid", "Wayne Gretzky's Hacking Box", "Chromium 10k", | |
| 314 "Chrome 10k", sync_pb::SyncEnums_DeviceType_TYPE_LINUX, "device_id"); | |
| 315 } | |
| 316 | |
| 317 void SetUp() override { | |
| 318 BrowserWithTestWindowTest::SetUp(); | |
| 319 sync_client_ = base::MakeUnique<browser_sync::ChromeSyncClient>(profile()); | |
| 320 sessions_client_shim_ = base::MakeUnique<SyncSessionsClientShim>( | |
| 321 sync_client_->GetSyncSessionsClient()); | |
| 322 NotificationServiceSessionsRouter* router( | |
| 323 new NotificationServiceSessionsRouter( | |
| 324 profile(), GetSyncSessionsClient(), | |
| 325 syncer::SyncableService::StartSyncFlare())); | |
| 326 sync_prefs_ = base::MakeUnique<syncer::SyncPrefs>(profile()->GetPrefs()); | |
| 327 manager_ = base::MakeUnique<SessionsSyncManager>( | |
| 328 GetSyncSessionsClient(), sync_prefs_.get(), local_device_.get(), | |
| 329 std::unique_ptr<LocalSessionEventRouter>(router), | |
| 330 base::Bind(&SessionNotificationObserver::NotifyOfUpdate, | |
| 331 base::Unretained(&observer_)), | |
| 332 base::Bind(&SessionNotificationObserver::NotifyOfRefresh, | |
| 333 base::Unretained(&observer_))); | |
| 334 } | |
| 335 | |
| 336 void TearDown() override { | |
| 337 test_processor_ = nullptr; | |
| 338 helper()->Reset(); | |
| 339 sync_prefs_.reset(); | |
| 340 manager_.reset(); | |
| 341 BrowserWithTestWindowTest::TearDown(); | |
| 342 } | |
| 343 | |
| 344 const DeviceInfo* GetLocalDeviceInfo() { | |
| 345 return local_device_->GetLocalDeviceInfo(); | |
| 346 } | |
| 347 | |
| 348 SessionsSyncManager* manager() { return manager_.get(); } | |
| 349 SessionSyncTestHelper* helper() { return &helper_; } | |
| 350 LocalDeviceInfoProvider* local_device() { return local_device_.get(); } | |
| 351 SessionNotificationObserver* observer() { return &observer_; } | |
| 352 | |
| 353 void InitWithSyncDataTakeOutput(const syncer::SyncDataList& initial_data, | |
| 354 syncer::SyncChangeList* output) { | |
| 355 test_processor_ = new TestSyncProcessorStub(output); | |
| 356 syncer::SyncMergeResult result = manager_->MergeDataAndStartSyncing( | |
| 357 syncer::SESSIONS, initial_data, | |
| 358 std::unique_ptr<syncer::SyncChangeProcessor>(test_processor_), | |
| 359 std::unique_ptr<syncer::SyncErrorFactory>( | |
| 360 new syncer::SyncErrorFactoryMock())); | |
| 361 EXPECT_FALSE(result.error().IsSet()); | |
| 362 } | |
| 363 | |
| 364 void InitWithNoSyncData() { | |
| 365 InitWithSyncDataTakeOutput(syncer::SyncDataList(), nullptr); | |
| 366 } | |
| 367 | |
| 368 void TriggerProcessSyncChangesError() { | |
| 369 test_processor_->FailProcessSyncChangesWith(syncer::SyncError( | |
| 370 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, "Error", | |
| 371 syncer::SESSIONS)); | |
| 372 } | |
| 373 | |
| 374 void SetSyncData(const syncer::SyncDataList& data) { | |
| 375 test_processor_->SetSyncDataToReturn(data); | |
| 376 } | |
| 377 | |
| 378 syncer::SyncChangeList* FilterOutLocalHeaderChanges( | |
| 379 syncer::SyncChangeList* list) { | |
| 380 syncer::SyncChangeList::iterator it = list->begin(); | |
| 381 bool found = false; | |
| 382 while (it != list->end()) { | |
| 383 if (it->sync_data().IsLocal() && | |
| 384 syncer::SyncDataLocal(it->sync_data()).GetTag() == | |
| 385 manager_->current_machine_tag()) { | |
| 386 EXPECT_TRUE(SyncChange::ACTION_ADD == it->change_type() || | |
| 387 SyncChange::ACTION_UPDATE == it->change_type()); | |
| 388 it = list->erase(it); | |
| 389 found = true; | |
| 390 } else { | |
| 391 ++it; | |
| 392 } | |
| 393 } | |
| 394 EXPECT_TRUE(found); | |
| 395 return list; | |
| 396 } | |
| 397 | |
| 398 SyncSessionsClient* GetSyncSessionsClient() { | |
| 399 return sessions_client_shim_.get(); | |
| 400 } | |
| 401 | |
| 402 syncer::SyncPrefs* sync_prefs() { return sync_prefs_.get(); } | |
| 403 | |
| 404 SyncedWindowDelegatesGetter* get_synced_window_getter() { | |
| 405 return manager()->synced_window_delegates_getter(); | |
| 406 } | |
| 407 | |
| 408 void set_synced_window_getter( | |
| 409 SyncedWindowDelegatesGetter* synced_window_getter) { | |
| 410 sessions_client_shim_->set_synced_window_getter(synced_window_getter); | |
| 411 } | |
| 412 | |
| 413 syncer::SyncChange MakeRemoteChange( | |
| 414 const sync_pb::SessionSpecifics& specifics, | |
| 415 SyncChange::SyncChangeType type) const { | |
| 416 return syncer::SyncChange(FROM_HERE, type, CreateRemoteData(specifics)); | |
| 417 } | |
| 418 | |
| 419 void AddTabsToChangeList(const std::vector<sync_pb::SessionSpecifics>& batch, | |
| 420 SyncChange::SyncChangeType type, | |
| 421 syncer::SyncChangeList* change_list) const { | |
| 422 for (const auto& specifics : batch) { | |
| 423 change_list->push_back( | |
| 424 syncer::SyncChange(FROM_HERE, type, CreateRemoteData(specifics))); | |
| 425 } | |
| 426 } | |
| 427 | |
| 428 void AddToSyncDataList(const sync_pb::SessionSpecifics& specifics, | |
| 429 syncer::SyncDataList* list, | |
| 430 base::Time mtime) const { | |
| 431 list->push_back(CreateRemoteData(specifics, mtime)); | |
| 432 } | |
| 433 | |
| 434 void AddTabsToSyncDataList(const std::vector<sync_pb::SessionSpecifics>& tabs, | |
| 435 syncer::SyncDataList* list) const { | |
| 436 for (size_t i = 0; i < tabs.size(); i++) { | |
| 437 AddToSyncDataList(tabs[i], list, base::Time::FromInternalValue(i + 1)); | |
| 438 } | |
| 439 } | |
| 440 | |
| 441 syncer::SyncData CreateRemoteData(const sync_pb::SessionSpecifics& specifics, | |
| 442 base::Time mtime = base::Time()) const { | |
| 443 sync_pb::EntitySpecifics entity; | |
| 444 entity.mutable_session()->CopyFrom(specifics); | |
| 445 return CreateRemoteData(entity, mtime); | |
| 446 } | |
| 447 | |
| 448 syncer::SyncData CreateRemoteData(const sync_pb::EntitySpecifics& entity, | |
| 449 base::Time mtime = base::Time()) const { | |
| 450 // The server ID is never relevant to these tests, so just use 1. | |
| 451 return SyncData::CreateRemoteData( | |
| 452 1, entity, mtime, syncer::AttachmentIdList(), | |
| 453 syncer::AttachmentServiceProxyForTest::Create(), | |
| 454 SessionsSyncManager::TagHashFromSpecifics(entity.session())); | |
| 455 } | |
| 456 | |
| 457 private: | |
| 458 std::unique_ptr<browser_sync::ChromeSyncClient> sync_client_; | |
| 459 std::unique_ptr<SyncSessionsClientShim> sessions_client_shim_; | |
| 460 std::unique_ptr<syncer::SyncPrefs> sync_prefs_; | |
| 461 SessionNotificationObserver observer_; | |
| 462 std::unique_ptr<SessionsSyncManager> manager_; | |
| 463 SessionSyncTestHelper helper_; | |
| 464 TestSyncProcessorStub* test_processor_; | |
| 465 std::unique_ptr<LocalDeviceInfoProviderMock> local_device_; | |
| 466 }; | |
| 467 | |
| 468 // Test that the SyncSessionManager can properly fill in a SessionHeader. | |
| 469 TEST_F(SessionsSyncManagerTest, PopulateSessionHeader) { | |
| 470 sync_pb::SessionHeader header_s; | |
| 471 header_s.set_client_name("Client 1"); | |
| 472 header_s.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_WIN); | |
| 473 | |
| 474 SyncedSession session; | |
| 475 base::Time time = base::Time::Now(); | |
| 476 SessionsSyncManager::PopulateSessionHeaderFromSpecifics( | |
| 477 header_s, time, &session); | |
| 478 ASSERT_EQ("Client 1", session.session_name); | |
| 479 ASSERT_EQ(SyncedSession::TYPE_WIN, session.device_type); | |
| 480 ASSERT_EQ(time, session.modified_time); | |
| 481 } | |
| 482 | |
| 483 // Test translation between protobuf types and chrome session types. | |
| 484 TEST_F(SessionsSyncManagerTest, PopulateSessionWindow) { | |
| 485 sync_pb::SessionWindow window_s; | |
| 486 window_s.add_tab(0); | |
| 487 window_s.set_browser_type(sync_pb::SessionWindow_BrowserType_TYPE_TABBED); | |
| 488 window_s.set_selected_tab_index(1); | |
| 489 | |
| 490 std::string tag = "tag"; | |
| 491 SyncedSession* session = manager()->session_tracker_.GetSession(tag); | |
| 492 manager()->session_tracker_.PutWindowInSession(tag, 0); | |
| 493 manager()->BuildSyncedSessionFromSpecifics(tag, window_s, base::Time(), | |
| 494 session->windows[0].get()); | |
| 495 ASSERT_EQ(1U, session->windows[0]->tabs.size()); | |
| 496 ASSERT_EQ(1, session->windows[0]->selected_tab_index); | |
| 497 ASSERT_EQ(sessions::SessionWindow::TYPE_TABBED, session->windows[0]->type); | |
| 498 ASSERT_EQ(1U, manager()->session_tracker_.num_synced_sessions()); | |
| 499 ASSERT_EQ(1U, | |
| 500 manager()->session_tracker_.num_synced_tabs(std::string("tag"))); | |
| 501 } | |
| 502 | |
| 503 namespace { | |
| 504 | |
| 505 class SyncedTabDelegateFake : public SyncedTabDelegate { | |
| 506 public: | |
| 507 SyncedTabDelegateFake() | |
| 508 : current_entry_index_(0), is_supervised_(false), sync_id_(-1) {} | |
| 509 ~SyncedTabDelegateFake() override {} | |
| 510 | |
| 511 bool IsInitialBlankNavigation() const override { | |
| 512 // This differs from NavigationControllerImpl, which has an initial blank | |
| 513 // NavigationEntry. | |
| 514 return GetEntryCount() == 0; | |
| 515 } | |
| 516 int GetCurrentEntryIndex() const override { return current_entry_index_; } | |
| 517 void set_current_entry_index(int i) { | |
| 518 current_entry_index_ = i; | |
| 519 } | |
| 520 | |
| 521 void AppendEntry(std::unique_ptr<content::NavigationEntry> entry) { | |
| 522 entries_.push_back(std::move(entry)); | |
| 523 } | |
| 524 | |
| 525 GURL GetVirtualURLAtIndex(int i) const override { | |
| 526 if (static_cast<size_t>(i) >= entries_.size()) | |
| 527 return GURL(); | |
| 528 return entries_[i]->GetVirtualURL(); | |
| 529 } | |
| 530 | |
| 531 GURL GetFaviconURLAtIndex(int i) const override { return GURL(); } | |
| 532 | |
| 533 ui::PageTransition GetTransitionAtIndex(int i) const override { | |
| 534 if (static_cast<size_t>(i) >= entries_.size()) | |
| 535 return ui::PAGE_TRANSITION_LINK; | |
| 536 return entries_[i]->GetTransitionType(); | |
| 537 } | |
| 538 | |
| 539 void GetSerializedNavigationAtIndex( | |
| 540 int i, | |
| 541 sessions::SerializedNavigationEntry* serialized_entry) const override { | |
| 542 if (static_cast<size_t>(i) >= entries_.size()) | |
| 543 return; | |
| 544 *serialized_entry = | |
| 545 sessions::ContentSerializedNavigationBuilder::FromNavigationEntry( | |
| 546 i, *entries_[i]); | |
| 547 } | |
| 548 | |
| 549 int GetEntryCount() const override { return entries_.size(); } | |
| 550 | |
| 551 SessionID::id_type GetWindowId() const override { | |
| 552 return SessionID::id_type(); | |
| 553 } | |
| 554 | |
| 555 SessionID::id_type GetSessionId() const override { | |
| 556 return SessionID::id_type(); | |
| 557 } | |
| 558 | |
| 559 bool IsBeingDestroyed() const override { return false; } | |
| 560 std::string GetExtensionAppId() const override { return std::string(); } | |
| 561 bool ProfileIsSupervised() const override { return is_supervised_; } | |
| 562 void set_is_supervised(bool is_supervised) { is_supervised_ = is_supervised; } | |
| 563 const std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>>* | |
| 564 GetBlockedNavigations() const override { | |
| 565 return &blocked_navigations_; | |
| 566 } | |
| 567 void set_blocked_navigations( | |
| 568 std::vector<const content::NavigationEntry*>* navs) { | |
| 569 for (auto* entry : *navs) { | |
| 570 auto serialized_entry = | |
| 571 base::MakeUnique<sessions::SerializedNavigationEntry>( | |
| 572 sessions::ContentSerializedNavigationBuilder::FromNavigationEntry( | |
| 573 blocked_navigations_.size(), *entry)); | |
| 574 blocked_navigations_.push_back(std::move(serialized_entry)); | |
| 575 } | |
| 576 } | |
| 577 bool IsPlaceholderTab() const override { return true; } | |
| 578 | |
| 579 // Session sync related methods. | |
| 580 int GetSyncId() const override { return sync_id_; } | |
| 581 void SetSyncId(int sync_id) override { sync_id_ = sync_id; } | |
| 582 | |
| 583 bool ShouldSync(SyncSessionsClient* sessions_client) override { | |
| 584 return false; | |
| 585 } | |
| 586 | |
| 587 void reset() { | |
| 588 current_entry_index_ = 0; | |
| 589 sync_id_ = -1; | |
| 590 entries_.clear(); | |
| 591 } | |
| 592 | |
| 593 private: | |
| 594 int current_entry_index_; | |
| 595 bool is_supervised_; | |
| 596 int sync_id_; | |
| 597 std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>> | |
| 598 blocked_navigations_; | |
| 599 std::vector<std::unique_ptr<content::NavigationEntry>> entries_; | |
| 600 }; | |
| 601 | |
| 602 } // namespace | |
| 603 | |
| 604 static const base::Time kTime0 = base::Time::FromInternalValue(100); | |
| 605 static const base::Time kTime1 = base::Time::FromInternalValue(110); | |
| 606 static const base::Time kTime2 = base::Time::FromInternalValue(120); | |
| 607 static const base::Time kTime3 = base::Time::FromInternalValue(130); | |
| 608 static const base::Time kTime4 = base::Time::FromInternalValue(140); | |
| 609 static const base::Time kTime5 = base::Time::FromInternalValue(150); | |
| 610 static const base::Time kTime6 = base::Time::FromInternalValue(160); | |
| 611 static const base::Time kTime7 = base::Time::FromInternalValue(170); | |
| 612 static const base::Time kTime8 = base::Time::FromInternalValue(180); | |
| 613 static const base::Time kTime9 = base::Time::FromInternalValue(190); | |
| 614 | |
| 615 // Populate the mock tab delegate with some data and navigation | |
| 616 // entries and make sure that setting a SessionTab from it preserves | |
| 617 // those entries (and clobbers any existing data). | |
| 618 TEST_F(SessionsSyncManagerTest, SetSessionTabFromDelegate) { | |
| 619 // Create a tab with three valid entries. | |
| 620 SyncedTabDelegateFake tab; | |
| 621 std::unique_ptr<content::NavigationEntry> entry1( | |
| 622 content::NavigationEntry::Create()); | |
| 623 GURL url1("http://www.google.com/"); | |
| 624 entry1->SetVirtualURL(url1); | |
| 625 entry1->SetTimestamp(kTime1); | |
| 626 entry1->SetHttpStatusCode(200); | |
| 627 std::unique_ptr<content::NavigationEntry> entry2( | |
| 628 content::NavigationEntry::Create()); | |
| 629 GURL url2("http://www.noodle.com/"); | |
| 630 entry2->SetVirtualURL(url2); | |
| 631 entry2->SetTimestamp(kTime2); | |
| 632 entry2->SetHttpStatusCode(201); | |
| 633 std::unique_ptr<content::NavigationEntry> entry3( | |
| 634 content::NavigationEntry::Create()); | |
| 635 GURL url3("http://www.doodle.com/"); | |
| 636 entry3->SetVirtualURL(url3); | |
| 637 entry3->SetTimestamp(kTime3); | |
| 638 entry3->SetHttpStatusCode(202); | |
| 639 | |
| 640 tab.AppendEntry(std::move(entry1)); | |
| 641 tab.AppendEntry(std::move(entry2)); | |
| 642 tab.AppendEntry(std::move(entry3)); | |
| 643 tab.set_current_entry_index(2); | |
| 644 | |
| 645 sessions::SessionTab session_tab; | |
| 646 session_tab.window_id.set_id(1); | |
| 647 session_tab.tab_id.set_id(1); | |
| 648 session_tab.tab_visual_index = 1; | |
| 649 session_tab.current_navigation_index = 1; | |
| 650 session_tab.pinned = true; | |
| 651 session_tab.extension_app_id = "app id"; | |
| 652 session_tab.user_agent_override = "override"; | |
| 653 session_tab.timestamp = kTime5; | |
| 654 session_tab.navigations.push_back( | |
| 655 SerializedNavigationEntryTestHelper::CreateNavigation( | |
| 656 "http://www.example.com", "Example")); | |
| 657 session_tab.session_storage_persistent_id = "persistent id"; | |
| 658 manager()->SetSessionTabFromDelegate(tab, kTime4, &session_tab); | |
| 659 | |
| 660 EXPECT_EQ(0, session_tab.window_id.id()); | |
| 661 EXPECT_EQ(0, session_tab.tab_id.id()); | |
| 662 EXPECT_EQ(0, session_tab.tab_visual_index); | |
| 663 EXPECT_EQ(2, session_tab.current_navigation_index); | |
| 664 EXPECT_FALSE(session_tab.pinned); | |
| 665 EXPECT_TRUE(session_tab.extension_app_id.empty()); | |
| 666 EXPECT_TRUE(session_tab.user_agent_override.empty()); | |
| 667 EXPECT_EQ(kTime4, session_tab.timestamp); | |
| 668 ASSERT_EQ(3u, session_tab.navigations.size()); | |
| 669 EXPECT_EQ(url1, session_tab.navigations[0].virtual_url()); | |
| 670 EXPECT_EQ(url2, session_tab.navigations[1].virtual_url()); | |
| 671 EXPECT_EQ(url3, session_tab.navigations[2].virtual_url()); | |
| 672 EXPECT_EQ(kTime1, session_tab.navigations[0].timestamp()); | |
| 673 EXPECT_EQ(kTime2, session_tab.navigations[1].timestamp()); | |
| 674 EXPECT_EQ(kTime3, session_tab.navigations[2].timestamp()); | |
| 675 EXPECT_EQ(200, session_tab.navigations[0].http_status_code()); | |
| 676 EXPECT_EQ(201, session_tab.navigations[1].http_status_code()); | |
| 677 EXPECT_EQ(202, session_tab.navigations[2].http_status_code()); | |
| 678 EXPECT_EQ(SerializedNavigationEntry::STATE_INVALID, | |
| 679 session_tab.navigations[0].blocked_state()); | |
| 680 EXPECT_EQ(SerializedNavigationEntry::STATE_INVALID, | |
| 681 session_tab.navigations[1].blocked_state()); | |
| 682 EXPECT_EQ(SerializedNavigationEntry::STATE_INVALID, | |
| 683 session_tab.navigations[2].blocked_state()); | |
| 684 EXPECT_TRUE(session_tab.session_storage_persistent_id.empty()); | |
| 685 } | |
| 686 | |
| 687 // Ensure the current_navigation_index gets set properly when the navigation | |
| 688 // stack gets trucated to +/- 6 entries. | |
| 689 TEST_F(SessionsSyncManagerTest, SetSessionTabFromDelegateNavigationIndex) { | |
| 690 SyncedTabDelegateFake tab; | |
| 691 std::unique_ptr<content::NavigationEntry> entry0( | |
| 692 content::NavigationEntry::Create()); | |
| 693 GURL url0("http://www.google.com/"); | |
| 694 entry0->SetVirtualURL(url0); | |
| 695 entry0->SetTimestamp(kTime0); | |
| 696 entry0->SetHttpStatusCode(200); | |
| 697 std::unique_ptr<content::NavigationEntry> entry1( | |
| 698 content::NavigationEntry::Create()); | |
| 699 GURL url1("http://www.zoogle.com/"); | |
| 700 entry1->SetVirtualURL(url1); | |
| 701 entry1->SetTimestamp(kTime1); | |
| 702 entry1->SetHttpStatusCode(200); | |
| 703 std::unique_ptr<content::NavigationEntry> entry2( | |
| 704 content::NavigationEntry::Create()); | |
| 705 GURL url2("http://www.noogle.com/"); | |
| 706 entry2->SetVirtualURL(url2); | |
| 707 entry2->SetTimestamp(kTime2); | |
| 708 entry2->SetHttpStatusCode(200); | |
| 709 std::unique_ptr<content::NavigationEntry> entry3( | |
| 710 content::NavigationEntry::Create()); | |
| 711 GURL url3("http://www.doogle.com/"); | |
| 712 entry3->SetVirtualURL(url3); | |
| 713 entry3->SetTimestamp(kTime3); | |
| 714 entry3->SetHttpStatusCode(200); | |
| 715 std::unique_ptr<content::NavigationEntry> entry4( | |
| 716 content::NavigationEntry::Create()); | |
| 717 GURL url4("http://www.yoogle.com/"); | |
| 718 entry4->SetVirtualURL(url4); | |
| 719 entry4->SetTimestamp(kTime4); | |
| 720 entry4->SetHttpStatusCode(200); | |
| 721 std::unique_ptr<content::NavigationEntry> entry5( | |
| 722 content::NavigationEntry::Create()); | |
| 723 GURL url5("http://www.foogle.com/"); | |
| 724 entry5->SetVirtualURL(url5); | |
| 725 entry5->SetTimestamp(kTime5); | |
| 726 entry5->SetHttpStatusCode(200); | |
| 727 std::unique_ptr<content::NavigationEntry> entry6( | |
| 728 content::NavigationEntry::Create()); | |
| 729 GURL url6("http://www.boogle.com/"); | |
| 730 entry6->SetVirtualURL(url6); | |
| 731 entry6->SetTimestamp(kTime6); | |
| 732 entry6->SetHttpStatusCode(200); | |
| 733 std::unique_ptr<content::NavigationEntry> entry7( | |
| 734 content::NavigationEntry::Create()); | |
| 735 GURL url7("http://www.moogle.com/"); | |
| 736 entry7->SetVirtualURL(url7); | |
| 737 entry7->SetTimestamp(kTime7); | |
| 738 entry7->SetHttpStatusCode(200); | |
| 739 std::unique_ptr<content::NavigationEntry> entry8( | |
| 740 content::NavigationEntry::Create()); | |
| 741 GURL url8("http://www.poogle.com/"); | |
| 742 entry8->SetVirtualURL(url8); | |
| 743 entry8->SetTimestamp(kTime8); | |
| 744 entry8->SetHttpStatusCode(200); | |
| 745 std::unique_ptr<content::NavigationEntry> entry9( | |
| 746 content::NavigationEntry::Create()); | |
| 747 GURL url9("http://www.roogle.com/"); | |
| 748 entry9->SetVirtualURL(url9); | |
| 749 entry9->SetTimestamp(kTime9); | |
| 750 entry9->SetHttpStatusCode(200); | |
| 751 | |
| 752 tab.AppendEntry(std::move(entry0)); | |
| 753 tab.AppendEntry(std::move(entry1)); | |
| 754 tab.AppendEntry(std::move(entry2)); | |
| 755 tab.AppendEntry(std::move(entry3)); | |
| 756 tab.AppendEntry(std::move(entry4)); | |
| 757 tab.AppendEntry(std::move(entry5)); | |
| 758 tab.AppendEntry(std::move(entry6)); | |
| 759 tab.AppendEntry(std::move(entry7)); | |
| 760 tab.AppendEntry(std::move(entry8)); | |
| 761 tab.AppendEntry(std::move(entry9)); | |
| 762 tab.set_current_entry_index(8); | |
| 763 | |
| 764 sessions::SessionTab session_tab; | |
| 765 manager()->SetSessionTabFromDelegate(tab, kTime9, &session_tab); | |
| 766 | |
| 767 EXPECT_EQ(6, session_tab.current_navigation_index); | |
| 768 ASSERT_EQ(8u, session_tab.navigations.size()); | |
| 769 EXPECT_EQ(url2, session_tab.navigations[0].virtual_url()); | |
| 770 EXPECT_EQ(url3, session_tab.navigations[1].virtual_url()); | |
| 771 EXPECT_EQ(url4, session_tab.navigations[2].virtual_url()); | |
| 772 } | |
| 773 | |
| 774 // Ensure the current_navigation_index gets set to the end of the navigation | |
| 775 // stack if the current navigation is invalid. | |
| 776 TEST_F(SessionsSyncManagerTest, SetSessionTabFromDelegateCurrentInvalid) { | |
| 777 SyncedTabDelegateFake tab; | |
| 778 std::unique_ptr<content::NavigationEntry> entry0( | |
| 779 content::NavigationEntry::Create()); | |
| 780 entry0->SetVirtualURL(GURL("http://www.google.com")); | |
| 781 entry0->SetTimestamp(kTime0); | |
| 782 entry0->SetHttpStatusCode(200); | |
| 783 std::unique_ptr<content::NavigationEntry> entry1( | |
| 784 content::NavigationEntry::Create()); | |
| 785 entry1->SetVirtualURL(GURL("")); | |
| 786 entry1->SetTimestamp(kTime1); | |
| 787 entry1->SetHttpStatusCode(200); | |
| 788 std::unique_ptr<content::NavigationEntry> entry2( | |
| 789 content::NavigationEntry::Create()); | |
| 790 entry2->SetVirtualURL(GURL("http://www.noogle.com")); | |
| 791 entry2->SetTimestamp(kTime2); | |
| 792 entry2->SetHttpStatusCode(200); | |
| 793 std::unique_ptr<content::NavigationEntry> entry3( | |
| 794 content::NavigationEntry::Create()); | |
| 795 entry3->SetVirtualURL(GURL("http://www.doogle.com")); | |
| 796 entry3->SetTimestamp(kTime3); | |
| 797 entry3->SetHttpStatusCode(200); | |
| 798 | |
| 799 tab.AppendEntry(std::move(entry0)); | |
| 800 tab.AppendEntry(std::move(entry1)); | |
| 801 tab.AppendEntry(std::move(entry2)); | |
| 802 tab.AppendEntry(std::move(entry3)); | |
| 803 tab.set_current_entry_index(1); | |
| 804 | |
| 805 sessions::SessionTab session_tab; | |
| 806 manager()->SetSessionTabFromDelegate(tab, kTime9, &session_tab); | |
| 807 | |
| 808 EXPECT_EQ(2, session_tab.current_navigation_index); | |
| 809 ASSERT_EQ(3u, session_tab.navigations.size()); | |
| 810 } | |
| 811 | |
| 812 // Tests that variation ids are set correctly. | |
| 813 TEST_F(SessionsSyncManagerTest, SetVariationIds) { | |
| 814 // Create two trials with a group which has a variation id for Chrome Sync | |
| 815 // and one with a variation id for another service. | |
| 816 const variations::VariationID kVariationId1 = 3300200; | |
| 817 const variations::VariationID kVariationId2 = 3300300; | |
| 818 const variations::VariationID kVariationId3 = 3300400; | |
| 819 | |
| 820 base::FieldTrialList field_trial_list(nullptr); | |
| 821 CreateAndActivateFieldTrial("trial name 1", "group name", kVariationId1, | |
| 822 variations::CHROME_SYNC_SERVICE); | |
| 823 CreateAndActivateFieldTrial("trial name 2", "group name", kVariationId2, | |
| 824 variations::CHROME_SYNC_SERVICE); | |
| 825 CreateAndActivateFieldTrial("trial name 3", "group name", kVariationId3, | |
| 826 variations::GOOGLE_WEB_PROPERTIES); | |
| 827 | |
| 828 sessions::SessionTab session_tab; | |
| 829 manager()->SetVariationIds(&session_tab); | |
| 830 | |
| 831 ASSERT_EQ(2u, session_tab.variation_ids.size()); | |
| 832 EXPECT_EQ(kVariationId1, session_tab.variation_ids[0]); | |
| 833 EXPECT_EQ(kVariationId2, session_tab.variation_ids[1]); | |
| 834 } | |
| 835 | |
| 836 // Tests that for supervised users blocked navigations are recorded and marked | |
| 837 // as such, while regular navigations are marked as allowed. | |
| 838 TEST_F(SessionsSyncManagerTest, BlockedNavigations) { | |
| 839 SyncedTabDelegateFake tab; | |
| 840 std::unique_ptr<content::NavigationEntry> entry1( | |
| 841 content::NavigationEntry::Create()); | |
| 842 GURL url1("http://www.google.com/"); | |
| 843 entry1->SetVirtualURL(url1); | |
| 844 entry1->SetTimestamp(kTime1); | |
| 845 tab.AppendEntry(std::move(entry1)); | |
| 846 | |
| 847 std::unique_ptr<content::NavigationEntry> entry2( | |
| 848 content::NavigationEntry::Create()); | |
| 849 GURL url2("http://blocked.com/foo"); | |
| 850 entry2->SetVirtualURL(url2); | |
| 851 entry2->SetTimestamp(kTime2); | |
| 852 std::unique_ptr<content::NavigationEntry> entry3( | |
| 853 content::NavigationEntry::Create()); | |
| 854 GURL url3("http://evil.com/"); | |
| 855 entry3->SetVirtualURL(url3); | |
| 856 entry3->SetTimestamp(kTime3); | |
| 857 ScopedVector<const content::NavigationEntry> blocked_navigations; | |
| 858 blocked_navigations.push_back(std::move(entry2)); | |
| 859 blocked_navigations.push_back(std::move(entry3)); | |
| 860 | |
| 861 tab.set_is_supervised(true); | |
| 862 tab.set_blocked_navigations(&blocked_navigations.get()); | |
| 863 | |
| 864 sessions::SessionTab session_tab; | |
| 865 session_tab.window_id.set_id(1); | |
| 866 session_tab.tab_id.set_id(1); | |
| 867 session_tab.tab_visual_index = 1; | |
| 868 session_tab.current_navigation_index = 1; | |
| 869 session_tab.pinned = true; | |
| 870 session_tab.extension_app_id = "app id"; | |
| 871 session_tab.user_agent_override = "override"; | |
| 872 session_tab.timestamp = kTime5; | |
| 873 session_tab.navigations.push_back( | |
| 874 SerializedNavigationEntryTestHelper::CreateNavigation( | |
| 875 "http://www.example.com", "Example")); | |
| 876 session_tab.session_storage_persistent_id = "persistent id"; | |
| 877 manager()->SetSessionTabFromDelegate(tab, kTime4, &session_tab); | |
| 878 | |
| 879 EXPECT_EQ(0, session_tab.window_id.id()); | |
| 880 EXPECT_EQ(0, session_tab.tab_id.id()); | |
| 881 EXPECT_EQ(0, session_tab.tab_visual_index); | |
| 882 EXPECT_EQ(0, session_tab.current_navigation_index); | |
| 883 EXPECT_FALSE(session_tab.pinned); | |
| 884 EXPECT_TRUE(session_tab.extension_app_id.empty()); | |
| 885 EXPECT_TRUE(session_tab.user_agent_override.empty()); | |
| 886 EXPECT_EQ(kTime4, session_tab.timestamp); | |
| 887 ASSERT_EQ(3u, session_tab.navigations.size()); | |
| 888 EXPECT_EQ(url1, session_tab.navigations[0].virtual_url()); | |
| 889 EXPECT_EQ(url2, session_tab.navigations[1].virtual_url()); | |
| 890 EXPECT_EQ(url3, session_tab.navigations[2].virtual_url()); | |
| 891 EXPECT_EQ(kTime1, session_tab.navigations[0].timestamp()); | |
| 892 EXPECT_EQ(kTime2, session_tab.navigations[1].timestamp()); | |
| 893 EXPECT_EQ(kTime3, session_tab.navigations[2].timestamp()); | |
| 894 EXPECT_EQ(SerializedNavigationEntry::STATE_ALLOWED, | |
| 895 session_tab.navigations[0].blocked_state()); | |
| 896 EXPECT_EQ(SerializedNavigationEntry::STATE_BLOCKED, | |
| 897 session_tab.navigations[1].blocked_state()); | |
| 898 EXPECT_EQ(SerializedNavigationEntry::STATE_BLOCKED, | |
| 899 session_tab.navigations[2].blocked_state()); | |
| 900 EXPECT_TRUE(session_tab.session_storage_persistent_id.empty()); | |
| 901 } | |
| 902 | |
| 903 // Tests that the local session header objects is created properly in | |
| 904 // presence of no other session activity, once and only once. | |
| 905 TEST_F(SessionsSyncManagerTest, MergeLocalSessionNoTabs) { | |
| 906 syncer::SyncChangeList out; | |
| 907 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); | |
| 908 EXPECT_FALSE(manager()->current_machine_tag().empty()); | |
| 909 | |
| 910 EXPECT_EQ(2U, out.size()); | |
| 911 EXPECT_TRUE(out[0].IsValid()); | |
| 912 EXPECT_EQ(SyncChange::ACTION_ADD, out[0].change_type()); | |
| 913 const SyncData data(out[0].sync_data()); | |
| 914 EXPECT_EQ(manager()->current_machine_tag(), | |
| 915 syncer::SyncDataLocal(data).GetTag()); | |
| 916 const sync_pb::SessionSpecifics& specifics(data.GetSpecifics().session()); | |
| 917 EXPECT_EQ(manager()->current_machine_tag(), specifics.session_tag()); | |
| 918 EXPECT_TRUE(specifics.has_header()); | |
| 919 const sync_pb::SessionHeader& header_s = specifics.header(); | |
| 920 EXPECT_TRUE(header_s.has_device_type()); | |
| 921 EXPECT_EQ(GetLocalDeviceInfo()->client_name(), header_s.client_name()); | |
| 922 EXPECT_EQ(0, header_s.window_size()); | |
| 923 | |
| 924 EXPECT_TRUE(out[1].IsValid()); | |
| 925 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[1].change_type()); | |
| 926 const SyncData data_2(out[1].sync_data()); | |
| 927 EXPECT_EQ(manager()->current_machine_tag(), | |
| 928 syncer::SyncDataLocal(data_2).GetTag()); | |
| 929 const sync_pb::SessionSpecifics& specifics2(data_2.GetSpecifics().session()); | |
| 930 EXPECT_EQ(manager()->current_machine_tag(), specifics2.session_tag()); | |
| 931 EXPECT_TRUE(specifics2.has_header()); | |
| 932 const sync_pb::SessionHeader& header_s2 = specifics2.header(); | |
| 933 EXPECT_EQ(0, header_s2.window_size()); | |
| 934 | |
| 935 // Now take that header node and feed it in as input. | |
| 936 SyncData d = CreateRemoteData(data.GetSpecifics()); | |
| 937 syncer::SyncDataList in(&d, &d + 1); | |
| 938 out.clear(); | |
| 939 SessionsSyncManager manager2(GetSyncSessionsClient(), sync_prefs(), | |
| 940 local_device(), NewDummyRouter(), | |
| 941 base::Closure(), base::Closure()); | |
| 942 syncer::SyncMergeResult result = manager2.MergeDataAndStartSyncing( | |
| 943 syncer::SESSIONS, in, std::unique_ptr<syncer::SyncChangeProcessor>( | |
| 944 new TestSyncProcessorStub(&out)), | |
| 945 std::unique_ptr<syncer::SyncErrorFactory>( | |
| 946 new syncer::SyncErrorFactoryMock())); | |
| 947 ASSERT_FALSE(result.error().IsSet()); | |
| 948 | |
| 949 EXPECT_EQ(1U, out.size()); | |
| 950 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[0].change_type()); | |
| 951 EXPECT_TRUE(out[0].sync_data().GetSpecifics().session().has_header()); | |
| 952 } | |
| 953 | |
| 954 // Ensure model association associates the pre-existing tabs. | |
| 955 TEST_F(SessionsSyncManagerTest, SwappedOutOnRestore) { | |
| 956 AddTab(browser(), GURL("http://foo1")); | |
| 957 NavigateAndCommitActiveTab(GURL("http://foo2")); | |
| 958 AddTab(browser(), GURL("http://bar1")); | |
| 959 NavigateAndCommitActiveTab(GURL("http://bar2")); | |
| 960 AddTab(browser(), GURL("http://baz1")); | |
| 961 NavigateAndCommitActiveTab(GURL("http://baz2")); | |
| 962 const int kRestoredTabId = 1337; | |
| 963 const int kNewTabId = 2468; | |
| 964 | |
| 965 syncer::SyncDataList in; | |
| 966 syncer::SyncChangeList out; | |
| 967 InitWithSyncDataTakeOutput(in, &out); | |
| 968 | |
| 969 // Should be one header add, 3 tab add/update pairs, one header update. | |
| 970 ASSERT_EQ(8U, out.size()); | |
| 971 | |
| 972 // For input, we set up: | |
| 973 // * one "normal" fully loaded tab | |
| 974 // * one "frozen" tab with no WebContents and a tab_id change | |
| 975 // * one "frozen" tab with no WebContents and no tab_id change | |
| 976 sync_pb::EntitySpecifics t0_entity = out[2].sync_data().GetSpecifics(); | |
| 977 sync_pb::EntitySpecifics t1_entity = out[4].sync_data().GetSpecifics(); | |
| 978 sync_pb::EntitySpecifics t2_entity = out[6].sync_data().GetSpecifics(); | |
| 979 t1_entity.mutable_session()->mutable_tab()->set_tab_id(kRestoredTabId); | |
| 980 in.push_back(CreateRemoteData(t0_entity)); | |
| 981 in.push_back(CreateRemoteData(t1_entity)); | |
| 982 in.push_back(CreateRemoteData(t2_entity)); | |
| 983 out.clear(); | |
| 984 manager()->StopSyncing(syncer::SESSIONS); | |
| 985 | |
| 986 const std::set<const SyncedWindowDelegate*>& windows = | |
| 987 manager()->synced_window_delegates_getter()->GetSyncedWindowDelegates(); | |
| 988 ASSERT_EQ(1U, windows.size()); | |
| 989 SyncedTabDelegateFake t1_override, t2_override; | |
| 990 t1_override.SetSyncId(1); // No WebContents by default. | |
| 991 t2_override.SetSyncId(2); // No WebContents by default. | |
| 992 SyncedWindowDelegateOverride window_override(*windows.begin()); | |
| 993 window_override.OverrideTabAt(1, &t1_override, kNewTabId); | |
| 994 window_override.OverrideTabAt(2, &t2_override, | |
| 995 t2_entity.session().tab().tab_id()); | |
| 996 std::set<const SyncedWindowDelegate*> delegates; | |
| 997 delegates.insert(&window_override); | |
| 998 std::unique_ptr<TestSyncedWindowDelegatesGetter> getter( | |
| 999 new TestSyncedWindowDelegatesGetter(delegates)); | |
| 1000 set_synced_window_getter(getter.get()); | |
| 1001 | |
| 1002 syncer::SyncMergeResult result = manager()->MergeDataAndStartSyncing( | |
| 1003 syncer::SESSIONS, in, std::unique_ptr<syncer::SyncChangeProcessor>( | |
| 1004 new TestSyncProcessorStub(&out)), | |
| 1005 std::unique_ptr<syncer::SyncErrorFactory>( | |
| 1006 new syncer::SyncErrorFactoryMock())); | |
| 1007 | |
| 1008 // There should be two changes, one for the fully associated tab, and | |
| 1009 // one for the tab_id update to t1. t2 shouldn't need to be updated. | |
| 1010 ASSERT_EQ(2U, FilterOutLocalHeaderChanges(&out)->size()); | |
| 1011 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[0].change_type()); | |
| 1012 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[1].change_type()); | |
| 1013 EXPECT_EQ(kNewTabId, | |
| 1014 out[1].sync_data().GetSpecifics().session().tab().tab_id()); | |
| 1015 | |
| 1016 // Verify TabLinks. | |
| 1017 SessionsSyncManager::TabLinksMap tab_map = manager()->local_tab_map_; | |
| 1018 ASSERT_EQ(3U, tab_map.size()); | |
| 1019 int t2_tab_id = t2_entity.session().tab().tab_id(); | |
| 1020 EXPECT_EQ(2, tab_map.find(t2_tab_id)->second->tab_node_id()); | |
| 1021 EXPECT_EQ(1, tab_map.find(kNewTabId)->second->tab_node_id()); | |
| 1022 int t0_tab_id = out[0].sync_data().GetSpecifics().session().tab().tab_id(); | |
| 1023 EXPECT_EQ(0, tab_map.find(t0_tab_id)->second->tab_node_id()); | |
| 1024 // TODO(tim): Once bug 337057 is fixed, we can issue an OnLocalTabModified | |
| 1025 // from here (using an override similar to above) to return a new tab id | |
| 1026 // and verify that we don't see any node creations in the SyncChangeProcessor | |
| 1027 // (similar to how SessionsSyncManagerTest.OnLocalTabModified works.) | |
| 1028 } | |
| 1029 | |
| 1030 // Ensure model association updates the window ID for tabs whose window's ID has | |
| 1031 // changed. | |
| 1032 TEST_F(SessionsSyncManagerTest, WindowIdUpdatedOnRestore) { | |
| 1033 const int kNewWindowId = 1337; | |
| 1034 syncer::SyncDataList in; | |
| 1035 syncer::SyncChangeList out; | |
| 1036 | |
| 1037 // Set up one tab and start sync with it. | |
| 1038 AddTab(browser(), GURL("http://foo1")); | |
| 1039 NavigateAndCommitActiveTab(GURL("http://foo2")); | |
| 1040 InitWithSyncDataTakeOutput(in, &out); | |
| 1041 | |
| 1042 // Should be one header add, 1 tab add/update pair, and one header update. | |
| 1043 ASSERT_EQ(4U, out.size()); | |
| 1044 const sync_pb::EntitySpecifics t0_entity = out[2].sync_data().GetSpecifics(); | |
| 1045 | |
| 1046 in.push_back(CreateRemoteData(t0_entity)); | |
| 1047 out.clear(); | |
| 1048 manager()->StopSyncing(syncer::SESSIONS); | |
| 1049 | |
| 1050 // SyncedTabDelegateFake is a placeholder (no WebContents) by default. | |
| 1051 SyncedTabDelegateFake t0_override; | |
| 1052 t0_override.SetSyncId(t0_entity.session().tab_node_id()); | |
| 1053 | |
| 1054 // Set up the window override with the new window ID and placeholder tab. | |
| 1055 const std::set<const SyncedWindowDelegate*>& windows = | |
| 1056 get_synced_window_getter()->GetSyncedWindowDelegates(); | |
| 1057 ASSERT_EQ(1U, windows.size()); | |
| 1058 SyncedWindowDelegateOverride window_override(*windows.begin()); | |
| 1059 window_override.OverrideSessionId(kNewWindowId); | |
| 1060 window_override.OverrideTabAt(0, &t0_override, | |
| 1061 t0_entity.session().tab().tab_id()); | |
| 1062 | |
| 1063 // Inject the window override. | |
| 1064 std::set<const SyncedWindowDelegate*> delegates; | |
| 1065 delegates.insert(&window_override); | |
| 1066 std::unique_ptr<TestSyncedWindowDelegatesGetter> getter( | |
| 1067 new TestSyncedWindowDelegatesGetter(delegates)); | |
| 1068 set_synced_window_getter(getter.get()); | |
| 1069 | |
| 1070 syncer::SyncMergeResult result = manager()->MergeDataAndStartSyncing( | |
| 1071 syncer::SESSIONS, in, std::unique_ptr<syncer::SyncChangeProcessor>( | |
| 1072 new TestSyncProcessorStub(&out)), | |
| 1073 std::unique_ptr<syncer::SyncErrorFactory>( | |
| 1074 new syncer::SyncErrorFactoryMock())); | |
| 1075 | |
| 1076 // There should be one change for t0's window ID update. | |
| 1077 ASSERT_EQ(1U, FilterOutLocalHeaderChanges(&out)->size()); | |
| 1078 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[0].change_type()); | |
| 1079 EXPECT_EQ(kNewWindowId, | |
| 1080 out[0].sync_data().GetSpecifics().session().tab().window_id()); | |
| 1081 } | |
| 1082 | |
| 1083 // Tests MergeDataAndStartSyncing with sync data but no local data. | |
| 1084 TEST_F(SessionsSyncManagerTest, MergeWithInitialForeignSession) { | |
| 1085 std::string tag = "tag1"; | |
| 1086 | |
| 1087 SessionID::id_type n1[] = {5, 10, 13, 17}; | |
| 1088 std::vector<SessionID::id_type> tab_list1(n1, n1 + arraysize(n1)); | |
| 1089 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 1090 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | |
| 1091 tag, tab_list1, &tabs1)); | |
| 1092 // Add a second window. | |
| 1093 SessionID::id_type n2[] = {7, 15, 18, 20}; | |
| 1094 std::vector<SessionID::id_type> tab_list2(n2, n2 + arraysize(n2)); | |
| 1095 helper()->AddWindowSpecifics(1, tab_list2, &meta); | |
| 1096 | |
| 1097 // Set up initial data. | |
| 1098 syncer::SyncDataList initial_data; | |
| 1099 initial_data.push_back(CreateRemoteData(meta)); | |
| 1100 AddTabsToSyncDataList(tabs1, &initial_data); | |
| 1101 | |
| 1102 for (size_t i = 0; i < tab_list2.size(); ++i) { | |
| 1103 sync_pb::EntitySpecifics entity; | |
| 1104 helper()->BuildTabSpecifics(tag, 0, tab_list2[i], | |
| 1105 entity.mutable_session()); | |
| 1106 initial_data.push_back(CreateRemoteData(entity)); | |
| 1107 } | |
| 1108 | |
| 1109 syncer::SyncChangeList output; | |
| 1110 InitWithSyncDataTakeOutput(initial_data, &output); | |
| 1111 EXPECT_TRUE(FilterOutLocalHeaderChanges(&output)->empty()); | |
| 1112 | |
| 1113 std::vector<const SyncedSession*> foreign_sessions; | |
| 1114 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1115 ASSERT_EQ(1U, foreign_sessions.size()); | |
| 1116 std::vector<std::vector<SessionID::id_type> > session_reference; | |
| 1117 session_reference.push_back(tab_list1); | |
| 1118 session_reference.push_back(tab_list2); | |
| 1119 helper()->VerifySyncedSession(tag, session_reference, *(foreign_sessions[0])); | |
| 1120 } | |
| 1121 | |
| 1122 // This is a combination of MergeWithInitialForeignSession and | |
| 1123 // MergeLocalSessionExistingTabs. We repeat some checks performed in each of | |
| 1124 // those tests to ensure the common mixed scenario works. | |
| 1125 TEST_F(SessionsSyncManagerTest, MergeWithLocalAndForeignTabs) { | |
| 1126 // Local. | |
| 1127 AddTab(browser(), GURL("http://foo1")); | |
| 1128 NavigateAndCommitActiveTab(GURL("http://foo2")); | |
| 1129 | |
| 1130 // Foreign. | |
| 1131 std::string tag = "tag1"; | |
| 1132 SessionID::id_type n1[] = {5, 10, 13, 17}; | |
| 1133 std::vector<SessionID::id_type> tab_list1(n1, n1 + arraysize(n1)); | |
| 1134 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 1135 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | |
| 1136 tag, tab_list1, &tabs1)); | |
| 1137 syncer::SyncDataList foreign_data; | |
| 1138 foreign_data.push_back(CreateRemoteData(meta)); | |
| 1139 AddTabsToSyncDataList(tabs1, &foreign_data); | |
| 1140 | |
| 1141 syncer::SyncChangeList output; | |
| 1142 InitWithSyncDataTakeOutput(foreign_data, &output); | |
| 1143 ASSERT_EQ(4U, output.size()); | |
| 1144 | |
| 1145 // Verify the local header. | |
| 1146 EXPECT_TRUE(output[0].IsValid()); | |
| 1147 EXPECT_EQ(SyncChange::ACTION_ADD, output[0].change_type()); | |
| 1148 const SyncData data(output[0].sync_data()); | |
| 1149 EXPECT_EQ(manager()->current_machine_tag(), | |
| 1150 syncer::SyncDataLocal(data).GetTag()); | |
| 1151 const sync_pb::SessionSpecifics& specifics(data.GetSpecifics().session()); | |
| 1152 EXPECT_EQ(manager()->current_machine_tag(), specifics.session_tag()); | |
| 1153 EXPECT_TRUE(specifics.has_header()); | |
| 1154 const sync_pb::SessionHeader& header_s = specifics.header(); | |
| 1155 EXPECT_TRUE(header_s.has_device_type()); | |
| 1156 EXPECT_EQ(GetLocalDeviceInfo()->client_name(), header_s.client_name()); | |
| 1157 EXPECT_EQ(0, header_s.window_size()); | |
| 1158 | |
| 1159 // Verify the tab node creations and updates with content. | |
| 1160 for (int i = 1; i < 3; i++) { | |
| 1161 EXPECT_TRUE(output[i].IsValid()); | |
| 1162 const SyncData data(output[i].sync_data()); | |
| 1163 EXPECT_TRUE(base::StartsWith(syncer::SyncDataLocal(data).GetTag(), | |
| 1164 manager()->current_machine_tag(), | |
| 1165 base::CompareCase::SENSITIVE)); | |
| 1166 const sync_pb::SessionSpecifics& specifics(data.GetSpecifics().session()); | |
| 1167 EXPECT_EQ(manager()->current_machine_tag(), specifics.session_tag()); | |
| 1168 } | |
| 1169 EXPECT_EQ(SyncChange::ACTION_ADD, output[1].change_type()); | |
| 1170 EXPECT_EQ(SyncChange::ACTION_UPDATE, output[2].change_type()); | |
| 1171 EXPECT_TRUE(output[2].sync_data().GetSpecifics().session().has_tab()); | |
| 1172 | |
| 1173 // Verify the header was updated to reflect window state. | |
| 1174 EXPECT_TRUE(output[3].IsValid()); | |
| 1175 EXPECT_EQ(SyncChange::ACTION_UPDATE, output[3].change_type()); | |
| 1176 const SyncData data_2(output[3].sync_data()); | |
| 1177 EXPECT_EQ(manager()->current_machine_tag(), | |
| 1178 syncer::SyncDataLocal(data_2).GetTag()); | |
| 1179 const sync_pb::SessionSpecifics& specifics2(data_2.GetSpecifics().session()); | |
| 1180 EXPECT_EQ(manager()->current_machine_tag(), specifics2.session_tag()); | |
| 1181 EXPECT_TRUE(specifics2.has_header()); | |
| 1182 const sync_pb::SessionHeader& header_s2 = specifics2.header(); | |
| 1183 EXPECT_EQ(1, header_s2.window_size()); | |
| 1184 | |
| 1185 // Verify foreign data. | |
| 1186 std::vector<const SyncedSession*> foreign_sessions; | |
| 1187 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1188 std::vector<std::vector<SessionID::id_type> > session_reference; | |
| 1189 session_reference.push_back(tab_list1); | |
| 1190 helper()->VerifySyncedSession(tag, session_reference, *(foreign_sessions[0])); | |
| 1191 // There should be one and only one foreign session. If VerifySyncedSession | |
| 1192 // was successful above this EXPECT call ensures the local session didn't | |
| 1193 // get mistakenly added to foreign tracking (Similar to ExistingTabs test). | |
| 1194 EXPECT_EQ(1U, foreign_sessions.size()); | |
| 1195 } | |
| 1196 | |
| 1197 // Tests the common scenario. Merge with both local and foreign session data | |
| 1198 // followed by updates flowing from sync and local. | |
| 1199 TEST_F(SessionsSyncManagerTest, UpdatesAfterMixedMerge) { | |
| 1200 // Add local and foreign data. | |
| 1201 AddTab(browser(), GURL("http://foo1")); | |
| 1202 NavigateAndCommitActiveTab(GURL("http://foo2")); | |
| 1203 | |
| 1204 std::string tag1 = "tag1"; | |
| 1205 syncer::SyncDataList foreign_data1; | |
| 1206 std::vector<std::vector<SessionID::id_type> > meta1_reference; | |
| 1207 sync_pb::SessionSpecifics meta1; | |
| 1208 | |
| 1209 SessionID::id_type n1[] = {5, 10, 13, 17}; | |
| 1210 std::vector<SessionID::id_type> tab_list1(n1, n1 + arraysize(n1)); | |
| 1211 meta1_reference.push_back(tab_list1); | |
| 1212 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 1213 meta1 = helper()->BuildForeignSession(tag1, tab_list1, &tabs1); | |
| 1214 foreign_data1.push_back(CreateRemoteData(meta1)); | |
| 1215 AddTabsToSyncDataList(tabs1, &foreign_data1); | |
| 1216 | |
| 1217 syncer::SyncChangeList output1; | |
| 1218 InitWithSyncDataTakeOutput(foreign_data1, &output1); | |
| 1219 ASSERT_EQ(4U, output1.size()); | |
| 1220 | |
| 1221 // Add a second window to the foreign session. | |
| 1222 // TODO(tim): Bug 98892. Add local window too when observers are hooked up. | |
| 1223 SessionID::id_type tab_nums2[] = {7, 15, 18, 20}; | |
| 1224 std::vector<SessionID::id_type> tab_list2( | |
| 1225 tab_nums2, tab_nums2 + arraysize(tab_nums2)); | |
| 1226 meta1_reference.push_back(tab_list2); | |
| 1227 helper()->AddWindowSpecifics(1, tab_list2, &meta1); | |
| 1228 std::vector<sync_pb::SessionSpecifics> tabs2; | |
| 1229 tabs2.resize(tab_list2.size()); | |
| 1230 for (size_t i = 0; i < tab_list2.size(); ++i) { | |
| 1231 helper()->BuildTabSpecifics(tag1, 0, tab_list2[i], &tabs2[i]); | |
| 1232 } | |
| 1233 | |
| 1234 syncer::SyncChangeList changes; | |
| 1235 changes.push_back(MakeRemoteChange(meta1, SyncChange::ACTION_UPDATE)); | |
| 1236 AddTabsToChangeList(tabs2, SyncChange::ACTION_ADD, &changes); | |
| 1237 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1238 changes.clear(); | |
| 1239 | |
| 1240 // Check that the foreign session was associated and retrieve the data. | |
| 1241 std::vector<const SyncedSession*> foreign_sessions; | |
| 1242 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1243 ASSERT_EQ(1U, foreign_sessions.size()); | |
| 1244 ASSERT_EQ(4U, foreign_sessions[0]->windows.find(0)->second->tabs.size()); | |
| 1245 ASSERT_EQ(4U, foreign_sessions[0]->windows.find(1)->second->tabs.size()); | |
| 1246 helper()->VerifySyncedSession(tag1, meta1_reference, *(foreign_sessions[0])); | |
| 1247 | |
| 1248 // Add a new foreign session. | |
| 1249 std::string tag2 = "tag2"; | |
| 1250 SessionID::id_type n2[] = {107, 115}; | |
| 1251 std::vector<SessionID::id_type> tag2_tab_list(n2, n2 + arraysize(n2)); | |
| 1252 std::vector<sync_pb::SessionSpecifics> tag2_tabs; | |
| 1253 sync_pb::SessionSpecifics meta2(helper()->BuildForeignSession( | |
| 1254 tag2, tag2_tab_list, &tag2_tabs)); | |
| 1255 changes.push_back(MakeRemoteChange(meta2, SyncChange::ACTION_ADD)); | |
| 1256 AddTabsToChangeList(tag2_tabs, SyncChange::ACTION_ADD, &changes); | |
| 1257 | |
| 1258 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1259 changes.clear(); | |
| 1260 | |
| 1261 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1262 std::vector<std::vector<SessionID::id_type> > meta2_reference; | |
| 1263 meta2_reference.push_back(tag2_tab_list); | |
| 1264 ASSERT_EQ(2U, foreign_sessions.size()); | |
| 1265 ASSERT_EQ(2U, foreign_sessions[1]->windows.find(0)->second->tabs.size()); | |
| 1266 helper()->VerifySyncedSession(tag2, meta2_reference, *(foreign_sessions[1])); | |
| 1267 foreign_sessions.clear(); | |
| 1268 | |
| 1269 // Remove a tab from a window. | |
| 1270 meta1_reference[0].pop_back(); | |
| 1271 tab_list1.pop_back(); | |
| 1272 sync_pb::SessionWindow* win = meta1.mutable_header()->mutable_window(0); | |
| 1273 win->clear_tab(); | |
| 1274 for (std::vector<int>::const_iterator iter = tab_list1.begin(); | |
| 1275 iter != tab_list1.end(); ++iter) { | |
| 1276 win->add_tab(*iter); | |
| 1277 } | |
| 1278 syncer::SyncChangeList removal; | |
| 1279 removal.push_back(MakeRemoteChange(meta1, SyncChange::ACTION_UPDATE)); | |
| 1280 AddTabsToChangeList(tabs1, SyncChange::ACTION_UPDATE, &removal); | |
| 1281 manager()->ProcessSyncChanges(FROM_HERE, removal); | |
| 1282 | |
| 1283 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1284 ASSERT_EQ(2U, foreign_sessions.size()); | |
| 1285 ASSERT_EQ(3U, foreign_sessions[0]->windows.find(0)->second->tabs.size()); | |
| 1286 helper()->VerifySyncedSession(tag1, meta1_reference, *(foreign_sessions[0])); | |
| 1287 } | |
| 1288 | |
| 1289 // Tests that this SyncSessionManager knows how to delete foreign sessions | |
| 1290 // if it wants to. | |
| 1291 TEST_F(SessionsSyncManagerTest, DeleteForeignSession) { | |
| 1292 InitWithNoSyncData(); | |
| 1293 std::string tag = "tag1"; | |
| 1294 syncer::SyncChangeList changes; | |
| 1295 | |
| 1296 std::vector<const SyncedSession*> foreign_sessions; | |
| 1297 ASSERT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1298 manager()->DeleteForeignSessionInternal(tag, &changes); | |
| 1299 ASSERT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1300 EXPECT_TRUE(changes.empty()); | |
| 1301 | |
| 1302 // Fill an instance of session specifics with a foreign session's data. | |
| 1303 std::vector<sync_pb::SessionSpecifics> tabs; | |
| 1304 SessionID::id_type n1[] = {5, 10, 13, 17}; | |
| 1305 std::vector<SessionID::id_type> tab_nums1(n1, n1 + arraysize(n1)); | |
| 1306 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | |
| 1307 tag, tab_nums1, &tabs)); | |
| 1308 | |
| 1309 // Update associator with the session's meta node, window, and tabs. | |
| 1310 manager()->UpdateTrackerWithForeignSession(meta, base::Time()); | |
| 1311 for (std::vector<sync_pb::SessionSpecifics>::iterator iter = tabs.begin(); | |
| 1312 iter != tabs.end(); ++iter) { | |
| 1313 manager()->UpdateTrackerWithForeignSession(*iter, base::Time()); | |
| 1314 } | |
| 1315 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1316 ASSERT_EQ(1U, foreign_sessions.size()); | |
| 1317 | |
| 1318 // Now delete the foreign session. | |
| 1319 manager()->DeleteForeignSessionInternal(tag, &changes); | |
| 1320 EXPECT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1321 | |
| 1322 EXPECT_EQ(5U, changes.size()); | |
| 1323 std::set<std::string> expected_tags(&tag, &tag + 1); | |
| 1324 for (int i = 0; i < 5; i++) | |
| 1325 expected_tags.insert(TabNodePool::TabIdToTag(tag, i)); | |
| 1326 | |
| 1327 for (int i = 0; i < 5; i++) { | |
| 1328 SCOPED_TRACE(changes[i].ToString()); | |
| 1329 EXPECT_TRUE(changes[i].IsValid()); | |
| 1330 EXPECT_EQ(SyncChange::ACTION_DELETE, changes[i].change_type()); | |
| 1331 EXPECT_TRUE(changes[i].sync_data().IsValid()); | |
| 1332 EXPECT_EQ(1U, | |
| 1333 expected_tags.erase( | |
| 1334 syncer::SyncDataLocal(changes[i].sync_data()).GetTag())); | |
| 1335 } | |
| 1336 } | |
| 1337 | |
| 1338 // Write a foreign session to a node, with the tabs arriving first, and then | |
| 1339 // retrieve it. | |
| 1340 TEST_F(SessionsSyncManagerTest, WriteForeignSessionToNodeTabsFirst) { | |
| 1341 InitWithNoSyncData(); | |
| 1342 | |
| 1343 // Fill an instance of session specifics with a foreign session's data. | |
| 1344 std::string tag = "tag1"; | |
| 1345 SessionID::id_type nums1[] = {5, 10, 13, 17}; | |
| 1346 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 1347 std::vector<SessionID::id_type> tab_list1(nums1, nums1 + arraysize(nums1)); | |
| 1348 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | |
| 1349 tag, tab_list1, &tabs1)); | |
| 1350 | |
| 1351 syncer::SyncChangeList adds; | |
| 1352 // Add tabs for first window, then the meta node. | |
| 1353 AddTabsToChangeList(tabs1, SyncChange::ACTION_ADD, &adds); | |
| 1354 adds.push_back(MakeRemoteChange(meta, SyncChange::ACTION_ADD)); | |
| 1355 manager()->ProcessSyncChanges(FROM_HERE, adds); | |
| 1356 | |
| 1357 // Check that the foreign session was associated and retrieve the data. | |
| 1358 std::vector<const SyncedSession*> foreign_sessions; | |
| 1359 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1360 ASSERT_EQ(1U, foreign_sessions.size()); | |
| 1361 std::vector<std::vector<SessionID::id_type> > session_reference; | |
| 1362 session_reference.push_back(tab_list1); | |
| 1363 helper()->VerifySyncedSession(tag, session_reference, *(foreign_sessions[0])); | |
| 1364 } | |
| 1365 | |
| 1366 // Write a foreign session to a node with some tabs that never arrive. | |
| 1367 TEST_F(SessionsSyncManagerTest, WriteForeignSessionToNodeMissingTabs) { | |
| 1368 InitWithNoSyncData(); | |
| 1369 | |
| 1370 // Fill an instance of session specifics with a foreign session's data. | |
| 1371 std::string tag = "tag1"; | |
| 1372 SessionID::id_type nums1[] = {5, 10, 13, 17}; | |
| 1373 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 1374 std::vector<SessionID::id_type> tab_list1(nums1, nums1 + arraysize(nums1)); | |
| 1375 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | |
| 1376 tag, tab_list1, &tabs1)); | |
| 1377 // Add a second window, but this time only create two tab nodes, despite the | |
| 1378 // window expecting four tabs. | |
| 1379 SessionID::id_type tab_nums2[] = {7, 15, 18, 20}; | |
| 1380 std::vector<SessionID::id_type> tab_list2( | |
| 1381 tab_nums2, tab_nums2 + arraysize(tab_nums2)); | |
| 1382 helper()->AddWindowSpecifics(1, tab_list2, &meta); | |
| 1383 std::vector<sync_pb::SessionSpecifics> tabs2; | |
| 1384 tabs2.resize(2); | |
| 1385 for (size_t i = 0; i < 2; ++i) { | |
| 1386 helper()->BuildTabSpecifics(tag, 0, tab_list2[i], &tabs2[i]); | |
| 1387 } | |
| 1388 | |
| 1389 syncer::SyncChangeList changes; | |
| 1390 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_ADD)); | |
| 1391 AddTabsToChangeList(tabs1, SyncChange::ACTION_ADD, &changes); | |
| 1392 AddTabsToChangeList(tabs2, SyncChange::ACTION_ADD, &changes); | |
| 1393 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1394 changes.clear(); | |
| 1395 | |
| 1396 // Check that the foreign session was associated and retrieve the data. | |
| 1397 std::vector<const SyncedSession*> foreign_sessions; | |
| 1398 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1399 ASSERT_EQ(1U, foreign_sessions.size()); | |
| 1400 ASSERT_EQ(2U, foreign_sessions[0]->windows.size()); | |
| 1401 ASSERT_EQ(4U, foreign_sessions[0]->windows.find(0)->second->tabs.size()); | |
| 1402 ASSERT_EQ(4U, foreign_sessions[0]->windows.find(1)->second->tabs.size()); | |
| 1403 | |
| 1404 // Close the second window. | |
| 1405 meta.mutable_header()->clear_window(); | |
| 1406 helper()->AddWindowSpecifics(0, tab_list1, &meta); | |
| 1407 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_UPDATE)); | |
| 1408 // Update associator with the session's meta node containing one window. | |
| 1409 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1410 | |
| 1411 // Check that the foreign session was associated and retrieve the data. | |
| 1412 foreign_sessions.clear(); | |
| 1413 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1414 ASSERT_EQ(1U, foreign_sessions.size()); | |
| 1415 ASSERT_EQ(1U, foreign_sessions[0]->windows.size()); | |
| 1416 std::vector<std::vector<SessionID::id_type> > session_reference; | |
| 1417 session_reference.push_back(tab_list1); | |
| 1418 helper()->VerifySyncedSession(tag, session_reference, *(foreign_sessions[0])); | |
| 1419 } | |
| 1420 | |
| 1421 // Tests that the SessionsSyncManager can handle a remote client deleting | |
| 1422 // sync nodes that belong to this local session. | |
| 1423 TEST_F(SessionsSyncManagerTest, ProcessRemoteDeleteOfLocalSession) { | |
| 1424 syncer::SyncChangeList out; | |
| 1425 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); | |
| 1426 ASSERT_EQ(2U, out.size()); | |
| 1427 sync_pb::EntitySpecifics entity(out[0].sync_data().GetSpecifics()); | |
| 1428 SyncData d = CreateRemoteData(entity); | |
| 1429 SetSyncData(syncer::SyncDataList(&d, &d + 1)); | |
| 1430 out.clear(); | |
| 1431 | |
| 1432 syncer::SyncChangeList changes; | |
| 1433 changes.push_back( | |
| 1434 MakeRemoteChange(entity.session(), SyncChange::ACTION_DELETE)); | |
| 1435 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1436 EXPECT_TRUE(manager()->local_tab_pool_out_of_sync_); | |
| 1437 EXPECT_TRUE(out.empty()); // ChangeProcessor shouldn't see any activity. | |
| 1438 | |
| 1439 // This should trigger repair of the TabNodePool. | |
| 1440 const GURL foo1("http://foo/1"); | |
| 1441 AddTab(browser(), foo1); | |
| 1442 EXPECT_FALSE(manager()->local_tab_pool_out_of_sync_); | |
| 1443 | |
| 1444 // AddTab triggers two notifications, one for the tab insertion and one for | |
| 1445 // committing the NavigationEntry. The first notification results in a tab | |
| 1446 // we don't associate although we do update the header node. The second | |
| 1447 // notification triggers association of the tab, and the subsequent window | |
| 1448 // update. So we should see 4 changes at the SyncChangeProcessor. | |
| 1449 ASSERT_EQ(4U, out.size()); | |
| 1450 | |
| 1451 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[0].change_type()); | |
| 1452 ASSERT_TRUE(out[0].sync_data().GetSpecifics().session().has_header()); | |
| 1453 EXPECT_EQ(SyncChange::ACTION_ADD, out[1].change_type()); | |
| 1454 int tab_node_id = out[1].sync_data().GetSpecifics().session().tab_node_id(); | |
| 1455 EXPECT_EQ(TabNodePool::TabIdToTag( | |
| 1456 manager()->current_machine_tag(), tab_node_id), | |
| 1457 syncer::SyncDataLocal(out[1].sync_data()).GetTag()); | |
| 1458 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[2].change_type()); | |
| 1459 ASSERT_TRUE(out[2].sync_data().GetSpecifics().session().has_tab()); | |
| 1460 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[3].change_type()); | |
| 1461 ASSERT_TRUE(out[3].sync_data().GetSpecifics().session().has_header()); | |
| 1462 | |
| 1463 // Verify the actual content. | |
| 1464 const sync_pb::SessionHeader& session_header = | |
| 1465 out[3].sync_data().GetSpecifics().session().header(); | |
| 1466 ASSERT_EQ(1, session_header.window_size()); | |
| 1467 EXPECT_EQ(1, session_header.window(0).tab_size()); | |
| 1468 const sync_pb::SessionTab& tab1 = | |
| 1469 out[2].sync_data().GetSpecifics().session().tab(); | |
| 1470 ASSERT_EQ(1, tab1.navigation_size()); | |
| 1471 EXPECT_EQ(foo1.spec(), tab1.navigation(0).virtual_url()); | |
| 1472 | |
| 1473 // Verify TabNodePool integrity. | |
| 1474 EXPECT_EQ(1U, manager()->local_tab_pool_.Capacity()); | |
| 1475 EXPECT_TRUE(manager()->local_tab_pool_.Empty()); | |
| 1476 | |
| 1477 // Verify TabLinks. | |
| 1478 SessionsSyncManager::TabLinksMap tab_map = manager()->local_tab_map_; | |
| 1479 ASSERT_EQ(1U, tab_map.size()); | |
| 1480 int tab_id = out[2].sync_data().GetSpecifics().session().tab().tab_id(); | |
| 1481 EXPECT_EQ(tab_node_id, tab_map.find(tab_id)->second->tab_node_id()); | |
| 1482 } | |
| 1483 | |
| 1484 // Test that receiving a session delete from sync removes the session | |
| 1485 // from tracking. | |
| 1486 TEST_F(SessionsSyncManagerTest, ProcessForeignDelete) { | |
| 1487 InitWithNoSyncData(); | |
| 1488 SessionID::id_type n[] = {5}; | |
| 1489 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 1490 std::vector<SessionID::id_type> tab_list(n, n + arraysize(n)); | |
| 1491 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | |
| 1492 "tag1", tab_list, &tabs1)); | |
| 1493 | |
| 1494 syncer::SyncChangeList changes; | |
| 1495 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_ADD)); | |
| 1496 AddTabsToChangeList(tabs1, SyncChange::ACTION_ADD, &changes); | |
| 1497 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1498 | |
| 1499 std::vector<const SyncedSession*> foreign_sessions; | |
| 1500 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1501 ASSERT_EQ(1U, foreign_sessions.size()); | |
| 1502 | |
| 1503 changes.clear(); | |
| 1504 foreign_sessions.clear(); | |
| 1505 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_DELETE)); | |
| 1506 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1507 | |
| 1508 EXPECT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1509 } | |
| 1510 | |
| 1511 TEST_F(SessionsSyncManagerTest, ProcessForeignDeleteTabs) { | |
| 1512 syncer::SyncDataList foreign_data; | |
| 1513 base::Time stale_mtime = base::Time::Now() - base::TimeDelta::FromDays(15); | |
| 1514 std::string session_tag = "tag1"; | |
| 1515 | |
| 1516 // 0 will not have ownership changed. | |
| 1517 // 1 will not be updated, but header will stop owning. | |
| 1518 // 2 will be deleted before header stops owning. | |
| 1519 // 3 will be deleted after header stops owning. | |
| 1520 // 4 will be deleted before header update, but header will still try to own. | |
| 1521 // 5 will be deleted after header update, but header will still try to own. | |
| 1522 // 6 starts orphaned and then deleted before header update. | |
| 1523 // 7 starts orphaned and then deleted after header update. | |
| 1524 SessionID::id_type tab_ids[] = {0, 1, 2, 3, 4, 5}; | |
| 1525 std::vector<SessionID::id_type> tab_list(tab_ids, | |
| 1526 tab_ids + arraysize(tab_ids)); | |
| 1527 std::vector<sync_pb::SessionSpecifics> tabs; | |
| 1528 sync_pb::SessionSpecifics meta( | |
| 1529 helper()->BuildForeignSession(session_tag, tab_list, &tabs)); | |
| 1530 AddToSyncDataList(meta, &foreign_data, stale_mtime); | |
| 1531 AddTabsToSyncDataList(tabs, &foreign_data); | |
| 1532 sync_pb::SessionSpecifics orphan6; | |
| 1533 helper()->BuildTabSpecifics(session_tag, 0, 6, &orphan6); | |
| 1534 AddToSyncDataList(orphan6, &foreign_data, stale_mtime); | |
| 1535 sync_pb::SessionSpecifics orphan7; | |
| 1536 helper()->BuildTabSpecifics(session_tag, 0, 7, &orphan7); | |
| 1537 AddToSyncDataList(orphan7, &foreign_data, stale_mtime); | |
| 1538 | |
| 1539 syncer::SyncChangeList output; | |
| 1540 InitWithSyncDataTakeOutput(foreign_data, &output); | |
| 1541 ASSERT_EQ(2U, output.size()); | |
| 1542 output.clear(); | |
| 1543 | |
| 1544 SessionID::id_type update_ids[] = {0, 4, 5}; | |
| 1545 std::vector<SessionID::id_type> update_list( | |
| 1546 update_ids, update_ids + arraysize(update_ids)); | |
| 1547 sync_pb::SessionWindow* window = meta.mutable_header()->mutable_window(0); | |
| 1548 window->clear_tab(); | |
| 1549 for (int i : update_ids) { | |
| 1550 window->add_tab(i); | |
| 1551 } | |
| 1552 | |
| 1553 syncer::SyncChangeList changes; | |
| 1554 changes.push_back(MakeRemoteChange(tabs[2], SyncChange::ACTION_DELETE)); | |
| 1555 changes.push_back(MakeRemoteChange(tabs[4], SyncChange::ACTION_DELETE)); | |
| 1556 changes.push_back(MakeRemoteChange(orphan6, SyncChange::ACTION_DELETE)); | |
| 1557 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_UPDATE)); | |
| 1558 changes.push_back(MakeRemoteChange(tabs[3], SyncChange::ACTION_DELETE)); | |
| 1559 changes.push_back(MakeRemoteChange(tabs[5], SyncChange::ACTION_DELETE)); | |
| 1560 changes.push_back(MakeRemoteChange(orphan7, SyncChange::ACTION_DELETE)); | |
| 1561 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1562 | |
| 1563 std::vector<const SyncedSession*> foreign_sessions; | |
| 1564 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1565 ASSERT_EQ(1U, foreign_sessions.size()); | |
| 1566 std::vector<std::vector<SessionID::id_type>> session_reference; | |
| 1567 session_reference.push_back(update_list); | |
| 1568 helper()->VerifySyncedSession(session_tag, session_reference, | |
| 1569 *(foreign_sessions[0])); | |
| 1570 | |
| 1571 // Everything except for session, tab0, and tab1 will have no node_id, and | |
| 1572 // should get skipped by garbage collection. | |
| 1573 manager()->DoGarbageCollection(); | |
| 1574 ASSERT_EQ(3U, output.size()); | |
| 1575 } | |
| 1576 | |
| 1577 TEST_F(SessionsSyncManagerTest, ProcessForeignDeleteTabsWithShadowing) { | |
| 1578 syncer::SyncDataList foreign_data; | |
| 1579 base::Time stale_mtime = base::Time::Now() - base::TimeDelta::FromDays(16); | |
| 1580 std::string session_tag = "tag1"; | |
| 1581 | |
| 1582 // Add several tabs that shadow eachother, in that they share tab_ids. They | |
| 1583 // will, thanks to the helper, have unique tab_node_ids. | |
| 1584 sync_pb::SessionSpecifics tab1A; | |
| 1585 helper()->BuildTabSpecifics(session_tag, 0, 1, &tab1A); | |
| 1586 AddToSyncDataList(tab1A, &foreign_data, | |
| 1587 stale_mtime + base::TimeDelta::FromMinutes(1)); | |
| 1588 | |
| 1589 sync_pb::SessionSpecifics tab1B; | |
| 1590 helper()->BuildTabSpecifics(session_tag, 0, 1, &tab1B); | |
| 1591 AddToSyncDataList(tab1B, &foreign_data, | |
| 1592 stale_mtime + base::TimeDelta::FromMinutes(2)); | |
| 1593 | |
| 1594 sync_pb::SessionSpecifics tab1C; | |
| 1595 helper()->BuildTabSpecifics(session_tag, 0, 1, &tab1C); | |
| 1596 AddToSyncDataList(tab1C, &foreign_data, stale_mtime); | |
| 1597 | |
| 1598 sync_pb::SessionSpecifics tab2A; | |
| 1599 helper()->BuildTabSpecifics(session_tag, 0, 2, &tab2A); | |
| 1600 AddToSyncDataList(tab2A, &foreign_data, | |
| 1601 stale_mtime + base::TimeDelta::FromMinutes(1)); | |
| 1602 | |
| 1603 sync_pb::SessionSpecifics tab2B; | |
| 1604 helper()->BuildTabSpecifics(session_tag, 0, 2, &tab2B); | |
| 1605 AddToSyncDataList(tab2B, &foreign_data, | |
| 1606 stale_mtime + base::TimeDelta::FromMinutes(2)); | |
| 1607 | |
| 1608 sync_pb::SessionSpecifics tab2C; | |
| 1609 helper()->BuildTabSpecifics(session_tag, 0, 2, &tab2C); | |
| 1610 AddToSyncDataList(tab2C, &foreign_data, stale_mtime); | |
| 1611 | |
| 1612 syncer::SyncChangeList output; | |
| 1613 InitWithSyncDataTakeOutput(foreign_data, &output); | |
| 1614 ASSERT_EQ(2U, output.size()); | |
| 1615 output.clear(); | |
| 1616 | |
| 1617 // Verify that cleanup post-merge cleanup correctly removes all tabs objects. | |
| 1618 const sessions::SessionTab* tab; | |
| 1619 ASSERT_FALSE( | |
| 1620 manager()->session_tracker_.LookupSessionTab(session_tag, 1, &tab)); | |
| 1621 ASSERT_FALSE( | |
| 1622 manager()->session_tracker_.LookupSessionTab(session_tag, 2, &tab)); | |
| 1623 | |
| 1624 std::set<int> tab_node_ids; | |
| 1625 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); | |
| 1626 EXPECT_EQ(6U, tab_node_ids.size()); | |
| 1627 EXPECT_TRUE(tab_node_ids.find(tab1A.tab_node_id()) != tab_node_ids.end()); | |
| 1628 EXPECT_TRUE(tab_node_ids.find(tab1B.tab_node_id()) != tab_node_ids.end()); | |
| 1629 EXPECT_TRUE(tab_node_ids.find(tab1C.tab_node_id()) != tab_node_ids.end()); | |
| 1630 EXPECT_TRUE(tab_node_ids.find(tab2A.tab_node_id()) != tab_node_ids.end()); | |
| 1631 EXPECT_TRUE(tab_node_ids.find(tab2B.tab_node_id()) != tab_node_ids.end()); | |
| 1632 EXPECT_TRUE(tab_node_ids.find(tab2C.tab_node_id()) != tab_node_ids.end()); | |
| 1633 | |
| 1634 syncer::SyncChangeList changes; | |
| 1635 changes.push_back(MakeRemoteChange(tab1A, SyncChange::ACTION_DELETE)); | |
| 1636 changes.push_back(MakeRemoteChange(tab1B, SyncChange::ACTION_DELETE)); | |
| 1637 changes.push_back(MakeRemoteChange(tab2C, SyncChange::ACTION_DELETE)); | |
| 1638 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1639 | |
| 1640 tab_node_ids.clear(); | |
| 1641 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); | |
| 1642 EXPECT_EQ(3U, tab_node_ids.size()); | |
| 1643 EXPECT_TRUE(tab_node_ids.find(tab1C.tab_node_id()) != tab_node_ids.end()); | |
| 1644 EXPECT_TRUE(tab_node_ids.find(tab2A.tab_node_id()) != tab_node_ids.end()); | |
| 1645 EXPECT_TRUE(tab_node_ids.find(tab2B.tab_node_id()) != tab_node_ids.end()); | |
| 1646 | |
| 1647 manager()->DoGarbageCollection(); | |
| 1648 ASSERT_EQ(3U, output.size()); | |
| 1649 } | |
| 1650 | |
| 1651 TEST_F(SessionsSyncManagerTest, ProcessForeignDeleteTabsWithReusedNodeIds) { | |
| 1652 syncer::SyncDataList foreign_data; | |
| 1653 base::Time stale_mtime = base::Time::Now() - base::TimeDelta::FromDays(16); | |
| 1654 std::string session_tag = "tag1"; | |
| 1655 int tab_node_id_shared = 13; | |
| 1656 int tab_node_id_unique = 14; | |
| 1657 | |
| 1658 sync_pb::SessionSpecifics tab1A; | |
| 1659 helper()->BuildTabSpecifics(session_tag, 0, 1, tab_node_id_shared, &tab1A); | |
| 1660 AddToSyncDataList(tab1A, &foreign_data, | |
| 1661 stale_mtime + base::TimeDelta::FromMinutes(1)); | |
| 1662 | |
| 1663 sync_pb::SessionSpecifics tab1B; | |
| 1664 helper()->BuildTabSpecifics(session_tag, 0, 1, tab_node_id_unique, &tab1B); | |
| 1665 AddToSyncDataList(tab1B, &foreign_data, | |
| 1666 stale_mtime + base::TimeDelta::FromMinutes(2)); | |
| 1667 | |
| 1668 sync_pb::SessionSpecifics tab2A; | |
| 1669 helper()->BuildTabSpecifics(session_tag, 0, 2, tab_node_id_shared, &tab2A); | |
| 1670 AddToSyncDataList(tab2A, &foreign_data, | |
| 1671 stale_mtime + base::TimeDelta::FromMinutes(1)); | |
| 1672 | |
| 1673 syncer::SyncChangeList output; | |
| 1674 InitWithSyncDataTakeOutput(foreign_data, &output); | |
| 1675 ASSERT_EQ(2U, output.size()); | |
| 1676 output.clear(); | |
| 1677 | |
| 1678 std::set<int> tab_node_ids; | |
| 1679 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); | |
| 1680 EXPECT_EQ(2U, tab_node_ids.size()); | |
| 1681 EXPECT_TRUE(tab_node_ids.find(tab_node_id_shared) != tab_node_ids.end()); | |
| 1682 EXPECT_TRUE(tab_node_ids.find(tab_node_id_unique) != tab_node_ids.end()); | |
| 1683 | |
| 1684 syncer::SyncChangeList changes; | |
| 1685 changes.push_back(MakeRemoteChange(tab1A, SyncChange::ACTION_DELETE)); | |
| 1686 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 1687 | |
| 1688 tab_node_ids.clear(); | |
| 1689 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); | |
| 1690 EXPECT_EQ(1U, tab_node_ids.size()); | |
| 1691 EXPECT_TRUE(tab_node_ids.find(tab_node_id_unique) != tab_node_ids.end()); | |
| 1692 | |
| 1693 manager()->DoGarbageCollection(); | |
| 1694 EXPECT_EQ(1U, output.size()); | |
| 1695 } | |
| 1696 | |
| 1697 // TODO(shashishekhar): "Move this to TabNodePool unittests." | |
| 1698 TEST_F(SessionsSyncManagerTest, SaveUnassociatedNodesForReassociation) { | |
| 1699 syncer::SyncChangeList changes; | |
| 1700 InitWithNoSyncData(); | |
| 1701 | |
| 1702 std::string local_tag = manager()->current_machine_tag(); | |
| 1703 // Create a free node and then dissassociate sessions so that it ends up | |
| 1704 // unassociated. | |
| 1705 manager()->local_tab_pool_.GetFreeTabNode(&changes); | |
| 1706 | |
| 1707 // Update the tab_id of the node, so that it is considered a valid | |
| 1708 // unassociated node otherwise it will be mistaken for a corrupted node and | |
| 1709 // will be deleted before being added to the tab node pool. | |
| 1710 sync_pb::EntitySpecifics entity(changes[0].sync_data().GetSpecifics()); | |
| 1711 entity.mutable_session()->mutable_tab()->set_tab_id(1); | |
| 1712 SyncData d = CreateRemoteData(entity); | |
| 1713 syncer::SyncDataList in(&d, &d + 1); | |
| 1714 changes.clear(); | |
| 1715 SessionsSyncManager manager2(GetSyncSessionsClient(), sync_prefs(), | |
| 1716 local_device(), NewDummyRouter(), | |
| 1717 base::Closure(), base::Closure()); | |
| 1718 syncer::SyncMergeResult result = manager2.MergeDataAndStartSyncing( | |
| 1719 syncer::SESSIONS, in, std::unique_ptr<syncer::SyncChangeProcessor>( | |
| 1720 new TestSyncProcessorStub(&changes)), | |
| 1721 std::unique_ptr<syncer::SyncErrorFactory>( | |
| 1722 new syncer::SyncErrorFactoryMock())); | |
| 1723 ASSERT_FALSE(result.error().IsSet()); | |
| 1724 EXPECT_TRUE(FilterOutLocalHeaderChanges(&changes)->empty()); | |
| 1725 } | |
| 1726 | |
| 1727 TEST_F(SessionsSyncManagerTest, MergeDeletesCorruptNode) { | |
| 1728 syncer::SyncChangeList changes; | |
| 1729 InitWithNoSyncData(); | |
| 1730 | |
| 1731 std::string local_tag = manager()->current_machine_tag(); | |
| 1732 int tab_node_id = manager()->local_tab_pool_.GetFreeTabNode(&changes); | |
| 1733 SyncData d = CreateRemoteData(changes[0].sync_data().GetSpecifics()); | |
| 1734 syncer::SyncDataList in(&d, &d + 1); | |
| 1735 changes.clear(); | |
| 1736 TearDown(); | |
| 1737 SetUp(); | |
| 1738 InitWithSyncDataTakeOutput(in, &changes); | |
| 1739 EXPECT_EQ(1U, FilterOutLocalHeaderChanges(&changes)->size()); | |
| 1740 EXPECT_EQ(SyncChange::ACTION_DELETE, changes[0].change_type()); | |
| 1741 EXPECT_EQ(TabNodePool::TabIdToTag(local_tag, tab_node_id), | |
| 1742 syncer::SyncDataLocal(changes[0].sync_data()).GetTag()); | |
| 1743 } | |
| 1744 | |
| 1745 // Verifies that we drop both headers and tabs during merge if their stored tag | |
| 1746 // hash doesn't match a computer tag hash. This mitigates potential failures | |
| 1747 // while cleaning up bad foreign data, see crbug.com/604657. | |
| 1748 TEST_F(SessionsSyncManagerTest, MergeDeletesBadHash) { | |
| 1749 syncer::SyncDataList foreign_data; | |
| 1750 std::vector<SessionID::id_type> empty_ids; | |
| 1751 std::vector<sync_pb::SessionSpecifics> empty_tabs; | |
| 1752 sync_pb::EntitySpecifics entity; | |
| 1753 | |
| 1754 const std::string good_header_tag = "good_header_tag"; | |
| 1755 sync_pb::SessionSpecifics good_header( | |
| 1756 helper()->BuildForeignSession(good_header_tag, empty_ids, &empty_tabs)); | |
| 1757 foreign_data.push_back(CreateRemoteData(good_header)); | |
| 1758 | |
| 1759 const std::string bad_header_tag = "bad_header_tag"; | |
| 1760 sync_pb::SessionSpecifics bad_header( | |
| 1761 helper()->BuildForeignSession(bad_header_tag, empty_ids, &empty_tabs)); | |
| 1762 entity.mutable_session()->CopyFrom(bad_header); | |
| 1763 foreign_data.push_back(SyncData::CreateRemoteData( | |
| 1764 1, entity, base::Time(), syncer::AttachmentIdList(), | |
| 1765 syncer::AttachmentServiceProxyForTest::Create(), "bad_header_tag_hash")); | |
| 1766 | |
| 1767 const std::string good_tag_tab = "good_tag_tab"; | |
| 1768 sync_pb::SessionSpecifics good_tab; | |
| 1769 helper()->BuildTabSpecifics(good_tag_tab, 0, 1, &good_tab); | |
| 1770 foreign_data.push_back(CreateRemoteData(good_tab)); | |
| 1771 | |
| 1772 const std::string bad_tab_tag = "bad_tab_tag"; | |
| 1773 sync_pb::SessionSpecifics bad_tab; | |
| 1774 helper()->BuildTabSpecifics(bad_tab_tag, 0, 2, &bad_tab); | |
| 1775 entity.mutable_session()->CopyFrom(bad_tab); | |
| 1776 foreign_data.push_back(SyncData::CreateRemoteData( | |
| 1777 1, entity, base::Time(), syncer::AttachmentIdList(), | |
| 1778 syncer::AttachmentServiceProxyForTest::Create(), "bad_tab_tag_hash")); | |
| 1779 | |
| 1780 syncer::SyncChangeList output; | |
| 1781 InitWithSyncDataTakeOutput(foreign_data, &output); | |
| 1782 ASSERT_EQ(2U, FilterOutLocalHeaderChanges(&output)->size()); | |
| 1783 ExpectAllOfChangesType(output, SyncChange::ACTION_DELETE); | |
| 1784 EXPECT_EQ(1, CountIfTagMatches(output, bad_header_tag)); | |
| 1785 EXPECT_EQ(1, CountIfTagMatches(output, bad_tab_tag)); | |
| 1786 | |
| 1787 std::vector<const SyncedSession*> sessions; | |
| 1788 manager()->session_tracker_.LookupAllForeignSessions( | |
| 1789 &sessions, SyncedSessionTracker::RAW); | |
| 1790 ASSERT_EQ(2U, sessions.size()); | |
| 1791 EXPECT_EQ(1, CountIfTagMatches(sessions, good_header_tag)); | |
| 1792 EXPECT_EQ(1, CountIfTagMatches(sessions, good_tag_tab)); | |
| 1793 } | |
| 1794 | |
| 1795 // Test that things work if a tab is initially ignored. | |
| 1796 TEST_F(SessionsSyncManagerTest, AssociateWindowsDontReloadTabs) { | |
| 1797 syncer::SyncChangeList out; | |
| 1798 // Go to a URL that is ignored by session syncing. | |
| 1799 AddTab(browser(), GURL("chrome://preferences/")); | |
| 1800 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); | |
| 1801 ASSERT_EQ(2U, out.size()); // Header add and update. | |
| 1802 EXPECT_EQ( | |
| 1803 0, | |
| 1804 out[1].sync_data().GetSpecifics().session().header().window_size()); | |
| 1805 out.clear(); | |
| 1806 | |
| 1807 // Go to a sync-interesting URL. | |
| 1808 NavigateAndCommitActiveTab(GURL("http://foo2")); | |
| 1809 | |
| 1810 EXPECT_EQ(3U, out.size()); // Tab add, update, and header update. | |
| 1811 | |
| 1812 EXPECT_TRUE( | |
| 1813 base::StartsWith(syncer::SyncDataLocal(out[0].sync_data()).GetTag(), | |
| 1814 manager()->current_machine_tag(), | |
| 1815 base::CompareCase::SENSITIVE)); | |
| 1816 EXPECT_EQ(manager()->current_machine_tag(), | |
| 1817 out[0].sync_data().GetSpecifics().session().session_tag()); | |
| 1818 EXPECT_EQ(SyncChange::ACTION_ADD, out[0].change_type()); | |
| 1819 | |
| 1820 EXPECT_TRUE( | |
| 1821 base::StartsWith(syncer::SyncDataLocal(out[1].sync_data()).GetTag(), | |
| 1822 manager()->current_machine_tag(), | |
| 1823 base::CompareCase::SENSITIVE)); | |
| 1824 EXPECT_EQ(manager()->current_machine_tag(), | |
| 1825 out[1].sync_data().GetSpecifics().session().session_tag()); | |
| 1826 EXPECT_TRUE(out[1].sync_data().GetSpecifics().session().has_tab()); | |
| 1827 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[1].change_type()); | |
| 1828 | |
| 1829 EXPECT_TRUE(out[2].IsValid()); | |
| 1830 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[2].change_type()); | |
| 1831 const SyncData data(out[2].sync_data()); | |
| 1832 EXPECT_EQ(manager()->current_machine_tag(), | |
| 1833 syncer::SyncDataLocal(data).GetTag()); | |
| 1834 const sync_pb::SessionSpecifics& specifics(data.GetSpecifics().session()); | |
| 1835 EXPECT_EQ(manager()->current_machine_tag(), specifics.session_tag()); | |
| 1836 EXPECT_TRUE(specifics.has_header()); | |
| 1837 const sync_pb::SessionHeader& header_s = specifics.header(); | |
| 1838 EXPECT_EQ(1, header_s.window_size()); | |
| 1839 EXPECT_EQ(1, header_s.window(0).tab_size()); | |
| 1840 } | |
| 1841 | |
| 1842 // Tests that the SyncSessionManager responds to local tab events properly. | |
| 1843 TEST_F(SessionsSyncManagerTest, OnLocalTabModified) { | |
| 1844 syncer::SyncChangeList out; | |
| 1845 // Init with no local data, relies on MergeLocalSessionNoTabs. | |
| 1846 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); | |
| 1847 ASSERT_FALSE(manager()->current_machine_tag().empty()); | |
| 1848 ASSERT_EQ(2U, out.size()); | |
| 1849 | |
| 1850 // Copy the original header. | |
| 1851 sync_pb::EntitySpecifics header(out[0].sync_data().GetSpecifics()); | |
| 1852 out.clear(); | |
| 1853 | |
| 1854 const GURL foo1("http://foo/1"); | |
| 1855 const GURL foo2("http://foo/2"); | |
| 1856 const GURL bar1("http://bar/1"); | |
| 1857 const GURL bar2("http://bar/2"); | |
| 1858 AddTab(browser(), foo1); | |
| 1859 NavigateAndCommitActiveTab(foo2); | |
| 1860 AddTab(browser(), bar1); | |
| 1861 NavigateAndCommitActiveTab(bar2); | |
| 1862 | |
| 1863 // One add, one update for each AddTab. | |
| 1864 // One update for each NavigateAndCommit. | |
| 1865 // = 6 total tab updates. | |
| 1866 // One header update corresponding to each of those. | |
| 1867 // = 6 total header updates. | |
| 1868 // 12 total updates. | |
| 1869 ASSERT_EQ(12U, out.size()); | |
| 1870 | |
| 1871 // Verify the tab node creations and updates to ensure the SyncProcessor | |
| 1872 // sees the right operations. | |
| 1873 for (int i = 0; i < 12; i++) { | |
| 1874 SCOPED_TRACE(i); | |
| 1875 EXPECT_TRUE(out[i].IsValid()); | |
| 1876 const SyncData data(out[i].sync_data()); | |
| 1877 EXPECT_TRUE(base::StartsWith(syncer::SyncDataLocal(data).GetTag(), | |
| 1878 manager()->current_machine_tag(), | |
| 1879 base::CompareCase::SENSITIVE)); | |
| 1880 const sync_pb::SessionSpecifics& specifics(data.GetSpecifics().session()); | |
| 1881 EXPECT_EQ(manager()->current_machine_tag(), specifics.session_tag()); | |
| 1882 if (i % 6 == 0) { | |
| 1883 // First thing on an AddTab is a no-op header update for parented tab. | |
| 1884 EXPECT_EQ(header.SerializeAsString(), | |
| 1885 data.GetSpecifics().SerializeAsString()); | |
| 1886 EXPECT_EQ(manager()->current_machine_tag(), | |
| 1887 syncer::SyncDataLocal(data).GetTag()); | |
| 1888 } else if (i % 6 == 1) { | |
| 1889 // Next, the TabNodePool should create the tab node. | |
| 1890 EXPECT_EQ(SyncChange::ACTION_ADD, out[i].change_type()); | |
| 1891 EXPECT_EQ(TabNodePool::TabIdToTag( | |
| 1892 manager()->current_machine_tag(), | |
| 1893 data.GetSpecifics().session().tab_node_id()), | |
| 1894 syncer::SyncDataLocal(data).GetTag()); | |
| 1895 } else if (i % 6 == 2) { | |
| 1896 // Then we see the tab update to the URL. | |
| 1897 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[i].change_type()); | |
| 1898 EXPECT_EQ(TabNodePool::TabIdToTag( | |
| 1899 manager()->current_machine_tag(), | |
| 1900 data.GetSpecifics().session().tab_node_id()), | |
| 1901 syncer::SyncDataLocal(data).GetTag()); | |
| 1902 ASSERT_TRUE(specifics.has_tab()); | |
| 1903 } else if (i % 6 == 3) { | |
| 1904 // The header needs to be updated to reflect the new window state. | |
| 1905 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[i].change_type()); | |
| 1906 EXPECT_TRUE(specifics.has_header()); | |
| 1907 } else if (i % 6 == 4) { | |
| 1908 // Now we move on to NavigateAndCommit. Update the tab. | |
| 1909 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[i].change_type()); | |
| 1910 EXPECT_EQ(TabNodePool::TabIdToTag( | |
| 1911 manager()->current_machine_tag(), | |
| 1912 data.GetSpecifics().session().tab_node_id()), | |
| 1913 syncer::SyncDataLocal(data).GetTag()); | |
| 1914 ASSERT_TRUE(specifics.has_tab()); | |
| 1915 } else if (i % 6 == 5) { | |
| 1916 // The header needs to be updated to reflect the new window state. | |
| 1917 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[i].change_type()); | |
| 1918 ASSERT_TRUE(specifics.has_header()); | |
| 1919 header = data.GetSpecifics(); | |
| 1920 } | |
| 1921 } | |
| 1922 | |
| 1923 // Verify the actual content to ensure sync sees the right data. | |
| 1924 // When it's all said and done, the header should reflect two tabs. | |
| 1925 const sync_pb::SessionHeader& session_header = header.session().header(); | |
| 1926 ASSERT_EQ(1, session_header.window_size()); | |
| 1927 EXPECT_EQ(2, session_header.window(0).tab_size()); | |
| 1928 | |
| 1929 // ASSERT_TRUEs above allow us to dive in freely here. | |
| 1930 // Verify first tab. | |
| 1931 const sync_pb::SessionTab& tab1_1 = | |
| 1932 out[2].sync_data().GetSpecifics().session().tab(); | |
| 1933 ASSERT_EQ(1, tab1_1.navigation_size()); | |
| 1934 EXPECT_EQ(foo1.spec(), tab1_1.navigation(0).virtual_url()); | |
| 1935 const sync_pb::SessionTab& tab1_2 = | |
| 1936 out[4].sync_data().GetSpecifics().session().tab(); | |
| 1937 ASSERT_EQ(2, tab1_2.navigation_size()); | |
| 1938 EXPECT_EQ(foo1.spec(), tab1_2.navigation(0).virtual_url()); | |
| 1939 EXPECT_EQ(foo2.spec(), tab1_2.navigation(1).virtual_url()); | |
| 1940 | |
| 1941 // Verify second tab. | |
| 1942 const sync_pb::SessionTab& tab2_1 = | |
| 1943 out[8].sync_data().GetSpecifics().session().tab(); | |
| 1944 ASSERT_EQ(1, tab2_1.navigation_size()); | |
| 1945 EXPECT_EQ(bar1.spec(), tab2_1.navigation(0).virtual_url()); | |
| 1946 const sync_pb::SessionTab& tab2_2 = | |
| 1947 out[10].sync_data().GetSpecifics().session().tab(); | |
| 1948 ASSERT_EQ(2, tab2_2.navigation_size()); | |
| 1949 EXPECT_EQ(bar1.spec(), tab2_2.navigation(0).virtual_url()); | |
| 1950 EXPECT_EQ(bar2.spec(), tab2_2.navigation(1).virtual_url()); | |
| 1951 } | |
| 1952 | |
| 1953 // Check that if a tab becomes uninteresting (for example no syncable URLs), | |
| 1954 // we correctly remove it from the header node. | |
| 1955 TEST_F(SessionsSyncManagerTest, TabBecomesUninteresting) { | |
| 1956 syncer::SyncChangeList out; | |
| 1957 // Init with no local data, relies on MergeLocalSessionNoTabs. | |
| 1958 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); | |
| 1959 ASSERT_FALSE(manager()->current_machine_tag().empty()); | |
| 1960 ASSERT_EQ(2U, out.size()); | |
| 1961 out.clear(); | |
| 1962 | |
| 1963 const GURL kValidUrl("http://foo/1"); | |
| 1964 const GURL kInternalUrl("chrome://internal"); | |
| 1965 | |
| 1966 // Add an interesting tab. | |
| 1967 AddTab(browser(), kValidUrl); | |
| 1968 // No-op header update, tab creation, tab update, header update. | |
| 1969 ASSERT_EQ(4U, out.size()); | |
| 1970 // The last two are the interesting updates. | |
| 1971 ASSERT_TRUE(out[2].sync_data().GetSpecifics().session().has_tab()); | |
| 1972 EXPECT_EQ(kValidUrl.spec(), out[2] | |
| 1973 .sync_data() | |
| 1974 .GetSpecifics() | |
| 1975 .session() | |
| 1976 .tab() | |
| 1977 .navigation(0) | |
| 1978 .virtual_url()); | |
| 1979 ASSERT_TRUE(out[3].sync_data().GetSpecifics().session().has_header()); | |
| 1980 ASSERT_EQ(1, | |
| 1981 out[3].sync_data().GetSpecifics().session().header().window_size()); | |
| 1982 ASSERT_EQ(1, out[3] | |
| 1983 .sync_data() | |
| 1984 .GetSpecifics() | |
| 1985 .session() | |
| 1986 .header() | |
| 1987 .window(0) | |
| 1988 .tab_size()); | |
| 1989 | |
| 1990 // Navigate five times to uninteresting urls to push the interesting one off | |
| 1991 // the back of the stack. | |
| 1992 NavigateAndCommitActiveTab(kInternalUrl); | |
| 1993 NavigateAndCommitActiveTab(kInternalUrl); | |
| 1994 NavigateAndCommitActiveTab(kInternalUrl); | |
| 1995 NavigateAndCommitActiveTab(kInternalUrl); | |
| 1996 | |
| 1997 // Reset |out| so we only see the effects of the final navigation. | |
| 1998 out.clear(); | |
| 1999 NavigateAndCommitActiveTab(kInternalUrl); | |
| 2000 | |
| 2001 // Only the header node should be updated, and it should no longer have any | |
| 2002 // valid windows/tabs. | |
| 2003 ASSERT_EQ(2U, out.size()); // Two header updates (first is a no-op). | |
| 2004 ASSERT_TRUE(out[1].sync_data().GetSpecifics().session().has_header()); | |
| 2005 EXPECT_EQ(1, | |
| 2006 out[1].sync_data().GetSpecifics().session().header().window_size()); | |
| 2007 } | |
| 2008 | |
| 2009 // Ensure model association associates the pre-existing tabs. | |
| 2010 TEST_F(SessionsSyncManagerTest, MergeLocalSessionExistingTabs) { | |
| 2011 AddTab(browser(), GURL("http://foo1")); | |
| 2012 NavigateAndCommitActiveTab(GURL("http://foo2")); // Adds back entry. | |
| 2013 AddTab(browser(), GURL("http://bar1")); | |
| 2014 NavigateAndCommitActiveTab(GURL("http://bar2")); // Adds back entry. | |
| 2015 | |
| 2016 syncer::SyncChangeList out; | |
| 2017 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); | |
| 2018 ASSERT_EQ(6U, out.size()); | |
| 2019 | |
| 2020 // Check that this machine's data is not included in the foreign windows. | |
| 2021 std::vector<const SyncedSession*> foreign_sessions; | |
| 2022 ASSERT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 2023 | |
| 2024 // Verify the header. | |
| 2025 EXPECT_TRUE(out[0].IsValid()); | |
| 2026 EXPECT_EQ(SyncChange::ACTION_ADD, out[0].change_type()); | |
| 2027 const SyncData data(out[0].sync_data()); | |
| 2028 EXPECT_EQ(manager()->current_machine_tag(), | |
| 2029 syncer::SyncDataLocal(data).GetTag()); | |
| 2030 const sync_pb::SessionSpecifics& specifics(data.GetSpecifics().session()); | |
| 2031 EXPECT_EQ(manager()->current_machine_tag(), specifics.session_tag()); | |
| 2032 EXPECT_TRUE(specifics.has_header()); | |
| 2033 const sync_pb::SessionHeader& header_s = specifics.header(); | |
| 2034 EXPECT_TRUE(header_s.has_device_type()); | |
| 2035 EXPECT_EQ(GetLocalDeviceInfo()->client_name(), header_s.client_name()); | |
| 2036 EXPECT_EQ(0, header_s.window_size()); | |
| 2037 | |
| 2038 // Verify the tab node creations and updates with content. | |
| 2039 for (int i = 1; i < 5; i++) { | |
| 2040 EXPECT_TRUE(out[i].IsValid()); | |
| 2041 const SyncData data(out[i].sync_data()); | |
| 2042 EXPECT_TRUE(base::StartsWith(syncer::SyncDataLocal(data).GetTag(), | |
| 2043 manager()->current_machine_tag(), | |
| 2044 base::CompareCase::SENSITIVE)); | |
| 2045 const sync_pb::SessionSpecifics& specifics(data.GetSpecifics().session()); | |
| 2046 EXPECT_EQ(manager()->current_machine_tag(), specifics.session_tag()); | |
| 2047 if (i % 2 == 1) { | |
| 2048 EXPECT_EQ(SyncChange::ACTION_ADD, out[i].change_type()); | |
| 2049 } else { | |
| 2050 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[i].change_type()); | |
| 2051 EXPECT_TRUE(specifics.has_tab()); | |
| 2052 } | |
| 2053 } | |
| 2054 | |
| 2055 // Verify the header was updated to reflect new window state. | |
| 2056 EXPECT_TRUE(out[5].IsValid()); | |
| 2057 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[5].change_type()); | |
| 2058 const SyncData data_2(out[5].sync_data()); | |
| 2059 EXPECT_EQ(manager()->current_machine_tag(), | |
| 2060 syncer::SyncDataLocal(data_2).GetTag()); | |
| 2061 const sync_pb::SessionSpecifics& specifics2(data_2.GetSpecifics().session()); | |
| 2062 EXPECT_EQ(manager()->current_machine_tag(), specifics2.session_tag()); | |
| 2063 EXPECT_TRUE(specifics2.has_header()); | |
| 2064 const sync_pb::SessionHeader& header_s2 = specifics2.header(); | |
| 2065 EXPECT_EQ(1, header_s2.window_size()); | |
| 2066 | |
| 2067 // Verify TabLinks. | |
| 2068 SessionsSyncManager::TabLinksMap tab_map = manager()->local_tab_map_; | |
| 2069 ASSERT_EQ(2U, tab_map.size()); | |
| 2070 // Tabs are ordered by sessionid in tab_map, so should be able to traverse | |
| 2071 // the tree based on order of tabs created | |
| 2072 SessionsSyncManager::TabLinksMap::iterator iter = tab_map.begin(); | |
| 2073 ASSERT_EQ(2, iter->second->tab()->GetEntryCount()); | |
| 2074 EXPECT_EQ(GURL("http://foo1"), iter->second->tab()->GetVirtualURLAtIndex(0)); | |
| 2075 EXPECT_EQ(GURL("http://foo2"), iter->second->tab()->GetVirtualURLAtIndex(1)); | |
| 2076 iter++; | |
| 2077 ASSERT_EQ(2, iter->second->tab()->GetEntryCount()); | |
| 2078 EXPECT_EQ(GURL("http://bar1"), iter->second->tab()->GetVirtualURLAtIndex(0)); | |
| 2079 EXPECT_EQ(GURL("http://bar2"), iter->second->tab()->GetVirtualURLAtIndex(1)); | |
| 2080 } | |
| 2081 | |
| 2082 TEST_F(SessionsSyncManagerTest, ForeignSessionModifiedTime) { | |
| 2083 syncer::SyncDataList foreign_data; | |
| 2084 base::Time newest_time = base::Time::Now() - base::TimeDelta::FromDays(1); | |
| 2085 base::Time middle_time = base::Time::Now() - base::TimeDelta::FromDays(2); | |
| 2086 base::Time oldest_time = base::Time::Now() - base::TimeDelta::FromDays(3); | |
| 2087 | |
| 2088 { | |
| 2089 std::string session_tag = "tag1"; | |
| 2090 SessionID::id_type n[] = {1, 2}; | |
| 2091 std::vector<SessionID::id_type> tab_list(n, n + arraysize(n)); | |
| 2092 std::vector<sync_pb::SessionSpecifics> tabs; | |
| 2093 sync_pb::SessionSpecifics meta( | |
| 2094 helper()->BuildForeignSession(session_tag, tab_list, &tabs)); | |
| 2095 AddToSyncDataList(tabs[0], &foreign_data, newest_time); | |
| 2096 AddToSyncDataList(meta, &foreign_data, middle_time); | |
| 2097 AddToSyncDataList(tabs[1], &foreign_data, oldest_time); | |
| 2098 } | |
| 2099 | |
| 2100 { | |
| 2101 std::string session_tag = "tag2"; | |
| 2102 SessionID::id_type n[] = {3, 4}; | |
| 2103 std::vector<SessionID::id_type> tab_list(n, n + arraysize(n)); | |
| 2104 std::vector<sync_pb::SessionSpecifics> tabs; | |
| 2105 sync_pb::SessionSpecifics meta( | |
| 2106 helper()->BuildForeignSession(session_tag, tab_list, &tabs)); | |
| 2107 AddToSyncDataList(tabs[0], &foreign_data, middle_time); | |
| 2108 AddToSyncDataList(meta, &foreign_data, newest_time); | |
| 2109 AddToSyncDataList(tabs[1], &foreign_data, oldest_time); | |
| 2110 } | |
| 2111 | |
| 2112 { | |
| 2113 std::string session_tag = "tag3"; | |
| 2114 SessionID::id_type n[] = {5, 6}; | |
| 2115 std::vector<SessionID::id_type> tab_list(n, n + arraysize(n)); | |
| 2116 std::vector<sync_pb::SessionSpecifics> tabs; | |
| 2117 sync_pb::SessionSpecifics meta( | |
| 2118 helper()->BuildForeignSession(session_tag, tab_list, &tabs)); | |
| 2119 AddToSyncDataList(tabs[0], &foreign_data, oldest_time); | |
| 2120 AddToSyncDataList(meta, &foreign_data, middle_time); | |
| 2121 AddToSyncDataList(tabs[1], &foreign_data, newest_time); | |
| 2122 } | |
| 2123 | |
| 2124 syncer::SyncChangeList output; | |
| 2125 InitWithSyncDataTakeOutput(foreign_data, &output); | |
| 2126 ASSERT_EQ(2U, output.size()); | |
| 2127 output.clear(); | |
| 2128 | |
| 2129 std::vector<const SyncedSession*> foreign_sessions; | |
| 2130 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 2131 ASSERT_EQ(3U, foreign_sessions.size()); | |
| 2132 EXPECT_EQ(newest_time, foreign_sessions[0]->modified_time); | |
| 2133 EXPECT_EQ(newest_time, foreign_sessions[1]->modified_time); | |
| 2134 EXPECT_EQ(newest_time, foreign_sessions[2]->modified_time); | |
| 2135 } | |
| 2136 | |
| 2137 // Test garbage collection of stale foreign sessions. | |
| 2138 TEST_F(SessionsSyncManagerTest, DoGarbageCollection) { | |
| 2139 // Fill two instances of session specifics with a foreign session's data. | |
| 2140 std::string tag1 = "tag1"; | |
| 2141 SessionID::id_type n1[] = {5, 10, 13, 17}; | |
| 2142 std::vector<SessionID::id_type> tab_list1(n1, n1 + arraysize(n1)); | |
| 2143 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 2144 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | |
| 2145 tag1, tab_list1, &tabs1)); | |
| 2146 std::string tag2 = "tag2"; | |
| 2147 SessionID::id_type n2[] = {8, 15, 18, 20}; | |
| 2148 std::vector<SessionID::id_type> tab_list2(n2, n2 + arraysize(n2)); | |
| 2149 std::vector<sync_pb::SessionSpecifics> tabs2; | |
| 2150 sync_pb::SessionSpecifics meta2(helper()->BuildForeignSession( | |
| 2151 tag2, tab_list2, &tabs2)); | |
| 2152 // Set the modification time for tag1 to be 21 days ago, tag2 to 5 days ago. | |
| 2153 base::Time tag1_time = base::Time::Now() - base::TimeDelta::FromDays(21); | |
| 2154 base::Time tag2_time = base::Time::Now() - base::TimeDelta::FromDays(5); | |
| 2155 | |
| 2156 syncer::SyncDataList foreign_data; | |
| 2157 foreign_data.push_back(CreateRemoteData(meta, tag1_time)); | |
| 2158 foreign_data.push_back(CreateRemoteData(meta2, tag2_time)); | |
| 2159 AddTabsToSyncDataList(tabs1, &foreign_data); | |
| 2160 AddTabsToSyncDataList(tabs2, &foreign_data); | |
| 2161 | |
| 2162 syncer::SyncChangeList output; | |
| 2163 InitWithSyncDataTakeOutput(foreign_data, &output); | |
| 2164 ASSERT_EQ(2U, output.size()); | |
| 2165 output.clear(); | |
| 2166 | |
| 2167 // Check that the foreign session was associated and retrieve the data. | |
| 2168 std::vector<const SyncedSession*> foreign_sessions; | |
| 2169 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 2170 ASSERT_EQ(2U, foreign_sessions.size()); | |
| 2171 foreign_sessions.clear(); | |
| 2172 | |
| 2173 // Now garbage collect and verify the non-stale session is still there. | |
| 2174 manager()->DoGarbageCollection(); | |
| 2175 ASSERT_EQ(5U, output.size()); | |
| 2176 EXPECT_EQ(SyncChange::ACTION_DELETE, output[0].change_type()); | |
| 2177 const SyncData data(output[0].sync_data()); | |
| 2178 EXPECT_EQ(tag1, syncer::SyncDataLocal(data).GetTag()); | |
| 2179 for (int i = 1; i < 5; i++) { | |
| 2180 EXPECT_EQ(SyncChange::ACTION_DELETE, output[i].change_type()); | |
| 2181 const SyncData data(output[i].sync_data()); | |
| 2182 EXPECT_EQ(TabNodePool::TabIdToTag(tag1, i), | |
| 2183 syncer::SyncDataLocal(data).GetTag()); | |
| 2184 } | |
| 2185 | |
| 2186 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 2187 ASSERT_EQ(1U, foreign_sessions.size()); | |
| 2188 std::vector<std::vector<SessionID::id_type> > session_reference; | |
| 2189 session_reference.push_back(tab_list2); | |
| 2190 helper()->VerifySyncedSession(tag2, session_reference, | |
| 2191 *(foreign_sessions[0])); | |
| 2192 } | |
| 2193 | |
| 2194 TEST_F(SessionsSyncManagerTest, DoGarbageCollectionOrphans) { | |
| 2195 syncer::SyncDataList foreign_data; | |
| 2196 base::Time stale_mtime = base::Time::Now() - base::TimeDelta::FromDays(15); | |
| 2197 | |
| 2198 { | |
| 2199 // A stale session with empty header | |
| 2200 std::string session_tag = "tag1"; | |
| 2201 std::vector<SessionID::id_type> tab_list; | |
| 2202 std::vector<sync_pb::SessionSpecifics> tabs; | |
| 2203 sync_pb::SessionSpecifics meta( | |
| 2204 helper()->BuildForeignSession(session_tag, tab_list, &tabs)); | |
| 2205 AddToSyncDataList(meta, &foreign_data, stale_mtime); | |
| 2206 } | |
| 2207 | |
| 2208 { | |
| 2209 // A stale session with orphans w/o header | |
| 2210 std::string session_tag = "tag2"; | |
| 2211 sync_pb::SessionSpecifics orphan; | |
| 2212 helper()->BuildTabSpecifics(session_tag, 0, 1, &orphan); | |
| 2213 AddToSyncDataList(orphan, &foreign_data, stale_mtime); | |
| 2214 } | |
| 2215 | |
| 2216 { | |
| 2217 // A stale session with valid header/tab and an orphaned tab. | |
| 2218 std::string session_tag = "tag3"; | |
| 2219 SessionID::id_type n[] = {2}; | |
| 2220 std::vector<SessionID::id_type> tab_list(n, n + arraysize(n)); | |
| 2221 std::vector<sync_pb::SessionSpecifics> tabs; | |
| 2222 sync_pb::SessionSpecifics meta( | |
| 2223 helper()->BuildForeignSession(session_tag, tab_list, &tabs)); | |
| 2224 | |
| 2225 // BuildForeignSession(...) will use a window id of 0, and we're also | |
| 2226 // passing a window id of 0 to BuildTabSpecifics(...) here. It doesn't | |
| 2227 // really matter what window id we use for the orphaned tab, in the real | |
| 2228 // world orphans often reference real/still valid windows, but they're | |
| 2229 // orphans because the window/header doesn't reference back to them. | |
| 2230 sync_pb::SessionSpecifics orphan; | |
| 2231 helper()->BuildTabSpecifics(session_tag, 0, 1, &orphan); | |
| 2232 AddToSyncDataList(orphan, &foreign_data, stale_mtime); | |
| 2233 | |
| 2234 AddToSyncDataList(tabs[0], &foreign_data, stale_mtime); | |
| 2235 AddToSyncDataList(orphan, &foreign_data, stale_mtime); | |
| 2236 AddToSyncDataList(meta, &foreign_data, stale_mtime); | |
| 2237 } | |
| 2238 | |
| 2239 syncer::SyncChangeList output; | |
| 2240 InitWithSyncDataTakeOutput(foreign_data, &output); | |
| 2241 ASSERT_EQ(2U, output.size()); | |
| 2242 output.clear(); | |
| 2243 | |
| 2244 // Although we have 3 foreign sessions, only 1 is valid/clean enough. | |
| 2245 std::vector<const SyncedSession*> foreign_sessions; | |
| 2246 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 2247 ASSERT_EQ(1U, foreign_sessions.size()); | |
| 2248 foreign_sessions.clear(); | |
| 2249 | |
| 2250 // Everything should get removed here. | |
| 2251 manager()->DoGarbageCollection(); | |
| 2252 // Expect 5 deletions. tag1 header only, tag2 tab only, tag3 header + 2x tabs. | |
| 2253 ASSERT_EQ(5U, output.size()); | |
| 2254 } | |
| 2255 | |
| 2256 // Test that an update to a previously considered "stale" session, | |
| 2257 // prior to garbage collection, will save the session from deletion. | |
| 2258 TEST_F(SessionsSyncManagerTest, GarbageCollectionHonoursUpdate) { | |
| 2259 std::string tag1 = "tag1"; | |
| 2260 SessionID::id_type n1[] = {5, 10, 13, 17}; | |
| 2261 std::vector<SessionID::id_type> tab_list1(n1, n1 + arraysize(n1)); | |
| 2262 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 2263 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | |
| 2264 tag1, tab_list1, &tabs1)); | |
| 2265 syncer::SyncDataList foreign_data; | |
| 2266 base::Time tag1_time = base::Time::Now() - base::TimeDelta::FromDays(21); | |
| 2267 foreign_data.push_back(CreateRemoteData(meta, tag1_time)); | |
| 2268 AddTabsToSyncDataList(tabs1, &foreign_data); | |
| 2269 syncer::SyncChangeList output; | |
| 2270 InitWithSyncDataTakeOutput(foreign_data, &output); | |
| 2271 ASSERT_EQ(2U, output.size()); | |
| 2272 | |
| 2273 // Update to a non-stale time. | |
| 2274 sync_pb::EntitySpecifics update_entity; | |
| 2275 update_entity.mutable_session()->CopyFrom(tabs1[0]); | |
| 2276 syncer::SyncChangeList changes; | |
| 2277 changes.push_back( | |
| 2278 syncer::SyncChange(FROM_HERE, SyncChange::ACTION_UPDATE, | |
| 2279 CreateRemoteData(tabs1[0], base::Time::Now()))); | |
| 2280 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 2281 | |
| 2282 // Check that the foreign session was associated and retrieve the data. | |
| 2283 std::vector<const SyncedSession*> foreign_sessions; | |
| 2284 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 2285 ASSERT_EQ(1U, foreign_sessions.size()); | |
| 2286 foreign_sessions.clear(); | |
| 2287 | |
| 2288 // Verify the now non-stale session does not get deleted. | |
| 2289 manager()->DoGarbageCollection(); | |
| 2290 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 2291 ASSERT_EQ(1U, foreign_sessions.size()); | |
| 2292 std::vector<std::vector<SessionID::id_type> > session_reference; | |
| 2293 session_reference.push_back(tab_list1); | |
| 2294 helper()->VerifySyncedSession( | |
| 2295 tag1, session_reference, *(foreign_sessions[0])); | |
| 2296 } | |
| 2297 | |
| 2298 // Test that swapping WebContents for a tab is properly observed and handled | |
| 2299 // by the SessionsSyncManager. | |
| 2300 TEST_F(SessionsSyncManagerTest, CheckPrerenderedWebContentsSwap) { | |
| 2301 AddTab(browser(), GURL("http://foo1")); | |
| 2302 NavigateAndCommitActiveTab(GURL("http://foo2")); | |
| 2303 | |
| 2304 syncer::SyncChangeList out; | |
| 2305 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); | |
| 2306 ASSERT_EQ(4U, out.size()); // Header, tab ADD, tab UPDATE, header UPDATE. | |
| 2307 | |
| 2308 // To simulate WebContents swap during prerendering, create new WebContents | |
| 2309 // and swap with old WebContents. | |
| 2310 std::unique_ptr<content::WebContents> old_web_contents; | |
| 2311 old_web_contents.reset(browser()->tab_strip_model()->GetActiveWebContents()); | |
| 2312 | |
| 2313 // Create new WebContents, with the required tab helpers. | |
| 2314 WebContents* new_web_contents = WebContents::CreateWithSessionStorage( | |
| 2315 WebContents::CreateParams(profile()), | |
| 2316 old_web_contents->GetController().GetSessionStorageNamespaceMap()); | |
| 2317 SessionTabHelper::CreateForWebContents(new_web_contents); | |
| 2318 TabContentsSyncedTabDelegate::CreateForWebContents(new_web_contents); | |
| 2319 new_web_contents->GetController() | |
| 2320 .CopyStateFrom(old_web_contents->GetController()); | |
| 2321 | |
| 2322 // Swap the WebContents. | |
| 2323 int index = browser()->tab_strip_model()->GetIndexOfWebContents( | |
| 2324 old_web_contents.get()); | |
| 2325 browser()->tab_strip_model()->ReplaceWebContentsAt(index, new_web_contents); | |
| 2326 | |
| 2327 ASSERT_EQ(9U, out.size()); | |
| 2328 EXPECT_EQ(SyncChange::ACTION_ADD, out[4].change_type()); | |
| 2329 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[5].change_type()); | |
| 2330 | |
| 2331 // Navigate away. | |
| 2332 NavigateAndCommitActiveTab(GURL("http://bar2")); | |
| 2333 | |
| 2334 // Delete old WebContents. This should not crash. | |
| 2335 old_web_contents.reset(); | |
| 2336 | |
| 2337 // Try more navigations and verify output size. This can also reveal | |
| 2338 // bugs (leaks) on memcheck bots if the SessionSyncManager | |
| 2339 // didn't properly clean up the tab pool or session tracker. | |
| 2340 NavigateAndCommitActiveTab(GURL("http://bar3")); | |
| 2341 | |
| 2342 AddTab(browser(), GURL("http://bar4")); | |
| 2343 NavigateAndCommitActiveTab(GURL("http://bar5")); | |
| 2344 ASSERT_EQ(19U, out.size()); | |
| 2345 } | |
| 2346 | |
| 2347 // Test that NOTIFICATION_FOREIGN_SESSION_UPDATED is sent when processing | |
| 2348 // sync changes. | |
| 2349 TEST_F(SessionsSyncManagerTest, NotifiedOfUpdates) { | |
| 2350 ASSERT_FALSE(observer()->notified_of_update()); | |
| 2351 InitWithNoSyncData(); | |
| 2352 | |
| 2353 SessionID::id_type n[] = {5}; | |
| 2354 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 2355 std::vector<SessionID::id_type> tab_list(n, n + arraysize(n)); | |
| 2356 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | |
| 2357 "tag1", tab_list, &tabs1)); | |
| 2358 | |
| 2359 syncer::SyncChangeList changes; | |
| 2360 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_ADD)); | |
| 2361 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 2362 EXPECT_TRUE(observer()->notified_of_update()); | |
| 2363 | |
| 2364 changes.clear(); | |
| 2365 observer()->Reset(); | |
| 2366 AddTabsToChangeList(tabs1, SyncChange::ACTION_ADD, &changes); | |
| 2367 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 2368 EXPECT_TRUE(observer()->notified_of_update()); | |
| 2369 | |
| 2370 changes.clear(); | |
| 2371 observer()->Reset(); | |
| 2372 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_DELETE)); | |
| 2373 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 2374 EXPECT_TRUE(observer()->notified_of_update()); | |
| 2375 } | |
| 2376 | |
| 2377 // Test that NOTIFICATION_FOREIGN_SESSION_UPDATED is sent when handling | |
| 2378 // local hide/removal of foreign session. | |
| 2379 TEST_F(SessionsSyncManagerTest, NotifiedOfLocalRemovalOfForeignSession) { | |
| 2380 InitWithNoSyncData(); | |
| 2381 const std::string tag("tag1"); | |
| 2382 SessionID::id_type n[] = {5}; | |
| 2383 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 2384 std::vector<SessionID::id_type> tab_list(n, n + arraysize(n)); | |
| 2385 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | |
| 2386 tag, tab_list, &tabs1)); | |
| 2387 | |
| 2388 syncer::SyncChangeList changes; | |
| 2389 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_ADD)); | |
| 2390 manager()->ProcessSyncChanges(FROM_HERE, changes); | |
| 2391 | |
| 2392 observer()->Reset(); | |
| 2393 ASSERT_FALSE(observer()->notified_of_update()); | |
| 2394 manager()->DeleteForeignSession(tag); | |
| 2395 ASSERT_TRUE(observer()->notified_of_update()); | |
| 2396 } | |
| 2397 | |
| 2398 #if defined(OS_ANDROID) | |
| 2399 // Tests that opening the other devices page triggers a session sync refresh. | |
| 2400 // This page only exists on mobile platforms today; desktop has a | |
| 2401 // search-enhanced NTP without other devices. | |
| 2402 TEST_F(SessionsSyncManagerTest, NotifiedOfRefresh) { | |
| 2403 ASSERT_FALSE(observer()->notified_of_refresh()); | |
| 2404 InitWithNoSyncData(); | |
| 2405 AddTab(browser(), GURL("http://foo1")); | |
| 2406 EXPECT_FALSE(observer()->notified_of_refresh()); | |
| 2407 NavigateAndCommitActiveTab(GURL("chrome://newtab/#open_tabs")); | |
| 2408 EXPECT_TRUE(observer()->notified_of_refresh()); | |
| 2409 } | |
| 2410 #endif // defined(OS_ANDROID) | |
| 2411 | |
| 2412 // Tests receipt of duplicate tab IDs in the same window. This should never | |
| 2413 // happen, but we want to make sure the client won't do anything bad if it does | |
| 2414 // receive such garbage input data. | |
| 2415 TEST_F(SessionsSyncManagerTest, ReceiveDuplicateTabInSameWindow) { | |
| 2416 std::string tag = "tag1"; | |
| 2417 | |
| 2418 // Reuse tab ID 10 in an attempt to trigger bad behavior. | |
| 2419 SessionID::id_type n1[] = {5, 10, 10, 17}; | |
| 2420 std::vector<SessionID::id_type> tab_list1(n1, n1 + arraysize(n1)); | |
| 2421 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 2422 sync_pb::SessionSpecifics meta( | |
| 2423 helper()->BuildForeignSession(tag, tab_list1, &tabs1)); | |
| 2424 | |
| 2425 // Set up initial data. | |
| 2426 syncer::SyncDataList initial_data; | |
| 2427 sync_pb::EntitySpecifics entity; | |
| 2428 entity.mutable_session()->CopyFrom(meta); | |
| 2429 initial_data.push_back(CreateRemoteData(entity)); | |
| 2430 AddTabsToSyncDataList(tabs1, &initial_data); | |
| 2431 | |
| 2432 syncer::SyncChangeList output; | |
| 2433 InitWithSyncDataTakeOutput(initial_data, &output); | |
| 2434 } | |
| 2435 | |
| 2436 // Tests receipt of duplicate tab IDs for the same session. The duplicate tab | |
| 2437 // ID is present in two different windows. A client can't be expected to do | |
| 2438 // anything reasonable with this input, but we can expect that it doesn't | |
| 2439 // crash. | |
| 2440 TEST_F(SessionsSyncManagerTest, ReceiveDuplicateTabInOtherWindow) { | |
| 2441 std::string tag = "tag1"; | |
| 2442 | |
| 2443 SessionID::id_type n1[] = {5, 10, 17}; | |
| 2444 std::vector<SessionID::id_type> tab_list1(n1, n1 + arraysize(n1)); | |
| 2445 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 2446 sync_pb::SessionSpecifics meta( | |
| 2447 helper()->BuildForeignSession(tag, tab_list1, &tabs1)); | |
| 2448 | |
| 2449 // Add a second window. Tab ID 10 is a duplicate. | |
| 2450 SessionID::id_type n2[] = {10, 18, 20}; | |
| 2451 std::vector<SessionID::id_type> tab_list2(n2, n2 + arraysize(n2)); | |
| 2452 helper()->AddWindowSpecifics(1, tab_list2, &meta); | |
| 2453 | |
| 2454 // Set up initial data. | |
| 2455 syncer::SyncDataList initial_data; | |
| 2456 sync_pb::EntitySpecifics entity; | |
| 2457 entity.mutable_session()->CopyFrom(meta); | |
| 2458 initial_data.push_back(CreateRemoteData(entity)); | |
| 2459 AddTabsToSyncDataList(tabs1, &initial_data); | |
| 2460 | |
| 2461 for (size_t i = 0; i < tab_list2.size(); ++i) { | |
| 2462 sync_pb::EntitySpecifics entity; | |
| 2463 helper()->BuildTabSpecifics(tag, 0, tab_list2[i], entity.mutable_session()); | |
| 2464 initial_data.push_back(CreateRemoteData(entity)); | |
| 2465 } | |
| 2466 | |
| 2467 syncer::SyncChangeList output; | |
| 2468 InitWithSyncDataTakeOutput(initial_data, &output); | |
| 2469 } | |
| 2470 | |
| 2471 // Tests receipt of multiple unassociated tabs and makes sure that | |
| 2472 // the ones with later timestamp win | |
| 2473 TEST_F(SessionsSyncManagerTest, ReceiveDuplicateUnassociatedTabs) { | |
| 2474 std::string tag = "tag1"; | |
| 2475 | |
| 2476 SessionID::id_type n1[] = {5, 10, 17}; | |
| 2477 std::vector<SessionID::id_type> tab_list1(n1, n1 + arraysize(n1)); | |
| 2478 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 2479 sync_pb::SessionSpecifics meta( | |
| 2480 helper()->BuildForeignSession(tag, tab_list1, &tabs1)); | |
| 2481 | |
| 2482 // Set up initial data. | |
| 2483 syncer::SyncDataList initial_data; | |
| 2484 initial_data.push_back(CreateRemoteData(meta)); | |
| 2485 | |
| 2486 sync_pb::EntitySpecifics entity; | |
| 2487 | |
| 2488 for (size_t i = 0; i < tabs1.size(); i++) { | |
| 2489 entity.mutable_session()->CopyFrom(tabs1[i]); | |
| 2490 initial_data.push_back( | |
| 2491 CreateRemoteData(entity, base::Time::FromDoubleT(2000))); | |
| 2492 } | |
| 2493 | |
| 2494 // Add two more tabs with duplicating IDs but with different modification | |
| 2495 // times, one before and one after the tabs above. | |
| 2496 // These two tabs get a different visual indices to distinguish them from the | |
| 2497 // tabs above that get visual index 1 by default. | |
| 2498 sync_pb::SessionSpecifics duplicating_tab1; | |
| 2499 helper()->BuildTabSpecifics(tag, 0, 10, &duplicating_tab1); | |
| 2500 duplicating_tab1.mutable_tab()->set_tab_visual_index(2); | |
| 2501 entity.mutable_session()->CopyFrom(duplicating_tab1); | |
| 2502 initial_data.push_back( | |
| 2503 CreateRemoteData(entity, base::Time::FromDoubleT(1000))); | |
| 2504 | |
| 2505 sync_pb::SessionSpecifics duplicating_tab2; | |
| 2506 helper()->BuildTabSpecifics(tag, 0, 17, &duplicating_tab2); | |
| 2507 duplicating_tab2.mutable_tab()->set_tab_visual_index(3); | |
| 2508 entity.mutable_session()->CopyFrom(duplicating_tab2); | |
| 2509 initial_data.push_back( | |
| 2510 CreateRemoteData(entity, base::Time::FromDoubleT(3000))); | |
| 2511 | |
| 2512 syncer::SyncChangeList output; | |
| 2513 InitWithSyncDataTakeOutput(initial_data, &output); | |
| 2514 | |
| 2515 std::vector<const SyncedSession*> foreign_sessions; | |
| 2516 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 2517 | |
| 2518 const std::vector<std::unique_ptr<sessions::SessionTab>>& window_tabs = | |
| 2519 foreign_sessions[0]->windows.find(0)->second->tabs; | |
| 2520 ASSERT_EQ(3U, window_tabs.size()); | |
| 2521 // The first one is from the original set of tabs. | |
| 2522 ASSERT_EQ(1, window_tabs[0]->tab_visual_index); | |
| 2523 // The one from the original set of tabs wins over duplicating_tab1. | |
| 2524 ASSERT_EQ(1, window_tabs[1]->tab_visual_index); | |
| 2525 // duplicating_tab2 wins due to the later timestamp. | |
| 2526 ASSERT_EQ(3, window_tabs[2]->tab_visual_index); | |
| 2527 } | |
| 2528 | |
| 2529 // Verify that GetAllForeignSessions returns all sessions sorted by recency. | |
| 2530 TEST_F(SessionsSyncManagerTest, GetAllForeignSessions) { | |
| 2531 SessionID::id_type ids[] = {5, 10, 13, 17}; | |
| 2532 std::vector<SessionID::id_type> tab_list(ids, ids + arraysize(ids)); | |
| 2533 | |
| 2534 const std::string kTag = "tag1"; | |
| 2535 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 2536 sync_pb::SessionSpecifics meta1(helper()->BuildForeignSession( | |
| 2537 kTag, tab_list, &tabs1)); | |
| 2538 | |
| 2539 const std::string kTag2 = "tag2"; | |
| 2540 std::vector<sync_pb::SessionSpecifics> tabs2; | |
| 2541 sync_pb::SessionSpecifics meta2(helper()->BuildForeignSession( | |
| 2542 kTag2, tab_list, &tabs2)); | |
| 2543 | |
| 2544 syncer::SyncDataList initial_data; | |
| 2545 initial_data.push_back( | |
| 2546 CreateRemoteData(meta1, base::Time::FromInternalValue(10))); | |
| 2547 AddTabsToSyncDataList(tabs1, &initial_data); | |
| 2548 initial_data.push_back( | |
| 2549 CreateRemoteData(meta2, base::Time::FromInternalValue(200))); | |
| 2550 AddTabsToSyncDataList(tabs2, &initial_data); | |
| 2551 | |
| 2552 syncer::SyncChangeList output; | |
| 2553 InitWithSyncDataTakeOutput(initial_data, &output); | |
| 2554 | |
| 2555 std::vector<const SyncedSession*> foreign_sessions; | |
| 2556 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 2557 ASSERT_EQ(2U, foreign_sessions.size()); | |
| 2558 ASSERT_GT(foreign_sessions[0]->modified_time, | |
| 2559 foreign_sessions[1]->modified_time); | |
| 2560 } | |
| 2561 | |
| 2562 // Verify that GetForeignSessionTabs returns all tabs for a session sorted | |
| 2563 // by recency. | |
| 2564 TEST_F(SessionsSyncManagerTest, GetForeignSessionTabs) { | |
| 2565 const std::string kTag = "tag1"; | |
| 2566 | |
| 2567 SessionID::id_type n1[] = {5, 10, 13, 17}; | |
| 2568 std::vector<SessionID::id_type> tab_list1(n1, n1 + arraysize(n1)); | |
| 2569 std::vector<sync_pb::SessionSpecifics> tabs1; | |
| 2570 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | |
| 2571 kTag, tab_list1, &tabs1)); | |
| 2572 // Add a second window. | |
| 2573 SessionID::id_type n2[] = {7, 15, 18, 20}; | |
| 2574 std::vector<SessionID::id_type> tab_list2(n2, n2 + arraysize(n2)); | |
| 2575 helper()->AddWindowSpecifics(1, tab_list2, &meta); | |
| 2576 | |
| 2577 // Set up initial data. | |
| 2578 syncer::SyncDataList initial_data; | |
| 2579 initial_data.push_back(CreateRemoteData(meta)); | |
| 2580 | |
| 2581 // Add the first window's tabs. | |
| 2582 AddTabsToSyncDataList(tabs1, &initial_data); | |
| 2583 | |
| 2584 // Add the second window's tabs. | |
| 2585 for (size_t i = 0; i < tab_list2.size(); ++i) { | |
| 2586 sync_pb::EntitySpecifics entity; | |
| 2587 helper()->BuildTabSpecifics(kTag, 0, tab_list2[i], | |
| 2588 entity.mutable_session()); | |
| 2589 // Order the tabs oldest to most ReceiveDuplicateUnassociatedTabs and | |
| 2590 // left to right visually. | |
| 2591 initial_data.push_back( | |
| 2592 CreateRemoteData(entity, base::Time::FromInternalValue(i + 1))); | |
| 2593 } | |
| 2594 | |
| 2595 syncer::SyncChangeList output; | |
| 2596 InitWithSyncDataTakeOutput(initial_data, &output); | |
| 2597 | |
| 2598 std::vector<const sessions::SessionTab*> tabs; | |
| 2599 ASSERT_TRUE(manager()->GetForeignSessionTabs(kTag, &tabs)); | |
| 2600 // Assert that the size matches the total number of tabs and that the order | |
| 2601 // is from most recent to least. | |
| 2602 ASSERT_EQ(tab_list1.size() + tab_list2.size(), tabs.size()); | |
| 2603 base::Time last_time; | |
| 2604 for (size_t i = 0; i < tabs.size(); ++i) { | |
| 2605 base::Time this_time = tabs[i]->timestamp; | |
| 2606 if (i > 0) | |
| 2607 ASSERT_GE(last_time, this_time); | |
| 2608 last_time = tabs[i]->timestamp; | |
| 2609 } | |
| 2610 } | |
| 2611 | |
| 2612 } // namespace sync_sessions | |
| OLD | NEW |