Back to index

nux  3.0.0
xtest-text-entry.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU General Public License version 3, as published
00006  * by the  Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranties of
00010  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00011  * PURPOSE.  See the GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * version 3 along with this program.  If not, see
00015  * <http://www.gnu.org/licenses/>
00016  *
00017  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00018  *
00019  */
00020 
00021 #include "Nux/Nux.h"
00022 #include "Nux/WindowThread.h"
00023 #include "Nux/VLayout.h"
00024 #include "Nux/TextEntry.h"
00025 #include "Nux/InputMethodIBus.h"
00026 #include "Nux/ProgramFramework/ProgramTemplate.h"
00027 #include "Nux/ProgramFramework/TestView.h"
00028 #include <X11/extensions/XTest.h>
00029 #include <X11/keysym.h> 
00030 #include "nux_automated_test_framework.h"
00031 
00032 class TextTextEntry: public ProgramTemplate
00033 {
00034 public:
00035   TextTextEntry(const char* program_name, int window_width, int window_height, int program_life_span);
00036   ~TextTextEntry();
00037 
00038   virtual void UserInterfaceSetup();
00039 
00040   void TextEntryClick(nux::TextEntry* text_entry);
00041   void OnActivated();
00042   void OnCursorMoved(int);
00043   void ResetEvents();
00044 
00045   nux::TextEntry* text_entry_;
00046 
00047   bool clicked_;
00048   bool activated_;
00049   bool cursor_moved_;
00050 };
00051 
00052 TextTextEntry::TextTextEntry(const char* program_name,
00053   int window_width,
00054   int window_height,
00055   int program_life_span)
00056   : ProgramTemplate(program_name, window_width, window_height, program_life_span)
00057   , clicked_(false)
00058   , activated_(false)
00059   , cursor_moved_(false)
00060 {
00061   ResetEvents();
00062   text_entry_ = NULL;
00063 }
00064 
00065 TextTextEntry::~TextTextEntry()
00066 {
00067   
00068 }
00069 
00070 void TextTextEntry::ResetEvents()
00071 {
00072   clicked_ = false;
00073   activated_ = false;
00074   cursor_moved_ = false;
00075 }
00076 
00077 void TextTextEntry::TextEntryClick(nux::TextEntry* text_entry)
00078 {
00079   if (text_entry_ == text_entry)
00080   {
00081     clicked_ = true;
00082   }
00083 }
00084 
00085 void TextTextEntry::OnActivated()
00086 {
00087   activated_ = true;
00088 }
00089 
00090 void TextTextEntry::OnCursorMoved(int position)
00091 {
00092   cursor_moved_ = true;
00093 }
00094 
00095 void TextTextEntry::UserInterfaceSetup()
00096 {
00097   nux::VLayout* main_layout = new nux::VLayout(NUX_TRACKER_LOCATION);
00098   text_entry_ = new nux::TextEntry("", NUX_TRACKER_LOCATION);
00099   text_entry_->activated.connect(sigc::mem_fun(this, &TextTextEntry::OnActivated));
00100   text_entry_->cursor_moved.connect(sigc::mem_fun(this, &TextTextEntry::OnCursorMoved));
00101   text_entry_->SetFontSize(76);
00102 
00103   main_layout->AddView(text_entry_, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00104   main_layout->SetSpaceBetweenChildren(10);
00105   main_layout->SetContentDistribution(nux::MAJOR_POSITION_CENTER);
00106 
00107   static_cast<nux::WindowThread*>(window_thread_)->SetLayout(main_layout);
00108   
00109   nux::ColorLayer background(nux::Color(0xFF4D4D4D));
00110   static_cast<nux::WindowThread*>(window_thread_)->SetWindowBackgroundPaintLayer(&background);
00111 }
00112 
00113 bool SetEngineActive (IBusBus* bus_, std::string engine)
00114 {
00115     GList* engines = ibus_bus_list_active_engines(bus_);
00116     if (!engines)
00117       return false;
00118 
00119     GList* start = engines;
00120     bool found = false;
00121     gboolean global_flag = ibus_bus_get_use_global_engine(bus_);
00122 
00123     // Iterates through the active engines
00124     do
00125     {
00126       IBusEngineDesc *engine_desc = IBUS_ENGINE_DESC (engines->data);
00127 
00128       // Found Engine, make it active!
00129       if (engine == ibus_engine_desc_get_name(engine_desc))
00130       {
00131         found = true;
00132 
00133         // Set ibus to use global engines
00134         if (!global_flag)
00135           ibus_config_set_value(ibus_bus_get_config(bus_),
00136                                 "general",
00137                                 "use_global_engine",
00138                                 g_variant_new_boolean(true));
00139 
00140         // Set and activate the engine
00141         ibus_bus_set_global_engine(bus_, engine.c_str());
00142       }
00143     } while ((engines = g_list_next(engines)) != NULL);
00144 
00145     // Restores the global setting back to what it was
00146     ibus_config_set_value(ibus_bus_get_config(bus_),
00147                           "general",
00148                           "use_global_engine",
00149                           g_variant_new_boolean(global_flag));
00150 
00151     g_list_free(start);
00152     return found;
00153 }
00154 
00155 TextTextEntry* test_textentry = NULL;
00156 
00157 void TestingThread(nux::NThread* thread, void* user_data)
00158 {
00159   while (test_textentry->ReadyToGo() == false)
00160   {
00161     nuxDebugMsg("Waiting to start");
00162     nux::SleepForMilliseconds(300);
00163   }
00164 
00165   nux::SleepForMilliseconds(1300);
00166 
00167   nux::WindowThread* wnd_thread = static_cast<nux::WindowThread*>(user_data);
00168 
00169   NuxAutomatedTestFramework test(wnd_thread);
00170 
00171   test.Startup();
00172 
00173   test.TestReportMsg(test_textentry->text_entry_, "TextEntry created");
00174 
00175   test_textentry->ResetEvents();
00176   test.ViewSendMouseMotionToCenter(test_textentry->text_entry_);
00177 
00178   test.ViewSendMouseClick(0, 1);
00179 
00180   // Type "Nux"
00181   // The cursor is at the end of the line
00182   // Unset/Set the focus on the text entry
00183   // Move the cursor
00184   {
00185     test.ViewSendString("Nux");
00186     test.TestReportMsg(test_textentry->text_entry_->GetText() == "Nux", "Typed \"Nux\"");
00187 
00188     test_textentry->GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(NULL); 
00189     test_textentry->GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(test_textentry->text_entry_);
00190 
00191     test_textentry->ResetEvents();
00192     test.ViewSendLeft();
00193     nux::SleepForMilliseconds(500);
00194     test.TestReportMsg(test_textentry->cursor_moved_, "Cursor moved.");
00195 
00196     test.ViewSendCtrlA();   
00197     test.ViewSendDelete();
00198   }
00199 
00200   // Type "Nux"
00201   // The cursor is at the end of the line
00202   // Unset/Set the focus on the text entry
00203   // Press enter
00204   {
00205     test.ViewSendString("Nux");
00206     test.TestReportMsg(test_textentry->text_entry_->GetText() == "Nux", "Typed \"Nux\"");
00207 
00208     test_textentry->GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(NULL); 
00209     test_textentry->GetWindowThread()->GetWindowCompositor().SetKeyFocusArea(test_textentry->text_entry_);
00210 
00211     test_textentry->ResetEvents();
00212     test.ViewSendReturn();
00213     nux::SleepForMilliseconds(500);
00214     test.TestReportMsg(test_textentry->activated_, "Activated.");
00215 
00216     test.ViewSendCtrlA();   
00217     test.ViewSendDelete();
00218   }
00219 
00220   // Type "Nux"
00221   // The cursor is at the end of the line
00222   // Simulate CTRL+A to select the entire text
00223   // Simulate DELETE key to delete the text
00224   {
00225     test.ViewSendString("Nux");
00226     test.TestReportMsg(test_textentry->text_entry_->GetText() == "Nux", "Typed \"Nux\"");
00227 
00228     test.ViewSendCtrlA();
00229     nux::SleepForMilliseconds(500);
00230     test.TestReportMsg(test_textentry->text_entry_->GetTextSelection() == "Nux", "Selection is \"Nux\"");
00231 
00232    
00233     test.ViewSendDelete();
00234     nux::SleepForMilliseconds(500);
00235     test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "TextEntry is empty");
00236   }
00237 
00238   // Type "0123456789"
00239   // The cursor is at the end of the line
00240   // Simulate BACKSPACE key until the text is enpty
00241   {
00242     test.ViewSendString("Ubuntu");
00243     test.TestReportMsg(test_textentry->text_entry_->GetText() == "Ubuntu", "Typed \"Ubuntu\"");
00244 
00245     test.ViewSendBackspace();
00246     nux::SleepForMilliseconds(500);
00247     test.TestReportMsg(test_textentry->text_entry_->GetText() == "Ubunt", "Text is \"Ubunt\"");
00248 
00249     test.ViewSendBackspace();
00250     nux::SleepForMilliseconds(500);
00251     test.TestReportMsg(test_textentry->text_entry_->GetText() == "Ubun", "Text is \"Ubun\"");
00252 
00253     test.ViewSendBackspace();
00254     nux::SleepForMilliseconds(500);
00255     test.TestReportMsg(test_textentry->text_entry_->GetText() == "Ubu", "Text is \"Ubu\"");
00256 
00257     test.ViewSendBackspace();
00258     nux::SleepForMilliseconds(500);
00259     test.TestReportMsg(test_textentry->text_entry_->GetText() == "Ub", "Text is \"Ub\"");
00260 
00261     test.ViewSendBackspace();
00262     nux::SleepForMilliseconds(500);
00263     test.TestReportMsg(test_textentry->text_entry_->GetText() == "U", "Text is \"U\"");
00264 
00265     test.ViewSendBackspace();
00266     nux::SleepForMilliseconds(500);
00267     test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "TextEntry is empty");
00268   }
00269 
00270   // Type "0123456789"
00271   // Move cursor to start of line
00272   // Simulate DELETE key until the text is 6789
00273   // Simulate CTRL+A to select the entire text
00274   // Simulate DELETE key to delete the text
00275   {
00276     test.ViewSendString("0123456789");
00277     test.TestReportMsg(test_textentry->text_entry_->GetText() == "0123456789", "Typed \"0123456789\"");
00278 
00279     test_textentry->text_entry_->MoveCursorToLineStart();
00280 
00281     test.ViewSendDelete();
00282     nux::SleepForMilliseconds(500);
00283     test.TestReportMsg(test_textentry->text_entry_->GetText() == "123456789", "Text is \"123456789\"");
00284 
00285     test.ViewSendDelete();
00286     nux::SleepForMilliseconds(500);
00287     test.TestReportMsg(test_textentry->text_entry_->GetText() == "23456789", "Text is \"23456789\"");
00288 
00289     test.ViewSendDelete();
00290     nux::SleepForMilliseconds(500);
00291     test.TestReportMsg(test_textentry->text_entry_->GetText() == "3456789", "Text is \"3456789\"");
00292 
00293     test.ViewSendDelete();
00294     nux::SleepForMilliseconds(500);
00295     test.TestReportMsg(test_textentry->text_entry_->GetText() == "456789", "Text is \"456789\"");
00296 
00297     test.ViewSendDelete();
00298     nux::SleepForMilliseconds(500);
00299     test.TestReportMsg(test_textentry->text_entry_->GetText() == "56789", "Text is \"56789\"");
00300 
00301     test.ViewSendDelete();
00302     nux::SleepForMilliseconds(500);
00303     test.TestReportMsg(test_textentry->text_entry_->GetText() == "6789", "Text \"6789\"");
00304 
00305 
00306     test.ViewSendCtrlA();
00307     nux::SleepForMilliseconds(500);
00308     test.TestReportMsg(test_textentry->text_entry_->GetTextSelection() == "6789", "Selection is \"6789\"");
00309 
00310    
00311     test.ViewSendDelete();
00312     nux::SleepForMilliseconds(500);
00313     test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "TextEntry is empty");
00314   }
00315 
00316   // Send invalid keys
00317   {
00318     test.ViewSendKeyCombo(XK_Control_L, 0, 0, 'r');
00319     test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "Invalid key: TextEntry is empty");
00320     nux::SleepForMilliseconds(500);
00321 
00322     test.ViewSendKeyCombo(XK_Control_L, 0, 0, 'w');
00323     test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "Invalid key: TextEntry is empty");
00324     nux::SleepForMilliseconds(500);
00325 
00326     test.ViewSendKeyCombo(XK_Control_L, 0, 0, 'g');
00327     test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "Invalid key: TextEntry is empty");
00328     nux::SleepForMilliseconds(500);
00329 
00330     test.ViewSendKeyCombo(XK_Control_L, 0, 0, 'h');
00331     test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "Invalid key: TextEntry is empty");
00332     nux::SleepForMilliseconds(500);
00333 
00334     nux::SleepForMilliseconds(500);
00335     test.ViewSendCtrlA();
00336     test.ViewSendDelete();
00337   }
00338 
00339   // Toggle IBus
00340   // Type "qwerty"
00341   // Simulate key '1' to select the first IM option
00342   {
00343     // CTRL+Space to initiate iBus
00344     //test.ViewSendIBusToggle();
00345     
00346     IBusBus* bus_;
00347     ibus_init();
00348     bus_ = ibus_bus_new();
00349     bool active = false;
00350   
00351     // Test for ibus-pinyin 
00352     if (bus_ && SetEngineActive(bus_,"pinyin"))
00353     {
00354       // Type random stuff
00355       {
00356         test.ViewSendString("ninhao");
00357         nux::SleepForMilliseconds(500);
00358         test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "TextEntry is only Preedit");
00359 
00360         test.ViewSendChar('1');
00361         nux::SleepForMilliseconds(500);
00362         test.TestReportMsg(test_textentry->text_entry_->GetText() == "您好", "TextEntry is \"您好\"");
00363 
00364         test.ViewSendCtrlA();
00365         nux::SleepForMilliseconds(500);
00366         
00367         test.ViewSendDelete();
00368         nux::SleepForMilliseconds(500);
00369       }
00370 
00371       // Testing Cursor in the preedit window
00372       {
00373         test.ViewSendString("ming");
00374         nux::SleepForMilliseconds(500);
00375         test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "TextEntry is only Preedit");
00376 
00377         test.ViewSendLeft();
00378         nux::SleepForMilliseconds(500);
00379 
00380         test.ViewSendDelete();
00381         nux::SleepForMilliseconds(500);
00382 
00383         test.ViewSendChar('1');
00384         nux::SleepForMilliseconds(500);
00385         test.TestReportMsg(test_textentry->text_entry_->GetText() == "民", "TextEntry is \"民\"");
00386 
00387         test.ViewSendCtrlA();
00388         nux::SleepForMilliseconds(500);
00389         
00390         test.ViewSendDelete();
00391         nux::SleepForMilliseconds(500);
00392       }
00393 
00394       active = true;
00395     }
00396 
00397     // Test for ibus-hangul    
00398     if (bus_ && SetEngineActive(bus_,"hangul"))
00399     {   
00400       // Test for the the space in ibus-hangul working correctlly 
00401       {
00402         test.ViewSendString("asd abc ");
00403         nux::SleepForMilliseconds(500);
00404         test.TestReportMsg(test_textentry->text_entry_->GetText() == "ㅁㄴㅇ 뮻 ", "TextEntry is \"ㅁㄴㅇ 뮻 \"");
00405 
00406         test.ViewSendCtrlA();
00407         nux::SleepForMilliseconds(500);
00408 
00409         test.ViewSendDelete();
00410         nux::SleepForMilliseconds(500);
00411       }
00412 
00413       active = true;
00414     }
00415 
00416     // Checking for ibus-anthy - Japanese
00417     if (bus_ && SetEngineActive(bus_,"anthy"))
00418     {
00419       {
00420         test.ViewSendString("shisutemu ");
00421         nux::SleepForMilliseconds(500);
00422         test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "TextEntry is only Preedit");
00423 
00424         // Ctrl + J commits for ibus-anthy
00425         test.ViewSendKeyCombo(XK_Control_L, 0, 0, 'j');
00426         nux::SleepForMilliseconds(500);
00427         test.TestReportMsg(test_textentry->text_entry_->GetText() == "システム", "TextEntry is \"システム\"");
00428 
00429         test.ViewSendCtrlA();
00430         nux::SleepForMilliseconds(500);
00431 
00432         test.ViewSendDelete();
00433 
00434         nux::SleepForMilliseconds(500);
00435       }
00436 
00437       active = true;
00438     }
00439 
00440     g_object_unref (bus_); 
00441 
00442     // CTRL+Space to deactivate iBus
00443     if (active)
00444       test.ViewSendIBusToggle();
00445   }
00446 
00447   if (test.WhenDoneTerminateProgram())
00448   {
00449     wnd_thread->ExitMainLoop();
00450   }
00451   nuxDebugMsg("Exit testing thread");
00452 }
00453 
00454 int main(int argc, char** argv)
00455 {
00456   int xstatus = XInitThreads();
00457   nuxAssertMsg(xstatus > 0, "XInitThreads has failed");
00458 
00459   test_textentry = new TextTextEntry("Text Entry", 600, 200, 40000);
00460   test_textentry->Startup();
00461   test_textentry->UserInterfaceSetup();
00462 
00463   nux::SystemThread* test_thread = nux::CreateSystemThread(NULL, &TestingThread, test_textentry->GetWindowThread());
00464 
00465   test_thread->Start(test_textentry);
00466 
00467   test_textentry->Run();
00468 
00469   delete test_thread;
00470   delete test_textentry;
00471 
00472   return 0;
00473 }
00474