• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "content/public/test/content_browser_test_utils.h"
6 
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"
22 
23 namespace content {
24 
GetTestFilePath(const char * dir,const char * file)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)));
30 }
31 
GetTestUrl(const char * dir,const char * file)32 GURL GetTestUrl(const char* dir, const char* file) {
33   return net::FilePathToFileURL(GetTestFilePath(dir, file));
34 }
35 
NavigateToURLBlockUntilNavigationsComplete(Shell * window,const GURL & url,int number_of_navigations)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);
42 
43   window->LoadURL(url);
44   same_tab_observer.Wait();
45 }
46 
ReloadBlockUntilNavigationsComplete(Shell * window,int number_of_navigations)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);
52 
53   window->Reload();
54   same_tab_observer.Wait();
55 }
56 
LoadDataWithBaseURL(Shell * window,const GURL & url,const std::string data,const GURL & base_url)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);
61 
62   window->LoadDataWithBaseURL(url, data, base_url);
63   same_tab_observer.Wait();
64 }
65 
NavigateToURL(Shell * window,const GURL & url)66 void NavigateToURL(Shell* window, const GURL& url) {
67   NavigateToURLBlockUntilNavigationsComplete(window, url, 1);
68 }
69 
WaitForAppModalDialog(Shell * window)70 void WaitForAppModalDialog(Shell* window) {
71   ShellJavaScriptDialogManager* dialog_manager=
72       static_cast<ShellJavaScriptDialogManager*>(
73           window->GetJavaScriptDialogManager());
74 
75   scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner();
76   dialog_manager->set_dialog_request_callback(runner->QuitClosure());
77   runner->Run();
78 }
79 
ShellAddedObserver()80 ShellAddedObserver::ShellAddedObserver()
81     : shell_(NULL) {
82   Shell::SetShellCreatedCallback(
83       base::Bind(&ShellAddedObserver::ShellCreated, base::Unretained(this)));
84 }
85 
~ShellAddedObserver()86 ShellAddedObserver::~ShellAddedObserver() {
87 }
88 
GetShell()89 Shell* ShellAddedObserver::GetShell() {
90   if (shell_)
91     return shell_;
92 
93   runner_ = new MessageLoopRunner();
94   runner_->Run();
95   return shell_;
96 }
97 
ShellCreated(Shell * shell)98 void ShellAddedObserver::ShellCreated(Shell* shell) {
99   DCHECK(!shell_);
100   shell_ = shell;
101   if (runner_.get())
102     runner_->QuitClosure().Run();
103 }
104 
105 class RenderViewCreatedObserver : public WebContentsObserver {
106  public:
RenderViewCreatedObserver(WebContents * web_contents)107   explicit RenderViewCreatedObserver(WebContents* web_contents)
108       : WebContentsObserver(web_contents),
109         render_view_created_called_(false) {
110   }
111 
112   // WebContentsObserver:
RenderViewCreated(RenderViewHost * rvh)113   virtual void RenderViewCreated(RenderViewHost* rvh) OVERRIDE {
114     render_view_created_called_ = true;
115   }
116 
117   bool render_view_created_called_;
118 };
119 
WebContentsAddedObserver()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_);
127 }
128 
~WebContentsAddedObserver()129 WebContentsAddedObserver::~WebContentsAddedObserver() {
130   WebContentsImpl::RemoveCreatedCallback(web_contents_created_callback_);
131 }
132 
WebContentsCreated(WebContents * web_contents)133 void WebContentsAddedObserver::WebContentsCreated(WebContents* web_contents) {
134   DCHECK(!web_contents_);
135   web_contents_ = web_contents;
136   child_observer_.reset(new RenderViewCreatedObserver(web_contents));
137 
138   if (runner_.get())
139     runner_->QuitClosure().Run();
140 }
141 
GetWebContents()142 WebContents* WebContentsAddedObserver::GetWebContents() {
143   if (web_contents_)
144     return web_contents_;
145 
146   runner_ = new MessageLoopRunner();
147   runner_->Run();
148   return web_contents_;
149 }
150 
RenderViewCreatedCalled()151 bool WebContentsAddedObserver::RenderViewCreatedCalled() {
152   if (child_observer_)
153     return child_observer_->render_view_created_called_;
154 
155   return false;
156 }
157 
158 }  // namespace content
159