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"
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"
25 base::FilePath
GetTestFilePath(const char* dir
, const char* file
) {
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
,
38 int number_of_navigations
) {
39 WaitForLoadStop(window
->web_contents());
40 TestNavigationObserver
same_tab_observer(window
->web_contents(),
41 number_of_navigations
);
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
);
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());
80 ShellAddedObserver::ShellAddedObserver()
82 Shell::SetShellCreatedCallback(
83 base::Bind(&ShellAddedObserver::ShellCreated
, base::Unretained(this)));
86 ShellAddedObserver::~ShellAddedObserver() {
89 Shell
* ShellAddedObserver::GetShell() {
93 runner_
= new MessageLoopRunner();
98 void ShellAddedObserver::ShellCreated(Shell
* shell
) {
102 runner_
->QuitClosure().Run();
105 class RenderViewCreatedObserver
: public WebContentsObserver
{
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_(
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
));
139 runner_
->QuitClosure().Run();
142 WebContents
* WebContentsAddedObserver::GetWebContents() {
144 return web_contents_
;
146 runner_
= new MessageLoopRunner();
148 return web_contents_
;
151 bool WebContentsAddedObserver::RenderViewCreatedCalled() {
153 return child_observer_
->render_view_created_called_
;
158 } // namespace content