2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <gtest/gtest.h>
35 #include "EditorClientImpl.h"
36 #include "EventTarget.h"
37 #include "KeyboardCodes.h"
38 #include "KeyboardEvent.h"
39 #include "WebInputEvent.h"
40 #include "WebInputEventConversion.h"
42 using namespace WebCore;
43 using namespace WebKit;
47 class KeyboardTest : public testing::Test {
50 // Pass a WebKeyboardEvent into the EditorClient and get back the string
51 // name of which editing event that key causes.
52 // E.g., sending in the enter key gives back "InsertNewline".
53 const char* interpretKeyEvent(
54 const WebKeyboardEvent& webKeyboardEvent,
55 PlatformKeyboardEvent::Type keyType)
57 EditorClientImpl editorImpl(0);
58 PlatformKeyboardEventBuilder evt(webKeyboardEvent);
59 evt.setKeyType(keyType);
60 RefPtr<KeyboardEvent> keyboardEvent = KeyboardEvent::create(evt, 0);
61 return editorImpl.interpretKeyEvent(keyboardEvent.get());
64 // Set up a WebKeyboardEvent KEY_DOWN event with key code and modifiers.
65 void setupKeyDownEvent(WebKeyboardEvent* keyboardEvent,
69 keyboardEvent->windowsKeyCode = keyCode;
70 keyboardEvent->modifiers = modifiers;
71 keyboardEvent->type = WebInputEvent::KeyDown;
72 keyboardEvent->text[0] = keyCode;
73 keyboardEvent->setKeyIdentifierFromWindowsKeyCode();
76 // Like interpretKeyEvent, but with pressing down OSModifier+|keyCode|.
77 // OSModifier is the platform's standard modifier key: control on most
78 // platforms, but meta (command) on Mac.
79 const char* interpretOSModifierKeyPress(char keyCode)
81 WebKeyboardEvent keyboardEvent;
83 WebInputEvent::Modifiers osModifier = WebInputEvent::MetaKey;
85 WebInputEvent::Modifiers osModifier = WebInputEvent::ControlKey;
87 setupKeyDownEvent(&keyboardEvent, keyCode, osModifier);
88 return interpretKeyEvent(keyboardEvent, PlatformKeyboardEvent::RawKeyDown);
91 // Like interpretKeyEvent, but with pressing down ctrl+|keyCode|.
92 const char* interpretCtrlKeyPress(char keyCode)
94 WebKeyboardEvent keyboardEvent;
95 setupKeyDownEvent(&keyboardEvent, keyCode, WebInputEvent::ControlKey);
96 return interpretKeyEvent(keyboardEvent, PlatformKeyboardEvent::RawKeyDown);
99 // Like interpretKeyEvent, but with typing a tab.
100 const char* interpretTab(int modifiers)
102 WebKeyboardEvent keyboardEvent;
103 setupKeyDownEvent(&keyboardEvent, '\t', modifiers);
104 return interpretKeyEvent(keyboardEvent, PlatformKeyboardEvent::Char);
107 // Like interpretKeyEvent, but with typing a newline.
108 const char* interpretNewLine(int modifiers)
110 WebKeyboardEvent keyboardEvent;
111 setupKeyDownEvent(&keyboardEvent, '\r', modifiers);
112 return interpretKeyEvent(keyboardEvent, PlatformKeyboardEvent::Char);
115 // A name for "no modifiers set".
116 static const int noModifiers = 0;
119 TEST_F(KeyboardTest, TestCtrlReturn)
121 EXPECT_STREQ("InsertNewline", interpretCtrlKeyPress(0xD));
124 TEST_F(KeyboardTest, TestOSModifierZ)
127 EXPECT_STREQ("Undo", interpretOSModifierKeyPress('Z'));
131 TEST_F(KeyboardTest, TestOSModifierY)
134 EXPECT_STREQ("Redo", interpretOSModifierKeyPress('Y'));
138 TEST_F(KeyboardTest, TestOSModifierA)
141 EXPECT_STREQ("SelectAll", interpretOSModifierKeyPress('A'));
145 TEST_F(KeyboardTest, TestOSModifierX)
148 EXPECT_STREQ("Cut", interpretOSModifierKeyPress('X'));
152 TEST_F(KeyboardTest, TestOSModifierC)
155 EXPECT_STREQ("Copy", interpretOSModifierKeyPress('C'));
159 TEST_F(KeyboardTest, TestOSModifierV)
162 EXPECT_STREQ("Paste", interpretOSModifierKeyPress('V'));
166 TEST_F(KeyboardTest, TestEscape)
168 WebKeyboardEvent keyboardEvent;
169 setupKeyDownEvent(&keyboardEvent, WebCore::VKEY_ESCAPE, noModifiers);
171 const char* result = interpretKeyEvent(keyboardEvent,
172 PlatformKeyboardEvent::RawKeyDown);
173 EXPECT_STREQ("Cancel", result);
176 TEST_F(KeyboardTest, TestInsertTab)
178 EXPECT_STREQ("InsertTab", interpretTab(noModifiers));
181 TEST_F(KeyboardTest, TestInsertBackTab)
183 EXPECT_STREQ("InsertBacktab", interpretTab(WebInputEvent::ShiftKey));
186 TEST_F(KeyboardTest, TestInsertNewline)
188 EXPECT_STREQ("InsertNewline", interpretNewLine(noModifiers));
191 TEST_F(KeyboardTest, TestInsertNewline2)
193 EXPECT_STREQ("InsertNewline", interpretNewLine(WebInputEvent::ControlKey));
196 TEST_F(KeyboardTest, TestInsertLineBreak)
198 EXPECT_STREQ("InsertLineBreak", interpretNewLine(WebInputEvent::ShiftKey));
201 TEST_F(KeyboardTest, TestInsertNewline3)
203 EXPECT_STREQ("InsertNewline", interpretNewLine(WebInputEvent::AltKey));
206 TEST_F(KeyboardTest, TestInsertNewline4)
208 int modifiers = WebInputEvent::AltKey | WebInputEvent::ShiftKey;
209 const char* result = interpretNewLine(modifiers);
210 EXPECT_STREQ("InsertNewline", result);