OLD | NEW |
| (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 "base/string16.h" | |
6 #include "chrome/common/native_web_keyboard_event.h" | |
7 #include "chrome/common/render_messages.h" | |
8 #include "chrome/test/render_view_test.h" | |
9 #include "testing/gtest/include/gtest/gtest.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 UniChar uniChar = 0; | |
16 switch(code) { | |
17 case kVK_UpArrow: | |
18 uniChar = NSUpArrowFunctionKey; | |
19 break; | |
20 case kVK_DownArrow: | |
21 uniChar = NSDownArrowFunctionKey; | |
22 break; | |
23 default: | |
24 CHECK(false); | |
25 } | |
26 NSString* s = [NSString stringWithFormat:@"%C", uniChar]; | |
27 | |
28 return [NSEvent keyEventWithType:type | |
29 location:NSMakePoint(0, 0) | |
30 modifierFlags:NSCommandKeyMask | |
31 timestamp:0.0 | |
32 windowNumber:0 | |
33 context:nil | |
34 characters:s | |
35 charactersIgnoringModifiers:s | |
36 isARepeat:NO | |
37 keyCode:code]; | |
38 } | |
39 | |
40 // Test that cmd-up/down scrolls the page exactly if it is not intercepted by | |
41 // javascript. | |
42 TEST_F(RenderViewTest, MacTestCmdUp) { | |
43 // Some preprocessor trickery so that we can have literal html in our source, | |
44 // makes it easier to copy html to and from an html file for testing (the | |
45 // preprocessor will remove the newlines at the line ends, turning this into | |
46 // a single long line). | |
47 #define HTML(s) #s | |
48 const char* kRawHtml = HTML( | |
49 <html> | |
50 <head><title></title> | |
51 <script type='text/javascript' language='javascript'> | |
52 function OnKeyEvent(ev) { | |
53 var result = document.getElementById(ev.type); | |
54 result.innerText = (ev.which || ev.keyCode) + ',' + | |
55 ev.shiftKey + ',' + | |
56 ev.ctrlKey + ',' + | |
57 ev.metaKey + ',' + | |
58 ev.altKey; | |
59 return %s; /* Replace with "return true;" when testing in an html file. */ | |
60 } | |
61 function OnScroll(ev) { | |
62 var result = document.getElementById("scroll"); | |
63 result.innerText = window.pageYOffset; | |
64 return true; | |
65 } | |
66 </script> | |
67 <style type="text/css"> | |
68 p { border-bottom:5000px solid black; } /* enforce vertical scroll bar */ | |
69 </style> | |
70 </head> | |
71 <body | |
72 onscroll='return OnScroll(event);' | |
73 onkeydown='return OnKeyEvent(event);'> | |
74 <div id='keydown' contenteditable='true'> </div> | |
75 <div id='scroll' contenteditable='true'> </div> | |
76 <p>p1 | |
77 <p>p2 | |
78 </body> | |
79 </html> | |
80 ); | |
81 #undef HTML | |
82 | |
83 const int kMaxOutputCharacters = 1024; | |
84 string16 output; | |
85 char htmlBuffer[2048]; | |
86 | |
87 NSEvent* arrowDownKeyDown = CmdDeadKeyEvent(NSKeyDown, kVK_DownArrow); | |
88 NSEvent* arrowUpKeyDown = CmdDeadKeyEvent(NSKeyDown, kVK_UpArrow); | |
89 | |
90 // First test when javascript does not eat keypresses -- should scroll. | |
91 sprintf(htmlBuffer, kRawHtml, "true"); | |
92 view_->set_send_content_state_immediately(true); | |
93 LoadHTML(htmlBuffer); | |
94 render_thread_.sink().ClearMessages(); | |
95 | |
96 const char* kArrowDownScrollDown = | |
97 "40,false,false,true,false\n1936\np1\n\np2"; | |
98 view_->OnSetEditCommandsForNextKeyEvent( | |
99 EditCommands(1, EditCommand("moveToEndOfDocument", ""))); | |
100 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowDownKeyDown)); | |
101 output = GetMainFrame()->contentAsText(kMaxOutputCharacters); | |
102 EXPECT_EQ(kArrowDownScrollDown, UTF16ToASCII(output)); | |
103 | |
104 const char* kArrowUpScrollUp = | |
105 "38,false,false,true,false\n0\np1\n\np2"; | |
106 view_->OnSetEditCommandsForNextKeyEvent( | |
107 EditCommands(1, EditCommand("moveToBeginningOfDocument", ""))); | |
108 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowUpKeyDown)); | |
109 output = GetMainFrame()->contentAsText(kMaxOutputCharacters); | |
110 EXPECT_EQ(kArrowUpScrollUp, UTF16ToASCII(output)); | |
111 | |
112 | |
113 // Now let javascript eat the key events -- no scrolling should happen | |
114 sprintf(htmlBuffer, kRawHtml, "false"); | |
115 view_->set_send_content_state_immediately(true); | |
116 LoadHTML(htmlBuffer); | |
117 render_thread_.sink().ClearMessages(); | |
118 | |
119 const char* kArrowDownNoScroll = | |
120 "40,false,false,true,false\np1\n\np2"; | |
121 view_->OnSetEditCommandsForNextKeyEvent( | |
122 EditCommands(1, EditCommand("moveToEndOfDocument", ""))); | |
123 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowDownKeyDown)); | |
124 output = GetMainFrame()->contentAsText(kMaxOutputCharacters); | |
125 EXPECT_EQ(kArrowDownNoScroll, UTF16ToASCII(output)); | |
126 | |
127 const char* kArrowUpNoScroll = | |
128 "38,false,false,true,false\np1\n\np2"; | |
129 view_->OnSetEditCommandsForNextKeyEvent( | |
130 EditCommands(1, EditCommand("moveToBeginningOfDocument", ""))); | |
131 SendNativeKeyEvent(NativeWebKeyboardEvent(arrowUpKeyDown)); | |
132 output = GetMainFrame()->contentAsText(kMaxOutputCharacters); | |
133 EXPECT_EQ(kArrowUpNoScroll, UTF16ToASCII(output)); | |
134 } | |
135 | |
OLD | NEW |