Back to index

nux  3.0.0
xtest-hgrid-key-navigation.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 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: Andrea Azzarone <azzaronea@gmail.com>
00018  *
00019  */
00020  
00021 #include "Nux/Nux.h"
00022 #include "Nux/WindowThread.h"
00023 #include "Nux/GridHLayout.h"
00024 #include "Nux/ProgramFramework/ProgramTemplate.h"
00025 #include "Nux/ProgramFramework/TestView.h"
00026 #include <X11/extensions/XTest.h>
00027 #include <X11/keysym.h> 
00028 #include "nux_automated_test_framework.h"
00029 
00030 
00031 // 0  1  2  3
00032 // 4  5  6  7
00033 // 8  9 10 
00034 
00035 class HGridKeyNavigationTest: public ProgramTemplate
00036 {
00037 public:
00038   HGridKeyNavigationTest(const char* program_name, int window_width, int window_height, int program_life_span);
00039   ~HGridKeyNavigationTest();
00040 
00041   virtual void UserInterfaceSetup();
00042   
00043   nux::TestView* CreateTile();
00044   void AddTile(nux::TestView* tile);
00045   
00046   nux::GridHLayout* grid_layout_;
00047   nux::TestView* tiles_[11];
00048 };
00049 
00050 HGridKeyNavigationTest::HGridKeyNavigationTest(const char* program_name,
00051                                                int window_width,
00052                                                int window_height,
00053                                                int program_life_span)
00054 : ProgramTemplate(program_name, window_width, window_height, program_life_span)
00055 {
00056 }
00057 
00058 HGridKeyNavigationTest::~HGridKeyNavigationTest()
00059 {
00060 }
00061 
00062 nux::TestView* HGridKeyNavigationTest::CreateTile()
00063 {
00064   nux::TestView* tile = new nux::TestView();
00065   return tile;
00066 }
00067 
00068 void HGridKeyNavigationTest::AddTile(nux::TestView* tile)
00069 {
00070   grid_layout_->AddView(tile, 1);
00071 }
00072 
00073 void HGridKeyNavigationTest::UserInterfaceSetup()
00074 {  
00075   grid_layout_ = new nux::GridHLayout(NUX_TRACKER_LOCATION);
00076   grid_layout_->ForceChildrenSize(true);
00077   grid_layout_->SetChildrenSize(100, 100);
00078   grid_layout_->EnablePartialVisibility(false);
00079   grid_layout_->SetPadding(20, 20);
00080   grid_layout_->SetSpaceBetweenChildren(10, 10);
00081   
00082   for (int i=0; i<11; ++i)
00083   {
00084     tiles_[i] = CreateTile();
00085     AddTile(tiles_[i]);
00086   }
00087   
00088   nux::GetWindowCompositor().SetKeyFocusArea(tiles_[0]);
00089   
00090   static_cast<nux::WindowThread*>(window_thread_)->SetLayout(grid_layout_);
00091   
00092   nux::ColorLayer background(nux::Color(0xFF4D4D4D));
00093   static_cast<nux::WindowThread*>(window_thread_)->SetWindowBackgroundPaintLayer(&background);
00094 }
00095 
00096 HGridKeyNavigationTest* key_navigation_test = NULL;
00097 
00098 void TestingThread(nux::NThread* thread, void* user_data)
00099 {
00100   while (key_navigation_test->ReadyToGo() == false)
00101   {
00102     nuxDebugMsg("Waiting to start");
00103     nux::SleepForMilliseconds(300);
00104   }
00105 
00106   nux::SleepForMilliseconds(1000);
00107 
00108   nux::WindowThread* wnd_thread = static_cast<nux::WindowThread*>(user_data);
00109 
00110   NuxAutomatedTestFramework test(wnd_thread);
00111 
00112   test.Startup();
00113 
00114   // Set the mouse at coordinates (0, 0) (top-left corner) on the display
00115   test.PutMouseAt(0, 0);
00116 
00117   test.TestReportMsg(key_navigation_test->grid_layout_, "TestView created");
00118   test.TestReportMsg(key_navigation_test->tiles_[0]->has_focus_, "Top left tile has key focus");
00119 
00120 
00121   // Rigth key
00122   for (int i=0; i<3; ++i)
00123   {
00124     test.SendFakeKeyEvent(XK_Right, 0);
00125     nux::SleepForMilliseconds(500);
00126     test.TestReportMsg(!key_navigation_test->tiles_[i]->has_focus_, "Right: key focus out");
00127     test.TestReportMsg(key_navigation_test->tiles_[i+1]->has_focus_, "Right: key focus in");
00128   }
00129   
00130   // Another right key, should do nothing
00131   test.SendFakeKeyEvent(XK_Right, 0);
00132   nux::SleepForMilliseconds(500);
00133   test.TestReportMsg(key_navigation_test->tiles_[3]->has_focus_, "Right key, last element");
00134   
00135   // Left key
00136   for (int i=3; i>0; --i)
00137   {
00138     test.SendFakeKeyEvent(XK_Left, 0);
00139     nux::SleepForMilliseconds(500);
00140     test.TestReportMsg(!key_navigation_test->tiles_[i]->has_focus_, "Left: key focus out");
00141     test.TestReportMsg(key_navigation_test->tiles_[i-1]->has_focus_, "Left: key focus in");
00142   }
00143   
00144   // Another left key, should do nothing
00145   test.SendFakeKeyEvent(XK_Left, 0);
00146   nux::SleepForMilliseconds(500);
00147   test.TestReportMsg(key_navigation_test->tiles_[0]->has_focus_, "Left key, fist element");
00148   
00149   // Down key
00150   for (int i=0; i<2; ++i)
00151   {
00152     test.SendFakeKeyEvent(XK_Down, 0);
00153     nux::SleepForMilliseconds(500);
00154     test.TestReportMsg(!key_navigation_test->tiles_[4*i]->has_focus_, "Down: key focus out");
00155     test.TestReportMsg(key_navigation_test->tiles_[4*(i+1)]->has_focus_, "Down: key focus in");
00156   }
00157   
00158   // Another down key, should do nothing
00159   test.SendFakeKeyEvent(XK_Down, 0);
00160   nux::SleepForMilliseconds(500);
00161   test.TestReportMsg(key_navigation_test->tiles_[8]->has_focus_, "Down key, last row");
00162   
00163   // Up key
00164   for (int i=2; i>0; --i)
00165   {
00166     test.SendFakeKeyEvent(XK_Up, 0);
00167     nux::SleepForMilliseconds(500);
00168     test.TestReportMsg(!key_navigation_test->tiles_[4*i]->has_focus_, "Up: key focus out");
00169     test.TestReportMsg(key_navigation_test->tiles_[4*(i-1)]->has_focus_, "Up: key focus in");
00170   }
00171   
00172   // Another down key, should do nothing
00173   test.SendFakeKeyEvent(XK_Up, 0);
00174   nux::SleepForMilliseconds(500);
00175   test.TestReportMsg(key_navigation_test->tiles_[0]->has_focus_, "Up key, first row");
00176 
00177   if (test.WhenDoneTerminateProgram())
00178   {
00179     nux::SleepForMilliseconds(1000);
00180     wnd_thread->ExitMainLoop();
00181   }
00182   nuxDebugMsg("Exit testing thread");
00183 }
00184 
00185 int main(int argc, char** argv)
00186 {
00187   int xstatus = XInitThreads();
00188   nuxAssertMsg(xstatus > 0, "XInitThreads has failed");
00189 
00190   key_navigation_test = new HGridKeyNavigationTest("Key navigation Test", 500, 400, 20000);
00191   key_navigation_test->Startup();
00192   key_navigation_test->UserInterfaceSetup();
00193 
00194   nux::SystemThread* test_thread = nux::CreateSystemThread(key_navigation_test->GetWindowThread(), &TestingThread, key_navigation_test->GetWindowThread());
00195 
00196   test_thread->Start(key_navigation_test);
00197 
00198   key_navigation_test->Run();
00199 
00200   delete test_thread;
00201   delete key_navigation_test;
00202 
00203   return 0;
00204 }
00205