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.
5 #include "content/public/test/content_browser_test_utils.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/browser/shell.h"
19 #include "content/shell/browser/shell_javascript_dialog_manager.h"
20 #include "net/base/filename_util.h"
24 base::FilePath
GetTestFilePath(const char* dir
, const char* file
) {
26 PathService::Get(DIR_TEST_DATA
, &path
);
27 return path
.Append(base::FilePath().AppendASCII(dir
).Append(
28 base::FilePath().AppendASCII(file
)));
31 GURL
GetTestUrl(const char* dir
, const char* file
) {
32 return net::FilePathToFileURL(GetTestFilePath(dir
, file
));
35 void NavigateToURLBlockUntilNavigationsComplete(Shell
* window
,
37 int number_of_navigations
) {
38 WaitForLoadStop(window
->web_contents());
39 TestNavigationObserver
same_tab_observer(window
->web_contents(),
40 number_of_navigations
);
43 same_tab_observer
.Wait();
46 void ReloadBlockUntilNavigationsComplete(Shell
* window
,
47 int number_of_navigations
) {
48 WaitForLoadStop(window
->web_contents());
49 TestNavigationObserver
same_tab_observer(window
->web_contents(),
50 number_of_navigations
);
53 same_tab_observer
.Wait();
56 void LoadDataWithBaseURL(Shell
* window
, const GURL
& url
,
57 const std::string data
, const GURL
& base_url
) {
58 WaitForLoadStop(window
->web_contents());
59 TestNavigationObserver
same_tab_observer(window
->web_contents(), 1);
61 window
->LoadDataWithBaseURL(url
, data
, base_url
);
62 same_tab_observer
.Wait();
65 bool NavigateToURL(Shell
* window
, const GURL
& url
) {
66 NavigateToURLBlockUntilNavigationsComplete(window
, url
, 1);
67 if (!IsLastCommittedEntryOfPageType(window
->web_contents(),
70 return window
->web_contents()->GetLastCommittedURL() == url
;
73 bool NavigateToURLAndExpectNoCommit(Shell
* window
, const GURL
& url
) {
74 NavigationEntry
* old_entry
=
75 window
->web_contents()->GetController().GetLastCommittedEntry();
76 NavigateToURLBlockUntilNavigationsComplete(window
, url
, 1);
77 NavigationEntry
* new_entry
=
78 window
->web_contents()->GetController().GetLastCommittedEntry();
79 return old_entry
== new_entry
;
82 void WaitForAppModalDialog(Shell
* window
) {
83 ShellJavaScriptDialogManager
* dialog_manager
=
84 static_cast<ShellJavaScriptDialogManager
*>(
85 window
->GetJavaScriptDialogManager(window
->web_contents()));
87 scoped_refptr
<MessageLoopRunner
> runner
= new MessageLoopRunner();
88 dialog_manager
->set_dialog_request_callback(runner
->QuitClosure());
92 ShellAddedObserver::ShellAddedObserver()
94 Shell::SetShellCreatedCallback(
95 base::Bind(&ShellAddedObserver::ShellCreated
, base::Unretained(this)));
98 ShellAddedObserver::~ShellAddedObserver() {
101 Shell
* ShellAddedObserver::GetShell() {
105 runner_
= new MessageLoopRunner();
110 void ShellAddedObserver::ShellCreated(Shell
* shell
) {
114 runner_
->QuitClosure().Run();
118 } // namespace content