Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / renderer_context_menu / render_view_context_menu_browsertest.cc
1 // Copyright 2014 The Chromium Authors. 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 <string>
6
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
13 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsertest_util.h"
14 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_util.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/test/base/in_process_browser_test.h"
18 #include "chrome/test/base/ui_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_service.h"
22 #include "content/public/browser/render_view_host.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/test/browser_test_utils.h"
25 #include "third_party/WebKit/public/web/WebContextMenuData.h"
26 #include "third_party/WebKit/public/web/WebInputEvent.h"
27
28 using content::WebContents;
29
30 namespace {
31
32 class ContextMenuBrowserTest : public InProcessBrowserTest {
33  public:
34   ContextMenuBrowserTest() { }
35
36   TestRenderViewContextMenu* CreateContextMenu(GURL unfiltered_url, GURL url) {
37     content::ContextMenuParams params;
38     params.media_type = blink::WebContextMenuData::MediaTypeNone;
39     params.unfiltered_link_url = unfiltered_url;
40     params.link_url = url;
41     WebContents* web_contents =
42         browser()->tab_strip_model()->GetActiveWebContents();
43     params.page_url = web_contents->GetController().GetActiveEntry()->GetURL();
44 #if defined(OS_MACOSX)
45     params.writing_direction_default = 0;
46     params.writing_direction_left_to_right = 0;
47     params.writing_direction_right_to_left = 0;
48 #endif  // OS_MACOSX
49     TestRenderViewContextMenu* menu = new TestRenderViewContextMenu(
50         browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
51         params);
52     menu->Init();
53     return menu;
54   }
55 };
56
57 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest,
58                        OpenEntryPresentForNormalURLs) {
59   scoped_ptr<TestRenderViewContextMenu> menu(
60       CreateContextMenu(GURL("http://www.google.com/"),
61                         GURL("http://www.google.com/")));
62
63   ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
64   ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
65   ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
66 }
67
68 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest,
69                        OpenEntryAbsentForFilteredURLs) {
70   scoped_ptr<TestRenderViewContextMenu> menu(
71       CreateContextMenu(GURL("chrome://history"),
72                         GURL()));
73
74   ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB));
75   ASSERT_FALSE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW));
76   ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_COPYLINKLOCATION));
77 }
78
79 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest,
80                        SaveAsImageForCanvas) {
81   content::ContextMenuParams params;
82   params.media_type = blink::WebContextMenuData::MediaTypeCanvas;
83
84   TestRenderViewContextMenu menu(
85       browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
86       params);
87   menu.Init();
88
89   ASSERT_TRUE(menu.IsItemPresent(IDC_CONTENT_CONTEXT_SAVEIMAGEAS));
90 }
91
92 // GTK requires a X11-level mouse event to open a context menu correctly.
93 #if defined(TOOLKIT_GTK)
94 #define MAYBE_RealMenu DISABLED_RealMenu
95 #else
96 #define MAYBE_RealMenu RealMenu
97 #endif
98 // Opens a link in a new tab via a "real" context menu.
99 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest,
100                        MAYBE_RealMenu) {
101   ContextMenuNotificationObserver menu_observer(
102       IDC_CONTENT_CONTEXT_OPENLINKNEWTAB);
103   ui_test_utils::WindowedTabAddedNotificationObserver tab_observer(
104       content::NotificationService::AllSources());
105
106   // Go to a page with a link
107   ui_test_utils::NavigateToURL(
108       browser(), GURL("data:text/html,<a href='about:blank'>link</a>"));
109
110   // Open a context menu.
111   blink::WebMouseEvent mouse_event;
112   mouse_event.type = blink::WebInputEvent::MouseDown;
113   mouse_event.button = blink::WebMouseEvent::ButtonRight;
114   mouse_event.x = 15;
115   mouse_event.y = 15;
116   content::WebContents* tab =
117       browser()->tab_strip_model()->GetActiveWebContents();
118   gfx::Rect offset = tab->GetContainerBounds();
119   mouse_event.globalX = 15 + offset.x();
120   mouse_event.globalY = 15 + offset.y();
121   mouse_event.clickCount = 1;
122   tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
123   mouse_event.type = blink::WebInputEvent::MouseUp;
124   tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
125
126   // The menu_observer will select "Open in new tab", wait for the new tab to
127   // be added.
128   tab_observer.Wait();
129   tab = tab_observer.GetTab();
130   content::WaitForLoadStop(tab);
131
132   // Verify that it's the correct tab.
133   EXPECT_EQ(GURL("about:blank"), tab->GetURL());
134 }
135
136 // Verify that "Open Link in New Tab" doesn't send URL fragment as referrer.
137 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenInNewTabReferrer) {
138   ui_test_utils::WindowedTabAddedNotificationObserver tab_observer(
139       content::NotificationService::AllSources());
140
141   ASSERT_TRUE(test_server()->Start());
142   GURL echoheader(test_server()->GetURL("echoheader?Referer"));
143
144   // Go to a |page| with a link to echoheader URL.
145   GURL page("data:text/html,<a href='" + echoheader.spec() + "'>link</a>");
146   ui_test_utils::NavigateToURL(browser(), page);
147
148   // Set up referrer URL with fragment.
149   const GURL kReferrerWithFragment("http://foo.com/test#fragment");
150   const std::string kCorrectReferrer("http://foo.com/test");
151
152   // Set up menu with link URL.
153   content::ContextMenuParams context_menu_params;
154   context_menu_params.page_url = kReferrerWithFragment;
155   context_menu_params.link_url = echoheader;
156
157   // Select "Open Link in New Tab" and wait for the new tab to be added.
158   TestRenderViewContextMenu menu(
159       browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
160       context_menu_params);
161   menu.Init();
162   menu.ExecuteCommand(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB, 0);
163
164   tab_observer.Wait();
165   content::WebContents* tab = tab_observer.GetTab();
166   content::WaitForLoadStop(tab);
167
168   // Verify that it's the correct tab.
169   ASSERT_EQ(echoheader, tab->GetURL());
170   // Verify that the text on the page matches |kCorrectReferrer|.
171   std::string actual_referrer;
172   ASSERT_TRUE(content::ExecuteScriptAndExtractString(
173       tab,
174       "window.domAutomationController.send(window.document.body.textContent);",
175       &actual_referrer));
176   ASSERT_EQ(kCorrectReferrer, actual_referrer);
177
178   // Verify that the referrer on the page matches |kCorrectReferrer|.
179   std::string page_referrer;
180   ASSERT_TRUE(content::ExecuteScriptAndExtractString(
181       tab,
182       "window.domAutomationController.send(window.document.referrer);",
183       &page_referrer));
184   ASSERT_EQ(kCorrectReferrer, page_referrer);
185 }
186
187 // Verify that "Open Link in Incognito Window " doesn't send referrer URL.
188 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenIncognitoNoneReferrer) {
189   ui_test_utils::WindowedTabAddedNotificationObserver tab_observer(
190       content::NotificationService::AllSources());
191
192   ASSERT_TRUE(test_server()->Start());
193   GURL echoheader(test_server()->GetURL("echoheader?Referer"));
194
195   // Go to a |page| with a link to echoheader URL.
196   GURL page("data:text/html,<a href='" + echoheader.spec() + "'>link</a>");
197   ui_test_utils::NavigateToURL(browser(), page);
198
199   // Set up referrer URL with fragment.
200   const GURL kReferrerWithFragment("http://foo.com/test#fragment");
201   const std::string kNoneReferrer("None");
202   const std::string kEmptyReferrer("");
203
204   // Set up menu with link URL.
205   content::ContextMenuParams context_menu_params;
206   context_menu_params.page_url = kReferrerWithFragment;
207   context_menu_params.link_url = echoheader;
208
209   // Select "Open Link in Incognito Window" and wait for window to be added.
210   TestRenderViewContextMenu menu(
211       browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
212       context_menu_params);
213   menu.Init();
214   menu.ExecuteCommand(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD, 0);
215
216   tab_observer.Wait();
217   content::WebContents* tab = tab_observer.GetTab();
218   content::WaitForLoadStop(tab);
219
220   // Verify that it's the correct tab.
221   ASSERT_EQ(echoheader, tab->GetURL());
222   // Verify that the text on the page matches |kNoneReferrer|.
223   std::string actual_referrer;
224   ASSERT_TRUE(content::ExecuteScriptAndExtractString(
225       tab,
226       "window.domAutomationController.send(window.document.body.textContent);",
227       &actual_referrer));
228   ASSERT_EQ(kNoneReferrer, actual_referrer);
229
230   // Verify that the referrer on the page matches |kEmptyReferrer|.
231   std::string page_referrer;
232   ASSERT_TRUE(content::ExecuteScriptAndExtractString(
233       tab,
234       "window.domAutomationController.send(window.document.referrer);",
235       &page_referrer));
236   ASSERT_EQ(kEmptyReferrer, page_referrer);
237 }
238
239 // Ensure that View Page Info won't crash if there is no visible entry.
240 // See http://crbug.com/370863.
241 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, ViewPageInfoWithNoEntry) {
242   // Create a new tab with no committed entry.
243   ui_test_utils::WindowedTabAddedNotificationObserver tab_observer(
244       content::NotificationService::AllSources());
245   ASSERT_TRUE(content::ExecuteScript(
246       browser()->tab_strip_model()->GetActiveWebContents(), "window.open();"));
247   tab_observer.Wait();
248   content::WebContents* tab = tab_observer.GetTab();
249   EXPECT_FALSE(tab->GetController().GetLastCommittedEntry());
250   EXPECT_FALSE(tab->GetController().GetVisibleEntry());
251
252   // Create a context menu.
253   content::ContextMenuParams context_menu_params;
254   TestRenderViewContextMenu menu(tab->GetMainFrame(), context_menu_params);
255   menu.Init();
256
257   // The item shouldn't be enabled in the menu.
258   EXPECT_FALSE(menu.IsCommandIdEnabled(IDC_CONTENT_CONTEXT_VIEWPAGEINFO));
259
260   // Ensure that viewing page info doesn't crash even if you can get to it.
261   menu.ExecuteCommand(IDC_CONTENT_CONTEXT_VIEWPAGEINFO, 0);
262 }
263
264 }  // namespace