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, | |
lazyboy
2014/09/19 21:38:18
Fix indentation of params.
pals
2014/09/22 05:05:26
Done.
| |
76 const std::string& expected) { | |
77 // menu_observer executes the menu item command with |command_id|. | |
78 ContextMenuNotificationObserver menu_observer(command_id); | |
79 OpenContextMenuAtPoint(50, 50); | |
lazyboy
2014/09/19 21:38:18
Add a comment noting the significance of point (50
pals
2014/09/22 05:05:26
Done.
| |
80 | |
81 // The menu_observer will execute the action corresponding to |command_id|, | |
82 // wait for the title to change. | |
83 base::string16 title = base::ASCIIToUTF16(expected); | |
84 content::WebContents* tab = | |
85 browser()->tab_strip_model()->GetActiveWebContents(); | |
86 content::TitleWatcher title_watcher(tab, title); | |
87 ASSERT_EQ(title, title_watcher.WaitAndGetTitle()); | |
88 | |
89 // Verify that it's the correct tab. | |
90 EXPECT_EQ(base::ASCIIToUTF16(expected), tab->GetTitle()); | |
91 } | |
55 }; | 92 }; |
56 | 93 |
57 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, | 94 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, |
58 OpenEntryPresentForNormalURLs) { | 95 OpenEntryPresentForNormalURLs) { |
59 scoped_ptr<TestRenderViewContextMenu> menu( | 96 scoped_ptr<TestRenderViewContextMenu> menu( |
60 CreateContextMenu(GURL("http://www.google.com/"), | 97 CreateContextMenu(GURL("http://www.google.com/"), |
61 GURL("http://www.google.com/"))); | 98 GURL("http://www.google.com/"))); |
62 | 99 |
63 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB)); | 100 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWTAB)); |
64 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW)); | 101 ASSERT_TRUE(menu->IsItemPresent(IDC_CONTENT_CONTEXT_OPENLINKNEWWINDOW)); |
(...skipping 30 matching lines...) Expand all Loading... | |
95 ContextMenuNotificationObserver menu_observer( | 132 ContextMenuNotificationObserver menu_observer( |
96 IDC_CONTENT_CONTEXT_OPENLINKNEWTAB); | 133 IDC_CONTENT_CONTEXT_OPENLINKNEWTAB); |
97 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer( | 134 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer( |
98 content::NotificationService::AllSources()); | 135 content::NotificationService::AllSources()); |
99 | 136 |
100 // Go to a page with a link | 137 // Go to a page with a link |
101 ui_test_utils::NavigateToURL( | 138 ui_test_utils::NavigateToURL( |
102 browser(), GURL("data:text/html,<a href='about:blank'>link</a>")); | 139 browser(), GURL("data:text/html,<a href='about:blank'>link</a>")); |
103 | 140 |
104 // Open a context menu. | 141 // Open a context menu. |
105 blink::WebMouseEvent mouse_event; | 142 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 = | 143 content::WebContents* tab = |
111 browser()->tab_strip_model()->GetActiveWebContents(); | 144 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 | 145 |
120 // The menu_observer will select "Open in new tab", wait for the new tab to | 146 // The menu_observer will select "Open in new tab", wait for the new tab to |
121 // be added. | 147 // be added. |
122 tab_observer.Wait(); | 148 tab_observer.Wait(); |
123 tab = tab_observer.GetTab(); | 149 tab = tab_observer.GetTab(); |
124 content::WaitForLoadStop(tab); | 150 content::WaitForLoadStop(tab); |
125 | 151 |
126 // Verify that it's the correct tab. | 152 // Verify that it's the correct tab. |
127 EXPECT_EQ(GURL("about:blank"), tab->GetURL()); | 153 EXPECT_EQ(GURL("about:blank"), tab->GetURL()); |
128 } | 154 } |
129 | 155 |
156 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, | |
lazyboy
2014/09/19 21:38:18
nit: this can fit in one line.
pals
2014/09/22 05:05:26
Done.
| |
157 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( | |
lazyboy
2014/09/19 21:38:18
nit: can fit in one line.
pals
2014/09/22 05:05:26
Done.
| |
163 browser(), url); | |
164 | |
165 ExecuteCustomMenuCommandAndVerifyTabTitle( | |
166 IDC_CONTENT_CONTEXT_CUSTOM_FIRST, "Title 1"); | |
167 ExecuteCustomMenuCommandAndVerifyTabTitle( | |
168 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 1, "Title 2"); | |
169 ExecuteCustomMenuCommandAndVerifyTabTitle( | |
170 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 2, "Title 3"); | |
171 ExecuteCustomMenuCommandAndVerifyTabTitle( | |
172 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 3, "Title 4"); | |
173 ExecuteCustomMenuCommandAndVerifyTabTitle( | |
174 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 4, "Title 5"); | |
175 ExecuteCustomMenuCommandAndVerifyTabTitle( | |
176 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 5, "Title 6"); | |
177 ExecuteCustomMenuCommandAndVerifyTabTitle( | |
178 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 6, "Title 7"); | |
179 ExecuteCustomMenuCommandAndVerifyTabTitle( | |
180 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 7, "Title 8"); | |
181 ExecuteCustomMenuCommandAndVerifyTabTitle( | |
182 IDC_CONTENT_CONTEXT_CUSTOM_FIRST + 8, "Title 9"); | |
183 } | |
184 | |
130 // Verify that "Open Link in New Tab" doesn't send URL fragment as referrer. | 185 // Verify that "Open Link in New Tab" doesn't send URL fragment as referrer. |
131 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenInNewTabReferrer) { | 186 IN_PROC_BROWSER_TEST_F(ContextMenuBrowserTest, OpenInNewTabReferrer) { |
132 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer( | 187 ui_test_utils::WindowedTabAddedNotificationObserver tab_observer( |
133 content::NotificationService::AllSources()); | 188 content::NotificationService::AllSources()); |
134 | 189 |
135 ASSERT_TRUE(test_server()->Start()); | 190 ASSERT_TRUE(test_server()->Start()); |
136 GURL echoheader(test_server()->GetURL("echoheader?Referer")); | 191 GURL echoheader(test_server()->GetURL("echoheader?Referer")); |
137 | 192 |
138 // Go to a |page| with a link to echoheader URL. | 193 // Go to a |page| with a link to echoheader URL. |
139 GURL page("data:text/html,<a href='" + echoheader.spec() + "'>link</a>"); | 194 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()); | 292 content::NotificationService::AllSources()); |
238 | 293 |
239 // Go to a page with a link having download attribute. | 294 // Go to a page with a link having download attribute. |
240 const std::string kSuggestedFilename("test_filename.png"); | 295 const std::string kSuggestedFilename("test_filename.png"); |
241 ui_test_utils::NavigateToURL( | 296 ui_test_utils::NavigateToURL( |
242 browser(), | 297 browser(), |
243 GURL("data:text/html,<a href='about:blank' download='" + | 298 GURL("data:text/html,<a href='about:blank' download='" + |
244 kSuggestedFilename + "'>link</a>")); | 299 kSuggestedFilename + "'>link</a>")); |
245 | 300 |
246 // Open a context menu. | 301 // Open a context menu. |
247 blink::WebMouseEvent mouse_event; | 302 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 | 303 |
258 // Wait for context menu to be visible. | 304 // Wait for context menu to be visible. |
259 menu_observer.WaitForMenu(); | 305 menu_observer.WaitForMenu(); |
260 | 306 |
261 // Compare filename. | 307 // Compare filename. |
262 base::string16 suggested_filename = menu_observer.GetSuggestedFilename(); | 308 base::string16 suggested_filename = menu_observer.GetSuggestedFilename(); |
263 ASSERT_EQ(kSuggestedFilename, base::UTF16ToUTF8(suggested_filename).c_str()); | 309 ASSERT_EQ(kSuggestedFilename, base::UTF16ToUTF8(suggested_filename).c_str()); |
264 } | 310 } |
265 | 311 |
266 // Ensure that View Page Info won't crash if there is no visible entry. | 312 // Ensure that View Page Info won't crash if there is no visible entry. |
(...skipping 15 matching lines...) Expand all Loading... | |
282 menu.Init(); | 328 menu.Init(); |
283 | 329 |
284 // The item shouldn't be enabled in the menu. | 330 // The item shouldn't be enabled in the menu. |
285 EXPECT_FALSE(menu.IsCommandIdEnabled(IDC_CONTENT_CONTEXT_VIEWPAGEINFO)); | 331 EXPECT_FALSE(menu.IsCommandIdEnabled(IDC_CONTENT_CONTEXT_VIEWPAGEINFO)); |
286 | 332 |
287 // Ensure that viewing page info doesn't crash even if you can get to it. | 333 // Ensure that viewing page info doesn't crash even if you can get to it. |
288 menu.ExecuteCommand(IDC_CONTENT_CONTEXT_VIEWPAGEINFO, 0); | 334 menu.ExecuteCommand(IDC_CONTENT_CONTEXT_VIEWPAGEINFO, 0); |
289 } | 335 } |
290 | 336 |
291 } // namespace | 337 } // namespace |
OLD | NEW |