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
=== modified file 'Nux/InputMethodIBus.cpp'
--- Nux/InputMethodIBus.cpp 2012-06-26 22:35:16 +0000
+++ Nux/InputMethodIBus.cpp 2012-07-03 14:59:18 +0000
@@ -504,7 +504,7 @@
504 {504 {
505 for (Event const& ev : hotkeys_)505 for (Event const& ev : hotkeys_)
506 {506 {
507 if (ev.type == type && ev.x11_keysym == keysym)507 if (ev.x11_keysym == keysym && (ev.type == type || type == EVENT_KEY_DOWN))
508 {508 {
509 if (type == EVENT_KEY_UP)509 if (type == EVENT_KEY_UP)
510 return (modifiers & ev.key_modifiers);510 return (modifiers & ev.key_modifiers);
511511
=== modified file 'Nux/TextEntry.cpp'
--- Nux/TextEntry.cpp 2012-07-03 11:04:59 +0000
+++ Nux/TextEntry.cpp 2012-07-03 14:59:18 +0000
@@ -316,7 +316,7 @@
316316
317 // FIXME Have to get the current event fot he x11_keycode for ibus-hangul/korean input317 // FIXME Have to get the current event fot he x11_keycode for ibus-hangul/korean input
318 nux::Event const& cur_event = GetGraphicsDisplay()->GetCurrentEvent();318 nux::Event const& cur_event = GetGraphicsDisplay()->GetCurrentEvent();
319 KeyEvent event(static_cast<EventType>(event_type), keysym,cur_event.x11_keycode, state, character);319 KeyEvent event(static_cast<EventType>(event_type), keysym, cur_event.x11_keycode, state, character);
320 im_filtered = ime_->FilterKeyEvent(event);320 im_filtered = ime_->FilterKeyEvent(event);
321#endif321#endif
322322
@@ -464,13 +464,16 @@
464// }464// }
465 }465 }
466466
467 if (character && strlen(character) && !im_filtered)
468 {
469 EnterText(character);
470 }
471
472 if (!im_filtered)467 if (!im_filtered)
473 {468 {
469 if (character)
470 {
471 unsigned int utf_char = g_utf8_get_char(character);
472
473 if (g_unichar_isprint(utf_char))
474 EnterText(character);
475 }
476
474 QueueRefresh(false, true);477 QueueRefresh(false, true);
475 }478 }
476 }479 }
@@ -2317,16 +2320,29 @@
23172320
2318 bool TextEntry::InspectKeyEvent(unsigned int eventType, unsigned int key_sym, const char* character)2321 bool TextEntry::InspectKeyEvent(unsigned int eventType, unsigned int key_sym, const char* character)
2319 {2322 {
2323 nux::Event const& cur_event = GetGraphicsDisplay()->GetCurrentEvent();
2324
2325 return InspectKeyEvent(cur_event);
2326 }
2327
2328 bool TextEntry::InspectKeyEvent(nux::Event const& event)
2329 {
2330 unsigned int eventType = event.type;
2331 unsigned int key_sym = event.GetKeySym();
2332
2320#if defined(NUX_OS_LINUX)2333#if defined(NUX_OS_LINUX)
2321 if (im_running())2334 if (im_running())
2322 {2335 {
2323 // Always allow IBus hotkey events2336 // Always allow IBus hotkey events
2324 nux::Event const& cur_event = GetGraphicsDisplay()->GetCurrentEvent();2337 if (ime_->IsHotkeyEvent(static_cast<EventType>(eventType), key_sym, event.key_modifiers))
2325 if (ime_->IsHotkeyEvent(static_cast<EventType>(eventType), key_sym, cur_event.key_modifiers))
2326 return true;2338 return true;
2327 }2339 }
2328#endif2340#endif
23292341
2342 // Ignore events when Alt or Super are pressed
2343 if (event.GetKeyModifierState(KEY_MODIFIER_SUPER) || event.GetKeyModifierState(KEY_MODIFIER_ALT))
2344 return false;
2345
2330 if ((eventType == NUX_KEYDOWN) && (key_nav_mode_ == true) && (text_input_mode_ == false) && (ime_active_ == false))2346 if ((eventType == NUX_KEYDOWN) && (key_nav_mode_ == true) && (text_input_mode_ == false) && (ime_active_ == false))
2331 {2347 {
2332 if (key_sym == NUX_VK_ENTER ||2348 if (key_sym == NUX_VK_ENTER ||
@@ -2388,14 +2404,14 @@
2388 {2404 {
2389 if ((!multiline_) && (!lose_key_focus_on_key_nav_direction_up_) && NUX_VK_UP)2405 if ((!multiline_) && (!lose_key_focus_on_key_nav_direction_up_) && NUX_VK_UP)
2390 {2406 {
2391 // By returning true, the text entry signals that it want to receinve the signal for this event.2407 // By returning true, the text entry signals that it want to receive the signal for this event.
2392 // Otherwise, the parent view of the text entry would be looking for another view to receive keynav focus to.2408 // Otherwise, the parent view of the text entry would be looking for another view to receive keynav focus to.
2393 return true;2409 return true;
2394 }2410 }
23952411
2396 if ((!multiline_) && (!lose_key_focus_on_key_nav_direction_down_) && NUX_VK_DOWN)2412 if ((!multiline_) && (!lose_key_focus_on_key_nav_direction_down_) && NUX_VK_DOWN)
2397 {2413 {
2398 // By returning true, the text entry signals that it want to receinve the signal for this event.2414 // By returning true, the text entry signals that it want to receive the signal for this event.
2399 // Otherwise, the parent view of the text entry would be looking for another view to receive keynav focus to.2415 // Otherwise, the parent view of the text entry would be looking for another view to receive keynav focus to.
2400 return true;2416 return true;
2401 }2417 }
24022418
=== modified file 'Nux/TextEntry.h'
--- Nux/TextEntry.h 2012-07-03 11:04:59 +0000
+++ Nux/TextEntry.h 2012-07-03 14:59:18 +0000
@@ -491,9 +491,8 @@
491 bool composition_mode_;491 bool composition_mode_;
492 std::string composition_string_;492 std::string composition_string_;
493493
494 virtual bool InspectKeyEvent(unsigned int eventType,494 virtual bool InspectKeyEvent(Event const& event);
495 unsigned int keysym,495 virtual bool InspectKeyEvent(unsigned int eventType, unsigned int keysym, const char* character);
496 const char* character);
497};496};
498}497}
499498
500499
=== modified file 'tests/gtest-nux-textentry.cpp'
--- tests/gtest-nux-textentry.cpp 2012-03-05 19:32:20 +0000
+++ tests/gtest-nux-textentry.cpp 2012-07-03 14:59:18 +0000
@@ -1,50 +1,67 @@
1#include <string>
2#include <fstream>
3
4#include <iostream>
5#include <gmock/gmock.h>1#include <gmock/gmock.h>
6#include <boost/filesystem.hpp>
7#include <glib.h>
82
9#include "Nux/Nux.h"3#include "Nux/Nux.h"
10#include "Nux/TextEntry.h"4#include "Nux/TextEntry.h"
5#if defined(NUX_OS_LINUX)
6#include "Nux/InputMethodIBus.h"
7#endif
118
129
13using namespace testing;10using namespace testing;
11using namespace nux;
1412
15namespace {13namespace {
1614
17TEST(TestTextEntry, TestSetText)15class MockTextEntry : public nux::TextEntry
18{16{
19 nux::NuxInitialize(0);17public:
20 nux::WindowThread* wnd_thread = nux::CreateNuxWindow("Nux Window", 300, 200,18 MockTextEntry(const char* text) : nux::TextEntry(text)
21 nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL);19 {}
2220
23 nux::TextEntry* text_entry = new nux::TextEntry("");21 bool InspectKeyEvent(nux::Event const& event)
24 22 {
25 nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(text_entry);23 return nux::TextEntry::InspectKeyEvent(event);
2624 }
25
26 nux::IBusIMEContext* ime() const
27 {
28#if defined(NUX_OS_LINUX)
29 return ime_;
30#else
31 return nullptr;
32#endif
33 }
34};
35
36class TestTextEntry : public Test
37{
38public:
39 virtual void SetUp()
40 {
41 nux::NuxInitialize(0);
42 wnd_thread.reset(nux::CreateNuxWindow("Nux Window", 300, 200,
43 nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL));
44
45 text_entry = new MockTextEntry("");
46 nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(text_entry.GetPointer());
47 }
48
49 std::unique_ptr<nux::WindowThread> wnd_thread;
50 nux::ObjectPtr<MockTextEntry> text_entry;
51};
52
53TEST_F(TestTextEntry, TestSetText)
54{
27 EXPECT_EQ(text_entry->IsInTextInputMode(), false);55 EXPECT_EQ(text_entry->IsInTextInputMode(), false);
2856
29 text_entry->SetText("Nux");57 text_entry->SetText("Nux");
30 EXPECT_EQ(text_entry->GetText() == std::string("Nux"), true);58 EXPECT_EQ(text_entry->GetText() == std::string("Nux"), true);
3159
32 EXPECT_EQ(text_entry->IsInTextInputMode(), true);60 EXPECT_EQ(text_entry->IsInTextInputMode(), true);
33
34 text_entry->UnReference();
35 delete wnd_thread;
36}61}
3762
38TEST(TestTextEntry, TestEnterText)63TEST_F(TestTextEntry, TestEnterText)
39{64{
40 nux::NuxInitialize(0);
41 nux::WindowThread* wnd_thread = nux::CreateNuxWindow("Nux Window", 300, 200,
42 nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL);
43
44 nux::TextEntry* text_entry = new nux::TextEntry("");
45
46 nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(text_entry);
47
48 EXPECT_EQ(text_entry->IsInTextInputMode(), false);65 EXPECT_EQ(text_entry->IsInTextInputMode(), false);
4966
50 text_entry->EnterText("Nux");67 text_entry->EnterText("Nux");
@@ -52,21 +69,10 @@
52 EXPECT_EQ(text_entry->GetText() == std::string("Nux"), true);69 EXPECT_EQ(text_entry->GetText() == std::string("Nux"), true);
5370
54 EXPECT_EQ(text_entry->IsInTextInputMode(), true);71 EXPECT_EQ(text_entry->IsInTextInputMode(), true);
55
56 text_entry->UnReference();
57 delete wnd_thread;
58}72}
5973
60TEST(TestTextEntry, TestDeleteText)74TEST_F(TestTextEntry, TestDeleteText)
61{75{
62 nux::NuxInitialize(0);
63 nux::WindowThread* wnd_thread = nux::CreateNuxWindow("Nux Window", 300, 200,
64 nux::WINDOWSTYLE_NORMAL, NULL, false, NULL, NULL);
65
66 nux::TextEntry* text_entry = new nux::TextEntry("");
67
68 nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(text_entry);
69
70 EXPECT_EQ(text_entry->IsInTextInputMode(), false);76 EXPECT_EQ(text_entry->IsInTextInputMode(), false);
7177
72 text_entry->EnterText("Nux");78 text_entry->EnterText("Nux");
@@ -77,7 +83,7 @@
7783
78 nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(NULL);84 nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(NULL);
7985
80 nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(text_entry);86 nux::GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(text_entry.GetPointer());
8187
82 EXPECT_EQ(text_entry->IsInTextInputMode(), false);88 EXPECT_EQ(text_entry->IsInTextInputMode(), false);
8389
@@ -86,10 +92,65 @@
86 EXPECT_EQ(text_entry->GetText() == std::string(""), true);92 EXPECT_EQ(text_entry->GetText() == std::string(""), true);
8793
88 EXPECT_EQ(text_entry->IsInTextInputMode(), true);94 EXPECT_EQ(text_entry->IsInTextInputMode(), true);
8995}
9096
91 text_entry->UnReference();97#if defined(NUX_OS_LINUX)
92 delete wnd_thread;98class TestEvent : public nux::Event
99{
100public:
101 TestEvent(nux::KeyModifier keymod, unsigned long keysym)
102 {
103 type = nux::NUX_KEYDOWN;
104 key_modifiers = keymod;
105 x11_keysym = keysym;
106 }
107
108 TestEvent(unsigned long keysym)
109 {
110 type = nux::NUX_KEYDOWN;
111 x11_keysym = keysym;
112 }
113};
114
115TEST_F(TestTextEntry, AltLinuxKeybindings)
116{
117 for (unsigned long keysym = 0; keysym < XK_VoidSymbol; ++keysym)
118 {
119 TestEvent event(KEY_MODIFIER_ALT, keysym);
120
121 if (text_entry->ime()->IsHotkeyEvent(event.type, event.GetKeySym(), event.key_modifiers))
122 EXPECT_TRUE(text_entry->InspectKeyEvent(event));
123 else
124 EXPECT_FALSE(text_entry->InspectKeyEvent(event));
125 }
126}
127
128TEST_F(TestTextEntry, SuperLinuxKeybindings)
129{
130 for (unsigned long keysym = 0; keysym < XK_VoidSymbol; ++keysym)
131 {
132 TestEvent event(KEY_MODIFIER_SUPER, keysym);
133
134 if (text_entry->ime()->IsHotkeyEvent(event.type, event.GetKeySym(), event.key_modifiers))
135 EXPECT_TRUE(text_entry->InspectKeyEvent(event));
136 else
137 EXPECT_FALSE(text_entry->InspectKeyEvent(event));
138 }
139}
140#endif
141
142TEST_F(TestTextEntry, InvalidKeys)
143{
144 std::vector<std::string> invalid_chars = {"", "", "", "", "",
145 "", "", "", "", "",
146 "", "", "
93"};147"};
148 for (auto c : invalid_chars)
149 {
150 unsigned int keysym = g_utf8_get_char(c.c_str());
151 text_entry->DeleteText(0, std::numeric_limits<int>::max());
152 text_entry->key_down.emit(NUX_KEYDOWN, keysym, 0, c.c_str(), 1);
153 EXPECT_EQ(text_entry->GetText(), "");
154 }
94}155}
95156
96}157}
97158
=== modified file 'tests/xtest-text-entry.cpp'
--- tests/xtest-text-entry.cpp 2012-04-25 15:37:23 +0000
+++ tests/xtest-text-entry.cpp 2012-07-03 14:59:18 +0000
@@ -313,6 +313,29 @@
313 test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "TextEntry is empty");313 test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "TextEntry is empty");
314 }314 }
315315
316 // Send invalid keys
317 {
318 test.ViewSendKeyCombo(XK_Control_L, 0, 0, 'r');
319 test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "Invalid key: TextEntry is empty");
320 nux::SleepForMilliseconds(500);
321
322 test.ViewSendKeyCombo(XK_Control_L, 0, 0, 'w');
323 test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "Invalid key: TextEntry is empty");
324 nux::SleepForMilliseconds(500);
325
326 test.ViewSendKeyCombo(XK_Control_L, 0, 0, 'g');
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, 'h');
331 test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "Invalid key: TextEntry is empty");
332 nux::SleepForMilliseconds(500);
333
334 nux::SleepForMilliseconds(500);
335 test.ViewSendCtrlA();
336 test.ViewSendDelete();
337 }
338
316 // Toggle IBus339 // Toggle IBus
317 // Type "qwerty"340 // Type "qwerty"
318 // Simulate key '1' to select the first IM option341 // Simulate key '1' to select the first IM option

Subscribers

People subscribed via source and target branches