Back to index

nux  3.0.0
xtest-keynav-directions.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 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/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 class KeyNavDirectionTest: public ProgramTemplate
00031 {
00032 public:
00033   KeyNavDirectionTest(const char *program_name, int window_width, int window_height, int program_life_span);
00034   ~KeyNavDirectionTest();
00035 
00036   virtual void UserInterfaceSetup();
00037   
00038   nux::TestView* view0_;
00039   nux::TestView* view1_;
00040   nux::TestView* view2_;
00041   nux::TestView* view3_;
00042 
00043 };
00044 
00045 KeyNavDirectionTest::KeyNavDirectionTest(const char* program_name,
00046                                            int window_width,
00047                                            int window_height,
00048                                            int program_life_span)
00049   : ProgramTemplate(program_name, window_width, window_height, program_life_span)
00050   , view0_(NULL)
00051   , view1_(NULL)
00052   , view2_(NULL)
00053   , view3_(NULL)  
00054 {
00055 }
00056 
00057 KeyNavDirectionTest::~KeyNavDirectionTest()
00058 {
00059 }
00060 
00061 void KeyNavDirectionTest::UserInterfaceSetup()
00062 {
00063   nux::HLayout* main_layout = new nux::HLayout(NUX_TRACKER_LOCATION);
00064   main_layout->SetSpaceBetweenChildren(10);
00065   main_layout->SetPadding(10, 10);
00066   
00067   nux::VLayout* layout0 = new nux::VLayout(NUX_TRACKER_LOCATION);
00068   nux::VLayout* layout1 = new nux::VLayout(NUX_TRACKER_LOCATION);
00069   layout0->SetSpaceBetweenChildren(10);
00070   layout1->SetSpaceBetweenChildren(10);
00071 
00072   view0_ = new nux::TestView(NUX_TRACKER_LOCATION);
00073   view0_->can_focus_ = true;
00074   view0_->SetAcceptKeyNavFocusOnMouseDown(true);
00075   layout0->AddView(view0_, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00076     
00077   view1_ = new nux::TestView(NUX_TRACKER_LOCATION);
00078   view1_->can_focus_ = true;
00079   view1_->SetAcceptKeyNavFocusOnMouseDown(true);
00080   layout0->AddView(view1_, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00081 
00082   view2_ = new nux::TestView(NUX_TRACKER_LOCATION);
00083   view2_->can_focus_ = true;
00084   view2_->SetAcceptKeyNavFocusOnMouseDown(true);
00085   layout1->AddView(view2_, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00086     
00087   view3_ = new nux::TestView(NUX_TRACKER_LOCATION);
00088   view3_->can_focus_ = true;
00089   view3_->SetAcceptKeyNavFocusOnMouseDown(true);
00090   layout1->AddView(view3_, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00091 
00092   main_layout->AddLayout(layout0, 1);
00093   main_layout->AddLayout(layout1, 1);
00094 
00095   static_cast<nux::WindowThread*>(window_thread_)->SetLayout(main_layout);
00096   
00097   nux::ColorLayer background(nux::Color(0xFF4D4D4D));
00098   static_cast<nux::WindowThread*>(window_thread_)->SetWindowBackgroundPaintLayer(&background);
00099 }
00100 
00101 KeyNavDirectionTest* keynav_direction_test = NULL;
00102 
00103 void TestingThread(nux::NThread* thread, void* user_data)
00104 {
00105   while (keynav_direction_test->ReadyToGo() == false)
00106   {
00107     nuxDebugMsg("Waiting to start");
00108     nux::SleepForMilliseconds(300);
00109   }
00110 
00111   nux::SleepForMilliseconds(1000);
00112 
00113   nux::WindowThread *wnd_thread = static_cast<nux::WindowThread*>(user_data);
00114 
00115   NuxAutomatedTestFramework test(wnd_thread);
00116 
00117   test.Startup();
00118 
00119   // Set the mouse at coordinates (0, 0) (top-left corner) on the display
00120   test.PutMouseAt(0, 0);
00121 
00122   // Move mouse to center of view0_
00123   test.ViewSendMouseMotionToCenter(keynav_direction_test->view0_);
00124 
00125   // Mouse down/up on view0_
00126   test.ViewSendMouseClick(keynav_direction_test->view0_, 1);
00127   
00128   nux::SleepForMilliseconds(500);
00129   test.SendFakeKeyEvent(XK_Down, 0);
00130   nux::SleepForMilliseconds(500);
00131   test.TestReportMsg(keynav_direction_test->view1_->has_focus_ && (keynav_direction_test->view1_->key_nav_direction_ == nux::KEY_NAV_DOWN),
00132    "View1 received key nav from top");
00133   
00134   test.SendFakeKeyEvent(XK_Up, 0);
00135   nux::SleepForMilliseconds(500);
00136   test.TestReportMsg(keynav_direction_test->view0_->has_focus_ && (keynav_direction_test->view0_->key_nav_direction_ == nux::KEY_NAV_UP),
00137    "View0 received key nav from bottom");
00138     
00139   test.SendFakeKeyEvent(XK_Right, 0);
00140   nux::SleepForMilliseconds(500);
00141   test.TestReportMsg(keynav_direction_test->view2_->has_focus_ && (keynav_direction_test->view0_->key_nav_direction_ == nux::KEY_NAV_RIGHT),
00142    "View2 received key nav from left");
00143    
00144   test.SendFakeKeyEvent(XK_Down, 0);
00145   nux::SleepForMilliseconds(500);
00146   test.TestReportMsg(keynav_direction_test->view3_->has_focus_ && (keynav_direction_test->view3_->key_nav_direction_ == nux::KEY_NAV_DOWN),
00147    "View3 received key nav from top");
00148    
00149   test.SendFakeKeyEvent(XK_Up, 0);
00150   nux::SleepForMilliseconds(500);
00151   test.TestReportMsg(keynav_direction_test->view2_->has_focus_ && (keynav_direction_test->view2_->key_nav_direction_ == nux::KEY_NAV_UP),
00152    "View2 received key nav from bottom");
00153    
00154   test.SendFakeKeyEvent(XK_Left, 0);
00155   nux::SleepForMilliseconds(500);
00156   test.TestReportMsg(keynav_direction_test->view0_->has_focus_ && (keynav_direction_test->view0_->key_nav_direction_ == nux::KEY_NAV_LEFT),
00157    "View0 received key nav from right");
00158                 
00159   if (test.WhenDoneTerminateProgram())
00160   {
00161     nux::SleepForMilliseconds(1000);
00162     wnd_thread->ExitMainLoop();
00163   }
00164   nuxDebugMsg("Exit testing thread");
00165 }
00166 
00167 int main(int argc, char **argv)
00168 {
00169   int xstatus = XInitThreads();
00170   nuxAssertMsg(xstatus > 0, "XInitThreads has failed");
00171 
00172   keynav_direction_test = new KeyNavDirectionTest("Automated: KeyNav Directions", 300, 300, 15000);
00173   keynav_direction_test->Startup();
00174   keynav_direction_test->UserInterfaceSetup();
00175 
00176   nux::SystemThread *test_thread = nux::CreateSystemThread(keynav_direction_test->GetWindowThread(), 
00177                                                            &TestingThread,
00178                                                            keynav_direction_test->GetWindowThread());
00179 
00180   test_thread->Start(keynav_direction_test);
00181 
00182   keynav_direction_test->Run();
00183 
00184   delete test_thread;
00185   delete keynav_direction_test;
00186 
00187   return 0;
00188 }
00189