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

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

Issue 2713913002: [sync] Add Sessions integration tests (Closed)
Patch Set: Update commit message Created 3 years, 10 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 <stddef.h> 7 #include <stddef.h>
8 8
9 #include <set> 9 #include <set>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/location.h" 14 #include "base/location.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
17 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
18 #include "base/test/test_timeouts.h" 18 #include "base/test/test_timeouts.h"
19 #include "base/threading/thread_task_runner_handle.h" 19 #include "base/threading/thread_task_runner_handle.h"
20 #include "base/time/time.h" 20 #include "base/time/time.h"
21 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/sync/profile_sync_service_factory.h" 22 #include "chrome/browser/sync/profile_sync_service_factory.h"
23 #include "chrome/browser/sync/sessions/notification_service_sessions_router.h" 23 #include "chrome/browser/sync/sessions/notification_service_sessions_router.h"
24 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h" 24 #include "chrome/browser/sync/test/integration/profile_sync_service_harness.h"
25 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h" 25 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
26 #include "chrome/browser/ui/browser.h"
27 #include "chrome/browser/ui/browser_tabstrip.h"
26 #include "chrome/browser/ui/singleton_tabs.h" 28 #include "chrome/browser/ui/singleton_tabs.h"
29 #include "chrome/browser/ui/tabs/tab_strip_model.h"
27 #include "chrome/common/chrome_switches.h" 30 #include "chrome/common/chrome_switches.h"
31 #include "chrome/test/base/ui_test_utils.h"
28 #include "components/browser_sync/profile_sync_service.h" 32 #include "components/browser_sync/profile_sync_service.h"
29 #include "components/sync/driver/sync_client.h" 33 #include "components/sync/driver/sync_client.h"
34 #include "components/sync/test/fake_server/fake_server.h"
35 #include "components/sync/test/fake_server/sessions_hierarchy.h"
30 #include "components/sync_sessions/open_tabs_ui_delegate.h" 36 #include "components/sync_sessions/open_tabs_ui_delegate.h"
37 #include "content/public/browser/navigation_entry.h"
38 #include "content/public/browser/web_contents.h"
31 #include "content/public/test/test_utils.h" 39 #include "content/public/test/test_utils.h"
32 #include "url/gurl.h" 40 #include "url/gurl.h"
33 41
34 using sync_datatype_helper::test; 42 using sync_datatype_helper::test;
35 43
36 namespace sessions_helper { 44 namespace sessions_helper {
37 45
38 bool GetLocalSession(int index, const sync_sessions::SyncedSession** session) { 46 bool GetLocalSession(int profile_index,
39 return ProfileSyncServiceFactory::GetInstance()->GetForProfile( 47 const sync_sessions::SyncedSession** session) {
40 test()->GetProfile(index))->GetOpenTabsUIDelegate()-> 48 return ProfileSyncServiceFactory::GetInstance()
41 GetLocalSession(session); 49 ->GetForProfile(test()->GetProfile(profile_index))
50 ->GetOpenTabsUIDelegate()
51 ->GetLocalSession(session);
42 } 52 }
43 53
44 bool ModelAssociatorHasTabWithUrl(int index, const GURL& url) { 54 bool ModelAssociatorHasTabWithUrl(int index, const GURL& url) {
45 content::RunAllPendingInMessageLoop(); 55 content::RunAllPendingInMessageLoop();
46 const sync_sessions::SyncedSession* local_session; 56 const sync_sessions::SyncedSession* local_session;
47 if (!GetLocalSession(index, &local_session)) { 57 if (!GetLocalSession(index, &local_session)) {
48 return false; 58 return false;
49 } 59 }
50 60
51 if (local_session->windows.size() == 0) { 61 if (local_session->windows.size() == 0) {
(...skipping 25 matching lines...) Expand all
77 continue; 87 continue;
78 } 88 }
79 return true; 89 return true;
80 } 90 }
81 } 91 }
82 } 92 }
83 DVLOG(1) << "Could not find tab with url " << url.spec(); 93 DVLOG(1) << "Could not find tab with url " << url.spec();
84 return false; 94 return false;
85 } 95 }
86 96
87 bool OpenTab(int index, const GURL& url) { 97 bool OpenTab(int browser_index, const GURL& url) {
88 DVLOG(1) << "Opening tab: " << url.spec() << " using profile " 98 DVLOG(1) << "Opening tab: " << url.spec() << " using browser "
89 << index << "."; 99 << browser_index << ".";
90 chrome::ShowSingletonTab(test()->GetBrowser(index), url); 100 return OpenTabAtIndex(browser_index, 0, url);
91 return WaitForTabsToLoad(index, std::vector<GURL>(1, url));
92 } 101 }
93 102
94 bool OpenMultipleTabs(int index, const std::vector<GURL>& urls) { 103 bool OpenTabAtIndex(int browser_index, int tab_index, const GURL& url) {
95 Browser* browser = test()->GetBrowser(index); 104 chrome::AddTabAt(test()->GetBrowser(browser_index), url, tab_index, true);
105 return WaitForTabsToLoad(browser_index, {url});
106 }
107
108 bool OpenMultipleTabs(int browser_index, const std::vector<GURL>& urls) {
109 Browser* browser = test()->GetBrowser(browser_index);
96 for (std::vector<GURL>::const_iterator it = urls.begin(); 110 for (std::vector<GURL>::const_iterator it = urls.begin();
97 it != urls.end(); ++it) { 111 it != urls.end(); ++it) {
98 DVLOG(1) << "Opening tab: " << it->spec() << " using profile " << index 112 DVLOG(1) << "Opening tab: " << it->spec() << " using browser "
99 << "."; 113 << browser_index << ".";
100 chrome::ShowSingletonTab(browser, *it); 114 chrome::ShowSingletonTab(browser, *it);
101 } 115 }
102 return WaitForTabsToLoad(index, urls); 116 return WaitForTabsToLoad(browser_index, urls);
117 }
118
119 void MoveTab(int from_browser_index, int to_browser_index, int tab_index) {
120 content::WebContents* detached_contents =
121 test()
122 ->GetBrowser(from_browser_index)
123 ->tab_strip_model()
124 ->DetachWebContentsAt(tab_index);
125
126 TabStripModel* target_strip =
127 test()->GetBrowser(to_browser_index)->tab_strip_model();
128 target_strip->InsertWebContentsAt(target_strip->count(), detached_contents,
129 TabStripModel::ADD_ACTIVE);
130 }
131
132 bool NavigateTab(int browser_index, const GURL& url) {
133 chrome::NavigateParams params(test()->GetBrowser(browser_index), url,
134 ui::PAGE_TRANSITION_LINK);
135 params.disposition = WindowOpenDisposition::CURRENT_TAB;
136
137 ui_test_utils::NavigateToURL(&params);
138 return WaitForTabsToLoad(browser_index, {url});
139 }
140
141 void NavigateTabBack(int browser_index) {
142 content::WebContents* tab_contents =
143 test()->GetBrowser(browser_index)->tab_strip_model()->GetWebContentsAt(0);
144 tab_contents->GetController().GoBack();
skym 2017/02/24 00:14:09 ->GetController().GoBack(); vs content::WebContent
Patrick Noland 2017/02/27 18:53:25 Done.
145 }
146
147 void NavigateTabForward(int browser_index) {
148 content::WebContents* tab_contents =
149 test()->GetBrowser(browser_index)->tab_strip_model()->GetWebContentsAt(0);
150 tab_contents->GetController().GoForward();
103 } 151 }
104 152
105 namespace { 153 namespace {
106 154
107 class TabEventHandler : public sync_sessions::LocalSessionEventHandler { 155 class TabEventHandler : public sync_sessions::LocalSessionEventHandler {
108 public: 156 public:
109 TabEventHandler() : weak_factory_(this) { 157 TabEventHandler() : weak_factory_(this) {
110 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 158 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
111 FROM_HERE, 159 FROM_HERE,
112 base::Bind(&TabEventHandler::QuitLoop, weak_factory_.GetWeakPtr()), 160 base::Bind(&TabEventHandler::QuitLoop, weak_factory_.GetWeakPtr()),
(...skipping 17 matching lines...) Expand all
130 } 178 }
131 179
132 private: 180 private:
133 void QuitLoop() { base::MessageLoop::current()->QuitWhenIdle(); } 181 void QuitLoop() { base::MessageLoop::current()->QuitWhenIdle(); }
134 182
135 base::WeakPtrFactory<TabEventHandler> weak_factory_; 183 base::WeakPtrFactory<TabEventHandler> weak_factory_;
136 }; 184 };
137 185
138 } // namespace 186 } // namespace
139 187
140 bool WaitForTabsToLoad(int index, const std::vector<GURL>& urls) { 188 bool WaitForTabsToLoad(int profile_index, const std::vector<GURL>& urls) {
141 DVLOG(1) << "Waiting for session to propagate to associator."; 189 DVLOG(1) << "Waiting for session to propagate to associator.";
142 base::TimeTicks start_time = base::TimeTicks::Now(); 190 base::TimeTicks start_time = base::TimeTicks::Now();
143 base::TimeTicks end_time = start_time + TestTimeouts::action_max_timeout(); 191 base::TimeTicks end_time = start_time + TestTimeouts::action_max_timeout();
144 bool found; 192 bool found;
145 for (std::vector<GURL>::const_iterator it = urls.begin(); 193 for (std::vector<GURL>::const_iterator it = urls.begin();
146 it != urls.end(); ++it) { 194 it != urls.end(); ++it) {
147 found = false; 195 found = false;
148 while (!found) { 196 while (!found) {
149 found = ModelAssociatorHasTabWithUrl(index, *it); 197 found = ModelAssociatorHasTabWithUrl(profile_index, *it);
150 if (base::TimeTicks::Now() >= end_time) { 198 if (base::TimeTicks::Now() >= end_time) {
151 LOG(ERROR) << "Failed to find all tabs after " 199 LOG(ERROR) << "Failed to find all tabs after "
152 << TestTimeouts::action_max_timeout().InSecondsF() 200 << TestTimeouts::action_max_timeout().InSecondsF()
153 << " seconds."; 201 << " seconds.";
154 return false; 202 return false;
155 } 203 }
156 if (!found) { 204 if (!found) {
157 TabEventHandler handler; 205 TabEventHandler handler;
158 sync_sessions::NotificationServiceSessionsRouter router( 206 sync_sessions::NotificationServiceSessionsRouter router(
159 test()->GetProfile(index), 207 test()->GetProfile(profile_index),
160 ProfileSyncServiceFactory::GetInstance() 208 ProfileSyncServiceFactory::GetInstance()
161 ->GetForProfile(test()->GetProfile(index)) 209 ->GetForProfile(test()->GetProfile(profile_index))
162 ->GetSyncClient() 210 ->GetSyncClient()
163 ->GetSyncSessionsClient(), 211 ->GetSyncSessionsClient(),
164 syncer::SyncableService::StartSyncFlare()); 212 syncer::SyncableService::StartSyncFlare());
165 router.StartRoutingTo(&handler); 213 router.StartRoutingTo(&handler);
166 content::RunMessageLoop(); 214 content::RunMessageLoop();
167 } 215 }
168 } 216 }
169 } 217 }
170 return true; 218 return true;
171 } 219 }
172 220
173 bool GetLocalWindows(int index, ScopedWindowMap* local_windows) { 221 bool GetLocalWindows(int profile_index, ScopedWindowMap* local_windows) {
174 // The local session provided by GetLocalSession is owned, and has lifetime 222 // The local session provided by GetLocalSession is owned, and has lifetime
175 // controlled, by the model associator, so we must make our own copy. 223 // controlled, by the model associator, so we must make our own copy.
176 const sync_sessions::SyncedSession* local_session; 224 const sync_sessions::SyncedSession* local_session;
177 if (!GetLocalSession(index, &local_session)) { 225 if (!GetLocalSession(profile_index, &local_session)) {
178 return false; 226 return false;
179 } 227 }
180 for (auto w = local_session->windows.begin(); 228 for (auto w = local_session->windows.begin();
181 w != local_session->windows.end(); ++w) { 229 w != local_session->windows.end(); ++w) {
182 const sessions::SessionWindow& window = *(w->second); 230 const sessions::SessionWindow& window = *(w->second);
183 std::unique_ptr<sessions::SessionWindow> new_window = 231 std::unique_ptr<sessions::SessionWindow> new_window =
184 base::MakeUnique<sessions::SessionWindow>(); 232 base::MakeUnique<sessions::SessionWindow>();
185 new_window->window_id.set_id(window.window_id.id()); 233 new_window->window_id.set_id(window.window_id.id());
186 for (size_t t = 0; t < window.tabs.size(); ++t) { 234 for (size_t t = 0; t < window.tabs.size(); ++t) {
187 const sessions::SessionTab& tab = *window.tabs.at(t); 235 const sessions::SessionTab& tab = *window.tabs.at(t);
188 std::unique_ptr<sessions::SessionTab> new_tab = 236 std::unique_ptr<sessions::SessionTab> new_tab =
189 base::MakeUnique<sessions::SessionTab>(); 237 base::MakeUnique<sessions::SessionTab>();
190 new_tab->navigations.resize(tab.navigations.size()); 238 new_tab->navigations.resize(tab.navigations.size());
191 std::copy(tab.navigations.begin(), tab.navigations.end(), 239 std::copy(tab.navigations.begin(), tab.navigations.end(),
192 new_tab->navigations.begin()); 240 new_tab->navigations.begin());
193 new_window->tabs.push_back(std::move(new_tab)); 241 new_window->tabs.push_back(std::move(new_tab));
194 } 242 }
195 auto id = new_window->window_id.id(); 243 auto id = new_window->window_id.id();
196 (*local_windows)[id] = std::move(new_window); 244 (*local_windows)[id] = std::move(new_window);
197 } 245 }
198 246
199 return true; 247 return true;
200 } 248 }
201 249
202 bool OpenTabAndGetLocalWindows(int index, 250 bool OpenTabAndGetLocalWindows(int profile_index,
203 const GURL& url, 251 const GURL& url,
204 ScopedWindowMap* local_windows) { 252 ScopedWindowMap* local_windows) {
205 if (!OpenTab(index, url)) { 253 if (!OpenTab(profile_index, url)) {
skym 2017/02/24 00:14:09 So you're passing a profile_index in as a browser_
Patrick Noland 2017/02/24 01:00:40 This is kind of ugly. SessionsHelper deeply assume
skym 2017/02/24 16:20:17 When you have some functions take a profile index
Patrick Noland 2017/02/27 18:53:25 Discussed offline, I went back to just naming ever
206 return false; 254 return false;
207 } 255 }
208 return GetLocalWindows(index, local_windows); 256 return GetLocalWindows(profile_index, local_windows);
209 } 257 }
210 258
211 bool CheckInitialState(int index) { 259 bool CheckInitialState(int profile_index) {
212 if (0 != GetNumWindows(index)) 260 if (0 != GetNumWindows(profile_index))
213 return false; 261 return false;
214 if (0 != GetNumForeignSessions(index)) 262 if (0 != GetNumForeignSessions(profile_index))
215 return false; 263 return false;
216 return true; 264 return true;
217 } 265 }
218 266
219 int GetNumWindows(int index) { 267 int GetNumWindows(int profile_index) {
220 const sync_sessions::SyncedSession* local_session; 268 const sync_sessions::SyncedSession* local_session;
221 if (!GetLocalSession(index, &local_session)) { 269 if (!GetLocalSession(profile_index, &local_session)) {
222 return 0; 270 return 0;
223 } 271 }
224 return local_session->windows.size(); 272 return local_session->windows.size();
225 } 273 }
226 274
227 int GetNumForeignSessions(int index) { 275 int GetNumForeignSessions(int profile_index) {
228 SyncedSessionVector sessions; 276 SyncedSessionVector sessions;
229 if (!ProfileSyncServiceFactory::GetInstance()->GetForProfile( 277 if (!ProfileSyncServiceFactory::GetInstance()
230 test()->GetProfile(index))-> 278 ->GetForProfile(test()->GetProfile(profile_index))
231 GetOpenTabsUIDelegate()->GetAllForeignSessions( 279 ->GetOpenTabsUIDelegate()
232 &sessions)) { 280 ->GetAllForeignSessions(&sessions)) {
233 return 0; 281 return 0;
234 } 282 }
235 return sessions.size(); 283 return sessions.size();
236 } 284 }
237 285
238 bool GetSessionData(int index, SyncedSessionVector* sessions) { 286 bool GetSessionData(int profile_index, SyncedSessionVector* sessions) {
239 if (!ProfileSyncServiceFactory::GetInstance()->GetForProfile( 287 if (!ProfileSyncServiceFactory::GetInstance()
240 test()->GetProfile(index))-> 288 ->GetForProfile(test()->GetProfile(profile_index))
241 GetOpenTabsUIDelegate()->GetAllForeignSessions( 289 ->GetOpenTabsUIDelegate()
242 sessions)) { 290 ->GetAllForeignSessions(sessions)) {
243 return false; 291 return false;
244 } 292 }
245 SortSyncedSessions(sessions); 293 SortSyncedSessions(sessions);
246 return true; 294 return true;
247 } 295 }
248 296
249 bool CompareSyncedSessions(const sync_sessions::SyncedSession* lhs, 297 bool CompareSyncedSessions(const sync_sessions::SyncedSession* lhs,
250 const sync_sessions::SyncedSession* rhs) { 298 const sync_sessions::SyncedSession* rhs) {
251 if (!lhs || 299 if (!lhs ||
252 !rhs || 300 !rhs ||
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 } // namespace 386 } // namespace
339 387
340 bool WindowsMatch(const ScopedWindowMap& win1, const ScopedWindowMap& win2) { 388 bool WindowsMatch(const ScopedWindowMap& win1, const ScopedWindowMap& win2) {
341 return WindowsMatchImpl(win1, win2); 389 return WindowsMatchImpl(win1, win2);
342 } 390 }
343 391
344 bool WindowsMatch(const SessionWindowMap& win1, const ScopedWindowMap& win2) { 392 bool WindowsMatch(const SessionWindowMap& win1, const ScopedWindowMap& win2) {
345 return WindowsMatchImpl(win1, win2); 393 return WindowsMatchImpl(win1, win2);
346 } 394 }
347 395
348 bool CheckForeignSessionsAgainst( 396 bool CheckForeignSessionsAgainst(int profile_index,
349 int index, 397 const std::vector<ScopedWindowMap>& windows) {
350 const std::vector<ScopedWindowMap>& windows) {
351 SyncedSessionVector sessions; 398 SyncedSessionVector sessions;
352 399
353 if (!GetSessionData(index, &sessions)) { 400 if (!GetSessionData(profile_index, &sessions)) {
354 LOG(ERROR) << "Cannot get session data"; 401 LOG(ERROR) << "Cannot get session data";
355 return false; 402 return false;
356 } 403 }
357 404
358 for (size_t w_index = 0; w_index < windows.size(); ++w_index) { 405 for (size_t w_index = 0; w_index < windows.size(); ++w_index) {
359 // Skip the client's local window 406 // Skip the client's local window
360 if (static_cast<int>(w_index) == index) 407 if (static_cast<int>(w_index) == profile_index) {
361 continue; 408 continue;
409 }
362 410
363 size_t s_index = 0; 411 size_t s_index = 0;
364 412
365 for (; s_index < sessions.size(); ++s_index) { 413 for (; s_index < sessions.size(); ++s_index) {
366 if (WindowsMatch(sessions[s_index]->windows, windows[w_index])) 414 if (WindowsMatch(sessions[s_index]->windows, windows[w_index]))
367 break; 415 break;
368 } 416 }
369 417
370 if (s_index == sessions.size()) { 418 if (s_index == sessions.size()) {
371 LOG(ERROR) << "Cannot find window #" << w_index; 419 LOG(ERROR) << "Cannot find window #" << w_index;
372 return false; 420 return false;
373 } 421 }
374 } 422 }
375 423
376 return true; 424 return true;
377 } 425 }
378 426
379 void DeleteForeignSession(int index, std::string session_tag) { 427 void DeleteForeignSession(int profile_index, std::string session_tag) {
380 ProfileSyncServiceFactory::GetInstance() 428 ProfileSyncServiceFactory::GetInstance()
381 ->GetForProfile(test()->GetProfile(index)) 429 ->GetForProfile(test()->GetProfile(profile_index))
382 ->GetOpenTabsUIDelegate() 430 ->GetOpenTabsUIDelegate()
383 ->DeleteForeignSession(session_tag); 431 ->DeleteForeignSession(session_tag);
384 } 432 }
385 433
386 } // namespace sessions_helper 434 } // namespace sessions_helper
387 435
388 ForeignSessionsMatchChecker::ForeignSessionsMatchChecker( 436 ForeignSessionsMatchChecker::ForeignSessionsMatchChecker(
389 int index, 437 int profile_index,
390 const std::vector<sessions_helper::ScopedWindowMap>& windows) 438 const std::vector<sessions_helper::ScopedWindowMap>& windows)
391 : MultiClientStatusChangeChecker( 439 : MultiClientStatusChangeChecker(
392 sync_datatype_helper::test()->GetSyncServices()), 440 sync_datatype_helper::test()->GetSyncServices()),
393 index_(index), 441 profile_index_(profile_index),
394 windows_(windows) {} 442 windows_(windows) {}
395 443
396 bool ForeignSessionsMatchChecker::IsExitConditionSatisfied() { 444 bool ForeignSessionsMatchChecker::IsExitConditionSatisfied() {
397 return sessions_helper::CheckForeignSessionsAgainst(index_, windows_); 445 return sessions_helper::CheckForeignSessionsAgainst(profile_index_, windows_);
398 } 446 }
399 447
400 std::string ForeignSessionsMatchChecker::GetDebugMessage() const { 448 std::string ForeignSessionsMatchChecker::GetDebugMessage() const {
401 return "Waiting for matching foreign sessions"; 449 return "Waiting for matching foreign sessions";
402 } 450 }
451
452 SessionHierarchyMatchChecker::SessionHierarchyMatchChecker(
453 browser_sync::ProfileSyncService* service,
454 const fake_server::SessionsHierarchy& sessions_hierarchy,
455 fake_server::FakeServer* fake_server)
456 : SingleClientStatusChangeChecker(service),
457 sessions_hierarchy_(sessions_hierarchy),
458 verifier_(fake_server) {}
459
460 bool SessionHierarchyMatchChecker::IsExitConditionSatisfied() {
461 return verifier_.VerifySessions(sessions_hierarchy_);
462 }
463
464 std::string SessionHierarchyMatchChecker::GetDebugMessage() const {
465 return "Waiting for matching sessions hierarchy to be reflected in fake "
466 "server";
467 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698