Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / xwalk_runtime_browsertest.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 "base/command_line.h"
6 #include "base/files/file_path.h"
7 #include "base/files/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/browser/runtime_ui_delegate.h"
16 #include "xwalk/runtime/common/xwalk_notification_types.h"
17 #include "xwalk/test/base/in_process_browser_test.h"
18 #include "xwalk/test/base/xwalk_test_utils.h"
19 #include "content/public/browser/navigation_controller.h"
20 #include "content/public/browser/navigation_entry.h"
21 #include "content/public/browser/notification_details.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/browser/notification_source.h"
26 #include "content/public/browser/render_view_host.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/test/browser_test_utils.h"
29 #include "content/public/test/test_utils.h"
30 #include "net/base/net_util.h"
31 #include "testing/gmock/include/gmock/gmock.h"
32
33 #if defined(USE_AURA)
34 #include "ui/aura/window.h"
35 #endif
36
37 using xwalk::NativeAppWindow;
38 using xwalk::Runtime;
39 using content::NotificationService;
40 using content::WebContents;
41 using content::WindowedNotificationObserver;
42 using testing::_;
43
44 class XWalkRuntimeTest : public InProcessBrowserTest {
45 };
46
47 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, CreateAndCloseRuntime) {
48   size_t len = runtimes().size();
49   // Create a new Runtime instance.
50   GURL url(test_server()->GetURL("test.html"));
51   Runtime* runtime = CreateRuntime(url);
52   EXPECT_TRUE(url == runtime->web_contents()->GetURL());
53   EXPECT_EQ(len + 1, runtimes().size());
54
55   // Close the newly created Runtime instance.
56   runtime->Close();
57   content::RunAllPendingInMessageLoop();
58   EXPECT_EQ(len, runtimes().size());
59 }
60
61 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, LoadURLAndClose) {
62   GURL url(test_server()->GetURL("test.html"));
63   Runtime* runtime = CreateRuntime(url);
64   size_t len = runtimes().size();
65   runtime->Close();
66   content::RunAllPendingInMessageLoop();
67   EXPECT_EQ(len - 1, runtimes().size());
68 }
69
70 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, CloseNativeWindow) {
71   GURL url(test_server()->GetURL("test.html"));
72   Runtime* new_runtime = CreateRuntime(url);
73   size_t len = runtimes().size();
74   new_runtime->window()->Close();
75   content::RunAllPendingInMessageLoop();
76   // Closing native window will lead to close Runtime instance.
77   EXPECT_EQ(len - 1, runtimes().size());
78 }
79
80 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, LaunchWithFullscreenWindow) {
81   GURL url(test_server()->GetURL("test.html"));
82   NativeAppWindow::CreateParams params;
83   params.state = ui::SHOW_STATE_FULLSCREEN;
84   Runtime* new_runtime = CreateRuntime(url, params);
85
86   EXPECT_TRUE(new_runtime->window()->IsFullscreen());
87 }
88
89 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, HTML5FullscreenAPI) {
90   GURL url = xwalk_test_utils::GetTestURL(
91       base::FilePath(), base::FilePath().AppendASCII("fullscreen.html"));
92   Runtime* runtime = CreateRuntime(url);
93   EXPECT_TRUE(false == runtime->window()->IsFullscreen());
94
95   WindowedNotificationObserver enter_observer(
96      xwalk::NOTIFICATION_FULLSCREEN_CHANGED,
97      NotificationService::AllSources());
98
99   bool ret = content::ExecuteScript(
100       runtime->web_contents(), "doFullscreenClick();");
101   EXPECT_TRUE(ret);
102   content::RunAllPendingInMessageLoop();
103   enter_observer.Wait();
104   // Calling doFullscreenClick defined in fullscreen.html leads to enter into
105   // fullscreen window state, so it's expected to be fullscreen.
106   EXPECT_TRUE(true == runtime->window()->IsFullscreen());
107
108   WindowedNotificationObserver exit_observer(
109      xwalk::NOTIFICATION_FULLSCREEN_CHANGED,
110      NotificationService::AllSources());
111
112   ret = content::ExecuteScript(
113       runtime->web_contents(), "doExitFullscreenClick();");
114   EXPECT_TRUE(ret);
115   content::RunAllPendingInMessageLoop();
116   exit_observer.Wait();
117   // Calling doExitFullscreenClick defined in fullscreen.html leads to exit
118   // fullscreen window state, so it's expected to be not fullscreen.
119   EXPECT_TRUE(false == runtime->window()->IsFullscreen());
120 }
121
122
123 #if !defined(OS_MACOSX)
124 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, GetWindowTitle) {
125   GURL url = xwalk_test_utils::GetTestURL(
126       base::FilePath(), base::FilePath().AppendASCII("title.html"));
127   base::string16 title = base::ASCIIToUTF16("Dummy Title");
128   Runtime* runtime = CreateRuntime();
129   content::TitleWatcher title_watcher(runtime->web_contents(), title);
130   xwalk_test_utils::NavigateToURL(runtime, url);
131   EXPECT_EQ(title, title_watcher.WaitAndGetTitle());
132
133   NativeAppWindow* window = runtime->window();
134   base::string16 window_title = window->GetNativeWindow()->title();
135   EXPECT_EQ(title, window_title);
136 }
137 #endif
138
139 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, OpenLinkInNewRuntime) {
140   GURL url = xwalk_test_utils::GetTestURL(
141       base::FilePath(), base::FilePath().AppendASCII("new_target.html"));
142   Runtime* runtime = CreateRuntime(url);
143   size_t len = runtimes().size();
144   bool ret = content::ExecuteScript(runtime->web_contents(), "doClick();");
145   EXPECT_TRUE(ret);
146   content::RunAllPendingInMessageLoop();
147   // Calling doClick defined in new_target.html leads to open a href in a new
148   // target window, and so it is expected to create a new Runtime instance.
149   EXPECT_EQ(len + 1, runtimes().size());
150   Runtime* second = runtimes().back();
151   EXPECT_TRUE(NULL != second);
152   EXPECT_NE(runtime, second);
153 }
154
155 #if defined(OS_TIZEN)
156 IN_PROC_BROWSER_TEST_F(XWalkRuntimeTest, LoadTizenWebUiFwFile) {
157   GURL url = xwalk_test_utils::GetTestURL(
158       base::FilePath(), base::FilePath().AppendASCII("tizenwebuifw.html"));
159   base::string16 title = base::ASCIIToUTF16("Pass");
160   Runtime* runtime = CreateRuntime();
161   content::TitleWatcher title_watcher(runtime->web_contents(), title);
162   xwalk_test_utils::NavigateToURL(runtime, url);
163   EXPECT_EQ(title, title_watcher.WaitAndGetTitle());
164 }
165 #endif