OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/app/chrome_command_ids.h" | 10 #include "chrome/app/chrome_command_ids.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" | 12 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h" |
13 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsert
est_util.h" | 13 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsert
est_util.h" |
14 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti
l.h" | 14 #include "chrome/browser/renderer_context_menu/render_view_context_menu_test_uti
l.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
17 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
18 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
19 #include "content/public/browser/navigation_controller.h" | 19 #include "content/public/browser/navigation_controller.h" |
20 #include "content/public/browser/navigation_entry.h" | 20 #include "content/public/browser/navigation_entry.h" |
21 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
22 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
23 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
24 #include "content/public/test/browser_test_utils.h" | 24 #include "content/public/test/browser_test_utils.h" |
| 25 #include "net/test/embedded_test_server/embedded_test_server.h" |
25 #include "third_party/WebKit/public/web/WebContextMenuData.h" | 26 #include "third_party/WebKit/public/web/WebContextMenuData.h" |
26 #include "third_party/WebKit/public/web/WebInputEvent.h" | 27 #include "third_party/WebKit/public/web/WebInputEvent.h" |
27 | 28 |
28 using content::WebContents; | 29 using content::WebContents; |
29 | 30 |
30 namespace { | 31 namespace { |
31 | 32 |
32 class ContextMenuBrowserTest : public InProcessBrowserTest { | 33 class ContextMenuBrowserTest : public InProcessBrowserTest { |
33 public: | 34 public: |
34 ContextMenuBrowserTest() { } | 35 ContextMenuBrowserTest() { } |
(...skipping 10 matching lines...) Expand all Loading... |
45 params.writing_direction_default = 0; | 46 params.writing_direction_default = 0; |
46 params.writing_direction_left_to_right = 0; | 47 params.writing_direction_left_to_right = 0; |
47 params.writing_direction_right_to_left = 0; | 48 params.writing_direction_right_to_left = 0; |
48 #endif // OS_MACOSX | 49 #endif // OS_MACOSX |
49 TestRenderViewContextMenu* menu = new TestRenderViewContextMenu( | 50 TestRenderViewContextMenu* menu = new TestRenderViewContextMenu( |
50 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), | 51 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame(), |
51 params); | 52 params); |
52 menu->Init(); | 53 menu->Init(); |
53 return menu; | 54 return menu; |
54 } | 55 } |
| 56 |
| 57 void OpenContextMenuAtPoint(int x, int y) { |
| 58 content::WebContents* tab = |
| 59 browser()->tab_strip_model()->GetActiveWebContents(); |
| 60 // Open context menu |
| 61 blink::WebMouseEvent mouse_event; |
| 62 mouse_event.button = blink::WebMouseEvent::ButtonRight; |
| 63 mouse_event.x = x; |
| 64 mouse_event.y = y; |
| 65 gfx::Rect offset = tab->GetContainerBounds(); |
| 66 mouse_event.globalX = x + offset.x(); |
| 67 mouse_event.globalY = y + offset.y(); |
| 68 mouse_event.clickCount = 1; |
| 69 mouse_event.type = blink::WebInputEvent::MouseDown; |
| 70 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event); |
| 71 mouse_event.type = blink::WebInputEvent::MouseUp; |
| 72 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event); |
| 73 } |
| 74 |
| 75 void ExecuteCustomMenuCommandAndVerifyTabTitle(unsigned command_id, |
| 76 const std::string& expected) { |
| 77 // menu_observer executes the menu item command with |command_id|. |
| 78 ContextMenuNotificationObserver menu_observer(command_id); |
| 79 // Right click on the center (50, 50) of the div with dimension (100, 100). |
| 80 OpenContextMenuAtPoint(50, 50); |
| 81 |
| 82 // The menu_observer will execute the action corresponding to |command_id|, |
| 83 // wait for the title to change. |
| 84 base::string16 title = base::ASCIIToUTF16(expected); |
| 85 content::WebContents* tab = |
| 86 browser()->tab_strip_model()->GetActiveWebContents(); |
| 87 content::TitleWatcher title_watcher(tab, title); |
| 88 ASSERT_EQ(title, title_watcher.WaitAndGetTitle()); |
| 89 |
| 90 // Verify that it's the correct tab. |
| 91 EXPECT_EQ(base::ASCIIToUTF16(expected), tab->GetTitle()); |
| 92 } |
55 }; | 93 }; |
56 | 94 |
57 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, | 95 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, |
58 OpenEntryPresentForNormalURLs) { | 96 OpenEntryPresentForNormalURLs) { |
59 scoped_ptr<TestRenderViewContextMenu> menu( | 97 scoped_ptr<TestRenderViewContextMenu> menu( |
60 CreateContextMenu(GURL("http://www.google.com/"), | 98 CreateContextMenu(GURL("http://www.google.com/"), |
61 GURL("http://www.google.com/"))); | 99 GURL("http://www.google.com/"))); |
62 | 100 |
63 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB)); | 101 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB)); |
64 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW)); | 102 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW)); |
(...skipping 30 matching lines...) Expand all Loading... |
95 ContextMenuNotificationObserver menu_observer( | 133 ContextMenuNotificationObserver menu_observer( |
96 IDC_CONTENT_CONTEXT_OPENLINKNEWTAB); | 134 IDC_CONTENT_CONTEXT_OPENLINKNEWTAB); |
97 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer( | 135 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer( |
98 content::NotificationService::AllSources()); | 136 content::NotificationService::AllSources()); |
99 | 137 |
100 // Go to a page with a link | 138 // Go to a page with a link |
101 ui_test_utils::NavigateToURL( | 139 ui_test_utils::NavigateToURL( |
102 browser(), GURL("data:text/html,<a href='about:blank'>link</a>")); | 140 browser(), GURL("data:text/html,<a href='about:blank'>link</a>")); |
103 | 141 |
104 // Open a context menu. | 142 // Open a context menu. |
105 blink::WebMouseEvent mouse_event; | 143 OpenContextMenuAtPoint(15, 15); |
106 mouse_event.type = blink::WebInputEvent::MouseDown; | |
107 mouse_event.button = blink::WebMouseEvent::ButtonRight; | |
108 mouse_event.x = 15; | |
109 mouse_event.y = 15; | |
110 content::WebContents* tab = | 144 content::WebContents* tab = |
111 browser()->tab_strip_model()->GetActiveWebContents(); | 145 browser()->tab_strip_model()->GetActiveWebContents(); |
112 gfx::Rect offset = tab->GetContainerBounds(); | |
113 mouse_event.globalX = 15 + offset.x(); | |
114 mouse_event.globalY = 15 + offset.y(); | |
115 mouse_event.clickCount = 1; | |
116 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event); | |
117 mouse_event.type = blink::WebInputEvent::MouseUp; | |
118 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event); | |
119 | 146 |
120 // The menu_observer will select "Open in new tab", wait for the new tab to | 147 // The menu_observer will select "Open in new tab", wait for the new tab to |
121 // be added. | 148 // be added. |
122 tab_observer.Wait(); | 149 tab_observer.Wait(); |
123 tab = tab_observer.GetTab(); | 150 tab = tab_observer.GetTab(); |
124 content::WaitForLoadStop(tab); | 151 content::WaitForLoadStop(tab); |
125 | 152 |
126 // Verify that it's the correct tab. | 153 // Verify that it's the correct tab. |
127 EXPECT_EQ(GURL("about:blank"), tab->GetURL()); | 154 EXPECT_EQ(GURL("about:blank"), tab->GetURL()); |
128 } | 155 } |
129 | 156 |
| 157 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, CustomMenu) { |
| 158 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 159 // Go to a test page |
| 160 GURL url = embedded_test_server()->GetURL( |
| 161 "/context_menu/custom_context_menu.html"); |
| 162 ui_test_utils::NavigateToURL(browser(), url); |
| 163 |
| 164 ExecuteCustomMenuCommandAndVerifyTabTitle( |
| 165 IDC_CONTENT_CONTEXT_CUSTOM_FIRST, "Title 1"); |
| 166 ExecuteCustomMenuCommandAndVerifyTabTitle( |
| 167 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 1, "Title 2"); |
| 168 ExecuteCustomMenuCommandAndVerifyTabTitle( |
| 169 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 2, "Title 3"); |
| 170 ExecuteCustomMenuCommandAndVerifyTabTitle( |
| 171 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 3, "Title 4"); |
| 172 ExecuteCustomMenuCommandAndVerifyTabTitle( |
| 173 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 4, "Title 5"); |
| 174 ExecuteCustomMenuCommandAndVerifyTabTitle( |
| 175 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 5, "Title 6"); |
| 176 ExecuteCustomMenuCommandAndVerifyTabTitle( |
| 177 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 6, "Title 7"); |
| 178 ExecuteCustomMenuCommandAndVerifyTabTitle( |
| 179 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 7, "Title 8"); |
| 180 ExecuteCustomMenuCommandAndVerifyTabTitle( |
| 181 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 8, "Title 9"); |
| 182 } |
| 183 |
130 // Verify that "Open Link in New Tab" doesn't send URL fragment as referrer. | 184 // Verify that "Open Link in New Tab" doesn't send URL fragment as referrer. |
131 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenInNewTabReferrer) { | 185 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenInNewTabReferrer) { |
132 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer( | 186 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer( |
133 content::NotificationService::AllSources()); | 187 content::NotificationService::AllSources()); |
134 | 188 |
135 ASSERT_TRUE(test_server()->Start()); | 189 ASSERT_TRUE(test_server()->Start()); |
136 GURL echoheader(test_server()->GetURL("echoheader?Referer")); | 190 GURL echoheader(test_server()->GetURL("echoheader?Referer")); |
137 | 191 |
138 // Go to a |page| with a link to echoheader URL. | 192 // Go to a |page| with a link to echoheader URL. |
139 GURL page("data:text/html,<a href='" + echoheader.spec() + "'>link</a>"); | 193 GURL page("data:text/html,<a href='" + echoheader.spec() + "'>link</a>"); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 content::NotificationService::AllSources()); | 291 content::NotificationService::AllSources()); |
238 | 292 |
239 // Go to a page with a link having download attribute. | 293 // Go to a page with a link having download attribute. |
240 const std::string kSuggestedFilename("test_filename.png"); | 294 const std::string kSuggestedFilename("test_filename.png"); |
241 ui_test_utils::NavigateToURL( | 295 ui_test_utils::NavigateToURL( |
242 browser(), | 296 browser(), |
243 GURL("data:text/html,<a href='about:blank' download='" + | 297 GURL("data:text/html,<a href='about:blank' download='" + |
244 kSuggestedFilename + "'>link</a>")); | 298 kSuggestedFilename + "'>link</a>")); |
245 | 299 |
246 // Open a context menu. | 300 // Open a context menu. |
247 blink::WebMouseEvent mouse_event; | 301 OpenContextMenuAtPoint(15, 15); |
248 mouse_event.type = blink::WebInputEvent::MouseDown; | |
249 mouse_event.button = blink::WebMouseEvent::ButtonRight; | |
250 mouse_event.x = 15; | |
251 mouse_event.y = 15; | |
252 content::WebContents* tab = | |
253 browser()->tab_strip_model()->GetActiveWebContents(); | |
254 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event); | |
255 mouse_event.type = blink::WebInputEvent::MouseUp; | |
256 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event); | |
257 | 302 |
258 // Wait for context menu to be visible. | 303 // Wait for context menu to be visible. |
259 menu_observer.WaitForMenu(); | 304 menu_observer.WaitForMenu(); |
260 | 305 |
261 // Compare filename. | 306 // Compare filename. |
262 base::string16 suggested_filename = menu_observer.GetSuggestedFilename(); | 307 base::string16 suggested_filename = menu_observer.GetSuggestedFilename(); |
263 ASSERT_EQ(kSuggestedFilename, base::UTF16ToUTF8(suggested_filename).c_str()); | 308 ASSERT_EQ(kSuggestedFilename, base::UTF16ToUTF8(suggested_filename).c_str()); |
264 } | 309 } |
265 | 310 |
266 // Ensure that View Page Info won't crash if there is no visible entry. | 311 // Ensure that View Page Info won't crash if there is no visible entry. |
(...skipping 15 matching lines...) Expand all Loading... |
282 menu.Init(); | 327 menu.Init(); |
283 | 328 |
284 // The item shouldn't be enabled in the menu. | 329 // The item shouldn't be enabled in the menu. |
285 EXPECT_FALSE(menu.IsCommandIdEnabled(IDC_CONTENT_CONTEXT_VIEWPAGEINFO)); | 330 EXPECT_FALSE(menu.IsCommandIdEnabled(IDC_CONTENT_CONTEXT_VIEWPAGEINFO)); |
286 | 331 |
287 // Ensure that viewing page info doesn't crash even if you can get to it. | 332 // Ensure that viewing page info doesn't crash even if you can get to it. |
288 menu.ExecuteCommand(IDC_CONTENT_CONTEXT_VIEWPAGEINFO, 0); | 333 menu.ExecuteCommand(IDC_CONTENT_CONTEXT_VIEWPAGEINFO, 0); |
289 } | 334 } |
290 | 335 |
291 } // namespace | 336 } // namespace |
OLD | NEW |