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