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

Side by Side Diff: chrome/test/ui_test_utils.cc

Issue 1371002: Fixes bug where triggering session restore while the browser was... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 9 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) 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 #include "chrome/test/ui_test_utils.h" 5 #include "chrome/test/ui_test_utils.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/json/json_reader.h" 9 #include "base/json/json_reader.h"
10 #include "base/message_loop.h" 10 #include "base/message_loop.h"
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 // The number of downloads to wait on completing. 207 // The number of downloads to wait on completing.
208 size_t wait_count_; 208 size_t wait_count_;
209 209
210 // Whether an internal message loop has been started and must be quit upon 210 // Whether an internal message loop has been started and must be quit upon
211 // all downloads completing. 211 // all downloads completing.
212 bool waiting_; 212 bool waiting_;
213 213
214 DISALLOW_COPY_AND_ASSIGN(DownloadsCompleteObserver); 214 DISALLOW_COPY_AND_ASSIGN(DownloadsCompleteObserver);
215 }; 215 };
216 216
217 // Used to block until an application modal dialog is shown.
218 class AppModalDialogObserver : public NotificationObserver {
219 public:
220 AppModalDialogObserver() : dialog_(NULL) {}
221
222 AppModalDialog* WaitForAppModalDialog() {
223 registrar_.Add(this, NotificationType::APP_MODAL_DIALOG_SHOWN,
224 NotificationService::AllSources());
225 dialog_ = NULL;
226 ui_test_utils::RunMessageLoop();
227 DCHECK(dialog_);
228 return dialog_;
229 }
230
231 virtual void Observe(NotificationType type,
232 const NotificationSource& source,
233 const NotificationDetails& details) {
234 if (type == NotificationType::APP_MODAL_DIALOG_SHOWN) {
235 registrar_.Remove(this, NotificationType::APP_MODAL_DIALOG_SHOWN,
236 NotificationService::AllSources());
237 dialog_ = Source<AppModalDialog>(source).ptr();
238 MessageLoopForUI::current()->Quit();
239 } else {
240 NOTREACHED();
241 }
242 }
243
244 private:
245 NotificationRegistrar registrar_;
246
247 AppModalDialog* dialog_;
248
249 DISALLOW_COPY_AND_ASSIGN(AppModalDialogObserver);
250 };
251
252 template <class T> 217 template <class T>
253 class SimpleNotificationObserver : public NotificationObserver { 218 class SimpleNotificationObserver : public NotificationObserver {
254 public: 219 public:
255 SimpleNotificationObserver(NotificationType notification_type, 220 SimpleNotificationObserver(NotificationType notification_type,
256 T* source) { 221 T* source) {
257 registrar_.Add(this, notification_type, Source<T>(source)); 222 registrar_.Add(this, notification_type, Source<T>(source));
258 ui_test_utils::RunMessageLoop(); 223 ui_test_utils::RunMessageLoop();
259 } 224 }
260 225
261 virtual void Observe(NotificationType type, 226 virtual void Observe(NotificationType type,
262 const NotificationSource& source, 227 const NotificationSource& source,
263 const NotificationDetails& details) { 228 const NotificationDetails& details) {
264 MessageLoopForUI::current()->Quit(); 229 MessageLoopForUI::current()->Quit();
265 } 230 }
266 231
267 private: 232 private:
268 NotificationRegistrar registrar_; 233 NotificationRegistrar registrar_;
269 234
270 DISALLOW_COPY_AND_ASSIGN(SimpleNotificationObserver); 235 DISALLOW_COPY_AND_ASSIGN(SimpleNotificationObserver);
271 }; 236 };
272 237
238 // SimpleNotificationObserver that waits for a single notification. When the
239 // notification is observer the source (of type S) is recorded and the message
240 // loop stopped. Use |source()| to access the source after the constructor
241 // returns.
242 template <class S>
243 class SimpleNotificationObserverWithSource : public NotificationObserver {
244 public:
245 SimpleNotificationObserverWithSource(NotificationType notification_type,
246 const NotificationSource& source)
247 : source_(NULL) {
248 registrar_.Add(this, notification_type, source);
249 ui_test_utils::RunMessageLoop();
250 }
251
252 virtual void Observe(NotificationType type,
253 const NotificationSource& source,
254 const NotificationDetails& details) {
255 source_ = Source<S>(source).ptr();
256
257 // Remove observer now, so that if there any other notifications we don't
258 // clobber source_.
259 registrar_.RemoveAll();
260
261 MessageLoopForUI::current()->Quit();
262 }
263
264 S* source() const { return source_; }
265
266 private:
267 S* source_;
268 NotificationRegistrar registrar_;
269
270 DISALLOW_COPY_AND_ASSIGN(SimpleNotificationObserverWithSource);
271 };
272
273 class LanguageDetectionNotificationObserver : public NotificationObserver { 273 class LanguageDetectionNotificationObserver : public NotificationObserver {
274 public: 274 public:
275 explicit LanguageDetectionNotificationObserver(TabContents* tab) { 275 explicit LanguageDetectionNotificationObserver(TabContents* tab) {
276 registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED, 276 registrar_.Add(this, NotificationType::TAB_LANGUAGE_DETERMINED,
277 Source<TabContents>(tab)); 277 Source<TabContents>(tab));
278 ui_test_utils::RunMessageLoop(); 278 ui_test_utils::RunMessageLoop();
279 } 279 }
280 280
281 virtual void Observe(NotificationType type, 281 virtual void Observe(NotificationType type,
282 const NotificationSource& source, 282 const NotificationSource& source,
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after
440 SimpleNotificationObserver<ExtensionAction> 440 SimpleNotificationObserver<ExtensionAction>
441 observer(NotificationType::EXTENSION_BROWSER_ACTION_UPDATED, 441 observer(NotificationType::EXTENSION_BROWSER_ACTION_UPDATED,
442 browser_action); 442 browser_action);
443 } 443 }
444 444
445 void WaitForLoadStop(NavigationController* controller) { 445 void WaitForLoadStop(NavigationController* controller) {
446 SimpleNotificationObserver<NavigationController> 446 SimpleNotificationObserver<NavigationController>
447 new_tab_observer(NotificationType::LOAD_STOP, controller); 447 new_tab_observer(NotificationType::LOAD_STOP, controller);
448 } 448 }
449 449
450 Browser* WaitForNewBrowser() {
451 SimpleNotificationObserverWithSource<Browser> observer(
452 NotificationType::BROWSER_WINDOW_READY,
453 NotificationService::AllSources());
454 return observer.source();
455 }
456
450 void OpenURLOffTheRecord(Profile* profile, const GURL& url) { 457 void OpenURLOffTheRecord(Profile* profile, const GURL& url) {
451 Browser::OpenURLOffTheRecord(profile, url); 458 Browser::OpenURLOffTheRecord(profile, url);
452 Browser* browser = BrowserList::FindBrowserWithType( 459 Browser* browser = BrowserList::FindBrowserWithType(
453 profile->GetOffTheRecordProfile(), Browser::TYPE_NORMAL); 460 profile->GetOffTheRecordProfile(), Browser::TYPE_NORMAL);
454 WaitForNavigations(&browser->GetSelectedTabContents()->controller(), 1); 461 WaitForNavigations(&browser->GetSelectedTabContents()->controller(), 1);
455 } 462 }
456 463
457 void NavigateToURL(Browser* browser, const GURL& url) { 464 void NavigateToURL(Browser* browser, const GURL& url) {
458 NavigateToURLBlockUntilNavigationsComplete(browser, url, 1); 465 NavigateToURLBlockUntilNavigationsComplete(browser, url, 1);
459 } 466 }
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 path = path.Append(FilePath::FromWStringHack(dir)); 556 path = path.Append(FilePath::FromWStringHack(dir));
550 path = path.Append(FilePath::FromWStringHack(file)); 557 path = path.Append(FilePath::FromWStringHack(file));
551 return net::FilePathToFileURL(path); 558 return net::FilePathToFileURL(path);
552 } 559 }
553 560
554 void WaitForDownloadCount(DownloadManager* download_manager, size_t count) { 561 void WaitForDownloadCount(DownloadManager* download_manager, size_t count) {
555 DownloadsCompleteObserver download_observer(download_manager, count); 562 DownloadsCompleteObserver download_observer(download_manager, count);
556 } 563 }
557 564
558 AppModalDialog* WaitForAppModalDialog() { 565 AppModalDialog* WaitForAppModalDialog() {
559 AppModalDialogObserver observer; 566 SimpleNotificationObserverWithSource<AppModalDialog> observer(
560 return observer.WaitForAppModalDialog(); 567 NotificationType::APP_MODAL_DIALOG_SHOWN,
568 NotificationService::AllSources());
569 return observer.source();
561 } 570 }
562 571
563 void CrashTab(TabContents* tab) { 572 void CrashTab(TabContents* tab) {
564 RenderProcessHost* rph = tab->render_view_host()->process(); 573 RenderProcessHost* rph = tab->render_view_host()->process();
565 base::KillProcess(rph->GetHandle(), 0, false); 574 base::KillProcess(rph->GetHandle(), 0, false);
566 SimpleNotificationObserver<RenderProcessHost> 575 SimpleNotificationObserver<RenderProcessHost>
567 crash_observer(NotificationType::RENDERER_PROCESS_CLOSED, rph); 576 crash_observer(NotificationType::RENDERER_PROCESS_CLOSED, rph);
568 } 577 }
569 578
570 void WaitForFocusChange(RenderViewHost* rvh) { 579 void WaitForFocusChange(RenderViewHost* rvh) {
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
720 729
721 TestWebSocketServer::~TestWebSocketServer() { 730 TestWebSocketServer::~TestWebSocketServer() {
722 scoped_ptr<CommandLine> cmd_line(CreateWebSocketServerCommandLine()); 731 scoped_ptr<CommandLine> cmd_line(CreateWebSocketServerCommandLine());
723 cmd_line->AppendSwitchWithValue("server", "stop"); 732 cmd_line->AppendSwitchWithValue("server", "stop");
724 cmd_line->AppendSwitchWithValue("pidfile", 733 cmd_line->AppendSwitchWithValue("pidfile",
725 websocket_pid_file_.ToWStringHack()); 734 websocket_pid_file_.ToWStringHack());
726 base::LaunchApp(*cmd_line.get(), true, false, NULL); 735 base::LaunchApp(*cmd_line.get(), true, false, NULL);
727 } 736 }
728 737
729 } // namespace ui_test_utils 738 } // namespace ui_test_utils
OLDNEW
« chrome/browser/sessions/session_restore_browsertest.cc ('K') | « chrome/test/ui_test_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698