| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/public/test/test_navigation_observer.h" | 5 #include "content/public/test/test_navigation_observer.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "content/browser/web_contents/web_contents_impl.h" | 11 #include "content/browser/web_contents/web_contents_impl.h" |
| 12 #include "content/public/browser/web_contents_observer.h" | 12 #include "content/public/browser/web_contents_observer.h" |
| 13 #include "content/public/test/browser_test_utils.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 | 17 |
| 17 class TestNavigationObserver::TestWebContentsObserver | 18 class TestNavigationObserver::TestWebContentsObserver |
| 18 : public WebContentsObserver { | 19 : public WebContentsObserver { |
| 19 public: | 20 public: |
| 20 TestWebContentsObserver(TestNavigationObserver* parent, | 21 TestWebContentsObserver(TestNavigationObserver* parent, |
| 21 WebContents* web_contents) | 22 WebContents* web_contents) |
| 22 : WebContentsObserver(web_contents), | 23 : WebContentsObserver(web_contents), |
| (...skipping 24 matching lines...) Expand all Loading... |
| 47 } | 48 } |
| 48 | 49 |
| 49 TestNavigationObserver* parent_; | 50 TestNavigationObserver* parent_; |
| 50 | 51 |
| 51 DISALLOW_COPY_AND_ASSIGN(TestWebContentsObserver); | 52 DISALLOW_COPY_AND_ASSIGN(TestWebContentsObserver); |
| 52 }; | 53 }; |
| 53 | 54 |
| 54 TestNavigationObserver::TestNavigationObserver( | 55 TestNavigationObserver::TestNavigationObserver( |
| 55 WebContents* web_contents, | 56 WebContents* web_contents, |
| 56 int number_of_navigations) | 57 int number_of_navigations) |
| 57 : navigation_started_(false), | 58 : web_contents_(web_contents), |
| 59 navigation_started_(false), |
| 58 navigations_completed_(0), | 60 navigations_completed_(0), |
| 59 number_of_navigations_(number_of_navigations), | 61 number_of_navigations_(number_of_navigations), |
| 60 message_loop_runner_(new MessageLoopRunner), | 62 message_loop_runner_(new MessageLoopRunner), |
| 61 web_contents_created_callback_( | 63 web_contents_created_callback_( |
| 62 base::Bind( | 64 base::Bind( |
| 63 &TestNavigationObserver::OnWebContentsCreated, | 65 &TestNavigationObserver::OnWebContentsCreated, |
| 64 base::Unretained(this))) { | 66 base::Unretained(this))) { |
| 65 if (web_contents) | 67 if (web_contents) |
| 66 RegisterAsObserver(web_contents); | 68 RegisterAsObserver(web_contents); |
| 67 } | 69 } |
| 68 | 70 |
| 69 TestNavigationObserver::TestNavigationObserver( | 71 TestNavigationObserver::TestNavigationObserver( |
| 70 WebContents* web_contents) | 72 WebContents* web_contents) |
| 71 : navigation_started_(false), | 73 : web_contents_(web_contents), |
| 74 navigation_started_(false), |
| 72 navigations_completed_(0), | 75 navigations_completed_(0), |
| 73 number_of_navigations_(1), | 76 number_of_navigations_(1), |
| 74 message_loop_runner_(new MessageLoopRunner), | 77 message_loop_runner_(new MessageLoopRunner), |
| 75 web_contents_created_callback_( | 78 web_contents_created_callback_( |
| 76 base::Bind( | 79 base::Bind( |
| 77 &TestNavigationObserver::OnWebContentsCreated, | 80 &TestNavigationObserver::OnWebContentsCreated, |
| 78 base::Unretained(this))) { | 81 base::Unretained(this))) { |
| 79 if (web_contents) | 82 if (web_contents) |
| 80 RegisterAsObserver(web_contents); | 83 RegisterAsObserver(web_contents); |
| 81 } | 84 } |
| 82 | 85 |
| 83 TestNavigationObserver::~TestNavigationObserver() { | 86 TestNavigationObserver::~TestNavigationObserver() { |
| 84 StopWatchingNewWebContents(); | 87 StopWatchingNewWebContents(); |
| 85 | 88 |
| 86 STLDeleteContainerPointers(web_contents_observers_.begin(), | 89 STLDeleteContainerPointers(web_contents_observers_.begin(), |
| 87 web_contents_observers_.end()); | 90 web_contents_observers_.end()); |
| 88 } | 91 } |
| 89 | 92 |
| 90 void TestNavigationObserver::Wait() { | 93 void TestNavigationObserver::Wait() { |
| 91 message_loop_runner_->Run(); | 94 message_loop_runner_->Run(); |
| 95 WaitForResizeComplete(web_contents_); |
| 92 } | 96 } |
| 93 | 97 |
| 94 void TestNavigationObserver::StartWatchingNewWebContents() { | 98 void TestNavigationObserver::StartWatchingNewWebContents() { |
| 95 WebContentsImpl::AddCreatedCallback(web_contents_created_callback_); | 99 WebContentsImpl::AddCreatedCallback(web_contents_created_callback_); |
| 96 } | 100 } |
| 97 | 101 |
| 98 void TestNavigationObserver::StopWatchingNewWebContents() { | 102 void TestNavigationObserver::StopWatchingNewWebContents() { |
| 99 WebContentsImpl::RemoveCreatedCallback(web_contents_created_callback_); | 103 WebContentsImpl::RemoveCreatedCallback(web_contents_created_callback_); |
| 100 } | 104 } |
| 101 | 105 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 return; | 142 return; |
| 139 | 143 |
| 140 ++navigations_completed_; | 144 ++navigations_completed_; |
| 141 if (navigations_completed_ == number_of_navigations_) { | 145 if (navigations_completed_ == number_of_navigations_) { |
| 142 navigation_started_ = false; | 146 navigation_started_ = false; |
| 143 message_loop_runner_->Quit(); | 147 message_loop_runner_->Quit(); |
| 144 } | 148 } |
| 145 } | 149 } |
| 146 | 150 |
| 147 } // namespace content | 151 } // namespace content |
| OLD | NEW |