| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_TEST_LIVE_SYNC_LIVE_SESSIONS_SYNC_TEST_H_ | 5 #ifndef CHROME_TEST_LIVE_SYNC_LIVE_SESSIONS_SYNC_TEST_H_ |
| 6 #define CHROME_TEST_LIVE_SYNC_LIVE_SESSIONS_SYNC_TEST_H_ | 6 #define CHROME_TEST_LIVE_SYNC_LIVE_SESSIONS_SYNC_TEST_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 return; | 86 return; |
| 87 } | 87 } |
| 88 SessionService::SessionCallback* callback = | 88 SessionService::SessionCallback* callback = |
| 89 NewCallback(this, &TestSessionService::OnGotSession); | 89 NewCallback(this, &TestSessionService::OnGotSession); |
| 90 service()->GetCurrentSession(&consumer_, callback); | 90 service()->GetCurrentSession(&consumer_, callback); |
| 91 } | 91 } |
| 92 | 92 |
| 93 // Internal method used in the callback to obtain the current session. | 93 // Internal method used in the callback to obtain the current session. |
| 94 // Lives on and called from backend thread (file_thread). | 94 // Lives on and called from backend thread (file_thread). |
| 95 // We don't own windows so need to make a deep copy. | 95 // We don't own windows so need to make a deep copy. |
| 96 // In this case, we only copy those values compared against in WindowsMatch |
| 97 // (number of windows, number of tabs, and navigations within tabs). |
| 96 void OnGotSession(int handle, std::vector<SessionWindow*>* windows) { | 98 void OnGotSession(int handle, std::vector<SessionWindow*>* windows) { |
| 97 // Hacky. We need to make a deep copy of the session windows. One way to do | 99 scoped_ptr<ForeignSession> foreign_session(new ForeignSession()); |
| 98 // this is to use the session model associators functionality to create | 100 for (size_t w = 0; w < windows->size(); ++w) { |
| 99 // foreign sessions, which themselves wrap a SessionWindow vector. We just | 101 const SessionWindow& window = *windows->at(w); |
| 100 // need to make sure to destroy all the foreign sessions we created when | 102 scoped_ptr<SessionWindow> new_window(new SessionWindow()); |
| 101 // we're done. That's what the foreign_sessions_ ScopedVector is for. | 103 for (size_t t = 0; t < window.tabs.size(); ++t) { |
| 102 sync_pb::SessionSpecifics session; | 104 const SessionTab& tab = *window.tabs.at(t); |
| 103 profile_->GetProfileSyncService()-> | 105 scoped_ptr<SessionTab> new_tab(new SessionTab()); |
| 104 GetSessionModelAssociator()-> | 106 new_tab->navigations.resize(tab.navigations.size()); |
| 105 FillSpecificsFromSessions(windows, &session); | 107 std::copy(tab.navigations.begin(), tab.navigations.end(), |
| 106 | 108 new_tab->navigations.begin()); |
| 107 std::vector<ForeignSession*> foreign_sessions; | 109 new_window->tabs.push_back(new_tab.release()); |
| 108 profile_->GetProfileSyncService()-> | 110 } |
| 109 GetSessionModelAssociator()-> | 111 foreign_session->windows.push_back(new_window.release()); |
| 110 AppendForeignSessionFromSpecifics(&session, &foreign_sessions); | 112 } |
| 111 ASSERT_EQ(foreign_sessions.size(), 1U); | 113 windows_ = &(foreign_session->windows); |
| 112 foreign_sessions_.push_back(foreign_sessions[0]); | 114 foreign_sessions_.push_back(foreign_session.release()); |
| 113 windows_ = &foreign_sessions[0]->windows; | |
| 114 got_windows_.Signal(); | 115 got_windows_.Signal(); |
| 115 } | 116 } |
| 116 | 117 |
| 117 private: | 118 private: |
| 118 ~TestSessionService() { | 119 ~TestSessionService() { |
| 119 ReleaseService(); // We don't own this, so don't destroy it. | 120 ReleaseService(); // We don't own this, so don't destroy it. |
| 120 } | 121 } |
| 121 | 122 |
| 122 friend class base::RefCountedThreadSafe<TestSessionService>; | 123 friend class base::RefCountedThreadSafe<TestSessionService>; |
| 123 | 124 |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 | 235 |
| 235 // Returns number of open windows for a profile. | 236 // Returns number of open windows for a profile. |
| 236 int GetNumWindows(int index) { | 237 int GetNumWindows(int index) { |
| 237 // We don't own windows. | 238 // We don't own windows. |
| 238 std::vector<SessionWindow*>* windows = GetHelper(index)->ReadWindows(); | 239 std::vector<SessionWindow*>* windows = GetHelper(index)->ReadWindows(); |
| 239 return windows->size(); | 240 return windows->size(); |
| 240 } | 241 } |
| 241 | 242 |
| 242 // Returns number of foreign sessions for a profile. | 243 // Returns number of foreign sessions for a profile. |
| 243 int GetNumForeignSessions(int index) { | 244 int GetNumForeignSessions(int index) { |
| 244 ScopedVector<ForeignSession> sessions; | 245 std::vector<const ForeignSession*> sessions; |
| 245 if (!GetProfile(index)->GetProfileSyncService()-> | 246 if (!GetProfile(index)->GetProfileSyncService()-> |
| 246 GetSessionModelAssociator()->GetSessionData(&sessions.get())) | 247 GetSessionModelAssociator()->GetAllForeignSessions(&sessions)) |
| 247 return 0; | 248 return 0; |
| 248 return sessions.size(); | 249 return sessions.size(); |
| 249 } | 250 } |
| 250 | 251 |
| 251 // Fills the sessions vector with the model associator's foreign session data. | 252 // Fills the sessions vector with the model associator's foreign session data. |
| 252 // Caller owns sessions. | 253 // Caller owns sessions, but not ForeignSession objects. |
| 253 bool GetSessionData(int index, std::vector<ForeignSession*>* sessions) | 254 bool GetSessionData(int index, std::vector<const ForeignSession*>* sessions) |
| 254 WARN_UNUSED_RESULT { | 255 WARN_UNUSED_RESULT { |
| 255 if (!GetProfile(index)->GetProfileSyncService()-> | 256 if (!GetProfile(index)->GetProfileSyncService()-> |
| 256 GetSessionModelAssociator()->GetSessionData(sessions)) | 257 GetSessionModelAssociator()->GetAllForeignSessions(sessions)) |
| 257 return false; | 258 return false; |
| 258 SortForeignSessions(sessions); | 259 SortForeignSessions(sessions); |
| 259 return true; | 260 return true; |
| 260 } | 261 } |
| 261 | 262 |
| 262 // Compare session windows based on their first tab's url. | 263 // Compare session windows based on their first tab's url. |
| 263 // Returns true if the virtual url of the lhs is < the rhs. | 264 // Returns true if the virtual url of the lhs is < the rhs. |
| 264 static bool CompareSessionWindows(SessionWindow* lhs, SessionWindow* rhs) { | 265 static bool CompareSessionWindows(SessionWindow* lhs, SessionWindow* rhs) { |
| 265 if (!lhs || | 266 if (!lhs || |
| 266 !rhs || | 267 !rhs || |
| (...skipping 11 matching lines...) Expand all Loading... |
| 278 | 279 |
| 279 // Sort session windows using our custom comparator (first tab url | 280 // Sort session windows using our custom comparator (first tab url |
| 280 // comparison). | 281 // comparison). |
| 281 void SortSessionWindows(std::vector<SessionWindow*>* windows) { | 282 void SortSessionWindows(std::vector<SessionWindow*>* windows) { |
| 282 std::sort(windows->begin(), windows->end(), | 283 std::sort(windows->begin(), windows->end(), |
| 283 LiveSessionsSyncTest::CompareSessionWindows); | 284 LiveSessionsSyncTest::CompareSessionWindows); |
| 284 } | 285 } |
| 285 | 286 |
| 286 // Compares a foreign session based on the first session window. | 287 // Compares a foreign session based on the first session window. |
| 287 // Returns true based on the comparison of the session windows. | 288 // Returns true based on the comparison of the session windows. |
| 288 static bool CompareForeignSessions(ForeignSession* lhs, ForeignSession* rhs) { | 289 static bool CompareForeignSessions( |
| 290 const ForeignSession* lhs, |
| 291 const ForeignSession* rhs) { |
| 289 if (!lhs || | 292 if (!lhs || |
| 290 !rhs || | 293 !rhs || |
| 291 lhs->windows.size() < 1 || | 294 lhs->windows.size() < 1 || |
| 292 rhs->windows.size() < 1) { | 295 rhs->windows.size() < 1) { |
| 293 // Catchall for uncomparable data. | 296 // Catchall for uncomparable data. |
| 294 return false; | 297 return false; |
| 295 } | 298 } |
| 296 | 299 |
| 297 return CompareSessionWindows(lhs->windows[0], rhs->windows[0]); | 300 return CompareSessionWindows(lhs->windows[0], rhs->windows[0]); |
| 298 } | 301 } |
| 299 | 302 |
| 300 // Sort a foreign session vector using our custom foreign session comparator. | 303 // Sort a foreign session vector using our custom foreign session comparator. |
| 301 void SortForeignSessions(std::vector<ForeignSession*>* sessions) { | 304 void SortForeignSessions(std::vector<const ForeignSession*>* sessions) { |
| 302 std::sort(sessions->begin(), sessions->end(), | 305 std::sort(sessions->begin(), sessions->end(), |
| 303 LiveSessionsSyncTest::CompareForeignSessions); | 306 LiveSessionsSyncTest::CompareForeignSessions); |
| 304 } | 307 } |
| 305 | 308 |
| 309 // Compares two tab navigations base on the parameters we sync. |
| 310 // (Namely, we don't sync state or type mask) |
| 311 bool NavigationEquals(const TabNavigation& expected, |
| 312 const TabNavigation& actual) { |
| 313 if (expected.virtual_url() != actual.virtual_url()) |
| 314 return false; |
| 315 if (expected.referrer() != actual.referrer()) |
| 316 return false; |
| 317 if (expected.title() != actual.title()) |
| 318 return false; |
| 319 if (expected.transition() != actual.transition()) |
| 320 return false; |
| 321 return true; |
| 322 } |
| 323 |
| 306 // Verifies that two SessionWindows match. | 324 // Verifies that two SessionWindows match. |
| 307 // Returns: | 325 // Returns: |
| 308 // - true if all the following match: | 326 // - true if all the following match: |
| 309 // 1. number of SessionWindows per vector, | 327 // 1. number of SessionWindows, |
| 310 // 2. number of tabs per SessionWindow, | 328 // 2. number of tabs per SessionWindow, |
| 311 // 3. number of tab navigations per nab, | 329 // 3. number of tab navigations per tab, |
| 312 // 4. actual tab navigations | 330 // 4. actual tab navigations contents |
| 313 // - false otherwise. | 331 // - false otherwise. |
| 314 bool WindowsMatch(const std::vector<SessionWindow*> &win1, | 332 bool WindowsMatch(const std::vector<SessionWindow*> &win1, |
| 315 const std::vector<SessionWindow*> &win2) WARN_UNUSED_RESULT { | 333 const std::vector<SessionWindow*> &win2) WARN_UNUSED_RESULT { |
| 316 SessionTab* client0_tab; | 334 SessionTab* client0_tab; |
| 317 SessionTab* client1_tab; | 335 SessionTab* client1_tab; |
| 318 if (win1.size() != win2.size()) | 336 if (win1.size() != win2.size()) |
| 319 return false; | 337 return false; |
| 320 for (size_t i = 0; i < win1.size(); ++i) { | 338 for (size_t i = 0; i < win1.size(); ++i) { |
| 321 if (win1[i]->tabs.size() != win2[i]->tabs.size()) | 339 if (win1[i]->tabs.size() != win2[i]->tabs.size()) |
| 322 return false; | 340 return false; |
| 323 for (size_t j = 0; j < win1[i]->tabs.size(); ++j) { | 341 for (size_t j = 0; j < win1[i]->tabs.size(); ++j) { |
| 324 client0_tab = win1[i]->tabs[j]; | 342 client0_tab = win1[i]->tabs[j]; |
| 325 client1_tab = win2[i]->tabs[j]; | 343 client1_tab = win2[i]->tabs[j]; |
| 326 for (size_t k = 0; k < client0_tab->navigations.size(); ++k) { | 344 for (size_t k = 0; k < client0_tab->navigations.size(); ++k) { |
| 327 GetHelper(0)->AssertNavigationEquals(client0_tab->navigations[k], | 345 if (!NavigationEquals(client0_tab->navigations[k], |
| 328 client1_tab->navigations[k]); | 346 client1_tab->navigations[k])) { |
| 347 return false; |
| 348 } |
| 329 } | 349 } |
| 330 } | 350 } |
| 331 } | 351 } |
| 332 | 352 |
| 333 return true; | 353 return true; |
| 334 } | 354 } |
| 335 | 355 |
| 336 // Retrieves the foreign sessions for a particular profile and compares them | 356 // Retrieves the foreign sessions for a particular profile and compares them |
| 337 // with a reference SessionWindow list. | 357 // with a reference SessionWindow list. |
| 338 // Returns true if the session windows of the foreign session matches the | 358 // Returns true if the session windows of the foreign session matches the |
| 339 // reference. | 359 // reference. |
| 340 bool CheckForeignSessionsAgainst(int index, | 360 bool CheckForeignSessionsAgainst(int index, |
| 341 const std::vector<std::vector<SessionWindow*>* >& windows) | 361 const std::vector<std::vector<SessionWindow*>* >& windows) |
| 342 WARN_UNUSED_RESULT { | 362 WARN_UNUSED_RESULT { |
| 343 ScopedVector<ForeignSession> sessions; | 363 std::vector<const ForeignSession*> sessions; |
| 344 if (!GetSessionData(index, &sessions.get())) | 364 if (!GetSessionData(index, &sessions)) |
| 345 return false; | 365 return false; |
| 346 if ((size_t)(num_clients()-1) != sessions.size()) | 366 if ((size_t)(num_clients()-1) != sessions.size()) |
| 347 return false; | 367 return false; |
| 348 | 368 |
| 349 int window_index = 0; | 369 int window_index = 0; |
| 350 for (size_t j = 0; j < sessions->size(); ++j, ++window_index) { | 370 for (size_t j = 0; j < sessions.size(); ++j, ++window_index) { |
| 351 if (window_index == index) | 371 if (window_index == index) |
| 352 window_index++; // Skip self. | 372 window_index++; // Skip self. |
| 353 if (!WindowsMatch(sessions[j]->windows, *windows[window_index])) | 373 if (!WindowsMatch(sessions[j]->windows, *windows[window_index])) |
| 354 return false; | 374 return false; |
| 355 } | 375 } |
| 356 | 376 |
| 357 return true; | 377 return true; |
| 358 } | 378 } |
| 359 | 379 |
| 360 protected: | 380 protected: |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 public: | 428 public: |
| 409 MultipleClientLiveSessionsSyncTest() | 429 MultipleClientLiveSessionsSyncTest() |
| 410 : LiveSessionsSyncTest(MULTIPLE_CLIENT) {} | 430 : LiveSessionsSyncTest(MULTIPLE_CLIENT) {} |
| 411 virtual ~MultipleClientLiveSessionsSyncTest() {} | 431 virtual ~MultipleClientLiveSessionsSyncTest() {} |
| 412 | 432 |
| 413 private: | 433 private: |
| 414 DISALLOW_COPY_AND_ASSIGN(MultipleClientLiveSessionsSyncTest); | 434 DISALLOW_COPY_AND_ASSIGN(MultipleClientLiveSessionsSyncTest); |
| 415 }; | 435 }; |
| 416 | 436 |
| 417 #endif // CHROME_TEST_LIVE_SYNC_LIVE_SESSIONS_SYNC_TEST_H_ | 437 #endif // CHROME_TEST_LIVE_SYNC_LIVE_SESSIONS_SYNC_TEST_H_ |
| OLD | NEW |