9cfb8ab6e15ff39bc65c940601f228b8fd0fa441
[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 void RuntimeRegistry::CloseAll() {
59   RuntimeList cached(runtimes_);
60   std::for_each(cached.begin(), cached.end(), std::mem_fun(&Runtime::Close));
61   DCHECK(runtimes_.empty()) << runtimes_.size();
62 }
63
64 void RuntimeRegistry::OnRuntimeAdded(Runtime* runtime) {
65   DCHECK(runtime);
66   runtimes_.push_back(runtime);
67 }
68
69 void RuntimeRegistry::OnRuntimeRemoved(Runtime* runtime) {
70   DCHECK(runtime);
71   RuntimeList::iterator it =
72        std::find(runtimes_.begin(), runtimes_.end(), runtime);
73   DCHECK(it != runtimes_.end());
74   runtimes_.erase(it);
75
76   if (runtimes_.empty())
77     base::MessageLoop::current()->PostTask(
78         FROM_HERE, base::MessageLoop::QuitClosure());
79 }
80
81 InProcessBrowserTest::InProcessBrowserTest()
82     : runtime_registry_(new RuntimeRegistry),
83       runtime_(NULL) {
84   CreateTestServer(base::FilePath(FILE_PATH_LITERAL("xwalk/test/data")));
85 }
86
87 InProcessBrowserTest::~InProcessBrowserTest() {
88 }
89
90 CommandLine InProcessBrowserTest::GetCommandLineForRelaunch() {
91   CommandLine new_command_line(CommandLine::ForCurrentProcess()->GetProgram());
92   CommandLine::SwitchMap switches =
93       CommandLine::ForCurrentProcess()->GetSwitches();
94   new_command_line.AppendSwitch(content::kLaunchAsBrowser);
95
96   for (CommandLine::SwitchMap::const_iterator iter = switches.begin();
97         iter != switches.end(); ++iter) {
98     new_command_line.AppendSwitchNative((*iter).first, (*iter).second);
99   }
100   return new_command_line;
101 }
102
103 void InProcessBrowserTest::SetUp() {
104   CommandLine* command_line = CommandLine::ForCurrentProcess();
105   // Allow subclasses to change the command line before running any tests.
106   SetUpCommandLine(command_line);
107   // Add command line arguments that are used by all InProcessBrowserTests.
108   PrepareTestCommandLine(command_line);
109
110   // Single-process mode is not set in BrowserMain, so process it explicitly,
111   // and set up renderer.
112   if (command_line->HasSwitch(switches::kSingleProcess)) {
113     content::SetRendererClientForTesting(
114         &g_xwalk_content_renderer_client.Get());
115   }
116
117   BrowserTestBase::SetUp();
118 }
119
120 void InProcessBrowserTest::PrepareTestCommandLine(CommandLine* command_line) {
121   // Propagate commandline settings from test_launcher_utils.
122   xwalk_test_utils::PrepareBrowserCommandLineForTests(command_line);
123 }
124
125 const InProcessBrowserTest::RuntimeList& InProcessBrowserTest::runtimes()
126                                                                const {
127   return runtime_registry_->runtimes();
128 }
129
130 void InProcessBrowserTest::RunTestOnMainThreadLoop() {
131   // Pump startup related events.
132   content::RunAllPendingInMessageLoop();
133   // FIXME : Unfortunately too many tests now rely on the 'runtime()'
134   // method, instead they should just create runtimes themselves
135   // when needed and thus the 'runtime()' method should be removed
136   // as well as 'runtime_' initialization below.
137   runtime_ = Runtime::CreateWithDefaultWindow(
138       XWalkRunner::GetInstance()->runtime_context(),
139           GURL(), runtime_registry_.get());
140   content::WaitForLoadStop(runtime_->web_contents());
141   content::RunAllPendingInMessageLoop();
142
143   SetUpOnMainThread();
144
145   if (!HasFatalFailure())
146     RunTestOnMainThread();
147
148   // Invoke cleanup and quit even if there are failures. This is similar to
149   // gtest in that it invokes TearDown even if Setup fails.
150   CleanUpOnMainThread();
151   // Sometimes tests leave Quit tasks in the MessageLoop (for shame), so let's
152   // run all pending messages here to avoid preempting the QuitBrowsers tasks.
153   content::RunAllPendingInMessageLoop();
154
155   runtime_registry_->CloseAll();
156 }
157
158 bool InProcessBrowserTest::CreateDataPathDir() {
159   CommandLine* command_line = CommandLine::ForCurrentProcess();
160   base::FilePath data_path_dir =
161       command_line->GetSwitchValuePath(switches::kXWalkDataPath);
162   if (data_path_dir.empty()) {
163     if (temp_data_path_dir_.CreateUniqueTempDir() &&
164         temp_data_path_dir_.IsValid()) {
165       data_path_dir = temp_data_path_dir_.path();
166     } else {
167       LOG(ERROR) << "Could not create temporary data directory \""
168                  << temp_data_path_dir_.path().value() << "\".";
169       return false;
170     }
171   }
172   return xwalk_test_utils::OverrideDataPathDir(data_path_dir);
173 }