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

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

Issue 6662001: ui_test_utils::NavigateToURLBlockUntilNavigationsComplete can be raced (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: track if the event loop is needed Created 9 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 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 #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/callback.h" 9 #include "base/callback.h"
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 54
55 namespace { 55 namespace {
56 56
57 // Used to block until a navigation completes. 57 // Used to block until a navigation completes.
58 class NavigationNotificationObserver : public NotificationObserver { 58 class NavigationNotificationObserver : public NotificationObserver {
59 public: 59 public:
60 NavigationNotificationObserver(NavigationController* controller, 60 NavigationNotificationObserver(NavigationController* controller,
61 int number_of_navigations) 61 int number_of_navigations)
62 : navigation_started_(false), 62 : navigation_started_(false),
63 navigations_completed_(0), 63 navigations_completed_(0),
64 number_of_navigations_(number_of_navigations) { 64 number_of_navigations_(number_of_navigations),
65 running_(false),
66 done_(false) {
65 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, 67 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED,
66 Source<NavigationController>(controller)); 68 Source<NavigationController>(controller));
67 registrar_.Add(this, NotificationType::LOAD_START, 69 registrar_.Add(this, NotificationType::LOAD_START,
68 Source<NavigationController>(controller)); 70 Source<NavigationController>(controller));
69 registrar_.Add(this, NotificationType::LOAD_STOP, 71 registrar_.Add(this, NotificationType::LOAD_STOP,
70 Source<NavigationController>(controller)); 72 Source<NavigationController>(controller));
71 RunMessageLoop(); 73 }
74
75 void Run() {
76 if (!done_) {
77 running_ = true;
78 RunMessageLoop();
79 }
72 } 80 }
73 81
74 virtual void Observe(NotificationType type, 82 virtual void Observe(NotificationType type,
75 const NotificationSource& source, 83 const NotificationSource& source,
76 const NotificationDetails& details) { 84 const NotificationDetails& details) {
77 if (type == NotificationType::NAV_ENTRY_COMMITTED || 85 if (type == NotificationType::NAV_ENTRY_COMMITTED ||
78 type == NotificationType::LOAD_START) { 86 type == NotificationType::LOAD_START) {
79 navigation_started_ = true; 87 navigation_started_ = true;
80 } else if (type == NotificationType::LOAD_STOP) { 88 } else if (type == NotificationType::LOAD_STOP) {
81 if (navigation_started_ && 89 if (navigation_started_ &&
82 ++navigations_completed_ == number_of_navigations_) { 90 ++navigations_completed_ == number_of_navigations_) {
83 navigation_started_ = false; 91 navigation_started_ = false;
84 MessageLoopForUI::current()->Quit(); 92 done_ = true;
93 if (running_)
94 MessageLoopForUI::current()->Quit();
85 } 95 }
86 } 96 }
87 } 97 }
88 98
89 private: 99 private:
90 NotificationRegistrar registrar_; 100 NotificationRegistrar registrar_;
91 101
92 // If true the navigation has started. 102 // If true the navigation has started.
93 bool navigation_started_; 103 bool navigation_started_;
94 104
95 // The number of navigations that have been completed. 105 // The number of navigations that have been completed.
96 int navigations_completed_; 106 int navigations_completed_;
97 107
98 // The number of navigations to wait for. 108 // The number of navigations to wait for.
99 int number_of_navigations_; 109 int number_of_navigations_;
100 110
111 // Calls to Observe() can happen early, before the user calls Run(), or
112 // after. When we've seen all the navigations we're looking for, we set
113 // done_ to true; then when Run() is called we'll never need to run the
114 // event loop. Also, we don't need to quit the event loop when we're
115 // done if we never had to start an event loop.
116 bool running_;
117 bool done_;
101 DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver); 118 DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver);
102 }; 119 };
103 120
104 class DOMOperationObserver : public NotificationObserver { 121 class DOMOperationObserver : public NotificationObserver {
105 public: 122 public:
106 explicit DOMOperationObserver(RenderViewHost* render_view_host) 123 explicit DOMOperationObserver(RenderViewHost* render_view_host)
107 : did_respond_(false) { 124 : did_respond_(false) {
108 registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE, 125 registrar_.Add(this, NotificationType::DOM_OPERATION_RESPONSE,
109 Source<RenderViewHost>(render_view_host)); 126 Source<RenderViewHost>(render_view_host));
110 ui_test_utils::RunMessageLoop(); 127 ui_test_utils::RunMessageLoop();
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 return true; 422 return true;
406 } 423 }
407 424
408 void WaitForNavigation(NavigationController* controller) { 425 void WaitForNavigation(NavigationController* controller) {
409 WaitForNavigations(controller, 1); 426 WaitForNavigations(controller, 1);
410 } 427 }
411 428
412 void WaitForNavigations(NavigationController* controller, 429 void WaitForNavigations(NavigationController* controller,
413 int number_of_navigations) { 430 int number_of_navigations) {
414 NavigationNotificationObserver observer(controller, number_of_navigations); 431 NavigationNotificationObserver observer(controller, number_of_navigations);
432 observer.Run();
415 } 433 }
416 434
417 void WaitForNewTab(Browser* browser) { 435 void WaitForNewTab(Browser* browser) {
418 TestNotificationObserver observer; 436 TestNotificationObserver observer;
419 RegisterAndWait(&observer, NotificationType::TAB_ADDED, 437 RegisterAndWait(&observer, NotificationType::TAB_ADDED,
420 Source<Browser>(browser)); 438 Source<Browser>(browser));
421 } 439 }
422 440
423 void WaitForBrowserActionUpdated(ExtensionAction* browser_action) { 441 void WaitForBrowserActionUpdated(ExtensionAction* browser_action) {
424 TestNotificationObserver observer; 442 TestNotificationObserver observer;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 // Navigates the specified tab (via |disposition|) of |browser| to |url|, 483 // Navigates the specified tab (via |disposition|) of |browser| to |url|,
466 // blocking until the |number_of_navigations| specified complete. 484 // blocking until the |number_of_navigations| specified complete.
467 // |disposition| indicates what tab the download occurs in, and 485 // |disposition| indicates what tab the download occurs in, and
468 // |browser_test_flags| controls what to wait for before continuing. 486 // |browser_test_flags| controls what to wait for before continuing.
469 static void NavigateToURLWithDispositionBlockUntilNavigationsComplete( 487 static void NavigateToURLWithDispositionBlockUntilNavigationsComplete(
470 Browser* browser, 488 Browser* browser,
471 const GURL& url, 489 const GURL& url,
472 int number_of_navigations, 490 int number_of_navigations,
473 WindowOpenDisposition disposition, 491 WindowOpenDisposition disposition,
474 int browser_test_flags) { 492 int browser_test_flags) {
493 NavigationNotificationObserver
494 same_tab_observer(&browser->GetSelectedTabContents()->controller(),
495 number_of_navigations);
496
475 std::set<Browser*> initial_browsers; 497 std::set<Browser*> initial_browsers;
476 for (std::vector<Browser*>::const_iterator iter = BrowserList::begin(); 498 for (std::vector<Browser*>::const_iterator iter = BrowserList::begin();
477 iter != BrowserList::end(); 499 iter != BrowserList::end();
478 ++iter) { 500 ++iter) {
479 initial_browsers.insert(*iter); 501 initial_browsers.insert(*iter);
480 } 502 }
481 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED); 503 browser->OpenURL(url, GURL(), disposition, PageTransition::TYPED);
482 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_BROWSER) 504 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_BROWSER)
483 browser = WaitForBrowserNotInSet(initial_browsers); 505 browser = WaitForBrowserNotInSet(initial_browsers);
484 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_TAB) 506 if (browser_test_flags & BROWSER_TEST_WAIT_FOR_TAB)
485 WaitForNotification(NotificationType::TAB_ADDED); 507 WaitForNotification(NotificationType::TAB_ADDED);
486 if (!(browser_test_flags & BROWSER_TEST_WAIT_FOR_NAVIGATION)) { 508 if (!(browser_test_flags & BROWSER_TEST_WAIT_FOR_NAVIGATION)) {
487 // Some other flag caused the wait prior to this. 509 // Some other flag caused the wait prior to this.
488 return; 510 return;
489 } 511 }
490 TabContents* tab_contents = NULL; 512 TabContents* tab_contents = NULL;
491 if (disposition == NEW_BACKGROUND_TAB) { 513 if (disposition == NEW_BACKGROUND_TAB) {
492 // We've opened up a new tab, but not selected it. 514 // We've opened up a new tab, but not selected it.
493 tab_contents = browser->GetTabContentsAt(browser->selected_index() + 1); 515 tab_contents = browser->GetTabContentsAt(browser->selected_index() + 1);
494 EXPECT_TRUE(tab_contents != NULL) 516 EXPECT_TRUE(tab_contents != NULL)
495 << " Unable to wait for navigation to \"" << url.spec() 517 << " Unable to wait for navigation to \"" << url.spec()
496 << "\" because the new tab is not available yet"; 518 << "\" because the new tab is not available yet";
497 return; 519 return;
498 } else if ((disposition == CURRENT_TAB) || 520 } else if ((disposition == CURRENT_TAB) ||
499 (disposition == NEW_FOREGROUND_TAB) || 521 (disposition == NEW_FOREGROUND_TAB) ||
500 (disposition == SINGLETON_TAB)) { 522 (disposition == SINGLETON_TAB)) {
501 // The currently selected tab is the right one. 523 // The currently selected tab is the right one.
502 tab_contents = browser->GetSelectedTabContents(); 524 tab_contents = browser->GetSelectedTabContents();
503 } 525 }
504 if (tab_contents) { 526 if (disposition == CURRENT_TAB) {
527 same_tab_observer.Run();
528 return;
529 } else if (tab_contents) {
505 NavigationController* controller = &tab_contents->controller(); 530 NavigationController* controller = &tab_contents->controller();
506 WaitForNavigations(controller, number_of_navigations); 531 WaitForNavigations(controller, number_of_navigations);
507 return; 532 return;
508 } 533 }
509 EXPECT_TRUE(NULL != tab_contents) << " Unable to wait for navigation to \"" 534 EXPECT_TRUE(NULL != tab_contents) << " Unable to wait for navigation to \""
510 << url.spec() << "\"" 535 << url.spec() << "\""
511 << " because we can't get the tab contents"; 536 << " because we can't get the tab contents";
512 } 537 }
513 538
514 void NavigateToURLWithDisposition(Browser* browser, 539 void NavigateToURLWithDisposition(Browser* browser,
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
1045 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap); 1070 return taker.TakeRenderWidgetSnapshot(rwh, page_size, page_size, bitmap);
1046 } 1071 }
1047 1072
1048 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) { 1073 bool TakeEntirePageSnapshot(RenderViewHost* rvh, SkBitmap* bitmap) {
1049 DCHECK(bitmap); 1074 DCHECK(bitmap);
1050 SnapshotTaker taker; 1075 SnapshotTaker taker;
1051 return taker.TakeEntirePageSnapshot(rvh, bitmap); 1076 return taker.TakeEntirePageSnapshot(rvh, bitmap);
1052 } 1077 }
1053 1078
1054 } // namespace ui_test_utils 1079 } // namespace ui_test_utils
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698