Salome HOME
d645ad64933518844fd78bdcc66af2e170797a19
[modules/med.git] / src / MEDCalc / test / gui / test_qttesting.py
1 # Copyright (C) 2016-2020  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 # Author: A. Bruneton (CEA)
20
21 import unittest, os, shutil, sys
22 from posixpath import basename
23
24 class MEDGUITest(unittest.TestCase):
25   def __init__(self, methodName='runTest'):
26     unittest.TestCase.__init__(self, methodName=methodName)
27     self._tmpDir = ""
28     self._removeDir = True
29
30   def setUp(self):
31     import tempfile
32     self._tmpDir = tempfile.mkdtemp(prefix="med_gui_tests_")
33     self._removeDir = True  # reset for each new test in the TestCase
34
35   def tearDown(self):
36     import shutil
37     unittest.TestCase.tearDown(self)
38     if self._removeDir:
39       shutil.rmtree(self._tmpDir, False)
40
41   def getTestName(self):
42     """ Return name of the test being currently executed. """
43     return self.id().split(".")[-1]
44
45   def launchSalomeWithScript(self, scriptname):
46     """ TODO: review this - what is the nicest way to launch SALOME GUI from a Python script? """
47     from salome_instance import SalomeInstance
48     from medcalc_testutils import GetScriptDirGUI
49     args = "args:%s" % self._tmpDir
50     pth = os.path.join(GetScriptDirGUI(), scriptname)
51     # Launch SALOME with the test script:
52     inst = SalomeInstance.start(with_gui=True, args=[pth, args])
53     # And make sure SALOME is stopped before running next one:
54     inst.stop()
55     status = 0 # TODO review this
56     if status:
57       raise Exception("SALOME exited abnormally for this test!")
58
59   def compareSnapshot(self, basename):
60     """ Compare the screenshot in the current temporary test directory with the reference baseline.
61     Assert if not matching. """
62     import filecmp
63     from medcalc_testutils import GetBaselineDirGUI
64
65     base_pth = os.path.join(GetBaselineDirGUI(), basename)
66     gen_path = os.path.join(self._tmpDir, basename)
67     print(base_pth, gen_path)
68     try:
69       ret = filecmp.cmp(base_pth, gen_path, shallow=False)
70     except OSError:
71       import traceback
72       traceback.print_exc()
73       ret = False
74     if not ret:
75       # Keep file if comparison fails
76       self._removeDir = False
77       self.assertTrue(ret, "[%s] -- Failed screenshot equality, or unable to open baseline file - directory is kept alive: %s" % (self.getTestName(), self._tmpDir))
78     return ret
79
80   def prepareScenario(self, scenario, baseline, med_file):
81     """ Copy scenario to current temporary test dir and substitute paths inside """
82     from medcalc_testutils import GetScenarioDirGUI, GetMEDFileDirGUI
83     scen_path = os.path.join(GetScenarioDirGUI(), scenario)
84     scen_pth2 = os.path.join(self._tmpDir, scenario)
85     try:
86       shutil.copy(scen_path, scen_pth2)
87     except IOError:
88       raise Exception("Could not copy test scenario '%s' to local test directory!" % scen_path)
89     with open(scen_pth2,'r') as f:
90       filedata = f.read()
91     filedata = filedata.replace("/tmp/%s" % baseline, "%s/%s" % (self._tmpDir, baseline))
92     filedata = filedata.replace("/tmp/%s" % med_file, os.path.join(GetMEDFileDirGUI(), med_file))
93     with open(scen_pth2,'w') as f:
94       f.write(filedata)
95
96   ##
97   ## Now the tests themselves
98   ##
99   def testScalarMap(self):
100     baseline = "test_scalarmap.png"
101     med_file = "test_scalarmap.med"  # will change with Cedric's files
102     scenario = "test_scalarmap.xml"
103     self.prepareScenario(scenario, baseline, med_file)
104     self.launchSalomeWithScript("test_scalarmap.py")
105     self.compareSnapshot(baseline)
106
107   def testScalarMap2(self):
108     baseline = "test_scalarmap.png"
109     med_file = "test_scalarmap.med"  # will change
110     scenario = "test_scalarmap.xml"
111     self.prepareScenario(scenario, baseline, med_file)
112     self.launchSalomeWithScript("test_scalarmap.py")
113     self.compareSnapshot(baseline)
114
115 if __name__ == "__main__":
116   suite = unittest.TestSuite()
117   suite.addTest(MEDGUITest('testScalarMap'))
118   suite.addTest(MEDGUITest('testScalarMap2'))
119 #  suite.addTest(MEDGUITest('testIsoContour'))
120
121   runner = unittest.TextTestRunner()
122   ret = not runner.run(suite).wasSuccessful()
123   sys.exit(ret)