Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: chrome/renderer/render_view_unittest_mac.mm

Issue 254002: Fix cmd-up/cmd-down. (Closed)
Patch Set: Fix typo found by suzhe Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2009 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.
4
5 #include "chrome/common/native_web_keyboard_event.h"
6 #include "chrome/common/render_messages.h"
7 #include "chrome/test/render_view_test.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "webkit/api/public/WebString.h"
10
11 #include <Cocoa/Cocoa.h>
12 #include <Carbon/Carbon.h> // for the kVK_* constants.
13
14 NSEvent* CmdDeadKeyEvent(NSEventType type, unsigned short code) {
15
jeremy 2009/09/29 18:12:56 extra newline.
Nico 2009/09/30 03:51:35 Done.
16 UniChar uniChar;
17 switch(code) {
18 case kVK_UpArrow: uniChar = NSUpArrowFunctionKey; break;
jeremy 2009/09/29 18:12:56 http://google-styleguide.googlecode.com/svn/trunk/
Nico 2009/09/30 03:51:35 Surely the styleguide wants to say switch() { ca
19 case kVK_DownArrow: uniChar = NSDownArrowFunctionKey; break;
20 default: CHECK(false);
21 }
22 NSString* s = [NSString stringWithFormat:@"%C", uniChar];
23
24 return [NSEvent keyEventWithType:type
25 location:NSMakePoint(0, 0)
26 modifierFlags:NSCommandKeyMask
27 timestamp:0.0
28 windowNumber:0
29 context:nil
30 characters:s
31 charactersIgnoringModifiers:s
32 isARepeat:NO
33 keyCode:code];
34 }
35
36 // Test that cmd-up/down scrolls the page exactly if it is not intercepted by
37 // javascript.
38 TEST_F(RenderViewTest, MacTestCmdUp) {
39
jeremy 2009/09/29 18:12:56 extra newline
Nico 2009/09/30 03:51:35 Done.
40 // Some preprocessor trickery so that we can have literal html in our source,
41 // makes it easier to copy html to and from an html file for testing (the
42 // preprocessor will remove the newlines at the line ends, turning this into
43 // a single long line).
44 #define HTML(s) #s
45 const char* kRawHtml = HTML(
46 <html>
47 <head><title></title>
48 <script type='text/javascript' language='javascript'>
49 function OnKeyEvent(ev) {
50 var result = document.getElementById(ev.type);
51 result.innerText = (ev.which || ev.keyCode) + ',' +
52 ev.shiftKey + ',' +
53 ev.ctrlKey + ',' +
54 ev.metaKey + ',' +
55 ev.altKey;
56 return %s; /* Replace with "return true;" when testing in an html file. */
57 }
58 function OnScroll(ev) {
59 var result = document.getElementById("scroll");
60 result.innerText = window.pageYOffset;
61 return true;
62 }
63 </script>
64 <style type="text/css">
65 p { border-bottom:5000px solid black; } /* enforce vertical scroll bar */
66 </style>
67 </head>
68 <body
69 onscroll='return OnScroll(event);'
70 onkeydown='return OnKeyEvent(event);'>
71 <div id='keydown' contenteditable='true'> </div>
72 <div id='scroll' contenteditable='true'> </div>
73 <p>p1
74 <p>p2
75 </body>
76 </html>
77 );
78 #undef HTML
79
80 const int kMaxOutputCharacters = 1024;
81 string16 output;
82 char htmlBuffer[2048];
83
84 NSEvent* arrowDownKeyDown = CmdDeadKeyEvent(NSKeyDown, kVK_DownArrow);
85 NSEvent* arrowUpKeyDown = CmdDeadKeyEvent(NSKeyDown, kVK_UpArrow);
86
87 // First test when javascript does not eat keypresses -- should scroll.
88 sprintf(htmlBuffer, kRawHtml, "true");
89 view_->set_delay_seconds_for_form_state_sync(0);
90 LoadHTML(htmlBuffer);
91 render_thread_.sink().ClearMessages();
92
93 const char* kArrowDownScrollDown =
94 "40,false,false,true,false\n1936\np1\n\np2";
jeremy 2009/09/29 18:12:56 Could you provide a helper function that does the
Nico 2009/09/30 03:51:35 Not easily. The helper function would have to go i
95 view_->OnSetEditCommandsForNextKeyEvent(
96 EditCommands(1, EditCommand("moveToEndOfDocument", "")));
97 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowDownKeyDown));
98 output = GetMainFrame()->contentAsText(kMaxOutputCharacters);
99 EXPECT_EQ(kArrowDownScrollDown, UTF16ToASCII(output));
100
101 const char* kArrowUpScrollUp =
102 "38,false,false,true,false\n0\np1\n\np2";
103 view_->OnSetEditCommandsForNextKeyEvent(
104 EditCommands(1, EditCommand("moveToBeginningOfDocument", "")));
105 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowUpKeyDown));
106 output = GetMainFrame()->contentAsText(kMaxOutputCharacters);
107 EXPECT_EQ(kArrowUpScrollUp, UTF16ToASCII(output));
108
109
110 // Now let javascript eat the key events -- no scrolling should happen
111 sprintf(htmlBuffer, kRawHtml, "false");
112 view_->set_delay_seconds_for_form_state_sync(0);
113 LoadHTML(htmlBuffer);
114 render_thread_.sink().ClearMessages();
115
116 const char* kArrowDownNoScroll =
117 "40,false,false,true,false\np1\n\np2";
118 view_->OnSetEditCommandsForNextKeyEvent(
119 EditCommands(1, EditCommand("moveToEndOfDocument", "")));
120 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowDownKeyDown));
121 output = GetMainFrame()->contentAsText(kMaxOutputCharacters);
122 EXPECT_EQ(kArrowDownNoScroll, UTF16ToASCII(output));
123
124 const char* kArrowUpNoScroll =
125 "38,false,false,true,false\np1\n\np2";
126 view_->OnSetEditCommandsForNextKeyEvent(
127 EditCommands(1, EditCommand("moveToBeginningOfDocument", "")));
128 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowUpKeyDown));
129 output = GetMainFrame()->contentAsText(kMaxOutputCharacters);
130 EXPECT_EQ(kArrowUpNoScroll, UTF16ToASCII(output));
131 }
132
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698