Nux

Merge lp:~3v1n0/nux/text-entry-meta-filters into lp:nux

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Marco Trevisan (Treviño)
Approved revision: 640
Merged at revision: 627
Proposed branch: lp:~3v1n0/nux/text-entry-meta-filters
Merge into: lp:nux
Diff against target: 348 lines (+158/-59)
5 files modified
Nux/InputMethodIBus.cpp (+1/-1)
Nux/TextEntry.cpp (+26/-10)
Nux/TextEntry.h (+2/-3)
tests/gtest-nux-textentry.cpp (+106/-45)
tests/xtest-text-entry.cpp (+23/-0)
To merge this branch: bzr merge lp:~3v1n0/nux/text-entry-meta-filters
Reviewer Review Type Date Requested Status
Brandon Schaefer (community) Approve
Review via email: mp+113137@code.launchpad.net

Commit message

TextEntry: don't allow invalid character to be written and ignore keypress when holding Alt or Super

Description of the change

Filter out keys when pressing Alt or Super modifiers, also avoid that invalid test is written into the text-entry.

Both xtests and gtests added.

UNBLOCK

To post a comment you must log in.
638. By Marco Trevisan (Treviño)

gtest-nux-textentry: don't be linux dependent on test

639. By Marco Trevisan (Treviño)

InputMethodIBus: both consider keydown and keyup events

Ibus needs both when using the +Release flag

640. By Marco Trevisan (Treviño)

gtest-nux-textentry: fix tests when ibus-daemon is active

Revision history for this message
Brandon Schaefer (brandontschaefer) wrote :

