| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/test/live_sync/live_sessions_sync_test.h" |
| 6 |
| 7 #include "chrome/browser/browser_thread.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/sessions/session_service.h" |
| 10 #include "chrome/browser/sync/profile_sync_service.h" |
| 11 #include "chrome/test/ui_test_utils.h" |
| 12 #include "googleurl/src/gurl.h" |
| 13 #include "chrome/browser/tab_contents/tab_contents.h" |
| 14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 15 |
| 16 TestSessionService::TestSessionService() |
| 17 : SessionServiceTestHelper(), |
| 18 done_saving_(false, false), |
| 19 got_windows_(false, false), |
| 20 profile_(NULL), |
| 21 window_bounds_(0, 1, 2, 3) {} |
| 22 |
| 23 TestSessionService::TestSessionService(SessionService* service, |
| 24 Profile* profile) |
| 25 : SessionServiceTestHelper(service), |
| 26 done_saving_(false, false), |
| 27 got_windows_(false, false), |
| 28 profile_(profile), |
| 29 window_bounds_(0, 1, 2, 3) {} |
| 30 |
| 31 void TestSessionService::SetUp() { |
| 32 ASSERT_TRUE(service()) << "SetUp() called without setting SessionService"; |
| 33 ASSERT_TRUE(profile_); |
| 34 service()->SetWindowType(window_id_, Browser::TYPE_NORMAL); |
| 35 service()->SetWindowBounds(window_id_, window_bounds_, false); |
| 36 } |
| 37 |
| 38 void TestSessionService::Save() { |
| 39 service()->Save(); |
| 40 } |
| 41 |
| 42 std::vector<SessionWindow*>* TestSessionService::ReadWindows() { |
| 43 // The session backend will post the callback as a task to whatever thread |
| 44 // called it. In our case, we don't want the main test thread to have tasks |
| 45 // posted to, so we perform the actual call to session service from the same |
| 46 // thread the work will be done on (backend_thread aka file thread). As a |
| 47 // result, it will directly call back, instead of posting a task, and we can |
| 48 // block on that callback. |
| 49 BrowserThread::PostTask( |
| 50 BrowserThread::FILE, |
| 51 FROM_HERE, |
| 52 NewRunnableMethod(this, &TestSessionService::DoReadWindows)); |
| 53 |
| 54 // Wait for callback to happen. |
| 55 got_windows_.Wait(); |
| 56 |
| 57 // By the time we reach here we've received the windows, so return them. |
| 58 return windows_; |
| 59 } |
| 60 |
| 61 |
| 62 void TestSessionService::DoReadWindows() { |
| 63 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
| 64 LOG(ERROR) << "DoReadWindows called from wrong thread!"; |
| 65 windows_ = NULL; |
| 66 got_windows_.Signal(); |
| 67 return; |
| 68 } |
| 69 SessionService::SessionCallback* callback = |
| 70 NewCallback(this, &TestSessionService::OnGotSession); |
| 71 service()->GetCurrentSession(&consumer_, callback); |
| 72 } |
| 73 |
| 74 void TestSessionService::OnGotSession(int handle, |
| 75 std::vector<SessionWindow*>* windows) { |
| 76 scoped_ptr<ForeignSession> foreign_session(new ForeignSession()); |
| 77 for (size_t w = 0; w < windows->size(); ++w) { |
| 78 const SessionWindow& window = *windows->at(w); |
| 79 scoped_ptr<SessionWindow> new_window(new SessionWindow()); |
| 80 for (size_t t = 0; t < window.tabs.size(); ++t) { |
| 81 const SessionTab& tab = *window.tabs.at(t); |
| 82 scoped_ptr<SessionTab> new_tab(new SessionTab()); |
| 83 new_tab->navigations.resize(tab.navigations.size()); |
| 84 std::copy(tab.navigations.begin(), tab.navigations.end(), |
| 85 new_tab->navigations.begin()); |
| 86 new_window->tabs.push_back(new_tab.release()); |
| 87 } |
| 88 foreign_session->windows.push_back(new_window.release()); |
| 89 } |
| 90 windows_ = &(foreign_session->windows); |
| 91 foreign_sessions_.push_back(foreign_session.release()); |
| 92 got_windows_.Signal(); |
| 93 } |
| 94 |
| 95 TestSessionService::~TestSessionService() { |
| 96 ReleaseService(); // We don't own this, so don't destroy it. |
| 97 } |
| 98 |
| 99 LiveSessionsSyncTest::LiveSessionsSyncTest(TestType test_type) |
| 100 : LiveSyncTest(test_type), |
| 101 done_closing_(false, false) {} |
| 102 |
| 103 LiveSessionsSyncTest::~LiveSessionsSyncTest() {} |
| 104 |
| 105 SessionService* LiveSessionsSyncTest::GetSessionService(int index) { |
| 106 return GetProfile(index)->GetSessionService(); |
| 107 } |
| 108 |
| 109 TestSessionService* LiveSessionsSyncTest::GetHelper(int index) { |
| 110 return test_session_services_[index]->get(); |
| 111 } |
| 112 |
| 113 Browser* LiveSessionsSyncTest::GetBrowser(int index) { |
| 114 return browsers_[index]; |
| 115 } |
| 116 |
| 117 bool LiveSessionsSyncTest::SetupClients() { |
| 118 if (!LiveSyncTest::SetupClients()) { |
| 119 return false; |
| 120 } |
| 121 |
| 122 // Go through and make the TestSessionServices and Browsers. |
| 123 for (int i = 0; i < num_clients(); ++i) { |
| 124 scoped_refptr<TestSessionService>* new_tester = |
| 125 new scoped_refptr<TestSessionService>; |
| 126 *new_tester = new TestSessionService( |
| 127 GetSessionService(i), GetProfile(i)); |
| 128 test_session_services_.push_back(new_tester); |
| 129 GetHelper(i)->SetUp(); |
| 130 |
| 131 browsers_.push_back(Browser::Create(GetProfile(i))); |
| 132 } |
| 133 |
| 134 return true; |
| 135 } |
| 136 |
| 137 TabContents* LiveSessionsSyncTest::OpenTab(int index, GURL url) { |
| 138 TabContents* tab = |
| 139 GetBrowser(index)-> |
| 140 AddSelectedTabWithURL(url, PageTransition::START_PAGE)->tab_contents(); |
| 141 |
| 142 // Wait for the page to finish loading. |
| 143 ui_test_utils::WaitForNavigation( |
| 144 &GetBrowser(index)->GetSelectedTabContents()->controller()); |
| 145 |
| 146 return tab; |
| 147 } |
| 148 |
| 149 std::vector<SessionWindow*>* LiveSessionsSyncTest::InitializeNewWindowWithTab( |
| 150 int index, GURL url) { |
| 151 if (!OpenTab(index, url)) { |
| 152 return NULL; |
| 153 } |
| 154 GetHelper(index)->Save(); |
| 155 std::vector<SessionWindow*>* windows = GetHelper(index)->ReadWindows(); |
| 156 if (windows->size() != 1) { |
| 157 LOG(ERROR) << "InitializeNewWindowWithTab called with open windows!"; |
| 158 return NULL; |
| 159 } |
| 160 if (1U != (*windows)[0]->tabs.size()) |
| 161 return NULL; |
| 162 SortSessionWindows(windows); |
| 163 return windows; |
| 164 } |
| 165 |
| 166 bool LiveSessionsSyncTest::CheckInitialState(int index) { |
| 167 if (0 != GetNumWindows(index)) |
| 168 return false; |
| 169 if (0 != GetNumForeignSessions(index)) |
| 170 return false; |
| 171 return true; |
| 172 } |
| 173 |
| 174 int LiveSessionsSyncTest::GetNumWindows(int index) { |
| 175 // We don't own windows. |
| 176 std::vector<SessionWindow*>* windows = GetHelper(index)->ReadWindows(); |
| 177 return windows->size(); |
| 178 } |
| 179 |
| 180 int LiveSessionsSyncTest::GetNumForeignSessions(int index) { |
| 181 std::vector<const ForeignSession*> sessions; |
| 182 if (!GetProfile(index)->GetProfileSyncService()-> |
| 183 GetSessionModelAssociator()->GetAllForeignSessions(&sessions)) |
| 184 return 0; |
| 185 return sessions.size(); |
| 186 } |
| 187 |
| 188 bool LiveSessionsSyncTest::GetSessionData( |
| 189 int index, |
| 190 std::vector<const ForeignSession*>* sessions) { |
| 191 if (!GetProfile(index)->GetProfileSyncService()-> |
| 192 GetSessionModelAssociator()->GetAllForeignSessions(sessions)) |
| 193 return false; |
| 194 SortForeignSessions(sessions); |
| 195 return true; |
| 196 } |
| 197 |
| 198 // static |
| 199 bool LiveSessionsSyncTest::CompareSessionWindows(SessionWindow* lhs, |
| 200 SessionWindow* rhs) { |
| 201 if (!lhs || |
| 202 !rhs || |
| 203 lhs->tabs.size() < 1 || |
| 204 rhs->tabs.size() < 1 || |
| 205 lhs->tabs[0]->navigations.size() < 1 || |
| 206 rhs->tabs[0]->navigations.size() < 1) { |
| 207 // Catchall for uncomparable data. |
| 208 return false; |
| 209 } |
| 210 |
| 211 return lhs->tabs[0]->navigations[0].virtual_url() < |
| 212 rhs->tabs[0]->navigations[0].virtual_url(); |
| 213 } |
| 214 |
| 215 void LiveSessionsSyncTest::SortSessionWindows( |
| 216 std::vector<SessionWindow*>* windows) { |
| 217 std::sort(windows->begin(), windows->end(), |
| 218 LiveSessionsSyncTest::CompareSessionWindows); |
| 219 } |
| 220 |
| 221 //static |
| 222 bool LiveSessionsSyncTest::CompareForeignSessions( |
| 223 const ForeignSession* lhs, |
| 224 const ForeignSession* rhs) { |
| 225 if (!lhs || |
| 226 !rhs || |
| 227 lhs->windows.size() < 1 || |
| 228 rhs->windows.size() < 1) { |
| 229 // Catchall for uncomparable data. |
| 230 return false; |
| 231 } |
| 232 |
| 233 return CompareSessionWindows(lhs->windows[0], rhs->windows[0]); |
| 234 } |
| 235 |
| 236 void LiveSessionsSyncTest::SortForeignSessions( |
| 237 std::vector<const ForeignSession*>* sessions) { |
| 238 std::sort(sessions->begin(), sessions->end(), |
| 239 LiveSessionsSyncTest::CompareForeignSessions); |
| 240 } |
| 241 |
| 242 bool LiveSessionsSyncTest::NavigationEquals(const TabNavigation& expected, |
| 243 const TabNavigation& actual) { |
| 244 if (expected.virtual_url() != actual.virtual_url()) |
| 245 return false; |
| 246 if (expected.referrer() != actual.referrer()) |
| 247 return false; |
| 248 if (expected.title() != actual.title()) |
| 249 return false; |
| 250 if (expected.transition() != actual.transition()) |
| 251 return false; |
| 252 return true; |
| 253 } |
| 254 |
| 255 bool LiveSessionsSyncTest::WindowsMatch( |
| 256 const std::vector<SessionWindow*> &win1, |
| 257 const std::vector<SessionWindow*> &win2) { |
| 258 SessionTab* client0_tab; |
| 259 SessionTab* client1_tab; |
| 260 if (win1.size() != win2.size()) |
| 261 return false; |
| 262 for (size_t i = 0; i < win1.size(); ++i) { |
| 263 if (win1[i]->tabs.size() != win2[i]->tabs.size()) |
| 264 return false; |
| 265 for (size_t j = 0; j < win1[i]->tabs.size(); ++j) { |
| 266 client0_tab = win1[i]->tabs[j]; |
| 267 client1_tab = win2[i]->tabs[j]; |
| 268 for (size_t k = 0; k < client0_tab->navigations.size(); ++k) { |
| 269 if (!NavigationEquals(client0_tab->navigations[k], |
| 270 client1_tab->navigations[k])) { |
| 271 return false; |
| 272 } |
| 273 } |
| 274 } |
| 275 } |
| 276 |
| 277 return true; |
| 278 } |
| 279 |
| 280 bool LiveSessionsSyncTest::CheckForeignSessionsAgainst( |
| 281 int index, |
| 282 const std::vector<std::vector<SessionWindow*>* >& windows) { |
| 283 std::vector<const ForeignSession*> sessions; |
| 284 if (!GetSessionData(index, &sessions)) |
| 285 return false; |
| 286 if ((size_t)(num_clients()-1) != sessions.size()) |
| 287 return false; |
| 288 |
| 289 int window_index = 0; |
| 290 for (size_t j = 0; j < sessions.size(); ++j, ++window_index) { |
| 291 if (window_index == index) |
| 292 window_index++; // Skip self. |
| 293 if (!WindowsMatch(sessions[j]->windows, *windows[window_index])) |
| 294 return false; |
| 295 } |
| 296 |
| 297 return true; |
| 298 } |
| 299 |
| 300 void LiveSessionsSyncTest::CleanUpOnMainThread() { |
| 301 // Close all browsers. We need to do this now, as opposed to letting the |
| 302 // test framework handle it, because we created our own browser for each |
| 303 // sync profile. |
| 304 BrowserList::CloseAllBrowsers(); |
| 305 ui_test_utils::RunAllPendingInMessageLoop(); |
| 306 |
| 307 // All browsers should be closed at this point, else when the framework |
| 308 // calls QuitBrowsers() we could see memory corruption. |
| 309 ASSERT_EQ(0U, BrowserList::size()); |
| 310 |
| 311 LiveSyncTest::CleanUpOnMainThread(); |
| 312 } |
| OLD | NEW |