Chromium Code Reviews| 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; | |
| 46 | 41 |
| 47 namespace sync_sessions { | 42 namespace sync_sessions { |
| 48 | 43 |
| 49 namespace { | 44 namespace { |
| 50 | 45 |
| 46 const char kTitle[] = "title"; | |
| 47 const char kFoo1[] = "http://foo1/"; | |
| 48 const char kFoo2[] = "http://foo2/"; | |
| 49 const char kBar1[] = "http://bar1/"; | |
| 50 const char kBar2[] = "http://bar2/"; | |
| 51 const char kBaz1[] = "http://baz1/"; | |
| 52 const char kBaz2[] = "http://baz2/"; | |
| 53 const char kTag1[] = "tag1"; | |
| 54 const char kTag2[] = "tag2"; | |
| 55 const int kTabIds1[] = {5, 10, 13, 17}; | |
| 56 const int kTabIds2[] = {7, 15, 18, 20}; | |
| 57 | |
| 58 const base::Time kTime0 = base::Time::FromInternalValue(100); | |
|
skym
2017/02/22 22:25:15
Is there a reason this isn't 0,1,2,3,... ? Just to
Nicolas Zea
2017/02/22 22:58:40
Exactly.
| |
| 59 const base::Time kTime1 = base::Time::FromInternalValue(110); | |
| 60 const base::Time kTime2 = base::Time::FromInternalValue(120); | |
| 61 const base::Time kTime3 = base::Time::FromInternalValue(130); | |
| 62 const base::Time kTime4 = base::Time::FromInternalValue(140); | |
| 63 const base::Time kTime5 = base::Time::FromInternalValue(150); | |
| 64 const base::Time kTime6 = base::Time::FromInternalValue(160); | |
| 65 const base::Time kTime7 = base::Time::FromInternalValue(170); | |
| 66 const base::Time kTime8 = base::Time::FromInternalValue(180); | |
| 67 const base::Time kTime9 = base::Time::FromInternalValue(190); | |
| 68 | |
| 69 std::string TabNodeIdToTag(const std::string& machine_tag, int tab_node_id) { | |
| 70 return base::StringPrintf("%s %d", machine_tag.c_str(), tab_node_id); | |
| 71 } | |
| 72 | |
| 73 size_t CountIfTagMatches(const SyncChangeList& changes, | |
| 74 const std::string& tag) { | |
| 75 return std::count_if( | |
| 76 changes.begin(), changes.end(), [&tag](const SyncChange& change) { | |
| 77 return change.sync_data().GetSpecifics().session().session_tag() == tag; | |
| 78 }); | |
| 79 } | |
| 80 | |
| 81 size_t CountIfTagMatches(const std::vector<const SyncedSession*>& sessions, | |
| 82 const std::string& tag) { | |
| 83 return std::count_if(sessions.begin(), sessions.end(), | |
| 84 [&tag](const SyncedSession* session) { | |
| 85 return session->session_tag == tag; | |
| 86 }); | |
| 87 } | |
| 88 | |
| 89 testing::AssertionResult AllOfChangesAreType( | |
| 90 const SyncChangeList& changes, | |
| 91 const SyncChange::SyncChangeType type) { | |
| 92 for (const SyncChange& change : changes) { | |
|
skym
2017/02/22 22:25:11
What? No fancy lambda? Could use find_if :)
Nicolas Zea
2017/02/22 22:58:40
Good call!
| |
| 93 if (type != change.change_type()) { | |
| 94 return testing::AssertionFailure() | |
| 95 << change.ToString() << " doesn't match " | |
| 96 << SyncChange::ChangeTypeToString(type); | |
| 97 } | |
| 98 } | |
| 99 return testing::AssertionSuccess(); | |
| 100 } | |
| 101 | |
| 102 testing::AssertionResult ChangeTypeMatches( | |
|
skym
2017/02/22 22:25:11
This method is kind of odd. I don't like that we'r
Nicolas Zea
2017/02/22 22:58:41
But the ordering matters. If we for example do an
skym
2017/02/22 23:17:26
Yes, the order matters for a specific tab. But the
| |
| 103 const SyncChangeList& changes, | |
| 104 const std::vector<SyncChange::SyncChangeType>& types) { | |
| 105 auto types_iter = types.begin(); | |
| 106 if (types.size() != static_cast<size_t>(std::count_if( | |
|
skym
2017/02/22 22:25:11
std::any_if with a negated version of the body wou
Nicolas Zea
2017/02/22 22:58:40
Nice, done.
| |
| 107 changes.begin(), changes.end(), | |
| 108 [&types_iter](const SyncChange& change) { | |
| 109 SCOPED_TRACE(change.ToString()); | |
| 110 return change.change_type() == *types_iter++; | |
|
skym
2017/02/22 22:25:12
Erm, if |types| is shorter than |changes|, bad thi
Nicolas Zea
2017/02/22 22:58:40
Fixed by adding an extra size condition (coupled w
| |
| 111 }))) { | |
| 112 std::string type_string; | |
| 113 std::for_each(types.begin(), types.end(), | |
| 114 [&type_string](const SyncChange::SyncChangeType& type) { | |
| 115 (type_string) += SyncChange::ChangeTypeToString(type) + " "; | |
| 116 }); | |
| 117 std::string change_string; | |
| 118 std::for_each(changes.begin(), changes.end(), | |
| 119 [&change_string](const SyncChange& change) { | |
| 120 change_string += change.ToString(); | |
| 121 }); | |
| 122 return testing::AssertionFailure() | |
| 123 << "Change type mismatch: " << type_string << " vs " | |
| 124 << change_string; | |
| 125 } | |
| 126 return testing::AssertionSuccess(); | |
| 127 } | |
| 128 | |
| 129 // Creates a field trial with the specified |trial_name| and |group_name| and | |
| 130 // registers an associated |variation_id| for it for the given |service|. | |
| 131 void CreateAndActivateFieldTrial(const std::string& trial_name, | |
| 132 const std::string& group_name, | |
| 133 variations::VariationID variation_id, | |
| 134 variations::IDCollectionKey service) { | |
| 135 base::FieldTrialList::CreateFieldTrial(trial_name, group_name); | |
| 136 variations::AssociateGoogleVariationID(service, trial_name, group_name, | |
| 137 variation_id); | |
| 138 // Access the trial to activate it. | |
| 139 base::FieldTrialList::FindFullName(trial_name); | |
| 140 } | |
| 141 | |
| 51 class SessionNotificationObserver { | 142 class SessionNotificationObserver { |
| 52 public: | 143 public: |
| 53 SessionNotificationObserver() | 144 SessionNotificationObserver() |
| 54 : notified_of_update_(false), notified_of_refresh_(false) {} | 145 : notified_of_update_(false), notified_of_refresh_(false) {} |
| 55 void NotifyOfUpdate() { notified_of_update_ = true; } | 146 void NotifyOfUpdate() { notified_of_update_ = true; } |
| 56 void NotifyOfRefresh() { notified_of_refresh_ = true; } | 147 void NotifyOfRefresh() { notified_of_refresh_ = true; } |
| 57 | 148 |
| 58 bool notified_of_update() const { return notified_of_update_; } | 149 bool notified_of_update() const { return notified_of_update_; } |
| 59 bool notified_of_refresh() const { return notified_of_refresh_; } | 150 bool notified_of_refresh() const { return notified_of_refresh_; } |
| 60 | 151 |
| 61 void Reset() { | 152 void Reset() { |
| 62 notified_of_update_ = false; | 153 notified_of_update_ = false; |
| 63 notified_of_refresh_ = false; | 154 notified_of_refresh_ = false; |
| 64 } | 155 } |
| 65 | 156 |
| 66 private: | 157 private: |
| 67 bool notified_of_update_; | 158 bool notified_of_update_; |
| 68 bool notified_of_refresh_; | 159 bool notified_of_refresh_; |
| 69 }; | 160 }; |
| 70 | 161 |
| 71 class SyncedWindowDelegateOverride : public SyncedWindowDelegate { | 162 // A SyncedTabDelegate fake for testing. It simulates a normal |
| 72 public: | 163 // SyncedTabDelegate with a proper WebContents. For a SyncedTabDelegate without |
| 73 explicit SyncedWindowDelegateOverride(const SyncedWindowDelegate* wrapped) | 164 // a WebContents, see PlaceholderTabDelegate below. |
| 74 : wrapped_(wrapped) { | 165 class TestSyncedTabDelegate : public SyncedTabDelegate { |
| 75 } | 166 public: |
| 76 ~SyncedWindowDelegateOverride() override {} | 167 TestSyncedTabDelegate() {} |
| 77 | 168 ~TestSyncedTabDelegate() override {} |
| 78 bool HasWindow() const override { return wrapped_->HasWindow(); } | 169 |
| 79 | 170 // SyncedTabDelegate overrides. |
| 80 SessionID::id_type GetSessionId() const override { | 171 bool IsInitialBlankNavigation() const override { |
| 81 return session_id_override_ >= 0 ? session_id_override_ | 172 // This differs from NavigationControllerImpl, which has an initial blank |
| 82 : wrapped_->GetSessionId(); | 173 // NavigationEntry. |
| 83 } | 174 return GetEntryCount() == 0; |
| 84 | 175 } |
| 85 int GetTabCount() const override { return wrapped_->GetTabCount(); } | 176 int GetCurrentEntryIndex() const override { return current_entry_index_; } |
| 86 | 177 GURL GetVirtualURLAtIndex(int i) const override { |
| 87 int GetActiveIndex() const override { return wrapped_->GetActiveIndex(); } | 178 if (static_cast<size_t>(i) >= entries_.size()) |
| 88 | 179 return GURL(); |
| 89 bool IsApp() const override { return wrapped_->IsApp(); } | 180 return entries_[i]->virtual_url(); |
| 90 | 181 } |
| 91 bool IsTypeTabbed() const override { return wrapped_->IsTypeTabbed(); } | 182 GURL GetFaviconURLAtIndex(int i) const override { return GURL(); } |
| 92 | 183 ui::PageTransition GetTransitionAtIndex(int i) const override { |
| 93 bool IsTypePopup() const override { return wrapped_->IsTypePopup(); } | 184 if (static_cast<size_t>(i) >= entries_.size()) |
| 185 return ui::PAGE_TRANSITION_LINK; | |
| 186 return entries_[i]->transition_type(); | |
| 187 } | |
| 188 void GetSerializedNavigationAtIndex( | |
| 189 int i, | |
| 190 sessions::SerializedNavigationEntry* serialized_entry) const override { | |
| 191 if (static_cast<size_t>(i) >= entries_.size()) | |
| 192 return; | |
| 193 *serialized_entry = *entries_[i]; | |
| 194 } | |
| 195 int GetEntryCount() const override { return entries_.size(); } | |
| 196 SessionID::id_type GetWindowId() const override { return window_id_.id(); } | |
| 197 SessionID::id_type GetSessionId() const override { return tab_id_.id(); } | |
| 198 bool IsBeingDestroyed() const override { return false; } | |
| 199 std::string GetExtensionAppId() const override { return std::string(); } | |
| 200 bool ProfileIsSupervised() const override { return is_supervised_; } | |
| 201 void set_is_supervised(bool is_supervised) { is_supervised_ = is_supervised; } | |
| 202 const std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>>* | |
| 203 GetBlockedNavigations() const override { | |
| 204 return &blocked_navigations_; | |
| 205 } | |
| 206 bool IsPlaceholderTab() const override { return false; } | |
| 207 int GetSyncId() const override { return sync_id_; } | |
| 208 void SetSyncId(int sync_id) override { sync_id_ = sync_id; } | |
| 209 bool ShouldSync(SyncSessionsClient* sessions_client) override { | |
| 210 // This is just a simple filter that isn't meant to fully reproduce | |
| 211 // the TabContentsTabDelegate's ShouldSync logic. | |
| 212 // Verify all URL's are valid (which will ignore an initial blank page) and | |
| 213 // that there is at least one http:// url. | |
| 214 int http_count = 0; | |
| 215 for (auto& entry : entries_) { | |
| 216 if (!entry->virtual_url().is_valid()) | |
| 217 return false; | |
| 218 if (entry->virtual_url().SchemeIsHTTPOrHTTPS()) | |
| 219 http_count++; | |
| 220 } | |
| 221 return http_count > 0; | |
| 222 } | |
| 223 | |
| 224 void AppendEntry(std::unique_ptr<sessions::SerializedNavigationEntry> entry) { | |
| 225 entries_.push_back(std::move(entry)); | |
| 226 } | |
| 227 | |
| 228 void set_current_entry_index(int i) { current_entry_index_ = i; } | |
| 229 | |
| 230 void SetWindowId(SessionID::id_type window_id) { | |
| 231 window_id_.set_id(window_id); | |
| 232 } | |
| 233 | |
| 234 void SetSessionId(SessionID::id_type id) { tab_id_.set_id(id); } | |
| 235 | |
| 236 void set_blocked_navigations( | |
| 237 const std::vector<std::unique_ptr<sessions::SerializedNavigationEntry>>& | |
| 238 navs) { | |
| 239 for (auto& entry : navs) { | |
| 240 blocked_navigations_.push_back( | |
| 241 base::MakeUnique<sessions::SerializedNavigationEntry>(*entry)); | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 void reset() { | |
| 246 current_entry_index_ = 0; | |
| 247 sync_id_ = TabNodePool::kInvalidTabNodeID; | |
| 248 entries_.clear(); | |
| 249 } | |
| 250 | |
| 251 private: | |
| 252 int current_entry_index_ = -1; | |
| 253 bool is_supervised_ = false; | |
| 254 int sync_id_ = -1; | |
| 255 SessionID tab_id_; | |
| 256 SessionID window_id_; | |
| 257 std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>> | |
| 258 blocked_navigations_; | |
| 259 std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>> | |
| 260 entries_; | |
| 261 }; | |
| 262 | |
| 263 // A placeholder delegate. These delegates have no WebContents, simulating a tab | |
| 264 // that has been restored without bringing its state fully into memory (for | |
| 265 // example on Android), or where the tab's contents have been evicted from | |
| 266 // memory. See SyncedTabDelegate::IsPlaceHolderTab for more info. | |
| 267 class PlaceholderTabDelegate : public SyncedTabDelegate { | |
| 268 public: | |
| 269 PlaceholderTabDelegate(SessionID::id_type session_id, int sync_id) | |
| 270 : session_id_(session_id), sync_id_(sync_id) {} | |
| 271 ~PlaceholderTabDelegate() override {} | |
| 272 | |
| 273 // SyncedTabDelegate overrides. | |
| 274 SessionID::id_type GetSessionId() const override { return session_id_; } | |
| 275 int GetSyncId() const override { return sync_id_; } | |
| 276 void SetSyncId(int sync_id) override { sync_id_ = sync_id; } | |
| 277 bool IsPlaceholderTab() const override { return true; } | |
| 278 | |
| 279 // Everything else is invalid to invoke as it depends on a valid WebContents. | |
| 280 SessionID::id_type GetWindowId() const override { | |
| 281 NOTREACHED(); | |
| 282 return 0; | |
| 283 } | |
| 284 bool IsBeingDestroyed() const override { | |
| 285 NOTREACHED(); | |
| 286 return false; | |
| 287 } | |
| 288 std::string GetExtensionAppId() const override { | |
| 289 NOTREACHED(); | |
| 290 return ""; | |
| 291 } | |
| 292 bool IsInitialBlankNavigation() const override { | |
| 293 NOTREACHED(); | |
| 294 return false; | |
| 295 } | |
| 296 int GetCurrentEntryIndex() const override { | |
| 297 NOTREACHED(); | |
| 298 return 0; | |
| 299 } | |
| 300 int GetEntryCount() const override { | |
| 301 NOTREACHED(); | |
| 302 return 0; | |
| 303 } | |
| 304 GURL GetVirtualURLAtIndex(int i) const override { | |
| 305 NOTREACHED(); | |
| 306 return GURL(); | |
| 307 } | |
| 308 GURL GetFaviconURLAtIndex(int i) const override { | |
| 309 NOTREACHED(); | |
| 310 return GURL(); | |
| 311 } | |
| 312 ui::PageTransition GetTransitionAtIndex(int i) const override { | |
| 313 NOTREACHED(); | |
| 314 return ui::PageTransition(); | |
| 315 } | |
| 316 void GetSerializedNavigationAtIndex( | |
| 317 int i, | |
| 318 sessions::SerializedNavigationEntry* serialized_entry) const override { | |
| 319 NOTREACHED(); | |
| 320 } | |
| 321 bool ProfileIsSupervised() const override { | |
| 322 NOTREACHED(); | |
| 323 return false; | |
| 324 } | |
| 325 const std::vector<std::unique_ptr<const sessions::SerializedNavigationEntry>>* | |
| 326 GetBlockedNavigations() const override { | |
| 327 NOTREACHED(); | |
| 328 return nullptr; | |
| 329 } | |
| 330 bool ShouldSync(SyncSessionsClient* sessions_client) override { | |
| 331 NOTREACHED(); | |
| 332 return false; | |
| 333 } | |
| 334 | |
| 335 private: | |
| 336 SessionID::id_type session_id_; | |
| 337 int sync_id_; | |
| 338 }; | |
| 339 | |
| 340 class TestSyncedWindowDelegate : public SyncedWindowDelegate { | |
| 341 public: | |
| 342 TestSyncedWindowDelegate() {} | |
| 343 ~TestSyncedWindowDelegate() override {} | |
| 344 | |
| 345 bool HasWindow() const override { return true; } | |
| 346 | |
| 347 SessionID::id_type GetSessionId() const override { return window_id_.id(); } | |
| 348 | |
| 349 int GetTabCount() const override { return tab_delegates_.size(); } | |
| 350 | |
| 351 int GetActiveIndex() const override { return 0; } | |
| 352 | |
| 353 bool IsApp() const override { return false; } | |
| 354 | |
| 355 bool IsTypeTabbed() const override { | |
| 356 return window_type_ == sync_pb::SessionWindow_BrowserType_TYPE_TABBED; | |
| 357 } | |
| 358 | |
| 359 bool IsTypePopup() const override { | |
| 360 return window_type_ == sync_pb::SessionWindow_BrowserType_TYPE_POPUP; | |
| 361 } | |
| 94 | 362 |
| 95 bool IsTabPinned(const SyncedTabDelegate* tab) const override { | 363 bool IsTabPinned(const SyncedTabDelegate* tab) const override { |
| 96 return wrapped_->IsTabPinned(tab); | 364 return false; |
| 97 } | 365 } |
| 98 | 366 |
| 99 SyncedTabDelegate* GetTabAt(int index) const override { | 367 SyncedTabDelegate* GetTabAt(int index) const override { |
| 100 if (tab_overrides_.find(index) != tab_overrides_.end()) | 368 if (tab_delegates_.find(index) != tab_delegates_.end()) |
| 101 return tab_overrides_.find(index)->second; | 369 return tab_delegates_.find(index)->second; |
| 102 | 370 |
| 103 return wrapped_->GetTabAt(index); | 371 return nullptr; |
| 104 } | 372 } |
| 105 | 373 |
| 106 void OverrideSessionId(SessionID::id_type id) { session_id_override_ = id; } | 374 void OverrideWindowId(SessionID::id_type id) { window_id_.set_id(id); } |
| 107 | 375 |
| 108 void OverrideTabAt(int index, | 376 void OverrideTabAt(int index, SyncedTabDelegate* delegate) { |
| 109 SyncedTabDelegate* delegate, | 377 tab_delegates_[index] = delegate; |
| 110 SessionID::id_type tab_id) { | |
| 111 tab_overrides_[index] = delegate; | |
| 112 tab_id_overrides_[index] = tab_id; | |
| 113 } | 378 } |
| 114 | 379 |
| 115 SessionID::id_type GetTabIdAt(int index) const override { | 380 SessionID::id_type GetTabIdAt(int index) const override { |
| 116 if (tab_id_overrides_.find(index) != tab_id_overrides_.end()) | 381 SyncedTabDelegate* delegate = GetTabAt(index); |
| 117 return tab_id_overrides_.find(index)->second; | 382 if (!delegate) |
| 118 return wrapped_->GetTabIdAt(index); | 383 return TabNodePool::kInvalidTabID; |
| 119 } | 384 return delegate->GetSessionId(); |
| 120 | 385 } |
| 121 bool IsSessionRestoreInProgress() const override { | 386 |
| 122 return wrapped_->IsSessionRestoreInProgress(); | 387 bool IsSessionRestoreInProgress() const override { return false; } |
| 123 } | 388 |
| 124 | 389 bool ShouldSync() const override { return true; } |
| 125 bool ShouldSync() const override { return wrapped_->ShouldSync(); } | |
| 126 | 390 |
| 127 private: | 391 private: |
| 128 std::map<int, SyncedTabDelegate*> tab_overrides_; | 392 std::map<int, SyncedTabDelegate*> tab_delegates_; |
| 129 std::map<int, SessionID::id_type> tab_id_overrides_; | 393 SessionID window_id_; |
| 130 const SyncedWindowDelegate* const wrapped_; | 394 sync_pb::SessionWindow_BrowserType window_type_ = |
| 131 SessionID::id_type session_id_override_ = -1; | 395 sync_pb::SessionWindow_BrowserType_TYPE_TABBED; |
| 132 }; | 396 }; |
| 133 | 397 |
| 134 class TestSyncedWindowDelegatesGetter : public SyncedWindowDelegatesGetter { | 398 class TestSyncedWindowDelegatesGetter : public SyncedWindowDelegatesGetter { |
| 135 public: | 399 public: |
| 136 TestSyncedWindowDelegatesGetter( | 400 TestSyncedWindowDelegatesGetter() {} |
| 137 const std::set<const SyncedWindowDelegate*>& delegates) | 401 ~TestSyncedWindowDelegatesGetter() override {} |
| 138 : delegates_(delegates) {} | |
| 139 | 402 |
| 140 std::set<const SyncedWindowDelegate*> GetSyncedWindowDelegates() override { | 403 std::set<const SyncedWindowDelegate*> GetSyncedWindowDelegates() override { |
| 141 return delegates_; | 404 return delegates_; |
| 142 } | 405 } |
| 143 | 406 |
| 144 const SyncedWindowDelegate* FindById(SessionID::id_type id) override { | 407 const SyncedWindowDelegate* FindById(SessionID::id_type id) override { |
| 145 for (auto* window : delegates_) { | 408 for (auto* window : delegates_) { |
| 146 if (window->GetSessionId() == id) | 409 if (window->GetSessionId() == id) |
| 147 return window; | 410 return window; |
| 148 } | 411 } |
| 149 return nullptr; | 412 return nullptr; |
| 150 } | 413 } |
| 151 | 414 |
| 415 void AddSyncedWindowDelegate(const SyncedWindowDelegate* delegate) { | |
| 416 delegates_.insert(delegate); | |
| 417 } | |
| 418 | |
| 152 private: | 419 private: |
| 153 const std::set<const SyncedWindowDelegate*> delegates_; | 420 std::set<const SyncedWindowDelegate*> delegates_; |
| 154 }; | 421 }; |
| 155 | 422 |
| 156 class TestSyncProcessorStub : public syncer::SyncChangeProcessor { | 423 class TestSyncChangeProcessor : public syncer::SyncChangeProcessor { |
| 157 public: | 424 public: |
| 158 explicit TestSyncProcessorStub(syncer::SyncChangeList* output) | 425 explicit TestSyncChangeProcessor(SyncChangeList* output) : output_(output) {} |
| 159 : output_(output) {} | |
| 160 syncer::SyncError ProcessSyncChanges( | 426 syncer::SyncError ProcessSyncChanges( |
| 161 const tracked_objects::Location& from_here, | 427 const tracked_objects::Location& from_here, |
| 162 const syncer::SyncChangeList& change_list) override { | 428 const SyncChangeList& change_list) override { |
| 163 if (error_.IsSet()) { | 429 if (error_.IsSet()) { |
| 164 syncer::SyncError error = error_; | 430 syncer::SyncError error = error_; |
| 165 error_ = syncer::SyncError(); | 431 error_ = syncer::SyncError(); |
| 166 return error; | 432 return error; |
| 167 } | 433 } |
| 168 | 434 |
| 169 if (output_) | 435 if (output_) |
| 170 output_->insert(output_->end(), change_list.begin(), change_list.end()); | 436 output_->insert(output_->end(), change_list.begin(), change_list.end()); |
| 171 NotifyLocalChangeObservers(); | 437 NotifyLocalChangeObservers(); |
| 172 | 438 |
| 173 return syncer::SyncError(); | 439 return syncer::SyncError(); |
| 174 } | 440 } |
| 175 | 441 |
| 176 syncer::SyncDataList GetAllSyncData(syncer::ModelType type) const override { | 442 SyncDataList GetAllSyncData(syncer::ModelType type) const override { |
| 177 return sync_data_to_return_; | 443 return sync_data_to_return_; |
| 178 } | 444 } |
| 179 | 445 |
| 180 void AddLocalChangeObserver(syncer::LocalChangeObserver* observer) override { | 446 void AddLocalChangeObserver(syncer::LocalChangeObserver* observer) override { |
| 181 local_change_observers_.AddObserver(observer); | 447 local_change_observers_.AddObserver(observer); |
| 182 } | 448 } |
| 183 void RemoveLocalChangeObserver( | 449 void RemoveLocalChangeObserver( |
| 184 syncer::LocalChangeObserver* observer) override { | 450 syncer::LocalChangeObserver* observer) override { |
| 185 local_change_observers_.RemoveObserver(observer); | 451 local_change_observers_.RemoveObserver(observer); |
| 186 } | 452 } |
| 187 | 453 |
| 188 void NotifyLocalChangeObservers() { | 454 void NotifyLocalChangeObservers() { |
| 189 const syncer::SyncChange empty_change; | 455 const SyncChange empty_change; |
| 190 for (syncer::LocalChangeObserver& observer : local_change_observers_) | 456 for (syncer::LocalChangeObserver& observer : local_change_observers_) |
| 191 observer.OnLocalChange(nullptr, empty_change); | 457 observer.OnLocalChange(nullptr, empty_change); |
| 192 } | 458 } |
| 193 | 459 |
| 194 void FailProcessSyncChangesWith(const syncer::SyncError& error) { | 460 void FailProcessSyncChangesWith(const syncer::SyncError& error) { |
| 195 error_ = error; | 461 error_ = error; |
| 196 } | 462 } |
| 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 syncer::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 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( |
| 370 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, "Error", | 582 syncer::SyncError(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, "Error", |
|
skym
2017/02/22 22:25:11
using syncer::SyncError?
Nicolas Zea
2017/02/22 22:58:40
Done.
| |
| 371 syncer::SESSIONS)); | 583 syncer::SESSIONS)); |
| 372 } | 584 } |
| 373 | 585 |
| 374 void SetSyncData(const syncer::SyncDataList& data) { | 586 void SetSyncData(const SyncDataList& data) { |
| 375 test_processor_->SetSyncDataToReturn(data); | 587 test_processor_->SetSyncDataToReturn(data); |
| 376 } | 588 } |
| 377 | 589 |
| 378 syncer::SyncChangeList* FilterOutLocalHeaderChanges( | 590 void VerifyLocalHeaderChange(const SyncChange& change, |
| 379 syncer::SyncChangeList* list) { | 591 int num_windows, |
| 380 syncer::SyncChangeList::iterator it = list->begin(); | 592 int num_tabs) { |
| 593 SCOPED_TRACE(change.ToString()); | |
| 594 SyncDataLocal data(change.sync_data()); | |
| 595 ASSERT_EQ(manager()->current_machine_tag(), data.GetTag()); | |
|
skym
2017/02/22 22:25:18
Lot of asserts, should some of these be expects?
Nicolas Zea
2017/02/22 22:58:40
Done.
| |
| 596 ASSERT_TRUE(data.GetSpecifics().session().has_header()); | |
| 597 ASSERT_FALSE(data.GetSpecifics().session().has_tab()); | |
| 598 ASSERT_TRUE(data.GetSpecifics().session().header().has_device_type()); | |
| 599 ASSERT_EQ(GetLocalDeviceInfo()->client_name(), | |
| 600 data.GetSpecifics().session().header().client_name()); | |
| 601 ASSERT_EQ(num_windows, | |
| 602 data.GetSpecifics().session().header().window_size()); | |
| 603 int tab_count = 0; | |
| 604 for (auto& window : data.GetSpecifics().session().header().window()) { | |
| 605 tab_count += window.tab_size(); | |
| 606 } | |
| 607 ASSERT_EQ(num_tabs, tab_count); | |
| 608 } | |
| 609 | |
| 610 void VerifyLocalTabChange(const SyncChange& change, | |
| 611 int num_navigations, | |
| 612 std::string final_url) { | |
| 613 SCOPED_TRACE(change.ToString()); | |
| 614 SyncDataLocal data(change.sync_data()); | |
| 615 ASSERT_TRUE(base::StartsWith(data.GetTag(), | |
| 616 manager()->current_machine_tag(), | |
| 617 base::CompareCase::SENSITIVE)); | |
| 618 ASSERT_FALSE(data.GetSpecifics().session().has_header()); | |
| 619 ASSERT_TRUE(data.GetSpecifics().session().has_tab()); | |
| 620 ASSERT_EQ(num_navigations, | |
| 621 data.GetSpecifics().session().tab().navigation_size()); | |
| 622 ASSERT_EQ(final_url, data.GetSpecifics() | |
| 623 .session() | |
| 624 .tab() | |
| 625 .navigation(num_navigations - 1) | |
| 626 .virtual_url()); | |
| 627 } | |
| 628 | |
| 629 SyncChangeList* FilterOutLocalHeaderChanges(SyncChangeList* list) { | |
| 630 SyncChangeList::iterator it = list->begin(); | |
| 381 bool found = false; | 631 bool found = false; |
| 382 while (it != list->end()) { | 632 while (it != list->end()) { |
| 383 if (it->sync_data().IsLocal() && | 633 if (it->sync_data().IsLocal() && |
| 384 syncer::SyncDataLocal(it->sync_data()).GetTag() == | 634 SyncDataLocal(it->sync_data()).GetTag() == |
| 385 manager_->current_machine_tag()) { | 635 manager_->current_machine_tag()) { |
| 386 EXPECT_TRUE(SyncChange::ACTION_ADD == it->change_type() || | 636 EXPECT_TRUE(SyncChange::ACTION_ADD == it->change_type() || |
| 387 SyncChange::ACTION_UPDATE == it->change_type()); | 637 SyncChange::ACTION_UPDATE == it->change_type()); |
| 388 it = list->erase(it); | 638 it = list->erase(it); |
| 389 found = true; | 639 found = true; |
| 390 } else { | 640 } else { |
| 391 ++it; | 641 ++it; |
| 392 } | 642 } |
| 393 } | 643 } |
| 394 EXPECT_TRUE(found); | 644 EXPECT_TRUE(found); |
| 395 return list; | 645 return list; |
| 396 } | 646 } |
| 397 | 647 |
| 398 SyncSessionsClient* GetSyncSessionsClient() { | 648 SyncChange MakeRemoteChange(const sync_pb::SessionSpecifics& specifics, |
| 399 return sessions_client_shim_.get(); | 649 SyncChange::SyncChangeType type) const { |
| 400 } | 650 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 } | 651 } |
| 418 | 652 |
| 419 void AddTabsToChangeList(const std::vector<sync_pb::SessionSpecifics>& batch, | 653 void AddTabsToChangeList(const std::vector<sync_pb::SessionSpecifics>& batch, |
| 420 SyncChange::SyncChangeType type, | 654 SyncChange::SyncChangeType type, |
| 421 syncer::SyncChangeList* change_list) const { | 655 SyncChangeList* change_list) const { |
| 422 for (const auto& specifics : batch) { | 656 for (const auto& specifics : batch) { |
| 423 change_list->push_back( | 657 change_list->push_back( |
| 424 syncer::SyncChange(FROM_HERE, type, CreateRemoteData(specifics))); | 658 SyncChange(FROM_HERE, type, CreateRemoteData(specifics))); |
| 425 } | 659 } |
| 426 } | 660 } |
| 427 | 661 |
| 428 void AddToSyncDataList(const sync_pb::SessionSpecifics& specifics, | 662 void AddToSyncDataList(const sync_pb::SessionSpecifics& specifics, |
| 429 syncer::SyncDataList* list, | 663 SyncDataList* list, |
| 430 base::Time mtime) const { | 664 base::Time mtime) const { |
| 431 list->push_back(CreateRemoteData(specifics, mtime)); | 665 list->push_back(CreateRemoteData(specifics, mtime)); |
| 432 } | 666 } |
| 433 | 667 |
| 434 void AddTabsToSyncDataList(const std::vector<sync_pb::SessionSpecifics>& tabs, | 668 void AddTabsToSyncDataList(const std::vector<sync_pb::SessionSpecifics>& tabs, |
| 435 syncer::SyncDataList* list) const { | 669 SyncDataList* list) const { |
| 436 for (size_t i = 0; i < tabs.size(); i++) { | 670 for (size_t i = 0; i < tabs.size(); ++i) { |
| 437 AddToSyncDataList(tabs[i], list, base::Time::FromInternalValue(i + 1)); | 671 AddToSyncDataList(tabs[i], list, base::Time::FromInternalValue(i + 1)); |
| 438 } | 672 } |
| 439 } | 673 } |
| 440 | 674 |
| 441 syncer::SyncData CreateRemoteData(const sync_pb::SessionSpecifics& specifics, | 675 SyncData CreateRemoteData(const sync_pb::SessionSpecifics& specifics, |
| 442 base::Time mtime = base::Time()) const { | 676 base::Time mtime = base::Time()) const { |
| 443 sync_pb::EntitySpecifics entity; | 677 sync_pb::EntitySpecifics entity; |
| 444 entity.mutable_session()->CopyFrom(specifics); | 678 entity.mutable_session()->CopyFrom(specifics); |
| 445 return CreateRemoteData(entity, mtime); | 679 return CreateRemoteData(entity, mtime); |
| 446 } | 680 } |
| 447 | 681 |
| 448 syncer::SyncData CreateRemoteData(const sync_pb::EntitySpecifics& entity, | 682 SyncData CreateRemoteData(const sync_pb::EntitySpecifics& entity, |
| 449 base::Time mtime = base::Time()) const { | 683 base::Time mtime = base::Time()) const { |
| 450 // The server ID is never relevant to these tests, so just use 1. | 684 // The server ID is never relevant to these tests, so just use 1. |
| 451 return SyncData::CreateRemoteData( | 685 return SyncData::CreateRemoteData( |
| 452 1, entity, mtime, syncer::AttachmentIdList(), | 686 1, entity, mtime, syncer::AttachmentIdList(), |
| 453 syncer::AttachmentServiceProxyForTest::Create(), | 687 syncer::AttachmentServiceProxyForTest::Create(), |
| 454 SessionsSyncManager::TagHashFromSpecifics(entity.session())); | 688 SessionsSyncManager::TagHashFromSpecifics(entity.session())); |
| 455 } | 689 } |
| 456 | 690 |
| 691 TestSyncedWindowDelegate* GetWindowAtIndex(size_t index) { | |
| 692 if (index >= windows_.size()) | |
| 693 return nullptr; | |
| 694 return windows_[index].get(); | |
| 695 } | |
| 696 | |
| 697 // Creates a new tab within the window specified by |window_id|, and points it | |
| 698 // at |url|. Returns the newly created TestSyncedTabDelegate (not owned). | |
| 699 TestSyncedTabDelegate* AddTab(SessionID::id_type window_id, | |
| 700 const std::string& url, | |
| 701 base::Time time) { | |
| 702 tabs_.push_back(base::MakeUnique<TestSyncedTabDelegate>()); | |
| 703 for (auto& window : windows_) { | |
| 704 if (window->GetSessionId() == window_id) { | |
| 705 int tab_index = window->GetTabCount(); | |
| 706 window->OverrideTabAt(tab_index, tabs_.back().get()); | |
| 707 } | |
| 708 } | |
| 709 | |
| 710 // Simulate the browser firing a tab parented notification, ahead of the | |
| 711 // actual navigation. | |
| 712 router_->NotifyNav(tabs_.back().get()); | |
| 713 | |
| 714 // Now do the actual navigation. | |
| 715 NavigateTab(tabs_.back().get(), url, time); | |
| 716 return tabs_.back().get(); | |
| 717 } | |
| 718 TestSyncedTabDelegate* AddTab(SessionID::id_type window_id, | |
| 719 const std::string& url) { | |
| 720 return AddTab(window_id, url, base::Time()); | |
| 721 } | |
| 722 | |
| 723 void NavigateTab(TestSyncedTabDelegate* delegate, | |
| 724 const std::string& url, | |
| 725 base::Time time) { | |
| 726 std::unique_ptr<sessions::SerializedNavigationEntry> entry( | |
|
skym
2017/02/22 22:25:13
What do you think of
auto entry = base::MakeUniqu
Nicolas Zea
2017/02/22 22:58:40
Done.
| |
| 727 new sessions::SerializedNavigationEntry( | |
| 728 SerializedNavigationEntryTestHelper::CreateNavigation(url, | |
| 729 kTitle))); | |
| 730 SerializedNavigationEntryTestHelper::SetTimestamp(time, entry.get()); | |
| 731 delegate->AppendEntry(std::move(entry)); | |
| 732 delegate->set_current_entry_index(delegate->GetCurrentEntryIndex() + 1); | |
| 733 router_->NotifyNav(delegate); | |
| 734 } | |
| 735 void NavigateTab(TestSyncedTabDelegate* delegate, const std::string& url) { | |
| 736 NavigateTab(delegate, url, base::Time()); | |
| 737 } | |
| 738 | |
| 739 TestSyncedWindowDelegate* AddWindow() { | |
| 740 windows_.push_back(base::MakeUnique<TestSyncedWindowDelegate>()); | |
| 741 window_getter_.AddSyncedWindowDelegate(windows_.back().get()); | |
| 742 return windows_.back().get(); | |
| 743 } | |
| 744 | |
| 457 private: | 745 private: |
| 458 std::unique_ptr<browser_sync::ChromeSyncClient> sync_client_; | 746 std::unique_ptr<syncer::FakeSyncClient> sync_client_; |
| 459 std::unique_ptr<SyncSessionsClientShim> sessions_client_shim_; | 747 std::unique_ptr<SyncSessionsClientShim> sessions_client_shim_; |
| 460 std::unique_ptr<syncer::SyncPrefs> sync_prefs_; | 748 std::unique_ptr<syncer::SyncPrefs> sync_prefs_; |
| 461 SessionNotificationObserver observer_; | 749 SessionNotificationObserver observer_; |
| 750 DummyRouter* router_; | |
| 462 std::unique_ptr<SessionsSyncManager> manager_; | 751 std::unique_ptr<SessionsSyncManager> manager_; |
| 463 SessionSyncTestHelper helper_; | 752 SessionSyncTestHelper helper_; |
| 464 TestSyncProcessorStub* test_processor_; | 753 TestSyncChangeProcessor* test_processor_; |
| 754 TestSyncedWindowDelegatesGetter window_getter_; | |
| 755 std::vector<std::unique_ptr<TestSyncedWindowDelegate>> windows_; | |
| 756 std::vector<std::unique_ptr<TestSyncedTabDelegate>> tabs_; | |
| 465 std::unique_ptr<LocalDeviceInfoProviderMock> local_device_; | 757 std::unique_ptr<LocalDeviceInfoProviderMock> local_device_; |
| 466 }; | 758 }; |
| 467 | 759 |
| 468 // Test that the SyncSessionManager can properly fill in a SessionHeader. | 760 // Test that the SyncSessionManager can properly fill in a SessionHeader. |
| 469 TEST_F(SessionsSyncManagerTest, PopulateSessionHeader) { | 761 TEST_F(SessionsSyncManagerTest, PopulateSessionHeader) { |
| 470 sync_pb::SessionHeader header_s; | 762 sync_pb::SessionHeader header_s; |
| 471 header_s.set_client_name("Client 1"); | 763 header_s.set_client_name("Client 1"); |
| 472 header_s.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_WIN); | 764 header_s.set_device_type(sync_pb::SyncEnums_DeviceType_TYPE_WIN); |
| 473 | 765 |
| 474 SyncedSession session; | 766 SyncedSession session; |
| 475 base::Time time = base::Time::Now(); | 767 base::Time time = base::Time::Now(); |
| 476 SessionsSyncManager::PopulateSessionHeaderFromSpecifics( | 768 SessionsSyncManager::PopulateSessionHeaderFromSpecifics(header_s, time, |
| 477 header_s, time, &session); | 769 &session); |
| 478 ASSERT_EQ("Client 1", session.session_name); | 770 ASSERT_EQ("Client 1", session.session_name); |
| 479 ASSERT_EQ(SyncedSession::TYPE_WIN, session.device_type); | 771 ASSERT_EQ(SyncedSession::TYPE_WIN, session.device_type); |
| 480 ASSERT_EQ(time, session.modified_time); | 772 ASSERT_EQ(time, session.modified_time); |
| 481 } | 773 } |
| 482 | 774 |
| 483 // Test translation between protobuf types and chrome session types. | 775 // Test translation between protobuf types and chrome session types. |
| 484 TEST_F(SessionsSyncManagerTest, PopulateSessionWindow) { | 776 TEST_F(SessionsSyncManagerTest, PopulateSessionWindow) { |
| 485 sync_pb::SessionWindow window_s; | 777 sync_pb::SessionWindow window_s; |
| 486 window_s.add_tab(0); | 778 window_s.add_tab(0); |
| 487 window_s.set_browser_type(sync_pb::SessionWindow_BrowserType_TYPE_TABBED); | 779 window_s.set_browser_type(sync_pb::SessionWindow_BrowserType_TYPE_TABBED); |
| 488 window_s.set_selected_tab_index(1); | 780 window_s.set_selected_tab_index(1); |
| 489 | 781 |
| 490 std::string tag = "tag"; | 782 SyncedSession* session = manager()->session_tracker_.GetSession(kTag1); |
| 491 SyncedSession* session = manager()->session_tracker_.GetSession(tag); | 783 manager()->session_tracker_.PutWindowInSession(kTag1, 0); |
| 492 manager()->session_tracker_.PutWindowInSession(tag, 0); | 784 manager()->BuildSyncedSessionFromSpecifics(kTag1, window_s, base::Time(), |
| 493 manager()->BuildSyncedSessionFromSpecifics(tag, window_s, base::Time(), | |
| 494 session->windows[0].get()); | 785 session->windows[0].get()); |
| 495 ASSERT_EQ(1U, session->windows[0]->tabs.size()); | 786 ASSERT_EQ(1U, session->windows[0]->tabs.size()); |
| 496 ASSERT_EQ(1, session->windows[0]->selected_tab_index); | 787 ASSERT_EQ(1, session->windows[0]->selected_tab_index); |
| 497 ASSERT_EQ(sessions::SessionWindow::TYPE_TABBED, session->windows[0]->type); | 788 ASSERT_EQ(sessions::SessionWindow::TYPE_TABBED, session->windows[0]->type); |
| 498 ASSERT_EQ(1U, manager()->session_tracker_.num_synced_sessions()); | 789 ASSERT_EQ(1U, manager()->session_tracker_.num_synced_sessions()); |
| 499 ASSERT_EQ(1U, | 790 ASSERT_EQ(1U, manager()->session_tracker_.num_synced_tabs(kTag1)); |
| 500 manager()->session_tracker_.num_synced_tabs(std::string("tag"))); | |
| 501 } | 791 } |
| 502 | 792 |
| 503 namespace { | 793 // 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 | 794 // entries and make sure that setting a SessionTab from it preserves |
| 617 // those entries (and clobbers any existing data). | 795 // those entries (and clobbers any existing data). |
| 618 TEST_F(SessionsSyncManagerTest, SetSessionTabFromDelegate) { | 796 TEST_F(SessionsSyncManagerTest, SetSessionTabFromDelegate) { |
| 619 // Create a tab with three valid entries. | 797 // Create a tab with three valid entries. |
| 620 SyncedTabDelegateFake tab; | 798 TestSyncedTabDelegate* tab = |
| 621 std::unique_ptr<content::NavigationEntry> entry1( | 799 AddTab(AddWindow()->GetSessionId(), kFoo1, kTime1); |
| 622 content::NavigationEntry::Create()); | 800 NavigateTab(tab, kBar1, kTime2); |
| 623 GURL url1("http://www.google.com/"); | 801 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 | 802 |
| 645 sessions::SessionTab session_tab; | 803 sessions::SessionTab session_tab; |
| 646 session_tab.window_id.set_id(1); | 804 session_tab.window_id.set_id(1); |
| 647 session_tab.tab_id.set_id(1); | 805 session_tab.tab_id.set_id(1); |
| 648 session_tab.tab_visual_index = 1; | 806 session_tab.tab_visual_index = 1; |
| 649 session_tab.current_navigation_index = 1; | 807 session_tab.current_navigation_index = 1; |
| 650 session_tab.pinned = true; | 808 session_tab.pinned = true; |
| 651 session_tab.extension_app_id = "app id"; | 809 session_tab.extension_app_id = "app id"; |
| 652 session_tab.user_agent_override = "override"; | 810 session_tab.user_agent_override = "override"; |
| 653 session_tab.timestamp = kTime5; | 811 session_tab.timestamp = kTime5; |
| 654 session_tab.navigations.push_back( | 812 session_tab.navigations.push_back( |
| 655 SerializedNavigationEntryTestHelper::CreateNavigation( | 813 SerializedNavigationEntryTestHelper::CreateNavigation( |
| 656 "http://www.example.com", "Example")); | 814 "http://www.example.com", "Example")); |
| 657 session_tab.session_storage_persistent_id = "persistent id"; | 815 session_tab.session_storage_persistent_id = "persistent id"; |
| 658 manager()->SetSessionTabFromDelegate(tab, kTime4, &session_tab); | 816 manager()->SetSessionTabFromDelegate(*tab, kTime4, &session_tab); |
| 659 | 817 |
| 660 EXPECT_EQ(0, session_tab.window_id.id()); | 818 EXPECT_EQ(tab->GetWindowId(), session_tab.window_id.id()); |
| 661 EXPECT_EQ(0, session_tab.tab_id.id()); | 819 EXPECT_EQ(tab->GetSessionId(), session_tab.tab_id.id()); |
| 662 EXPECT_EQ(0, session_tab.tab_visual_index); | 820 EXPECT_EQ(0, session_tab.tab_visual_index); |
| 663 EXPECT_EQ(2, session_tab.current_navigation_index); | 821 EXPECT_EQ(tab->GetCurrentEntryIndex(), session_tab.current_navigation_index); |
| 664 EXPECT_FALSE(session_tab.pinned); | 822 EXPECT_FALSE(session_tab.pinned); |
| 665 EXPECT_TRUE(session_tab.extension_app_id.empty()); | 823 EXPECT_TRUE(session_tab.extension_app_id.empty()); |
| 666 EXPECT_TRUE(session_tab.user_agent_override.empty()); | 824 EXPECT_TRUE(session_tab.user_agent_override.empty()); |
| 667 EXPECT_EQ(kTime4, session_tab.timestamp); | 825 EXPECT_EQ(kTime4, session_tab.timestamp); |
| 668 ASSERT_EQ(3u, session_tab.navigations.size()); | 826 ASSERT_EQ(3u, session_tab.navigations.size()); |
| 669 EXPECT_EQ(url1, session_tab.navigations[0].virtual_url()); | 827 EXPECT_EQ(GURL(kFoo1), session_tab.navigations[0].virtual_url()); |
| 670 EXPECT_EQ(url2, session_tab.navigations[1].virtual_url()); | 828 EXPECT_EQ(GURL(kBar1), session_tab.navigations[1].virtual_url()); |
| 671 EXPECT_EQ(url3, session_tab.navigations[2].virtual_url()); | 829 EXPECT_EQ(GURL(kBaz1), session_tab.navigations[2].virtual_url()); |
| 672 EXPECT_EQ(kTime1, session_tab.navigations[0].timestamp()); | 830 EXPECT_EQ(kTime1, session_tab.navigations[0].timestamp()); |
| 673 EXPECT_EQ(kTime2, session_tab.navigations[1].timestamp()); | 831 EXPECT_EQ(kTime2, session_tab.navigations[1].timestamp()); |
| 674 EXPECT_EQ(kTime3, session_tab.navigations[2].timestamp()); | 832 EXPECT_EQ(kTime3, session_tab.navigations[2].timestamp()); |
| 675 EXPECT_EQ(200, session_tab.navigations[0].http_status_code()); | 833 EXPECT_EQ(200, session_tab.navigations[0].http_status_code()); |
| 676 EXPECT_EQ(201, session_tab.navigations[1].http_status_code()); | 834 EXPECT_EQ(200, session_tab.navigations[1].http_status_code()); |
| 677 EXPECT_EQ(202, session_tab.navigations[2].http_status_code()); | 835 EXPECT_EQ(200, session_tab.navigations[2].http_status_code()); |
| 678 EXPECT_EQ(SerializedNavigationEntry::STATE_INVALID, | 836 EXPECT_EQ(SerializedNavigationEntry::STATE_INVALID, |
| 679 session_tab.navigations[0].blocked_state()); | 837 session_tab.navigations[0].blocked_state()); |
| 680 EXPECT_EQ(SerializedNavigationEntry::STATE_INVALID, | 838 EXPECT_EQ(SerializedNavigationEntry::STATE_INVALID, |
| 681 session_tab.navigations[1].blocked_state()); | 839 session_tab.navigations[1].blocked_state()); |
| 682 EXPECT_EQ(SerializedNavigationEntry::STATE_INVALID, | 840 EXPECT_EQ(SerializedNavigationEntry::STATE_INVALID, |
| 683 session_tab.navigations[2].blocked_state()); | 841 session_tab.navigations[2].blocked_state()); |
| 684 EXPECT_TRUE(session_tab.session_storage_persistent_id.empty()); | 842 EXPECT_TRUE(session_tab.session_storage_persistent_id.empty()); |
| 685 } | 843 } |
| 686 | 844 |
| 687 // Ensure the current_navigation_index gets set properly when the navigation | 845 // Ensure the current_navigation_index gets set properly when the navigation |
| 688 // stack gets trucated to +/- 6 entries. | 846 // stack gets trucated to +/- 6 entries. |
| 689 TEST_F(SessionsSyncManagerTest, SetSessionTabFromDelegateNavigationIndex) { | 847 TEST_F(SessionsSyncManagerTest, SetSessionTabFromDelegateNavigationIndex) { |
| 690 SyncedTabDelegateFake tab; | 848 TestSyncedTabDelegate* tab = AddTab(AddWindow()->GetSessionId(), kFoo1); |
| 691 std::unique_ptr<content::NavigationEntry> entry0( | 849 const int kNavs = 10; |
|
skym
2017/02/22 22:25:14
This naming seems odd to me. I always thought the
Nicolas Zea
2017/02/22 22:58:40
Acknowledged.
| |
| 692 content::NavigationEntry::Create()); | 850 for (int i = 1; i < kNavs; ++i) { |
| 693 GURL url0("http://www.google.com/"); | 851 NavigateTab(tab, base::StringPrintf("http://foo%i", i)); |
| 694 entry0->SetVirtualURL(url0); | 852 } |
| 695 entry0->SetTimestamp(kTime0); | 853 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 | 854 |
| 764 sessions::SessionTab session_tab; | 855 sessions::SessionTab session_tab; |
| 765 manager()->SetSessionTabFromDelegate(tab, kTime9, &session_tab); | 856 manager()->SetSessionTabFromDelegate(*tab, kTime9, &session_tab); |
| 766 | 857 |
| 767 EXPECT_EQ(6, session_tab.current_navigation_index); | 858 EXPECT_EQ(6, session_tab.current_navigation_index); |
| 768 ASSERT_EQ(8u, session_tab.navigations.size()); | 859 ASSERT_EQ(8u, session_tab.navigations.size()); |
| 769 EXPECT_EQ(url2, session_tab.navigations[0].virtual_url()); | 860 EXPECT_EQ(GURL("http://foo2"), session_tab.navigations[0].virtual_url()); |
| 770 EXPECT_EQ(url3, session_tab.navigations[1].virtual_url()); | 861 EXPECT_EQ(GURL("http://foo3"), session_tab.navigations[1].virtual_url()); |
| 771 EXPECT_EQ(url4, session_tab.navigations[2].virtual_url()); | 862 EXPECT_EQ(GURL("http://foo4"), session_tab.navigations[2].virtual_url()); |
| 772 } | 863 } |
| 773 | 864 |
| 774 // Ensure the current_navigation_index gets set to the end of the navigation | 865 // Ensure the current_navigation_index gets set to the end of the navigation |
| 775 // stack if the current navigation is invalid. | 866 // stack if the current navigation is invalid. |
| 776 TEST_F(SessionsSyncManagerTest, SetSessionTabFromDelegateCurrentInvalid) { | 867 TEST_F(SessionsSyncManagerTest, SetSessionTabFromDelegateCurrentInvalid) { |
| 777 SyncedTabDelegateFake tab; | 868 TestSyncedTabDelegate* tab = |
| 778 std::unique_ptr<content::NavigationEntry> entry0( | 869 AddTab(AddWindow()->GetSessionId(), kFoo1, kTime0); |
| 779 content::NavigationEntry::Create()); | 870 NavigateTab(tab, std::string(""), kTime1); |
| 780 entry0->SetVirtualURL(GURL("http://www.google.com")); | 871 NavigateTab(tab, kBar1, kTime2); |
| 781 entry0->SetTimestamp(kTime0); | 872 NavigateTab(tab, kBar2, kTime3); |
| 782 entry0->SetHttpStatusCode(200); | 873 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 | 874 |
| 805 sessions::SessionTab session_tab; | 875 sessions::SessionTab session_tab; |
| 806 manager()->SetSessionTabFromDelegate(tab, kTime9, &session_tab); | 876 manager()->SetSessionTabFromDelegate(*tab, kTime9, &session_tab); |
| 807 | 877 |
| 808 EXPECT_EQ(2, session_tab.current_navigation_index); | 878 EXPECT_EQ(2, session_tab.current_navigation_index); |
| 809 ASSERT_EQ(3u, session_tab.navigations.size()); | 879 ASSERT_EQ(3u, session_tab.navigations.size()); |
| 810 } | 880 } |
| 811 | 881 |
| 812 // Tests that variation ids are set correctly. | 882 // Tests that variation ids are set correctly. |
| 813 TEST_F(SessionsSyncManagerTest, SetVariationIds) { | 883 TEST_F(SessionsSyncManagerTest, SetVariationIds) { |
| 814 // Create two trials with a group which has a variation id for Chrome Sync | 884 // Create two trials with a group which has a variation id for Chrome Sync |
| 815 // and one with a variation id for another service. | 885 // and one with a variation id for another service. |
| 816 const variations::VariationID kVariationId1 = 3300200; | 886 const variations::VariationID kVariationId1 = 3300200; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 829 manager()->SetVariationIds(&session_tab); | 899 manager()->SetVariationIds(&session_tab); |
| 830 | 900 |
| 831 ASSERT_EQ(2u, session_tab.variation_ids.size()); | 901 ASSERT_EQ(2u, session_tab.variation_ids.size()); |
| 832 EXPECT_EQ(kVariationId1, session_tab.variation_ids[0]); | 902 EXPECT_EQ(kVariationId1, session_tab.variation_ids[0]); |
| 833 EXPECT_EQ(kVariationId2, session_tab.variation_ids[1]); | 903 EXPECT_EQ(kVariationId2, session_tab.variation_ids[1]); |
| 834 } | 904 } |
| 835 | 905 |
| 836 // Tests that for supervised users blocked navigations are recorded and marked | 906 // Tests that for supervised users blocked navigations are recorded and marked |
| 837 // as such, while regular navigations are marked as allowed. | 907 // as such, while regular navigations are marked as allowed. |
| 838 TEST_F(SessionsSyncManagerTest, BlockedNavigations) { | 908 TEST_F(SessionsSyncManagerTest, BlockedNavigations) { |
| 839 SyncedTabDelegateFake tab; | 909 TestSyncedTabDelegate* tab = |
| 840 std::unique_ptr<content::NavigationEntry> entry1( | 910 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 | 911 |
| 847 std::unique_ptr<content::NavigationEntry> entry2( | 912 std::unique_ptr<sessions::SerializedNavigationEntry> entry2( |
| 848 content::NavigationEntry::Create()); | 913 new sessions::SerializedNavigationEntry()); |
| 849 GURL url2("http://blocked.com/foo"); | 914 GURL url2("http://blocked.com/foo"); |
| 850 entry2->SetVirtualURL(url2); | 915 SerializedNavigationEntryTestHelper::SetVirtualURL(GURL(url2), entry2.get()); |
| 851 entry2->SetTimestamp(kTime2); | 916 SerializedNavigationEntryTestHelper::SetTimestamp(kTime2, entry2.get()); |
| 852 std::unique_ptr<content::NavigationEntry> entry3( | 917 std::unique_ptr<sessions::SerializedNavigationEntry> entry3( |
|
skym
2017/02/22 22:25:20
MakeUnique
Nicolas Zea
2017/02/22 22:58:40
Done.
| |
| 853 content::NavigationEntry::Create()); | 918 new sessions::SerializedNavigationEntry()); |
| 854 GURL url3("http://evil.com/"); | 919 GURL url3("http://evil.com"); |
| 855 entry3->SetVirtualURL(url3); | 920 SerializedNavigationEntryTestHelper::SetVirtualURL(GURL(url3), entry3.get()); |
| 856 entry3->SetTimestamp(kTime3); | 921 SerializedNavigationEntryTestHelper::SetTimestamp(kTime3, entry3.get()); |
| 857 ScopedVector<const content::NavigationEntry> blocked_navigations; | 922 std::vector<std::unique_ptr<sessions::SerializedNavigationEntry>> |
| 923 blocked_navigations; | |
| 858 blocked_navigations.push_back(std::move(entry2)); | 924 blocked_navigations.push_back(std::move(entry2)); |
| 859 blocked_navigations.push_back(std::move(entry3)); | 925 blocked_navigations.push_back(std::move(entry3)); |
| 860 | 926 |
| 861 tab.set_is_supervised(true); | 927 tab->set_is_supervised(true); |
| 862 tab.set_blocked_navigations(&blocked_navigations.get()); | 928 tab->set_blocked_navigations(blocked_navigations); |
| 863 | 929 |
| 864 sessions::SessionTab session_tab; | 930 sessions::SessionTab session_tab; |
| 865 session_tab.window_id.set_id(1); | 931 session_tab.window_id.set_id(1); |
| 866 session_tab.tab_id.set_id(1); | 932 session_tab.tab_id.set_id(1); |
| 867 session_tab.tab_visual_index = 1; | 933 session_tab.tab_visual_index = 1; |
| 868 session_tab.current_navigation_index = 1; | 934 session_tab.current_navigation_index = 1; |
| 869 session_tab.pinned = true; | 935 session_tab.pinned = true; |
| 870 session_tab.extension_app_id = "app id"; | 936 session_tab.extension_app_id = "app id"; |
| 871 session_tab.user_agent_override = "override"; | 937 session_tab.user_agent_override = "override"; |
| 872 session_tab.timestamp = kTime5; | 938 session_tab.timestamp = kTime5; |
| 873 session_tab.navigations.push_back( | 939 session_tab.navigations.push_back( |
| 874 SerializedNavigationEntryTestHelper::CreateNavigation( | 940 SerializedNavigationEntryTestHelper::CreateNavigation( |
| 875 "http://www.example.com", "Example")); | 941 "http://www.example.com", "Example")); |
| 876 session_tab.session_storage_persistent_id = "persistent id"; | 942 session_tab.session_storage_persistent_id = "persistent id"; |
| 877 manager()->SetSessionTabFromDelegate(tab, kTime4, &session_tab); | 943 manager()->SetSessionTabFromDelegate(*tab, kTime4, &session_tab); |
| 878 | 944 |
| 879 EXPECT_EQ(0, session_tab.window_id.id()); | 945 EXPECT_EQ(tab->GetWindowId(), session_tab.window_id.id()); |
| 880 EXPECT_EQ(0, session_tab.tab_id.id()); | 946 EXPECT_EQ(tab->GetSessionId(), session_tab.tab_id.id()); |
| 881 EXPECT_EQ(0, session_tab.tab_visual_index); | 947 EXPECT_EQ(0, session_tab.tab_visual_index); |
| 882 EXPECT_EQ(0, session_tab.current_navigation_index); | 948 EXPECT_EQ(0, session_tab.current_navigation_index); |
| 883 EXPECT_FALSE(session_tab.pinned); | 949 EXPECT_FALSE(session_tab.pinned); |
| 884 EXPECT_TRUE(session_tab.extension_app_id.empty()); | 950 EXPECT_TRUE(session_tab.extension_app_id.empty()); |
| 885 EXPECT_TRUE(session_tab.user_agent_override.empty()); | 951 EXPECT_TRUE(session_tab.user_agent_override.empty()); |
| 886 EXPECT_EQ(kTime4, session_tab.timestamp); | 952 EXPECT_EQ(kTime4, session_tab.timestamp); |
| 887 ASSERT_EQ(3u, session_tab.navigations.size()); | 953 ASSERT_EQ(3u, session_tab.navigations.size()); |
| 888 EXPECT_EQ(url1, session_tab.navigations[0].virtual_url()); | 954 EXPECT_EQ(GURL(kFoo1), session_tab.navigations[0].virtual_url()); |
| 889 EXPECT_EQ(url2, session_tab.navigations[1].virtual_url()); | 955 EXPECT_EQ(url2, session_tab.navigations[1].virtual_url()); |
| 890 EXPECT_EQ(url3, session_tab.navigations[2].virtual_url()); | 956 EXPECT_EQ(url3, session_tab.navigations[2].virtual_url()); |
| 891 EXPECT_EQ(kTime1, session_tab.navigations[0].timestamp()); | 957 EXPECT_EQ(kTime1, session_tab.navigations[0].timestamp()); |
| 892 EXPECT_EQ(kTime2, session_tab.navigations[1].timestamp()); | 958 EXPECT_EQ(kTime2, session_tab.navigations[1].timestamp()); |
| 893 EXPECT_EQ(kTime3, session_tab.navigations[2].timestamp()); | 959 EXPECT_EQ(kTime3, session_tab.navigations[2].timestamp()); |
| 894 EXPECT_EQ(SerializedNavigationEntry::STATE_ALLOWED, | 960 EXPECT_EQ(SerializedNavigationEntry::STATE_ALLOWED, |
| 895 session_tab.navigations[0].blocked_state()); | 961 session_tab.navigations[0].blocked_state()); |
| 896 EXPECT_EQ(SerializedNavigationEntry::STATE_BLOCKED, | 962 EXPECT_EQ(SerializedNavigationEntry::STATE_BLOCKED, |
| 897 session_tab.navigations[1].blocked_state()); | 963 session_tab.navigations[1].blocked_state()); |
| 898 EXPECT_EQ(SerializedNavigationEntry::STATE_BLOCKED, | 964 EXPECT_EQ(SerializedNavigationEntry::STATE_BLOCKED, |
| 899 session_tab.navigations[2].blocked_state()); | 965 session_tab.navigations[2].blocked_state()); |
| 900 EXPECT_TRUE(session_tab.session_storage_persistent_id.empty()); | 966 EXPECT_TRUE(session_tab.session_storage_persistent_id.empty()); |
| 901 } | 967 } |
| 902 | 968 |
| 903 // Tests that the local session header objects is created properly in | 969 // Tests that the local session header objects is created properly in |
| 904 // presence of no other session activity, once and only once. | 970 // presence of no other session activity, once and only once. |
| 905 TEST_F(SessionsSyncManagerTest, MergeLocalSessionNoTabs) { | 971 TEST_F(SessionsSyncManagerTest, MergeLocalSessionNoTabs) { |
| 906 syncer::SyncChangeList out; | 972 // Add a single window with no tabs. |
| 907 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); | 973 AddWindow(); |
| 974 | |
| 975 SyncChangeList out; | |
| 976 InitWithSyncDataTakeOutput(SyncDataList(), &out); | |
| 908 EXPECT_FALSE(manager()->current_machine_tag().empty()); | 977 EXPECT_FALSE(manager()->current_machine_tag().empty()); |
| 909 | 978 |
| 910 EXPECT_EQ(2U, out.size()); | 979 // Header creation + update. |
| 911 EXPECT_TRUE(out[0].IsValid()); | 980 ASSERT_TRUE(ChangeTypeMatches( |
| 912 EXPECT_EQ(SyncChange::ACTION_ADD, out[0].change_type()); | 981 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE})); |
| 913 const SyncData data(out[0].sync_data()); | 982 EXPECT_EQ(out.size(), |
| 914 EXPECT_EQ(manager()->current_machine_tag(), | 983 CountIfTagMatches(out, manager()->current_machine_tag())); |
| 915 syncer::SyncDataLocal(data).GetTag()); | 984 VerifyLocalHeaderChange(out[0], 0, 0); |
| 916 const sync_pb::SessionSpecifics& specifics(data.GetSpecifics().session()); | 985 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 | 986 |
| 935 // Now take that header node and feed it in as input. | 987 // Now take that header node and feed it in as input. |
| 936 SyncData d = CreateRemoteData(data.GetSpecifics()); | 988 SyncData d = CreateRemoteData(out[1].sync_data().GetSpecifics()); |
| 937 syncer::SyncDataList in(&d, &d + 1); | 989 SyncDataList in(&d, &d + 1); |
| 938 out.clear(); | 990 out.clear(); |
| 939 SessionsSyncManager manager2(GetSyncSessionsClient(), sync_prefs(), | 991 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) { | |
|
Nicolas Zea
2017/02/22 20:37:49
This test was moved to the bottom of the file (alo
| |
| 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); | 992 InitWithSyncDataTakeOutput(in, &out); |
| 968 | 993 |
| 969 // Should be one header add, 3 tab add/update pairs, one header update. | 994 ASSERT_TRUE(ChangeTypeMatches(out, {SyncChange::ACTION_UPDATE})); |
| 970 ASSERT_EQ(8U, out.size()); | 995 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 } | 996 } |
| 1082 | 997 |
| 1083 // Tests MergeDataAndStartSyncing with sync data but no local data. | 998 // Tests MergeDataAndStartSyncing with sync data but no local data. |
| 1084 TEST_F(SessionsSyncManagerTest, MergeWithInitialForeignSession) { | 999 TEST_F(SessionsSyncManagerTest, MergeWithInitialForeignSession) { |
| 1085 std::string tag = "tag1"; | 1000 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1), |
|
skym
2017/02/22 22:25:12
This happens over and over. Are we not allowed to
Nicolas Zea
2017/02/22 22:58:40
No, static consts must be POD.
| |
| 1086 | 1001 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; | 1002 std::vector<sync_pb::SessionSpecifics> tabs1; |
| 1090 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | 1003 sync_pb::SessionSpecifics meta( |
| 1091 tag, tab_list1, &tabs1)); | 1004 helper()->BuildForeignSession(kTag1, tab_list1, &tabs1)); |
| 1092 // Add a second window. | 1005 // Add a second window. |
| 1093 SessionID::id_type n2[] = {7, 15, 18, 20}; | 1006 std::vector<SessionID::id_type> tab_list2(std::begin(kTabIds2), |
| 1094 std::vector<SessionID::id_type> tab_list2(n2, n2 + arraysize(n2)); | 1007 std::end(kTabIds2)); |
| 1095 helper()->AddWindowSpecifics(1, tab_list2, &meta); | 1008 helper()->AddWindowSpecifics(1, tab_list2, &meta); |
| 1096 | 1009 |
| 1097 // Set up initial data. | 1010 // Set up initial data. |
| 1098 syncer::SyncDataList initial_data; | 1011 SyncDataList initial_data; |
| 1099 initial_data.push_back(CreateRemoteData(meta)); | 1012 initial_data.push_back(CreateRemoteData(meta)); |
| 1100 AddTabsToSyncDataList(tabs1, &initial_data); | 1013 AddTabsToSyncDataList(tabs1, &initial_data); |
| 1101 | 1014 for (auto tab_id : tab_list2) { |
| 1102 for (size_t i = 0; i < tab_list2.size(); ++i) { | |
| 1103 sync_pb::EntitySpecifics entity; | 1015 sync_pb::EntitySpecifics entity; |
| 1104 helper()->BuildTabSpecifics(tag, 0, tab_list2[i], | 1016 helper()->BuildTabSpecifics(kTag1, 0, tab_id, entity.mutable_session()); |
| 1105 entity.mutable_session()); | |
| 1106 initial_data.push_back(CreateRemoteData(entity)); | 1017 initial_data.push_back(CreateRemoteData(entity)); |
| 1107 } | 1018 } |
| 1108 | 1019 |
| 1109 syncer::SyncChangeList output; | 1020 SyncChangeList output; |
| 1110 InitWithSyncDataTakeOutput(initial_data, &output); | 1021 InitWithSyncDataTakeOutput(initial_data, &output); |
| 1111 EXPECT_TRUE(FilterOutLocalHeaderChanges(&output)->empty()); | 1022 EXPECT_TRUE(FilterOutLocalHeaderChanges(&output)->empty()); |
| 1112 | 1023 |
| 1113 std::vector<const SyncedSession*> foreign_sessions; | 1024 std::vector<const SyncedSession*> foreign_sessions; |
| 1114 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1025 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1115 ASSERT_EQ(1U, foreign_sessions.size()); | 1026 ASSERT_EQ(1U, foreign_sessions.size()); |
| 1116 std::vector<std::vector<SessionID::id_type> > session_reference; | 1027 std::vector<std::vector<SessionID::id_type>> session_reference; |
| 1117 session_reference.push_back(tab_list1); | 1028 session_reference.push_back(tab_list1); |
| 1118 session_reference.push_back(tab_list2); | 1029 session_reference.push_back(tab_list2); |
| 1119 helper()->VerifySyncedSession(tag, session_reference, *(foreign_sessions[0])); | 1030 helper()->VerifySyncedSession(kTag1, session_reference, |
| 1031 *(foreign_sessions[0])); | |
| 1032 } | |
| 1033 | |
| 1034 // Ensure model association associates the pre-existing tabs. | |
| 1035 TEST_F(SessionsSyncManagerTest, MergeLocalSessionExistingTabs) { | |
| 1036 SessionID::id_type window_id = AddWindow()->GetSessionId(); | |
| 1037 TestSyncedTabDelegate* tab = AddTab(window_id, kFoo1); | |
| 1038 NavigateTab(tab, kBar1); // Adds back entry. | |
| 1039 NavigateTab(tab, kBaz1); // Adds back entry. | |
| 1040 TestSyncedTabDelegate* tab2 = AddTab(window_id, kFoo2); | |
| 1041 NavigateTab(tab2, kBar2); // Adds back entry. | |
| 1042 | |
| 1043 SyncChangeList out; | |
| 1044 InitWithSyncDataTakeOutput(SyncDataList(), &out); | |
| 1045 // Header creation, add two tabs (and update them), header update. | |
| 1046 ASSERT_TRUE(ChangeTypeMatches( | |
| 1047 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_ADD, | |
| 1048 SyncChange::ACTION_UPDATE, SyncChange::ACTION_ADD, | |
| 1049 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE})); | |
| 1050 EXPECT_EQ(out.size(), | |
| 1051 CountIfTagMatches(out, manager()->current_machine_tag())); | |
| 1052 | |
| 1053 // Check that this machine's data is not included in the foreign windows. | |
| 1054 std::vector<const SyncedSession*> foreign_sessions; | |
| 1055 ASSERT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); | |
| 1056 | |
| 1057 VerifyLocalHeaderChange(out[0], 0, 0); | |
| 1058 VerifyLocalTabChange(out[2], tab->GetEntryCount(), kBaz1); | |
| 1059 VerifyLocalTabChange(out[4], tab2->GetEntryCount(), kBar2); | |
| 1060 VerifyLocalHeaderChange(out[5], 1, 2); | |
| 1061 | |
| 1062 // Verify tab delegates have Sync ids. | |
| 1063 std::set<const SyncedWindowDelegate*> window_delegates = | |
| 1064 window_getter()->GetSyncedWindowDelegates(); | |
| 1065 // Sync ids are in same order as tabs because the association happens after | |
| 1066 // the tabs are opened (and therefore iterates through same order). | |
| 1067 EXPECT_EQ(0, (*window_delegates.begin())->GetTabAt(0)->GetSyncId()); | |
| 1068 EXPECT_EQ(1, (*window_delegates.begin())->GetTabAt(1)->GetSyncId()); | |
| 1120 } | 1069 } |
| 1121 | 1070 |
| 1122 // This is a combination of MergeWithInitialForeignSession and | 1071 // This is a combination of MergeWithInitialForeignSession and |
| 1123 // MergeLocalSessionExistingTabs. We repeat some checks performed in each of | 1072 // MergeLocalSessionExistingTabs. We repeat some checks performed in each of |
| 1124 // those tests to ensure the common mixed scenario works. | 1073 // those tests to ensure the common mixed scenario works. |
| 1125 TEST_F(SessionsSyncManagerTest, MergeWithLocalAndForeignTabs) { | 1074 TEST_F(SessionsSyncManagerTest, MergeWithLocalAndForeignTabs) { |
| 1126 // Local. | 1075 // Local. |
| 1127 AddTab(browser(), GURL("http://foo1")); | 1076 TestSyncedTabDelegate* tab = AddTab(AddWindow()->GetSessionId(), kFoo1); |
| 1128 NavigateAndCommitActiveTab(GURL("http://foo2")); | 1077 NavigateTab(tab, kFoo2); |
| 1129 | 1078 |
| 1130 // Foreign. | 1079 // Foreign. |
| 1131 std::string tag = "tag1"; | 1080 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1), |
| 1132 SessionID::id_type n1[] = {5, 10, 13, 17}; | 1081 std::end(kTabIds1)); |
| 1133 std::vector<SessionID::id_type> tab_list1(n1, n1 + arraysize(n1)); | |
| 1134 std::vector<sync_pb::SessionSpecifics> tabs1; | 1082 std::vector<sync_pb::SessionSpecifics> tabs1; |
| 1135 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | 1083 sync_pb::SessionSpecifics meta( |
| 1136 tag, tab_list1, &tabs1)); | 1084 helper()->BuildForeignSession(kTag1, tab_list1, &tabs1)); |
| 1137 syncer::SyncDataList foreign_data; | 1085 SyncDataList foreign_data; |
| 1138 foreign_data.push_back(CreateRemoteData(meta)); | 1086 foreign_data.push_back(CreateRemoteData(meta)); |
| 1139 AddTabsToSyncDataList(tabs1, &foreign_data); | 1087 AddTabsToSyncDataList(tabs1, &foreign_data); |
| 1140 | 1088 |
| 1141 syncer::SyncChangeList output; | 1089 SyncChangeList out; |
| 1142 InitWithSyncDataTakeOutput(foreign_data, &output); | 1090 InitWithSyncDataTakeOutput(foreign_data, &out); |
| 1143 ASSERT_EQ(4U, output.size()); | 1091 // Should be one header add, 1 tab add (and update), and one header update. |
| 1144 | 1092 |
| 1145 // Verify the local header. | 1093 ASSERT_TRUE(ChangeTypeMatches( |
| 1146 EXPECT_TRUE(output[0].IsValid()); | 1094 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_ADD, |
| 1147 EXPECT_EQ(SyncChange::ACTION_ADD, output[0].change_type()); | 1095 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE})); |
| 1148 const SyncData data(output[0].sync_data()); | 1096 EXPECT_EQ(out.size(), |
| 1149 EXPECT_EQ(manager()->current_machine_tag(), | 1097 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 | 1098 |
| 1159 // Verify the tab node creations and updates with content. | 1099 // Verify local data. |
| 1160 for (int i = 1; i < 3; i++) { | 1100 VerifyLocalHeaderChange(out[0], 0, 0); |
| 1161 EXPECT_TRUE(output[i].IsValid()); | 1101 VerifyLocalTabChange(out[2], tab->GetEntryCount(), kFoo2); |
| 1162 const SyncData data(output[i].sync_data()); | 1102 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 | 1103 |
| 1185 // Verify foreign data. | 1104 // Verify foreign data. |
| 1186 std::vector<const SyncedSession*> foreign_sessions; | 1105 std::vector<const SyncedSession*> foreign_sessions; |
| 1187 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1106 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1188 std::vector<std::vector<SessionID::id_type> > session_reference; | 1107 std::vector<std::vector<SessionID::id_type>> session_reference; |
| 1189 session_reference.push_back(tab_list1); | 1108 session_reference.push_back(tab_list1); |
| 1190 helper()->VerifySyncedSession(tag, session_reference, *(foreign_sessions[0])); | 1109 helper()->VerifySyncedSession(kTag1, session_reference, |
| 1110 *(foreign_sessions[0])); | |
| 1191 // There should be one and only one foreign session. If VerifySyncedSession | 1111 // There should be one and only one foreign session. If VerifySyncedSession |
| 1192 // was successful above this EXPECT call ensures the local session didn't | 1112 // was successful above this EXPECT call ensures the local session didn't |
| 1193 // get mistakenly added to foreign tracking (Similar to ExistingTabs test). | 1113 // get mistakenly added to foreign tracking (Similar to ExistingTabs test). |
| 1194 EXPECT_EQ(1U, foreign_sessions.size()); | 1114 EXPECT_EQ(1U, foreign_sessions.size()); |
| 1195 } | 1115 } |
| 1196 | 1116 |
| 1197 // Tests the common scenario. Merge with both local and foreign session data | 1117 // Tests the common scenario. Merge with both local and foreign session data |
| 1198 // followed by updates flowing from sync and local. | 1118 // followed by updates flowing from sync and local. |
| 1199 TEST_F(SessionsSyncManagerTest, UpdatesAfterMixedMerge) { | 1119 TEST_F(SessionsSyncManagerTest, UpdatesAfterMixedMerge) { |
| 1200 // Add local and foreign data. | 1120 // Add local and foreign data. |
| 1201 AddTab(browser(), GURL("http://foo1")); | 1121 TestSyncedTabDelegate* tab = AddTab(AddWindow()->GetSessionId(), kFoo1); |
| 1202 NavigateAndCommitActiveTab(GURL("http://foo2")); | 1122 NavigateTab(tab, kFoo2); |
| 1123 AddTab(AddWindow()->GetSessionId(), kBar1); | |
| 1203 | 1124 |
| 1204 std::string tag1 = "tag1"; | 1125 SyncDataList foreign_data1; |
| 1205 syncer::SyncDataList foreign_data1; | 1126 std::vector<std::vector<SessionID::id_type>> meta1_reference; |
| 1206 std::vector<std::vector<SessionID::id_type> > meta1_reference; | |
| 1207 sync_pb::SessionSpecifics meta1; | 1127 sync_pb::SessionSpecifics meta1; |
| 1208 | 1128 |
| 1209 SessionID::id_type n1[] = {5, 10, 13, 17}; | 1129 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1), |
| 1210 std::vector<SessionID::id_type> tab_list1(n1, n1 + arraysize(n1)); | 1130 std::end(kTabIds1)); |
| 1211 meta1_reference.push_back(tab_list1); | 1131 meta1_reference.push_back(tab_list1); |
| 1212 std::vector<sync_pb::SessionSpecifics> tabs1; | 1132 std::vector<sync_pb::SessionSpecifics> tabs1; |
| 1213 meta1 = helper()->BuildForeignSession(tag1, tab_list1, &tabs1); | 1133 meta1 = helper()->BuildForeignSession(kTag1, tab_list1, &tabs1); |
| 1214 foreign_data1.push_back(CreateRemoteData(meta1)); | 1134 foreign_data1.push_back(CreateRemoteData(meta1)); |
| 1215 AddTabsToSyncDataList(tabs1, &foreign_data1); | 1135 AddTabsToSyncDataList(tabs1, &foreign_data1); |
| 1216 | 1136 |
| 1217 syncer::SyncChangeList output1; | 1137 SyncChangeList out; |
| 1218 InitWithSyncDataTakeOutput(foreign_data1, &output1); | 1138 InitWithSyncDataTakeOutput(foreign_data1, &out); |
| 1219 ASSERT_EQ(4U, output1.size()); | 1139 |
| 1140 // 1 header add, two tab adds, one header update. | |
| 1141 ASSERT_TRUE(ChangeTypeMatches( | |
| 1142 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_ADD, | |
| 1143 SyncChange::ACTION_UPDATE, SyncChange::ACTION_ADD, | |
| 1144 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE})); | |
| 1145 EXPECT_EQ(out.size(), | |
| 1146 CountIfTagMatches(out, manager()->current_machine_tag())); | |
| 1147 VerifyLocalHeaderChange(out[5], 2, 2); | |
| 1220 | 1148 |
| 1221 // Add a second window to the foreign session. | 1149 // Add a second window to the foreign session. |
| 1222 // TODO(tim): Bug 98892. Add local window too when observers are hooked up. | 1150 std::vector<SessionID::id_type> tab_list2(std::begin(kTabIds2), |
| 1223 SessionID::id_type tab_nums2[] = {7, 15, 18, 20}; | 1151 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); | 1152 meta1_reference.push_back(tab_list2); |
| 1227 helper()->AddWindowSpecifics(1, tab_list2, &meta1); | 1153 helper()->AddWindowSpecifics(1, tab_list2, &meta1); |
| 1228 std::vector<sync_pb::SessionSpecifics> tabs2; | 1154 std::vector<sync_pb::SessionSpecifics> tabs2; |
| 1229 tabs2.resize(tab_list2.size()); | 1155 tabs2.resize(tab_list2.size()); |
| 1230 for (size_t i = 0; i < tab_list2.size(); ++i) { | 1156 for (size_t i = 0; i < tab_list2.size(); ++i) { |
| 1231 helper()->BuildTabSpecifics(tag1, 0, tab_list2[i], &tabs2[i]); | 1157 helper()->BuildTabSpecifics(kTag1, 0, tab_list2[i], &tabs2[i]); |
| 1232 } | 1158 } |
| 1233 | 1159 |
| 1234 syncer::SyncChangeList changes; | 1160 SyncChangeList changes; |
| 1235 changes.push_back(MakeRemoteChange(meta1, SyncChange::ACTION_UPDATE)); | 1161 changes.push_back(MakeRemoteChange(meta1, SyncChange::ACTION_UPDATE)); |
| 1236 AddTabsToChangeList(tabs2, SyncChange::ACTION_ADD, &changes); | 1162 AddTabsToChangeList(tabs2, SyncChange::ACTION_ADD, &changes); |
| 1237 manager()->ProcessSyncChanges(FROM_HERE, changes); | 1163 manager()->ProcessSyncChanges(FROM_HERE, changes); |
| 1238 changes.clear(); | 1164 changes.clear(); |
| 1239 | 1165 |
| 1240 // Check that the foreign session was associated and retrieve the data. | 1166 // Check that the foreign session was associated and retrieve the data. |
| 1241 std::vector<const SyncedSession*> foreign_sessions; | 1167 std::vector<const SyncedSession*> foreign_sessions; |
| 1242 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1168 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1243 ASSERT_EQ(1U, foreign_sessions.size()); | 1169 ASSERT_EQ(1U, foreign_sessions.size()); |
| 1244 ASSERT_EQ(4U, foreign_sessions[0]->windows.find(0)->second->tabs.size()); | 1170 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()); | 1171 ASSERT_EQ(4U, foreign_sessions[0]->windows.find(1)->second->tabs.size()); |
| 1246 helper()->VerifySyncedSession(tag1, meta1_reference, *(foreign_sessions[0])); | 1172 helper()->VerifySyncedSession(kTag1, meta1_reference, *(foreign_sessions[0])); |
| 1247 | 1173 |
| 1248 // Add a new foreign session. | 1174 // Add a new foreign session. |
| 1249 std::string tag2 = "tag2"; | 1175 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; | 1176 std::vector<sync_pb::SessionSpecifics> tag2_tabs; |
| 1253 sync_pb::SessionSpecifics meta2(helper()->BuildForeignSession( | 1177 sync_pb::SessionSpecifics meta2( |
| 1254 tag2, tag2_tab_list, &tag2_tabs)); | 1178 helper()->BuildForeignSession(kTag2, tag2_tab_list, &tag2_tabs)); |
| 1255 changes.push_back(MakeRemoteChange(meta2, SyncChange::ACTION_ADD)); | 1179 changes.push_back(MakeRemoteChange(meta2, SyncChange::ACTION_ADD)); |
| 1256 AddTabsToChangeList(tag2_tabs, SyncChange::ACTION_ADD, &changes); | 1180 AddTabsToChangeList(tag2_tabs, SyncChange::ACTION_ADD, &changes); |
| 1257 | 1181 |
| 1258 manager()->ProcessSyncChanges(FROM_HERE, changes); | 1182 manager()->ProcessSyncChanges(FROM_HERE, changes); |
| 1259 changes.clear(); | 1183 changes.clear(); |
| 1260 | 1184 |
| 1261 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1185 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1262 std::vector<std::vector<SessionID::id_type> > meta2_reference; | 1186 std::vector<std::vector<SessionID::id_type>> meta2_reference; |
| 1263 meta2_reference.push_back(tag2_tab_list); | 1187 meta2_reference.push_back(tag2_tab_list); |
| 1264 ASSERT_EQ(2U, foreign_sessions.size()); | 1188 ASSERT_EQ(2U, foreign_sessions.size()); |
| 1265 ASSERT_EQ(2U, foreign_sessions[1]->windows.find(0)->second->tabs.size()); | 1189 ASSERT_EQ(2U, foreign_sessions[1]->windows.find(0)->second->tabs.size()); |
| 1266 helper()->VerifySyncedSession(tag2, meta2_reference, *(foreign_sessions[1])); | 1190 helper()->VerifySyncedSession(kTag2, meta2_reference, *(foreign_sessions[1])); |
| 1267 foreign_sessions.clear(); | 1191 foreign_sessions.clear(); |
| 1268 | 1192 |
| 1269 // Remove a tab from a window. | 1193 // Remove a tab from a window. |
| 1270 meta1_reference[0].pop_back(); | 1194 meta1_reference[0].pop_back(); |
| 1271 tab_list1.pop_back(); | 1195 tab_list1.pop_back(); |
| 1272 sync_pb::SessionWindow* win = meta1.mutable_header()->mutable_window(0); | 1196 sync_pb::SessionWindow* win = meta1.mutable_header()->mutable_window(0); |
| 1273 win->clear_tab(); | 1197 win->clear_tab(); |
| 1274 for (std::vector<int>::const_iterator iter = tab_list1.begin(); | 1198 for (std::vector<int>::const_iterator iter = tab_list1.begin(); |
| 1275 iter != tab_list1.end(); ++iter) { | 1199 iter != tab_list1.end(); ++iter) { |
| 1276 win->add_tab(*iter); | 1200 win->add_tab(*iter); |
| 1277 } | 1201 } |
| 1278 syncer::SyncChangeList removal; | 1202 SyncChangeList removal; |
| 1279 removal.push_back(MakeRemoteChange(meta1, SyncChange::ACTION_UPDATE)); | 1203 removal.push_back(MakeRemoteChange(meta1, SyncChange::ACTION_UPDATE)); |
| 1280 AddTabsToChangeList(tabs1, SyncChange::ACTION_UPDATE, &removal); | 1204 AddTabsToChangeList(tabs1, SyncChange::ACTION_UPDATE, &removal); |
| 1281 manager()->ProcessSyncChanges(FROM_HERE, removal); | 1205 manager()->ProcessSyncChanges(FROM_HERE, removal); |
| 1282 | 1206 |
| 1283 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1207 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1284 ASSERT_EQ(2U, foreign_sessions.size()); | 1208 ASSERT_EQ(2U, foreign_sessions.size()); |
| 1285 ASSERT_EQ(3U, foreign_sessions[0]->windows.find(0)->second->tabs.size()); | 1209 ASSERT_EQ(3U, foreign_sessions[0]->windows.find(0)->second->tabs.size()); |
| 1286 helper()->VerifySyncedSession(tag1, meta1_reference, *(foreign_sessions[0])); | 1210 helper()->VerifySyncedSession(kTag1, meta1_reference, *(foreign_sessions[0])); |
| 1287 } | 1211 } |
| 1288 | 1212 |
| 1289 // Tests that this SyncSessionManager knows how to delete foreign sessions | 1213 // Tests that this SyncSessionManager knows how to delete foreign sessions |
| 1290 // if it wants to. | 1214 // if it wants to. |
| 1291 TEST_F(SessionsSyncManagerTest, DeleteForeignSession) { | 1215 TEST_F(SessionsSyncManagerTest, DeleteForeignSession) { |
| 1292 InitWithNoSyncData(); | 1216 InitWithNoSyncData(); |
| 1293 std::string tag = "tag1"; | 1217 SyncChangeList changes; |
| 1294 syncer::SyncChangeList changes; | |
| 1295 | 1218 |
| 1296 std::vector<const SyncedSession*> foreign_sessions; | 1219 std::vector<const SyncedSession*> foreign_sessions; |
| 1297 ASSERT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1220 ASSERT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1298 manager()->DeleteForeignSessionInternal(tag, &changes); | 1221 manager()->DeleteForeignSessionInternal(kTag1, &changes); |
| 1299 ASSERT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1222 ASSERT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1300 EXPECT_TRUE(changes.empty()); | 1223 EXPECT_TRUE(changes.empty()); |
| 1301 | 1224 |
| 1302 // Fill an instance of session specifics with a foreign session's data. | 1225 // Fill an instance of session specifics with a foreign session's data. |
| 1303 std::vector<sync_pb::SessionSpecifics> tabs; | 1226 std::vector<sync_pb::SessionSpecifics> tabs; |
| 1304 SessionID::id_type n1[] = {5, 10, 13, 17}; | 1227 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1), |
| 1305 std::vector<SessionID::id_type> tab_nums1(n1, n1 + arraysize(n1)); | 1228 std::end(kTabIds1)); |
| 1306 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | 1229 sync_pb::SessionSpecifics meta( |
| 1307 tag, tab_nums1, &tabs)); | 1230 helper()->BuildForeignSession(kTag1, tab_list1, &tabs)); |
| 1308 | 1231 |
| 1309 // Update associator with the session's meta node, window, and tabs. | 1232 // Update associator with the session's meta node, window, and tabs. |
| 1310 manager()->UpdateTrackerWithForeignSession(meta, base::Time()); | 1233 manager()->UpdateTrackerWithForeignSession(meta, base::Time()); |
| 1311 for (std::vector<sync_pb::SessionSpecifics>::iterator iter = tabs.begin(); | 1234 for (std::vector<sync_pb::SessionSpecifics>::iterator iter = tabs.begin(); |
| 1312 iter != tabs.end(); ++iter) { | 1235 iter != tabs.end(); ++iter) { |
| 1313 manager()->UpdateTrackerWithForeignSession(*iter, base::Time()); | 1236 manager()->UpdateTrackerWithForeignSession(*iter, base::Time()); |
| 1314 } | 1237 } |
| 1315 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1238 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1316 ASSERT_EQ(1U, foreign_sessions.size()); | 1239 ASSERT_EQ(1U, foreign_sessions.size()); |
| 1317 | 1240 |
| 1318 // Now delete the foreign session. | 1241 // Now delete the foreign session. |
| 1319 manager()->DeleteForeignSessionInternal(tag, &changes); | 1242 manager()->DeleteForeignSessionInternal(kTag1, &changes); |
| 1320 EXPECT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1243 EXPECT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1321 | 1244 |
| 1322 EXPECT_EQ(5U, changes.size()); | 1245 EXPECT_EQ(5U, changes.size()); |
| 1323 std::set<std::string> expected_tags(&tag, &tag + 1); | 1246 ASSERT_TRUE(AllOfChangesAreType(changes, SyncChange::ACTION_DELETE)); |
| 1324 for (int i = 0; i < 5; i++) | 1247 std::set<std::string> expected_tags(&kTag1, &kTag1 + 1); |
| 1325 expected_tags.insert(TabNodePool::TabIdToTag(tag, i)); | 1248 for (int i = 0; i < 5; ++i) |
| 1249 expected_tags.insert(TabNodeIdToTag(kTag1, i)); | |
| 1326 | 1250 |
| 1327 for (int i = 0; i < 5; i++) { | 1251 for (int i = 0; i < 5; ++i) { |
| 1328 SCOPED_TRACE(changes[i].ToString()); | 1252 SCOPED_TRACE(changes[i].ToString()); |
| 1329 EXPECT_TRUE(changes[i].IsValid()); | 1253 EXPECT_TRUE(changes[i].IsValid()); |
| 1330 EXPECT_EQ(SyncChange::ACTION_DELETE, changes[i].change_type()); | |
| 1331 EXPECT_TRUE(changes[i].sync_data().IsValid()); | 1254 EXPECT_TRUE(changes[i].sync_data().IsValid()); |
| 1332 EXPECT_EQ(1U, | 1255 EXPECT_EQ(1U, expected_tags.erase( |
| 1333 expected_tags.erase( | 1256 SyncDataLocal(changes[i].sync_data()).GetTag())); |
| 1334 syncer::SyncDataLocal(changes[i].sync_data()).GetTag())); | |
| 1335 } | 1257 } |
| 1336 } | 1258 } |
| 1337 | 1259 |
| 1338 // Write a foreign session to a node, with the tabs arriving first, and then | 1260 // Write a foreign session to a node, with the tabs arriving first, and then |
| 1339 // retrieve it. | 1261 // retrieve it. |
| 1340 TEST_F(SessionsSyncManagerTest, WriteForeignSessionToNodeTabsFirst) { | 1262 TEST_F(SessionsSyncManagerTest, WriteForeignSessionToNodeTabsFirst) { |
| 1341 InitWithNoSyncData(); | 1263 InitWithNoSyncData(); |
| 1342 | 1264 |
| 1343 // Fill an instance of session specifics with a foreign session's data. | 1265 // Fill an instance of session specifics with a foreign session's data. |
| 1344 std::string tag = "tag1"; | 1266 std::string tag = "tag1"; |
| 1345 SessionID::id_type nums1[] = {5, 10, 13, 17}; | |
| 1346 std::vector<sync_pb::SessionSpecifics> tabs1; | 1267 std::vector<sync_pb::SessionSpecifics> tabs1; |
| 1347 std::vector<SessionID::id_type> tab_list1(nums1, nums1 + arraysize(nums1)); | 1268 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1), |
| 1348 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | 1269 std::end(kTabIds1)); |
| 1349 tag, tab_list1, &tabs1)); | 1270 sync_pb::SessionSpecifics meta( |
| 1271 helper()->BuildForeignSession(tag, tab_list1, &tabs1)); | |
| 1350 | 1272 |
| 1351 syncer::SyncChangeList adds; | 1273 SyncChangeList adds; |
| 1352 // Add tabs for first window, then the meta node. | 1274 // Add tabs for first window, then the meta node. |
| 1353 AddTabsToChangeList(tabs1, SyncChange::ACTION_ADD, &adds); | 1275 AddTabsToChangeList(tabs1, SyncChange::ACTION_ADD, &adds); |
| 1354 adds.push_back(MakeRemoteChange(meta, SyncChange::ACTION_ADD)); | 1276 adds.push_back(MakeRemoteChange(meta, SyncChange::ACTION_ADD)); |
| 1355 manager()->ProcessSyncChanges(FROM_HERE, adds); | 1277 manager()->ProcessSyncChanges(FROM_HERE, adds); |
| 1356 | 1278 |
| 1357 // Check that the foreign session was associated and retrieve the data. | 1279 // Check that the foreign session was associated and retrieve the data. |
| 1358 std::vector<const SyncedSession*> foreign_sessions; | 1280 std::vector<const SyncedSession*> foreign_sessions; |
| 1359 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1281 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1360 ASSERT_EQ(1U, foreign_sessions.size()); | 1282 ASSERT_EQ(1U, foreign_sessions.size()); |
| 1361 std::vector<std::vector<SessionID::id_type> > session_reference; | 1283 std::vector<std::vector<SessionID::id_type>> session_reference; |
| 1362 session_reference.push_back(tab_list1); | 1284 session_reference.push_back(tab_list1); |
| 1363 helper()->VerifySyncedSession(tag, session_reference, *(foreign_sessions[0])); | 1285 helper()->VerifySyncedSession(tag, session_reference, *(foreign_sessions[0])); |
| 1364 } | 1286 } |
| 1365 | 1287 |
| 1366 // Write a foreign session to a node with some tabs that never arrive. | 1288 // Write a foreign session to a node with some tabs that never arrive. |
| 1367 TEST_F(SessionsSyncManagerTest, WriteForeignSessionToNodeMissingTabs) { | 1289 TEST_F(SessionsSyncManagerTest, WriteForeignSessionToNodeMissingTabs) { |
| 1368 InitWithNoSyncData(); | 1290 InitWithNoSyncData(); |
| 1369 | 1291 |
| 1370 // Fill an instance of session specifics with a foreign session's data. | 1292 // Fill an instance of session specifics with a foreign session's data. |
| 1371 std::string tag = "tag1"; | 1293 std::string tag = "tag1"; |
| 1372 SessionID::id_type nums1[] = {5, 10, 13, 17}; | |
| 1373 std::vector<sync_pb::SessionSpecifics> tabs1; | 1294 std::vector<sync_pb::SessionSpecifics> tabs1; |
| 1374 std::vector<SessionID::id_type> tab_list1(nums1, nums1 + arraysize(nums1)); | 1295 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1), |
| 1375 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | 1296 std::end(kTabIds1)); |
| 1376 tag, tab_list1, &tabs1)); | 1297 sync_pb::SessionSpecifics meta( |
| 1298 helper()->BuildForeignSession(tag, tab_list1, &tabs1)); | |
| 1377 // Add a second window, but this time only create two tab nodes, despite the | 1299 // Add a second window, but this time only create two tab nodes, despite the |
| 1378 // window expecting four tabs. | 1300 // window expecting four tabs. |
| 1379 SessionID::id_type tab_nums2[] = {7, 15, 18, 20}; | 1301 std::vector<SessionID::id_type> tab_list2(std::begin(kTabIds2), |
| 1380 std::vector<SessionID::id_type> tab_list2( | 1302 std::end(kTabIds2)); |
| 1381 tab_nums2, tab_nums2 + arraysize(tab_nums2)); | |
| 1382 helper()->AddWindowSpecifics(1, tab_list2, &meta); | 1303 helper()->AddWindowSpecifics(1, tab_list2, &meta); |
| 1383 std::vector<sync_pb::SessionSpecifics> tabs2; | 1304 std::vector<sync_pb::SessionSpecifics> tabs2; |
| 1384 tabs2.resize(2); | 1305 tabs2.resize(2); |
| 1385 for (size_t i = 0; i < 2; ++i) { | 1306 for (size_t i = 0; i < 2; ++i) { |
| 1386 helper()->BuildTabSpecifics(tag, 0, tab_list2[i], &tabs2[i]); | 1307 helper()->BuildTabSpecifics(tag, 0, tab_list2[i], &tabs2[i]); |
| 1387 } | 1308 } |
| 1388 | 1309 |
| 1389 syncer::SyncChangeList changes; | 1310 SyncChangeList changes; |
| 1390 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_ADD)); | 1311 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_ADD)); |
| 1391 AddTabsToChangeList(tabs1, SyncChange::ACTION_ADD, &changes); | 1312 AddTabsToChangeList(tabs1, SyncChange::ACTION_ADD, &changes); |
| 1392 AddTabsToChangeList(tabs2, SyncChange::ACTION_ADD, &changes); | 1313 AddTabsToChangeList(tabs2, SyncChange::ACTION_ADD, &changes); |
| 1393 manager()->ProcessSyncChanges(FROM_HERE, changes); | 1314 manager()->ProcessSyncChanges(FROM_HERE, changes); |
| 1394 changes.clear(); | 1315 changes.clear(); |
| 1395 | 1316 |
| 1396 // Check that the foreign session was associated and retrieve the data. | 1317 // Check that the foreign session was associated and retrieve the data. |
| 1397 std::vector<const SyncedSession*> foreign_sessions; | 1318 std::vector<const SyncedSession*> foreign_sessions; |
| 1398 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1319 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1399 ASSERT_EQ(1U, foreign_sessions.size()); | 1320 ASSERT_EQ(1U, foreign_sessions.size()); |
| 1400 ASSERT_EQ(2U, foreign_sessions[0]->windows.size()); | 1321 ASSERT_EQ(2U, foreign_sessions[0]->windows.size()); |
| 1401 ASSERT_EQ(4U, foreign_sessions[0]->windows.find(0)->second->tabs.size()); | 1322 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()); | 1323 ASSERT_EQ(4U, foreign_sessions[0]->windows.find(1)->second->tabs.size()); |
| 1403 | 1324 |
| 1404 // Close the second window. | 1325 // Close the second window. |
| 1405 meta.mutable_header()->clear_window(); | 1326 meta.mutable_header()->clear_window(); |
| 1406 helper()->AddWindowSpecifics(0, tab_list1, &meta); | 1327 helper()->AddWindowSpecifics(0, tab_list1, &meta); |
| 1407 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_UPDATE)); | 1328 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_UPDATE)); |
| 1408 // Update associator with the session's meta node containing one window. | 1329 // Update associator with the session's meta node containing one window. |
| 1409 manager()->ProcessSyncChanges(FROM_HERE, changes); | 1330 manager()->ProcessSyncChanges(FROM_HERE, changes); |
| 1410 | 1331 |
| 1411 // Check that the foreign session was associated and retrieve the data. | 1332 // Check that the foreign session was associated and retrieve the data. |
| 1412 foreign_sessions.clear(); | 1333 foreign_sessions.clear(); |
| 1413 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1334 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1414 ASSERT_EQ(1U, foreign_sessions.size()); | 1335 ASSERT_EQ(1U, foreign_sessions.size()); |
| 1415 ASSERT_EQ(1U, foreign_sessions[0]->windows.size()); | 1336 ASSERT_EQ(1U, foreign_sessions[0]->windows.size()); |
| 1416 std::vector<std::vector<SessionID::id_type> > session_reference; | 1337 std::vector<std::vector<SessionID::id_type>> session_reference; |
| 1417 session_reference.push_back(tab_list1); | 1338 session_reference.push_back(tab_list1); |
| 1418 helper()->VerifySyncedSession(tag, session_reference, *(foreign_sessions[0])); | 1339 helper()->VerifySyncedSession(tag, session_reference, *(foreign_sessions[0])); |
| 1419 } | 1340 } |
| 1420 | 1341 |
| 1421 // Tests that the SessionsSyncManager can handle a remote client deleting | 1342 // Tests that the SessionsSyncManager can handle a remote client deleting |
| 1422 // sync nodes that belong to this local session. | 1343 // sync nodes that belong to this local session. |
| 1423 TEST_F(SessionsSyncManagerTest, ProcessRemoteDeleteOfLocalSession) { | 1344 TEST_F(SessionsSyncManagerTest, ProcessRemoteDeleteOfLocalSession) { |
| 1424 syncer::SyncChangeList out; | 1345 SessionID::id_type window_id = AddWindow()->GetSessionId(); |
| 1425 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); | 1346 SyncChangeList out; |
| 1347 InitWithSyncDataTakeOutput(SyncDataList(), &out); | |
| 1426 ASSERT_EQ(2U, out.size()); | 1348 ASSERT_EQ(2U, out.size()); |
| 1427 sync_pb::EntitySpecifics entity(out[0].sync_data().GetSpecifics()); | 1349 |
| 1428 SyncData d = CreateRemoteData(entity); | 1350 SyncChangeList changes; |
| 1429 SetSyncData(syncer::SyncDataList(&d, &d + 1)); | 1351 changes.push_back(MakeRemoteChange( |
| 1352 out[1].sync_data().GetSpecifics().session(), SyncChange::ACTION_DELETE)); | |
| 1430 out.clear(); | 1353 out.clear(); |
| 1431 | |
| 1432 syncer::SyncChangeList changes; | |
| 1433 changes.push_back( | |
| 1434 MakeRemoteChange(entity.session(), SyncChange::ACTION_DELETE)); | |
| 1435 manager()->ProcessSyncChanges(FROM_HERE, changes); | 1354 manager()->ProcessSyncChanges(FROM_HERE, changes); |
| 1436 EXPECT_TRUE(manager()->local_tab_pool_out_of_sync_); | 1355 EXPECT_TRUE(manager()->local_tab_pool_out_of_sync_); |
| 1437 EXPECT_TRUE(out.empty()); // ChangeProcessor shouldn't see any activity. | 1356 EXPECT_TRUE(out.empty()); // ChangeProcessor shouldn't see any activity. |
| 1438 | 1357 |
| 1439 // This should trigger repair of the TabNodePool. | 1358 // This should trigger repair of the TabNodePool. |
| 1440 const GURL foo1("http://foo/1"); | 1359 AddTab(window_id, kFoo1); |
| 1441 AddTab(browser(), foo1); | |
| 1442 EXPECT_FALSE(manager()->local_tab_pool_out_of_sync_); | 1360 EXPECT_FALSE(manager()->local_tab_pool_out_of_sync_); |
| 1443 | 1361 |
| 1444 // AddTab triggers two notifications, one for the tab insertion and one for | 1362 // Rebuilding associations will trigger an initial header add and update, |
| 1445 // committing the NavigationEntry. The first notification results in a tab | 1363 // 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 | 1364 // new tab. In total, that means four changes. |
| 1447 // notification triggers association of the tab, and the subsequent window | 1365 ASSERT_TRUE( |
| 1448 // update. So we should see 4 changes at the SyncChangeProcessor. | 1366 ChangeTypeMatches(out, {SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE, |
| 1449 ASSERT_EQ(4U, out.size()); | 1367 SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE, |
| 1450 | 1368 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 | 1369 |
| 1463 // Verify the actual content. | 1370 // Verify the actual content. |
| 1464 const sync_pb::SessionHeader& session_header = | 1371 VerifyLocalTabChange(out[3], 1, kFoo1); |
| 1465 out[3].sync_data().GetSpecifics().session().header(); | 1372 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 | 1373 |
| 1477 // Verify TabLinks. | 1374 // Verify TabLinks. |
| 1478 SessionsSyncManager::TabLinksMap tab_map = manager()->local_tab_map_; | 1375 SessionsSyncManager::TabLinksMap tab_map = manager()->local_tab_map_; |
| 1479 ASSERT_EQ(1U, tab_map.size()); | 1376 ASSERT_EQ(1U, tab_map.size()); |
| 1480 int tab_id = out[2].sync_data().GetSpecifics().session().tab().tab_id(); | 1377 int tab_node_id = out[3].sync_data().GetSpecifics().session().tab_node_id(); |
| 1378 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()); | 1379 EXPECT_EQ(tab_node_id, tab_map.find(tab_id)->second->tab_node_id()); |
| 1482 } | 1380 } |
| 1483 | 1381 |
| 1484 // Test that receiving a session delete from sync removes the session | 1382 // Test that receiving a session delete from sync removes the session |
| 1485 // from tracking. | 1383 // from tracking. |
| 1486 TEST_F(SessionsSyncManagerTest, ProcessForeignDelete) { | 1384 TEST_F(SessionsSyncManagerTest, ProcessForeignDelete) { |
| 1487 InitWithNoSyncData(); | 1385 InitWithNoSyncData(); |
| 1488 SessionID::id_type n[] = {5}; | |
| 1489 std::vector<sync_pb::SessionSpecifics> tabs1; | 1386 std::vector<sync_pb::SessionSpecifics> tabs1; |
| 1490 std::vector<SessionID::id_type> tab_list(n, n + arraysize(n)); | 1387 std::vector<SessionID::id_type> tab_list1(std::begin(kTabIds1), |
| 1491 sync_pb::SessionSpecifics meta(helper()->BuildForeignSession( | 1388 std::end(kTabIds1)); |
| 1492 "tag1", tab_list, &tabs1)); | 1389 sync_pb::SessionSpecifics meta( |
| 1390 helper()->BuildForeignSession("tag1", tab_list1, &tabs1)); | |
| 1493 | 1391 |
| 1494 syncer::SyncChangeList changes; | 1392 SyncChangeList changes; |
| 1495 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_ADD)); | 1393 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_ADD)); |
| 1496 AddTabsToChangeList(tabs1, SyncChange::ACTION_ADD, &changes); | 1394 AddTabsToChangeList(tabs1, SyncChange::ACTION_ADD, &changes); |
| 1497 manager()->ProcessSyncChanges(FROM_HERE, changes); | 1395 manager()->ProcessSyncChanges(FROM_HERE, changes); |
| 1498 | 1396 |
| 1499 std::vector<const SyncedSession*> foreign_sessions; | 1397 std::vector<const SyncedSession*> foreign_sessions; |
| 1500 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1398 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1501 ASSERT_EQ(1U, foreign_sessions.size()); | 1399 ASSERT_EQ(1U, foreign_sessions.size()); |
| 1502 | 1400 |
| 1503 changes.clear(); | 1401 changes.clear(); |
| 1504 foreign_sessions.clear(); | 1402 foreign_sessions.clear(); |
| 1505 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_DELETE)); | 1403 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_DELETE)); |
| 1506 manager()->ProcessSyncChanges(FROM_HERE, changes); | 1404 manager()->ProcessSyncChanges(FROM_HERE, changes); |
| 1507 | 1405 |
| 1508 EXPECT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1406 EXPECT_FALSE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1509 } | 1407 } |
| 1510 | 1408 |
| 1511 TEST_F(SessionsSyncManagerTest, ProcessForeignDeleteTabs) { | 1409 TEST_F(SessionsSyncManagerTest, ProcessForeignDeleteTabs) { |
| 1512 syncer::SyncDataList foreign_data; | 1410 SyncDataList foreign_data; |
| 1513 base::Time stale_mtime = base::Time::Now() - base::TimeDelta::FromDays(15); | 1411 base::Time stale_mtime = base::Time::Now() - base::TimeDelta::FromDays(15); |
| 1514 std::string session_tag = "tag1"; | 1412 std::string session_tag = "tag1"; |
| 1515 | 1413 |
| 1516 // 0 will not have ownership changed. | 1414 // 0 will not have ownership changed. |
| 1517 // 1 will not be updated, but header will stop owning. | 1415 // 1 will not be updated, but header will stop owning. |
| 1518 // 2 will be deleted before header stops owning. | 1416 // 2 will be deleted before header stops owning. |
| 1519 // 3 will be deleted after header stops owning. | 1417 // 3 will be deleted after header stops owning. |
| 1520 // 4 will be deleted before header update, but header will still try to own. | 1418 // 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. | 1419 // 5 will be deleted after header update, but header will still try to own. |
| 1522 // 6 starts orphaned and then deleted before header update. | 1420 // 6 starts orphaned and then deleted before header update. |
| 1523 // 7 starts orphaned and then deleted after header update. | 1421 // 7 starts orphaned and then deleted after header update. |
| 1524 SessionID::id_type tab_ids[] = {0, 1, 2, 3, 4, 5}; | 1422 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; | 1423 std::vector<sync_pb::SessionSpecifics> tabs; |
| 1528 sync_pb::SessionSpecifics meta( | 1424 sync_pb::SessionSpecifics meta( |
| 1529 helper()->BuildForeignSession(session_tag, tab_list, &tabs)); | 1425 helper()->BuildForeignSession(session_tag, tab_list, &tabs)); |
| 1530 AddToSyncDataList(meta, &foreign_data, stale_mtime); | 1426 AddToSyncDataList(meta, &foreign_data, stale_mtime); |
| 1531 AddTabsToSyncDataList(tabs, &foreign_data); | 1427 AddTabsToSyncDataList(tabs, &foreign_data); |
| 1532 sync_pb::SessionSpecifics orphan6; | 1428 sync_pb::SessionSpecifics orphan6; |
| 1533 helper()->BuildTabSpecifics(session_tag, 0, 6, &orphan6); | 1429 helper()->BuildTabSpecifics(session_tag, 0, 6, &orphan6); |
| 1534 AddToSyncDataList(orphan6, &foreign_data, stale_mtime); | 1430 AddToSyncDataList(orphan6, &foreign_data, stale_mtime); |
| 1535 sync_pb::SessionSpecifics orphan7; | 1431 sync_pb::SessionSpecifics orphan7; |
| 1536 helper()->BuildTabSpecifics(session_tag, 0, 7, &orphan7); | 1432 helper()->BuildTabSpecifics(session_tag, 0, 7, &orphan7); |
| 1537 AddToSyncDataList(orphan7, &foreign_data, stale_mtime); | 1433 AddToSyncDataList(orphan7, &foreign_data, stale_mtime); |
| 1538 | 1434 |
| 1539 syncer::SyncChangeList output; | 1435 AddWindow(); |
| 1436 SyncChangeList output; | |
| 1540 InitWithSyncDataTakeOutput(foreign_data, &output); | 1437 InitWithSyncDataTakeOutput(foreign_data, &output); |
| 1541 ASSERT_EQ(2U, output.size()); | 1438 ASSERT_EQ(2U, output.size()); |
| 1542 output.clear(); | 1439 output.clear(); |
| 1543 | 1440 |
| 1544 SessionID::id_type update_ids[] = {0, 4, 5}; | 1441 SessionID::id_type update_ids[] = {0, 4, 5}; |
| 1545 std::vector<SessionID::id_type> update_list( | 1442 std::vector<SessionID::id_type> update_list( |
| 1546 update_ids, update_ids + arraysize(update_ids)); | 1443 update_ids, update_ids + arraysize(update_ids)); |
| 1547 sync_pb::SessionWindow* window = meta.mutable_header()->mutable_window(0); | 1444 sync_pb::SessionWindow* window = meta.mutable_header()->mutable_window(0); |
| 1548 window->clear_tab(); | 1445 window->clear_tab(); |
| 1549 for (int i : update_ids) { | 1446 for (int i : update_ids) { |
| 1550 window->add_tab(i); | 1447 window->add_tab(i); |
| 1551 } | 1448 } |
| 1552 | 1449 |
| 1553 syncer::SyncChangeList changes; | 1450 SyncChangeList changes; |
| 1554 changes.push_back(MakeRemoteChange(tabs[2], SyncChange::ACTION_DELETE)); | 1451 changes.push_back(MakeRemoteChange(tabs[2], SyncChange::ACTION_DELETE)); |
| 1555 changes.push_back(MakeRemoteChange(tabs[4], SyncChange::ACTION_DELETE)); | 1452 changes.push_back(MakeRemoteChange(tabs[4], SyncChange::ACTION_DELETE)); |
| 1556 changes.push_back(MakeRemoteChange(orphan6, SyncChange::ACTION_DELETE)); | 1453 changes.push_back(MakeRemoteChange(orphan6, SyncChange::ACTION_DELETE)); |
| 1557 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_UPDATE)); | 1454 changes.push_back(MakeRemoteChange(meta, SyncChange::ACTION_UPDATE)); |
| 1558 changes.push_back(MakeRemoteChange(tabs[3], SyncChange::ACTION_DELETE)); | 1455 changes.push_back(MakeRemoteChange(tabs[3], SyncChange::ACTION_DELETE)); |
| 1559 changes.push_back(MakeRemoteChange(tabs[5], SyncChange::ACTION_DELETE)); | 1456 changes.push_back(MakeRemoteChange(tabs[5], SyncChange::ACTION_DELETE)); |
| 1560 changes.push_back(MakeRemoteChange(orphan7, SyncChange::ACTION_DELETE)); | 1457 changes.push_back(MakeRemoteChange(orphan7, SyncChange::ACTION_DELETE)); |
| 1561 manager()->ProcessSyncChanges(FROM_HERE, changes); | 1458 manager()->ProcessSyncChanges(FROM_HERE, changes); |
| 1562 | 1459 |
| 1563 std::vector<const SyncedSession*> foreign_sessions; | 1460 std::vector<const SyncedSession*> foreign_sessions; |
| 1564 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); | 1461 ASSERT_TRUE(manager()->GetAllForeignSessions(&foreign_sessions)); |
| 1565 ASSERT_EQ(1U, foreign_sessions.size()); | 1462 ASSERT_EQ(1U, foreign_sessions.size()); |
| 1566 std::vector<std::vector<SessionID::id_type>> session_reference; | 1463 std::vector<std::vector<SessionID::id_type>> session_reference; |
| 1567 session_reference.push_back(update_list); | 1464 session_reference.push_back(update_list); |
| 1568 helper()->VerifySyncedSession(session_tag, session_reference, | 1465 helper()->VerifySyncedSession(session_tag, session_reference, |
| 1569 *(foreign_sessions[0])); | 1466 *(foreign_sessions[0])); |
| 1570 | 1467 |
| 1571 // Everything except for session, tab0, and tab1 will have no node_id, and | 1468 // Everything except for session, tab0, and tab1 will have no node_id, and |
| 1572 // should get skipped by garbage collection. | 1469 // should get skipped by garbage collection. |
| 1573 manager()->DoGarbageCollection(); | 1470 manager()->DoGarbageCollection(); |
| 1574 ASSERT_EQ(3U, output.size()); | 1471 ASSERT_EQ(3U, output.size()); |
| 1575 } | 1472 } |
| 1576 | 1473 |
| 1577 TEST_F(SessionsSyncManagerTest, ProcessForeignDeleteTabsWithShadowing) { | 1474 TEST_F(SessionsSyncManagerTest, ProcessForeignDeleteTabsWithShadowing) { |
| 1578 syncer::SyncDataList foreign_data; | 1475 SyncDataList foreign_data; |
| 1579 base::Time stale_mtime = base::Time::Now() - base::TimeDelta::FromDays(16); | 1476 base::Time stale_mtime = base::Time::Now() - base::TimeDelta::FromDays(16); |
| 1580 std::string session_tag = "tag1"; | 1477 std::string session_tag = "tag1"; |
| 1581 | 1478 |
| 1582 // Add several tabs that shadow eachother, in that they share tab_ids. They | 1479 // Add several tabs that shadow eachother, in that they share tab_ids. They |
| 1583 // will, thanks to the helper, have unique tab_node_ids. | 1480 // will, thanks to the helper, have unique tab_node_ids. |
| 1584 sync_pb::SessionSpecifics tab1A; | 1481 sync_pb::SessionSpecifics tab1A; |
| 1585 helper()->BuildTabSpecifics(session_tag, 0, 1, &tab1A); | 1482 helper()->BuildTabSpecifics(session_tag, 0, 1, &tab1A); |
| 1586 AddToSyncDataList(tab1A, &foreign_data, | 1483 AddToSyncDataList(tab1A, &foreign_data, |
| 1587 stale_mtime + base::TimeDelta::FromMinutes(1)); | 1484 stale_mtime + base::TimeDelta::FromMinutes(1)); |
| 1588 | 1485 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 1602 | 1499 |
| 1603 sync_pb::SessionSpecifics tab2B; | 1500 sync_pb::SessionSpecifics tab2B; |
| 1604 helper()->BuildTabSpecifics(session_tag, 0, 2, &tab2B); | 1501 helper()->BuildTabSpecifics(session_tag, 0, 2, &tab2B); |
| 1605 AddToSyncDataList(tab2B, &foreign_data, | 1502 AddToSyncDataList(tab2B, &foreign_data, |
| 1606 stale_mtime + base::TimeDelta::FromMinutes(2)); | 1503 stale_mtime + base::TimeDelta::FromMinutes(2)); |
| 1607 | 1504 |
| 1608 sync_pb::SessionSpecifics tab2C; | 1505 sync_pb::SessionSpecifics tab2C; |
| 1609 helper()->BuildTabSpecifics(session_tag, 0, 2, &tab2C); | 1506 helper()->BuildTabSpecifics(session_tag, 0, 2, &tab2C); |
| 1610 AddToSyncDataList(tab2C, &foreign_data, stale_mtime); | 1507 AddToSyncDataList(tab2C, &foreign_data, stale_mtime); |
| 1611 | 1508 |
| 1612 syncer::SyncChangeList output; | 1509 AddWindow(); |
| 1510 SyncChangeList output; | |
| 1613 InitWithSyncDataTakeOutput(foreign_data, &output); | 1511 InitWithSyncDataTakeOutput(foreign_data, &output); |
| 1614 ASSERT_EQ(2U, output.size()); | 1512 ASSERT_EQ(2U, output.size()); |
| 1615 output.clear(); | 1513 output.clear(); |
| 1616 | 1514 |
| 1617 // Verify that cleanup post-merge cleanup correctly removes all tabs objects. | 1515 // Verify that cleanup post-merge cleanup correctly removes all tabs objects. |
| 1618 const sessions::SessionTab* tab; | 1516 const sessions::SessionTab* tab; |
| 1619 ASSERT_FALSE( | 1517 ASSERT_FALSE( |
| 1620 manager()->session_tracker_.LookupSessionTab(session_tag, 1, &tab)); | 1518 manager()->session_tracker_.LookupSessionTab(session_tag, 1, &tab)); |
| 1621 ASSERT_FALSE( | 1519 ASSERT_FALSE( |
| 1622 manager()->session_tracker_.LookupSessionTab(session_tag, 2, &tab)); | 1520 manager()->session_tracker_.LookupSessionTab(session_tag, 2, &tab)); |
| 1623 | 1521 |
| 1624 std::set<int> tab_node_ids; | 1522 std::set<int> tab_node_ids; |
| 1625 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); | 1523 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); |
| 1626 EXPECT_EQ(6U, tab_node_ids.size()); | 1524 EXPECT_EQ(6U, tab_node_ids.size()); |
| 1627 EXPECT_TRUE(tab_node_ids.find(tab1A.tab_node_id()) != tab_node_ids.end()); | 1525 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()); | 1526 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()); | 1527 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()); | 1528 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()); | 1529 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()); | 1530 EXPECT_TRUE(tab_node_ids.find(tab2C.tab_node_id()) != tab_node_ids.end()); |
| 1633 | 1531 |
| 1634 syncer::SyncChangeList changes; | 1532 SyncChangeList changes; |
| 1635 changes.push_back(MakeRemoteChange(tab1A, SyncChange::ACTION_DELETE)); | 1533 changes.push_back(MakeRemoteChange(tab1A, SyncChange::ACTION_DELETE)); |
| 1636 changes.push_back(MakeRemoteChange(tab1B, SyncChange::ACTION_DELETE)); | 1534 changes.push_back(MakeRemoteChange(tab1B, SyncChange::ACTION_DELETE)); |
| 1637 changes.push_back(MakeRemoteChange(tab2C, SyncChange::ACTION_DELETE)); | 1535 changes.push_back(MakeRemoteChange(tab2C, SyncChange::ACTION_DELETE)); |
| 1638 manager()->ProcessSyncChanges(FROM_HERE, changes); | 1536 manager()->ProcessSyncChanges(FROM_HERE, changes); |
| 1639 | 1537 |
| 1640 tab_node_ids.clear(); | 1538 tab_node_ids.clear(); |
| 1641 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); | 1539 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); |
| 1642 EXPECT_EQ(3U, tab_node_ids.size()); | 1540 EXPECT_EQ(3U, tab_node_ids.size()); |
| 1643 EXPECT_TRUE(tab_node_ids.find(tab1C.tab_node_id()) != tab_node_ids.end()); | 1541 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()); | 1542 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()); | 1543 EXPECT_TRUE(tab_node_ids.find(tab2B.tab_node_id()) != tab_node_ids.end()); |
| 1646 | 1544 |
| 1647 manager()->DoGarbageCollection(); | 1545 manager()->DoGarbageCollection(); |
| 1648 ASSERT_EQ(3U, output.size()); | 1546 ASSERT_EQ(3U, output.size()); |
| 1649 } | 1547 } |
| 1650 | 1548 |
| 1651 TEST_F(SessionsSyncManagerTest, ProcessForeignDeleteTabsWithReusedNodeIds) { | 1549 TEST_F(SessionsSyncManagerTest, ProcessForeignDeleteTabsWithReusedNodeIds) { |
| 1652 syncer::SyncDataList foreign_data; | 1550 SyncDataList foreign_data; |
| 1653 base::Time stale_mtime = base::Time::Now() - base::TimeDelta::FromDays(16); | 1551 base::Time stale_mtime = base::Time::Now() - base::TimeDelta::FromDays(16); |
| 1654 std::string session_tag = "tag1"; | 1552 std::string session_tag = "tag1"; |
| 1655 int tab_node_id_shared = 13; | 1553 int tab_node_id_shared = 13; |
| 1656 int tab_node_id_unique = 14; | 1554 int tab_node_id_unique = 14; |
| 1657 | 1555 |
| 1658 sync_pb::SessionSpecifics tab1A; | 1556 sync_pb::SessionSpecifics tab1A; |
| 1659 helper()->BuildTabSpecifics(session_tag, 0, 1, tab_node_id_shared, &tab1A); | 1557 helper()->BuildTabSpecifics(session_tag, 0, 1, tab_node_id_shared, &tab1A); |
| 1660 AddToSyncDataList(tab1A, &foreign_data, | 1558 AddToSyncDataList(tab1A, &foreign_data, |
| 1661 stale_mtime + base::TimeDelta::FromMinutes(1)); | 1559 stale_mtime + base::TimeDelta::FromMinutes(1)); |
| 1662 | 1560 |
| 1663 sync_pb::SessionSpecifics tab1B; | 1561 sync_pb::SessionSpecifics tab1B; |
| 1664 helper()->BuildTabSpecifics(session_tag, 0, 1, tab_node_id_unique, &tab1B); | 1562 helper()->BuildTabSpecifics(session_tag, 0, 1, tab_node_id_unique, &tab1B); |
| 1665 AddToSyncDataList(tab1B, &foreign_data, | 1563 AddToSyncDataList(tab1B, &foreign_data, |
| 1666 stale_mtime + base::TimeDelta::FromMinutes(2)); | 1564 stale_mtime + base::TimeDelta::FromMinutes(2)); |
| 1667 | 1565 |
| 1668 sync_pb::SessionSpecifics tab2A; | 1566 sync_pb::SessionSpecifics tab2A; |
| 1669 helper()->BuildTabSpecifics(session_tag, 0, 2, tab_node_id_shared, &tab2A); | 1567 helper()->BuildTabSpecifics(session_tag, 0, 2, tab_node_id_shared, &tab2A); |
| 1670 AddToSyncDataList(tab2A, &foreign_data, | 1568 AddToSyncDataList(tab2A, &foreign_data, |
| 1671 stale_mtime + base::TimeDelta::FromMinutes(1)); | 1569 stale_mtime + base::TimeDelta::FromMinutes(1)); |
| 1672 | 1570 |
| 1673 syncer::SyncChangeList output; | 1571 AddWindow(); |
| 1572 SyncChangeList output; | |
| 1674 InitWithSyncDataTakeOutput(foreign_data, &output); | 1573 InitWithSyncDataTakeOutput(foreign_data, &output); |
| 1675 ASSERT_EQ(2U, output.size()); | 1574 ASSERT_EQ(2U, output.size()); |
| 1676 output.clear(); | 1575 output.clear(); |
| 1677 | 1576 |
| 1678 std::set<int> tab_node_ids; | 1577 std::set<int> tab_node_ids; |
| 1679 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); | 1578 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); |
| 1680 EXPECT_EQ(2U, tab_node_ids.size()); | 1579 EXPECT_EQ(2U, tab_node_ids.size()); |
| 1681 EXPECT_TRUE(tab_node_ids.find(tab_node_id_shared) != tab_node_ids.end()); | 1580 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()); | 1581 EXPECT_TRUE(tab_node_ids.find(tab_node_id_unique) != tab_node_ids.end()); |
| 1683 | 1582 |
| 1684 syncer::SyncChangeList changes; | 1583 SyncChangeList changes; |
| 1685 changes.push_back(MakeRemoteChange(tab1A, SyncChange::ACTION_DELETE)); | 1584 changes.push_back(MakeRemoteChange(tab1A, SyncChange::ACTION_DELETE)); |
| 1686 manager()->ProcessSyncChanges(FROM_HERE, changes); | 1585 manager()->ProcessSyncChanges(FROM_HERE, changes); |
| 1687 | 1586 |
| 1688 tab_node_ids.clear(); | 1587 tab_node_ids.clear(); |
| 1689 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); | 1588 manager()->session_tracker_.LookupTabNodeIds(session_tag, &tab_node_ids); |
| 1690 EXPECT_EQ(1U, tab_node_ids.size()); | 1589 EXPECT_EQ(1U, tab_node_ids.size()); |
| 1691 EXPECT_TRUE(tab_node_ids.find(tab_node_id_unique) != tab_node_ids.end()); | 1590 EXPECT_TRUE(tab_node_ids.find(tab_node_id_unique) != tab_node_ids.end()); |
| 1692 | 1591 |
| 1693 manager()->DoGarbageCollection(); | 1592 manager()->DoGarbageCollection(); |
| 1694 EXPECT_EQ(1U, output.size()); | 1593 EXPECT_EQ(1U, output.size()); |
| 1695 } | 1594 } |
| 1696 | 1595 |
| 1697 // TODO(shashishekhar): "Move this to TabNodePool unittests." | 1596 TEST_F(SessionsSyncManagerTest, AssociationReusesNodes) { |
| 1698 TEST_F(SessionsSyncManagerTest, SaveUnassociatedNodesForReassociation) { | 1597 SyncChangeList changes; |
| 1699 syncer::SyncChangeList changes; | 1598 AddTab(AddWindow()->GetSessionId(), kFoo1); |
| 1599 InitWithSyncDataTakeOutput(SyncDataList(), &changes); | |
| 1600 ASSERT_TRUE(ChangeTypeMatches( | |
| 1601 changes, {SyncChange::ACTION_ADD, SyncChange::ACTION_ADD, | |
| 1602 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE})); | |
| 1603 ASSERT_TRUE(changes[2].sync_data().GetSpecifics().session().has_tab()); | |
| 1604 int tab_node_id = | |
| 1605 changes[1].sync_data().GetSpecifics().session().tab_node_id(); | |
| 1606 | |
| 1607 // Pass back the previous tab and header nodes at association, along with a | |
| 1608 // second tab node (with a rewritten tab node id). | |
| 1609 SyncDataList in; | |
| 1610 in.push_back( | |
| 1611 CreateRemoteData(changes[3].sync_data().GetSpecifics())); // Header node. | |
| 1612 sync_pb::SessionSpecifics new_tab( | |
| 1613 changes[2].sync_data().GetSpecifics().session()); | |
| 1614 new_tab.set_tab_node_id(tab_node_id + 1); | |
| 1615 in.push_back(CreateRemoteData(new_tab)); // New tab node. | |
| 1616 in.push_back(CreateRemoteData( | |
| 1617 changes[2].sync_data().GetSpecifics())); // Old tab node. | |
| 1618 changes.clear(); | |
| 1619 | |
| 1620 // Reassociate (with the same single tab/window open). | |
| 1621 manager()->StopSyncing(syncer::SESSIONS); | |
| 1622 InitWithSyncDataTakeOutput(in, &changes); | |
| 1623 | |
| 1624 // No tab entities should be deleted. The original (lower) tab node id should | |
| 1625 // be reused for association. | |
| 1626 FilterOutLocalHeaderChanges(&changes); | |
| 1627 ASSERT_TRUE(ChangeTypeMatches(changes, {SyncChange::ACTION_UPDATE})); | |
| 1628 VerifyLocalTabChange(changes[0], 1, kFoo1); | |
| 1629 EXPECT_EQ(tab_node_id, | |
| 1630 changes[0].sync_data().GetSpecifics().session().tab_node_id()); | |
| 1631 } | |
| 1632 | |
| 1633 // Ensure that the merge process deletes a tab node without a tab id. | |
| 1634 TEST_F(SessionsSyncManagerTest, MergeDeletesTabMissingTabId) { | |
| 1635 SyncChangeList changes; | |
| 1700 InitWithNoSyncData(); | 1636 InitWithNoSyncData(); |
| 1701 | 1637 |
| 1702 std::string local_tag = manager()->current_machine_tag(); | 1638 std::string local_tag = manager()->current_machine_tag(); |
| 1703 // Create a free node and then dissassociate sessions so that it ends up | 1639 int tab_node_id = 0; |
| 1704 // unassociated. | 1640 sync_pb::SessionSpecifics specifics; |
| 1705 manager()->local_tab_pool_.GetFreeTabNode(&changes); | 1641 specifics.set_session_tag(local_tag); |
| 1706 | 1642 specifics.set_tab_node_id(tab_node_id); |
| 1707 // Update the tab_id of the node, so that it is considered a valid | 1643 SyncDataList in = {CreateRemoteData(specifics)}; |
|
skym
2017/02/22 22:25:11
Might be able to inline this without causing a lin
Nicolas Zea
2017/02/22 22:58:40
Done.
| |
| 1708 // unassociated node otherwise it will be mistaken for a corrupted node and | 1644 manager()->StopSyncing(syncer::SESSIONS); |
| 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); | 1645 InitWithSyncDataTakeOutput(in, &changes); |
| 1739 EXPECT_EQ(1U, FilterOutLocalHeaderChanges(&changes)->size()); | 1646 EXPECT_EQ(1U, FilterOutLocalHeaderChanges(&changes)->size()); |
| 1740 EXPECT_EQ(SyncChange::ACTION_DELETE, changes[0].change_type()); | 1647 EXPECT_EQ(SyncChange::ACTION_DELETE, changes[0].change_type()); |
| 1741 EXPECT_EQ(TabNodePool::TabIdToTag(local_tag, tab_node_id), | 1648 EXPECT_EQ(TabNodeIdToTag(local_tag, tab_node_id), |
| 1742 syncer::SyncDataLocal(changes[0].sync_data()).GetTag()); | 1649 SyncDataLocal(changes[0].sync_data()).GetTag()); |
| 1743 } | 1650 } |
| 1744 | 1651 |
| 1745 // Verifies that we drop both headers and tabs during merge if their stored tag | 1652 // 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 | 1653 // hash doesn't match a computer tag hash. This mitigates potential failures |
| 1747 // while cleaning up bad foreign data, see crbug.com/604657. | 1654 // while cleaning up bad foreign data, see crbug.com/604657. |
| 1748 TEST_F(SessionsSyncManagerTest, MergeDeletesBadHash) { | 1655 TEST_F(SessionsSyncManagerTest, MergeDeletesBadHash) { |
| 1749 syncer::SyncDataList foreign_data; | 1656 SyncDataList foreign_data; |
| 1750 std::vector<SessionID::id_type> empty_ids; | 1657 std::vector<SessionID::id_type> empty_ids; |
| 1751 std::vector<sync_pb::SessionSpecifics> empty_tabs; | 1658 std::vector<sync_pb::SessionSpecifics> empty_tabs; |
| 1752 sync_pb::EntitySpecifics entity; | 1659 sync_pb::EntitySpecifics entity; |
| 1753 | 1660 |
| 1754 const std::string good_header_tag = "good_header_tag"; | 1661 const std::string good_header_tag = "good_header_tag"; |
| 1755 sync_pb::SessionSpecifics good_header( | 1662 sync_pb::SessionSpecifics good_header( |
| 1756 helper()->BuildForeignSession(good_header_tag, empty_ids, &empty_tabs)); | 1663 helper()->BuildForeignSession(good_header_tag, empty_ids, &empty_tabs)); |
| 1757 foreign_data.push_back(CreateRemoteData(good_header)); | 1664 foreign_data.push_back(CreateRemoteData(good_header)); |
| 1758 | 1665 |
| 1759 const std::string bad_header_tag = "bad_header_tag"; | 1666 const std::string bad_header_tag = "bad_header_tag"; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1770 foreign_data.push_back(CreateRemoteData(good_tab)); | 1677 foreign_data.push_back(CreateRemoteData(good_tab)); |
| 1771 | 1678 |
| 1772 const std::string bad_tab_tag = "bad_tab_tag"; | 1679 const std::string bad_tab_tag = "bad_tab_tag"; |
| 1773 sync_pb::SessionSpecifics bad_tab; | 1680 sync_pb::SessionSpecifics bad_tab; |
| 1774 helper()->BuildTabSpecifics(bad_tab_tag, 0, 2, &bad_tab); | 1681 helper()->BuildTabSpecifics(bad_tab_tag, 0, 2, &bad_tab); |
| 1775 entity.mutable_session()->CopyFrom(bad_tab); | 1682 entity.mutable_session()->CopyFrom(bad_tab); |
| 1776 foreign_data.push_back(SyncData::CreateRemoteData( | 1683 foreign_data.push_back(SyncData::CreateRemoteData( |
| 1777 1, entity, base::Time(), syncer::AttachmentIdList(), | 1684 1, entity, base::Time(), syncer::AttachmentIdList(), |
| 1778 syncer::AttachmentServiceProxyForTest::Create(), "bad_tab_tag_hash")); | 1685 syncer::AttachmentServiceProxyForTest::Create(), "bad_tab_tag_hash")); |
| 1779 | 1686 |
| 1780 syncer::SyncChangeList output; | 1687 SyncChangeList output; |
| 1781 InitWithSyncDataTakeOutput(foreign_data, &output); | 1688 InitWithSyncDataTakeOutput(foreign_data, &output); |
| 1782 ASSERT_EQ(2U, FilterOutLocalHeaderChanges(&output)->size()); | 1689 ASSERT_EQ(2U, FilterOutLocalHeaderChanges(&output)->size()); |
| 1783 ExpectAllOfChangesType(output, SyncChange::ACTION_DELETE); | 1690 ASSERT_TRUE(AllOfChangesAreType(output, SyncChange::ACTION_DELETE)); |
| 1784 EXPECT_EQ(1, CountIfTagMatches(output, bad_header_tag)); | 1691 EXPECT_EQ(1U, CountIfTagMatches(output, bad_header_tag)); |
| 1785 EXPECT_EQ(1, CountIfTagMatches(output, bad_tab_tag)); | 1692 EXPECT_EQ(1U, CountIfTagMatches(output, bad_tab_tag)); |
| 1786 | 1693 |
| 1787 std::vector<const SyncedSession*> sessions; | 1694 std::vector<const SyncedSession*> sessions; |
| 1788 manager()->session_tracker_.LookupAllForeignSessions( | 1695 manager()->session_tracker_.LookupAllForeignSessions( |
| 1789 &sessions, SyncedSessionTracker::RAW); | 1696 &sessions, SyncedSessionTracker::RAW); |
| 1790 ASSERT_EQ(2U, sessions.size()); | 1697 ASSERT_EQ(2U, sessions.size()); |
| 1791 EXPECT_EQ(1, CountIfTagMatches(sessions, good_header_tag)); | 1698 EXPECT_EQ(1U, CountIfTagMatches(sessions, good_header_tag)); |
| 1792 EXPECT_EQ(1, CountIfTagMatches(sessions, good_tag_tab)); | 1699 EXPECT_EQ(1U, CountIfTagMatches(sessions, good_tag_tab)); |
| 1793 } | 1700 } |
| 1794 | 1701 |
| 1795 // Test that things work if a tab is initially ignored. | 1702 // Test that things work if a tab is initially ignored. |
| 1796 TEST_F(SessionsSyncManagerTest, AssociateWindowsDontReloadTabs) { | 1703 TEST_F(SessionsSyncManagerTest, AssociateWindowsDontReloadTabs) { |
| 1797 syncer::SyncChangeList out; | 1704 SyncChangeList out; |
| 1798 // Go to a URL that is ignored by session syncing. | 1705 // Go to a URL that is ignored by session syncing. |
| 1799 AddTab(browser(), GURL("chrome://preferences/")); | 1706 TestSyncedTabDelegate* tab = |
| 1800 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); | 1707 AddTab(AddWindow()->GetSessionId(), "chrome://preferences/"); |
| 1801 ASSERT_EQ(2U, out.size()); // Header add and update. | 1708 InitWithSyncDataTakeOutput(SyncDataList(), &out); |
| 1802 EXPECT_EQ( | 1709 ASSERT_TRUE(ChangeTypeMatches( |
| 1803 0, | 1710 out, {SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE})); |
| 1804 out[1].sync_data().GetSpecifics().session().header().window_size()); | 1711 VerifyLocalHeaderChange(out[1], 0, 0); |
| 1805 out.clear(); | 1712 out.clear(); |
| 1806 | 1713 |
| 1807 // Go to a sync-interesting URL. | 1714 // Go to a sync-interesting URL. |
| 1808 NavigateAndCommitActiveTab(GURL("http://foo2")); | 1715 NavigateTab(tab, kFoo1); |
| 1809 | 1716 |
| 1810 EXPECT_EQ(3U, out.size()); // Tab add, update, and header update. | 1717 // The tab should be created/updated, coupled with a header update. |
| 1811 | 1718 ASSERT_TRUE( |
| 1812 EXPECT_TRUE( | 1719 ChangeTypeMatches(out, {SyncChange::ACTION_ADD, SyncChange::ACTION_UPDATE, |
| 1813 base::StartsWith(syncer::SyncDataLocal(out[0].sync_data()).GetTag(), | 1720 SyncChange::ACTION_UPDATE})); |
| 1814 manager()->current_machine_tag(), | 1721 VerifyLocalTabChange(out[1], 2, kFoo1); |
| 1815 base::CompareCase::SENSITIVE)); | 1722 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 } | 1723 } |
| 1841 | 1724 |
| 1842 // Tests that the SyncSessionManager responds to local tab events properly. | 1725 // Tests that the SyncSessionManager responds to local tab events properly. |
| 1843 TEST_F(SessionsSyncManagerTest, OnLocalTabModified) { | 1726 TEST_F(SessionsSyncManagerTest, OnLocalTabModified) { |
| 1844 syncer::SyncChangeList out; | 1727 SyncChangeList out; |
| 1845 // Init with no local data, relies on MergeLocalSessionNoTabs. | 1728 // Init with no local data, relies on MergeLocalSessionNoTabs. |
| 1846 InitWithSyncDataTakeOutput(syncer::SyncDataList(), &out); | 1729 SessionID::id_type window_id = AddWindow()->GetSessionId(); |
| 1730 InitWithSyncDataTakeOutput(SyncDataList(), &out); | |
| 1847 ASSERT_FALSE(manager()->current_machine_tag().empty()); | 1731 ASSERT_FALSE(manager()->current_machine_tag().empty()); |
| 1848 ASSERT_EQ(2U, out.size()); | 1732 ASSERT_EQ(2U, out.size()); |
| 1849 | 1733 |
| 1850 // Copy the original header. | 1734 // Copy the original header. |
| 1851 sync_pb::EntitySpecifics header(out[0].sync_data().GetSpecifics()); | 1735 sync_pb::EntitySpecifics header(out[0].sync_data().GetSpecifics()); |
| 1852 out.clear(); | 1736 out.clear(); |
| 1853 | 1737 |
| 1854 const GURL foo1("http://foo/1"); | 1738 NavigateTab(AddTab(window_id, kFoo1), kFoo2); |
| 1855 const GURL foo2("http://foo/2"); | 1739 NavigateTab(AddTab(window_id, kBar1), kBar2); |
| 1856 const GURL bar1("http://bar/1"); | 1740 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 | 1741 |
| 1863 // One add, one update for each AddTab. | 1742 // Change type breakdown: |
| 1864 // One update for each NavigateAndCommit. | 1743 // 1 tab add/update + 2 header updates. |
| 1865 // = 6 total tab updates. | 1744 const size_t kChangesPerTabCreation = 4; |
| 1866 // One header update corresponding to each of those. | 1745 // 1 tab update + 1 header update. |
| 1867 // = 6 total header updates. | 1746 const size_t kChangesPerTabNav = 2; |
| 1868 // 12 total updates. | 1747 const size_t kChangesPerTab = kChangesPerTabNav + kChangesPerTabCreation; |
| 1869 ASSERT_EQ(12U, out.size()); | 1748 const size_t kNumTabs = 2; |
| 1749 const size_t kTotalUpdates = kChangesPerTab * kNumTabs; | |
| 1750 | |
| 1751 std::vector<SyncChange::SyncChangeType> types = { | |
| 1752 // Tab 1 | |
| 1753 SyncChange::ACTION_UPDATE, SyncChange::ACTION_ADD, | |
| 1754 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE, | |
| 1755 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE, | |
| 1756 // Tab 2 | |
| 1757 SyncChange::ACTION_UPDATE, SyncChange::ACTION_ADD, | |
| 1758 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE, | |
| 1759 SyncChange::ACTION_UPDATE, SyncChange::ACTION_UPDATE}; | |
| 1760 ASSERT_EQ(kTotalUpdates, types.size()); | |
| 1870 | 1761 |
| 1871 // Verify the tab node creations and updates to ensure the SyncProcessor | 1762 // Verify the tab node creations and updates to ensure the SyncProcessor |
| 1872 // sees the right operations. | 1763 // sees the right operations. |
| 1873 for (int i = 0; i < 12; i++) { | 1764 ASSERT_TRUE(ChangeTypeMatches(out, types)); |
| 1874 SCOPED_TRACE(i); | 1765 for (size_t i = 0; i < kNumTabs; ++i) { |
|
skym
2017/02/22 22:25:11
I find all of this really confusing and difficult
Nicolas Zea
2017/02/22 22:58:41
I added some comments to try to clarify what this
| |
| 1875 EXPECT_TRUE(out[i].IsValid()); | 1766 int index = kChangesPerTab * i; |
|
skym
2017/02/22 22:25:11
This really confuses me. So |out| is presumably of
Nicolas Zea
2017/02/22 22:58:40
It does start at 6. kChangesPerTab is 6 (2 + 4)
skym
2017/02/22 23:17:26
Oh, you're right, I kept reading the wrong line.
| |
| 1876 const SyncData data(out[i].sync_data()); | 1767 int nav_per_tab_count = 0; |
| 1877 EXPECT_TRUE(base::StartsWith(syncer::SyncDataLocal(data).GetTag(), | 1768 { |
| 1878 manager()->current_machine_tag(), | 1769 SCOPED_TRACE(index); |
| 1879 base::CompareCase::SENSITIVE)); | 1770 VerifyLocalHeaderChange(out[index++], (i == 0 ? 0 : 1), |
|
skym
2017/02/22 22:25:15
isn't i == 0 ? 0 : 1 the same as just i when i onl
Nicolas Zea
2017/02/22 22:58:40
True, but that then is based on the assumption tha
| |
| 1880 const sync_pb::SessionSpecifics& specifics(data.GetSpecifics().session()); | 1771 i); // No-op header update for parented tab. |
|
skym
2017/02/22 22:25:12
Is this comment talking about the i param or the e
Nicolas Zea
2017/02/22 22:58:41
No, the entire line. Moved the comments for this a
| |
| 1881 EXPECT_EQ(manager()->current_machine_tag(), specifics.session_tag()); | 1772 } |
| 1882 if (i % 6 == 0) { | 1773 { |
| 1883 // First thing on an AddTab is a no-op header update for parented tab. | 1774 SCOPED_TRACE(index); |
| 1884 EXPECT_EQ(header.SerializeAsString(), | 1775 nav_per_tab_count++; |
| 1885 data.GetSpecifics().SerializeAsString()); | 1776 index++; // Ignore the tab add, which has no data. |
|
skym
2017/02/22 22:25:19
I think this would make more sense to put between
Nicolas Zea
2017/02/22 22:58:41
Done.
| |
| 1886 EXPECT_EQ(manager()->current_machine_tag(), | 1777 VerifyLocalTabChange(out[index++], nav_per_tab_count, |
| 1887 syncer::SyncDataLocal(data).GetTag()); | 1778 urls[i * kChangesPerTabNav + nav_per_tab_count - |
| 1888 } else if (i % 6 == 1) { | 1779 1]); // Tab creation. |
| 1889 // Next, the TabNodePool should create the tab node. | 1780 } |
| 1890 EXPECT_EQ(SyncChange::ACTION_ADD, out[i].change_type()); | 1781 { |
| 1891 EXPECT_EQ(TabNodePool::TabIdToTag( | 1782 SCOPED_TRACE(index); |
| 1892 manager()->current_machine_tag(), | 1783 VerifyLocalHeaderChange(out[index++], 1, i + 1); // Updated window state. |
| 1893 data.GetSpecifics().session().tab_node_id()), | 1784 } |
| 1894 syncer::SyncDataLocal(data).GetTag()); | 1785 { |
| 1895 } else if (i % 6 == 2) { | 1786 SCOPED_TRACE(index); |
| 1896 // Then we see the tab update to the URL. | 1787 nav_per_tab_count++; |
| 1897 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[i].change_type()); | 1788 VerifyLocalTabChange(out[index++], nav_per_tab_count, |
| 1898 EXPECT_EQ(TabNodePool::TabIdToTag( | 1789 urls[i * kChangesPerTabNav + nav_per_tab_count - |
| 1899 manager()->current_machine_tag(), | 1790 1]); // Tab navigation. |
| 1900 data.GetSpecifics().session().tab_node_id()), | 1791 } |
| 1901 syncer::SyncDataLocal(data).GetTag()); | 1792 { |
| 1902 ASSERT_TRUE(specifics.has_tab()); | 1793 SCOPED_TRACE(index); |
| 1903 } else if (i % 6 == 3) { | 1794 VerifyLocalHeaderChange(out[index++], 1, i + 1); // Updated window state. |
| 1904 // The header needs to be updated to reflect the new window state. | |
| 1905 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[i].change_type()); | |
| 1906 EXPECT_TRUE(specifics.has_header()); | |
| 1907 } else if (i % 6 == 4) { | |
| 1908 // Now we move on to NavigateAndCommit. Update the tab. | |
| 1909 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[i].change_type()); | |
| 1910 EXPECT_EQ(TabNodePool::TabIdToTag( | |
| 1911 manager()->current_machine_tag(), | |
| 1912 data.GetSpecifics().session().tab_node_id()), | |
| 1913 syncer::SyncDataLocal(data).GetTag()); | |
| 1914 ASSERT_TRUE(specifics.has_tab()); | |
| 1915 } else if (i % 6 == 5) { | |
| 1916 // The header needs to be updated to reflect the new window state. | |
| 1917 EXPECT_EQ(SyncChange::ACTION_UPDATE, out[i].change_type()); | |
| 1918 ASSERT_TRUE(specifics.has_header()); | |
| 1919 header = data.GetSpecifics(); | |
| 1920 } | 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( |
|
skym
2017/02/22 22:25:17
What do you think of using SessionSpecifics; ?
Al
Nicolas Zea
2017/02/22 22:58:40
Almost all of the others "using" are for syncer::.
| |
| 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) { | |
|
Nicolas Zea
2017/02/22 20:37:49
This was deleted as the SessionSyncManager no long
| |
| 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 |