| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "chrome/browser/browser.h" | 8 #include "chrome/browser/browser.h" |
| 9 #include "chrome/browser/tab_contents/navigation_controller.h" | 9 #include "chrome/browser/tab_contents/navigation_controller.h" |
| 10 #include "chrome/browser/tab_contents/web_contents.h" | 10 #include "chrome/browser/tab_contents/web_contents.h" |
| 11 #include "chrome/common/notification_registrar.h" | 11 #include "chrome/common/notification_registrar.h" |
| 12 #include "chrome/common/notification_service.h" | 12 #include "chrome/common/notification_service.h" |
| 13 #include "chrome/views/widget/accelerator_handler.h" | 13 #include "chrome/views/widget/accelerator_handler.h" |
| 14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 15 | 15 |
| 16 namespace ui_test_utils { | 16 namespace ui_test_utils { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // Used to block until a navigation completes. | 20 // Used to block until a navigation completes. |
| 21 class NavigationNotificationObserver : public NotificationObserver { | 21 class NavigationNotificationObserver : public NotificationObserver { |
| 22 public: | 22 public: |
| 23 explicit NavigationNotificationObserver(NavigationController* controller) | 23 NavigationNotificationObserver(NavigationController* controller, |
| 24 : navigation_started_(false) { | 24 int number_of_navigations) |
| 25 : navigation_started_(false), |
| 26 navigations_completed_(0), |
| 27 number_of_navigations_(number_of_navigations) { |
| 25 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, | 28 registrar_.Add(this, NotificationType::NAV_ENTRY_COMMITTED, |
| 26 Source<NavigationController>(controller)); | 29 Source<NavigationController>(controller)); |
| 27 registrar_.Add(this, NotificationType::LOAD_START, | 30 registrar_.Add(this, NotificationType::LOAD_START, |
| 28 Source<NavigationController>(controller)); | 31 Source<NavigationController>(controller)); |
| 29 registrar_.Add(this, NotificationType::LOAD_STOP, | 32 registrar_.Add(this, NotificationType::LOAD_STOP, |
| 30 Source<NavigationController>(controller)); | 33 Source<NavigationController>(controller)); |
| 31 RunMessageLoop(); | 34 RunMessageLoop(); |
| 32 } | 35 } |
| 33 | 36 |
| 34 virtual void Observe(NotificationType type, | 37 virtual void Observe(NotificationType type, |
| 35 const NotificationSource& source, | 38 const NotificationSource& source, |
| 36 const NotificationDetails& details) { | 39 const NotificationDetails& details) { |
| 37 if (type == NotificationType::NAV_ENTRY_COMMITTED || | 40 if (type == NotificationType::NAV_ENTRY_COMMITTED || |
| 38 type == NotificationType::LOAD_START) { | 41 type == NotificationType::LOAD_START) { |
| 39 navigation_started_ = true; | 42 navigation_started_ = true; |
| 40 } else if (type == NotificationType::LOAD_STOP) { | 43 } else if (type == NotificationType::LOAD_STOP) { |
| 41 if (navigation_started_) { | 44 if (navigation_started_ && |
| 45 ++navigations_completed_ == number_of_navigations_) { |
| 42 navigation_started_ = false; | 46 navigation_started_ = false; |
| 43 MessageLoopForUI::current()->Quit(); | 47 MessageLoopForUI::current()->Quit(); |
| 44 } | 48 } |
| 45 } | 49 } |
| 46 } | 50 } |
| 47 | 51 |
| 48 private: | 52 private: |
| 49 NotificationRegistrar registrar_; | 53 NotificationRegistrar registrar_; |
| 50 | 54 |
| 51 // If true the navigation has started. | 55 // If true the navigation has started. |
| 52 bool navigation_started_; | 56 bool navigation_started_; |
| 53 | 57 |
| 58 // The number of navigations that have been completed. |
| 59 int navigations_completed_; |
| 60 |
| 61 // The number of navigations to wait for. |
| 62 int number_of_navigations_; |
| 63 |
| 54 DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver); | 64 DISALLOW_COPY_AND_ASSIGN(NavigationNotificationObserver); |
| 55 }; | 65 }; |
| 56 | 66 |
| 57 } // namespace | 67 } // namespace |
| 58 | 68 |
| 59 void RunMessageLoop() { | 69 void RunMessageLoop() { |
| 60 MessageLoopForUI* loop = MessageLoopForUI::current(); | 70 MessageLoopForUI* loop = MessageLoopForUI::current(); |
| 61 bool did_allow_task_nesting = loop->NestableTasksAllowed(); | 71 bool did_allow_task_nesting = loop->NestableTasksAllowed(); |
| 62 loop->SetNestableTasksAllowed(true); | 72 loop->SetNestableTasksAllowed(true); |
| 63 views::AcceleratorHandler handler; | 73 views::AcceleratorHandler handler; |
| 64 loop->Run(&handler); | 74 loop->Run(&handler); |
| 65 loop->SetNestableTasksAllowed(did_allow_task_nesting); | 75 loop->SetNestableTasksAllowed(did_allow_task_nesting); |
| 66 } | 76 } |
| 67 | 77 |
| 68 void WaitForNavigation(NavigationController* controller) { | 78 void WaitForNavigation(NavigationController* controller) { |
| 69 NavigationNotificationObserver observer(controller); | 79 WaitForNavigations(controller, 1); |
| 80 } |
| 81 |
| 82 void WaitForNavigations(NavigationController* controller, |
| 83 int number_of_navigations) { |
| 84 NavigationNotificationObserver observer(controller, number_of_navigations); |
| 70 } | 85 } |
| 71 | 86 |
| 72 void NavigateToURL(Browser* browser, const GURL& url) { | 87 void NavigateToURL(Browser* browser, const GURL& url) { |
| 88 NavigateToURLBlockUntilNavigationsComplete(browser, url, 1); |
| 89 } |
| 90 |
| 91 void NavigateToURLBlockUntilNavigationsComplete(Browser* browser, |
| 92 const GURL& url, |
| 93 int number_of_navigations) { |
| 73 NavigationController* controller = | 94 NavigationController* controller = |
| 74 browser->GetSelectedTabContents()->controller(); | 95 browser->GetSelectedTabContents()->controller(); |
| 75 browser->OpenURLFromTab(browser->GetSelectedTabContents(), url, GURL(), | 96 browser->OpenURLFromTab(browser->GetSelectedTabContents(), url, GURL(), |
| 76 CURRENT_TAB, PageTransition::TYPED); | 97 CURRENT_TAB, PageTransition::TYPED); |
| 77 WaitForNavigation(controller); | 98 WaitForNavigations(controller, number_of_navigations); |
| 78 } | 99 } |
| 79 | 100 |
| 80 } // namespace ui_test_utils | 101 } // namespace ui_test_utils |
| OLD | NEW |