| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "webkit/tools/test_shell/test_webview_delegate.h" | 5 #include "webkit/tools/test_shell/test_webview_delegate.h" |
| 6 | 6 |
| 7 #import <Cocoa/Cocoa.h> | 7 #import <Cocoa/Cocoa.h> |
| 8 #include "base/sys_string_conversions.h" | 8 #include "base/sys_string_conversions.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebCursorInfo.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h" |
| 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 13 #include "webkit/glue/webcursor.h" | 13 #include "webkit/glue/webcursor.h" |
| 14 #include "webkit/glue/webmenurunner_mac.h" | 14 #include "webkit/glue/webmenurunner_mac.h" |
| 15 #include "webkit/plugins/npapi/plugin_list.h" | 15 #include "webkit/plugins/npapi/plugin_list.h" |
| 16 #include "webkit/plugins/npapi/webplugin_delegate_impl.h" | 16 #include "webkit/plugins/npapi/webplugin_delegate_impl.h" |
| 17 #include "webkit/tools/test_shell/test_shell.h" | 17 #include "webkit/tools/test_shell/test_shell.h" |
| 18 | 18 |
| 19 using webkit::npapi::WebPluginDelegateImpl; | 19 using webkit::npapi::WebPluginDelegateImpl; |
| 20 using WebKit::WebCursorInfo; | 20 using WebKit::WebCursorInfo; |
| 21 using WebKit::WebNavigationPolicy; | 21 using WebKit::WebNavigationPolicy; |
| 22 using WebKit::WebPopupMenu; | 22 using WebKit::WebPopupMenu; |
| 23 using WebKit::WebPopupMenuInfo; | 23 using WebKit::WebPopupMenuInfo; |
| 24 using WebKit::WebRect; | 24 using WebKit::WebRect; |
| 25 using WebKit::WebWidget; | 25 using WebKit::WebWidget; |
| 26 | 26 |
| 27 namespace { |
| 28 |
| 29 // Helper function for manufacturing input events to send to WebKit. |
| 30 NSEvent* EventWithMenuAction(BOOL item_chosen, int window_num, |
| 31 int item_height, int selected_index, |
| 32 NSRect menu_bounds, NSRect view_bounds) { |
| 33 NSEvent* event = nil; |
| 34 double event_time = (double)(AbsoluteToDuration(UpTime())) / 1000.0; |
| 35 |
| 36 if (item_chosen) { |
| 37 // Construct a mouse up event to simulate the selection of an appropriate |
| 38 // menu item. |
| 39 NSPoint click_pos; |
| 40 click_pos.x = menu_bounds.size.width / 2; |
| 41 |
| 42 // This is going to be hard to calculate since the button is painted by |
| 43 // WebKit, the menu by Cocoa, and we have to translate the selected_item |
| 44 // index to a coordinate that WebKit's PopupMenu expects which uses a |
| 45 // different font *and* expects to draw the menu below the button like we do |
| 46 // on Windows. |
| 47 // The WebKit popup menu thinks it will draw just below the button, so |
| 48 // create the click at the offset based on the selected item's index and |
| 49 // account for the different coordinate system used by NSView. |
| 50 int item_offset = selected_index * item_height + item_height / 2; |
| 51 click_pos.y = view_bounds.size.height - item_offset; |
| 52 event = [NSEvent mouseEventWithType:NSLeftMouseUp |
| 53 location:click_pos |
| 54 modifierFlags:0 |
| 55 timestamp:event_time |
| 56 windowNumber:window_num |
| 57 context:nil |
| 58 eventNumber:0 |
| 59 clickCount:1 |
| 60 pressure:1.0]; |
| 61 } else { |
| 62 // Fake an ESC key event (keyCode = 0x1B, from webinputevent_mac.mm) and |
| 63 // forward that to WebKit. |
| 64 NSPoint key_pos; |
| 65 key_pos.x = 0; |
| 66 key_pos.y = 0; |
| 67 NSString* escape_str = [NSString stringWithFormat:@"%c", 0x1B]; |
| 68 event = [NSEvent keyEventWithType:NSKeyDown |
| 69 location:key_pos |
| 70 modifierFlags:0 |
| 71 timestamp:event_time |
| 72 windowNumber:window_num |
| 73 context:nil |
| 74 characters:@"" |
| 75 charactersIgnoringModifiers:escape_str |
| 76 isARepeat:NO |
| 77 keyCode:0x1B]; |
| 78 } |
| 79 |
| 80 return event; |
| 81 } |
| 82 |
| 83 } // namespace |
| 84 |
| 27 // WebViewClient -------------------------------------------------------------- | 85 // WebViewClient -------------------------------------------------------------- |
| 28 | 86 |
| 29 WebWidget* TestWebViewDelegate::createPopupMenu( | 87 WebWidget* TestWebViewDelegate::createPopupMenu( |
| 30 const WebPopupMenuInfo& info) { | 88 const WebPopupMenuInfo& info) { |
| 31 WebWidget* webwidget = shell_->CreatePopupWidget(); | 89 WebWidget* webwidget = shell_->CreatePopupWidget(); |
| 32 popup_menu_info_.reset(new WebPopupMenuInfo(info)); | 90 popup_menu_info_.reset(new WebPopupMenuInfo(info)); |
| 33 return webwidget; | 91 return webwidget; |
| 34 } | 92 } |
| 35 | 93 |
| 36 // WebWidgetClient ------------------------------------------------------------ | 94 // WebWidgetClient ------------------------------------------------------------ |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 | 127 |
| 70 [menu_runner runMenuInView:shell_->webViewWnd() | 128 [menu_runner runMenuInView:shell_->webViewWnd() |
| 71 withBounds:position | 129 withBounds:position |
| 72 initialIndex:selected_index]; | 130 initialIndex:selected_index]; |
| 73 | 131 |
| 74 // Get the selected item and forward to WebKit. WebKit expects an input event | 132 // Get the selected item and forward to WebKit. WebKit expects an input event |
| 75 // (mouse down, keyboard activity) for this, so we calculate the proper | 133 // (mouse down, keyboard activity) for this, so we calculate the proper |
| 76 // position based on the selected index and provided bounds. | 134 // position based on the selected index and provided bounds. |
| 77 WebWidgetHost* popup = shell_->popupHost(); | 135 WebWidgetHost* popup = shell_->popupHost(); |
| 78 int window_num = [shell_->mainWnd() windowNumber]; | 136 int window_num = [shell_->mainWnd() windowNumber]; |
| 79 NSEvent* event = | 137 NSEvent* event = EventWithMenuAction([menu_runner menuItemWasChosen], |
| 80 webkit_glue::EventWithMenuAction([menu_runner menuItemWasChosen], | |
| 81 window_num, item_height, | 138 window_num, item_height, |
| 82 [menu_runner indexOfSelectedItem], | 139 [menu_runner indexOfSelectedItem], |
| 83 position, view_rect); | 140 position, view_rect); |
| 84 | 141 |
| 85 if ([menu_runner menuItemWasChosen]) { | 142 if ([menu_runner menuItemWasChosen]) { |
| 86 // Construct a mouse up event to simulate the selection of an appropriate | 143 // Construct a mouse up event to simulate the selection of an appropriate |
| 87 // menu item. | 144 // menu item. |
| 88 popup->MouseEvent(event); | 145 popup->MouseEvent(event); |
| 89 } else { | 146 } else { |
| 90 // Fake an ESC key event (keyCode = 0x1B, from webinputevent_mac.mm) and | 147 // Fake an ESC key event (keyCode = 0x1B, from webinputevent_mac.mm) and |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 void TestWebViewDelegate::SetPageTitle(const string16& title) { | 276 void TestWebViewDelegate::SetPageTitle(const string16& title) { |
| 220 [[shell_->webViewHost()->view_handle() window] | 277 [[shell_->webViewHost()->view_handle() window] |
| 221 setTitle:[NSString stringWithUTF8String:UTF16ToUTF8(title).c_str()]]; | 278 setTitle:[NSString stringWithUTF8String:UTF16ToUTF8(title).c_str()]]; |
| 222 } | 279 } |
| 223 | 280 |
| 224 void TestWebViewDelegate::SetAddressBarURL(const GURL& url) { | 281 void TestWebViewDelegate::SetAddressBarURL(const GURL& url) { |
| 225 const char* frameURL = url.spec().c_str(); | 282 const char* frameURL = url.spec().c_str(); |
| 226 NSString *address = [NSString stringWithUTF8String:frameURL]; | 283 NSString *address = [NSString stringWithUTF8String:frameURL]; |
| 227 [shell_->editWnd() setStringValue:address]; | 284 [shell_->editWnd() setStringValue:address]; |
| 228 } | 285 } |
| OLD | NEW |