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

Side by Side Diff: chrome/test/live_sync/live_sessions_sync_test.h

Issue 6462015: Cleanup more test code. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Copyright update Created 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 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 #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>
11 11
12 #include "base/compiler_specific.h" 12 #include "base/compiler_specific.h"
13 #include "base/scoped_vector.h" 13 #include "base/scoped_vector.h"
14 #include "base/ref_counted.h" 14 #include "base/ref_counted.h"
15 #include "base/synchronization/waitable_event.h" 15 #include "base/synchronization/waitable_event.h"
16 #include "chrome/browser/browser_window.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/renderer_host/render_view_host_delegate.h"
19 #include "chrome/browser/sessions/base_session_service.h" 16 #include "chrome/browser/sessions/base_session_service.h"
20 #include "chrome/browser/sessions/session_service_test_helper.h" 17 #include "chrome/browser/sessions/session_service_test_helper.h"
21 #include "chrome/browser/tab_contents/tab_contents.h" 18 #include "chrome/browser/sessions/session_types.h"
22 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
23 #include "chrome/test/live_sync/live_sync_test.h" 19 #include "chrome/test/live_sync/live_sync_test.h"
24 #include "chrome/test/ui_test_utils.h" 20
25 #include "googleurl/src/gurl.h" 21 class GURL;
26 #include "webkit/glue/window_open_disposition.h" 22 class Profile;
23 class TabContents;
27 24
28 // Helper for accessing session service internals. 25 // Helper for accessing session service internals.
29 class TestSessionService 26 class TestSessionService
30 : public SessionServiceTestHelper, 27 : public SessionServiceTestHelper,
31 public base::RefCountedThreadSafe<TestSessionService> { 28 public base::RefCountedThreadSafe<TestSessionService> {
32 public: 29 public:
33 TestSessionService() 30 TestSessionService();
34 : SessionServiceTestHelper(), 31 TestSessionService(SessionService* service, Profile* profile);
35 done_saving_(false, false),
36 got_windows_(false, false),
37 profile_(NULL),
38 window_bounds_(0, 1, 2, 3) {}
39 TestSessionService(SessionService* service, Profile* profile)
40 : SessionServiceTestHelper(service),
41 done_saving_(false, false),
42 got_windows_(false, false),
43 profile_(profile),
44 window_bounds_(0, 1, 2, 3) {}
45 32
46 void SetUp() { 33 void SetUp();
47 ASSERT_TRUE(service()) << "SetUp() called without setting SessionService";
48 ASSERT_TRUE(profile_);
49 service()->SetWindowType(window_id_, Browser::TYPE_NORMAL);
50 service()->SetWindowBounds(window_id_, window_bounds_, false);
51 }
52 34
53 // Trigger saving the current session commands to file. 35 // Trigger saving the current session commands to file.
54 void Save() { 36 void Save();
55 service()->Save();
56 }
57 37
58 // Synchronously reads the contents of the current session. 38 // Synchronously reads the contents of the current session.
59 std::vector<SessionWindow*>* ReadWindows() { 39 std::vector<SessionWindow*>* ReadWindows();
60 // The session backend will post the callback as a task to whatever thread
61 // called it. In our case, we don't want the main test thread to have tasks
62 // posted to, so we perform the actual call to session service from the same
63 // thread the work will be done on (backend_thread aka file thread). As a
64 // result, it will directly call back, instead of posting a task, and we can
65 // block on that callback.
66 BrowserThread::PostTask(
67 BrowserThread::FILE,
68 FROM_HERE,
69 NewRunnableMethod(this, &TestSessionService::DoReadWindows));
70
71 // Wait for callback to happen.
72 got_windows_.Wait();
73
74 // By the time we reach here we've received the windows, so return them.
75 return windows_;
76 }
77 40
78 // Makes the actual call to session service. 41 // Makes the actual call to session service.
79 // Lives on the file thread. 42 // Lives on the file thread.
80 void DoReadWindows() { 43 void DoReadWindows();
81 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
82 LOG(ERROR) << "DoReadWindows called from wrong thread!";
83 windows_ = NULL;
84 got_windows_.Signal();
85 return;
86 }
87 SessionService::SessionCallback* callback =
88 NewCallback(this, &TestSessionService::OnGotSession);
89 service()->GetCurrentSession(&consumer_, callback);
90 }
91 44
92 // Internal method used in the callback to obtain the current session. 45 // Internal method used in the callback to obtain the current session.
93 // Lives on and called from backend thread (file_thread). 46 // Lives on and called from backend thread (file_thread).
94 // We don't own windows so need to make a deep copy. 47 // We don't own windows so need to make a deep copy.
95 // In this case, we only copy those values compared against in WindowsMatch 48 // In this case, we only copy those values compared against in WindowsMatch
96 // (number of windows, number of tabs, and navigations within tabs). 49 // (number of windows, number of tabs, and navigations within tabs).
97 void OnGotSession(int handle, std::vector<SessionWindow*>* windows) { 50 void OnGotSession(int handle, std::vector<SessionWindow*>* windows);
98 scoped_ptr<ForeignSession> foreign_session(new ForeignSession());
99 for (size_t w = 0; w < windows->size(); ++w) {
100 const SessionWindow& window = *windows->at(w);
101 scoped_ptr<SessionWindow> new_window(new SessionWindow());
102 for (size_t t = 0; t < window.tabs.size(); ++t) {
103 const SessionTab& tab = *window.tabs.at(t);
104 scoped_ptr<SessionTab> new_tab(new SessionTab());
105 new_tab->navigations.resize(tab.navigations.size());
106 std::copy(tab.navigations.begin(), tab.navigations.end(),
107 new_tab->navigations.begin());
108 new_window->tabs.push_back(new_tab.release());
109 }
110 foreign_session->windows.push_back(new_window.release());
111 }
112 windows_ = &(foreign_session->windows);
113 foreign_sessions_.push_back(foreign_session.release());
114 got_windows_.Signal();
115 }
116 51
117 private: 52 private:
118 ~TestSessionService() { 53 virtual ~TestSessionService();
119 ReleaseService(); // We don't own this, so don't destroy it.
120 }
121 54
122 friend class base::RefCountedThreadSafe<TestSessionService>; 55 friend class base::RefCountedThreadSafe<TestSessionService>;
123 56
124 // Current session buffer. 57 // Current session buffer.
125 std::vector<SessionWindow*>* windows_; 58 std::vector<SessionWindow*>* windows_;
126 59
127 // List of all foreign sessions we created in ReadWindows. We delete them all 60 // List of all foreign sessions we created in ReadWindows. We delete them all
128 // at destruction time so that complex tests can keep comparing against old 61 // at destruction time so that complex tests can keep comparing against old
129 // SessionWindow data. Note that since we're constantly creating new foreign 62 // SessionWindow data. Note that since we're constantly creating new foreign
130 // sessions, we don't have to worry about duplicates. 63 // sessions, we don't have to worry about duplicates.
(...skipping 10 matching lines...) Expand all
141 74
142 // Sync profile associated with this session service. 75 // Sync profile associated with this session service.
143 Profile* profile_; 76 Profile* profile_;
144 77
145 SessionID window_id_; 78 SessionID window_id_;
146 const gfx::Rect window_bounds_; 79 const gfx::Rect window_bounds_;
147 }; 80 };
148 81
149 class LiveSessionsSyncTest : public LiveSyncTest { 82 class LiveSessionsSyncTest : public LiveSyncTest {
150 public: 83 public:
151 explicit LiveSessionsSyncTest(TestType test_type) 84 explicit LiveSessionsSyncTest(TestType test_type);
152 : LiveSyncTest(test_type), 85 virtual ~LiveSessionsSyncTest();
153 done_closing_(false, false) {}
154 virtual ~LiveSessionsSyncTest() {}
155 86
156 // Used to access the session service associated with a specific sync profile. 87 // Used to access the session service associated with a specific sync profile.
157 SessionService* GetSessionService(int index) { 88 SessionService* GetSessionService(int index);
158 return GetProfile(index)->GetSessionService();
159 }
160 89
161 // Used to access the session service test helper associated with a specific 90 // Used to access the session service test helper associated with a specific
162 // sync profile. 91 // sync profile.
163 TestSessionService* GetHelper(int index) { 92 TestSessionService* GetHelper(int index);
164 return test_session_services_[index]->get();
165 }
166 93
167 // Used to access the browser associated with a specific sync profile. 94 // Used to access the browser associated with a specific sync profile.
168 Browser* GetBrowser(int index) { 95 Browser* GetBrowser(int index);
169 return browsers_[index];
170 }
171 96
172 // Sets up the TestSessionService helper and the new browser windows. 97 // Sets up the TestSessionService helper and the new browser windows.
173 bool SetupClients() { 98 bool SetupClients();
174 if (!LiveSyncTest::SetupClients()) {
175 return false;
176 }
177
178 // Go through and make the TestSessionServices and Browsers.
179 for (int i = 0; i < num_clients(); ++i) {
180 scoped_refptr<TestSessionService>* new_tester =
181 new scoped_refptr<TestSessionService>;
182 *new_tester = new TestSessionService(
183 GetSessionService(i), GetProfile(i));
184 test_session_services_.push_back(new_tester);
185 GetHelper(i)->SetUp();
186
187 browsers_.push_back(Browser::Create(GetProfile(i)));
188 }
189
190 return true;
191 }
192 99
193 // Open a single tab and return the TabContents. TabContents must be checked 100 // Open a single tab and return the TabContents. TabContents must be checked
194 // to ensure the tab opened successsfully. 101 // to ensure the tab opened successsfully.
195 TabContents* OpenTab(int index, GURL url) WARN_UNUSED_RESULT { 102 TabContents* OpenTab(int index, GURL url) WARN_UNUSED_RESULT;
196 TabContents* tab = GetBrowser(index)->
197 AddSelectedTabWithURL(url, PageTransition::START_PAGE)->tab_contents();
198
199 // Wait for the page to finish loading.
200 ui_test_utils::WaitForNavigation(
201 &GetBrowser(index)->GetSelectedTabContents()->controller());
202
203 return tab;
204 }
205 103
206 // Creates and verifies the creation of a new window with one tab displaying 104 // Creates and verifies the creation of a new window with one tab displaying
207 // the specified GURL. 105 // the specified GURL.
208 // Returns: the SessionWindow associated with the new window. 106 // Returns: the SessionWindow associated with the new window.
209 std::vector<SessionWindow*>* InitializeNewWindowWithTab(int index, GURL url) 107 std::vector<SessionWindow*>* InitializeNewWindowWithTab(int index, GURL url)
210 WARN_UNUSED_RESULT { 108 WARN_UNUSED_RESULT;
211 if (!OpenTab(index, url)) {
212 return NULL;
213 }
214 GetHelper(index)->Save();
215 std::vector<SessionWindow*>* windows = GetHelper(index)->ReadWindows();
216 if (windows->size() != 1) {
217 LOG(ERROR) << "InitializeNewWindowWithTab called with open windows!";
218 return NULL;
219 }
220 if (1U != (*windows)[0]->tabs.size())
221 return NULL;
222 SortSessionWindows(windows);
223 return windows;
224 }
225 109
226 // Checks that window count and foreign session count are 0. 110 // Checks that window count and foreign session count are 0.
227 bool CheckInitialState(int index) WARN_UNUSED_RESULT { 111 bool CheckInitialState(int index) WARN_UNUSED_RESULT;
228 if (0 != GetNumWindows(index))
229 return false;
230 if (0 != GetNumForeignSessions(index))
231 return false;
232 return true;
233 }
234 112
235 // Returns number of open windows for a profile. 113 // Returns number of open windows for a profile.
236 int GetNumWindows(int index) { 114 int GetNumWindows(int index);
237 // We don't own windows.
238 std::vector<SessionWindow*>* windows = GetHelper(index)->ReadWindows();
239 return windows->size();
240 }
241 115
242 // Returns number of foreign sessions for a profile. 116 // Returns number of foreign sessions for a profile.
243 int GetNumForeignSessions(int index) { 117 int GetNumForeignSessions(int index);
244 std::vector<const ForeignSession*> sessions;
245 if (!GetProfile(index)->GetProfileSyncService()->
246 GetSessionModelAssociator()->GetAllForeignSessions(&sessions))
247 return 0;
248 return sessions.size();
249 }
250 118
251 // Fills the sessions vector with the model associator's foreign session data. 119 // Fills the sessions vector with the model associator's foreign session data.
252 // Caller owns |sessions|, but not ForeignSession objects within. 120 // Caller owns |sessions|, but not ForeignSession objects within.
253 bool GetSessionData(int index, std::vector<const ForeignSession*>* sessions) 121 bool GetSessionData(int index, std::vector<const ForeignSession*>* sessions)
254 WARN_UNUSED_RESULT { 122 WARN_UNUSED_RESULT;
255 if (!GetProfile(index)->GetProfileSyncService()->
256 GetSessionModelAssociator()->GetAllForeignSessions(sessions))
257 return false;
258 SortForeignSessions(sessions);
259 return true;
260 }
261 123
262 // Compare session windows based on their first tab's url. 124 // Compare session windows based on their first tab's url.
263 // Returns true if the virtual url of the lhs is < the rhs. 125 // Returns true if the virtual url of the lhs is < the rhs.
264 static bool CompareSessionWindows(SessionWindow* lhs, SessionWindow* rhs) { 126 static bool CompareSessionWindows(SessionWindow* lhs, SessionWindow* rhs);
265 if (!lhs ||
266 !rhs ||
267 lhs->tabs.size() < 1 ||
268 rhs->tabs.size() < 1 ||
269 lhs->tabs[0]->navigations.size() < 1 ||
270 rhs->tabs[0]->navigations.size() < 1) {
271 // Catchall for uncomparable data.
272 return false;
273 }
274
275 return lhs->tabs[0]->navigations[0].virtual_url() <
276 rhs->tabs[0]->navigations[0].virtual_url();
277 }
278 127
279 // Sort session windows using our custom comparator (first tab url 128 // Sort session windows using our custom comparator (first tab url
280 // comparison). 129 // comparison).
281 void SortSessionWindows(std::vector<SessionWindow*>* windows) { 130 void SortSessionWindows(std::vector<SessionWindow*>* windows);
282 std::sort(windows->begin(), windows->end(),
283 LiveSessionsSyncTest::CompareSessionWindows);
284 }
285 131
286 // Compares a foreign session based on the first session window. 132 // Compares a foreign session based on the first session window.
287 // Returns true based on the comparison of the session windows. 133 // Returns true based on the comparison of the session windows.
288 static bool CompareForeignSessions( 134 static bool CompareForeignSessions(
289 const ForeignSession* lhs, 135 const ForeignSession* lhs,
290 const ForeignSession* rhs) { 136 const ForeignSession* rhs);
291 if (!lhs ||
292 !rhs ||
293 lhs->windows.size() < 1 ||
294 rhs->windows.size() < 1) {
295 // Catchall for uncomparable data.
296 return false;
297 }
298
299 return CompareSessionWindows(lhs->windows[0], rhs->windows[0]);
300 }
301 137
302 // Sort a foreign session vector using our custom foreign session comparator. 138 // Sort a foreign session vector using our custom foreign session comparator.
303 void SortForeignSessions(std::vector<const ForeignSession*>* sessions) { 139 void SortForeignSessions(std::vector<const ForeignSession*>* sessions);
304 std::sort(sessions->begin(), sessions->end(),
305 LiveSessionsSyncTest::CompareForeignSessions);
306 }
307 140
308 // Compares two tab navigations base on the parameters we sync. 141 // Compares two tab navigations base on the parameters we sync.
309 // (Namely, we don't sync state or type mask) 142 // (Namely, we don't sync state or type mask)
310 bool NavigationEquals(const TabNavigation& expected, 143 bool NavigationEquals(const TabNavigation& expected,
311 const TabNavigation& actual) { 144 const TabNavigation& actual);
312 if (expected.virtual_url() != actual.virtual_url())
313 return false;
314 if (expected.referrer() != actual.referrer())
315 return false;
316 if (expected.title() != actual.title())
317 return false;
318 if (expected.transition() != actual.transition())
319 return false;
320 return true;
321 }
322 145
323 // Verifies that two SessionWindows match. 146 // Verifies that two SessionWindows match.
324 // Returns: 147 // Returns:
325 // - true if all the following match: 148 // - true if all the following match:
326 // 1. number of SessionWindows, 149 // 1. number of SessionWindows,
327 // 2. number of tabs per SessionWindow, 150 // 2. number of tabs per SessionWindow,
328 // 3. number of tab navigations per tab, 151 // 3. number of tab navigations per tab,
329 // 4. actual tab navigations contents 152 // 4. actual tab navigations contents
330 // - false otherwise. 153 // - false otherwise.
331 bool WindowsMatch(const std::vector<SessionWindow*> &win1, 154 bool WindowsMatch(const std::vector<SessionWindow*> &win1,
332 const std::vector<SessionWindow*> &win2) WARN_UNUSED_RESULT { 155 const std::vector<SessionWindow*> &win2) WARN_UNUSED_RESULT;
333 SessionTab* client0_tab;
334 SessionTab* client1_tab;
335 if (win1.size() != win2.size())
336 return false;
337 for (size_t i = 0; i < win1.size(); ++i) {
338 if (win1[i]->tabs.size() != win2[i]->tabs.size())
339 return false;
340 for (size_t j = 0; j < win1[i]->tabs.size(); ++j) {
341 client0_tab = win1[i]->tabs[j];
342 client1_tab = win2[i]->tabs[j];
343 for (size_t k = 0; k < client0_tab->navigations.size(); ++k) {
344 if (!NavigationEquals(client0_tab->navigations[k],
345 client1_tab->navigations[k])) {
346 return false;
347 }
348 }
349 }
350 }
351
352 return true;
353 }
354 156
355 // Retrieves the foreign sessions for a particular profile and compares them 157 // Retrieves the foreign sessions for a particular profile and compares them
356 // with a reference SessionWindow list. 158 // with a reference SessionWindow list.
357 // Returns true if the session windows of the foreign session matches the 159 // Returns true if the session windows of the foreign session matches the
358 // reference. 160 // reference.
359 bool CheckForeignSessionsAgainst(int index, 161 bool CheckForeignSessionsAgainst(int index,
360 const std::vector<std::vector<SessionWindow*>* >& windows) 162 const std::vector<std::vector<SessionWindow*>* >& windows)
361 WARN_UNUSED_RESULT { 163 WARN_UNUSED_RESULT;
362 std::vector<const ForeignSession*> sessions;
363 if (!GetSessionData(index, &sessions))
364 return false;
365 if ((size_t)(num_clients()-1) != sessions.size())
366 return false;
367
368 int window_index = 0;
369 for (size_t j = 0; j < sessions.size(); ++j, ++window_index) {
370 if (window_index == index)
371 window_index++; // Skip self.
372 if (!WindowsMatch(sessions[j]->windows, *windows[window_index]))
373 return false;
374 }
375
376 return true;
377 }
378 164
379 protected: 165 protected:
380 // Clean up our mess. 166 // Clean up our mess.
381 virtual void CleanUpOnMainThread() { 167 virtual void CleanUpOnMainThread();
382 // Close all browsers. We need to do this now, as opposed to letting the
383 // test framework handle it, because we created our own browser for each
384 // sync profile.
385 BrowserList::CloseAllBrowsers();
386 ui_test_utils::RunAllPendingInMessageLoop();
387
388 // All browsers should be closed at this point, else when the framework
389 // calls QuitBrowsers() we could see memory corruption.
390 ASSERT_EQ(0U, BrowserList::size());
391
392 LiveSyncTest::CleanUpOnMainThread();
393 }
394 168
395 // Vector of our TestSessionService helpers. 169 // Vector of our TestSessionService helpers.
396 ScopedVector<scoped_refptr<TestSessionService> > test_session_services_; 170 ScopedVector<scoped_refptr<TestSessionService> > test_session_services_;
397 171
398 // Vector of our browsers for each profile. 172 // Vector of our browsers for each profile.
399 std::vector<Browser*> browsers_; 173 std::vector<Browser*> browsers_;
400 174
401 // Barrier for closing the browsers we create in UI thread. 175 // Barrier for closing the browsers we create in UI thread.
402 base::WaitableEvent done_closing_; 176 base::WaitableEvent done_closing_;
403 177
(...skipping 23 matching lines...) Expand all
427 public: 201 public:
428 MultipleClientLiveSessionsSyncTest() 202 MultipleClientLiveSessionsSyncTest()
429 : LiveSessionsSyncTest(MULTIPLE_CLIENT) {} 203 : LiveSessionsSyncTest(MULTIPLE_CLIENT) {}
430 virtual ~MultipleClientLiveSessionsSyncTest() {} 204 virtual ~MultipleClientLiveSessionsSyncTest() {}
431 205
432 private: 206 private:
433 DISALLOW_COPY_AND_ASSIGN(MultipleClientLiveSessionsSyncTest); 207 DISALLOW_COPY_AND_ASSIGN(MultipleClientLiveSessionsSyncTest);
434 }; 208 };
435 209
436 #endif // CHROME_TEST_LIVE_SYNC_LIVE_SESSIONS_SYNC_TEST_H_ 210 #endif // CHROME_TEST_LIVE_SYNC_LIVE_SESSIONS_SYNC_TEST_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698