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 "core/editing/EditingBehavior.h"
36 #include "core/editing/Editor.h"
37 #include "core/events/EventTarget.h"
38 #include "core/events/KeyboardEvent.h"
39 #include "core/frame/Settings.h"
40 #include "platform/KeyboardCodes.h"
41 #include "public/web/WebInputEvent.h"
42 #include "web/WebInputEventConversion.h"
44 using namespace blink;
45 using namespace blink;
49 class KeyboardTest : public testing::Test {
52 // Pass a WebKeyboardEvent into the EditorClient and get back the string
53 // name of which editing event that key causes.
54 // E.g., sending in the enter key gives back "InsertNewline".
55 const char* interpretKeyEvent(
56 const WebKeyboardEvent& webKeyboardEvent,
57 PlatformEvent::Type keyType)
59 PlatformKeyboardEventBuilder evt(webKeyboardEvent);
60 evt.setKeyType(keyType);
61 RefPtrWillBeRawPtr<KeyboardEvent> keyboardEvent = KeyboardEvent::create(evt, 0);
62 OwnPtr<Settings> settings = Settings::create();
63 EditingBehavior behavior(settings->editingBehaviorType());
64 return behavior.interpretKeyEvent(*keyboardEvent);
67 // Set up a WebKeyboardEvent KEY_DOWN event with key code and modifiers.
68 void setupKeyDownEvent(WebKeyboardEvent* keyboardEvent,
72 keyboardEvent->windowsKeyCode = keyCode;
73 keyboardEvent->modifiers = modifiers;
74 keyboardEvent->type = WebInputEvent::KeyDown;
75 keyboardEvent->text[0] = keyCode;
76 keyboardEvent->setKeyIdentifierFromWindowsKeyCode();
79 // Like interpretKeyEvent, but with pressing down OSModifier+|keyCode|.
80 // OSModifier is the platform's standard modifier key: control on most
81 // platforms, but meta (command) on Mac.
82 const char* interpretOSModifierKeyPress(char keyCode)
84 WebKeyboardEvent keyboardEvent;
86 WebInputEvent::Modifiers osModifier = WebInputEvent::MetaKey;
88 WebInputEvent::Modifiers osModifier = WebInputEvent::ControlKey;
90 setupKeyDownEvent(&keyboardEvent, keyCode, osModifier);
91 return interpretKeyEvent(keyboardEvent, PlatformEvent::RawKeyDown);
94 // Like interpretKeyEvent, but with pressing down ctrl+|keyCode|.
95 const char* interpretCtrlKeyPress(char keyCode)
97 WebKeyboardEvent keyboardEvent;
98 setupKeyDownEvent(&keyboardEvent, keyCode, WebInputEvent::ControlKey);
99 return interpretKeyEvent(keyboardEvent, PlatformEvent::RawKeyDown);
102 // Like interpretKeyEvent, but with typing a tab.
103 const char* interpretTab(int modifiers)
105 WebKeyboardEvent keyboardEvent;
106 setupKeyDownEvent(&keyboardEvent, '\t', modifiers);
107 return interpretKeyEvent(keyboardEvent, PlatformEvent::Char);
110 // Like interpretKeyEvent, but with typing a newline.
111 const char* interpretNewLine(int modifiers)
113 WebKeyboardEvent keyboardEvent;
114 setupKeyDownEvent(&keyboardEvent, '\r', modifiers);
115 return interpretKeyEvent(keyboardEvent, PlatformEvent::Char);
118 // A name for "no modifiers set".
119 static const int noModifiers = 0;
122 TEST_F(KeyboardTest, TestCtrlReturn)
124 EXPECT_STREQ("InsertNewline", interpretCtrlKeyPress(0xD));
127 TEST_F(KeyboardTest, TestOSModifierZ)
130 EXPECT_STREQ("Undo", interpretOSModifierKeyPress('Z'));
134 TEST_F(KeyboardTest, TestOSModifierY)
137 EXPECT_STREQ("Redo", interpretOSModifierKeyPress('Y'));
141 TEST_F(KeyboardTest, TestOSModifierA)
144 EXPECT_STREQ("SelectAll", interpretOSModifierKeyPress('A'));
148 TEST_F(KeyboardTest, TestOSModifierX)
151 EXPECT_STREQ("Cut", interpretOSModifierKeyPress('X'));
155 TEST_F(KeyboardTest, TestOSModifierC)
158 EXPECT_STREQ("Copy", interpretOSModifierKeyPress('C'));
162 TEST_F(KeyboardTest, TestOSModifierV)
165 EXPECT_STREQ("Paste", interpretOSModifierKeyPress('V'));
169 TEST_F(KeyboardTest, TestEscape)
171 WebKeyboardEvent keyboardEvent;
172 setupKeyDownEvent(&keyboardEvent, blink::VKEY_ESCAPE, noModifiers);
174 const char* result = interpretKeyEvent(keyboardEvent,
175 PlatformEvent::RawKeyDown);
176 EXPECT_STREQ("Cancel", result);
179 TEST_F(KeyboardTest, TestInsertTab)
181 EXPECT_STREQ("InsertTab", interpretTab(noModifiers));
184 TEST_F(KeyboardTest, TestInsertBackTab)
186 EXPECT_STREQ("InsertBacktab", interpretTab(WebInputEvent::ShiftKey));
189 TEST_F(KeyboardTest, TestInsertNewline)
191 EXPECT_STREQ("InsertNewline", interpretNewLine(noModifiers));
194 TEST_F(KeyboardTest, TestInsertNewline2)
196 EXPECT_STREQ("InsertNewline", interpretNewLine(WebInputEvent::ControlKey));
199 TEST_F(KeyboardTest, TestInsertLineBreak)
201 EXPECT_STREQ("InsertLineBreak", interpretNewLine(WebInputEvent::ShiftKey));
204 TEST_F(KeyboardTest, TestInsertNewline3)
206 EXPECT_STREQ("InsertNewline", interpretNewLine(WebInputEvent::AltKey));
209 TEST_F(KeyboardTest, TestInsertNewline4)
211 int modifiers = WebInputEvent::AltKey | WebInputEvent::ShiftKey;
212 const char* result = interpretNewLine(modifiers);
213 EXPECT_STREQ("InsertNewline", result);