Back to index

nux  3.0.0
xtest-hlayout-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/HLayout.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 unsigned int const NUM_VIEWS = 5;
00031 unsigned int const ID_UNFOCUSABLE_VIEW = 2; // Please don't make this the last/first one :)
00032 
00033 class HLayoutKeyNavigationTest : public ProgramTemplate
00034 {
00035 public:
00036   HLayoutKeyNavigationTest(const char* program_name, int window_width, int window_height, int program_life_span);
00037   ~HLayoutKeyNavigationTest();
00038 
00039   virtual void UserInterfaceSetup();
00040   
00041   nux::TestView* CreateView();
00042   void AddView(nux::TestView* view);
00043   
00044   nux::HLayout* hlayout_;
00045   nux::TestView* views_[NUM_VIEWS];
00046 };
00047 
00048 HLayoutKeyNavigationTest::HLayoutKeyNavigationTest(const char* program_name,
00049                                                    int window_width,
00050                                                    int window_height,
00051                                                    int program_life_span)
00052 : ProgramTemplate(program_name, window_width, window_height, program_life_span)
00053 {
00054 }
00055 
00056 HLayoutKeyNavigationTest::~HLayoutKeyNavigationTest()
00057 {
00058 }
00059 
00060 nux::TestView* HLayoutKeyNavigationTest::CreateView()
00061 {
00062   nux::TestView* view = new nux::TestView();
00063   return view;
00064 }
00065 
00066 void HLayoutKeyNavigationTest::AddView(nux::TestView* view)
00067 {
00068   hlayout_->AddView(view, 1);
00069 }
00070 
00071 void HLayoutKeyNavigationTest::UserInterfaceSetup()
00072 {  
00073   hlayout_ = new nux::HLayout(NUX_TRACKER_LOCATION);
00074   hlayout_->SetPadding(20, 20);
00075   hlayout_->SetSpaceBetweenChildren(10);
00076   
00077   for (unsigned int i=0; i<NUM_VIEWS; ++i)
00078   {
00079     views_[i] = CreateView();
00080     AddView(views_[i]);
00081   }
00082   
00083   views_[ID_UNFOCUSABLE_VIEW]->can_focus_ = false;
00084   nux::GetWindowCompositor().SetKeyFocusArea(views_[0]);
00085   
00086   static_cast<nux::WindowThread*>(window_thread_)->SetLayout(hlayout_);
00087   
00088   nux::ColorLayer background(nux::Color(0xFF4D4D4D));
00089   static_cast<nux::WindowThread*>(window_thread_)->SetWindowBackgroundPaintLayer(&background);
00090 }
00091 
00092 HLayoutKeyNavigationTest* key_navigation_test = NULL;
00093 
00094 void TestingThread(nux::NThread* thread, void* user_data)
00095 {
00096   while (key_navigation_test->ReadyToGo() == false)
00097   {
00098     nuxDebugMsg("Waiting to start");
00099     nux::SleepForMilliseconds(300);
00100   }
00101 
00102   nux::SleepForMilliseconds(1000);
00103 
00104   nux::WindowThread* wnd_thread = static_cast<nux::WindowThread*>(user_data);
00105 
00106   NuxAutomatedTestFramework test(wnd_thread);
00107 
00108   test.Startup();
00109 
00110   // Set the mouse at coordinates (0, 0) (top-left corner) on the display
00111   test.PutMouseAt(0, 0);
00112 
00113   test.TestReportMsg(key_navigation_test->hlayout_, "TestView created");
00114   test.TestReportMsg(key_navigation_test->views_[0]->has_focus_, "Top left tile has key focus");
00115   
00116   // Right key
00117   for (unsigned int i=0; i<NUM_VIEWS-1; ++i)
00118   {
00119     test.SendFakeKeyEvent(XK_Right, 0);
00120     nux::SleepForMilliseconds(500);
00121     test.TestReportMsg(!key_navigation_test->views_[i]->has_focus_, "Right: key focus out");
00122     if ((i+1) == ID_UNFOCUSABLE_VIEW)
00123     {
00124       test.TestReportMsg(!key_navigation_test->views_[i+1]->has_focus_, "Right: key focus skipped");
00125       test.TestReportMsg(key_navigation_test->views_[i+2]->has_focus_, "Right: key focus in");
00126       ++i;
00127     }
00128     else
00129     {
00130       test.TestReportMsg(key_navigation_test->views_[i+1]->has_focus_, "Right: key focus in");
00131     }
00132       
00133   }
00134   
00135   // Another right key, should do nothing
00136   test.SendFakeKeyEvent(XK_Right, 0);
00137   nux::SleepForMilliseconds(500);
00138   test.TestReportMsg(key_navigation_test->views_[NUM_VIEWS-1]->has_focus_, "Rigth key, last element");
00139   
00140   // Left key
00141   for (unsigned int i=NUM_VIEWS-1; i>0; --i)
00142   {
00143     test.SendFakeKeyEvent(XK_Left, 0);
00144     nux::SleepForMilliseconds(500);
00145     test.TestReportMsg(!key_navigation_test->views_[i]->has_focus_, "Left: key focus out");
00146     if ((i-1) == ID_UNFOCUSABLE_VIEW)
00147     {
00148       test.TestReportMsg(!key_navigation_test->views_[i-1]->has_focus_, "Left: key focus skipped");
00149       test.TestReportMsg(key_navigation_test->views_[i-2]->has_focus_, "Left: key focus in");
00150       --i;
00151     }
00152     else
00153     {
00154       test.TestReportMsg(key_navigation_test->views_[i-1]->has_focus_, "Left: key focus in");
00155     }
00156   }
00157   
00158   // Another Left key, should do nothing
00159   test.SendFakeKeyEvent(XK_Left, 0);
00160   nux::SleepForMilliseconds(500);
00161   test.TestReportMsg(key_navigation_test->views_[0]->has_focus_, "Left key, fist element");
00162   
00163    // Up key, should do nothing
00164   test.SendFakeKeyEvent(XK_Up, 0);
00165   nux::SleepForMilliseconds(500);
00166   test.TestReportMsg(key_navigation_test->views_[0]->has_focus_, "Up key");
00167   
00168    // Down key, should do nothing
00169   test.SendFakeKeyEvent(XK_Down, 0);
00170   nux::SleepForMilliseconds(500);
00171   test.TestReportMsg(key_navigation_test->views_[0]->has_focus_, "Down key");
00172 
00173 
00174   if (test.WhenDoneTerminateProgram())
00175   {
00176     nux::SleepForMilliseconds(1000);
00177     wnd_thread->ExitMainLoop();
00178   }
00179   nuxDebugMsg("Exit testing thread");
00180 }
00181 
00182 int main(int argc, char** argv)
00183 {
00184   int xstatus = XInitThreads();
00185   nuxAssertMsg(xstatus > 0, "XInitThreads has failed");
00186 
00187   key_navigation_test = new HLayoutKeyNavigationTest("Key navigation Test", 500, 400, 8000);
00188   key_navigation_test->Startup();
00189   key_navigation_test->UserInterfaceSetup();
00190 
00191   nux::SystemThread* test_thread = nux::CreateSystemThread(key_navigation_test->GetWindowThread(), &TestingThread, key_navigation_test->GetWindowThread());
00192 
00193   test_thread->Start(key_navigation_test);
00194 
00195   key_navigation_test->Run();
00196 
00197   delete test_thread;
00198   delete key_navigation_test;
00199 
00200   return 0;
00201 }
00202