Salome HOME
First draft of a possible GUI testing framework. Still work todo:
[modules/med.git] / src / MEDCalc / gui / TestController.cxx
1 // Copyright (C) 2016  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "TestController.hxx"
21 #include "MEDModule.hxx"
22
23 #include <Basics_Utils.hxx>  // STDLOG() macro
24
25 #include <SUIT_Session.h>
26 #include <SUIT_Desktop.h>
27
28 #include <SALOMEconfig.h>
29 #include CORBA_CLIENT_HEADER(MEDEventListener)
30
31 #include <pqTestUtility.h>
32 #include <pqXMLEventObserver.h>
33 #include <pqXMLEventSource.h>
34
35 #include <pqQVTKWidgetEventPlayer.h>
36 #include <pqQVTKWidgetEventTranslator.h>
37
38 #include <pqTabbedMultiViewWidget.h>
39 #include <pqApplicationCore.h>
40 #include <pqTestingReaction.h>
41 #include <pqSaveScreenshotReaction.h>
42
43 #include <QFileDialog>
44 #include <QString>
45 #include <QDir>
46 #include <QIcon>
47 #include <QTimer>
48
49 TestController::TestController(MEDModule* mod):
50   _salomeModule(mod),
51   _desk(SUIT_Session::session()->activeApplication()->desktop()),
52   _tester(0), _lock_action(0)
53 {
54   STDLOG("Creating a TestController");
55   _tester = new pqTestUtility(_desk);
56   _tester->addEventObserver("xml", new pqXMLEventObserver(_desk));
57   _tester->addEventSource("xml", new pqXMLEventSource(_desk));
58 }
59
60 TestController::~TestController()
61 {
62   if (_tester)
63     delete _tester;
64   _tester = 0;
65 }
66
67 void
68 TestController::createActions() {
69   //
70   // Main actions
71   //
72   QString label   = tr("LAB_RECORD_TEST");
73   QString tooltip = tr("TIP_RECORD_TEST");
74   int actionId;
75   actionId = _salomeModule->createStandardAction(label,this, SLOT(onRecordTest()),QString(),tooltip);
76
77   // This action has to be placed in the general file menu
78   int menuId = _salomeModule->createMenu( tr( "MEN_FILE" ), -1,  1 );
79   _salomeModule->action(actionId)->setIconVisibleInMenu(false);
80   _salomeModule->createMenu(actionId, menuId, 60);
81
82   label   = tr("LAB_PLAY_TEST");
83   tooltip = tr("TIP_PLAY_TEST");
84   actionId = _salomeModule->createStandardAction(label,this, SLOT(onPlayTest()),QString(),tooltip);
85   _salomeModule->action(actionId)->setIconVisibleInMenu(false);
86   _salomeModule->createMenu(actionId, menuId, 70);
87
88   label   = tr("LAB_LOCK_TEST");
89   tooltip = tr("TIP_LOCK_TEST");
90   actionId = _salomeModule->createStandardAction(label,this, SLOT(onLockViewSize()),QString(),tooltip);
91   _salomeModule->action(actionId)->setIconVisibleInMenu(false);
92   _salomeModule->action(actionId)->setCheckable(true);
93   _lock_action = _salomeModule->action(actionId);
94   _salomeModule->createMenu(actionId, menuId, 70);
95
96   label   = tr("LAB_SNAP_TEST");
97   tooltip = tr("TIP_SNAP_TEST");
98   actionId = _salomeModule->createStandardAction(label,this, SLOT(onTakeSnapshot()),QString(),tooltip);
99   _salomeModule->action(actionId)->setIconVisibleInMenu(false);
100   _salomeModule->createMenu(actionId, menuId, 70);
101 }
102
103 void TestController::onRecordTest()
104 {
105   QString fileName =
106       QFileDialog::getSaveFileName(_desk, "Save test", QString(), QString("XML file (*.xml)"));
107   if (!fileName.isEmpty())
108     {
109       QApplication::setActiveWindow(_desk); //mandatory otherwise record pop up doesn't show up
110       _tester->recordTests(fileName);
111     }
112 }
113
114
115 void TestController::onPlayTest()
116 {
117   QString fileName =
118       QFileDialog::getOpenFileName(_desk, "Open test", QString(), QString("XML file (*.xml)"));
119   if (!fileName.isEmpty())
120     _tester->playTests(fileName);
121 }
122
123 void TestController::onPlayTestScenario()
124 {
125   STDLOG("@@@@ About to play test " << _test_scenario.toStdString());
126   _tester->playTests(_test_scenario);
127   STDLOG("@@@@ Done playing test " << _test_scenario.toStdString());
128 }
129
130 void TestController::onLockViewSize()
131 {
132   pqTestingReaction::lockViewSize(_lock_action->isChecked());
133 }
134
135 void TestController::onTakeSnapshot()
136 {
137   pqSaveScreenshotReaction::saveScreenshot();
138 }
139
140 void
141 TestController::processWorkspaceEvent(const MEDCALC::MedEvent* event)
142 {
143   if ( event->type == MEDCALC::EVENT_PLAY_TEST ) {
144       /* [ABN] Post an event. Indeed, calling the function directly would prevent the proper refresh of the
145        * GUI which also needs to go through the MED event loop (WorkspaceController::processWorkspaceEvent)
146        */
147       _test_scenario = QString(event->filename);
148       QTimer::singleShot(100, this, SLOT(onPlayTestScenario()));
149   }
150 }
151