• 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 
LoadDataWithBaseURL(Shell * window,const GURL & url,const std::string data,const GURL & base_url)47 void LoadDataWithBaseURL(Shell* window, const GURL& url,
48     const std::string data, const GURL& base_url) {
49   WaitForLoadStop(window->web_contents());
50   TestNavigationObserver same_tab_observer(window->web_contents(), 1);
51 
52   window->LoadDataWithBaseURL(url, data, base_url);
53   same_tab_observer.Wait();
54 }
55 
NavigateToURL(Shell * window,const GURL & url)56 void NavigateToURL(Shell* window, const GURL& url) {
57   NavigateToURLBlockUntilNavigationsComplete(window, url, 1);
58 }
59 
WaitForAppModalDialog(Shell * window)60 void WaitForAppModalDialog(Shell* window) {
61   ShellJavaScriptDialogManager* dialog_manager=
62       static_cast<ShellJavaScriptDialogManager*>(
63           window->GetJavaScriptDialogManager());
64 
65   scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner();
66   dialog_manager->set_dialog_request_callback(runner->QuitClosure());
67   runner->Run();
68 }
69 
ShellAddedObserver()70 ShellAddedObserver::ShellAddedObserver()
71     : shell_(NULL) {
72   Shell::SetShellCreatedCallback(
73       base::Bind(&ShellAddedObserver::ShellCreated, base::Unretained(this)));
74 }
75 
~ShellAddedObserver()76 ShellAddedObserver::~ShellAddedObserver() {
77 }
78 
GetShell()79 Shell* ShellAddedObserver::GetShell() {
80   if (shell_)
81     return shell_;
82 
83   runner_ = new MessageLoopRunner();
84   runner_->Run();
85   return shell_;
86 }
87 
ShellCreated(Shell * shell)88 void ShellAddedObserver::ShellCreated(Shell* shell) {
89   DCHECK(!shell_);
90   shell_ = shell;
91   if (runner_.get())
92     runner_->QuitClosure().Run();
93 }
94 
95 class RenderViewCreatedObserver : public WebContentsObserver {
96  public:
RenderViewCreatedObserver(WebContents * web_contents)97   RenderViewCreatedObserver(WebContents* web_contents)
98       : WebContentsObserver(web_contents),
99         render_view_created_called_(false) {
100   }
101 
102   // WebContentsObserver:
RenderViewCreated(RenderViewHost * rvh)103   virtual void RenderViewCreated(RenderViewHost* rvh) OVERRIDE {
104     render_view_created_called_ = true;
105   }
106 
107   bool render_view_created_called_;
108 };
109 
WebContentsAddedObserver()110 WebContentsAddedObserver::WebContentsAddedObserver()
111     : web_contents_created_callback_(
112           base::Bind(
113               &WebContentsAddedObserver::WebContentsCreated,
114               base::Unretained(this))),
115       web_contents_(NULL) {
116   WebContentsImpl::AddCreatedCallback(web_contents_created_callback_);
117 }
118 
~WebContentsAddedObserver()119 WebContentsAddedObserver::~WebContentsAddedObserver() {
120   WebContentsImpl::RemoveCreatedCallback(web_contents_created_callback_);
121 }
122 
WebContentsCreated(WebContents * web_contents)123 void WebContentsAddedObserver::WebContentsCreated(WebContents* web_contents) {
124   DCHECK(!web_contents_);
125   web_contents_ = web_contents;
126   child_observer_.reset(new RenderViewCreatedObserver(web_contents));
127 
128   if (runner_.get())
129     runner_->QuitClosure().Run();
130 }
131 
GetWebContents()132 WebContents* WebContentsAddedObserver::GetWebContents() {
133   if (web_contents_)
134     return web_contents_;
135 
136   runner_ = new MessageLoopRunner();
137   runner_->Run();
138   return web_contents_;
139 }
140 
RenderViewCreatedCalled()141 bool WebContentsAddedObserver::RenderViewCreatedCalled() {
142   if (child_observer_)
143     return child_observer_->render_view_created_called_;
144 
145   return false;
146 }
147 
148 }  // namespace content
149