1 // Copyright (C) 2016 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "TestController.hxx"
21 #include "MEDModule.hxx"
23 #include <Basics_Utils.hxx> // STDLOG() macro
25 #include <SUIT_Session.h>
26 #include <SUIT_Desktop.h>
28 #include <SALOMEconfig.h>
29 #include CORBA_CLIENT_HEADER(MEDEventListener)
31 #include <pqTestUtility.h>
32 #include <pqXMLEventObserver.h>
33 #include <pqXMLEventSource.h>
35 #include <pqQVTKWidgetEventPlayer.h>
36 #include <pqQVTKWidgetEventTranslator.h>
38 #include <pqTabbedMultiViewWidget.h>
39 #include <pqApplicationCore.h>
40 #include <pqTestingReaction.h>
41 #include <pqSaveScreenshotReaction.h>
43 #include <QFileDialog>
50 class PlayTestEvent: public QEvent {
52 PlayTestEvent(QEvent::Type type, const std::string & filename): QEvent(type), _filename(filename) {}
53 virtual ~PlayTestEvent() {}
54 const std::string _filename;
57 TestController::TestController(MEDModule* mod):
59 _desk(SUIT_Session::session()->activeApplication()->desktop()),
60 _tester(0), _lock_action(0),
61 _quitEventType(QEvent::registerEventType()),
62 _playEventType(QEvent::registerEventType()),
63 _aboutToPlayTest(false),
64 _myEventLoopStarted(false)
66 STDLOG("Creating a TestController");
67 _tester = new pqTestUtility(_desk);
68 _tester->addEventObserver("xml", new pqXMLEventObserver(_desk));
69 _tester->addEventSource("xml", new pqXMLEventSource(_desk));
71 QTimer::singleShot(0, this, SLOT(onMainEventLoopStarting()));
74 TestController::~TestController()
82 TestController::createActions() {
86 QString label = tr("LAB_RECORD_TEST");
87 QString tooltip = tr("TIP_RECORD_TEST");
89 actionId = _salomeModule->createStandardAction(label,this, SLOT(onRecordTest()),QString(),tooltip);
91 // This action has to be placed in the general file menu
92 int menuId = _salomeModule->createMenu( tr( "MEN_FILE" ), -1, 1 );
93 _salomeModule->action(actionId)->setIconVisibleInMenu(false);
94 _salomeModule->createMenu(actionId, menuId, 60);
96 label = tr("LAB_PLAY_TEST");
97 tooltip = tr("TIP_PLAY_TEST");
98 actionId = _salomeModule->createStandardAction(label,this, SLOT(onPlayTest()),QString(),tooltip);
99 _salomeModule->action(actionId)->setIconVisibleInMenu(false);
100 _salomeModule->createMenu(actionId, menuId, 70);
102 label = tr("LAB_LOCK_TEST");
103 tooltip = tr("TIP_LOCK_TEST");
104 actionId = _salomeModule->createStandardAction(label,this, SLOT(onLockViewSize()),QString(),tooltip);
105 _salomeModule->action(actionId)->setIconVisibleInMenu(false);
106 _salomeModule->action(actionId)->setCheckable(true);
107 _lock_action = _salomeModule->action(actionId);
108 _salomeModule->createMenu(actionId, menuId, 70);
110 label = tr("LAB_SNAP_TEST");
111 tooltip = tr("TIP_SNAP_TEST");
112 actionId = _salomeModule->createStandardAction(label,this, SLOT(onTakeSnapshot()),QString(),tooltip);
113 _salomeModule->action(actionId)->setIconVisibleInMenu(false);
114 _salomeModule->createMenu(actionId, menuId, 70);
117 void TestController::onRecordTest()
120 QFileDialog::getSaveFileName(_desk, "Save test", QString(), QString("XML file (*.xml)"));
121 if (!fileName.isEmpty())
123 QApplication::setActiveWindow(_desk); //mandatory otherwise record pop up doesn't show up
124 _tester->recordTests(fileName);
129 void TestController::onPlayTest()
132 QFileDialog::getOpenFileName(_desk, "Open test", QString(), QString("XML file (*.xml)"));
133 if (!fileName.isEmpty())
134 _tester->playTests(fileName);
137 void TestController::onLockViewSize() const
139 pqTestingReaction::lockViewSize(_lock_action->isChecked());
142 void TestController::onTakeSnapshot() const
144 pqSaveScreenshotReaction::saveScreenshot();
147 void TestController::onRequestTermination()
149 // Check if test playing
150 if (_tester->playingTest() || _aboutToPlayTest)
152 QEvent * e = new QEvent((QEvent::Type)_quitEventType);
153 QApplication::postEvent(this, e);
157 _salomeModule->requestSALOMETermination();
162 TestController::customEvent(QEvent * event)
164 if (event->type() == _quitEventType)
166 if(!isMainEventLoopStarted())
168 QApplication::postEvent(this, new QEvent((QEvent::Type)_quitEventType));
170 onRequestTermination();
172 else if (event->type() == _playEventType)
174 PlayTestEvent * e = dynamic_cast<PlayTestEvent *>(event);
177 // // Wait for main event loop to start:
178 if(!isMainEventLoopStarted())
180 QApplication::postEvent(this, new PlayTestEvent((QEvent::Type)_playEventType, e->_filename));
183 STDLOG("About to play test " << e->_filename);
184 _tester->playTests(e->_filename.c_str());
185 _aboutToPlayTest = false;
186 STDLOG("Done playing test " << e->_filename);
191 { QObject::customEvent(event); }
195 TestController::processWorkspaceEvent(const MEDCALC::MedEvent* event)
197 if ( event->type == MEDCALC::EVENT_PLAY_TEST ) {
198 /* [ABN] Post an event. Indeed, calling the function directly would prevent the proper refresh of the
199 * GUI which also needs to go through the MED event loop (WorkspaceController::processWorkspaceEvent)
201 _aboutToPlayTest = true; // to prevent an early quit!
202 PlayTestEvent * e = new PlayTestEvent((QEvent::Type)_playEventType, std::string(event->filename));
203 QApplication::postEvent(this, e);
205 else if ( event->type == MEDCALC::EVENT_QUIT_SALOME ) {
206 // [ABN] again: post as an event to give a chance to other events (piled up by test
207 // scenarios for example) to execute:
208 QEvent * e = new QEvent((QEvent::Type)_quitEventType);
209 QApplication::postEvent(this, e);
213 void TestController::onMainEventLoopStarting()
215 _myEventLoopStarted = true;