| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/sync/test/integration/sessions_helper.h" | 5 #include "chrome/browser/sync/test/integration/sessions_helper.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 7 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 8 #include "base/test/test_timeouts.h" | 10 #include "base/test/test_timeouts.h" |
| 9 #include "base/time.h" | 11 #include "base/time.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/sync/profile_sync_service.h" | 13 #include "chrome/browser/sync/profile_sync_service.h" |
| 12 #include "chrome/browser/sync/profile_sync_service_harness.h" | 14 #include "chrome/browser/sync/profile_sync_service_harness.h" |
| 13 #include "chrome/browser/sync/glue/session_model_associator.h" | 15 #include "chrome/browser/sync/glue/session_model_associator.h" |
| 14 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" | 16 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" |
| 15 #include "chrome/browser/sync/test/integration/sync_test.h" | 17 #include "chrome/browser/sync/test/integration/sync_test.h" |
| 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
| 18 #include "content/browser/browser_thread.h" | 20 #include "content/browser/browser_thread.h" |
| 19 #include "content/browser/tab_contents/tab_contents.h" | 21 #include "content/browser/tab_contents/tab_contents.h" |
| 20 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
| 21 | 23 |
| 22 using sync_datatype_helper::test; | 24 using sync_datatype_helper::test; |
| 23 | 25 |
| 24 namespace sessions_helper { | 26 namespace sessions_helper { |
| 25 | 27 |
| 28 ScopedWindowMap::ScopedWindowMap() { |
| 29 } |
| 30 |
| 31 ScopedWindowMap::ScopedWindowMap(SessionWindowMap* windows) { |
| 32 set(windows); |
| 33 } |
| 34 |
| 35 ScopedWindowMap::~ScopedWindowMap() { |
| 36 STLDeleteContainerPairSecondPointers(windows_.begin(), windows_.end()); |
| 37 } |
| 38 |
| 39 SessionWindowMap* ScopedWindowMap::get() { |
| 40 return &windows_; |
| 41 } |
| 42 |
| 43 const SessionWindowMap* ScopedWindowMap::const_get() const { |
| 44 return &windows_; |
| 45 } |
| 46 |
| 47 void ScopedWindowMap::set(SessionWindowMap* windows) { |
| 48 std::swap(*windows, windows_); |
| 49 } |
| 50 |
| 26 bool GetLocalSession(int index, const browser_sync::SyncedSession** session) { | 51 bool GetLocalSession(int index, const browser_sync::SyncedSession** session) { |
| 27 return test()->GetProfile(index)->GetProfileSyncService()-> | 52 return test()->GetProfile(index)->GetProfileSyncService()-> |
| 28 GetSessionModelAssociator()->GetLocalSession(session); | 53 GetSessionModelAssociator()->GetLocalSession(session); |
| 29 } | 54 } |
| 30 | 55 |
| 31 bool ModelAssociatorHasTabWithUrl(int index, const GURL& url) { | 56 bool ModelAssociatorHasTabWithUrl(int index, const GURL& url) { |
| 32 ui_test_utils::RunAllPendingInMessageLoop(); | 57 ui_test_utils::RunAllPendingInMessageLoop(); |
| 33 const browser_sync::SyncedSession* local_session; | 58 const browser_sync::SyncedSession* local_session; |
| 34 if (!GetLocalSession(index, &local_session)) { | 59 if (!GetLocalSession(index, &local_session)) { |
| 35 return false; | 60 return false; |
| 36 } | 61 } |
| 37 | 62 |
| 38 if (local_session->windows.size() == 0) { | 63 if (local_session->windows.size() == 0) { |
| 39 VLOG(1) << "Empty windows vector"; | 64 VLOG(1) << "Empty windows vector"; |
| 40 return false; | 65 return false; |
| 41 } | 66 } |
| 42 | 67 |
| 43 int nav_index; | 68 int nav_index; |
| 44 TabNavigation nav; | 69 TabNavigation nav; |
| 45 for (SessionWindowVector::const_iterator it = | 70 for (SessionWindowMap::const_iterator it = |
| 46 local_session->windows.begin(); it != local_session->windows.end(); | 71 local_session->windows.begin(); |
| 47 ++it) { | 72 it != local_session->windows.end(); ++it) { |
| 48 if ((*it)->tabs.size() == 0) { | 73 if (it->second->tabs.size() == 0) { |
| 49 VLOG(1) << "Empty tabs vector"; | 74 VLOG(1) << "Empty tabs vector"; |
| 50 continue; | 75 continue; |
| 51 } | 76 } |
| 52 for (std::vector<SessionTab*>::const_iterator tab_it = | 77 for (std::vector<SessionTab*>::const_iterator tab_it = |
| 53 (*it)->tabs.begin(); tab_it != (*it)->tabs.end(); ++tab_it) { | 78 it->second->tabs.begin(); |
| 79 tab_it != it->second->tabs.end(); ++tab_it) { |
| 54 if ((*tab_it)->navigations.size() == 0) { | 80 if ((*tab_it)->navigations.size() == 0) { |
| 55 VLOG(1) << "Empty navigations vector"; | 81 VLOG(1) << "Empty navigations vector"; |
| 56 continue; | 82 continue; |
| 57 } | 83 } |
| 58 nav_index = (*tab_it)->current_navigation_index; | 84 nav_index = (*tab_it)->current_navigation_index; |
| 59 nav = (*tab_it)->navigations[nav_index]; | 85 nav = (*tab_it)->navigations[nav_index]; |
| 60 if (nav.virtual_url() == url) { | 86 if (nav.virtual_url() == url) { |
| 61 VLOG(1) << "Found tab with url " << url.spec(); | 87 VLOG(1) << "Found tab with url " << url.spec(); |
| 62 if (nav.title().empty()) { | 88 if (nav.title().empty()) { |
| 63 VLOG(1) << "No title!"; | 89 VLOG(1) << "No title!"; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 test()->GetProfile(index)->GetProfileSyncService()-> | 135 test()->GetProfile(index)->GetProfileSyncService()-> |
| 110 GetSessionModelAssociator()-> | 136 GetSessionModelAssociator()-> |
| 111 BlockUntilLocalChangeForTest(timeout_milli); | 137 BlockUntilLocalChangeForTest(timeout_milli); |
| 112 ui_test_utils::RunMessageLoop(); | 138 ui_test_utils::RunMessageLoop(); |
| 113 } | 139 } |
| 114 } | 140 } |
| 115 } | 141 } |
| 116 return true; | 142 return true; |
| 117 } | 143 } |
| 118 | 144 |
| 119 bool GetLocalWindows(int index, SessionWindowVector& local_windows) { | 145 bool GetLocalWindows(int index, SessionWindowMap* local_windows) { |
| 120 // The local session provided by GetLocalSession is owned, and has lifetime | 146 // The local session provided by GetLocalSession is owned, and has lifetime |
| 121 // controlled, by the model associator, so we must make our own copy. | 147 // controlled, by the model associator, so we must make our own copy. |
| 122 const browser_sync::SyncedSession* local_session; | 148 const browser_sync::SyncedSession* local_session; |
| 123 if (!GetLocalSession(index, &local_session)) { | 149 if (!GetLocalSession(index, &local_session)) { |
| 124 return false; | 150 return false; |
| 125 } | 151 } |
| 126 for (size_t w = 0; w < local_session->windows.size(); ++w) { | 152 for (SessionWindowMap::const_iterator w = local_session->windows.begin(); |
| 127 const SessionWindow& window = *local_session->windows.at(w); | 153 w != local_session->windows.end(); ++w) { |
| 154 const SessionWindow& window = *(w->second); |
| 128 SessionWindow* new_window = new SessionWindow(); | 155 SessionWindow* new_window = new SessionWindow(); |
| 156 new_window->window_id.set_id(window.window_id.id()); |
| 129 for (size_t t = 0; t < window.tabs.size(); ++t) { | 157 for (size_t t = 0; t < window.tabs.size(); ++t) { |
| 130 const SessionTab& tab = *window.tabs.at(t); | 158 const SessionTab& tab = *window.tabs.at(t); |
| 131 SessionTab* new_tab = new SessionTab(); | 159 SessionTab* new_tab = new SessionTab(); |
| 132 new_tab->navigations.resize(tab.navigations.size()); | 160 new_tab->navigations.resize(tab.navigations.size()); |
| 133 std::copy(tab.navigations.begin(), tab.navigations.end(), | 161 std::copy(tab.navigations.begin(), tab.navigations.end(), |
| 134 new_tab->navigations.begin()); | 162 new_tab->navigations.begin()); |
| 135 new_window->tabs.push_back(new_tab); | 163 new_window->tabs.push_back(new_tab); |
| 136 } | 164 } |
| 137 local_windows.push_back(new_window); | 165 (*local_windows)[new_window->window_id.id()] = new_window; |
| 138 } | 166 } |
| 139 | 167 |
| 140 // Sort the windows so their order is deterministic. | |
| 141 SortSessionWindows(local_windows); | |
| 142 | |
| 143 return true; | 168 return true; |
| 144 } | 169 } |
| 145 | 170 |
| 146 bool OpenTabAndGetLocalWindows(int index, | 171 bool OpenTabAndGetLocalWindows(int index, |
| 147 const GURL& url, | 172 const GURL& url, |
| 148 SessionWindowVector& local_windows) { | 173 SessionWindowMap* local_windows) { |
| 149 if (!OpenTab(index, url)) { | 174 if (!OpenTab(index, url)) { |
| 150 return false; | 175 return false; |
| 151 } | 176 } |
| 152 return GetLocalWindows(index, local_windows); | 177 return GetLocalWindows(index, local_windows); |
| 153 } | 178 } |
| 154 | 179 |
| 155 bool CheckInitialState(int index) { | 180 bool CheckInitialState(int index) { |
| 156 if (0 != GetNumWindows(index)) | 181 if (0 != GetNumWindows(index)) |
| 157 return false; | 182 return false; |
| 158 if (0 != GetNumForeignSessions(index)) | 183 if (0 != GetNumForeignSessions(index)) |
| (...skipping 18 matching lines...) Expand all Loading... |
| 177 } | 202 } |
| 178 | 203 |
| 179 bool GetSessionData(int index, SyncedSessionVector* sessions) { | 204 bool GetSessionData(int index, SyncedSessionVector* sessions) { |
| 180 if (!test()->GetProfile(index)->GetProfileSyncService()-> | 205 if (!test()->GetProfile(index)->GetProfileSyncService()-> |
| 181 GetSessionModelAssociator()->GetAllForeignSessions(sessions)) | 206 GetSessionModelAssociator()->GetAllForeignSessions(sessions)) |
| 182 return false; | 207 return false; |
| 183 SortSyncedSessions(sessions); | 208 SortSyncedSessions(sessions); |
| 184 return true; | 209 return true; |
| 185 } | 210 } |
| 186 | 211 |
| 187 bool CompareSessionWindows(SessionWindow* lhs, SessionWindow* rhs) { | |
| 188 if (!lhs || | |
| 189 !rhs || | |
| 190 lhs->tabs.size() < 1 || | |
| 191 rhs->tabs.size() < 1 || | |
| 192 lhs->tabs[0]->navigations.size() < 1 || | |
| 193 rhs->tabs[0]->navigations.size() < 1) { | |
| 194 // Catchall for uncomparable data. | |
| 195 return false; | |
| 196 } | |
| 197 | |
| 198 return lhs->tabs[0]->navigations[0].virtual_url() < | |
| 199 rhs->tabs[0]->navigations[0].virtual_url(); | |
| 200 } | |
| 201 | |
| 202 void SortSessionWindows(SessionWindowVector& windows) { | |
| 203 std::sort(windows.begin(), windows.end(), | |
| 204 CompareSessionWindows); | |
| 205 } | |
| 206 | |
| 207 bool CompareSyncedSessions(const browser_sync::SyncedSession* lhs, | 212 bool CompareSyncedSessions(const browser_sync::SyncedSession* lhs, |
| 208 const browser_sync::SyncedSession* rhs) { | 213 const browser_sync::SyncedSession* rhs) { |
| 209 if (!lhs || | 214 if (!lhs || |
| 210 !rhs || | 215 !rhs || |
| 211 lhs->windows.size() < 1 || | 216 lhs->windows.size() < 1 || |
| 212 rhs->windows.size() < 1) { | 217 rhs->windows.size() < 1) { |
| 213 // Catchall for uncomparable data. | 218 // Catchall for uncomparable data. |
| 214 return false; | 219 return false; |
| 215 } | 220 } |
| 216 | 221 |
| 217 return CompareSessionWindows(lhs->windows[0], rhs->windows[0]); | 222 return lhs->windows < rhs->windows; |
| 218 } | 223 } |
| 219 | 224 |
| 220 void SortSyncedSessions(SyncedSessionVector* sessions) { | 225 void SortSyncedSessions(SyncedSessionVector* sessions) { |
| 221 std::sort(sessions->begin(), sessions->end(), | 226 std::sort(sessions->begin(), sessions->end(), |
| 222 CompareSyncedSessions); | 227 CompareSyncedSessions); |
| 223 } | 228 } |
| 224 | 229 |
| 225 bool NavigationEquals(const TabNavigation& expected, | 230 bool NavigationEquals(const TabNavigation& expected, |
| 226 const TabNavigation& actual) { | 231 const TabNavigation& actual) { |
| 227 if (expected.virtual_url() != actual.virtual_url()) { | 232 if (expected.virtual_url() != actual.virtual_url()) { |
| (...skipping 12 matching lines...) Expand all Loading... |
| 240 return false; | 245 return false; |
| 241 } | 246 } |
| 242 if (expected.transition() != actual.transition()) { | 247 if (expected.transition() != actual.transition()) { |
| 243 LOG(ERROR) << "Expected transition " << expected.transition() | 248 LOG(ERROR) << "Expected transition " << expected.transition() |
| 244 << ", actual " << actual.transition(); | 249 << ", actual " << actual.transition(); |
| 245 return false; | 250 return false; |
| 246 } | 251 } |
| 247 return true; | 252 return true; |
| 248 } | 253 } |
| 249 | 254 |
| 250 bool WindowsMatch(const SessionWindowVector& win1, | 255 bool WindowsMatch(const SessionWindowMap& win1, |
| 251 const SessionWindowVector& win2) { | 256 const SessionWindowMap& win2) { |
| 252 SessionTab* client0_tab; | 257 SessionTab* client0_tab; |
| 253 SessionTab* client1_tab; | 258 SessionTab* client1_tab; |
| 254 if (win1.size() != win2.size()) | 259 if (win1.size() != win2.size()) |
| 255 return false; | 260 return false; |
| 256 for (size_t i = 0; i < win1.size(); ++i) { | 261 for (SessionWindowMap::const_iterator i = win1.begin(); |
| 257 if (win1[i]->tabs.size() != win2[i]->tabs.size()) | 262 i != win1.end(); ++i) { |
| 263 SessionWindowMap::const_iterator j = win2.find(i->first); |
| 264 if (j == win2.end()) |
| 258 return false; | 265 return false; |
| 259 for (size_t j = 0; j < win1[i]->tabs.size(); ++j) { | 266 if (i->second->tabs.size() != j->second->tabs.size()) |
| 260 client0_tab = win1[i]->tabs[j]; | 267 return false; |
| 261 client1_tab = win2[i]->tabs[j]; | 268 for (size_t t = 0; t < i->second->tabs.size(); ++t) { |
| 262 for (size_t k = 0; k < client0_tab->navigations.size(); ++k) { | 269 client0_tab = i->second->tabs[t]; |
| 263 if (!NavigationEquals(client0_tab->navigations[k], | 270 client1_tab = j->second->tabs[t]; |
| 264 client1_tab->navigations[k])) { | 271 for (size_t n = 0; n < client0_tab->navigations.size(); ++n) { |
| 272 if (!NavigationEquals(client0_tab->navigations[n], |
| 273 client1_tab->navigations[n])) { |
| 265 return false; | 274 return false; |
| 266 } | 275 } |
| 267 } | 276 } |
| 268 } | 277 } |
| 269 } | 278 } |
| 270 | 279 |
| 271 return true; | 280 return true; |
| 272 } | 281 } |
| 273 | 282 |
| 274 bool CheckForeignSessionsAgainst( | 283 bool CheckForeignSessionsAgainst( |
| 275 int index, | 284 int index, |
| 276 const std::vector<SessionWindowVector*>& windows) { | 285 const std::vector<ScopedWindowMap>& windows) { |
| 277 SyncedSessionVector sessions; | 286 SyncedSessionVector sessions; |
| 278 if (!GetSessionData(index, &sessions)) | 287 if (!GetSessionData(index, &sessions)) |
| 279 return false; | 288 return false; |
| 280 if ((size_t)(test()->num_clients()-1) != sessions.size()) | 289 if ((size_t)(test()->num_clients()-1) != sessions.size()) |
| 281 return false; | 290 return false; |
| 282 | 291 |
| 283 int window_index = 0; | 292 int window_index = 0; |
| 284 for (size_t j = 0; j < sessions.size(); ++j, ++window_index) { | 293 for (size_t j = 0; j < sessions.size(); ++j, ++window_index) { |
| 285 if (window_index == index) | 294 if (window_index == index) |
| 286 window_index++; // Skip self. | 295 window_index++; // Skip self. |
| 287 if (!WindowsMatch(sessions[j]->windows, *windows[window_index])) | 296 if (!WindowsMatch(sessions[j]->windows, |
| 297 *(windows[window_index].const_get()))) |
| 288 return false; | 298 return false; |
| 289 } | 299 } |
| 290 | 300 |
| 291 return true; | 301 return true; |
| 292 } | 302 } |
| 293 | 303 |
| 294 } // namespace sessions_helper | 304 } // namespace sessions_helper |
| OLD | NEW |