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

Side by Side Diff: chrome/browser/sync/test/integration/sessions_helper.cc

Issue 7966020: [Sync] Fix Session's handling of windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix nits, rebase. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 Reset(windows);
33 }
34
35 ScopedWindowMap::~ScopedWindowMap() {
36 STLDeleteContainerPairSecondPointers(windows_.begin(), windows_.end());
37 }
38
39 SessionWindowMap* ScopedWindowMap::GetMutable() {
40 return &windows_;
41 }
42
43 const SessionWindowMap* ScopedWindowMap::Get() const {
44 return &windows_;
45 }
46
47 void ScopedWindowMap::Reset(SessionWindowMap* windows) {
48 STLDeleteContainerPairSecondPointers(windows_.begin(), windows_.end());
49 windows_.clear();
50 std::swap(*windows, windows_);
51 }
52
26 bool GetLocalSession(int index, const browser_sync::SyncedSession** session) { 53 bool GetLocalSession(int index, const browser_sync::SyncedSession** session) {
27 return test()->GetProfile(index)->GetProfileSyncService()-> 54 return test()->GetProfile(index)->GetProfileSyncService()->
28 GetSessionModelAssociator()->GetLocalSession(session); 55 GetSessionModelAssociator()->GetLocalSession(session);
29 } 56 }
30 57
31 bool ModelAssociatorHasTabWithUrl(int index, const GURL& url) { 58 bool ModelAssociatorHasTabWithUrl(int index, const GURL& url) {
32 ui_test_utils::RunAllPendingInMessageLoop(); 59 ui_test_utils::RunAllPendingInMessageLoop();
33 const browser_sync::SyncedSession* local_session; 60 const browser_sync::SyncedSession* local_session;
34 if (!GetLocalSession(index, &local_session)) { 61 if (!GetLocalSession(index, &local_session)) {
35 return false; 62 return false;
36 } 63 }
37 64
38 if (local_session->windows.size() == 0) { 65 if (local_session->windows.size() == 0) {
39 VLOG(1) << "Empty windows vector"; 66 VLOG(1) << "Empty windows vector";
40 return false; 67 return false;
41 } 68 }
42 69
43 int nav_index; 70 int nav_index;
44 TabNavigation nav; 71 TabNavigation nav;
45 for (SessionWindowVector::const_iterator it = 72 for (SessionWindowMap::const_iterator it =
46 local_session->windows.begin(); it != local_session->windows.end(); 73 local_session->windows.begin();
47 ++it) { 74 it != local_session->windows.end(); ++it) {
48 if ((*it)->tabs.size() == 0) { 75 if (it->second->tabs.size() == 0) {
49 VLOG(1) << "Empty tabs vector"; 76 VLOG(1) << "Empty tabs vector";
50 continue; 77 continue;
51 } 78 }
52 for (std::vector<SessionTab*>::const_iterator tab_it = 79 for (std::vector<SessionTab*>::const_iterator tab_it =
53 (*it)->tabs.begin(); tab_it != (*it)->tabs.end(); ++tab_it) { 80 it->second->tabs.begin();
81 tab_it != it->second->tabs.end(); ++tab_it) {
54 if ((*tab_it)->navigations.size() == 0) { 82 if ((*tab_it)->navigations.size() == 0) {
55 VLOG(1) << "Empty navigations vector"; 83 VLOG(1) << "Empty navigations vector";
56 continue; 84 continue;
57 } 85 }
58 nav_index = (*tab_it)->current_navigation_index; 86 nav_index = (*tab_it)->current_navigation_index;
59 nav = (*tab_it)->navigations[nav_index]; 87 nav = (*tab_it)->navigations[nav_index];
60 if (nav.virtual_url() == url) { 88 if (nav.virtual_url() == url) {
61 VLOG(1) << "Found tab with url " << url.spec(); 89 VLOG(1) << "Found tab with url " << url.spec();
62 if (nav.title().empty()) { 90 if (nav.title().empty()) {
63 VLOG(1) << "No title!"; 91 VLOG(1) << "No title!";
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 test()->GetProfile(index)->GetProfileSyncService()-> 137 test()->GetProfile(index)->GetProfileSyncService()->
110 GetSessionModelAssociator()-> 138 GetSessionModelAssociator()->
111 BlockUntilLocalChangeForTest(timeout_milli); 139 BlockUntilLocalChangeForTest(timeout_milli);
112 ui_test_utils::RunMessageLoop(); 140 ui_test_utils::RunMessageLoop();
113 } 141 }
114 } 142 }
115 } 143 }
116 return true; 144 return true;
117 } 145 }
118 146
119 bool GetLocalWindows(int index, SessionWindowVector& local_windows) { 147 bool GetLocalWindows(int index, SessionWindowMap* local_windows) {
120 // The local session provided by GetLocalSession is owned, and has lifetime 148 // The local session provided by GetLocalSession is owned, and has lifetime
121 // controlled, by the model associator, so we must make our own copy. 149 // controlled, by the model associator, so we must make our own copy.
122 const browser_sync::SyncedSession* local_session; 150 const browser_sync::SyncedSession* local_session;
123 if (!GetLocalSession(index, &local_session)) { 151 if (!GetLocalSession(index, &local_session)) {
124 return false; 152 return false;
125 } 153 }
126 for (size_t w = 0; w < local_session->windows.size(); ++w) { 154 for (SessionWindowMap::const_iterator w = local_session->windows.begin();
127 const SessionWindow& window = *local_session->windows.at(w); 155 w != local_session->windows.end(); ++w) {
156 const SessionWindow& window = *(w->second);
128 SessionWindow* new_window = new SessionWindow(); 157 SessionWindow* new_window = new SessionWindow();
158 new_window->window_id.set_id(window.window_id.id());
129 for (size_t t = 0; t < window.tabs.size(); ++t) { 159 for (size_t t = 0; t < window.tabs.size(); ++t) {
130 const SessionTab& tab = *window.tabs.at(t); 160 const SessionTab& tab = *window.tabs.at(t);
131 SessionTab* new_tab = new SessionTab(); 161 SessionTab* new_tab = new SessionTab();
132 new_tab->navigations.resize(tab.navigations.size()); 162 new_tab->navigations.resize(tab.navigations.size());
133 std::copy(tab.navigations.begin(), tab.navigations.end(), 163 std::copy(tab.navigations.begin(), tab.navigations.end(),
134 new_tab->navigations.begin()); 164 new_tab->navigations.begin());
135 new_window->tabs.push_back(new_tab); 165 new_window->tabs.push_back(new_tab);
136 } 166 }
137 local_windows.push_back(new_window); 167 (*local_windows)[new_window->window_id.id()] = new_window;
138 } 168 }
139 169
140 // Sort the windows so their order is deterministic.
141 SortSessionWindows(local_windows);
142
143 return true; 170 return true;
144 } 171 }
145 172
146 bool OpenTabAndGetLocalWindows(int index, 173 bool OpenTabAndGetLocalWindows(int index,
147 const GURL& url, 174 const GURL& url,
148 SessionWindowVector& local_windows) { 175 SessionWindowMap* local_windows) {
149 if (!OpenTab(index, url)) { 176 if (!OpenTab(index, url)) {
150 return false; 177 return false;
151 } 178 }
152 return GetLocalWindows(index, local_windows); 179 return GetLocalWindows(index, local_windows);
153 } 180 }
154 181
155 bool CheckInitialState(int index) { 182 bool CheckInitialState(int index) {
156 if (0 != GetNumWindows(index)) 183 if (0 != GetNumWindows(index))
157 return false; 184 return false;
158 if (0 != GetNumForeignSessions(index)) 185 if (0 != GetNumForeignSessions(index))
(...skipping 18 matching lines...) Expand all
177 } 204 }
178 205
179 bool GetSessionData(int index, SyncedSessionVector* sessions) { 206 bool GetSessionData(int index, SyncedSessionVector* sessions) {
180 if (!test()->GetProfile(index)->GetProfileSyncService()-> 207 if (!test()->GetProfile(index)->GetProfileSyncService()->
181 GetSessionModelAssociator()->GetAllForeignSessions(sessions)) 208 GetSessionModelAssociator()->GetAllForeignSessions(sessions))
182 return false; 209 return false;
183 SortSyncedSessions(sessions); 210 SortSyncedSessions(sessions);
184 return true; 211 return true;
185 } 212 }
186 213
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, 214 bool CompareSyncedSessions(const browser_sync::SyncedSession* lhs,
208 const browser_sync::SyncedSession* rhs) { 215 const browser_sync::SyncedSession* rhs) {
209 if (!lhs || 216 if (!lhs ||
210 !rhs || 217 !rhs ||
211 lhs->windows.size() < 1 || 218 lhs->windows.size() < 1 ||
212 rhs->windows.size() < 1) { 219 rhs->windows.size() < 1) {
213 // Catchall for uncomparable data. 220 // Catchall for uncomparable data.
214 return false; 221 return false;
215 } 222 }
216 223
217 return CompareSessionWindows(lhs->windows[0], rhs->windows[0]); 224 return lhs->windows < rhs->windows;
218 } 225 }
219 226
220 void SortSyncedSessions(SyncedSessionVector* sessions) { 227 void SortSyncedSessions(SyncedSessionVector* sessions) {
221 std::sort(sessions->begin(), sessions->end(), 228 std::sort(sessions->begin(), sessions->end(),
222 CompareSyncedSessions); 229 CompareSyncedSessions);
223 } 230 }
224 231
225 bool NavigationEquals(const TabNavigation& expected, 232 bool NavigationEquals(const TabNavigation& expected,
226 const TabNavigation& actual) { 233 const TabNavigation& actual) {
227 if (expected.virtual_url() != actual.virtual_url()) { 234 if (expected.virtual_url() != actual.virtual_url()) {
(...skipping 12 matching lines...) Expand all
240 return false; 247 return false;
241 } 248 }
242 if (expected.transition() != actual.transition()) { 249 if (expected.transition() != actual.transition()) {
243 LOG(ERROR) << "Expected transition " << expected.transition() 250 LOG(ERROR) << "Expected transition " << expected.transition()
244 << ", actual " << actual.transition(); 251 << ", actual " << actual.transition();
245 return false; 252 return false;
246 } 253 }
247 return true; 254 return true;
248 } 255 }
249 256
250 bool WindowsMatch(const SessionWindowVector& win1, 257 bool WindowsMatch(const SessionWindowMap& win1,
251 const SessionWindowVector& win2) { 258 const SessionWindowMap& win2) {
252 SessionTab* client0_tab; 259 SessionTab* client0_tab;
253 SessionTab* client1_tab; 260 SessionTab* client1_tab;
254 if (win1.size() != win2.size()) 261 if (win1.size() != win2.size())
255 return false; 262 return false;
256 for (size_t i = 0; i < win1.size(); ++i) { 263 for (SessionWindowMap::const_iterator i = win1.begin();
257 if (win1[i]->tabs.size() != win2[i]->tabs.size()) 264 i != win1.end(); ++i) {
265 SessionWindowMap::const_iterator j = win2.find(i->first);
266 if (j == win2.end())
258 return false; 267 return false;
259 for (size_t j = 0; j < win1[i]->tabs.size(); ++j) { 268 if (i->second->tabs.size() != j->second->tabs.size())
260 client0_tab = win1[i]->tabs[j]; 269 return false;
261 client1_tab = win2[i]->tabs[j]; 270 for (size_t t = 0; t < i->second->tabs.size(); ++t) {
262 for (size_t k = 0; k < client0_tab->navigations.size(); ++k) { 271 client0_tab = i->second->tabs[t];
263 if (!NavigationEquals(client0_tab->navigations[k], 272 client1_tab = j->second->tabs[t];
264 client1_tab->navigations[k])) { 273 for (size_t n = 0; n < client0_tab->navigations.size(); ++n) {
274 if (!NavigationEquals(client0_tab->navigations[n],
275 client1_tab->navigations[n])) {
265 return false; 276 return false;
266 } 277 }
267 } 278 }
268 } 279 }
269 } 280 }
270 281
271 return true; 282 return true;
272 } 283 }
273 284
274 bool CheckForeignSessionsAgainst( 285 bool CheckForeignSessionsAgainst(
275 int index, 286 int index,
276 const std::vector<SessionWindowVector*>& windows) { 287 const std::vector<ScopedWindowMap>& windows) {
277 SyncedSessionVector sessions; 288 SyncedSessionVector sessions;
278 if (!GetSessionData(index, &sessions)) 289 if (!GetSessionData(index, &sessions))
279 return false; 290 return false;
280 if ((size_t)(test()->num_clients()-1) != sessions.size()) 291 if ((size_t)(test()->num_clients()-1) != sessions.size())
281 return false; 292 return false;
282 293
283 int window_index = 0; 294 int window_index = 0;
284 for (size_t j = 0; j < sessions.size(); ++j, ++window_index) { 295 for (size_t j = 0; j < sessions.size(); ++j, ++window_index) {
285 if (window_index == index) 296 if (window_index == index)
286 window_index++; // Skip self. 297 window_index++; // Skip self.
287 if (!WindowsMatch(sessions[j]->windows, *windows[window_index])) 298 if (!WindowsMatch(sessions[j]->windows,
299 *(windows[window_index].Get())))
288 return false; 300 return false;
289 } 301 }
290 302
291 return true; 303 return true;
292 } 304 }
293 305
294 } // namespace sessions_helper 306 } // namespace sessions_helper
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698