[email protected] | 78089f0 | 2012-07-19 06:11:28 | [diff] [blame] | 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" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
[email protected] | 78089f0 | 2012-07-19 06:11:28 | [diff] [blame] | 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" |
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 15 | #include "content/public/test/browser_test_utils.h" |
[email protected] | 78089f0 | 2012-07-19 06:11:28 | [diff] [blame] | 16 | #include "content/public/test/test_navigation_observer.h" |
[email protected] | a7fe911 | 2012-07-20 02:34:45 | [diff] [blame] | 17 | #include "content/public/test/test_utils.h" |
[email protected] | de7d61ff | 2013-08-20 11:30:41 | [diff] [blame] | 18 | #include "content/shell/browser/shell.h" |
| 19 | #include "content/shell/browser/shell_javascript_dialog_manager.h" |
[email protected] | 78089f0 | 2012-07-19 06:11:28 | [diff] [blame] | 20 | #include "net/base/net_util.h" |
| 21 | |
| 22 | namespace content { |
| 23 | |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 24 | base::FilePath GetTestFilePath(const char* dir, const char* file) { |
| 25 | base::FilePath path; |
[email protected] | 78089f0 | 2012-07-19 06:11:28 | [diff] [blame] | 26 | PathService::Get(DIR_TEST_DATA, &path); |
[email protected] | d30a36f | 2013-02-07 04:16:26 | [diff] [blame] | 27 | return path.Append(base::FilePath().AppendASCII(dir).Append( |
| 28 | base::FilePath().AppendASCII(file))); |
[email protected] | 78089f0 | 2012-07-19 06:11:28 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | GURL GetTestUrl(const char* dir, const char* file) { |
| 32 | return net::FilePathToFileURL(GetTestFilePath(dir, file)); |
| 33 | } |
| 34 | |
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 35 | void NavigateToURLBlockUntilNavigationsComplete(Shell* window, |
| 36 | const GURL& url, |
| 37 | int number_of_navigations) { |
| 38 | WaitForLoadStop(window->web_contents()); |
[email protected] | cbb1ef59 | 2013-06-05 19:49:46 | [diff] [blame] | 39 | TestNavigationObserver same_tab_observer(window->web_contents(), |
| 40 | number_of_navigations); |
[email protected] | 78089f0 | 2012-07-19 06:11:28 | [diff] [blame] | 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 | |
[email protected] | 4cdc9a2 | 2012-07-26 03:39:31 | [diff] [blame] | 50 | void NavigateToURL(Shell* window, const GURL& url) { |
| 51 | NavigateToURLBlockUntilNavigationsComplete(window, url, 1); |
| 52 | } |
| 53 | |
[email protected] | 95b2791 | 2012-07-23 23:14:59 | [diff] [blame] | 54 | void WaitForAppModalDialog(Shell* window) { |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 55 | ShellJavaScriptDialogManager* dialog_manager= |
| 56 | static_cast<ShellJavaScriptDialogManager*>( |
| 57 | window->GetJavaScriptDialogManager()); |
[email protected] | 95b2791 | 2012-07-23 23:14:59 | [diff] [blame] | 58 | |
| 59 | scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner(); |
[email protected] | 71a88bb | 2013-02-01 22:05:15 | [diff] [blame] | 60 | dialog_manager->set_dialog_request_callback(runner->QuitClosure()); |
[email protected] | 95b2791 | 2012-07-23 23:14:59 | [diff] [blame] | 61 | runner->Run(); |
| 62 | } |
| 63 | |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 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; |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 85 | if (runner_.get()) |
[email protected] | 9e00e635 | 2012-07-30 17:05:18 | [diff] [blame] | 86 | runner_->QuitClosure().Run(); |
| 87 | } |
| 88 | |
[email protected] | 78089f0 | 2012-07-19 06:11:28 | [diff] [blame] | 89 | } // namespace content |