diff options
Diffstat (limited to 'silx/gui/plot/test')
-rw-r--r-- | silx/gui/plot/test/testAlphaSlider.py | 5 | ||||
-rw-r--r-- | silx/gui/plot/test/testComplexImageView.py | 6 | ||||
-rw-r--r-- | silx/gui/plot/test/testCurvesROIWidget.py | 140 | ||||
-rw-r--r-- | silx/gui/plot/test/testItem.py | 13 | ||||
-rw-r--r-- | silx/gui/plot/test/testPlotWidget.py | 55 | ||||
-rw-r--r-- | silx/gui/plot/test/testPlotWindow.py | 29 | ||||
-rw-r--r-- | silx/gui/plot/test/testProfile.py | 6 | ||||
-rw-r--r-- | silx/gui/plot/test/testStackView.py | 6 | ||||
-rw-r--r-- | silx/gui/plot/test/testStats.py | 259 |
9 files changed, 427 insertions, 92 deletions
diff --git a/silx/gui/plot/test/testAlphaSlider.py b/silx/gui/plot/test/testAlphaSlider.py index 63de441..01e6969 100644 --- a/silx/gui/plot/test/testAlphaSlider.py +++ b/silx/gui/plot/test/testAlphaSlider.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2017 European Synchrotron Radiation Facility +# Copyright (c) 2017-2019 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 @@ -37,9 +37,6 @@ from silx.gui.utils.testutils import TestCaseQt from silx.gui.plot import PlotWidget from silx.gui.plot import AlphaSlider -# Makes sure a QApplication exists -_qapp = qt.QApplication.instance() or qt.QApplication([]) - class TestActiveImageAlphaSlider(TestCaseQt): def setUp(self): diff --git a/silx/gui/plot/test/testComplexImageView.py b/silx/gui/plot/test/testComplexImageView.py index 1933a95..051ec4d 100644 --- a/silx/gui/plot/test/testComplexImageView.py +++ b/silx/gui/plot/test/testComplexImageView.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2017 European Synchrotron Radiation Facility +# Copyright (c) 2017-2019 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 @@ -63,10 +63,10 @@ class TestComplexImageView(PlotWidgetTestCase, ParametricTestCase): self.qWait(100) # Test all modes - modes = self.plot.getSupportedVisualizationModes() + modes = self.plot.supportedComplexModes() for mode in modes: with self.subTest(mode=mode): - self.plot.setVisualizationMode(mode) + self.plot.setComplexMode(mode) self.qWait(100) # Test origin and scale API diff --git a/silx/gui/plot/test/testCurvesROIWidget.py b/silx/gui/plot/test/testCurvesROIWidget.py index 5bcabd8..5886456 100644 --- a/silx/gui/plot/test/testCurvesROIWidget.py +++ b/silx/gui/plot/test/testCurvesROIWidget.py @@ -34,11 +34,15 @@ import os.path import unittest from collections import OrderedDict import numpy + from silx.gui import qt +from silx.gui.plot import Plot1D from silx.test.utils import temp_dir from silx.gui.utils.testutils import TestCaseQt, SignalListener from silx.gui.plot import PlotWindow, CurvesROIWidget - +from silx.gui.plot.CurvesROIWidget import ROITable +from silx.gui.utils.testutils import getQToolButtonFromAction +from silx.gui.plot.PlotInteraction import ItemsInteraction _logger = logging.getLogger(__name__) @@ -68,6 +72,18 @@ class TestCurvesROIWidget(TestCaseQt): super(TestCurvesROIWidget, self).tearDown() + def testDummyAPI(self): + """Simple test of the getRois and setRois API""" + roi_neg = CurvesROIWidget.ROI(name='negative', fromdata=-20, + todata=-10, type_='X') + roi_pos = CurvesROIWidget.ROI(name='positive', fromdata=10, + todata=20, type_='X') + + self.widget.roiWidget.setRois((roi_pos, roi_neg)) + + rois_defs = self.widget.roiWidget.getRois() + self.widget.roiWidget.setRois(rois=rois_defs) + def testWithCurves(self): """Plot with curves: test all ROI widget buttons""" for offset in range(2): @@ -301,7 +317,7 @@ class TestCurvesROIWidget(TestCaseQt): self.widget.roiWidget.setRois((roi,)) self.widget.roiWidget.roiTable.setActiveRoi(None) - self.assertTrue(len(self.widget.roiWidget.roiTable.selectedItems()) is 0) + self.assertEqual(len(self.widget.roiWidget.roiTable.selectedItems()), 0) self.widget.roiWidget.setRois((roi,)) self.plot.setActiveCurve(legend='linearCurve') self.widget.calculateROIs() @@ -314,14 +330,128 @@ class TestCurvesROIWidget(TestCaseQt): self.widget.roiWidget.sigROISignal.connect(signalListener.partial()) self.widget.show() self.qapp.processEvents() - self.assertTrue(signalListener.callCount() is 0) + self.assertEqual(signalListener.callCount(), 0) self.assertTrue(self.widget.roiWidget.roiTable.activeRoi is roi) roi.setFrom(0.0) self.qapp.processEvents() - self.assertTrue(signalListener.callCount() is 0) + self.assertEqual(signalListener.callCount(), 0) roi.setFrom(0.3) self.qapp.processEvents() - self.assertTrue(signalListener.callCount() is 1) + self.assertEqual(signalListener.callCount(), 1) + + +class TestRoiWidgetSignals(TestCaseQt): + """Test Signals emitted by the RoiWidgetSignals""" + + def setUp(self): + self.plot = Plot1D() + x = range(20) + y = range(20) + self.plot.addCurve(x, y, legend='curve0') + self.listener = SignalListener() + self.curves_roi_widget = self.plot.getCurvesRoiWidget() + self.curves_roi_widget.sigROISignal.connect(self.listener) + assert self.curves_roi_widget.isVisible() is False + assert self.listener.callCount() == 0 + self.plot.show() + self.qWaitForWindowExposed(self.plot) + + toolButton = getQToolButtonFromAction(self.plot.getRoiAction()) + self.mouseClick(widget=toolButton, button=qt.Qt.LeftButton) + + self.curves_roi_widget.show() + self.qWaitForWindowExposed(self.curves_roi_widget) + + def tearDown(self): + self.plot = None + + def testSigROISignalAddRmRois(self): + """Test SigROISignal when adding and removing ROIS""" + print(self.listener.callCount()) + self.assertEqual(self.listener.callCount(), 1) + self.listener.clear() + + roi1 = CurvesROIWidget.ROI(name='linear', fromdata=0, todata=5) + self.curves_roi_widget.roiTable.addRoi(roi1) + self.assertEqual(self.listener.callCount(), 1) + self.assertTrue(self.listener.arguments()[0][0]['current'] == 'linear') + self.listener.clear() + + roi2 = CurvesROIWidget.ROI(name='linear2', fromdata=0, todata=5) + self.curves_roi_widget.roiTable.addRoi(roi2) + self.assertEqual(self.listener.callCount(), 1) + self.assertTrue(self.listener.arguments()[0][0]['current'] == 'linear2') + self.listener.clear() + + self.curves_roi_widget.roiTable.removeROI(roi2) + self.assertEqual(self.listener.callCount(), 1) + self.assertTrue(self.curves_roi_widget.roiTable.activeRoi == roi1) + self.assertTrue(self.listener.arguments()[0][0]['current'] == 'linear') + self.listener.clear() + + self.curves_roi_widget.roiTable.deleteActiveRoi() + self.assertEqual(self.listener.callCount(), 1) + self.assertTrue(self.curves_roi_widget.roiTable.activeRoi is None) + self.assertTrue(self.listener.arguments()[0][0]['current'] is None) + self.listener.clear() + + self.curves_roi_widget.roiTable.addRoi(roi1) + self.assertEqual(self.listener.callCount(), 1) + self.assertTrue(self.listener.arguments()[0][0]['current'] == 'linear') + self.assertTrue(self.curves_roi_widget.roiTable.activeRoi == roi1) + self.listener.clear() + self.qapp.processEvents() + + self.curves_roi_widget.roiTable.removeROI(roi1) + self.qapp.processEvents() + self.assertEqual(self.listener.callCount(), 1) + self.assertTrue(self.listener.arguments()[0][0]['current'] == 'ICR') + self.listener.clear() + + def testSigROISignalModifyROI(self): + """Test SigROISignal when modifying it""" + self.curves_roi_widget.roiTable.setMiddleROIMarkerFlag(True) + roi1 = CurvesROIWidget.ROI(name='linear', fromdata=2, todata=5) + self.curves_roi_widget.roiTable.addRoi(roi1) + self.curves_roi_widget.roiTable.setActiveRoi(roi1) + + # test modify the roi2 object + self.listener.clear() + roi1.setFrom(0.56) + self.assertEqual(self.listener.callCount(), 1) + self.listener.clear() + roi1.setTo(2.56) + self.assertEqual(self.listener.callCount(), 1) + self.listener.clear() + roi1.setName('linear2') + self.assertEqual(self.listener.callCount(), 1) + self.listener.clear() + roi1.setType('new type') + self.assertEqual(self.listener.callCount(), 1) + + # modify roi limits (from the gui) + roi_marker_handler = self.curves_roi_widget.roiTable._markersHandler.getMarkerHandler(roi1.getID()) + for marker_type in ('min', 'max', 'middle'): + with self.subTest(marker_type=marker_type): + self.listener.clear() + marker = roi_marker_handler.getMarker(marker_type) + self.qapp.processEvents() + items_interaction = ItemsInteraction(plot=self.plot) + x_pix, y_pix = self.plot.dataToPixel(marker.getXPosition(), 1) + items_interaction.beginDrag(x_pix, y_pix) + self.qapp.processEvents() + items_interaction.endDrag(x_pix+10, y_pix) + self.qapp.processEvents() + self.assertEqual(self.listener.callCount(), 1) + + def testSetActiveCurve(self): + """Test sigRoiSignal when set an active curve""" + roi1 = CurvesROIWidget.ROI(name='linear', fromdata=2, todata=5) + self.curves_roi_widget.roiTable.addRoi(roi1) + self.curves_roi_widget.roiTable.setActiveRoi(roi1) + self.listener.clear() + self.plot.setActiveCurve('curve0') + self.assertEqual(self.listener.callCount(), 0) def suite(): diff --git a/silx/gui/plot/test/testItem.py b/silx/gui/plot/test/testItem.py index 993cce7..c864545 100644 --- a/silx/gui/plot/test/testItem.py +++ b/silx/gui/plot/test/testItem.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2017 European Synchrotron Radiation Facility +# Copyright (c) 2017-2019 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 @@ -54,7 +54,7 @@ class TestSigItemChangedSignal(PlotWidgetTestCase): curve.setVisible(True) curve.setZValue(100) - # Test for signals in Points class + # Test for signals in PointsBase class curve.setData(numpy.arange(100), numpy.arange(100)) # SymbolMixIn @@ -194,14 +194,17 @@ class TestSigItemChangedSignal(PlotWidgetTestCase): # ColormapMixIn scatter.getColormap().setName('viridis') - data2 = data + 10 # Test of signals in Scatter class - scatter.setData(data2, data2, data2) + scatter.setData((0, 1, 2), (1, 0, 2), (0, 1, 2)) + + # Visualization mode changed + scatter.setVisualization(scatter.Visualization.SOLID) self.assertEqual(listener.arguments(), [(ItemChangedType.COLORMAP,), - (ItemChangedType.DATA,)]) + (ItemChangedType.DATA,), + (ItemChangedType.VISUALIZATION_MODE,)]) def testShapeChanged(self): """Test sigItemChanged for shape""" diff --git a/silx/gui/plot/test/testPlotWidget.py b/silx/gui/plot/test/testPlotWidget.py index 9d7c093..7449c12 100644 --- a/silx/gui/plot/test/testPlotWidget.py +++ b/silx/gui/plot/test/testPlotWidget.py @@ -386,6 +386,16 @@ class TestPlotImage(PlotWidgetTestCase, ParametricTestCase): self.assertTrue(numpy.all(numpy.equal(retrievedData, data))) self.assertIs(retrievedData.dtype.type, numpy.int8) + def testPlotAlphaImage(self): + """Test with an alpha image layer""" + data = numpy.random.random((10, 10)) + alpha = numpy.linspace(0, 1, 100).reshape(10, 10) + self.plot.addImage(data, legend='image') + image = self.plot.getActiveImage() + image.setData(data, alpha=alpha) + self.qapp.processEvents() + self.assertTrue(numpy.array_equal(alpha, image.getAlphaData())) + class TestPlotCurve(PlotWidgetTestCase): """Basic tests for addCurve.""" @@ -463,7 +473,34 @@ class TestPlotCurve(PlotWidgetTestCase): self.plot.addCurve(self.xData, self.yData, legend="curve 2", replace=False, resetzoom=False, - color=color, symbol='o') + color=color, symbol='o') + + +class TestPlotScatter(PlotWidgetTestCase, ParametricTestCase): + """Basic tests for addScatter""" + + def testScatter(self): + x = numpy.arange(100) + y = numpy.arange(100) + value = numpy.arange(100) + self.plot.addScatter(x, y, value) + self.plot.resetZoom() + + def testScatterVisualization(self): + self.plot.addScatter((0, 1, 2, 3), (2, 0, 2, 1), (0, 1, 2, 3)) + self.plot.resetZoom() + self.qapp.processEvents() + + scatter = self.plot.getItems()[0] + + for visualization in ('solid', + 'points', + scatter.Visualization.SOLID, + scatter.Visualization.POINTS): + with self.subTest(visualization=visualization): + scatter.setVisualization(visualization) + self.qapp.processEvents() + class TestPlotMarker(PlotWidgetTestCase): """Basic tests for add*Marker""" @@ -1524,11 +1561,19 @@ class TestPlotItemLog(PlotWidgetTestCase): def suite(): - testClasses = (TestPlotWidget, TestPlotImage, TestPlotCurve, - TestPlotMarker, TestPlotItem, TestPlotAxes, + testClasses = (TestPlotWidget, + TestPlotImage, + TestPlotCurve, + TestPlotScatter, + TestPlotMarker, + TestPlotItem, + TestPlotAxes, TestPlotActiveCurveImage, - TestPlotEmptyLog, TestPlotCurveLog, TestPlotImageLog, - TestPlotMarkerLog, TestPlotItemLog) + TestPlotEmptyLog, + TestPlotCurveLog, + TestPlotImageLog, + TestPlotMarkerLog, + TestPlotItemLog) test_suite = unittest.TestSuite() diff --git a/silx/gui/plot/test/testPlotWindow.py b/silx/gui/plot/test/testPlotWindow.py index 6d3eb8f..0a7d108 100644 --- a/silx/gui/plot/test/testPlotWindow.py +++ b/silx/gui/plot/test/testPlotWindow.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2016 European Synchrotron Radiation Facility +# Copyright (c) 2016-2019 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 @@ -38,32 +38,6 @@ from silx.gui import qt from silx.gui.plot import PlotWindow -# Test of the docstrings # - -# Makes sure a QApplication exists -_qapp = qt.QApplication.instance() or qt.QApplication([]) - - -def _tearDownQt(docTest): - """Tear down to use for test from docstring. - - Checks that plt widget is displayed - """ - _qapp.processEvents() - for obj in docTest.globs.values(): - if isinstance(obj, PlotWindow): - # Commented out as it takes too long - # qWaitForWindowExposedAndActivate(obj) - obj.setAttribute(qt.Qt.WA_DeleteOnClose) - obj.close() - del obj - - -plotWindowDocTestSuite = doctest.DocTestSuite('silx.gui.plot.PlotWindow', - tearDown=_tearDownQt) -"""Test suite of tests from the module's docstrings.""" - - class TestPlotWindow(TestCaseQt): """Base class for tests of PlotWindow.""" @@ -128,7 +102,6 @@ class TestPlotWindow(TestCaseQt): def suite(): test_suite = unittest.TestSuite() - test_suite.addTest(plotWindowDocTestSuite) test_suite.addTest( unittest.defaultTestLoader.loadTestsFromTestCase(TestPlotWindow)) return test_suite diff --git a/silx/gui/plot/test/testProfile.py b/silx/gui/plot/test/testProfile.py index 847f404..cf40f76 100644 --- a/silx/gui/plot/test/testProfile.py +++ b/silx/gui/plot/test/testProfile.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2016-2017 European Synchrotron Radiation Facility +# Copyright (c) 2016-2019 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 @@ -39,10 +39,6 @@ from silx.gui.plot import PlotWindow, Plot1D, Plot2D, Profile from silx.gui.plot.StackView import StackView -# Makes sure a QApplication exists -_qapp = qt.QApplication.instance() or qt.QApplication([]) - - class TestProfileToolBar(TestCaseQt, ParametricTestCase): """Tests for ProfileToolBar widget.""" diff --git a/silx/gui/plot/test/testStackView.py b/silx/gui/plot/test/testStackView.py index a5f649c..80c85d6 100644 --- a/silx/gui/plot/test/testStackView.py +++ b/silx/gui/plot/test/testStackView.py @@ -1,7 +1,7 @@ # coding: utf-8 # /*########################################################################## # -# Copyright (c) 2016-2018 European Synchrotron Radiation Facility +# Copyright (c) 2016-2019 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 @@ -41,10 +41,6 @@ from silx.gui.plot.StackView import StackViewMainWindow from silx.utils.array_like import ListOfImages -# Makes sure a QApplication exists -_qapp = qt.QApplication.instance() or qt.QApplication([]) - - class TestStackView(TestCaseQt): """Base class for tests of StackView.""" diff --git a/silx/gui/plot/test/testStats.py b/silx/gui/plot/test/testStats.py index 7fbc247..4bc2144 100644 --- a/silx/gui/plot/test/testStats.py +++ b/silx/gui/plot/test/testStats.py @@ -33,8 +33,9 @@ from silx.gui import qt from silx.gui.plot.stats import stats from silx.gui.plot import StatsWidget from silx.gui.plot.stats import statshandler -from silx.gui.utils.testutils import TestCaseQt +from silx.gui.utils.testutils import TestCaseQt, SignalListener from silx.gui.plot import Plot1D, Plot2D +from silx.utils.testutils import ParametricTestCase import unittest import logging import numpy @@ -350,7 +351,7 @@ class TestStatsHandler(unittest.TestCase): statshandler.StatsHandler(('name')) -class TestStatsWidgetWithCurves(TestCaseQt): +class TestStatsWidgetWithCurves(TestCaseQt, ParametricTestCase): """Basic test for StatsWidget with curves""" def setUp(self): TestCaseQt.setUp(self) @@ -363,7 +364,8 @@ class TestStatsWidgetWithCurves(TestCaseQt): self.plot.addCurve(x, y, legend='curve1') y = range(-2, 18) self.plot.addCurve(x, y, legend='curve2') - self.widget = StatsWidget.StatsTable(plot=self.plot) + self.widget = StatsWidget.StatsWidget(plot=self.plot) + self.statsTable = self.widget._statsTable mystats = statshandler.StatsHandler(( stats.StatMin(), @@ -376,67 +378,170 @@ class TestStatsWidgetWithCurves(TestCaseQt): stats.StatCOM() )) - self.widget.setStats(mystats) + self.statsTable.setStats(mystats) def tearDown(self): self.plot.setAttribute(qt.Qt.WA_DeleteOnClose) self.plot.close() + self.statsTable = None self.widget.setAttribute(qt.Qt.WA_DeleteOnClose) self.widget.close() self.widget = None self.plot = None TestCaseQt.tearDown(self) + def testDisplayActiveItemsSyncOptions(self): + """ + Test that the several option of the sync options are well + synchronized between the different object""" + widget = StatsWidget.StatsWidget(plot=self.plot) + table = StatsWidget.StatsTable(plot=self.plot) + + def check_display_only_active_item(only_active): + # check internal value + self.assertTrue(widget._statsTable._displayOnlyActItem is only_active) + # self.assertTrue(table._displayOnlyActItem is only_active) + # check gui display + self.assertTrue(widget._options.isActiveItemMode() is only_active) + + for displayOnlyActiveItems in (True, False): + with self.subTest(displayOnlyActiveItems=displayOnlyActiveItems): + widget.setDisplayOnlyActiveItem(displayOnlyActiveItems) + # table.setDisplayOnlyActiveItem(displayOnlyActiveItems) + check_display_only_active_item(displayOnlyActiveItems) + + check_display_only_active_item(only_active=False) + widget.setAttribute(qt.Qt.WA_DeleteOnClose) + table.setAttribute(qt.Qt.WA_DeleteOnClose) + widget.close() + table.close() + def testInit(self): """Make sure all the curves are registred on initialization""" - self.assertEqual(self.widget.rowCount(), 3) + self.assertEqual(self.statsTable.rowCount(), 3) def testRemoveCurve(self): """Make sure the Curves stats take into account the curve removal from plot""" self.plot.removeCurve('curve2') - self.assertEqual(self.widget.rowCount(), 2) + self.assertEqual(self.statsTable.rowCount(), 2) for iRow in range(2): - self.assertTrue(self.widget.item(iRow, 0).text() in ('curve0', 'curve1')) + self.assertTrue(self.statsTable.item(iRow, 0).text() in ('curve0', 'curve1')) self.plot.removeCurve('curve0') - self.assertEqual(self.widget.rowCount(), 1) + self.assertEqual(self.statsTable.rowCount(), 1) self.plot.removeCurve('curve1') - self.assertEqual(self.widget.rowCount(), 0) + self.assertEqual(self.statsTable.rowCount(), 0) def testAddCurve(self): """Make sure the Curves stats take into account the add curve action""" self.plot.addCurve(legend='curve3', x=range(10), y=range(10)) - self.assertEqual(self.widget.rowCount(), 4) + self.assertEqual(self.statsTable.rowCount(), 4) def testUpdateCurveFromAddCurve(self): """Make sure the stats of the cuve will be removed after updating a curve""" self.plot.addCurve(legend='curve0', x=range(10), y=range(10)) self.qapp.processEvents() - self.assertEqual(self.widget.rowCount(), 3) + self.assertEqual(self.statsTable.rowCount(), 3) curve = self.plot._getItem(kind='curve', legend='curve0') - tableItems = self.widget._itemToTableItems(curve) + tableItems = self.statsTable._itemToTableItems(curve) self.assertEqual(tableItems['max'].text(), '9') def testUpdateCurveFromCurveObj(self): self.plot.getCurve('curve0').setData(x=range(4), y=range(4)) self.qapp.processEvents() - self.assertEqual(self.widget.rowCount(), 3) + self.assertEqual(self.statsTable.rowCount(), 3) curve = self.plot._getItem(kind='curve', legend='curve0') - tableItems = self.widget._itemToTableItems(curve) + tableItems = self.statsTable._itemToTableItems(curve) self.assertEqual(tableItems['max'].text(), '3') def testSetAnotherPlot(self): plot2 = Plot1D() plot2.addCurve(x=range(26), y=range(26), legend='new curve') - self.widget.setPlot(plot2) - self.assertEqual(self.widget.rowCount(), 1) + self.statsTable.setPlot(plot2) + self.assertEqual(self.statsTable.rowCount(), 1) self.qapp.processEvents() plot2.setAttribute(qt.Qt.WA_DeleteOnClose) plot2.close() plot2 = None + def testUpdateMode(self): + """Make sure the update modes are well take into account""" + self.plot.setActiveCurve('curve0') + for display_only_active in (True, False): + with self.subTest(display_only_active=display_only_active): + self.widget.setDisplayOnlyActiveItem(display_only_active) + self.plot.getCurve('curve0').setData(x=range(4), y=range(4)) + self.widget.setUpdateMode(StatsWidget.UpdateMode.AUTO) + update_stats_action = self.widget._options.getUpdateStatsAction() + # test from api + self.assertTrue(self.widget.getUpdateMode() is StatsWidget.UpdateMode.AUTO) + self.widget.show() + # check stats change in auto mode + self.plot.getCurve('curve0').setData(x=range(4), y=range(-1, 3)) + self.qapp.processEvents() + tableItems = self.statsTable._itemToTableItems(self.plot.getCurve('curve0')) + curve0_min = tableItems['min'].text() + print(curve0_min) + self.assertTrue(float(curve0_min) == -1.) + + self.plot.getCurve('curve0').setData(x=range(4), y=range(1, 5)) + self.qapp.processEvents() + tableItems = self.statsTable._itemToTableItems(self.plot.getCurve('curve0')) + curve0_min = tableItems['min'].text() + self.assertTrue(float(curve0_min) == 1.) + + # check stats change in manual mode only if requested + self.widget.setUpdateMode(StatsWidget.UpdateMode.MANUAL) + self.assertTrue(self.widget.getUpdateMode() is StatsWidget.UpdateMode.MANUAL) + + self.plot.getCurve('curve0').setData(x=range(4), y=range(2, 6)) + self.qapp.processEvents() + tableItems = self.statsTable._itemToTableItems(self.plot.getCurve('curve0')) + curve0_min = tableItems['min'].text() + self.assertTrue(float(curve0_min) == 1.) + + update_stats_action.trigger() + tableItems = self.statsTable._itemToTableItems(self.plot.getCurve('curve0')) + curve0_min = tableItems['min'].text() + self.assertTrue(float(curve0_min) == 2.) + + def testItemHidden(self): + """Test if an item is hide, then the associated stats item is also + hide""" + curve0 = self.plot.getCurve('curve0') + curve1 = self.plot.getCurve('curve1') + curve2 = self.plot.getCurve('curve2') + + self.plot.show() + self.widget.show() + self.qWaitForWindowExposed(self.widget) + self.assertFalse(self.statsTable.isRowHidden(0)) + self.assertFalse(self.statsTable.isRowHidden(1)) + self.assertFalse(self.statsTable.isRowHidden(2)) + + curve0.setVisible(False) + self.qapp.processEvents() + self.assertTrue(self.statsTable.isRowHidden(0)) + curve0.setVisible(True) + self.qapp.processEvents() + self.assertFalse(self.statsTable.isRowHidden(0)) + curve1.setVisible(False) + self.qapp.processEvents() + self.assertTrue(self.statsTable.isRowHidden(1)) + tableItems = self.statsTable._itemToTableItems(curve2) + curve2_min = tableItems['min'].text() + self.assertTrue(float(curve2_min) == -2.) + + curve0.setVisible(False) + curve1.setVisible(False) + curve2.setVisible(False) + self.qapp.processEvents() + self.assertTrue(self.statsTable.isRowHidden(0)) + self.assertTrue(self.statsTable.isRowHidden(1)) + self.assertTrue(self.statsTable.isRowHidden(2)) + class TestStatsWidgetWithImages(TestCaseQt): """Basic test for StatsWidget with images""" @@ -487,6 +592,17 @@ class TestStatsWidgetWithImages(TestCaseQt): self.assertEqual(tableItems['coords min'].text(), '0.0, 0.0') self.assertEqual(tableItems['coords max'].text(), '127.0, 127.0') + def testItemHidden(self): + """Test if an item is hide, then the associated stats item is also + hide""" + self.widget.show() + self.plot.show() + self.qWaitForWindowExposed(self.widget) + self.assertFalse(self.widget.isRowHidden(0)) + self.plot.getImage(self.IMAGE_LEGEND).setVisible(False) + self.qapp.processEvents() + self.assertTrue(self.widget.isRowHidden(0)) + class TestStatsWidgetWithScatters(TestCaseQt): @@ -556,13 +672,13 @@ class TestLineWidget(TestCaseQt): self.plot = Plot1D() self.plot.show() - x = range(20) - y = range(20) - self.plot.addCurve(x, y, legend='curve0') - y = range(12, 32) - self.plot.addCurve(x, y, legend='curve1') - y = range(-2, 18) - self.plot.addCurve(x, y, legend='curve2') + self.x = range(20) + self.y0 = range(20) + self.curve0 = self.plot.addCurve(self.x, self.y0, legend='curve0') + self.y1 = range(12, 32) + self.plot.addCurve(self.x, self.y1, legend='curve1') + self.y2 = range(-2, 18) + self.plot.addCurve(self.x, self.y2, legend='curve2') self.widget = StatsWidget.BasicGridStatsWidget(plot=self.plot, kind='curve', stats=mystats) @@ -572,33 +688,112 @@ class TestLineWidget(TestCaseQt): self.plot.setAttribute(qt.Qt.WA_DeleteOnClose) self.plot.close() self.widget.setPlot(None) - self.widget._statQlineEdit.clear() + self.widget._lineStatsWidget._statQlineEdit.clear() self.widget.setAttribute(qt.Qt.WA_DeleteOnClose) self.widget.close() self.widget = None self.plot = None TestCaseQt.tearDown(self) - def test(self): - self.widget.setStatsOnVisibleData(False) + def testProcessing(self): + self.widget._lineStatsWidget.setStatsOnVisibleData(False) self.qapp.processEvents() self.plot.setActiveCurve(legend='curve0') - self.assertTrue(self.widget._statQlineEdit['min'].text() == '0.000') + self.assertTrue(self.widget._lineStatsWidget._statQlineEdit['min'].text() == '0.000') self.plot.setActiveCurve(legend='curve1') - self.assertTrue(self.widget._statQlineEdit['min'].text() == '12.000') + self.assertTrue(self.widget._lineStatsWidget._statQlineEdit['min'].text() == '12.000') self.plot.getXAxis().setLimitsConstraints(minPos=2, maxPos=5) self.widget.setStatsOnVisibleData(True) self.qapp.processEvents() - self.assertTrue(self.widget._statQlineEdit['min'].text() == '14.000') + self.assertTrue(self.widget._lineStatsWidget._statQlineEdit['min'].text() == '14.000') self.plot.setActiveCurve(None) self.assertTrue(self.plot.getActiveCurve() is None) self.widget.setStatsOnVisibleData(False) self.qapp.processEvents() - self.assertFalse(self.widget._statQlineEdit['min'].text() == '14.000') + self.assertFalse(self.widget._lineStatsWidget._statQlineEdit['min'].text() == '14.000') self.widget.setKind('image') self.plot.addImage(numpy.arange(100*100).reshape(100, 100) + 0.312) self.qapp.processEvents() - self.assertTrue(self.widget._statQlineEdit['min'].text() == '0.312') + self.assertTrue(self.widget._lineStatsWidget._statQlineEdit['min'].text() == '0.312') + + def testUpdateMode(self): + """Make sure the update modes are well take into account""" + self.plot.setActiveCurve(self.curve0) + _autoRB = self.widget._options._autoRB + _manualRB = self.widget._options._manualRB + # test from api + self.widget.setUpdateMode(StatsWidget.UpdateMode.AUTO) + self.assertTrue(_autoRB.isChecked()) + self.assertFalse(_manualRB.isChecked()) + + # check stats change in auto mode + curve0_min = self.widget._lineStatsWidget._statQlineEdit['min'].text() + new_y = numpy.array(self.y0) - 2.56 + self.plot.addCurve(x=self.x, y=new_y, legend=self.curve0) + curve0_min2 = self.widget._lineStatsWidget._statQlineEdit['min'].text() + self.assertTrue(curve0_min != curve0_min2) + + # check stats change in manual mode only if requested + self.widget.setUpdateMode(StatsWidget.UpdateMode.MANUAL) + self.assertFalse(_autoRB.isChecked()) + self.assertTrue(_manualRB.isChecked()) + + new_y = numpy.array(self.y0) - 1.2 + self.plot.addCurve(x=self.x, y=new_y, legend=self.curve0) + curve0_min3 = self.widget._lineStatsWidget._statQlineEdit['min'].text() + self.assertTrue(curve0_min3 == curve0_min2) + self.widget._options._updateRequested() + curve0_min3 = self.widget._lineStatsWidget._statQlineEdit['min'].text() + self.assertTrue(curve0_min3 != curve0_min2) + + # test from gui + self.widget.showRadioButtons(True) + self.widget._options._autoRB.toggle() + self.assertTrue(_autoRB.isChecked()) + self.assertFalse(_manualRB.isChecked()) + + self.widget._options._manualRB.toggle() + self.assertFalse(_autoRB.isChecked()) + self.assertTrue(_manualRB.isChecked()) + + +class TestUpdateModeWidget(TestCaseQt): + """Test UpdateModeWidget""" + def setUp(self): + TestCaseQt.setUp(self) + self.widget = StatsWidget.UpdateModeWidget(parent=None) + + def tearDown(self): + self.widget.setAttribute(qt.Qt.WA_DeleteOnClose) + self.widget.close() + self.widget = None + TestCaseQt.tearDown(self) + + def testSignals(self): + """Test the signal emission of the widget""" + self.widget.setUpdateMode(StatsWidget.UpdateMode.AUTO) + modeChangedListener = SignalListener() + manualUpdateListener = SignalListener() + self.widget.sigUpdateModeChanged.connect(modeChangedListener) + self.widget.sigUpdateRequested.connect(manualUpdateListener) + self.widget.setUpdateMode(StatsWidget.UpdateMode.AUTO) + self.assertTrue(self.widget.getUpdateMode() is StatsWidget.UpdateMode.AUTO) + self.assertTrue(modeChangedListener.callCount() is 0) + self.qapp.processEvents() + + self.widget.setUpdateMode(StatsWidget.UpdateMode.MANUAL) + self.assertTrue(self.widget.getUpdateMode() is StatsWidget.UpdateMode.MANUAL) + self.qapp.processEvents() + self.assertTrue(modeChangedListener.callCount() is 1) + self.assertTrue(manualUpdateListener.callCount() is 0) + self.widget._updatePB.click() + self.widget._updatePB.click() + self.assertTrue(manualUpdateListener.callCount() is 2) + + self.widget._autoRB.setChecked(True) + self.assertTrue(modeChangedListener.callCount() is 2) + self.widget._updatePB.click() + self.assertTrue(manualUpdateListener.callCount() is 2) def suite(): @@ -606,7 +801,7 @@ def suite(): for TestClass in (TestStats, TestStatsHandler, TestStatsWidgetWithScatters, TestStatsWidgetWithImages, TestStatsWidgetWithCurves, TestStatsFormatter, TestEmptyStatsWidget, - TestLineWidget): + TestLineWidget, TestUpdateModeWidget): test_suite.addTest( unittest.defaultTestLoader.loadTestsFromTestCase(TestClass)) return test_suite |