a54a81f1750cbf9298a2dc934d1f811823c871ca
[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 void InProcessBrowserTest::PrepareTestCommandLine(
130     base::CommandLine* command_line) {
131   // Propagate commandline settings from test_launcher_utils.
132   xwalk_test_utils::PrepareBrowserCommandLineForTests(command_line);
133 }
134
135 const InProcessBrowserTest::RuntimeList& InProcessBrowserTest::runtimes()
136                                                                const {
137   return runtime_registry_->runtimes();
138 }
139
140 void InProcessBrowserTest::RunTestOnMainThreadLoop() {
141   // Pump startup related events.
142   content::RunAllPendingInMessageLoop();
143   // FIXME : Unfortunately too many tests now rely on the 'runtime()'
144   // method, instead they should just create runtimes themselves
145   // when needed and thus the 'runtime()' method should be removed
146   // as well as 'runtime_' initialization below.
147   runtime_ = Runtime::CreateWithDefaultWindow(
148       XWalkRunner::GetInstance()->runtime_context(),
149           GURL(), runtime_registry_.get());
150   content::WaitForLoadStop(runtime_->web_contents());
151   content::RunAllPendingInMessageLoop();
152
153   SetUpOnMainThread();
154
155   if (!HasFatalFailure())
156     RunTestOnMainThread();
157
158   // Invoke cleanup and quit even if there are failures. This is similar to
159   // gtest in that it invokes TearDown even if Setup fails.
160   ProperMainThreadCleanup();
161
162   runtime_registry_->CloseAll();
163 }
164
165 bool InProcessBrowserTest::CreateDataPathDir() {
166   base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
167   base::FilePath data_path_dir =
168       command_line->GetSwitchValuePath(switches::kXWalkDataPath);
169   if (data_path_dir.empty()) {
170     if (temp_data_path_dir_.CreateUniqueTempDir() &&
171         temp_data_path_dir_.IsValid()) {
172       data_path_dir = temp_data_path_dir_.path();
173     } else {
174       LOG(ERROR) << "Could not create temporary data directory \""
175                  << temp_data_path_dir_.path().value() << "\".";
176       return false;
177     }
178   }
179   return xwalk_test_utils::OverrideDataPathDir(data_path_dir);
180 }