| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/renderer_host/gtk_key_bindings_handler.h" | 5 #include "chrome/browser/ui/libgtk2ui/gtk2_key_bindings_handler.h" |
| 6 | 6 |
| 7 #include <gdk/gdkkeysyms.h> | 7 #include <gdk/gdkkeysyms.h> |
| 8 |
| 8 #include <string> | 9 #include <string> |
| 9 #include <utility> | 10 #include <utility> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 13 #include "base/base_paths.h" |
| 12 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
| 13 #include "base/file_util.h" | 15 #include "base/file_util.h" |
| 14 #include "base/logging.h" | 16 #include "base/logging.h" |
| 15 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 16 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 17 #include "content/common/edit_command.h" | 19 #include "chrome/browser/ui/libgtk2ui/gtk2_key_bindings_handler_unittest_helper.
h" |
| 18 #include "content/public/browser/native_web_keyboard_event.h" | 20 #include "content/public/browser/native_web_keyboard_event.h" |
| 19 #include "content/public/common/content_paths.h" | 21 #include "content/public/common/content_paths.h" |
| 22 #include "content/public/common/edit_command.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "ui/events/event.h" |
| 21 | 25 |
| 22 namespace content { | 26 namespace libgtk2ui { |
| 23 | 27 |
| 24 class GtkKeyBindingsHandlerTest : public testing::Test { | 28 class Gtk2KeyBindingsHandlerTest : public testing::Test { |
| 25 protected: | 29 protected: |
| 26 struct EditCommand { | 30 struct EditCommand { |
| 27 const char* name; | 31 const char* name; |
| 28 const char* value; | 32 const char* value; |
| 29 }; | 33 }; |
| 30 | 34 |
| 31 GtkKeyBindingsHandlerTest() | 35 Gtk2KeyBindingsHandlerTest() |
| 32 : window_(gtk_window_new(GTK_WINDOW_TOPLEVEL)), | 36 : handler_(NULL) { |
| 33 handler_(NULL) { | |
| 34 base::FilePath gtkrc; | 37 base::FilePath gtkrc; |
| 35 PathService::Get(DIR_TEST_DATA, >krc); | 38 PathService::Get(base::DIR_TEST_DATA, >krc); |
| 36 gtkrc = gtkrc.AppendASCII("gtk_key_bindings_test_gtkrc"); | 39 gtkrc = gtkrc.AppendASCII("gtk_key_bindings_test_gtkrc"); |
| 37 EXPECT_TRUE(base::PathExists(gtkrc)); | 40 EXPECT_TRUE(base::PathExists(gtkrc)); |
| 38 | 41 |
| 39 gtk_rc_parse(gtkrc.value().c_str()); | 42 gtk_rc_parse(gtkrc.value().c_str()); |
| 40 | 43 |
| 41 GtkWidget* fixed = gtk_fixed_new(); | 44 handler_ = new Gtk2KeyBindingsHandler(); |
| 42 handler_ = new GtkKeyBindingsHandler(fixed); | |
| 43 gtk_container_add(GTK_CONTAINER(window_), fixed); | |
| 44 gtk_widget_show(fixed); | |
| 45 gtk_widget_show(window_); | |
| 46 } | 45 } |
| 47 virtual ~GtkKeyBindingsHandlerTest() { | 46 virtual ~Gtk2KeyBindingsHandlerTest() { |
| 48 gtk_widget_destroy(window_); | |
| 49 delete handler_; | 47 delete handler_; |
| 50 } | 48 } |
| 51 | 49 |
| 52 NativeWebKeyboardEvent NewNativeWebKeyboardEvent(guint keyval, guint state) { | 50 void TestKeyBinding(const content::NativeWebKeyboardEvent& event, |
| 53 GdkKeymap* keymap = | |
| 54 gdk_keymap_get_for_display(gtk_widget_get_display(window_)); | |
| 55 | |
| 56 GdkKeymapKey *keys = NULL; | |
| 57 gint n_keys = 0; | |
| 58 if (gdk_keymap_get_entries_for_keyval(keymap, keyval, &keys, &n_keys)) { | |
| 59 GdkEventKey event; | |
| 60 event.type = GDK_KEY_PRESS; | |
| 61 event.window = NULL; | |
| 62 event.send_event = 0; | |
| 63 event.time = 0; | |
| 64 event.state = state; | |
| 65 event.keyval = keyval; | |
| 66 event.length = 0; | |
| 67 event.string = NULL; | |
| 68 event.hardware_keycode = keys[0].keycode; | |
| 69 event.group = keys[0].group; | |
| 70 event.is_modifier = 0; | |
| 71 g_free(keys); | |
| 72 return NativeWebKeyboardEvent(reinterpret_cast<GdkEvent*>(&event)); | |
| 73 } | |
| 74 LOG(ERROR) << "Failed to create key event for keyval:" << keyval; | |
| 75 return NativeWebKeyboardEvent(); | |
| 76 } | |
| 77 | |
| 78 void TestKeyBinding(const NativeWebKeyboardEvent& event, | |
| 79 const EditCommand expected_result[], | 51 const EditCommand expected_result[], |
| 80 size_t size) { | 52 size_t size) { |
| 81 EditCommands result; | 53 content::EditCommands result; |
| 82 ASSERT_TRUE(handler_->Match(event, &result)); | 54 ASSERT_TRUE(handler_->Match(event, &result)); |
| 83 ASSERT_EQ(size, result.size()); | 55 ASSERT_EQ(size, result.size()); |
| 84 for (size_t i = 0; i < size; ++i) { | 56 for (size_t i = 0; i < size; ++i) { |
| 85 ASSERT_STREQ(expected_result[i].name, result[i].name.c_str()); | 57 ASSERT_STREQ(expected_result[i].name, result[i].name.c_str()); |
| 86 ASSERT_STREQ(expected_result[i].value, result[i].value.c_str()); | 58 ASSERT_STREQ(expected_result[i].value, result[i].value.c_str()); |
| 87 } | 59 } |
| 88 } | 60 } |
| 89 | 61 |
| 90 protected: | 62 protected: |
| 91 GtkWidget* window_; | 63 Gtk2KeyBindingsHandler* handler_; |
| 92 GtkKeyBindingsHandler* handler_; | |
| 93 }; | 64 }; |
| 94 | 65 |
| 95 TEST_F(GtkKeyBindingsHandlerTest, MoveCursor) { | 66 TEST_F(Gtk2KeyBindingsHandlerTest, MoveCursor) { |
| 96 static const EditCommand kEditCommands[] = { | 67 static const EditCommand kEditCommands[] = { |
| 97 // "move-cursor" (logical-positions, -2, 0) | 68 // "move-cursor" (logical-positions, -2, 0) |
| 98 { "MoveBackward", "" }, | 69 { "MoveBackward", "" }, |
| 99 { "MoveBackward", "" }, | 70 { "MoveBackward", "" }, |
| 100 // "move-cursor" (logical-positions, 2, 0) | 71 // "move-cursor" (logical-positions, 2, 0) |
| 101 { "MoveForward", "" }, | 72 { "MoveForward", "" }, |
| 102 { "MoveForward", "" }, | 73 { "MoveForward", "" }, |
| 103 // "move-cursor" (visual-positions, -1, 1) | 74 // "move-cursor" (visual-positions, -1, 1) |
| 104 { "MoveLeftAndModifySelection", "" }, | 75 { "MoveLeftAndModifySelection", "" }, |
| 105 // "move-cursor" (visual-positions, 1, 1) | 76 // "move-cursor" (visual-positions, 1, 1) |
| (...skipping 17 matching lines...) Expand all Loading... |
| 123 // "move-cursor" (pages, -1, 0) | 94 // "move-cursor" (pages, -1, 0) |
| 124 { "MovePageUp", "" }, | 95 { "MovePageUp", "" }, |
| 125 // "move-cursor" (pages, 1, 0) | 96 // "move-cursor" (pages, 1, 0) |
| 126 { "MovePageDown", "" }, | 97 { "MovePageDown", "" }, |
| 127 // "move-cursor" (buffer-ends, -1, 0) | 98 // "move-cursor" (buffer-ends, -1, 0) |
| 128 { "MoveToBeginningOfDocument", "" }, | 99 { "MoveToBeginningOfDocument", "" }, |
| 129 // "move-cursor" (buffer-ends, 1, 0) | 100 // "move-cursor" (buffer-ends, 1, 0) |
| 130 { "MoveToEndOfDocument", "" } | 101 { "MoveToEndOfDocument", "" } |
| 131 }; | 102 }; |
| 132 | 103 |
| 133 TestKeyBinding(NewNativeWebKeyboardEvent(GDK_1, GDK_CONTROL_MASK), | 104 TestKeyBinding(NewNativeWebKeyboardEvent(ui::VKEY_1, ui::EF_CONTROL_DOWN), |
| 134 kEditCommands, arraysize(kEditCommands)); | 105 kEditCommands, arraysize(kEditCommands)); |
| 135 } | 106 } |
| 136 | 107 |
| 137 TEST_F(GtkKeyBindingsHandlerTest, DeleteFromCursor) { | 108 TEST_F(Gtk2KeyBindingsHandlerTest, DeleteFromCursor) { |
| 138 static const EditCommand kEditCommands[] = { | 109 static const EditCommand kEditCommands[] = { |
| 139 // "delete-from-cursor" (chars, -2) | 110 // "delete-from-cursor" (chars, -2) |
| 140 { "DeleteBackward", "" }, | 111 { "DeleteBackward", "" }, |
| 141 { "DeleteBackward", "" }, | 112 { "DeleteBackward", "" }, |
| 142 // "delete-from-cursor" (chars, 2) | 113 // "delete-from-cursor" (chars, 2) |
| 143 { "DeleteForward", "" }, | 114 { "DeleteForward", "" }, |
| 144 { "DeleteForward", "" }, | 115 { "DeleteForward", "" }, |
| 145 // "delete-from-cursor" (word-ends, -1) | 116 // "delete-from-cursor" (word-ends, -1) |
| 146 { "DeleteWordBackward", "" }, | 117 { "DeleteWordBackward", "" }, |
| 147 // "delete-from-cursor" (word-ends, 1) | 118 // "delete-from-cursor" (word-ends, 1) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 167 // "delete-from-cursor" (paragraph-ends, 1) | 138 // "delete-from-cursor" (paragraph-ends, 1) |
| 168 { "DeleteToEndOfParagraph", "" }, | 139 { "DeleteToEndOfParagraph", "" }, |
| 169 // "delete-from-cursor" (paragraphs, -1) | 140 // "delete-from-cursor" (paragraphs, -1) |
| 170 { "MoveToBeginningOfParagraph", "" }, | 141 { "MoveToBeginningOfParagraph", "" }, |
| 171 { "DeleteToEndOfParagraph", "" }, | 142 { "DeleteToEndOfParagraph", "" }, |
| 172 // "delete-from-cursor" (paragraphs, 1) | 143 // "delete-from-cursor" (paragraphs, 1) |
| 173 { "MoveToBeginningOfParagraph", "" }, | 144 { "MoveToBeginningOfParagraph", "" }, |
| 174 { "DeleteToEndOfParagraph", "" }, | 145 { "DeleteToEndOfParagraph", "" }, |
| 175 }; | 146 }; |
| 176 | 147 |
| 177 TestKeyBinding(NewNativeWebKeyboardEvent(GDK_2, GDK_CONTROL_MASK), | 148 TestKeyBinding(NewNativeWebKeyboardEvent(ui::VKEY_2, ui::EF_CONTROL_DOWN), |
| 178 kEditCommands, arraysize(kEditCommands)); | 149 kEditCommands, arraysize(kEditCommands)); |
| 179 } | 150 } |
| 180 | 151 |
| 181 TEST_F(GtkKeyBindingsHandlerTest, OtherActions) { | 152 TEST_F(Gtk2KeyBindingsHandlerTest, OtherActions) { |
| 182 static const EditCommand kBackspace[] = { | 153 static const EditCommand kBackspace[] = { |
| 183 { "DeleteBackward", "" } | 154 { "DeleteBackward", "" } |
| 184 }; | 155 }; |
| 185 TestKeyBinding(NewNativeWebKeyboardEvent(GDK_3, GDK_CONTROL_MASK), | 156 TestKeyBinding(NewNativeWebKeyboardEvent(ui::VKEY_3, ui::EF_CONTROL_DOWN), |
| 186 kBackspace, arraysize(kBackspace)); | 157 kBackspace, arraysize(kBackspace)); |
| 187 | 158 |
| 188 static const EditCommand kCopyClipboard[] = { | 159 static const EditCommand kCopyClipboard[] = { |
| 189 { "Copy", "" } | 160 { "Copy", "" } |
| 190 }; | 161 }; |
| 191 TestKeyBinding(NewNativeWebKeyboardEvent(GDK_4, GDK_CONTROL_MASK), | 162 TestKeyBinding(NewNativeWebKeyboardEvent(ui::VKEY_4, ui::EF_CONTROL_DOWN), |
| 192 kCopyClipboard, arraysize(kCopyClipboard)); | 163 kCopyClipboard, arraysize(kCopyClipboard)); |
| 193 | 164 |
| 194 static const EditCommand kCutClipboard[] = { | 165 static const EditCommand kCutClipboard[] = { |
| 195 { "Cut", "" } | 166 { "Cut", "" } |
| 196 }; | 167 }; |
| 197 TestKeyBinding(NewNativeWebKeyboardEvent(GDK_5, GDK_CONTROL_MASK), | 168 TestKeyBinding(NewNativeWebKeyboardEvent(ui::VKEY_5, ui::EF_CONTROL_DOWN), |
| 198 kCutClipboard, arraysize(kCutClipboard)); | 169 kCutClipboard, arraysize(kCutClipboard)); |
| 199 | 170 |
| 200 static const EditCommand kInsertAtCursor[] = { | 171 static const EditCommand kInsertAtCursor[] = { |
| 201 { "InsertText", "hello" } | 172 { "InsertText", "hello" } |
| 202 }; | 173 }; |
| 203 TestKeyBinding(NewNativeWebKeyboardEvent(GDK_6, GDK_CONTROL_MASK), | 174 TestKeyBinding(NewNativeWebKeyboardEvent(ui::VKEY_6, ui::EF_CONTROL_DOWN), |
| 204 kInsertAtCursor, arraysize(kInsertAtCursor)); | 175 kInsertAtCursor, arraysize(kInsertAtCursor)); |
| 205 | 176 |
| 206 static const EditCommand kPasteClipboard[] = { | 177 static const EditCommand kPasteClipboard[] = { |
| 207 { "Paste", "" } | 178 { "Paste", "" } |
| 208 }; | 179 }; |
| 209 TestKeyBinding(NewNativeWebKeyboardEvent(GDK_7, GDK_CONTROL_MASK), | 180 TestKeyBinding(NewNativeWebKeyboardEvent(ui::VKEY_7, ui::EF_CONTROL_DOWN), |
| 210 kPasteClipboard, arraysize(kPasteClipboard)); | 181 kPasteClipboard, arraysize(kPasteClipboard)); |
| 211 | 182 |
| 212 static const EditCommand kSelectAll[] = { | 183 static const EditCommand kSelectAll[] = { |
| 213 { "Unselect", "" }, | 184 { "Unselect", "" }, |
| 214 { "SelectAll", "" } | 185 { "SelectAll", "" } |
| 215 }; | 186 }; |
| 216 TestKeyBinding(NewNativeWebKeyboardEvent(GDK_8, GDK_CONTROL_MASK), | 187 TestKeyBinding(NewNativeWebKeyboardEvent(ui::VKEY_8, ui::EF_CONTROL_DOWN), |
| 217 kSelectAll, arraysize(kSelectAll)); | 188 kSelectAll, arraysize(kSelectAll)); |
| 218 | 189 |
| 219 static const EditCommand kSetAnchor[] = { | 190 static const EditCommand kSetAnchor[] = { |
| 220 { "SetMark", "" } | 191 { "SetMark", "" } |
| 221 }; | 192 }; |
| 222 TestKeyBinding(NewNativeWebKeyboardEvent(GDK_9, GDK_CONTROL_MASK), | 193 TestKeyBinding(NewNativeWebKeyboardEvent(ui::VKEY_9, ui::EF_CONTROL_DOWN), |
| 223 kSetAnchor, arraysize(kSetAnchor)); | 194 kSetAnchor, arraysize(kSetAnchor)); |
| 224 } | 195 } |
| 225 | 196 |
| 226 } // namespace content | 197 } // namespace libgtk2ui |
| OLD | NEW |