Standardize usage of virtual/override/final in content/
[chromium-blink-merge.git] / content / public / test / content_browser_test_utils.cc
blobba391947fc958ae28085c284815c155fdc8100f4
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
3 // found in the LICENSE file.
5 #include "content/public/test/content_browser_test_utils.h"
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/path_service.h"
10 #include "base/run_loop.h"
11 #include "content/browser/web_contents/web_contents_impl.h"
12 #include "content/public/browser/navigation_controller.h"
13 #include "content/public/browser/notification_source.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/common/content_paths.h"
16 #include "content/public/test/browser_test_utils.h"
17 #include "content/public/test/test_navigation_observer.h"
18 #include "content/public/test/test_utils.h"
19 #include "content/shell/browser/shell.h"
20 #include "content/shell/browser/shell_javascript_dialog_manager.h"
21 #include "net/base/filename_util.h"
23 namespace content {
25 base::FilePath GetTestFilePath(const char* dir, const char* file) {
26 base::FilePath path;
27 PathService::Get(DIR_TEST_DATA, &path);
28 return path.Append(base::FilePath().AppendASCII(dir).Append(
29 base::FilePath().AppendASCII(file)));
32 GURL GetTestUrl(const char* dir, const char* file) {
33 return net::FilePathToFileURL(GetTestFilePath(dir, file));
36 void NavigateToURLBlockUntilNavigationsComplete(Shell* window,
37 const GURL& url,
38 int number_of_navigations) {
39 WaitForLoadStop(window->web_contents());
40 TestNavigationObserver same_tab_observer(window->web_contents(),
41 number_of_navigations);
43 window->LoadURL(url);
44 same_tab_observer.Wait();
47 void ReloadBlockUntilNavigationsComplete(Shell* window,
48 int number_of_navigations) {
49 WaitForLoadStop(window->web_contents());
50 TestNavigationObserver same_tab_observer(window->web_contents(),
51 number_of_navigations);
53 window->Reload();
54 same_tab_observer.Wait();
57 void LoadDataWithBaseURL(Shell* window, const GURL& url,
58 const std::string data, const GURL& base_url) {
59 WaitForLoadStop(window->web_contents());
60 TestNavigationObserver same_tab_observer(window->web_contents(), 1);
62 window->LoadDataWithBaseURL(url, data, base_url);
63 same_tab_observer.Wait();
66 void NavigateToURL(Shell* window, const GURL& url) {
67 NavigateToURLBlockUntilNavigationsComplete(window, url, 1);
70 void WaitForAppModalDialog(Shell* window) {
71 ShellJavaScriptDialogManager* dialog_manager=
72 static_cast<ShellJavaScriptDialogManager*>(
73 window->GetJavaScriptDialogManager());
75 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner();
76 dialog_manager->set_dialog_request_callback(runner->QuitClosure());
77 runner->Run();
80 ShellAddedObserver::ShellAddedObserver()
81 : shell_(NULL) {
82 Shell::SetShellCreatedCallback(
83 base::Bind(&ShellAddedObserver::ShellCreated, base::Unretained(this)));
86 ShellAddedObserver::~ShellAddedObserver() {
89 Shell* ShellAddedObserver::GetShell() {
90 if (shell_)
91 return shell_;
93 runner_ = new MessageLoopRunner();
94 runner_->Run();
95 return shell_;
98 void ShellAddedObserver::ShellCreated(Shell* shell) {
99 DCHECK(!shell_);
100 shell_ = shell;
101 if (runner_.get())
102 runner_->QuitClosure().Run();
105 class RenderViewCreatedObserver : public WebContentsObserver {
106 public:
107 explicit RenderViewCreatedObserver(WebContents* web_contents)
108 : WebContentsObserver(web_contents),
109 render_view_created_called_(false) {
112 // WebContentsObserver:
113 void RenderViewCreated(RenderViewHost* rvh) override {
114 render_view_created_called_ = true;
117 bool render_view_created_called_;
120 WebContentsAddedObserver::WebContentsAddedObserver()
121 : web_contents_created_callback_(
122 base::Bind(
123 &WebContentsAddedObserver::WebContentsCreated,
124 base::Unretained(this))),
125 web_contents_(NULL) {
126 WebContentsImpl::AddCreatedCallback(web_contents_created_callback_);
129 WebContentsAddedObserver::~WebContentsAddedObserver() {
130 WebContentsImpl::RemoveCreatedCallback(web_contents_created_callback_);
133 void WebContentsAddedObserver::WebContentsCreated(WebContents* web_contents) {
134 DCHECK(!web_contents_);
135 web_contents_ = web_contents;
136 child_observer_.reset(new RenderViewCreatedObserver(web_contents));
138 if (runner_.get())
139 runner_->QuitClosure().Run();
142 WebContents* WebContentsAddedObserver::GetWebContents() {
143 if (web_contents_)
144 return web_contents_;
146 runner_ = new MessageLoopRunner();
147 runner_->Run();
148 return web_contents_;
151 bool WebContentsAddedObserver::RenderViewCreatedCalled() {
152 if (child_observer_)
153 return child_observer_->render_view_created_called_;
155 return false;
158 } // namespace content