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.
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/browser/web_contents_view.h"
25 #include "content/public/test/browser_test_utils.h"
26 #include "third_party/WebKit/public/web/WebContextMenuData.h"
27 #include "third_party/WebKit/public/web/WebInputEvent.h"
29 using content::WebContents
;
33 class ContextMenuBrowserTest
: public InProcessBrowserTest
{
35 ContextMenuBrowserTest() { }
37 TestRenderViewContextMenu
* CreateContextMenu(GURL unfiltered_url
, GURL url
) {
38 content::ContextMenuParams params
;
39 params
.media_type
= blink::WebContextMenuData::MediaTypeNone
;
40 params
.unfiltered_link_url
= unfiltered_url
;
41 params
.link_url
= url
;
42 WebContents
* web_contents
=
43 browser()->tab_strip_model()->GetActiveWebContents();
44 params
.page_url
= web_contents
->GetController().GetActiveEntry()->GetURL();
45 #if defined(OS_MACOSX)
46 params
.writing_direction_default
= 0;
47 params
.writing_direction_left_to_right
= 0;
48 params
.writing_direction_right_to_left
= 0;
50 TestRenderViewContextMenu
* menu
= new TestRenderViewContextMenu(
51 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
58 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest
,
59 OpenEntryPresentForNormalURLs
) {
60 scoped_ptr
<TestRenderViewContextMenu
> menu(
61 CreateContextMenu(GURL("http://www.google.com/"),
62 GURL("http://www.google.com/")));
64 ASSERT_TRUE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB
));
65 ASSERT_TRUE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW
));
66 ASSERT_TRUE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_COPYLINKLOCATION
));
69 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest
,
70 OpenEntryAbsentForFilteredURLs
) {
71 scoped_ptr
<TestRenderViewContextMenu
> menu(
72 CreateContextMenu(GURL("chrome://history"),
75 ASSERT_FALSE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB
));
76 ASSERT_FALSE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW
));
77 ASSERT_TRUE(menu
->IsItemPresent(IDC_CONTENT_CONTEXT_COPYLINKLOCATION
));
80 // GTK requires a X11-level mouse event to open a context menu correctly.
81 #if defined(TOOLKIT_GTK)
82 #define MAYBE_RealMenu DISABLED_RealMenu
84 #define MAYBE_RealMenu RealMenu
86 // Opens a link in a new tab via a "real" context menu.
87 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest
,
89 ContextMenuNotificationObserver
menu_observer(
90 IDC_CONTENT_CONTEXT_OPENLINKNEWTAB
);
91 ui_test_utils::WindowedTabAddedNotificationObserver
tab_observer(
92 content::NotificationService::AllSources());
94 // Go to a page with a link
95 ui_test_utils::NavigateToURL(
96 browser(), GURL("data:text/html,<a href='about:blank'>link</a>"));
98 // Open a context menu.
99 blink::WebMouseEvent mouse_event
;
100 mouse_event
.type
= blink::WebInputEvent::MouseDown
;
101 mouse_event
.button
= blink::WebMouseEvent::ButtonRight
;
105 content::WebContents
* tab
=
106 browser()->tab_strip_model()->GetActiveWebContents();
107 tab
->GetView()->GetContainerBounds(&offset
);
108 mouse_event
.globalX
= 15 + offset
.x();
109 mouse_event
.globalY
= 15 + offset
.y();
110 mouse_event
.clickCount
= 1;
111 tab
->GetRenderViewHost()->ForwardMouseEvent(mouse_event
);
112 mouse_event
.type
= blink::WebInputEvent::MouseUp
;
113 tab
->GetRenderViewHost()->ForwardMouseEvent(mouse_event
);
115 // The menu_observer will select "Open in new tab", wait for the new tab to
118 tab
= tab_observer
.GetTab();
119 content::WaitForLoadStop(tab
);
121 // Verify that it's the correct tab.
122 EXPECT_EQ(GURL("about:blank"), tab
->GetURL());
125 // Verify that "Open Link in New Tab" doesn't send URL fragment as referrer.
126 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest
, OpenInNewTabReferrer
) {
127 ui_test_utils::WindowedTabAddedNotificationObserver
tab_observer(
128 content::NotificationService::AllSources());
130 ASSERT_TRUE(test_server()->Start());
131 GURL
echoheader(test_server()->GetURL("echoheader?Referer"));
133 // Go to a |page| with a link to echoheader URL.
134 GURL
page("data:text/html,<a href='" + echoheader
.spec() + "'>link</a>");
135 ui_test_utils::NavigateToURL(browser(), page
);
137 // Set up referrer URL with fragment.
138 const GURL
kReferrerWithFragment("http://foo.com/test#fragment");
139 const std::string
kCorrectReferrer("http://foo.com/test");
141 // Set up menu with link URL.
142 content::ContextMenuParams context_menu_params
;
143 context_menu_params
.page_url
= kReferrerWithFragment
;
144 context_menu_params
.link_url
= echoheader
;
146 // Select "Open Link in New Tab" and wait for the new tab to be added.
147 TestRenderViewContextMenu
menu(
148 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
149 context_menu_params
);
151 menu
.ExecuteCommand(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB
, 0);
154 content::WebContents
* tab
= tab_observer
.GetTab();
155 content::WaitForLoadStop(tab
);
157 // Verify that it's the correct tab.
158 ASSERT_EQ(echoheader
, tab
->GetURL());
159 // Verify that the text on the page matches |kCorrectReferrer|.
160 std::string actual_referrer
;
161 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
163 "window.domAutomationController.send(window.document.body.textContent);",
165 ASSERT_EQ(kCorrectReferrer
, actual_referrer
);
167 // Verify that the referrer on the page matches |kCorrectReferrer|.
168 std::string page_referrer
;
169 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
171 "window.domAutomationController.send(window.document.referrer);",
173 ASSERT_EQ(kCorrectReferrer
, page_referrer
);
176 // Verify that "Open Link in Incognito Window " doesn't send referrer URL.
177 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest
, OpenIncognitoNoneReferrer
) {
178 ui_test_utils::WindowedTabAddedNotificationObserver
tab_observer(
179 content::NotificationService::AllSources());
181 ASSERT_TRUE(test_server()->Start());
182 GURL
echoheader(test_server()->GetURL("echoheader?Referer"));
184 // Go to a |page| with a link to echoheader URL.
185 GURL
page("data:text/html,<a href='" + echoheader
.spec() + "'>link</a>");
186 ui_test_utils::NavigateToURL(browser(), page
);
188 // Set up referrer URL with fragment.
189 const GURL
kReferrerWithFragment("http://foo.com/test#fragment");
190 const std::string
kNoneReferrer("None");
191 const std::string
kEmptyReferrer("");
193 // Set up menu with link URL.
194 content::ContextMenuParams context_menu_params
;
195 context_menu_params
.page_url
= kReferrerWithFragment
;
196 context_menu_params
.link_url
= echoheader
;
198 // Select "Open Link in Incognito Window" and wait for window to be added.
199 TestRenderViewContextMenu
menu(
200 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(),
201 context_menu_params
);
203 menu
.ExecuteCommand(IDC_CONTENT_CONTEXT_OPENLINKOFFTHERECORD
, 0);
206 content::WebContents
* tab
= tab_observer
.GetTab();
207 content::WaitForLoadStop(tab
);
209 // Verify that it's the correct tab.
210 ASSERT_EQ(echoheader
, tab
->GetURL());
211 // Verify that the text on the page matches |kNoneReferrer|.
212 std::string actual_referrer
;
213 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
215 "window.domAutomationController.send(window.document.body.textContent);",
217 ASSERT_EQ(kNoneReferrer
, actual_referrer
);
219 // Verify that the referrer on the page matches |kEmptyReferrer|.
220 std::string page_referrer
;
221 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
223 "window.domAutomationController.send(window.document.referrer);",
225 ASSERT_EQ(kEmptyReferrer
, page_referrer
);