diff options
author | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-29 03:56:51 +0000 |
---|---|---|
committer | thakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-09-29 03:56:51 +0000 |
commit | 507b33eab2acbb4e360b5b6eb9f2e666079d0e3b (patch) | |
tree | 6fd3dd864e9da3e68efc7ded0dbaf96b597fcd3c /chrome/renderer/render_view_unittest_mac.mm | |
parent | 529338471fec2ab9b07bac9b07e3477b20e0ae28 (diff) | |
download | chromium_src-507b33eab2acbb4e360b5b6eb9f2e666079d0e3b.zip chromium_src-507b33eab2acbb4e360b5b6eb9f2e666079d0e3b.tar.gz chromium_src-507b33eab2acbb4e360b5b6eb9f2e666079d0e3b.tar.bz2 |
Fix cmd-up/cmd-down.
The approach is to special-case moveToBeginningOfDocument and moveToEndOfDocument in WebFrameImpl::executeCommand(). With this (and the cocoa keyboard bindings patch being landed), the special-case code in WebViewImpl::ScrollViewWithKeyboard() can be removed.
Also add a test for cmd-up/down.
Change chrome.gyp so that it supports osx-only unit tests (_unittest_mac.mm).
Move OnPrintPageAsBitmap to the other printing tests.
BUG=22585
TEST=Go to a page with both text box and scrollbar (e.g. http://en.wikipedia.org ). Pressing cmd-down should scroll to end of page, cmd-up back to start. Clicking the text box and trying some emacs shortcuts should work (ctrl-a jumps to start of line, cmd-h acts as backspace, etc).
Review URL: http://codereview.chromium.org/254002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27464 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer/render_view_unittest_mac.mm')
-rw-r--r-- | chrome/renderer/render_view_unittest_mac.mm | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/chrome/renderer/render_view_unittest_mac.mm b/chrome/renderer/render_view_unittest_mac.mm new file mode 100644 index 0000000..e978626 --- /dev/null +++ b/chrome/renderer/render_view_unittest_mac.mm @@ -0,0 +1,132 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/common/native_web_keyboard_event.h" +#include "chrome/common/render_messages.h" +#include "chrome/test/render_view_test.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "webkit/api/public/WebString.h" + +#include <Cocoa/Cocoa.h> +#include <Carbon/Carbon.h> // for the kVK_* constants. + +NSEvent* CmdDeadKeyEvent(NSEventType type, unsigned short code) { + + UniChar uniChar; + switch(code) { + case kVK_UpArrow: uniChar = NSUpArrowFunctionKey; break; + case kVK_DownArrow: uniChar = NSDownArrowFunctionKey; break; + default: CHECK(false); + } + NSString* s = [NSString stringWithFormat:@"%C", uniChar]; + + return [NSEvent keyEventWithType:type + location:NSMakePoint(0, 0) + modifierFlags:NSCommandKeyMask + timestamp:0.0 + windowNumber:0 + context:nil + characters:s + charactersIgnoringModifiers:s + isARepeat:NO + keyCode:code]; +} + +// Test that cmd-up/down scrolls the page exactly if it is not intercepted by +// javascript. +TEST_F(RenderViewTest, MacTestCmdUp) { + + // Some preprocessor trickery so that we can have literal html in our source, + // makes it easier to copy html to and from an html file for testing (the + // preprocessor will remove the newlines at the line ends, turning this into + // a single long line). + #define HTML(s) #s + const char* kRawHtml = HTML( + <html> + <head><title></title> + <script type='text/javascript' language='javascript'> + function OnKeyEvent(ev) { + var result = document.getElementById(ev.type); + result.innerText = (ev.which || ev.keyCode) + ',' + + ev.shiftKey + ',' + + ev.ctrlKey + ',' + + ev.metaKey + ',' + + ev.altKey; + return %s; /* Replace with "return true;" when testing in an html file. */ + } + function OnScroll(ev) { + var result = document.getElementById("scroll"); + result.innerText = window.pageYOffset; + return true; + } + </script> + <style type="text/css"> + p { border-bottom:5000px solid black; } /* enforce vertical scroll bar */ + </style> + </head> + <body + onscroll='return OnScroll(event);' + onkeydown='return OnKeyEvent(event);'> + <div id='keydown' contenteditable='true'> </div> + <div id='scroll' contenteditable='true'> </div> + <p>p1 + <p>p2 + </body> + </html> + ); + #undef HTML + + const int kMaxOutputCharacters = 1024; + string16 output; + char htmlBuffer[2048]; + + NSEvent* arrowDownKeyDown = CmdDeadKeyEvent(NSKeyDown, kVK_DownArrow); + NSEvent* arrowUpKeyDown = CmdDeadKeyEvent(NSKeyDown, kVK_UpArrow); + + // First test when javascript does not eat keypresses -- should scroll. + sprintf(htmlBuffer, kRawHtml, "true"); + view_->set_delay_seconds_for_form_state_sync(0); + LoadHTML(htmlBuffer); + render_thread_.sink().ClearMessages(); + + const char* kArrowDownScrollDown = + "40,false,false,true,false\n1936\np1\n\np2"; + view_->OnSetEditCommandsForNextKeyEvent( + EditCommands(1, EditCommand("moveToEndOfDocument", ""))); + SendNativeKeyEvent(NativeWebKeyboardEvent(arrowDownKeyDown)); + output = GetMainFrame()->contentAsText(kMaxOutputCharacters); + EXPECT_EQ(kArrowDownScrollDown, UTF16ToASCII(output)); + + const char* kArrowUpScrollUp = + "38,false,false,true,false\n0\np1\n\np2"; + view_->OnSetEditCommandsForNextKeyEvent( + EditCommands(1, EditCommand("moveToBeginningOfDocument", ""))); + SendNativeKeyEvent(NativeWebKeyboardEvent(arrowUpKeyDown)); + output = GetMainFrame()->contentAsText(kMaxOutputCharacters); + EXPECT_EQ(kArrowUpScrollUp, UTF16ToASCII(output)); + + + // Now let javascript eat the key events -- no scrolling should happen + sprintf(htmlBuffer, kRawHtml, "false"); + view_->set_delay_seconds_for_form_state_sync(0); + LoadHTML(htmlBuffer); + render_thread_.sink().ClearMessages(); + + const char* kArrowDownNoScroll = + "40,false,false,true,false\np1\n\np2"; + view_->OnSetEditCommandsForNextKeyEvent( + EditCommands(1, EditCommand("moveToEndOfDocument", ""))); + SendNativeKeyEvent(NativeWebKeyboardEvent(arrowDownKeyDown)); + output = GetMainFrame()->contentAsText(kMaxOutputCharacters); + EXPECT_EQ(kArrowDownNoScroll, UTF16ToASCII(output)); + + const char* kArrowUpNoScroll = + "38,false,false,true,false\np1\n\np2"; + view_->OnSetEditCommandsForNextKeyEvent( + EditCommands(1, EditCommand("moveToBeginningOfDocument", ""))); + SendNativeKeyEvent(NativeWebKeyboardEvent(arrowUpKeyDown)); + output = GetMainFrame()->contentAsText(kMaxOutputCharacters); + EXPECT_EQ(kArrowUpNoScroll, UTF16ToASCII(output)); +} + |