| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "core/editing/EditingBehavior.h" | 31 #include "core/editing/EditingBehavior.h" |
| 32 #include "core/editing/Editor.h" | 32 #include "core/editing/Editor.h" |
| 33 #include "core/events/EventTarget.h" | 33 #include "core/events/EventTarget.h" |
| 34 #include "core/events/KeyboardEvent.h" | 34 #include "core/events/KeyboardEvent.h" |
| 35 #include "core/frame/Settings.h" | 35 #include "core/frame/Settings.h" |
| 36 #include "platform/KeyboardCodes.h" | 36 #include "platform/KeyboardCodes.h" |
| 37 #include "public/web/WebInputEvent.h" | 37 #include "public/platform/Platform.h" |
| 38 #include "public/platform/WebInputEvent.h" |
| 38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
| 39 #include "web/WebInputEventConversion.h" | |
| 40 #include <memory> | 40 #include <memory> |
| 41 | 41 |
| 42 namespace blink { | 42 namespace blink { |
| 43 | 43 |
| 44 class KeyboardTest : public testing::Test { | 44 class KeyboardTest : public testing::Test { |
| 45 public: | 45 public: |
| 46 | 46 // Pass a WebKeyboardEvent into the EditorClient and get back the string |
| 47 // Pass a PlatformKeyboardEvent into the EditorClient and get back the strin
g | |
| 48 // name of which editing event that key causes. | 47 // name of which editing event that key causes. |
| 49 // E.g., sending in the enter key gives back "InsertNewline". | 48 // E.g., sending in the enter key gives back "InsertNewline". |
| 50 const char* interpretKeyEvent(const PlatformKeyboardEvent& platformKeyboardE
vent) | 49 const char* interpretKeyEvent(const WebKeyboardEvent& webKeyboardEvent) |
| 51 { | 50 { |
| 52 KeyboardEvent* keyboardEvent = KeyboardEvent::create(platformKeyboardEve
nt, 0); | 51 KeyboardEvent* keyboardEvent = KeyboardEvent::create(webKeyboardEvent, 0
); |
| 53 std::unique_ptr<Settings> settings = Settings::create(); | 52 std::unique_ptr<Settings> settings = Settings::create(); |
| 54 EditingBehavior behavior(settings->editingBehaviorType()); | 53 EditingBehavior behavior(settings->editingBehaviorType()); |
| 55 return behavior.interpretKeyEvent(*keyboardEvent); | 54 return behavior.interpretKeyEvent(*keyboardEvent); |
| 56 } | 55 } |
| 57 | 56 |
| 58 PlatformKeyboardEvent createFakeKeyboardEvent( | 57 WebKeyboardEvent createFakeKeyboardEvent( |
| 59 char keyCode, | 58 char keyCode, |
| 60 int modifiers, | 59 int modifiers, |
| 61 PlatformEvent::EventType type, | 60 WebInputEvent::Type type, |
| 62 const String& key = emptyString()) | 61 const String& key = emptyString()) |
| 63 { | 62 { |
| 64 String text = emptyString(); | 63 WebKeyboardEvent event; |
| 65 text.append(keyCode); | 64 event.type = type; |
| 66 return PlatformKeyboardEvent( | 65 event.modifiers = modifiers; |
| 67 type, /* EventType */ | 66 event.text[0] = keyCode; |
| 68 text, | 67 event.windowsKeyCode = keyCode; |
| 69 emptyString(), /* unmodifiedText*/ | 68 event.domKey = Platform::current()->domKeyEnumFromString(key); |
| 70 emptyString(), /* DomCode */ | 69 return event; |
| 71 key, /* DomKey */ | |
| 72 keyCode, /* windowsVirtualKeyCode */ | |
| 73 0, /* nativeVirtualKeyCode */ | |
| 74 false, /* isSystemKey */ | |
| 75 static_cast<PlatformEvent::Modifiers>(modifiers), | |
| 76 0 /* timestamp */); | |
| 77 } | 70 } |
| 78 | 71 |
| 79 // Like interpretKeyEvent, but with pressing down OSModifier+|keyCode|. | 72 // Like interpretKeyEvent, but with pressing down OSModifier+|keyCode|. |
| 80 // OSModifier is the platform's standard modifier key: control on most | 73 // OSModifier is the platform's standard modifier key: control on most |
| 81 // platforms, but meta (command) on Mac. | 74 // platforms, but meta (command) on Mac. |
| 82 const char* interpretOSModifierKeyPress(char keyCode) | 75 const char* interpretOSModifierKeyPress(char keyCode) |
| 83 { | 76 { |
| 84 #if OS(MACOSX) | 77 #if OS(MACOSX) |
| 85 WebInputEvent::Modifiers osModifier = WebInputEvent::MetaKey; | 78 WebInputEvent::Modifiers osModifier = WebInputEvent::MetaKey; |
| 86 #else | 79 #else |
| 87 WebInputEvent::Modifiers osModifier = WebInputEvent::ControlKey; | 80 WebInputEvent::Modifiers osModifier = WebInputEvent::ControlKey; |
| 88 #endif | 81 #endif |
| 89 return interpretKeyEvent(createFakeKeyboardEvent(keyCode, osModifier, Pl
atformEvent::RawKeyDown)); | 82 return interpretKeyEvent(createFakeKeyboardEvent(keyCode, osModifier, We
bInputEvent::RawKeyDown)); |
| 90 } | 83 } |
| 91 | 84 |
| 92 // Like interpretKeyEvent, but with pressing down ctrl+|keyCode|. | 85 // Like interpretKeyEvent, but with pressing down ctrl+|keyCode|. |
| 93 const char* interpretCtrlKeyPress(char keyCode) | 86 const char* interpretCtrlKeyPress(char keyCode) |
| 94 { | 87 { |
| 95 return interpretKeyEvent(createFakeKeyboardEvent(keyCode, WebInputEvent:
:ControlKey, PlatformEvent::RawKeyDown)); | 88 return interpretKeyEvent(createFakeKeyboardEvent(keyCode, WebInputEvent:
:ControlKey, WebInputEvent::RawKeyDown)); |
| 96 } | 89 } |
| 97 | 90 |
| 98 // Like interpretKeyEvent, but with typing a tab. | 91 // Like interpretKeyEvent, but with typing a tab. |
| 99 const char* interpretTab(int modifiers) | 92 const char* interpretTab(int modifiers) |
| 100 { | 93 { |
| 101 return interpretKeyEvent(createFakeKeyboardEvent('\t', modifiers, Platfo
rmEvent::Char)); | 94 return interpretKeyEvent(createFakeKeyboardEvent('\t', modifiers, WebInp
utEvent::Char)); |
| 102 } | 95 } |
| 103 | 96 |
| 104 // Like interpretKeyEvent, but with typing a newline. | 97 // Like interpretKeyEvent, but with typing a newline. |
| 105 const char* interpretNewLine(int modifiers) | 98 const char* interpretNewLine(int modifiers) |
| 106 { | 99 { |
| 107 return interpretKeyEvent(createFakeKeyboardEvent('\r', modifiers, Platfo
rmEvent::Char)); | 100 return interpretKeyEvent(createFakeKeyboardEvent('\r', modifiers, WebInp
utEvent::Char)); |
| 108 } | 101 } |
| 109 | 102 |
| 110 const char* interpretDomKey(const char* key) | 103 const char* interpretDomKey(const char* key) |
| 111 { | 104 { |
| 112 return interpretKeyEvent(createFakeKeyboardEvent(0, noModifiers, Platfor
mEvent::RawKeyDown, key)); | 105 return interpretKeyEvent(createFakeKeyboardEvent(0, noModifiers, WebInpu
tEvent::RawKeyDown, key)); |
| 113 } | 106 } |
| 114 | 107 |
| 115 // A name for "no modifiers set". | 108 // A name for "no modifiers set". |
| 116 static const int noModifiers = 0; | 109 static const int noModifiers = 0; |
| 117 }; | 110 }; |
| 118 | 111 |
| 119 TEST_F(KeyboardTest, TestCtrlReturn) | 112 TEST_F(KeyboardTest, TestCtrlReturn) |
| 120 { | 113 { |
| 121 EXPECT_STREQ("InsertNewline", interpretCtrlKeyPress(0xD)); | 114 EXPECT_STREQ("InsertNewline", interpretCtrlKeyPress(0xD)); |
| 122 } | 115 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 | 151 |
| 159 TEST_F(KeyboardTest, TestOSModifierV) | 152 TEST_F(KeyboardTest, TestOSModifierV) |
| 160 { | 153 { |
| 161 #if !OS(MACOSX) | 154 #if !OS(MACOSX) |
| 162 EXPECT_STREQ("Paste", interpretOSModifierKeyPress('V')); | 155 EXPECT_STREQ("Paste", interpretOSModifierKeyPress('V')); |
| 163 #endif | 156 #endif |
| 164 } | 157 } |
| 165 | 158 |
| 166 TEST_F(KeyboardTest, TestEscape) | 159 TEST_F(KeyboardTest, TestEscape) |
| 167 { | 160 { |
| 168 const char* result = interpretKeyEvent(createFakeKeyboardEvent(VKEY_ESCAPE,
noModifiers, PlatformEvent::RawKeyDown)); | 161 const char* result = interpretKeyEvent(createFakeKeyboardEvent(VKEY_ESCAPE,
noModifiers, WebInputEvent::RawKeyDown)); |
| 169 EXPECT_STREQ("Cancel", result); | 162 EXPECT_STREQ("Cancel", result); |
| 170 } | 163 } |
| 171 | 164 |
| 172 TEST_F(KeyboardTest, TestInsertTab) | 165 TEST_F(KeyboardTest, TestInsertTab) |
| 173 { | 166 { |
| 174 EXPECT_STREQ("InsertTab", interpretTab(noModifiers)); | 167 EXPECT_STREQ("InsertTab", interpretTab(noModifiers)); |
| 175 } | 168 } |
| 176 | 169 |
| 177 TEST_F(KeyboardTest, TestInsertBackTab) | 170 TEST_F(KeyboardTest, TestInsertBackTab) |
| 178 { | 171 { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 198 { "Copy", "Copy" }, | 191 { "Copy", "Copy" }, |
| 199 { "Cut", "Cut" }, | 192 { "Cut", "Cut" }, |
| 200 { "Paste", "Paste" }, | 193 { "Paste", "Paste" }, |
| 201 }; | 194 }; |
| 202 | 195 |
| 203 for (const auto& test_case : kDomKeyTestCases) | 196 for (const auto& test_case : kDomKeyTestCases) |
| 204 EXPECT_STREQ(test_case.command, interpretDomKey(test_case.key)); | 197 EXPECT_STREQ(test_case.command, interpretDomKey(test_case.key)); |
| 205 } | 198 } |
| 206 | 199 |
| 207 } // namespace blink | 200 } // namespace blink |
| OLD | NEW |