Back to index

nux  3.0.0
xtest-focus-on-mouse-down.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 class FocusOnMouseDownTest: public ProgramTemplate
00031 {
00032 public:
00033   FocusOnMouseDownTest(const char* program_name, int window_width, int window_height, int program_life_span);
00034   ~FocusOnMouseDownTest();
00035 
00036   virtual void UserInterfaceSetup();
00037   
00038   nux::TestView* focus_view_;
00039   nux::TestView* no_focus_view_;
00040 };
00041 
00042 FocusOnMouseDownTest::FocusOnMouseDownTest(const char* program_name,
00043                                            int window_width,
00044                                            int window_height,
00045                                            int program_life_span)
00046 : ProgramTemplate(program_name, window_width, window_height, program_life_span)
00047 , focus_view_(nullptr)
00048 , no_focus_view_(nullptr)
00049 {
00050 }
00051 
00052 FocusOnMouseDownTest::~FocusOnMouseDownTest()
00053 {
00054 }
00055 
00056 void FocusOnMouseDownTest::UserInterfaceSetup()
00057 {
00058   nux::VLayout* main_layout = new nux::VLayout(NUX_TRACKER_LOCATION);
00059   main_layout->SetSpaceBetweenChildren(10);
00060   main_layout->SetPadding(10, 10);
00061   
00062   focus_view_ = new nux::TestView(NUX_TRACKER_LOCATION);
00063   focus_view_->can_focus_ = true;
00064   focus_view_->SetAcceptKeyNavFocusOnMouseDown(true);
00065   main_layout->AddView(focus_view_, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00066     
00067   no_focus_view_ = new nux::TestView(NUX_TRACKER_LOCATION);
00068   no_focus_view_->can_focus_ = true;
00069   no_focus_view_->SetAcceptKeyNavFocusOnMouseDown(false);
00070   main_layout->AddView(no_focus_view_, 1, nux::MINOR_POSITION_CENTER, nux::MINOR_SIZE_FULL);
00071 
00072   static_cast<nux::WindowThread*>(window_thread_)->SetLayout(main_layout);
00073   
00074   nux::ColorLayer background(nux::Color(0xFF4D4D4D));
00075   static_cast<nux::WindowThread*>(window_thread_)->SetWindowBackgroundPaintLayer(&background);
00076 }
00077 
00078 FocusOnMouseDownTest* focus_on_mouse_down_test = nullptr;
00079 
00080 void TestingThread(nux::NThread* thread, void* user_data)
00081 {
00082   while (focus_on_mouse_down_test->ReadyToGo() == false)
00083   {
00084     nuxDebugMsg("Waiting to start");
00085     nux::SleepForMilliseconds(300);
00086   }
00087 
00088   nux::SleepForMilliseconds(1000);
00089 
00090   nux::WindowThread* wnd_thread = static_cast<nux::WindowThread*>(user_data);
00091 
00092   NuxAutomatedTestFramework test(wnd_thread);
00093 
00094   test.Startup();
00095 
00096   // Set the mouse at coordinates (0, 0) (top-left corner) on the display
00097   test.PutMouseAt(0, 0);
00098 
00099   test.TestReportMsg(focus_on_mouse_down_test->focus_view_, "Focus view created");
00100   test.TestReportMsg(focus_on_mouse_down_test->no_focus_view_, "No focus view created");
00101 
00102   // Move mouse to center of focus_view
00103   test.ViewSendMouseMotionToCenter(focus_on_mouse_down_test->focus_view_);
00104 
00105   // Mouse down/up on focus_view_
00106   test.ViewSendMouseClick(focus_on_mouse_down_test->focus_view_, 1);
00107   nux::SleepForMilliseconds(500);
00108   test.TestReportMsg(focus_on_mouse_down_test->focus_view_->has_focus_, "Mouse down: focus_view_ got the focus");
00109   test.ViewSendMouseUp(focus_on_mouse_down_test->focus_view_, 1);
00110   nux::SleepForMilliseconds(500);
00111   test.TestReportMsg(focus_on_mouse_down_test->focus_view_->has_focus_, "Mouse up: focus is still on focus_view_");
00112   
00113    // Move mouse to center of no_focus_view
00114   test.ViewSendMouseMotionToCenter(focus_on_mouse_down_test->no_focus_view_);
00115 
00116   // Mouse down/up on no_focus_view_
00117   test.ViewSendMouseDown(focus_on_mouse_down_test->no_focus_view_, 1);
00118   nux::SleepForMilliseconds(500);
00119   test.TestReportMsg(!focus_on_mouse_down_test->no_focus_view_->has_focus_, "Mouse down: no_focus_view_ did not take the focus");
00120   test.TestReportMsg(focus_on_mouse_down_test->focus_view_->has_focus_, "Mouse down: focus is still on focus_view_");
00121   test.ViewSendMouseUp(focus_on_mouse_down_test->no_focus_view_, 1);
00122   nux::SleepForMilliseconds(500);
00123   test.TestReportMsg(!focus_on_mouse_down_test->no_focus_view_->has_focus_, "Mouse up: no_focus_view_ still doesn't have the focus");
00124   test.TestReportMsg(focus_on_mouse_down_test->focus_view_->has_focus_, "Mouse up: focus is still on focus_view_");
00125   
00126 
00127   if (test.WhenDoneTerminateProgram())
00128   {
00129     nux::SleepForMilliseconds(1000);
00130     wnd_thread->ExitMainLoop();
00131   }
00132   nuxDebugMsg("Exit testing thread");
00133 }
00134 
00135 int main(int argc, char** argv)
00136 {
00137   int xstatus = XInitThreads();
00138   nuxAssertMsg(xstatus > 0, "XInitThreads has failed");
00139 
00140   focus_on_mouse_down_test = new FocusOnMouseDownTest("Focus On Mouse Down Test", 300, 300, 15000);
00141   focus_on_mouse_down_test->Startup();
00142   focus_on_mouse_down_test->UserInterfaceSetup();
00143 
00144   nux::SystemThread* test_thread = nux::CreateSystemThread(focus_on_mouse_down_test->GetWindowThread(), 
00145                                                            &TestingThread,
00146                                                            focus_on_mouse_down_test->GetWindowThread());
00147 
00148   test_thread->Start(focus_on_mouse_down_test);
00149 
00150   focus_on_mouse_down_test->Run();
00151 
00152   delete test_thread;
00153   delete focus_on_mouse_down_test;
00154 
00155   return 0;
00156 }
00157