Home | History | Annotate | Download | only in test
      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/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/public/browser/navigation_controller.h"
     12 #include "content/public/browser/notification_source.h"
     13 #include "content/public/browser/web_contents.h"
     14 #include "content/public/common/content_paths.h"
     15 #include "content/public/test/browser_test_utils.h"
     16 #include "content/public/test/test_navigation_observer.h"
     17 #include "content/public/test/test_utils.h"
     18 #include "content/shell/shell.h"
     19 #include "content/shell/shell_javascript_dialog_manager.h"
     20 #include "net/base/net_util.h"
     21 
     22 namespace content {
     23 
     24 base::FilePath GetTestFilePath(const char* dir, const char* file) {
     25   base::FilePath path;
     26   PathService::Get(DIR_TEST_DATA, &path);
     27   return path.Append(base::FilePath().AppendASCII(dir).Append(
     28       base::FilePath().AppendASCII(file)));
     29 }
     30 
     31 GURL GetTestUrl(const char* dir, const char* file) {
     32   return net::FilePathToFileURL(GetTestFilePath(dir, file));
     33 }
     34 
     35 void NavigateToURLBlockUntilNavigationsComplete(Shell* window,
     36                                                 const GURL& url,
     37                                                 int number_of_navigations) {
     38   WaitForLoadStop(window->web_contents());
     39   TestNavigationObserver same_tab_observer(window->web_contents(),
     40                                            number_of_navigations);
     41 
     42   window->LoadURL(url);
     43 
     44   base::RunLoop run_loop;
     45   same_tab_observer.WaitForObservation(
     46       base::Bind(&RunThisRunLoop, base::Unretained(&run_loop)),
     47       GetQuitTaskForRunLoop(&run_loop));
     48 }
     49 
     50 void NavigateToURL(Shell* window, const GURL& url) {
     51   NavigateToURLBlockUntilNavigationsComplete(window, url, 1);
     52 }
     53 
     54 void WaitForAppModalDialog(Shell* window) {
     55   ShellJavaScriptDialogManager* dialog_manager=
     56       static_cast<ShellJavaScriptDialogManager*>(
     57           window->GetJavaScriptDialogManager());
     58 
     59   scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner();
     60   dialog_manager->set_dialog_request_callback(runner->QuitClosure());
     61   runner->Run();
     62 }
     63 
     64 ShellAddedObserver::ShellAddedObserver()
     65     : shell_(NULL) {
     66   Shell::SetShellCreatedCallback(
     67       base::Bind(&ShellAddedObserver::ShellCreated, base::Unretained(this)));
     68 }
     69 
     70 ShellAddedObserver::~ShellAddedObserver() {
     71 }
     72 
     73 Shell* ShellAddedObserver::GetShell() {
     74   if (shell_)
     75     return shell_;
     76 
     77   runner_ = new MessageLoopRunner();
     78   runner_->Run();
     79   return shell_;
     80 }
     81 
     82 void ShellAddedObserver::ShellCreated(Shell* shell) {
     83   DCHECK(!shell_);
     84   shell_ = shell;
     85   if (runner_.get())
     86     runner_->QuitClosure().Run();
     87 }
     88 
     89 }  // namespace content
     90