diff options
author | Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr> | 2017-08-18 14:48:52 +0200 |
---|---|---|
committer | Picca Frédéric-Emmanuel <picca@synchrotron-soleil.fr> | 2017-08-18 14:48:52 +0200 |
commit | f7bdc2acff3c13a6d632c28c4569690ab106eed7 (patch) | |
tree | 9d67cdb7152ee4e711379e03fe0546c7c3b97303 /silx/gui/widgets/test |
Import Upstream version 0.5.0+dfsg
Diffstat (limited to 'silx/gui/widgets/test')
-rw-r--r-- | silx/gui/widgets/test/__init__.py | 45 | ||||
-rw-r--r-- | silx/gui/widgets/test/test_hierarchicaltableview.py | 117 | ||||
-rw-r--r-- | silx/gui/widgets/test/test_periodictable.py | 163 | ||||
-rw-r--r-- | silx/gui/widgets/test/test_tablewidget.py | 61 | ||||
-rw-r--r-- | silx/gui/widgets/test/test_threadpoolpushbutton.py | 129 |
5 files changed, 515 insertions, 0 deletions
diff --git a/silx/gui/widgets/test/__init__.py b/silx/gui/widgets/test/__init__.py new file mode 100644 index 0000000..afa0f78 --- /dev/null +++ b/silx/gui/widgets/test/__init__.py @@ -0,0 +1,45 @@ +# coding: utf-8 +# /*########################################################################## +# +# Copyright (c) 2016-2017 European Synchrotron Radiation Facility +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# ###########################################################################*/ +import unittest + +from . import test_periodictable +from . import test_tablewidget +from . import test_threadpoolpushbutton +from . import test_hierarchicaltableview + +__authors__ = ["V. Valls", "P. Knobel"] +__license__ = "MIT" +__date__ = "07/04/2017" + + +def suite(): + test_suite = unittest.TestSuite() + test_suite.addTests( + [test_threadpoolpushbutton.suite(), + test_tablewidget.suite(), + test_periodictable.suite(), + test_hierarchicaltableview.suite(), + ]) + return test_suite diff --git a/silx/gui/widgets/test/test_hierarchicaltableview.py b/silx/gui/widgets/test/test_hierarchicaltableview.py new file mode 100644 index 0000000..b3d37ed --- /dev/null +++ b/silx/gui/widgets/test/test_hierarchicaltableview.py @@ -0,0 +1,117 @@ +# coding: utf-8 +# /*########################################################################## +# +# Copyright (c) 2016-2017 European Synchrotron Radiation Facility +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# ###########################################################################*/ +__authors__ = ["V. Valls"] +__license__ = "MIT" +__date__ = "07/04/2017" + +import unittest + +from .. import HierarchicalTableView +from ...test.utils import TestCaseQt +from silx.gui import qt + + +class TableModel(HierarchicalTableView.HierarchicalTableModel): + + def __init__(self, parent): + HierarchicalTableView.HierarchicalTableModel.__init__(self, parent) + self.__content = {} + + def rowCount(self, parent=qt.QModelIndex()): + return 3 + + def columnCount(self, parent=qt.QModelIndex()): + return 3 + + def setData1(self): + if qt.qVersion() > "4.6": + self.beginResetModel() + else: + self.reset() + + content = {} + content[0, 0] = ("title", True, (1, 3)) + content[0, 1] = ("a", True, (2, 1)) + content[1, 1] = ("b", False, (1, 2)) + content[1, 2] = ("c", False, (1, 1)) + content[2, 2] = ("d", False, (1, 1)) + self.__content = content + if qt.qVersion() > "4.6": + self.endResetModel() + + def data(self, index, role=qt.Qt.DisplayRole): + if not index.isValid(): + return None + cell = self.__content.get((index.column(), index.row()), None) + if cell is None: + return None + + if role == self.SpanRole: + return cell[2] + elif role == self.IsHeaderRole: + return cell[1] + elif role == qt.Qt.DisplayRole: + return cell[0] + return None + + +class TestHierarchicalTableView(TestCaseQt): + """Test for HierarchicalTableView""" + + def testEmpty(self): + widget = HierarchicalTableView.HierarchicalTableView() + widget.show() + self.qWaitForWindowExposed(widget) + + def testModel(self): + widget = HierarchicalTableView.HierarchicalTableView() + model = TableModel(widget) + # set the data before using the model into the widget + model.setData1() + widget.setModel(model) + span = widget.rowSpan(0, 0), widget.columnSpan(0, 0) + self.assertEqual(span, (1, 3)) + widget.show() + self.qWaitForWindowExposed(widget) + + def testModelUpdate(self): + widget = HierarchicalTableView.HierarchicalTableView() + model = TableModel(widget) + widget.setModel(model) + # set the data after using the model into the widget + model.setData1() + span = widget.rowSpan(0, 0), widget.columnSpan(0, 0) + self.assertEqual(span, (1, 3)) + + +def suite(): + loader = unittest.defaultTestLoader.loadTestsFromTestCase + test_suite = unittest.TestSuite() + test_suite.addTest(loader(TestHierarchicalTableView)) + return test_suite + + +if __name__ == '__main__': + unittest.main(defaultTest='suite') diff --git a/silx/gui/widgets/test/test_periodictable.py b/silx/gui/widgets/test/test_periodictable.py new file mode 100644 index 0000000..c6bed81 --- /dev/null +++ b/silx/gui/widgets/test/test_periodictable.py @@ -0,0 +1,163 @@ +# coding: utf-8 +# /*########################################################################## +# +# Copyright (c) 2016-2017 European Synchrotron Radiation Facility +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# ###########################################################################*/ +__authors__ = ["P. Knobel"] +__license__ = "MIT" +__date__ = "05/12/2016" + +import unittest + +from .. import PeriodicTable +from ...test.utils import TestCaseQt +from silx.gui import qt + + +class TestPeriodicTable(TestCaseQt): + """Basic test for ArrayTableWidget with a numpy array""" + + def testShow(self): + """basic test (instantiation done in setUp)""" + pt = PeriodicTable.PeriodicTable() + pt.show() + self.qWaitForWindowExposed(pt) + + def testSelectable(self): + """basic test (instantiation done in setUp)""" + pt = PeriodicTable.PeriodicTable(selectable=True) + self.assertTrue(pt.selectable) + + def testCustomElements(self): + PTI = PeriodicTable.ColoredPeriodicTableItem + my_items = [ + PTI("Xx", 42, 43, 44, "xaxatorium", 1002.2, + bgcolor="#FF0000"), + PTI("Yy", 25, 22, 44, "yoyotrium", 8.8) + ] + + pt = PeriodicTable.PeriodicTable(elements=my_items) + + pt.setSelection(["He", "Xx"]) + selection = pt.getSelection() + self.assertEqual(len(selection), 1) # "He" not found + self.assertEqual(selection[0].symbol, "Xx") + self.assertEqual(selection[0].Z, 42) + self.assertEqual(selection[0].col, 43) + self.assertAlmostEqual(selection[0].mass, 1002.2) + self.assertEqual(qt.QColor(selection[0].bgcolor), + qt.QColor(qt.Qt.red)) + + self.assertTrue(pt.isElementSelected("Xx")) + self.assertFalse(pt.isElementSelected("Yy")) + self.assertRaises(KeyError, pt.isElementSelected, "Yx") + + def testVeryCustomElements(self): + class MyPTI(PeriodicTable.PeriodicTableItem): + def __init__(self, *args): + PeriodicTable.PeriodicTableItem.__init__(self, *args[:6]) + self.my_feature = args[6] + + my_items = [ + MyPTI("Xx", 42, 43, 44, "xaxatorium", 1002.2, "spam"), + MyPTI("Yy", 25, 22, 44, "yoyotrium", 8.8, "eggs") + ] + + pt = PeriodicTable.PeriodicTable(elements=my_items) + + pt.setSelection(["Xx", "Yy"]) + selection = pt.getSelection() + self.assertEqual(len(selection), 2) + self.assertEqual(selection[1].symbol, "Yy") + self.assertEqual(selection[1].Z, 25) + self.assertEqual(selection[1].col, 22) + self.assertEqual(selection[1].row, 44) + self.assertAlmostEqual(selection[0].mass, 1002.2) + self.assertAlmostEqual(selection[0].my_feature, "spam") + + +class TestPeriodicCombo(TestCaseQt): + """Basic test for ArrayTableWidget with a numpy array""" + def setUp(self): + super(TestPeriodicCombo, self).setUp() + self.pc = PeriodicTable.PeriodicCombo() + + def tearDown(self): + del self.pc + super(TestPeriodicCombo, self).tearDown() + + def testShow(self): + """basic test (instantiation done in setUp)""" + self.pc.show() + self.qWaitForWindowExposed(self.pc) + + def testSelect(self): + self.pc.setSelection("Sb") + selection = self.pc.getSelection() + self.assertIsInstance(selection, + PeriodicTable.PeriodicTableItem) + self.assertEqual(selection.symbol, "Sb") + self.assertEqual(selection.Z, 51) + self.assertEqual(selection.name, "antimony") + + +class TestPeriodicList(TestCaseQt): + """Basic test for ArrayTableWidget with a numpy array""" + def setUp(self): + super(TestPeriodicList, self).setUp() + self.pl = PeriodicTable.PeriodicList() + + def tearDown(self): + del self.pl + super(TestPeriodicList, self).tearDown() + + def testShow(self): + """basic test (instantiation done in setUp)""" + self.pl.show() + self.qWaitForWindowExposed(self.pl) + + def testSelect(self): + self.pl.setSelectedElements(["Li", "He", "Au"]) + sel_elmts = self.pl.getSelection() + + self.assertEqual(len(sel_elmts), 3, + "Wrong number of elements selected") + for e in sel_elmts: + self.assertIsInstance(e, PeriodicTable.PeriodicTableItem) + self.assertIn(e.symbol, ["Li", "He", "Au"]) + self.assertIn(e.Z, [2, 3, 79]) + self.assertIn(e.name, ["lithium", "helium", "gold"]) + + +def suite(): + test_suite = unittest.TestSuite() + test_suite.addTest( + unittest.defaultTestLoader.loadTestsFromTestCase(TestPeriodicTable)) + test_suite.addTest( + unittest.defaultTestLoader.loadTestsFromTestCase(TestPeriodicList)) + test_suite.addTest( + unittest.defaultTestLoader.loadTestsFromTestCase(TestPeriodicCombo)) + return test_suite + + +if __name__ == '__main__': + unittest.main(defaultTest='suite') diff --git a/silx/gui/widgets/test/test_tablewidget.py b/silx/gui/widgets/test/test_tablewidget.py new file mode 100644 index 0000000..5ad0a06 --- /dev/null +++ b/silx/gui/widgets/test/test_tablewidget.py @@ -0,0 +1,61 @@ +# coding: utf-8 +# /*########################################################################## +# +# Copyright (c) 2016 European Synchrotron Radiation Facility +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# ###########################################################################*/ +"""Test TableWidget""" + +__authors__ = ["P. Knobel"] +__license__ = "MIT" +__date__ = "05/12/2016" + + +import unittest +from silx.gui.test.utils import TestCaseQt +from silx.gui.widgets.TableWidget import TableWidget + + +class TestTableWidget(TestCaseQt): + def setUp(self): + super(TestTableWidget, self).setUp() + self._result = [] + + def testShow(self): + table = TableWidget() + table.setColumnCount(10) + table.setRowCount(7) + table.enableCut() + table.enablePaste() + table.show() + table.hide() + self.qapp.processEvents() + + +def suite(): + test_suite = unittest.TestSuite() + test_suite.addTest( + unittest.defaultTestLoader.loadTestsFromTestCase(TestTableWidget)) + return test_suite + + +if __name__ == '__main__': + unittest.main(defaultTest='suite') diff --git a/silx/gui/widgets/test/test_threadpoolpushbutton.py b/silx/gui/widgets/test/test_threadpoolpushbutton.py new file mode 100644 index 0000000..126f8f3 --- /dev/null +++ b/silx/gui/widgets/test/test_threadpoolpushbutton.py @@ -0,0 +1,129 @@ +# coding: utf-8 +# /*########################################################################## +# +# Copyright (c) 2016 European Synchrotron Radiation Facility +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. +# +# ###########################################################################*/ +"""Test for silx.gui.hdf5 module""" + +__authors__ = ["V. Valls"] +__license__ = "MIT" +__date__ = "15/12/2016" + + +import unittest +import time +from silx.gui import qt +from silx.gui.test.utils import TestCaseQt +from silx.gui.test.utils import SignalListener +from silx.gui.widgets.ThreadPoolPushButton import ThreadPoolPushButton +from silx.test.utils import TestLogging + + +class TestThreadPoolPushButton(TestCaseQt): + + def setUp(self): + super(TestThreadPoolPushButton, self).setUp() + self._result = [] + + def _trace(self, name, delay=0): + self._result.append(name) + if delay != 0: + time.sleep(delay / 1000.0) + + def _compute(self): + return "result" + + def _computeFail(self): + raise Exception("exception") + + def testExecute(self): + button = ThreadPoolPushButton() + button.setCallable(self._trace, "a", 0) + button.executeCallable() + time.sleep(0.1) + self.assertListEqual(self._result, ["a"]) + self.qapp.processEvents() + + def testMultiExecution(self): + button = ThreadPoolPushButton() + button.setCallable(self._trace, "a", 0) + number = qt.QThreadPool.globalInstance().maxThreadCount() * 2 + for _ in range(number): + button.executeCallable() + time.sleep(number * 0.01 + 0.1) + self.assertListEqual(self._result, ["a"] * number) + self.qapp.processEvents() + + def testSaturateThreadPool(self): + button = ThreadPoolPushButton() + button.setCallable(self._trace, "a", 100) + number = qt.QThreadPool.globalInstance().maxThreadCount() * 2 + for _ in range(number): + button.executeCallable() + time.sleep(number * 0.1 + 0.1) + self.assertListEqual(self._result, ["a"] * number) + self.qapp.processEvents() + + def testSuccess(self): + listener = SignalListener() + button = ThreadPoolPushButton() + button.setCallable(self._compute) + button.beforeExecuting.connect(listener.partial(test="be")) + button.started.connect(listener.partial(test="s")) + button.succeeded.connect(listener.partial(test="result")) + button.failed.connect(listener.partial(test="Unexpected exception")) + button.finished.connect(listener.partial(test="f")) + button.executeCallable() + self.qapp.processEvents() + time.sleep(0.1) + self.qapp.processEvents() + result = listener.karguments(argumentName="test") + self.assertListEqual(result, ["be", "s", "result", "f"]) + + def testFail(self): + listener = SignalListener() + button = ThreadPoolPushButton() + button.setCallable(self._computeFail) + button.beforeExecuting.connect(listener.partial(test="be")) + button.started.connect(listener.partial(test="s")) + button.succeeded.connect(listener.partial(test="Unexpected success")) + button.failed.connect(listener.partial(test="exception")) + button.finished.connect(listener.partial(test="f")) + with TestLogging('silx.gui.widgets.ThreadPoolPushButton', error=1): + button.executeCallable() + self.qapp.processEvents() + time.sleep(0.1) + self.qapp.processEvents() + result = listener.karguments(argumentName="test") + self.assertListEqual(result, ["be", "s", "exception", "f"]) + listener.clear() + + +def suite(): + test_suite = unittest.TestSuite() + test_suite.addTest( + unittest.defaultTestLoader.loadTestsFromTestCase(TestThreadPoolPushButton)) + return test_suite + + +if __name__ == '__main__': + unittest.main(defaultTest='suite') |