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
3 // found in the LICENSE file.
5 #include "base/string_util.h"
6 #include "base/string16.h"
7 #include "content/public/browser/native_web_keyboard_event.h"
8 #include "content/public/test/render_view_test.h"
9 #include "content/renderer/render_view_impl.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 #include <Cocoa/Cocoa.h>
13 #include <Carbon/Carbon.h> // for the kVK_* constants.
17 NSEvent* CmdDeadKeyEvent(NSEventType type, unsigned short code) {
21 uniChar = NSUpArrowFunctionKey;
24 uniChar = NSDownArrowFunctionKey;
29 NSString* s = [NSString stringWithFormat:@"%C", uniChar];
31 return [NSEvent keyEventWithType:type
32 location:NSMakePoint(0, 0)
33 modifierFlags:NSCommandKeyMask
38 charactersIgnoringModifiers:s
43 // Test that cmd-up/down scrolls the page exactly if it is not intercepted by
45 TEST_F(RenderViewTest, MacTestCmdUp) {
46 // Some preprocessor trickery so that we can have literal html in our source,
47 // makes it easier to copy html to and from an html file for testing (the
48 // preprocessor will remove the newlines at the line ends, turning this into
49 // a single long line).
51 const char* kRawHtml = HTML(
54 <script type='text/javascript' language='javascript'>
55 function OnKeyEvent(ev) {
56 var result = document.getElementById(ev.type);
57 result.innerText = (ev.which || ev.keyCode) + ',' +
62 return %s; /* Replace with "return true;" when testing in an html file. */
64 function OnScroll(ev) {
65 var result = document.getElementById("scroll");
66 result.innerText = window.pageYOffset;
70 <style type="text/css">
71 p { border-bottom:5000px solid black; } /* enforce vertical scroll bar */
75 onscroll='return OnScroll(event);'
76 onkeydown='return OnKeyEvent(event);'>
77 <div id='keydown' contenteditable='true'> </div>
78 <div id='scroll' contenteditable='true'> </div>
86 webkit_glue::WebPreferences prefs;
87 prefs.enable_scroll_animator = false;
89 RenderViewImpl* view = static_cast<RenderViewImpl*>(view_);
90 view->OnUpdateWebPreferences(prefs);
92 const int kMaxOutputCharacters = 1024;
94 char htmlBuffer[2048];
96 NSEvent* arrowDownKeyDown = CmdDeadKeyEvent(NSKeyDown, kVK_DownArrow);
97 NSEvent* arrowUpKeyDown = CmdDeadKeyEvent(NSKeyDown, kVK_UpArrow);
99 // First test when javascript does not eat keypresses -- should scroll.
100 sprintf(htmlBuffer, kRawHtml, "true");
101 view->set_send_content_state_immediately(true);
102 LoadHTML(htmlBuffer);
103 render_thread_->sink().ClearMessages();
105 const char* kArrowDownScrollDown =
106 "40,false,false,true,false\n10128\np1\n\np2";
107 view->OnSetEditCommandsForNextKeyEvent(
108 EditCommands(1, EditCommand("moveToEndOfDocument", "")));
109 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowDownKeyDown));
110 ProcessPendingMessages();
111 output = GetMainFrame()->contentAsText(kMaxOutputCharacters);
112 EXPECT_EQ(kArrowDownScrollDown, UTF16ToASCII(output));
114 const char* kArrowUpScrollUp =
115 "38,false,false,true,false\n0\np1\n\np2";
116 view->OnSetEditCommandsForNextKeyEvent(
117 EditCommands(1, EditCommand("moveToBeginningOfDocument", "")));
118 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowUpKeyDown));
119 ProcessPendingMessages();
120 output = GetMainFrame()->contentAsText(kMaxOutputCharacters);
121 EXPECT_EQ(kArrowUpScrollUp, UTF16ToASCII(output));
124 // Now let javascript eat the key events -- no scrolling should happen
125 sprintf(htmlBuffer, kRawHtml, "false");
126 view->set_send_content_state_immediately(true);
127 LoadHTML(htmlBuffer);
128 render_thread_->sink().ClearMessages();
130 const char* kArrowDownNoScroll =
131 "40,false,false,true,false\np1\n\np2";
132 view->OnSetEditCommandsForNextKeyEvent(
133 EditCommands(1, EditCommand("moveToEndOfDocument", "")));
134 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowDownKeyDown));
135 ProcessPendingMessages();
136 output = GetMainFrame()->contentAsText(kMaxOutputCharacters);
137 EXPECT_EQ(kArrowDownNoScroll, UTF16ToASCII(output));
139 const char* kArrowUpNoScroll =
140 "38,false,false,true,false\np1\n\np2";
141 view->OnSetEditCommandsForNextKeyEvent(
142 EditCommands(1, EditCommand("moveToBeginningOfDocument", "")));
143 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowUpKeyDown));
144 ProcessPendingMessages();
145 output = GetMainFrame()->contentAsText(kMaxOutputCharacters);
146 EXPECT_EQ(kArrowUpNoScroll, UTF16ToASCII(output));
149 } // namespace content