All tests pass and looks good. +1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Nux/InputMethodIBus.cpp'
2--- Nux/InputMethodIBus.cpp 2012-06-26 22:35:16 +0000
3+++ Nux/InputMethodIBus.cpp 2012-07-03 14:59:18 +0000
4@@ -504,7 +504,7 @@
5 {
6 for (Event const& ev : hotkeys_)
7 {
8- if (ev.type == type && ev.x11_keysym == keysym)
9+ if (ev.x11_keysym == keysym && (ev.type == type || type == EVENT_KEY_DOWN))
10 {
11 if (type == EVENT_KEY_UP)
12 return (modifiers & ev.key_modifiers);
13
14=== modified file 'Nux/TextEntry.cpp'
15--- Nux/TextEntry.cpp 2012-07-03 11:04:59 +0000
16+++ Nux/TextEntry.cpp 2012-07-03 14:59:18 +0000
17@@ -316,7 +316,7 @@
18
19 // FIXME Have to get the current event fot he x11_keycode for ibus-hangul/korean input
20 nux::Event const& cur_event = GetGraphicsDisplay()->GetCurrentEvent();
21- KeyEvent event(static_cast<EventType>(event_type), keysym,cur_event.x11_keycode, state, character);
22+ KeyEvent event(static_cast<EventType>(event_type), keysym, cur_event.x11_keycode, state, character);
23 im_filtered = ime_->FilterKeyEvent(event);
24 #endif
25
26@@ -464,13 +464,16 @@
27 // }
28 }
29
30- if (character && strlen(character) && !im_filtered)
31- {
32- EnterText(character);
33- }
34-
35 if (!im_filtered)
36 {
37+ if (character)
38+ {
39+ unsigned int utf_char = g_utf8_get_char(character);
40+
41+ if (g_unichar_isprint(utf_char))
42+ EnterText(character);
43+ }
44+
45 QueueRefresh(false, true);
46 }
47 }
48@@ -2317,16 +2320,29 @@
49
50 bool TextEntry::InspectKeyEvent(unsigned int eventType, unsigned int key_sym, const char* character)
51 {
52+ nux::Event const& cur_event = GetGraphicsDisplay()->GetCurrentEvent();
53+
54+ return InspectKeyEvent(cur_event);
55+ }
56+
57+ bool TextEntry::InspectKeyEvent(nux::Event const& event)
58+ {
59+ unsigned int eventType = event.type;
60+ unsigned int key_sym = event.GetKeySym();
61+
62 #if defined(NUX_OS_LINUX)
63 if (im_running())
64 {
65 // Always allow IBus hotkey events
66- nux::Event const& cur_event = GetGraphicsDisplay()->GetCurrentEvent();
67- if (ime_->IsHotkeyEvent(static_cast<EventType>(eventType), key_sym, cur_event.key_modifiers))
68+ if (ime_->IsHotkeyEvent(static_cast<EventType>(eventType), key_sym, event.key_modifiers))
69 return true;
70 }
71 #endif
72
73+ // Ignore events when Alt or Super are pressed
74+ if (event.GetKeyModifierState(KEY_MODIFIER_SUPER) || event.GetKeyModifierState(KEY_MODIFIER_ALT))
75+ return false;
76+
77 if ((eventType == NUX_KEYDOWN) && (key_nav_mode_ == true) && (text_input_mode_ == false) && (ime_active_ == false))
78 {
79 if (key_sym == NUX_VK_ENTER ||
80@@ -2388,14 +2404,14 @@
81 {
82 if ((!multiline_) && (!lose_key_focus_on_key_nav_direction_up_) && NUX_VK_UP)
83 {
84- // By returning true, the text entry signals that it want to receinve the signal for this event.
85+ // By returning true, the text entry signals that it want to receive the signal for this event.
86 // Otherwise, the parent view of the text entry would be looking for another view to receive keynav focus to.
87 return true;
88 }
89
90 if ((!multiline_) && (!lose_key_focus_on_key_nav_direction_down_) && NUX_VK_DOWN)
91 {
92- // By returning true, the text entry signals that it want to receinve the signal for this event.
93+ // By returning true, the text entry signals that it want to receive the signal for this event.
94 // Otherwise, the parent view of the text entry would be looking for another view to receive keynav focus to.
95 return true;
96 }
97
98=== modified file 'Nux/TextEntry.h'
99--- Nux/TextEntry.h 2012-07-03 11:04:59 +0000
100+++ Nux/TextEntry.h 2012-07-03 14:59:18 +0000
101@@ -491,9 +491,8 @@
102 bool composition_mode_;
103 std::string composition_string_;
104
105- virtual bool InspectKeyEvent(unsigned int eventType,
106- unsigned int keysym,
107- const char* character);
108+ virtual bool InspectKeyEvent(Event const& event);
109+ virtual bool InspectKeyEvent(unsigned int eventType, unsigned int keysym, const char* character);
110 };
111 }
112
113
114=== modified file 'tests/gtest-nux-textentry.cpp'
115--- tests/gtest-nux-textentry.cpp 2012-03-05 19:32:20 +0000
116+++ tests/gtest-nux-textentry.cpp 2012-07-03 14:59:18 +0000
117@@ -1,50 +1,67 @@
118-#include <string>
119-#include <fstream>
120-
121-#include <iostream>
122 #include <gmock/gmock.h>
123-#include <boost/filesystem.hpp>
124-#include <glib.h>
125
126 #include "Nux/Nux.h"
127 #include "Nux/TextEntry.h"
128+#if defined(NUX_OS_LINUX)
129+#include "Nux/InputMethodIBus.h"
130+#endif
131
132
133 using namespace testing;
134+using namespace nux;
135
136 namespace {
137
138-TEST(TestTextEntry, TestSetText)
139-{
140- nux::NuxInitialize(0);
141- nux::WindowThread* wnd_thread = nux::CreateNuxWindow("Nux Window", 300, 200,
142- nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL);
143-
144- nux::TextEntry* text_entry = new nux::TextEntry("");
145-
146- nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(text_entry);
147-
148+class MockTextEntry : public nux::TextEntry
149+{
150+public:
151+ MockTextEntry(const char* text) : nux::TextEntry(text)
152+ {}
153+
154+ bool InspectKeyEvent(nux::Event const& event)
155+ {
156+ return nux::TextEntry::InspectKeyEvent(event);
157+ }
158+
159+ nux::IBusIMEContext* ime() const
160+ {
161+#if defined(NUX_OS_LINUX)
162+ return ime_;
163+#else
164+ return nullptr;
165+#endif
166+ }
167+};
168+
169+class TestTextEntry : public Test
170+{
171+public:
172+ virtual void SetUp()
173+ {
174+ nux::NuxInitialize(0);
175+ wnd_thread.reset(nux::CreateNuxWindow("Nux Window", 300, 200,
176+ nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL));
177+
178+ text_entry = new MockTextEntry("");
179+ nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(text_entry.GetPointer());
180+ }
181+
182+ std::unique_ptr<nux::WindowThread> wnd_thread;
183+ nux::ObjectPtr<MockTextEntry> text_entry;
184+};
185+
186+TEST_F(TestTextEntry, TestSetText)
187+{
188 EXPECT_EQ(text_entry->IsInTextInputMode(), false);
189
190 text_entry->SetText("Nux");
191 EXPECT_EQ(text_entry->GetText() == std::string("Nux"), true);
192
193 EXPECT_EQ(text_entry->IsInTextInputMode(), true);
194-
195- text_entry->UnReference();
196- delete wnd_thread;
197 }
198
199-TEST(TestTextEntry, TestEnterText)
200+TEST_F(TestTextEntry, TestEnterText)
201 {
202- nux::NuxInitialize(0);
203- nux::WindowThread* wnd_thread = nux::CreateNuxWindow("Nux Window", 300, 200,
204- nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL);
205-
206- nux::TextEntry* text_entry = new nux::TextEntry("");
207-
208- nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(text_entry);
209-
210 EXPECT_EQ(text_entry->IsInTextInputMode(), false);
211
212 text_entry->EnterText("Nux");
213@@ -52,21 +69,10 @@
214 EXPECT_EQ(text_entry->GetText() == std::string("Nux"), true);
215
216 EXPECT_EQ(text_entry->IsInTextInputMode(), true);
217-
218- text_entry->UnReference();
219- delete wnd_thread;
220 }
221
222-TEST(TestTextEntry, TestDeleteText)
223+TEST_F(TestTextEntry, TestDeleteText)
224 {
225- nux::NuxInitialize(0);
226- nux::WindowThread* wnd_thread = nux::CreateNuxWindow("Nux Window", 300, 200,
227- nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL);
228-
229- nux::TextEntry* text_entry = new nux::TextEntry("");
230-
231- nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(text_entry);
232-
233 EXPECT_EQ(text_entry->IsInTextInputMode(), false);
234
235 text_entry->EnterText("Nux");
236@@ -77,7 +83,7 @@
237
238 nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(NULL);
239
240- nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(text_entry);
241+ nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(text_entry.GetPointer());
242
243 EXPECT_EQ(text_entry->IsInTextInputMode(), false);
244
245@@ -86,10 +92,65 @@
246 EXPECT_EQ(text_entry->GetText() == std::string(""), true);
247
248 EXPECT_EQ(text_entry->IsInTextInputMode(), true);
249-
250-
251- text_entry->UnReference();
252- delete wnd_thread;
253+}
254+
255+#if defined(NUX_OS_LINUX)
256+class TestEvent : public nux::Event
257+{
258+public:
259+ TestEvent(nux::KeyModifier keymod, unsigned long keysym)
260+ {
261+ type = nux::NUX_KEYDOWN;
262+ key_modifiers = keymod;
263+ x11_keysym = keysym;
264+ }
265+
266+ TestEvent(unsigned long keysym)
267+ {
268+ type = nux::NUX_KEYDOWN;
269+ x11_keysym = keysym;
270+ }
271+};
272+
273+TEST_F(TestTextEntry, AltLinuxKeybindings)
274+{
275+ for (unsigned long keysym = 0; keysym < XK_VoidSymbol; ++keysym)
276+ {
277+ TestEvent event(KEY_MODIFIER_ALT, keysym);
278+
279+ if (text_entry->ime()->IsHotkeyEvent(event.type, event.GetKeySym(), event.key_modifiers))
280+ EXPECT_TRUE(text_entry->InspectKeyEvent(event));
281+ else
282+ EXPECT_FALSE(text_entry->InspectKeyEvent(event));
283+ }
284+}
285+
286+TEST_F(TestTextEntry, SuperLinuxKeybindings)
287+{
288+ for (unsigned long keysym = 0; keysym < XK_VoidSymbol; ++keysym)
289+ {
290+ TestEvent event(KEY_MODIFIER_SUPER, keysym);
291+
292+ if (text_entry->ime()->IsHotkeyEvent(event.type, event.GetKeySym(), event.key_modifiers))
293+ EXPECT_TRUE(text_entry->InspectKeyEvent(event));
294+ else
295+ EXPECT_FALSE(text_entry->InspectKeyEvent(event));
296+ }
297+}
298+#endif
299+
300+TEST_F(TestTextEntry, InvalidKeys)
301+{
302+ std::vector<std::string> invalid_chars = {"", "", "", "", "",
303+ "", "", "", "", "",
304+ "", "", "
305"};
306+ for (auto c : invalid_chars)
307+ {
308+ unsigned int keysym = g_utf8_get_char(c.c_str());
309+ text_entry->DeleteText(0, std::numeric_limits<int>::max());
310+ text_entry->key_down.emit(NUX_KEYDOWN, keysym, 0, c.c_str(), 1);
311+ EXPECT_EQ(text_entry->GetText(), "");
312+ }
313 }
314
315 }
316
317=== modified file 'tests/xtest-text-entry.cpp'
318--- tests/xtest-text-entry.cpp 2012-04-25 15:37:23 +0000
319+++ tests/xtest-text-entry.cpp 2012-07-03 14:59:18 +0000
320@@ -313,6 +313,29 @@
321 test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "TextEntry is empty");
322 }
323
324+ // Send invalid keys
325+ {
326+ test.ViewSendKeyCombo(XK_Control_L, 0, 0, 'r');
327+ test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "Invalid key: TextEntry is empty");
328+ nux::SleepForMilliseconds(500);
329+
330+ test.ViewSendKeyCombo(XK_Control_L, 0, 0, 'w');
331+ test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "Invalid key: TextEntry is empty");
332+ nux::SleepForMilliseconds(500);
333+
334+ test.ViewSendKeyCombo(XK_Control_L, 0, 0, 'g');
335+ test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "Invalid key: TextEntry is empty");
336+ nux::SleepForMilliseconds(500);
337+
338+ test.ViewSendKeyCombo(XK_Control_L, 0, 0, 'h');
339+ test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "Invalid key: TextEntry is empty");
340+ nux::SleepForMilliseconds(500);
341+
342+ nux::SleepForMilliseconds(500);
343+ test.ViewSendCtrlA();
344+ test.ViewSendDelete();
345+ }
346+
347 // Toggle IBus
348 // Type "qwerty"
349 // Simulate key '1' to select the first IM option

Subscribers

People subscribed via source and target branches