Chromium Code Reviews| 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 // If this is the very first navigation, the RenderView for WebContents | |
| 96 // will be created as a result. The creation involves sending a resize | |
| 97 // message to the renderer process. Here we wait for the resize ack to be | |
| 98 // received, since tests may not expect to end up in a "waiting for resize" | |
| 99 // state after the first navigation. | |
| 100 // E.g. currently WindowEventDispatcher has code to hold touch and mouse | |
| 101 // move events until resize is complete (crbug.com/384342) | |
| 102 WaitForResizeComplete(web_contents_); | |
|
mfomitchev
2014/06/13 15:56:21
I wonder if we should make it so that this is done
| |
| 92 } | 103 } |
| 93 | 104 |
| 94 void TestNavigationObserver::StartWatchingNewWebContents() { | 105 void TestNavigationObserver::StartWatchingNewWebContents() { |
| 95 WebContentsImpl::AddCreatedCallback(web_contents_created_callback_); | 106 WebContentsImpl::AddCreatedCallback(web_contents_created_callback_); |
| 96 } | 107 } |
| 97 | 108 |
| 98 void TestNavigationObserver::StopWatchingNewWebContents() { | 109 void TestNavigationObserver::StopWatchingNewWebContents() { |
| 99 WebContentsImpl::RemoveCreatedCallback(web_contents_created_callback_); | 110 WebContentsImpl::RemoveCreatedCallback(web_contents_created_callback_); |
| 100 } | 111 } |
| 101 | 112 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 return; | 149 return; |
| 139 | 150 |
| 140 ++navigations_completed_; | 151 ++navigations_completed_; |
| 141 if (navigations_completed_ == number_of_navigations_) { | 152 if (navigations_completed_ == number_of_navigations_) { |
| 142 navigation_started_ = false; | 153 navigation_started_ = false; |
| 143 message_loop_runner_->Quit(); | 154 message_loop_runner_->Quit(); |
| 144 } | 155 } |
| 145 } | 156 } |
| 146 | 157 |
| 147 } // namespace content | 158 } // namespace content |
| OLD | NEW |