Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / public / test / content_browser_test_utils.cc
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/public/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/browser/shell.h"
19 #include "content/shell/browser/shell_javascript_dialog_manager.h"
20 #include "net/base/filename_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   same_tab_observer.Wait();
44 }
45
46 void LoadDataWithBaseURL(Shell* window, const GURL& url,
47     const std::string data, const GURL& base_url) {
48   WaitForLoadStop(window->web_contents());
49   TestNavigationObserver same_tab_observer(window->web_contents(), 1);
50
51   window->LoadDataWithBaseURL(url, data, base_url);
52   same_tab_observer.Wait();
53 }
54
55 void NavigateToURL(Shell* window, const GURL& url) {
56   NavigateToURLBlockUntilNavigationsComplete(window, url, 1);
57 }
58
59 void WaitForAppModalDialog(Shell* window) {
60   ShellJavaScriptDialogManager* dialog_manager=
61       static_cast<ShellJavaScriptDialogManager*>(
62           window->GetJavaScriptDialogManager());
63
64   scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner();
65   dialog_manager->set_dialog_request_callback(runner->QuitClosure());
66   runner->Run();
67 }
68
69 ShellAddedObserver::ShellAddedObserver()
70     : shell_(NULL) {
71   Shell::SetShellCreatedCallback(
72       base::Bind(&ShellAddedObserver::ShellCreated, base::Unretained(this)));
73 }
74
75 ShellAddedObserver::~ShellAddedObserver() {
76 }
77
78 Shell* ShellAddedObserver::GetShell() {
79   if (shell_)
80     return shell_;
81
82   runner_ = new MessageLoopRunner();
83   runner_->Run();
84   return shell_;
85 }
86
87 void ShellAddedObserver::ShellCreated(Shell* shell) {
88   DCHECK(!shell_);
89   shell_ = shell;
90   if (runner_.get())
91     runner_->QuitClosure().Run();
92 }
93
94 }  // namespace content