Fix build break
[chromium-blink-merge.git] / content / renderer / render_view_browsertest_mac.mm
blobed9908a6c3e6bf43a3efc2365aac0259b341664b
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.
15 namespace content {
17 NSEvent* CmdDeadKeyEvent(NSEventType type, unsigned short code) {
18   UniChar uniChar = 0;
19   switch(code) {
20     case kVK_UpArrow:
21       uniChar = NSUpArrowFunctionKey;
22       break;
23     case kVK_DownArrow:
24       uniChar = NSDownArrowFunctionKey;
25       break;
26     default:
27       CHECK(false);
28   }
29   NSString* s = [NSString stringWithFormat:@"%C", uniChar];
31   return [NSEvent keyEventWithType:type
32                           location:NSMakePoint(0, 0)
33                      modifierFlags:NSCommandKeyMask
34                          timestamp:0.0
35                       windowNumber:0
36                            context:nil
37                         characters:s
38        charactersIgnoringModifiers:s
39                          isARepeat:NO
40                            keyCode:code];
43 // Test that cmd-up/down scrolls the page exactly if it is not intercepted by
44 // javascript.
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).
50   #define HTML(s) #s
51   const char* kRawHtml = HTML(
52   <html>
53   <head><title></title>
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) + ',' +
58       ev.shiftKey + ',' +
59       ev.ctrlKey + ',' +
60       ev.metaKey + ',' +
61       ev.altKey;
62     return %s;  /* Replace with "return true;" when testing in an html file. */
63   }
64   function OnScroll(ev) {
65     var result = document.getElementById("scroll");
66     result.innerText = window.pageYOffset;
67     return true;
68   }
69   </script>
70   <style type="text/css">
71   p { border-bottom:5000px solid black; } /* enforce vertical scroll bar */
72   </style>
73   </head>
74   <body
75     onscroll='return OnScroll(event);'
76     onkeydown='return OnKeyEvent(event);'>
77   <div id='keydown' contenteditable='true'> </div>
78   <div id='scroll' contenteditable='true'> </div>
79   <p>p1
80   <p>p2
81   </body>
82   </html>
83   );
84   #undef HTML
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;
93   string16 output;
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