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 "core/editing/EditingBehavior.h"
34 #include "core/editing/Editor.h"
35 #include "core/events/EventTarget.h"
36 #include "core/events/KeyboardEvent.h"
37 #include "core/frame/Settings.h"
38 #include "platform/KeyboardCodes.h"
39 #include "public/web/WebInputEvent.h"
40 #include "web/WebInputEventConversion.h"
41 #include <gtest/gtest.h>
45 class KeyboardTest
: public testing::Test
{
48 // Pass a WebKeyboardEvent into the EditorClient and get back the string
49 // name of which editing event that key causes.
50 // E.g., sending in the enter key gives back "InsertNewline".
51 const char* interpretKeyEvent(
52 const WebKeyboardEvent
& webKeyboardEvent
,
53 PlatformEvent::Type keyType
)
55 PlatformKeyboardEventBuilder
evt(webKeyboardEvent
);
56 evt
.setKeyType(keyType
);
57 RefPtrWillBeRawPtr
<KeyboardEvent
> keyboardEvent
= KeyboardEvent::create(evt
, 0);
58 OwnPtr
<Settings
> settings
= Settings::create();
59 EditingBehavior
behavior(settings
->editingBehaviorType());
60 return behavior
.interpretKeyEvent(*keyboardEvent
);
63 // Set up a WebKeyboardEvent KEY_DOWN event with key code and modifiers.
64 void setupKeyDownEvent(WebKeyboardEvent
* keyboardEvent
,
68 keyboardEvent
->windowsKeyCode
= keyCode
;
69 keyboardEvent
->modifiers
= modifiers
;
70 keyboardEvent
->type
= WebInputEvent::KeyDown
;
71 keyboardEvent
->text
[0] = keyCode
;
72 keyboardEvent
->setKeyIdentifierFromWindowsKeyCode();
75 // Like interpretKeyEvent, but with pressing down OSModifier+|keyCode|.
76 // OSModifier is the platform's standard modifier key: control on most
77 // platforms, but meta (command) on Mac.
78 const char* interpretOSModifierKeyPress(char keyCode
)
80 WebKeyboardEvent keyboardEvent
;
82 WebInputEvent::Modifiers osModifier
= WebInputEvent::MetaKey
;
84 WebInputEvent::Modifiers osModifier
= WebInputEvent::ControlKey
;
86 setupKeyDownEvent(&keyboardEvent
, keyCode
, osModifier
);
87 return interpretKeyEvent(keyboardEvent
, PlatformEvent::RawKeyDown
);
90 // Like interpretKeyEvent, but with pressing down ctrl+|keyCode|.
91 const char* interpretCtrlKeyPress(char keyCode
)
93 WebKeyboardEvent keyboardEvent
;
94 setupKeyDownEvent(&keyboardEvent
, keyCode
, WebInputEvent::ControlKey
);
95 return interpretKeyEvent(keyboardEvent
, PlatformEvent::RawKeyDown
);
98 // Like interpretKeyEvent, but with typing a tab.
99 const char* interpretTab(int modifiers
)
101 WebKeyboardEvent keyboardEvent
;
102 setupKeyDownEvent(&keyboardEvent
, '\t', modifiers
);
103 return interpretKeyEvent(keyboardEvent
, PlatformEvent::Char
);
106 // Like interpretKeyEvent, but with typing a newline.
107 const char* interpretNewLine(int modifiers
)
109 WebKeyboardEvent keyboardEvent
;
110 setupKeyDownEvent(&keyboardEvent
, '\r', modifiers
);
111 return interpretKeyEvent(keyboardEvent
, PlatformEvent::Char
);
114 // A name for "no modifiers set".
115 static const int noModifiers
= 0;
118 TEST_F(KeyboardTest
, TestCtrlReturn
)
120 EXPECT_STREQ("InsertNewline", interpretCtrlKeyPress(0xD));
123 TEST_F(KeyboardTest
, TestOSModifierZ
)
126 EXPECT_STREQ("Undo", interpretOSModifierKeyPress('Z'));
130 TEST_F(KeyboardTest
, TestOSModifierY
)
133 EXPECT_STREQ("Redo", interpretOSModifierKeyPress('Y'));
137 TEST_F(KeyboardTest
, TestOSModifierA
)
140 EXPECT_STREQ("SelectAll", interpretOSModifierKeyPress('A'));
144 TEST_F(KeyboardTest
, TestOSModifierX
)
147 EXPECT_STREQ("Cut", interpretOSModifierKeyPress('X'));
151 TEST_F(KeyboardTest
, TestOSModifierC
)
154 EXPECT_STREQ("Copy", interpretOSModifierKeyPress('C'));
158 TEST_F(KeyboardTest
, TestOSModifierV
)
161 EXPECT_STREQ("Paste", interpretOSModifierKeyPress('V'));
165 TEST_F(KeyboardTest
, TestEscape
)
167 WebKeyboardEvent keyboardEvent
;
168 setupKeyDownEvent(&keyboardEvent
, VKEY_ESCAPE
, noModifiers
);
170 const char* result
= interpretKeyEvent(keyboardEvent
,
171 PlatformEvent::RawKeyDown
);
172 EXPECT_STREQ("Cancel", result
);
175 TEST_F(KeyboardTest
, TestInsertTab
)
177 EXPECT_STREQ("InsertTab", interpretTab(noModifiers
));
180 TEST_F(KeyboardTest
, TestInsertBackTab
)
182 EXPECT_STREQ("InsertBacktab", interpretTab(WebInputEvent::ShiftKey
));
185 TEST_F(KeyboardTest
, TestInsertNewline
)
187 EXPECT_STREQ("InsertNewline", interpretNewLine(noModifiers
));
190 TEST_F(KeyboardTest
, TestInsertNewline2
)
192 EXPECT_STREQ("InsertNewline", interpretNewLine(WebInputEvent::ControlKey
));
195 TEST_F(KeyboardTest
, TestInsertLineBreak
)
197 EXPECT_STREQ("InsertLineBreak", interpretNewLine(WebInputEvent::ShiftKey
));
200 TEST_F(KeyboardTest
, TestInsertNewline3
)
202 EXPECT_STREQ("InsertNewline", interpretNewLine(WebInputEvent::AltKey
));
205 TEST_F(KeyboardTest
, TestInsertNewline4
)
207 int modifiers
= WebInputEvent::AltKey
| WebInputEvent::ShiftKey
;
208 const char* result
= interpretNewLine(modifiers
);
209 EXPECT_STREQ("InsertNewline", result
);