1 // Copyright (c) 2013 Intel Corporation. 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 "xwalk/test/base/in_process_browser_test.h"
8 #include "base/auto_reset.h"
10 #include "base/command_line.h"
11 #include "base/debug/leak_annotations.h"
12 #include "base/files/file_path.h"
13 #include "base/file_util.h"
14 #include "base/lazy_instance.h"
15 #include "base/path_service.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/test/test_file_util.h"
18 #include "content/public/browser/notification_service.h"
19 #include "content/public/browser/notification_types.h"
20 #include "content/public/common/content_switches.h"
21 #include "content/public/test/browser_test_utils.h"
22 #include "content/public/test/test_browser_thread.h"
23 #include "content/public/test/test_launcher.h"
24 #include "content/public/test/test_navigation_observer.h"
25 #include "content/public/test/test_utils.h"
26 #include "net/test/spawned_test_server/spawned_test_server.h"
27 #include "xwalk/runtime/browser/xwalk_runner.h"
28 #include "xwalk/runtime/common/xwalk_paths.h"
29 #include "xwalk/runtime/common/xwalk_switches.h"
30 #include "xwalk/runtime/renderer/xwalk_content_renderer_client.h"
31 #include "xwalk/test/base/xwalk_test_suite.h"
32 #include "xwalk/test/base/xwalk_test_utils.h"
35 #include "xwalk/runtime/renderer/tizen/xwalk_content_renderer_client_tizen.h"
39 using xwalk::XWalkContentRendererClient;
40 using xwalk::XWalkRunner;
44 // Used when running in single-process mode.
46 base::LazyInstance<XWalkContentRendererClientTizen>::Leaky
47 g_xwalk_content_renderer_client = LAZY_INSTANCE_INITIALIZER;
49 base::LazyInstance<XWalkContentRendererClient>::Leaky
50 g_xwalk_content_renderer_client = LAZY_INSTANCE_INITIALIZER;
55 RuntimeRegistry::RuntimeRegistry() {
58 RuntimeRegistry::~RuntimeRegistry() {
61 void RuntimeRegistry::CloseAll() {
62 if (runtimes_.empty())
65 RuntimeList cached(runtimes_);
66 std::for_each(cached.begin(), cached.end(), std::mem_fun(&Runtime::Close));
67 // Wait until all windows are closed.
68 content::RunAllPendingInMessageLoop();
69 DCHECK(runtimes_.empty()) << runtimes_.size();
72 void RuntimeRegistry::OnRuntimeAdded(Runtime* runtime) {
74 runtimes_.push_back(runtime);
77 void RuntimeRegistry::OnRuntimeRemoved(Runtime* runtime) {
79 RuntimeList::iterator it =
80 std::find(runtimes_.begin(), runtimes_.end(), runtime);
81 DCHECK(it != runtimes_.end());
84 if (runtimes_.empty())
85 base::MessageLoop::current()->PostTask(
86 FROM_HERE, base::MessageLoop::QuitClosure());
89 InProcessBrowserTest::InProcessBrowserTest()
90 : runtime_registry_(new RuntimeRegistry),
92 CreateTestServer(base::FilePath(FILE_PATH_LITERAL("xwalk/test/data")));
95 InProcessBrowserTest::~InProcessBrowserTest() {
98 base::CommandLine InProcessBrowserTest::GetCommandLineForRelaunch() {
99 base::CommandLine new_command_line(
100 base::CommandLine::ForCurrentProcess()->GetProgram());
101 CommandLine::SwitchMap switches =
102 CommandLine::ForCurrentProcess()->GetSwitches();
103 new_command_line.AppendSwitch(content::kLaunchAsBrowser);
105 for (base::CommandLine::SwitchMap::const_iterator iter = switches.begin();
106 iter != switches.end(); ++iter) {
107 new_command_line.AppendSwitchNative((*iter).first, (*iter).second);
109 return new_command_line;
112 void InProcessBrowserTest::SetUp() {
113 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
114 // Allow subclasses to change the command line before running any tests.
115 SetUpCommandLine(command_line);
116 // Add command line arguments that are used by all InProcessBrowserTests.
117 PrepareTestCommandLine(command_line);
119 // Single-process mode is not set in BrowserMain, so process it explicitly,
120 // and set up renderer.
121 if (command_line->HasSwitch(switches::kSingleProcess)) {
122 content::SetRendererClientForTesting(
123 &g_xwalk_content_renderer_client.Get());
126 BrowserTestBase::SetUp();
129 xwalk::RuntimeContext* InProcessBrowserTest::GetRuntimeContext() const {
130 return XWalkRunner::GetInstance()->runtime_context();
133 void InProcessBrowserTest::PrepareTestCommandLine(
134 base::CommandLine* command_line) {
135 // Propagate commandline settings from test_launcher_utils.
136 xwalk_test_utils::PrepareBrowserCommandLineForTests(command_line);
139 const InProcessBrowserTest::RuntimeList& InProcessBrowserTest::runtimes()
141 return runtime_registry_->runtimes();
144 void InProcessBrowserTest::RunTestOnMainThreadLoop() {
145 // Pump startup related events.
146 content::RunAllPendingInMessageLoop();
147 // FIXME : Unfortunately too many tests now rely on the 'runtime()'
148 // method, instead they should just create runtimes themselves
149 // when needed and thus the 'runtime()' method should be removed
150 // as well as 'runtime_' initialization below.
151 runtime_ = Runtime::CreateWithDefaultWindow(
153 GURL(), runtime_registry_.get());
154 content::WaitForLoadStop(runtime_->web_contents());
155 content::RunAllPendingInMessageLoop();
159 if (!HasFatalFailure())
160 RunTestOnMainThread();
162 // Invoke cleanup and quit even if there are failures. This is similar to
163 // gtest in that it invokes TearDown even if Setup fails.
164 ProperMainThreadCleanup();
166 runtime_registry_->CloseAll();
169 bool InProcessBrowserTest::CreateDataPathDir() {
170 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
171 base::FilePath data_path_dir =
172 command_line->GetSwitchValuePath(switches::kXWalkDataPath);
173 if (data_path_dir.empty()) {
174 if (temp_data_path_dir_.CreateUniqueTempDir() &&
175 temp_data_path_dir_.IsValid()) {
176 data_path_dir = temp_data_path_dir_.path();
178 LOG(ERROR) << "Could not create temporary data directory \""
179 << temp_data_path_dir_.path().value() << "\".";
183 return xwalk_test_utils::OverrideDataPathDir(data_path_dir);