| OLD | NEW |
| 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 index, const sync_sessions::SyncedSession** session) { |
| 39 return ProfileSyncServiceFactory::GetInstance()->GetForProfile( | 47 return ProfileSyncServiceFactory::GetInstance() |
| 40 test()->GetProfile(index))->GetOpenTabsUIDelegate()-> | 48 ->GetForProfile(test()->GetProfile(index)) |
| 41 GetLocalSession(session); | 49 ->GetOpenTabsUIDelegate() |
| 50 ->GetLocalSession(session); |
| 42 } | 51 } |
| 43 | 52 |
| 44 bool ModelAssociatorHasTabWithUrl(int index, const GURL& url) { | 53 bool ModelAssociatorHasTabWithUrl(int index, const GURL& url) { |
| 45 content::RunAllPendingInMessageLoop(); | 54 content::RunAllPendingInMessageLoop(); |
| 46 const sync_sessions::SyncedSession* local_session; | 55 const sync_sessions::SyncedSession* local_session; |
| 47 if (!GetLocalSession(index, &local_session)) { | 56 if (!GetLocalSession(index, &local_session)) { |
| 48 return false; | 57 return false; |
| 49 } | 58 } |
| 50 | 59 |
| 51 if (local_session->windows.size() == 0) { | 60 if (local_session->windows.size() == 0) { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 78 } | 87 } |
| 79 return true; | 88 return true; |
| 80 } | 89 } |
| 81 } | 90 } |
| 82 } | 91 } |
| 83 DVLOG(1) << "Could not find tab with url " << url.spec(); | 92 DVLOG(1) << "Could not find tab with url " << url.spec(); |
| 84 return false; | 93 return false; |
| 85 } | 94 } |
| 86 | 95 |
| 87 bool OpenTab(int index, const GURL& url) { | 96 bool OpenTab(int index, const GURL& url) { |
| 88 DVLOG(1) << "Opening tab: " << url.spec() << " using profile " | 97 DVLOG(1) << "Opening tab: " << url.spec() << " using browser " << index |
| 89 << index << "."; | 98 << "."; |
| 90 chrome::ShowSingletonTab(test()->GetBrowser(index), url); | 99 return OpenTabAtIndex(index, 0, url); |
| 91 return WaitForTabsToLoad(index, std::vector<GURL>(1, url)); | 100 } |
| 101 |
| 102 bool OpenTabAtIndex(int index, int tab_index, const GURL& url) { |
| 103 chrome::AddTabAt(test()->GetBrowser(index), url, tab_index, true); |
| 104 return WaitForTabsToLoad(index, {url}); |
| 92 } | 105 } |
| 93 | 106 |
| 94 bool OpenMultipleTabs(int index, const std::vector<GURL>& urls) { | 107 bool OpenMultipleTabs(int index, const std::vector<GURL>& urls) { |
| 95 Browser* browser = test()->GetBrowser(index); | 108 Browser* browser = test()->GetBrowser(index); |
| 96 for (std::vector<GURL>::const_iterator it = urls.begin(); | 109 for (std::vector<GURL>::const_iterator it = urls.begin(); |
| 97 it != urls.end(); ++it) { | 110 it != urls.end(); ++it) { |
| 98 DVLOG(1) << "Opening tab: " << it->spec() << " using profile " << index | 111 DVLOG(1) << "Opening tab: " << it->spec() << " using browser " << index |
| 99 << "."; | 112 << "."; |
| 100 chrome::ShowSingletonTab(browser, *it); | 113 chrome::ShowSingletonTab(browser, *it); |
| 101 } | 114 } |
| 102 return WaitForTabsToLoad(index, urls); | 115 return WaitForTabsToLoad(index, urls); |
| 103 } | 116 } |
| 104 | 117 |
| 118 void MoveTab(int from_index, int to_index, int tab_index) { |
| 119 content::WebContents* detached_contents = |
| 120 test() |
| 121 ->GetBrowser(from_index) |
| 122 ->tab_strip_model() |
| 123 ->DetachWebContentsAt(tab_index); |
| 124 |
| 125 TabStripModel* target_strip = test()->GetBrowser(to_index)->tab_strip_model(); |
| 126 target_strip->InsertWebContentsAt(target_strip->count(), detached_contents, |
| 127 TabStripModel::ADD_ACTIVE); |
| 128 } |
| 129 |
| 130 bool NavigateTab(int index, const GURL& url) { |
| 131 chrome::NavigateParams params(test()->GetBrowser(index), url, |
| 132 ui::PAGE_TRANSITION_LINK); |
| 133 params.disposition = WindowOpenDisposition::CURRENT_TAB; |
| 134 |
| 135 ui_test_utils::NavigateToURL(¶ms); |
| 136 return WaitForTabsToLoad(index, {url}); |
| 137 } |
| 138 |
| 139 void NavigateTabBack(int index) { |
| 140 test() |
| 141 ->GetBrowser(index) |
| 142 ->tab_strip_model() |
| 143 ->GetWebContentsAt(0) |
| 144 ->GetController() |
| 145 .GoBack(); |
| 146 } |
| 147 |
| 148 void NavigateTabForward(int index) { |
| 149 test() |
| 150 ->GetBrowser(index) |
| 151 ->tab_strip_model() |
| 152 ->GetWebContentsAt(0) |
| 153 ->GetController() |
| 154 .GoForward(); |
| 155 } |
| 156 |
| 105 namespace { | 157 namespace { |
| 106 | 158 |
| 107 class TabEventHandler : public sync_sessions::LocalSessionEventHandler { | 159 class TabEventHandler : public sync_sessions::LocalSessionEventHandler { |
| 108 public: | 160 public: |
| 109 TabEventHandler() : weak_factory_(this) { | 161 TabEventHandler() : weak_factory_(this) { |
| 110 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 162 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 111 FROM_HERE, | 163 FROM_HERE, |
| 112 base::Bind(&TabEventHandler::QuitLoop, weak_factory_.GetWeakPtr()), | 164 base::Bind(&TabEventHandler::QuitLoop, weak_factory_.GetWeakPtr()), |
| 113 TestTimeouts::action_max_timeout()); | 165 TestTimeouts::action_max_timeout()); |
| 114 } | 166 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 new_tab->navigations.begin()); | 244 new_tab->navigations.begin()); |
| 193 new_window->tabs.push_back(std::move(new_tab)); | 245 new_window->tabs.push_back(std::move(new_tab)); |
| 194 } | 246 } |
| 195 auto id = new_window->window_id.id(); | 247 auto id = new_window->window_id.id(); |
| 196 (*local_windows)[id] = std::move(new_window); | 248 (*local_windows)[id] = std::move(new_window); |
| 197 } | 249 } |
| 198 | 250 |
| 199 return true; | 251 return true; |
| 200 } | 252 } |
| 201 | 253 |
| 202 bool OpenTabAndGetLocalWindows(int index, | |
| 203 const GURL& url, | |
| 204 ScopedWindowMap* local_windows) { | |
| 205 if (!OpenTab(index, url)) { | |
| 206 return false; | |
| 207 } | |
| 208 return GetLocalWindows(index, local_windows); | |
| 209 } | |
| 210 | |
| 211 bool CheckInitialState(int index) { | 254 bool CheckInitialState(int index) { |
| 212 if (0 != GetNumWindows(index)) | 255 if (0 != GetNumWindows(index)) |
| 213 return false; | 256 return false; |
| 214 if (0 != GetNumForeignSessions(index)) | 257 if (0 != GetNumForeignSessions(index)) |
| 215 return false; | 258 return false; |
| 216 return true; | 259 return true; |
| 217 } | 260 } |
| 218 | 261 |
| 219 int GetNumWindows(int index) { | 262 int GetNumWindows(int index) { |
| 220 const sync_sessions::SyncedSession* local_session; | 263 const sync_sessions::SyncedSession* local_session; |
| 221 if (!GetLocalSession(index, &local_session)) { | 264 if (!GetLocalSession(index, &local_session)) { |
| 222 return 0; | 265 return 0; |
| 223 } | 266 } |
| 224 return local_session->windows.size(); | 267 return local_session->windows.size(); |
| 225 } | 268 } |
| 226 | 269 |
| 227 int GetNumForeignSessions(int index) { | 270 int GetNumForeignSessions(int index) { |
| 228 SyncedSessionVector sessions; | 271 SyncedSessionVector sessions; |
| 229 if (!ProfileSyncServiceFactory::GetInstance()->GetForProfile( | 272 if (!ProfileSyncServiceFactory::GetInstance() |
| 230 test()->GetProfile(index))-> | 273 ->GetForProfile(test()->GetProfile(index)) |
| 231 GetOpenTabsUIDelegate()->GetAllForeignSessions( | 274 ->GetOpenTabsUIDelegate() |
| 232 &sessions)) { | 275 ->GetAllForeignSessions(&sessions)) { |
| 233 return 0; | 276 return 0; |
| 234 } | 277 } |
| 235 return sessions.size(); | 278 return sessions.size(); |
| 236 } | 279 } |
| 237 | 280 |
| 238 bool GetSessionData(int index, SyncedSessionVector* sessions) { | 281 bool GetSessionData(int index, SyncedSessionVector* sessions) { |
| 239 if (!ProfileSyncServiceFactory::GetInstance()->GetForProfile( | 282 if (!ProfileSyncServiceFactory::GetInstance() |
| 240 test()->GetProfile(index))-> | 283 ->GetForProfile(test()->GetProfile(index)) |
| 241 GetOpenTabsUIDelegate()->GetAllForeignSessions( | 284 ->GetOpenTabsUIDelegate() |
| 242 sessions)) { | 285 ->GetAllForeignSessions(sessions)) { |
| 243 return false; | 286 return false; |
| 244 } | 287 } |
| 245 SortSyncedSessions(sessions); | 288 SortSyncedSessions(sessions); |
| 246 return true; | 289 return true; |
| 247 } | 290 } |
| 248 | 291 |
| 249 bool CompareSyncedSessions(const sync_sessions::SyncedSession* lhs, | 292 bool CompareSyncedSessions(const sync_sessions::SyncedSession* lhs, |
| 250 const sync_sessions::SyncedSession* rhs) { | 293 const sync_sessions::SyncedSession* rhs) { |
| 251 if (!lhs || | 294 if (!lhs || |
| 252 !rhs || | 295 !rhs || |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 } // namespace | 381 } // namespace |
| 339 | 382 |
| 340 bool WindowsMatch(const ScopedWindowMap& win1, const ScopedWindowMap& win2) { | 383 bool WindowsMatch(const ScopedWindowMap& win1, const ScopedWindowMap& win2) { |
| 341 return WindowsMatchImpl(win1, win2); | 384 return WindowsMatchImpl(win1, win2); |
| 342 } | 385 } |
| 343 | 386 |
| 344 bool WindowsMatch(const SessionWindowMap& win1, const ScopedWindowMap& win2) { | 387 bool WindowsMatch(const SessionWindowMap& win1, const ScopedWindowMap& win2) { |
| 345 return WindowsMatchImpl(win1, win2); | 388 return WindowsMatchImpl(win1, win2); |
| 346 } | 389 } |
| 347 | 390 |
| 348 bool CheckForeignSessionsAgainst( | 391 bool CheckForeignSessionsAgainst(int index, |
| 349 int index, | 392 const std::vector<ScopedWindowMap>& windows) { |
| 350 const std::vector<ScopedWindowMap>& windows) { | |
| 351 SyncedSessionVector sessions; | 393 SyncedSessionVector sessions; |
| 352 | 394 |
| 353 if (!GetSessionData(index, &sessions)) { | 395 if (!GetSessionData(index, &sessions)) { |
| 354 LOG(ERROR) << "Cannot get session data"; | 396 LOG(ERROR) << "Cannot get session data"; |
| 355 return false; | 397 return false; |
| 356 } | 398 } |
| 357 | 399 |
| 358 for (size_t w_index = 0; w_index < windows.size(); ++w_index) { | 400 for (size_t w_index = 0; w_index < windows.size(); ++w_index) { |
| 359 // Skip the client's local window | 401 // Skip the client's local window |
| 360 if (static_cast<int>(w_index) == index) | 402 if (static_cast<int>(w_index) == index) { |
| 361 continue; | 403 continue; |
| 404 } |
| 362 | 405 |
| 363 size_t s_index = 0; | 406 size_t s_index = 0; |
| 364 | 407 |
| 365 for (; s_index < sessions.size(); ++s_index) { | 408 for (; s_index < sessions.size(); ++s_index) { |
| 366 if (WindowsMatch(sessions[s_index]->windows, windows[w_index])) | 409 if (WindowsMatch(sessions[s_index]->windows, windows[w_index])) |
| 367 break; | 410 break; |
| 368 } | 411 } |
| 369 | 412 |
| 370 if (s_index == sessions.size()) { | 413 if (s_index == sessions.size()) { |
| 371 LOG(ERROR) << "Cannot find window #" << w_index; | 414 LOG(ERROR) << "Cannot find window #" << w_index; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 393 index_(index), | 436 index_(index), |
| 394 windows_(windows) {} | 437 windows_(windows) {} |
| 395 | 438 |
| 396 bool ForeignSessionsMatchChecker::IsExitConditionSatisfied() { | 439 bool ForeignSessionsMatchChecker::IsExitConditionSatisfied() { |
| 397 return sessions_helper::CheckForeignSessionsAgainst(index_, windows_); | 440 return sessions_helper::CheckForeignSessionsAgainst(index_, windows_); |
| 398 } | 441 } |
| 399 | 442 |
| 400 std::string ForeignSessionsMatchChecker::GetDebugMessage() const { | 443 std::string ForeignSessionsMatchChecker::GetDebugMessage() const { |
| 401 return "Waiting for matching foreign sessions"; | 444 return "Waiting for matching foreign sessions"; |
| 402 } | 445 } |
| OLD | NEW |