15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "content/public/test/content_browser_test_utils.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/path_service.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/run_loop.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/browser/web_contents/web_contents_impl.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/navigation_controller.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/notification_source.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/common/content_paths.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/browser_test_utils.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/test_navigation_observer.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/test/test_utils.h"
193551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/shell.h"
203551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "content/shell/browser/shell_javascript_dialog_manager.h"
21c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "net/base/filename_util.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::FilePath GetTestFilePath(const char* dir, const char* file) {
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath path;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PathService::Get(DIR_TEST_DATA, &path);
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return path.Append(base::FilePath().AppendASCII(dir).Append(
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      base::FilePath().AppendASCII(file)));
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL GetTestUrl(const char* dir, const char* file) {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return net::FilePathToFileURL(GetTestFilePath(dir, file));
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NavigateToURLBlockUntilNavigationsComplete(Shell* window,
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                const GURL& url,
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                                int number_of_navigations) {
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  WaitForLoadStop(window->web_contents());
40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  TestNavigationObserver same_tab_observer(window->web_contents(),
41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                                           number_of_navigations);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  window->LoadURL(url);
444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  same_tab_observer.Wait();
450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch}
460529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid ReloadBlockUntilNavigationsComplete(Shell* window,
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                         int number_of_navigations) {
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  WaitForLoadStop(window->web_contents());
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  TestNavigationObserver same_tab_observer(window->web_contents(),
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                           number_of_navigations);
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  window->Reload();
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  same_tab_observer.Wait();
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
570529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochvoid LoadDataWithBaseURL(Shell* window, const GURL& url,
580529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    const std::string data, const GURL& base_url) {
590529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  WaitForLoadStop(window->web_contents());
600529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  TestNavigationObserver same_tab_observer(window->web_contents(), 1);
610529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
620529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  window->LoadDataWithBaseURL(url, data, base_url);
630529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  same_tab_observer.Wait();
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void NavigateToURL(Shell* window, const GURL& url) {
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NavigateToURLBlockUntilNavigationsComplete(window, url, 1);
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void WaitForAppModalDialog(Shell* window) {
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ShellJavaScriptDialogManager* dialog_manager=
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      static_cast<ShellJavaScriptDialogManager*>(
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          window->GetJavaScriptDialogManager());
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner();
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  dialog_manager->set_dialog_request_callback(runner->QuitClosure());
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  runner->Run();
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellAddedObserver::ShellAddedObserver()
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : shell_(NULL) {
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Shell::SetShellCreatedCallback(
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::Bind(&ShellAddedObserver::ShellCreated, base::Unretained(this)));
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ShellAddedObserver::~ShellAddedObserver() {
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Shell* ShellAddedObserver::GetShell() {
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (shell_)
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return shell_;
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  runner_ = new MessageLoopRunner();
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  runner_->Run();
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return shell_;
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ShellAddedObserver::ShellCreated(Shell* shell) {
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DCHECK(!shell_);
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  shell_ = shell;
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  if (runner_.get())
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    runner_->QuitClosure().Run();
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class RenderViewCreatedObserver : public WebContentsObserver {
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public:
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  explicit RenderViewCreatedObserver(WebContents* web_contents)
108cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      : WebContentsObserver(web_contents),
109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        render_view_created_called_(false) {
110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // WebContentsObserver:
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void RenderViewCreated(RenderViewHost* rvh) OVERRIDE {
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    render_view_created_called_ = true;
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
116cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool render_view_created_called_;
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)WebContentsAddedObserver::WebContentsAddedObserver()
121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    : web_contents_created_callback_(
122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          base::Bind(
123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)              &WebContentsAddedObserver::WebContentsCreated,
124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)              base::Unretained(this))),
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      web_contents_(NULL) {
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  WebContentsImpl::AddCreatedCallback(web_contents_created_callback_);
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)WebContentsAddedObserver::~WebContentsAddedObserver() {
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  WebContentsImpl::RemoveCreatedCallback(web_contents_created_callback_);
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void WebContentsAddedObserver::WebContentsCreated(WebContents* web_contents) {
134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(!web_contents_);
135cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  web_contents_ = web_contents;
136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  child_observer_.reset(new RenderViewCreatedObserver(web_contents));
137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (runner_.get())
139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    runner_->QuitClosure().Run();
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)WebContents* WebContentsAddedObserver::GetWebContents() {
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (web_contents_)
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return web_contents_;
145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  runner_ = new MessageLoopRunner();
147cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  runner_->Run();
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return web_contents_;
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool WebContentsAddedObserver::RenderViewCreatedCalled() {
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (child_observer_)
153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return child_observer_->render_view_created_called_;
154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
155cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return false;
156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
159