a0ba604c43827ff91e3760e14b42d82b83f2cafc
[platform/framework/web/crosswalk.git] / src / xwalk / test / base / in_process_browser_test.cc
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.
4
5 #include "xwalk/test/base/in_process_browser_test.h"
6
7 #include <algorithm>
8 #include "base/auto_reset.h"
9 #include "base/bind.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"
33
34 #if defined(OS_TIZEN)
35 #include "xwalk/runtime/renderer/tizen/xwalk_content_renderer_client_tizen.h"
36 #endif
37
38 using xwalk::Runtime;
39 using xwalk::XWalkContentRendererClient;
40 using xwalk::XWalkRunner;
41
42 namespace {
43
44 // Used when running in single-process mode.
45 #if defined(OS_TIZEN)
46 base::LazyInstance<XWalkContentRendererClientTizen>::Leaky
47         g_xwalk_content_renderer_client = LAZY_INSTANCE_INITIALIZER;
48 #else
49 base::LazyInstance<XWalkContentRendererClient>::Leaky
50         g_xwalk_content_renderer_client = LAZY_INSTANCE_INITIALIZER;
51 #endif
52
53 }  // namespace
54
55 RuntimeRegistry::RuntimeRegistry() {
56 }
57
58 RuntimeRegistry::~RuntimeRegistry() {
59 }
60
61 void RuntimeRegistry::CloseAll() {
62   if (runtimes_.empty())
63     return;
64
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();
70 }
71
72 void RuntimeRegistry::OnRuntimeAdded(Runtime* runtime) {
73   DCHECK(runtime);
74   runtimes_.push_back(runtime);
75 }
76
77 void RuntimeRegistry::OnRuntimeRemoved(Runtime* runtime) {
78   DCHECK(runtime);
79   RuntimeList::iterator it =
80        std::find(runtimes_.begin(), runtimes_.end(), runtime);
81   DCHECK(it != runtimes_.end());
82   runtimes_.erase(it);
83
84   if (runtimes_.empty())
85     base::MessageLoop::current()->PostTask(
86         FROM_HERE, base::MessageLoop::QuitClosure());
87 }
88
89 InProcessBrowserTest::InProcessBrowserTest()
90     : runtime_registry_(new RuntimeRegistry),
91       runtime_(NULL) {
92   CreateTestServer(base::FilePath(FILE_PATH_LITERAL("xwalk/test/data")));
93 }
94
95 InProcessBrowserTest::~InProcessBrowserTest() {
96 }
97
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);
104
105   for (base::CommandLine::SwitchMap::const_iterator iter = switches.begin();
106         iter != switches.end(); ++iter) {
107     new_command_line.AppendSwitchNative((*iter).first, (*iter).second);
108   }
109   return new_command_line;
110 }
111
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);
118
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());
124   }
125
126   BrowserTestBase::SetUp();
127 }
128
129 xwalk::RuntimeContext* InProcessBrowserTest::GetRuntimeContext() const {
130   return XWalkRunner::GetInstance()->runtime_context();
131 }
132
133 void InProcessBrowserTest::PrepareTestCommandLine(
134     base::CommandLine* command_line) {
135   // Propagate commandline settings from test_launcher_utils.
136   xwalk_test_utils::PrepareBrowserCommandLineForTests(command_line);
137 }
138
139 const InProcessBrowserTest::RuntimeList& InProcessBrowserTest::runtimes()
140                                                                const {
141   return runtime_registry_->runtimes();
142 }
143
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(
152           GetRuntimeContext(),
153           GURL(), runtime_registry_.get());
154   content::WaitForLoadStop(runtime_->web_contents());
155   content::RunAllPendingInMessageLoop();
156
157   SetUpOnMainThread();
158
159   if (!HasFatalFailure())
160     RunTestOnMainThread();
161
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();
165
166   runtime_registry_->CloseAll();
167 }
168
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();
177     } else {
178       LOG(ERROR) << "Could not create temporary data directory \""
179                  << temp_data_path_dir_.path().value() << "\".";
180       return false;
181     }
182   }
183   return xwalk_test_utils::OverrideDataPathDir(data_path_dir);
184 }