| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/sys_string_conversions.h" | 9 #include "base/sys_string_conversions.h" |
| 10 #include "webkit/api/public/WebCursorInfo.h" | 10 #include "webkit/api/public/WebCursorInfo.h" |
| 11 #include "webkit/glue/webcursor.h" | 11 #include "webkit/glue/webcursor.h" |
| 12 #include "webkit/glue/webview.h" | 12 #include "webkit/glue/webview.h" |
| 13 #include "webkit/glue/plugins/plugin_list.h" | 13 #include "webkit/glue/plugins/plugin_list.h" |
| 14 #include "webkit/glue/plugins/webplugin_delegate_impl.h" | 14 #include "webkit/glue/plugins/webplugin_delegate_impl.h" |
| 15 #include "webkit/glue/webmenurunner_mac.h" | 15 #include "webkit/glue/webmenurunner_mac.h" |
| 16 #include "webkit/tools/test_shell/test_shell.h" | 16 #include "webkit/tools/test_shell/test_shell.h" |
| 17 | 17 |
| 18 using WebKit::WebCursorInfo; | 18 using WebKit::WebCursorInfo; |
| 19 using WebKit::WebNavigationPolicy; |
| 19 using WebKit::WebPopupMenuInfo; | 20 using WebKit::WebPopupMenuInfo; |
| 20 using WebKit::WebRect; | 21 using WebKit::WebRect; |
| 22 using WebKit::WebWidget; |
| 21 | 23 |
| 22 // WebViewDelegate ----------------------------------------------------------- | 24 // WebViewDelegate ----------------------------------------------------------- |
| 23 | 25 |
| 24 TestWebViewDelegate::~TestWebViewDelegate() { | 26 TestWebViewDelegate::~TestWebViewDelegate() { |
| 25 } | 27 } |
| 26 | 28 |
| 27 WebWidget* TestWebViewDelegate::CreatePopupWidgetWithInfo( | 29 WebWidget* TestWebViewDelegate::CreatePopupWidgetWithInfo( |
| 28 WebView* webview, | 30 WebView* webview, |
| 29 const WebPopupMenuInfo& info) { | 31 const WebPopupMenuInfo& info) { |
| 30 WebWidget* webwidget = shell_->CreatePopupWidget(webview); | 32 WebWidget* webwidget = shell_->CreatePopupWidget(webview); |
| 31 popup_menu_info_.reset(new WebPopupMenuInfo(info)); | 33 popup_menu_info_.reset(new WebPopupMenuInfo(info)); |
| 32 return webwidget; | 34 return webwidget; |
| 33 } | 35 } |
| 34 | 36 |
| 35 WebPluginDelegate* TestWebViewDelegate::CreatePluginDelegate( | 37 WebPluginDelegate* TestWebViewDelegate::CreatePluginDelegate( |
| 36 WebView* webview, | 38 WebView* webview, |
| 37 const GURL& url, | 39 const GURL& url, |
| 38 const std::string& mime_type, | 40 const std::string& mime_type, |
| 39 const std::string& clsid, | 41 const std::string& clsid, |
| 40 std::string* actual_mime_type) { | 42 std::string* actual_mime_type) { |
| 41 WebWidgetHost *host = GetHostForWidget(webview); | 43 WebWidgetHost *host = GetWidgetHost(); |
| 42 if (!host) | 44 if (!host) |
| 43 return NULL; | 45 return NULL; |
| 44 gfx::NativeView view = host->view_handle(); | 46 gfx::NativeView view = host->view_handle(); |
| 45 | 47 |
| 46 bool allow_wildcard = true; | 48 bool allow_wildcard = true; |
| 47 WebPluginInfo info; | 49 WebPluginInfo info; |
| 48 if (!NPAPI::PluginList::Singleton()->GetPluginInfo(url, mime_type, clsid, | 50 if (!NPAPI::PluginList::Singleton()->GetPluginInfo(url, mime_type, clsid, |
| 49 allow_wildcard, &info, | 51 allow_wildcard, &info, |
| 50 actual_mime_type)) | 52 actual_mime_type)) |
| 51 return NULL; | 53 return NULL; |
| 52 | 54 |
| 53 if (actual_mime_type && !actual_mime_type->empty()) | 55 if (actual_mime_type && !actual_mime_type->empty()) |
| 54 return WebPluginDelegateImpl::Create(info.path, *actual_mime_type, view); | 56 return WebPluginDelegateImpl::Create(info.path, *actual_mime_type, view); |
| 55 else | 57 else |
| 56 return WebPluginDelegateImpl::Create(info.path, mime_type, view); | 58 return WebPluginDelegateImpl::Create(info.path, mime_type, view); |
| 57 } | 59 } |
| 58 | 60 |
| 61 void TestWebViewDelegate::DidMovePlugin(const WebPluginGeometry& move) { |
| 62 // TODO(port): add me once plugins work. |
| 63 } |
| 64 |
| 59 void TestWebViewDelegate::ShowJavaScriptAlert(const std::wstring& message) { | 65 void TestWebViewDelegate::ShowJavaScriptAlert(const std::wstring& message) { |
| 60 NSString *text = | 66 NSString *text = |
| 61 [NSString stringWithUTF8String:WideToUTF8(message).c_str()]; | 67 [NSString stringWithUTF8String:WideToUTF8(message).c_str()]; |
| 62 NSAlert *alert = [NSAlert alertWithMessageText:@"JavaScript Alert" | 68 NSAlert *alert = [NSAlert alertWithMessageText:@"JavaScript Alert" |
| 63 defaultButton:@"OK" | 69 defaultButton:@"OK" |
| 64 alternateButton:nil | 70 alternateButton:nil |
| 65 otherButton:nil | 71 otherButton:nil |
| 66 informativeTextWithFormat:text]; | 72 informativeTextWithFormat:text]; |
| 67 [alert runModal]; | 73 [alert runModal]; |
| 68 } | 74 } |
| 69 | 75 |
| 70 | 76 |
| 71 // WebWidgetDelegate --------------------------------------------------------- | 77 // WebWidgetDelegate --------------------------------------------------------- |
| 72 | 78 |
| 73 void TestWebViewDelegate::Show(WebWidget* webwidget, | 79 void TestWebViewDelegate::show(WebNavigationPolicy policy) { |
| 74 WindowOpenDisposition disposition) { | |
| 75 if (!popup_menu_info_.get()) | 80 if (!popup_menu_info_.get()) |
| 76 return; | 81 return; |
| 77 if (webwidget != shell_->popup()) | 82 if (this != shell_->popup_delegate()) |
| 78 return; | 83 return; |
| 79 // Display a HTML select menu. | 84 // Display a HTML select menu. |
| 80 | 85 |
| 81 std::vector<WebMenuItem> items; | 86 std::vector<WebMenuItem> items; |
| 82 for (size_t i = 0; i < popup_menu_info_->items.size(); ++i) | 87 for (size_t i = 0; i < popup_menu_info_->items.size(); ++i) |
| 83 items.push_back(popup_menu_info_->items[i]); | 88 items.push_back(popup_menu_info_->items[i]); |
| 84 | 89 |
| 85 int item_height = popup_menu_info_->itemHeight; | 90 int item_height = popup_menu_info_->itemHeight; |
| 86 int selected_index = popup_menu_info_->selectedIndex; | 91 int selected_index = popup_menu_info_->selectedIndex; |
| 87 popup_menu_info_.reset(); // No longer needed. | 92 popup_menu_info_.reset(); // No longer needed. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 118 // Construct a mouse up event to simulate the selection of an appropriate | 123 // Construct a mouse up event to simulate the selection of an appropriate |
| 119 // menu item. | 124 // menu item. |
| 120 popup->MouseEvent(event); | 125 popup->MouseEvent(event); |
| 121 } else { | 126 } else { |
| 122 // Fake an ESC key event (keyCode = 0x1B, from webinputevent_mac.mm) and | 127 // Fake an ESC key event (keyCode = 0x1B, from webinputevent_mac.mm) and |
| 123 // forward that to WebKit. | 128 // forward that to WebKit. |
| 124 popup->KeyEvent(event); | 129 popup->KeyEvent(event); |
| 125 } | 130 } |
| 126 } | 131 } |
| 127 | 132 |
| 128 void TestWebViewDelegate::CloseWidgetSoon(WebWidget* webwidget) { | 133 void TestWebViewDelegate::closeWidgetSoon() { |
| 129 if (webwidget == shell_->webView()) { | 134 if (this == shell_->delegate()) { |
| 130 NSWindow *win = shell_->mainWnd(); | 135 NSWindow *win = shell_->mainWnd(); |
| 131 // Tell Cocoa to close the window, which will let the window's delegate | 136 // Tell Cocoa to close the window, which will let the window's delegate |
| 132 // handle getting rid of the shell. |shell_| will still be alive for a short | 137 // handle getting rid of the shell. |shell_| will still be alive for a short |
| 133 // period of time after this call returns (cleanup is done after going back | 138 // period of time after this call returns (cleanup is done after going back |
| 134 // to the event loop), so we should make sure we don't leave it dangling. | 139 // to the event loop), so we should make sure we don't leave it dangling. |
| 135 [win performClose:nil]; | 140 [win performClose:nil]; |
| 136 shell_ = NULL; | 141 shell_ = NULL; |
| 137 } else if (webwidget == shell_->popup()) { | 142 } else if (this == shell_->popup_delegate()) { |
| 138 shell_->ClosePopup(); | 143 shell_->ClosePopup(); |
| 139 } | 144 } |
| 140 } | 145 } |
| 141 | 146 |
| 142 void TestWebViewDelegate::SetCursor(WebWidget* webwidget, | 147 void TestWebViewDelegate::didChangeCursor(const WebCursorInfo& cursor_info) { |
| 143 const WebCursorInfo& cursor_info) { | |
| 144 NSCursor* ns_cursor = WebCursor(cursor_info).GetCursor(); | 148 NSCursor* ns_cursor = WebCursor(cursor_info).GetCursor(); |
| 145 [ns_cursor set]; | 149 [ns_cursor set]; |
| 146 } | 150 } |
| 147 | 151 |
| 148 void TestWebViewDelegate::GetWindowRect(WebWidget* webwidget, | 152 WebRect TestWebViewDelegate::windowRect() { |
| 149 WebRect* out_rect) { | 153 if (WebWidgetHost* host = GetWidgetHost()) { |
| 150 DCHECK(out_rect); | |
| 151 if (WebWidgetHost* host = GetHostForWidget(webwidget)) { | |
| 152 NSView *view = host->view_handle(); | 154 NSView *view = host->view_handle(); |
| 153 NSRect rect = [view frame]; | 155 NSRect rect = [view frame]; |
| 154 *out_rect = gfx::Rect(NSRectToCGRect(rect)); | 156 return gfx::Rect(NSRectToCGRect(rect)); |
| 155 } | 157 } |
| 158 return WebRect(); |
| 156 } | 159 } |
| 157 | 160 |
| 158 void TestWebViewDelegate::SetWindowRect(WebWidget* webwidget, | 161 void TestWebViewDelegate::setWindowRect(const WebRect& rect) { |
| 159 const WebRect& rect) { | |
| 160 // TODO: Mac window movement | 162 // TODO: Mac window movement |
| 161 if (webwidget == shell_->webView()) { | 163 if (this == shell_->delegate()) { |
| 162 // ignored | 164 // ignored |
| 163 } else if (webwidget == shell_->popup()) { | 165 } else if (this == shell_->popup_delegate()) { |
| 164 popup_bounds_ = rect; // The initial position of the popup. | 166 popup_bounds_ = rect; // The initial position of the popup. |
| 165 } | 167 } |
| 166 } | 168 } |
| 167 | 169 |
| 168 void TestWebViewDelegate::GetRootWindowRect(WebWidget* webwidget, | 170 WebRect TestWebViewDelegate::rootWindowRect() { |
| 169 WebRect* out_rect) { | 171 if (WebWidgetHost* host = GetWidgetHost()) { |
| 170 if (WebWidgetHost* host = GetHostForWidget(webwidget)) { | |
| 171 NSView *view = host->view_handle(); | 172 NSView *view = host->view_handle(); |
| 172 NSRect rect = [[[view window] contentView] frame]; | 173 NSRect rect = [[[view window] contentView] frame]; |
| 173 *out_rect = gfx::Rect(NSRectToCGRect(rect)); | 174 return gfx::Rect(NSRectToCGRect(rect)); |
| 174 } | 175 } |
| 176 return WebRect(); |
| 175 } | 177 } |
| 176 | 178 |
| 177 @interface NSWindow(OSInternals) | 179 @interface NSWindow(OSInternals) |
| 178 - (NSRect)_growBoxRect; | 180 - (NSRect)_growBoxRect; |
| 179 @end | 181 @end |
| 180 | 182 |
| 181 void TestWebViewDelegate::GetRootWindowResizerRect(WebWidget* webwidget, | 183 WebRect TestWebViewDelegate::windowResizerRect() { |
| 182 WebRect* out_rect) { | |
| 183 NSRect resize_rect = NSMakeRect(0, 0, 0, 0); | 184 NSRect resize_rect = NSMakeRect(0, 0, 0, 0); |
| 184 WebWidgetHost* host = GetHostForWidget(webwidget); | 185 WebWidgetHost* host = GetWidgetHost(); |
| 185 // To match the WebKit screen shots, we need the resize area to overlap | 186 // To match the WebKit screen shots, we need the resize area to overlap |
| 186 // the scroll arrows, so in layout test mode, we don't return a real rect. | 187 // the scroll arrows, so in layout test mode, we don't return a real rect. |
| 187 if (!(shell_->layout_test_mode()) && host) { | 188 if (!(shell_->layout_test_mode()) && host) { |
| 188 NSView *view = host->view_handle(); | 189 NSView *view = host->view_handle(); |
| 189 NSWindow* window = [view window]; | 190 NSWindow* window = [view window]; |
| 190 resize_rect = [window _growBoxRect]; | 191 resize_rect = [window _growBoxRect]; |
| 191 // The scrollbar assumes that the resizer goes all the way down to the | 192 // The scrollbar assumes that the resizer goes all the way down to the |
| 192 // bottom corner, so we ignore any y offset to the rect itself and use the | 193 // bottom corner, so we ignore any y offset to the rect itself and use the |
| 193 // entire bottom corner. | 194 // entire bottom corner. |
| 194 resize_rect.origin.y = 0; | 195 resize_rect.origin.y = 0; |
| 195 // Convert to view coordinates from window coordinates. | 196 // Convert to view coordinates from window coordinates. |
| 196 resize_rect = [view convertRect:resize_rect fromView:nil]; | 197 resize_rect = [view convertRect:resize_rect fromView:nil]; |
| 197 // Flip the rect in view coordinates | 198 // Flip the rect in view coordinates |
| 198 resize_rect.origin.y = | 199 resize_rect.origin.y = |
| 199 [view frame].size.height - resize_rect.origin.y - | 200 [view frame].size.height - resize_rect.origin.y - |
| 200 resize_rect.size.height; | 201 resize_rect.size.height; |
| 201 } | 202 } |
| 202 *out_rect = gfx::Rect(NSRectToCGRect(resize_rect)); | 203 return gfx::Rect(NSRectToCGRect(resize_rect)); |
| 203 } | 204 } |
| 204 | 205 |
| 205 void TestWebViewDelegate::DidMove(WebWidget* webwidget, | 206 void TestWebViewDelegate::runModal() { |
| 206 const WebPluginGeometry& move) { | |
| 207 // TODO(port): add me once plugins work. | |
| 208 } | |
| 209 | |
| 210 void TestWebViewDelegate::RunModal(WebWidget* webwidget) { | |
| 211 NOTIMPLEMENTED(); | 207 NOTIMPLEMENTED(); |
| 212 } | 208 } |
| 213 | 209 |
| 214 void TestWebViewDelegate::UpdateSelectionClipboard(bool is_empty_selection) { | 210 void TestWebViewDelegate::UpdateSelectionClipboard(bool is_empty_selection) { |
| 215 // No selection clipboard on mac, do nothing. | 211 // No selection clipboard on mac, do nothing. |
| 216 } | 212 } |
| 217 | 213 |
| 218 // Private methods ----------------------------------------------------------- | 214 // Private methods ----------------------------------------------------------- |
| 219 | 215 |
| 220 void TestWebViewDelegate::SetPageTitle(const std::wstring& title) { | 216 void TestWebViewDelegate::SetPageTitle(const std::wstring& title) { |
| 221 [[shell_->webViewHost()->view_handle() window] | 217 [[shell_->webViewHost()->view_handle() window] |
| 222 setTitle:[NSString stringWithUTF8String:WideToUTF8(title).c_str()]]; | 218 setTitle:[NSString stringWithUTF8String:WideToUTF8(title).c_str()]]; |
| 223 } | 219 } |
| 224 | 220 |
| 225 void TestWebViewDelegate::SetAddressBarURL(const GURL& url) { | 221 void TestWebViewDelegate::SetAddressBarURL(const GURL& url) { |
| 226 const char* frameURL = url.spec().c_str(); | 222 const char* frameURL = url.spec().c_str(); |
| 227 NSString *address = [NSString stringWithUTF8String:frameURL]; | 223 NSString *address = [NSString stringWithUTF8String:frameURL]; |
| 228 [shell_->editWnd() setStringValue:address]; | 224 [shell_->editWnd() setStringValue:address]; |
| 229 } | 225 } |
| OLD | NEW |