| OLD | NEW |
| 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/test_tab_strip_model_observer.h" | 5 #include "chrome/test/test_navigation_observer.h" |
| 6 | 6 |
| 7 #include "chrome/browser/tabs/tab_strip_model.h" | |
| 8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 7 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 9 #include "chrome/test/ui_test_utils.h" | 8 #include "chrome/test/ui_test_utils.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 10 |
| 12 TestTabStripModelObserver::LoadStartObserver::LoadStartObserver() { | 11 TestNavigationObserver::LoadStartObserver::LoadStartObserver() { |
| 13 } | 12 } |
| 14 | 13 |
| 15 TestTabStripModelObserver::LoadStartObserver::~LoadStartObserver() { | 14 TestNavigationObserver::LoadStartObserver::~LoadStartObserver() { |
| 16 } | 15 } |
| 17 | 16 |
| 18 TestTabStripModelObserver::TestTabStripModelObserver( | 17 TestNavigationObserver::TestNavigationObserver( |
| 19 TabStripModel* tab_strip_model, | 18 TabContentsWrapper* contents, |
| 20 TestTabStripModelObserver::LoadStartObserver* load_start_observer) | 19 TestNavigationObserver::LoadStartObserver* load_start_observer) |
| 20 : navigation_started_(false), |
| 21 navigation_entry_committed_(false), |
| 22 navigations_completed_(0), |
| 23 number_of_navigations_(1), |
| 24 load_start_observer_(load_start_observer), |
| 25 done_(false), |
| 26 running_(false) { |
| 27 RegisterAsObserver(contents); |
| 28 } |
| 29 |
| 30 TestNavigationObserver::TestNavigationObserver( |
| 31 TestNavigationObserver::LoadStartObserver* load_start_observer) |
| 21 : navigation_started_(false), | 32 : navigation_started_(false), |
| 22 navigations_completed_(0), | 33 navigations_completed_(0), |
| 23 number_of_navigations_(1), | 34 number_of_navigations_(1), |
| 24 tab_strip_model_(tab_strip_model), | |
| 25 load_start_observer_(load_start_observer), | 35 load_start_observer_(load_start_observer), |
| 26 done_(false), | 36 done_(false), |
| 27 running_(false) { | 37 running_(false) { |
| 28 tab_strip_model_->AddObserver(this); | |
| 29 } | 38 } |
| 30 | 39 |
| 31 TestTabStripModelObserver::~TestTabStripModelObserver() { | 40 TestNavigationObserver::~TestNavigationObserver() { |
| 32 tab_strip_model_->RemoveObserver(this); | |
| 33 } | 41 } |
| 34 | 42 |
| 35 void TestTabStripModelObserver::WaitForObservation() { | 43 void TestNavigationObserver::RegisterAsObserver(TabContentsWrapper* contents) { |
| 36 if (!done_) { | |
| 37 EXPECT_FALSE(running_); | |
| 38 running_ = true; | |
| 39 ui_test_utils::RunMessageLoop(); | |
| 40 } | |
| 41 } | |
| 42 | |
| 43 void TestTabStripModelObserver::TabInsertedAt( | |
| 44 TabContentsWrapper* contents, int index, bool foreground) { | |
| 45 NavigationController* controller = &contents->controller(); | 44 NavigationController* controller = &contents->controller(); |
| 46 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 45 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 47 Source<NavigationController>(controller)); | 46 Source<NavigationController>(controller)); |
| 48 registrar_.Add(this, content::NOTIFICATION_LOAD_START, | 47 registrar_.Add(this, content::NOTIFICATION_LOAD_START, |
| 49 Source<NavigationController>(controller)); | 48 Source<NavigationController>(controller)); |
| 50 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, | 49 registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, |
| 51 Source<NavigationController>(controller)); | 50 Source<NavigationController>(controller)); |
| 52 } | 51 } |
| 53 | 52 |
| 54 void TestTabStripModelObserver::Observe( | 53 void TestNavigationObserver::WaitForObservation() { |
| 54 if (!done_) { |
| 55 EXPECT_FALSE(running_); |
| 56 running_ = true; |
| 57 ui_test_utils::RunMessageLoop(); |
| 58 } |
| 59 } |
| 60 |
| 61 void TestNavigationObserver::Observe( |
| 55 int type, const NotificationSource& source, | 62 int type, const NotificationSource& source, |
| 56 const NotificationDetails& details) { | 63 const NotificationDetails& details) { |
| 57 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED || | 64 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { |
| 58 type == content::NOTIFICATION_LOAD_START) { | 65 if (!navigation_entry_committed_ && load_start_observer_) |
| 59 if (!navigation_started_) { | |
| 60 load_start_observer_->OnLoadStart(); | 66 load_start_observer_->OnLoadStart(); |
| 61 navigation_started_ = true; | 67 navigation_started_ = true; |
| 62 } | 68 navigation_entry_committed_ = true; |
| 69 } else if (type == content::NOTIFICATION_LOAD_START) { |
| 70 navigation_started_ = true; |
| 63 } else if (type == content::NOTIFICATION_LOAD_STOP) { | 71 } else if (type == content::NOTIFICATION_LOAD_STOP) { |
| 64 if (navigation_started_ && | 72 if (navigation_started_ && |
| 65 ++navigations_completed_ == number_of_navigations_) { | 73 ++navigations_completed_ == number_of_navigations_) { |
| 66 navigation_started_ = false; | 74 navigation_started_ = false; |
| 67 done_ = true; | 75 done_ = true; |
| 68 if (running_) | 76 if (running_) |
| 69 MessageLoopForUI::current()->Quit(); | 77 MessageLoopForUI::current()->Quit(); |
| 70 } | 78 } |
| 71 } | 79 } |
| 72 } | 80 } |
| OLD | NEW |