Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / public / test / content_browser_test_utils.cc
blobd20e98e2c7f0322dfd8e0f26de5dde72ab5d6f0f
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"
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/browser/shell.h"
19 #include "content/shell/browser/shell_javascript_dialog_manager.h"
20 #include "net/base/filename_util.h"
22 namespace content {
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)));
31 GURL GetTestUrl(const char* dir, const char* file) {
32 return net::FilePathToFileURL(GetTestFilePath(dir, file));
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);
42 window->LoadURL(url);
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);
52 window->Reload();
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(),
68 PAGE_TYPE_NORMAL))
69 return false;
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());
89 runner->Run();
92 ShellAddedObserver::ShellAddedObserver()
93 : shell_(NULL) {
94 Shell::SetShellCreatedCallback(
95 base::Bind(&ShellAddedObserver::ShellCreated, base::Unretained(this)));
98 ShellAddedObserver::~ShellAddedObserver() {
101 Shell* ShellAddedObserver::GetShell() {
102 if (shell_)
103 return shell_;
105 runner_ = new MessageLoopRunner();
106 runner_->Run();
107 return shell_;
110 void ShellAddedObserver::ShellCreated(Shell* shell) {
111 DCHECK(!shell_);
112 shell_ = shell;
113 if (runner_.get())
114 runner_->QuitClosure().Run();
118 } // namespace content