Back to index

nux  3.0.0
xtest-text-entry-logic.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 "Nux/ProgramFramework/TestTextEntry.h"
00029 #include <X11/extensions/XTest.h>
00030 #include <X11/keysym.h> 
00031 #include "nux_automated_test_framework.h"
00032 
00033 class TestTextEntry: public ProgramTemplate
00034 {
00035 public:
00036   TestTextEntry(const char* program_name, int window_width, int window_height, int program_life_span);
00037   ~TestTextEntry();
00038 
00039   virtual void UserInterfaceSetup();
00040 
00041   void ResetEvents();
00042 
00043   nux::TestTextEntry* text_entry_;
00044 
00045 };
00046 
00047 TestTextEntry::TestTextEntry(const char* program_name,
00048   int window_width,
00049   int window_height,
00050   int program_life_span)
00051   : ProgramTemplate(program_name, window_width, window_height, program_life_span)
00052   , text_entry_(NULL)
00053 {
00054   ResetEvents();
00055 }
00056 
00057 TestTextEntry::~TestTextEntry()
00058 {
00059   
00060 }
00061 
00062 void TestTextEntry::ResetEvents()
00063 {
00064   if (text_entry_)
00065     text_entry_->ResetEvents();
00066 }
00067 
00068 void TestTextEntry::UserInterfaceSetup()
00069 {
00070   nux::VLayout* main_layout = new nux::VLayout(NUX_TRACKER_LOCATION);
00071   text_entry_ = new nux::TestTextEntry(NUX_TRACKER_LOCATION);
00072   text_entry_->SetFontSize(76);
00073 
00074   main_layout->AddView(text_entry_, 0, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00075   main_layout->SetSpaceBetweenChildren(10);
00076   main_layout->SetContentDistribution(nux::MAJOR_POSITION_CENTER);
00077 
00078   static_cast<nux::WindowThread*>(window_thread_)->SetLayout(main_layout);
00079   
00080   nux::ColorLayer background(nux::Color(0xFF4D4D4D));
00081   static_cast<nux::WindowThread*>(window_thread_)->SetWindowBackgroundPaintLayer(&background);
00082 }
00083 
00084 TestTextEntry* test_textentry = NULL;
00085 
00086 void TestingThread(nux::NThread* thread, void* user_data)
00087 {
00088   while (test_textentry->ReadyToGo() == false)
00089   {
00090     nuxDebugMsg("Waiting to start");
00091     nux::SleepForMilliseconds(300);
00092   }
00093 
00094   nux::SleepForMilliseconds(1300);
00095 
00096   nux::WindowThread* wnd_thread = static_cast<nux::WindowThread*>(user_data);
00097 
00098   NuxAutomatedTestFramework test(wnd_thread);
00099 
00100   test.Startup();
00101 
00102   test.TestReportMsg(test_textentry->text_entry_, "TextEntry created");
00103 
00104   test_textentry->ResetEvents();
00105   test.ViewSendMouseMotionToCenter(test_textentry->text_entry_);
00106 
00107   test.TestReportMsg(test_textentry->text_entry_->HasKeyFocus() == false, "Focus test");
00108   test.TestReportMsg(test_textentry->text_entry_->IsInTextInputMode() == false, "Text input mode test");
00109   test.ViewSendMouseClick(0, 1);
00110   test.TestReportMsg(test_textentry->text_entry_->HasKeyFocus() == true, "Focus test");
00111   test.TestReportMsg(test_textentry->text_entry_->IsInTextInputMode() == false, "Text input mode test");
00112 
00113   // Type "Nux"
00114   // The cursor is at the end of the line
00115   // Simulate CTRL+A to select the entire text
00116   // Simulate DELETE key to delete the text
00117   {
00118     test.ViewSendString("Nux");
00119     test.TestReportMsg(test_textentry->text_entry_->GetText() == "Nux", "Typed \"Nux\"");
00120 
00121     test.TestReportMsg(test_textentry->text_entry_->IsInTextInputMode() == true, "Text input mode test");
00122 
00123     test.ViewSendCtrlA();
00124     nux::SleepForMilliseconds(500);
00125     test.TestReportMsg(test_textentry->text_entry_->GetTextSelection() == "Nux", "Selection is \"Nux\"");
00126 
00127     test.ViewSendDelete();
00128     nux::SleepForMilliseconds(500);
00129     test.TestReportMsg(test_textentry->text_entry_->GetText() == "", "TextEntry is empty");
00130   }
00131 
00132   if (test.WhenDoneTerminateProgram())
00133   {
00134     wnd_thread->ExitMainLoop();
00135   }
00136   nuxDebugMsg("Exit testing thread");
00137 }
00138 
00139 int main(int argc, char** argv)
00140 {
00141   int xstatus = XInitThreads();
00142   nuxAssertMsg(xstatus > 0, "XInitThreads has failed");
00143 
00144   test_textentry = new TestTextEntry("Text Entry", 600, 200, 10000);
00145   test_textentry->Startup();
00146   test_textentry->UserInterfaceSetup();
00147 
00148   nux::SystemThread* test_thread = nux::CreateSystemThread(NULL, &TestingThread, test_textentry->GetWindowThread());
00149 
00150   test_thread->Start(test_textentry);
00151 
00152   test_textentry->Run();
00153 
00154   delete test_thread;
00155   delete test_textentry;
00156 
00157   return 0;
00158 }
00159