Back to index

nux  3.0.0
xtest-vlayout-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/VLayout.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 = 1; // Please don't make this the last/first one :)
00032 
00033 class VLayoutKeyNavigationTest : public ProgramTemplate
00034 {
00035 public:
00036   VLayoutKeyNavigationTest(const char* program_name, int window_width, int window_height, int program_life_span);
00037   ~VLayoutKeyNavigationTest();
00038 
00039   virtual void UserInterfaceSetup();
00040   
00041   nux::TestView* CreateView();
00042   void AddView(nux::TestView* view);
00043   
00044   nux::VLayout* vlayout_;
00045   nux::TestView* views_[NUM_VIEWS];
00046 };
00047 
00048 VLayoutKeyNavigationTest::VLayoutKeyNavigationTest(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 VLayoutKeyNavigationTest::~VLayoutKeyNavigationTest()
00057 {
00058 }
00059 
00060 nux::TestView* VLayoutKeyNavigationTest::CreateView()
00061 {
00062   nux::TestView* view = new nux::TestView();
00063   return view;
00064 }
00065 
00066 void VLayoutKeyNavigationTest::AddView(nux::TestView* view)
00067 {
00068   vlayout_->AddView(view, 1);
00069 }
00070 
00071 void VLayoutKeyNavigationTest::UserInterfaceSetup()
00072 {  
00073   vlayout_ = new nux::VLayout(NUX_TRACKER_LOCATION);
00074   vlayout_->SetPadding(20, 20);
00075   vlayout_->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(vlayout_);
00087   
00088   nux::ColorLayer background(nux::Color(0xFF4D4D4D));
00089   static_cast<nux::WindowThread*>(window_thread_)->SetWindowBackgroundPaintLayer(&background);
00090 }
00091 
00092 VLayoutKeyNavigationTest* 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->vlayout_, "TestView created");
00114   test.TestReportMsg(key_navigation_test->views_[0]->has_focus_, "Top left tile has key focus");
00115   
00116   // Down key
00117   for (int i=0; i<NUM_VIEWS-1; ++i)
00118   {
00119     test.SendFakeKeyEvent(XK_Down, 0);
00120     nux::SleepForMilliseconds(500);
00121     test.TestReportMsg(!key_navigation_test->views_[i]->has_focus_, "Down: key focus out");
00122     if ((i + 1) == ID_UNFOCUSABLE_VIEW)
00123     {
00124       test.TestReportMsg(!key_navigation_test->views_[i+1]->has_focus_, "Down: key focus skipped");
00125       test.TestReportMsg(key_navigation_test->views_[i+2]->has_focus_, "Down: key focus in");
00126       ++i;
00127     }
00128     else
00129     {
00130       test.TestReportMsg(key_navigation_test->views_[i+1]->has_focus_, "Down: key focus in");
00131     }
00132   }
00133   
00134   // Another down key, should do nothing
00135   test.SendFakeKeyEvent(XK_Down, 0);
00136   nux::SleepForMilliseconds(500);
00137   test.TestReportMsg(key_navigation_test->views_[NUM_VIEWS-1]->has_focus_, "Down key, last element");
00138   
00139   // Up key
00140   for (int i=NUM_VIEWS-1; i>0; --i)
00141   {
00142     test.SendFakeKeyEvent(XK_Up, 0);
00143     nux::SleepForMilliseconds(500);
00144     test.TestReportMsg(!key_navigation_test->views_[i]->has_focus_, "Up: key focus out");
00145     if ((i - 1) == ID_UNFOCUSABLE_VIEW)
00146     {
00147       test.TestReportMsg(!key_navigation_test->views_[i-1]->has_focus_, "Up: key focus skipped");
00148       test.TestReportMsg(key_navigation_test->views_[i-2]->has_focus_, "Up: key focus in");
00149       --i;
00150     }
00151     else
00152     {
00153       test.TestReportMsg(key_navigation_test->views_[i-1]->has_focus_, "Up: key focus in");
00154     }
00155   }
00156   
00157   // Another up key, should do nothing
00158   test.SendFakeKeyEvent(XK_Up, 0);
00159   nux::SleepForMilliseconds(500);
00160   test.TestReportMsg(key_navigation_test->views_[0]->has_focus_, "Up key, fist element");
00161   
00162    // Keft key, should do nothing
00163   test.SendFakeKeyEvent(XK_Left, 0);
00164   nux::SleepForMilliseconds(500);
00165   test.TestReportMsg(key_navigation_test->views_[0]->has_focus_, "Left key");
00166   
00167    // Right key, should do nothing
00168   test.SendFakeKeyEvent(XK_Right, 0);
00169   nux::SleepForMilliseconds(500);
00170   test.TestReportMsg(key_navigation_test->views_[0]->has_focus_, "Right key");
00171 
00172 
00173   if (test.WhenDoneTerminateProgram())
00174   {
00175     nux::SleepForMilliseconds(1000);
00176     wnd_thread->ExitMainLoop();
00177   }
00178   nuxDebugMsg("Exit testing thread");
00179 }
00180 
00181 int main(int argc, char** argv)
00182 {
00183   int xstatus = XInitThreads();
00184   nuxAssertMsg(xstatus > 0, "XInitThreads has failed");
00185 
00186   key_navigation_test = new VLayoutKeyNavigationTest("Key navigation Test", 500, 400, 8000);
00187   key_navigation_test->Startup();
00188   key_navigation_test->UserInterfaceSetup();
00189 
00190   nux::SystemThread* test_thread = nux::CreateSystemThread(key_navigation_test->GetWindowThread(), &TestingThread, key_navigation_test->GetWindowThread());
00191 
00192   test_thread->Start(key_navigation_test);
00193 
00194   key_navigation_test->Run();
00195 
00196   delete test_thread;
00197   delete key_navigation_test;
00198 
00199   return 0;
00200 }
00201