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 "base/command_line.h"
6 #include "base/files/file_path.h"
7 #include "base/file_util.h"
8 #include "base/path_service.h"
9 #include "base/process/launch.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "xwalk/runtime/browser/image_util.h"
14 #include "xwalk/runtime/browser/runtime.h"
15 #include "xwalk/runtime/common/xwalk_notification_types.h"
16 #include "xwalk/test/base/in_process_browser_test.h"
17 #include "xwalk/test/base/xwalk_test_utils.h"
18 #include "content/public/browser/navigation_controller.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/notification_details.h"
21 #include "content/public/browser/notification_observer.h"
22 #include "content/public/browser/notification_registrar.h"
23 #include "content/public/browser/notification_service.h"
24 #include "content/public/browser/notification_source.h"
25 #include "content/public/browser/render_view_host.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/test/browser_test_utils.h"
28 #include "content/public/test/test_utils.h"
29 #include "net/base/net_util.h"
30 #include "testing/gmock/include/gmock/gmock.h"
33 #include "ui/aura/window.h"
36 using xwalk::NativeAppWindow;
38 using content::WebContents;
41 // Observer for NOTIFICATION_FULLSCREEN_CHANGED notifications.
42 class FullscreenNotificationObserver
43 : public content::WindowedNotificationObserver {
45 FullscreenNotificationObserver() : WindowedNotificationObserver(
46 xwalk::NOTIFICATION_FULLSCREEN_CHANGED,
47 content::NotificationService::AllSources()) {}
49 DISALLOW_COPY_AND_ASSIGN(FullscreenNotificationObserver);
52 class XWalkRuntimeTest : public InProcessBrowserTest {
55 virtual ~XWalkRuntimeTest() {
56 original_runtimes_.clear();
57 notification_observer_.reset();
60 void Relaunch(const CommandLine& new_command_line) {
61 base::LaunchProcess(new_command_line, base::LaunchOptions(), NULL);
64 // SetUpOnMainThread is called after BrowserMainRunner was initialized and
65 // just before RunTestOnMainThread (aka. TestBody).
66 virtual void SetUpOnMainThread() OVERRIDE {
67 notification_observer_.reset(
68 new content::WindowedNotificationObserver(
69 xwalk::NOTIFICATION_RUNTIME_OPENED,
70 content::NotificationService::AllSources()));
71 original_runtimes_.assign(runtimes().begin(), runtimes().end());
74 // Block UI thread until a new Runtime instance is created.
75 Runtime* WaitForSingleNewRuntime() {
76 notification_observer_->Wait();
77 const RuntimeList& runtime_list = runtimes();
78 for (RuntimeList::const_iterator it = runtime_list.begin();
79 it != runtime_list.end(); ++it) {
80 RuntimeList::iterator target =
81 std::find(original_runtimes_.begin(), original_runtimes_.end(), *it);
82 // Not found means a new one.
83 if (target == original_runtimes_.end()) {
84 original_runtimes_.push_back(*it);
92 RuntimeList original_runtimes_;
93 scoped_ptr<content::WindowedNotificationObserver> notification_observer_;
96 // FIXME(hmin): Since currently the browser process is not shared by multiple
97 // app launch, this test is disabled to avoid floody launches.
98 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, DISABLED_SecondLaunch) {
99 Relaunch(GetCommandLineForRelaunch());
101 Runtime* second_runtime = NULL;
102 EXPECT_TRUE(second_runtime == WaitForSingleNewRuntime());
103 ASSERT_EQ(2u, runtimes().size());
106 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, CreateAndCloseRuntime) {
107 size_t len = runtimes().size();
110 // Create a new Runtime instance.
111 GURL url(test_server()->GetURL("test.html"));
112 Runtime* new_runtime = Runtime::CreateWithDefaultWindow(
113 GetRuntimeContext(), url, runtime_registry());
114 EXPECT_TRUE(url == new_runtime->web_contents()->GetURL());
115 EXPECT_EQ(new_runtime, WaitForSingleNewRuntime());
116 content::RunAllPendingInMessageLoop();
117 EXPECT_EQ(len + 1, runtimes().size());
119 // Close the newly created Runtime instance.
120 new_runtime->Close();
121 content::RunAllPendingInMessageLoop();
122 EXPECT_EQ(len, runtimes().size());
125 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, LoadURLAndClose) {
126 GURL url(test_server()->GetURL("test.html"));
127 size_t len = runtimes().size();
128 runtime()->LoadURL(url);
129 content::RunAllPendingInMessageLoop();
130 EXPECT_EQ(len, runtimes().size());
132 content::RunAllPendingInMessageLoop();
133 EXPECT_EQ(len - 1, runtimes().size());
136 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, CloseNativeWindow) {
137 GURL url(test_server()->GetURL("test.html"));
138 Runtime* new_runtime = Runtime::CreateWithDefaultWindow(
139 GetRuntimeContext(), url, runtime_registry());
140 size_t len = runtimes().size();
141 new_runtime->window()->Close();
142 content::RunAllPendingInMessageLoop();
143 // Closing native window will lead to close Runtime instance.
144 EXPECT_EQ(len - 1, runtimes().size());
147 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, LaunchWithFullscreenWindow) {
148 GURL url(test_server()->GetURL("test.html"));
149 Runtime* new_runtime = Runtime::Create(
150 GetRuntimeContext(), runtime_registry());
152 NativeAppWindow::CreateParams params;
153 params.state = ui::SHOW_STATE_FULLSCREEN;
154 new_runtime->AttachWindow(params);
155 xwalk_test_utils::NavigateToURL(new_runtime, url);
157 EXPECT_TRUE(new_runtime->window()->IsFullscreen());
160 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, HTML5FullscreenAPI) {
161 size_t len = runtimes().size();
162 GURL url = xwalk_test_utils::GetTestURL(
163 base::FilePath(), base::FilePath().AppendASCII("fullscreen.html"));
164 xwalk_test_utils::NavigateToURL(runtime(), url);
165 EXPECT_TRUE(false == runtime()->window()->IsFullscreen());
167 FullscreenNotificationObserver enter_observer;
168 bool ret = content::ExecuteScript(
169 runtime()->web_contents(), "doFullscreenClick();");
171 content::RunAllPendingInMessageLoop();
172 enter_observer.Wait();
173 // Calling doFullscreenClick defined in fullscreen.html leads to enter into
174 // fullscreen window state, so it's expected to be fullscreen.
175 EXPECT_TRUE(true == runtime()->window()->IsFullscreen());
177 FullscreenNotificationObserver exit_observer;
178 ret = content::ExecuteScript(
179 runtime()->web_contents(), "doExitFullscreenClick();");
181 content::RunAllPendingInMessageLoop();
182 exit_observer.Wait();
183 // Calling doExitFullscreenClick defined in fullscreen.html leads to exit
184 // fullscreen window state, so it's expected to be not fullscreen.
185 EXPECT_TRUE(false == runtime()->window()->IsFullscreen());
189 #if !defined(OS_MACOSX)
190 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, GetWindowTitle) {
191 GURL url = xwalk_test_utils::GetTestURL(
192 base::FilePath(), base::FilePath().AppendASCII("title.html"));
193 base::string16 title = base::ASCIIToUTF16("Dummy Title");
194 content::TitleWatcher title_watcher(runtime()->web_contents(), title);
195 xwalk_test_utils::NavigateToURL(runtime(), url);
196 EXPECT_EQ(title, title_watcher.WaitAndGetTitle());
198 NativeAppWindow* window = runtime()->window();
199 base::string16 window_title = window->GetNativeWindow()->title();
200 EXPECT_EQ(title, window_title);
204 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, OpenLinkInNewRuntime) {
205 size_t len = runtimes().size();
206 GURL url = xwalk_test_utils::GetTestURL(
207 base::FilePath(), base::FilePath().AppendASCII("new_target.html"));
208 xwalk_test_utils::NavigateToURL(runtime(), url);
209 bool ret = content::ExecuteScript(runtime()->web_contents(), "doClick();");
211 content::RunAllPendingInMessageLoop();
212 // Calling doClick defined in new_target.html leads to open a href in a new
213 // target window, and so it is expected to create a new Runtime instance.
214 Runtime* second = WaitForSingleNewRuntime();
215 EXPECT_TRUE(NULL != second);
216 EXPECT_NE(runtime(), second);
217 EXPECT_EQ(len + 1, runtimes().size());
220 #if defined(OS_TIZEN)
221 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, LoadTizenWebUiFwFile) {
222 GURL url = xwalk_test_utils::GetTestURL(
223 base::FilePath(), base::FilePath().AppendASCII("tizenwebuifw.html"));
224 base::string16 title = base::ASCIIToUTF16("Pass");
225 content::TitleWatcher title_watcher(runtime()->web_contents(), title);
226 xwalk_test_utils::NavigateToURL(runtime(), url);
227 EXPECT_EQ(title, title_watcher.WaitAndGetTitle());