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/strings/string16.h"
6 #include "base/strings/string_util.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "content/public/browser/native_web_keyboard_event.h"
9 #include "content/public/test/render_view_test.h"
10 #include "content/renderer/render_view_impl.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "webkit/common/webpreferences.h"
14 #include <Cocoa/Cocoa.h>
15 #include <Carbon/Carbon.h> // for the kVK_* constants.
19 NSEvent* CmdDeadKeyEvent(NSEventType type, unsigned short code) {
23 uniChar = NSUpArrowFunctionKey;
26 uniChar = NSDownArrowFunctionKey;
31 NSString* s = [NSString stringWithFormat:@"%C", uniChar];
33 return [NSEvent keyEventWithType:type
35 modifierFlags:NSCommandKeyMask
40 charactersIgnoringModifiers:s
45 // Test that cmd-up/down scrolls the page exactly if it is not intercepted by
47 TEST_F(RenderViewTest, MacTestCmdUp) {
48 // Some preprocessor trickery so that we can have literal html in our source,
49 // makes it easier to copy html to and from an html file for testing (the
50 // preprocessor will remove the newlines at the line ends, turning this into
51 // a single long line).
53 const char* kRawHtml = HTML(
56 /* Add a vertical scrollbar */
57 body { height: 10128px; }
59 <div id='keydown'></div>
60 <div id='scroll'></div>
62 var allowKeyEvents = true;
63 var scroll = document.getElementById('scroll');
64 var result = document.getElementById('keydown');
65 onkeydown = function(event) {
68 event.shiftKey + ',' +
72 return allowKeyEvents;
76 TODO(esprehn): For some strange reason we need a non-empty document for
77 scrolling to work. This is not the case in a real browser only in the test.
84 prefs.enable_scroll_animator = false;
86 RenderViewImpl* view = static_cast<RenderViewImpl*>(view_);
87 view->OnUpdateWebPreferences(prefs);
89 const int kMaxOutputCharacters = 1024;
90 base::string16 output;
92 NSEvent* arrowDownKeyDown = CmdDeadKeyEvent(NSKeyDown, kVK_DownArrow);
93 NSEvent* arrowUpKeyDown = CmdDeadKeyEvent(NSKeyDown, kVK_UpArrow);
95 // First test when javascript does not eat keypresses -- should scroll.
96 view->set_send_content_state_immediately(true);
98 render_thread_->sink().ClearMessages();
100 const char* kArrowDownScrollDown = "40,false,false,true,false\n10144\np1";
101 view->OnSetEditCommandsForNextKeyEvent(
102 EditCommands(1, EditCommand("moveToEndOfDocument", "")));
103 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowDownKeyDown));
104 ProcessPendingMessages();
105 ExecuteJavaScript("scroll.textContent = window.pageYOffset");
106 output = GetMainFrame()->contentAsText(kMaxOutputCharacters);
107 EXPECT_EQ(kArrowDownScrollDown, base::UTF16ToASCII(output));
109 const char* kArrowUpScrollUp = "38,false,false,true,false\n0\np1";
110 view->OnSetEditCommandsForNextKeyEvent(
111 EditCommands(1, EditCommand("moveToBeginningOfDocument", "")));
112 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowUpKeyDown));
113 ProcessPendingMessages();
114 ExecuteJavaScript("scroll.textContent = window.pageYOffset");
115 output = GetMainFrame()->contentAsText(kMaxOutputCharacters);
116 EXPECT_EQ(kArrowUpScrollUp, base::UTF16ToASCII(output));
118 // Now let javascript eat the key events -- no scrolling should happen.
119 // Set a scroll position slightly down the page to ensure that it does not
121 ExecuteJavaScript("allowKeyEvents = false; window.scrollTo(0, 100)");
123 const char* kArrowDownNoScroll = "40,false,false,true,false\n100\np1";
124 view->OnSetEditCommandsForNextKeyEvent(
125 EditCommands(1, EditCommand("moveToEndOfDocument", "")));
126 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowDownKeyDown));
127 ProcessPendingMessages();
128 ExecuteJavaScript("scroll.textContent = window.pageYOffset");
129 output = GetMainFrame()->contentAsText(kMaxOutputCharacters);
130 EXPECT_EQ(kArrowDownNoScroll, base::UTF16ToASCII(output));
132 const char* kArrowUpNoScroll = "38,false,false,true,false\n100\np1";
133 view->OnSetEditCommandsForNextKeyEvent(
134 EditCommands(1, EditCommand("moveToBeginningOfDocument", "")));
135 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowUpKeyDown));
136 ProcessPendingMessages();
137 ExecuteJavaScript("scroll.textContent = window.pageYOffset");
138 output = GetMainFrame()->contentAsText(kMaxOutputCharacters);
139 EXPECT_EQ(kArrowUpNoScroll, base::UTF16ToASCII(output));
142 } // namespace content