VICI  0.11.815
Visual Chart Interpreter
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
libgth.h
Go to the documentation of this file.
1 /*
2  libgth.h
3 
4  Copyright 2012 - 2017 Brenton Ross
5 
6 This file is part of VICI.
7 
8 VICI is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 VICI is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with VICI. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
31 #ifndef GTH_LIBGTH_H
32 #define GTH_LIBGTH_H
33 
34 #include <vici/testmgr.h>
35 #include <vici/proc.h>
36 #include <vici/fdstream.h>
37 #include <vici/xml.h>
38 #include "libgui.h"
39 #include "gth.h"
40 #include <map>
41 #include <memory>
42 #include <mutex>
43 #include <condition_variable>
44 #include <QThread>
45 #include <QGraphicsView>
46 #ifdef HAVE_QT4
47 #include <QtGui>
48 #endif
49 #ifdef HAVE_QT5
50 #include <QtWidgets>
51 #endif
52 
53 extern "C" {
54 // forward declaration
55 struct lua_State; // this stops the requirement for Lua from leaking into other projects.
56 }
57 
58 
59 
60 namespace VICI
61 {
62 
63 namespace gth
64 {
65 
66 class GTHTest;
67 class ScriptXML;
68 
70 typedef std::vector< std::string > ParamList;
71 
73 typedef std::vector< std::list< std::string> > ListOfLists;
74 
76 typedef std::shared_ptr< ScriptXML > ScriptXmlPtr;
77 
78 // ----------------------------------------------------------
79 
81 
88 class Adaptor
89 {
90 protected:
92  void mouseDoubleClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = 0,
93  QPoint pos = QPoint(), int delay=-1);
94 
96  GTHTest * gthTest; // reference
97 
99  std::string widgetName;
100 public:
102  Adaptor( csr name, GTHTest *gth) : gthTest(gth), widgetName(name) {}
103 
105  virtual ~Adaptor(){}
106 
108 
114  virtual std::string action( csr tcname, csr cmnd, const ParamList &params ) = 0;
115 
117 
120  virtual VICI::gth::WidgetType getType() = 0;
121 
124 };
125 
126 // ----------------------------------------------------------
127 
129 
133 {
134 private:
135  lua_State *L;
136 public:
138  LuaScript( csr chunk );
139 
141  ~LuaScript();
142 
144  std::string call( csr cmnd, const ParamList &params );
145 
147  static int luaVersion();
148 };
149 
151 typedef std::shared_ptr<LuaScript> LuaScriptPtr;
152 
153 // ----------------------------------------------------------
154 
156 
160 {
161  std::string label;
162  int delay;
163  std::string widgetName;
164  std::string command;
166  std::string assignmentRegex;
168 
170  std::vector< std::pair<std::string, std::string > > jumps;
171 };
172 
173 
174 // ----------------------------------------------------------
175 
177 enum ConDes { Constructor, Destructor };
178 
179 // ----------------------------------------------------------
180 
182 
185 class TestsForWindow : public QThread
186 {
187  Q_OBJECT
188 private:
189  // the list of test case names as used to install the test cases.
190  ListOfLists testCases;
191  ListOfLists::iterator seq;
192 
193  std::string windowName;
194  bool forMainWindow;
195  GTHTest *owner;
196  LuaScriptPtr luaScript;
197 
198  // wait for other windows to complete
199  bool paused;
200  std::mutex pauseMx;
201  std::condition_variable pauseCV;
202 
203  std::mutex jumpMx;
204  std::condition_variable jumpCV;
205  bool jumpCompleted;
206 
207 public:
209  TestsForWindow(GTHTest *, csr window, bool isMainWindow);
210 
212  ~TestsForWindow();
213 
215  void addSequence();
216 
218  void addTest(csr testCase);
219 
221  void addAction( csr testCase, ConDes, const TestAction &);
222 
224  void addScript( csr chunk );
225 
227  csr name() { return windowName; }
228 
230  virtual void run();
231 
233  void pause();
234 
236  void resume();
237 
239  bool checkPaused();
240 
242  void jumpThread( csr tcname, csr widgetName, csr command, const std::vector< std::string> &params);
243 
245  void jumpNotify();
246 
247 signals:
249  void sendAdaptor( const std::string &tcname, const std::string & widgetName,
250  const std::string & command,
251  const std::vector< std::string> &params);
252 };
253 
255 typedef std::shared_ptr< TestsForWindow > TestsForWindowPtr;
256 
257 // -------------------------------------------------------------
259 
268 class UserEscaper : public QObject
269 {
270  Q_OBJECT
271 private:
272  bool &escapeFlag;
273 public:
274  UserEscaper( bool &flag ) : escapeFlag(flag) {}
275 protected:
276  bool eventFilter(QObject *obj, QEvent *event);
277 };
278 
279 // ----------------------------------------------------------
280 
282 
289 class GTHTest : public QObject, public VICI::cdi::ScenarioT<GTHTest>, public VICI::WidgetMgrClient
290 {
291  Q_OBJECT
292 private:
293  // map from widget name to its adaptor
294  std::map< std::string, Adaptor * > adaptors;
295  Adaptor *makeAdaptor( void *w, VICI::gth::WidgetType, csr widgetName, GTHTest *);
296  void describeAdaptors();
297 public:
299  void jumpThread( csr tcname, csr widgetName, csr command, const std::vector< std::string> &params);
300 
301 private slots:
302  void runAdaptor( const std::string &tcname, const std::string & widgetName,
303  const std::string & command,
304  const std::vector< std::string > &params);
305 
306 private:
307  // registering widgets
308  void registerStandardWindow(QWidget *);
309  void registerMessageBox(QWidget *w);
310  void registerFileDialog(QWidget *w);
311  void registerFontDialog(QWidget *w);
312  void registerColorDialog(QWidget *w);
313  void registerInputDialog(QWidget *w);
314  void registerButtons(QWidget *w, csr prefix );
315  void clean( std::string & );
316  std::string widgetTypeToString( WidgetType );
317 
318  void newActiveWindow( QWidget *);
319 
320  QWidget *mainWindow;
321 
322  ScriptXmlPtr script;
323  void loadScript();
324  std::set<TestsForWindowPtr> activeTests;
325  TestsForWindowPtr currentActiveTests;
326 
327  // map from window name to the test thread
328  std::map< std::string, TestsForWindowPtr > testsForWindows;
329 
330  // map from test case name to test thread
331  std::map< std::string, TestsForWindowPtr > testCases;
332  void installDefaultTestCases();
333 
334  // map test case, constructor/destructor to the vector of test actions
335  std::map< std::pair<std::string, ConDes>, std::vector<TestAction> > actions;
336 
337  // map from variable name to its value
338  std::map< std::string, std::string> variables;
339 
340  // std::mutex focusMx;
341  // support for allowing user to escape from the test
342  bool userEscape;
343  UserEscaper *escaper;
344 
345 private slots:
346  void focusChanged(QWidget *, QWidget * );
347  //void beginTests();
348 
349 public:
351  GTHTest(csr name);
352 
354  ~GTHTest();
355 
357  csr getName() { return scenarioName; }
358 
360  virtual bool willRunTests() { return true; }
361 
364 
367 
369  void endMainWindow();
370 
372  void runActions( csr testCase, ConDes );
373 
375  void assignVariables( const TestAction &, csr result );
376 
378  std::string getJump( const TestAction &, csr result);
379 
381  csr getVar( csr varName ) const;
382 
384  void substVars( const ParamList &, ParamList &);
385 
386  virtual void windowHasRegistered( GTHWindowWidget * );
387  virtual void windowHasDeregistered( GTHWindowWidget * );
388 
390  void regWidget( void *w, VICI::gth::WidgetType t, csr n);
391 
393  std::string prefix();
394 
395 signals:
397  void testingCompleted();
398 };
399 
400 // ----------------------------------------------------------
401 
403 
407 {
408 protected:
410  const std::string & name;
411  csr getVar(csr);
412 protected:
415 
417  ~GTHTestCase();
418 
419 };
420 
421 // ----------------------------------------------------------
422 
424 
427 class DefaultTestCase : public VICI::gth::GTHTestCase, public VICI::cdi::TestCaseT<DefaultTestCase>
428 {
429 protected:
430  virtual bool runTest(){ return true; }
431 public:
434  : GTHTestCase(nm), VICI::cdi::TestCaseT<DefaultTestCase>(nm)
435  {}
436 
439 };
440 
441 // -------------------------------------------------------------
442 
444 
447 class ScriptXML : public VICI::cfi::Xml
448 {
449 public:
451  ScriptXML( const VICI::Path &, bool create);
452 
454  ~ScriptXML();
455 
456  // get the necessaries for running a test
457 
459  void getWindows( ParamList &);
460 
462  void getTestsForWindow( csr wnm, ListOfLists &);
463 
465  void getTestActions(csr tnm, ConDes, std::vector< TestAction > & );
466 
468  void getScripts(std::map< std::string, std::string > &);
469 
471  void addExecFilePath(csr fileName);
472 
474  void addTest( csr testName, bool unreferenced );
475 
477  void addWindow( csr windowName, bool unreferenced );
478 
480  void addWidget( csr widgetName, csr widgetType );
481 
483  void addWidgetType( csr widgetType, const ListOfLists &);
484 };
485 
486 
487 // -------------------------------------------------------------
488 
490 
494 class MouseEventReporter : public QObject
495 {
496  Q_OBJECT
497 private:
498  QGraphicsView *view;
499 
500 public:
502  MouseEventReporter(QGraphicsView *v) : view(v) {}
503 
504 protected:
506  bool eventFilter(QObject *obj, QEvent *event);
507 };
508 
509 // -------------------------------------------------------------
510 
512 
515 template < VICI::gth::WidgetType wt >
516 class AdaptorST : public Adaptor
517 {
518 private:
519 public:
521  AdaptorST(csr name, GTHTest *gth) : Adaptor(name, gth) {}
522 
524  static void describe( ListOfLists & );
525 };
526 
527 // -------------------------------------------------------------
528 
529 
531 
535 template < VICI::gth::WidgetType wt, class T >
536 class AdaptorT : public AdaptorST<wt>
537 {
538 private:
539  T *w; // reference
540  static const VICI::gth::WidgetType wType = wt;
541 public:
543 
548  AdaptorT( void *p, csr name, GTHTest *gth)
549  : AdaptorST<wt>(name, gth), w( reinterpret_cast<T *>(p)) {}
550 
551  virtual ~AdaptorT(){}
552 
554 
557  VICI::gth::WidgetType getType() { return wType; }
558 
560  virtual std::string action( csr tcname, csr cmnd, const ParamList & params );
561 };
562 
563 // ----------------------------------------------------------
564 
565 } // namespace gth
566 
567 } // namespace VICI
568 // ----------------------------------------------------------
569 
570 // ----------------------------------------------------------
571 
572 #endif
std::shared_ptr< TestsForWindow > TestsForWindowPtr
Provides for automatic destruction when the owner is deleted.
Definition: libgth.h:255
LuaScript(csr chunk)
Constructor.
Definition: libgth.cpp:1124
Adaptor(csr name, GTHTest *gth)
Constructor.
Definition: libgth.h:102
DefaultTestCase(csr nm)
Constructor.
Definition: libgth.h:433
Provides an interface to the script XML file.
Definition: libgth.h:447
A mix-in class with the functions required for the gui test harness.
Definition: libgui.h:56
const std::string & name
The name of the test case, as installed.
Definition: libgth.h:410
bool checkPaused()
Test if paused.
Definition: libgth.cpp:1069
void getScripts(std::map< std::string, std::string > &)
Get the script for a window.
Definition: script.cpp:325
void resume()
Resume execution of the thread.
Definition: libgth.cpp:1085
void runActions(csr testCase, ConDes)
Called by the constructor and destructor of a test case.
Definition: libgth.cpp:429
std::string assignmentRegex
Regular expression for specifying values for variables.
Definition: libgth.h:166
An interface for the WidgetMgr to call its clients when a window registers itself.
Definition: libgui.h:80
static void describe(ListOfLists &)
Get a list of commands and their parameters for the Adaptor.
void jumpNotify()
Notification that the action has completed.
Definition: libgth.cpp:1113
virtual std::string action(csr tcname, csr cmnd, const ParamList &params)
Run the command on the Adaptor&#39;s widget.
static int luaVersion()
Allow test scripts to check if Lua is installed (&gt;0) and the version (51, 53)
Definition: libgth.cpp:1164
void mouseDoubleClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers stateKey=0, QPoint pos=QPoint(), int delay=-1)
Utility functions.
void addExecFilePath(csr fileName)
Insert the executable&#39;s path into the script.
Definition: script.cpp:109
Provides a means of getting scene coordinates in a QGraphicsView.
Definition: libgth.h:494
void addSequence()
Increment the sequence number.
Definition: libgth.cpp:1004
csr getWidgetName()
Get the name of the widget.
Definition: libgth.h:123
void endMainWindow()
Called when testing for the main window completes.
Definition: libgth.cpp:420
virtual void run()
Begin execution of the thread.
Definition: libgth.cpp:1033
GTHTest(csr name)
Costructor.
Definition: libgth.cpp:58
A class for allowing the user to suspend the test.
Definition: libgth.h:268
void getWindows(ParamList &)
Get a list of windows from the XML script.
Definition: script.cpp:203
std::vector< std::list< std::string > > ListOfLists
ListOfLists is used to describe the commands and parameters of an Adaptor.
Definition: libgth.h:73
virtual bool runTest()
Run the tests for the test case.
Definition: libgth.h:430
Provide the interface between the GUI applications and the GUI Test Harness.
std::shared_ptr< LuaScript > LuaScriptPtr
Provides for automatic destruction when the owner is deleted.
Definition: libgth.h:151
~TestsForWindow()
Destructor.
Definition: libgth.cpp:997
~DefaultTestCase()
Destructor.
Definition: libgth.h:438
void assignVariables(const TestAction &, csr result)
Applies a regular expression to the result to get values for variables.
Definition: libgth.cpp:497
GTHTestCase(csr name)
Constructor.
Definition: libgth.cpp:1217
bool eventFilter(QObject *obj, QEvent *event)
Report position of mouse click in a graphics view.
void addScript(csr chunk)
Add a Lua script.
Definition: libgth.cpp:1022
Provides a test case to use for actions that don&#39;t have an explicit test case.
Definition: libgth.h:427
std::string widgetName
The name of the widget.
Definition: libgth.h:163
The interpreter for the script commands.
Definition: libgth.h:536
std::vector< std::pair< std::string, std::string > > jumps
Pairs of regular expressions and jump labels.
Definition: libgth.h:170
Provides a thread for applying test cases to a window.
Definition: libgth.h:185
WidgetType
List the types of widgets that we can interact with during testing.
Definition: gth.h:43
void getTestActions(csr tnm, ConDes, std::vector< TestAction > &)
Get the test actions for a test case constructor or destructor.
Definition: script.cpp:250
std::string label
Destination label for jumps.
Definition: libgth.h:161
GTHTest * gthTest
Reference to the owner.
Definition: libgth.h:96
Responsible for installing a factory for the test case.
Definition: testmgr.h:585
void addTest(csr testCase)
Add a test case to the current sequence.
Definition: libgth.cpp:1014
int delay
Time to wait in milliseconds.
Definition: libgth.h:162
void addWindow(csr windowName, bool unreferenced)
Add the name of windows that are [not] referenced in the script.
Definition: script.cpp:140
GTHTest * myScenario
The owning scenario.
Definition: libgth.h:409
~GTHTest()
Destructor.
Definition: libgth.cpp:100
~GTHTestCase()
Destructor.
Definition: libgth.cpp:1232
void jumpThread(csr tcname, csr widgetName, csr command, const std::vector< std::string > &params)
Method for thread jumping.
Definition: libgth.cpp:908
void addAction(csr testCase, ConDes, const TestAction &)
Add an action to a test case.
void regWidget(void *w, VICI::gth::WidgetType t, csr n)
Register a widget so that it can be found by name.
Definition: libgth.cpp:815
A single action that can be applied to a GUI.
Definition: libgth.h:159
virtual void runTests(csr scenarioName, cdi::ScenarioResults *)
Run an event loop and wait until the testing completes.
Definition: libgth.cpp:393
virtual std::string action(csr tcname, csr cmnd, const ParamList &params)=0
Interpreter function.
ConDes
Enumerates the stages at which the actions for a test case can be run.
Definition: libgth.h:177
Manipulate path strings.
Definition: stringy.h:74
virtual ~Adaptor()
Destructor.
Definition: libgth.h:105
void getTestsForWindow(csr wnm, ListOfLists &)
Get a list of test cases for a window.
Definition: script.cpp:219
Responsible for storing the results of testing for a scenario.
Definition: testmgr.h:409
csr getName()
Get the name of the scenario.
Definition: libgth.h:357
csr getVar(csr)
Get the value of a variable from GTHTest.
Definition: libgth.cpp:1240
Provides a wrapper for a lua_State object.
Definition: libgth.h:132
Defines an abstract base class for widget adaptors.
Definition: libgth.h:88
csr getVar(csr varName) const
Get the value of a variable.
Definition: libgth.cpp:519
void create(const Path &fname, csr root_element)
Create a new xml file and document.
Definition: xml.cpp:108
void addWidgetType(csr widgetType, const ListOfLists &)
Update description of commands for a widget type.
Definition: script.cpp:176
VICI::gth::WidgetType getType()
Get the type of the adaptor.
Definition: libgth.h:557
virtual void windowHasRegistered(GTHWindowWidget *)
Called to notify of a new window registering itself.
Definition: libgth.cpp:762
std::vector< std::string > ParamList
ParamList is used for the parameters of a command.
Definition: libgth.h:67
AdaptorT(void *p, csr name, GTHTest *gth)
Constructor.
Definition: libgth.h:548
std::shared_ptr< ScriptXML > ScriptXmlPtr
Provide for automatic destruction when owner is deleted.
Definition: libgth.h:76
AdaptorST(csr name, GTHTest *gth)
Constructor.
Definition: libgth.h:521
MouseEventReporter(QGraphicsView *v)
Constructor.
Definition: libgth.h:502
void pause()
Pause the thread.
Definition: libgth.cpp:1060
void jumpThread(csr tcname, csr widgetName, csr command, const std::vector< std::string > &params)
Run the action in the GUI thread.
Definition: libgth.cpp:1095
void sendAdaptor(const std::string &tcname, const std::string &widgetName, const std::string &command, const std::vector< std::string > &params)
A signal containing the adaptor to run in the GUI thread.
std::string scenarioName
The name of the scenario, as used when installed.
Definition: testmgr.h:429
virtual VICI::gth::WidgetType getType()=0
Get the type of the adaptor.
An AbstractTest derived class for testing GUI programs.
Definition: libgth.h:289
std::string command
The command to run.
Definition: libgth.h:164
cdi::ScenarioResults * results
Reference to the results.
Definition: libgth.h:366
void addWidget(csr widgetName, csr widgetType)
Update the list of widgets and their types.
Definition: script.cpp:164
Interface for test cases that do gui testing.
Definition: libgth.h:406
ScriptXML(const VICI::Path &, bool create)
Constructor.
Definition: script.cpp:38
virtual bool willRunTests()
Indicate that the scenario will take over from the Tester.
Definition: libgth.h:360
const std::string & csr
short cut for string constants
Definition: vici.h:80
virtual void windowHasDeregistered(GTHWindowWidget *)
Called to notify that a window has deregistered itself,.
Definition: libgth.cpp:772
bool eventFilter(QObject *obj, QEvent *event)
Called to process events.
Definition: libgth.cpp:952
void substVars(const ParamList &, ParamList &)
Substitute variables for their values in the parameters.
Definition: libgth.cpp:532
void testingCompleted()
This signal is emitted when the main window tests have completed.
~ScriptXML()
Destructor.
Definition: script.cpp:95
TestCaseT(csr nm)
Constructor.
Definition: testmgr.h:592
void addTest(csr testName, bool unreferenced)
Add the name of test cases that are [not] referenced in the script.
Definition: script.cpp:120
TestsForWindow(GTHTest *, csr window, bool isMainWindow)
Constructor.
Definition: libgth.cpp:982
ParamList params
The parameters for the command.
Definition: libgth.h:165
A C++ wrapper for libxml2.
Definition: xml.h:73
std::string widgetName
The name of the widget as set when it was registered.
Definition: libgth.h:99
ParamList assignmentNames
The variables to accept the values.
Definition: libgth.h:167
Responsible for installing a factory to create scenarios of the required type.
Definition: testmgr.h:453
UserEscaper(bool &flag)
Constructor.
Definition: libgth.h:274
std::string call(csr cmnd, const ParamList &params)
Run a function in the Lua chunk.
Definition: libgth.cpp:1178
Provides project wide GUI components.
std::string getJump(const TestAction &, csr result)
Applies regular expressions to the result to find a jump label.
Definition: libgth.cpp:560
std::string prefix()
get thread and time
Definition: libgth.cpp:891
Adds a static method to Adaptor to describe it;.
Definition: libgth.h:516
~LuaScript()
Destructor.
Definition: libgth.cpp:1153
csr name()
Get the name of the window.
Definition: libgth.h:227