Changeset c4e5400 in sasview for src/sas/qtgui/UnitTesting


Ignore:
Timestamp:
Dec 12, 2016 5:21:03 AM (7 years ago)
Author:
Piotr Rozyczko <rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
27313b7
Parents:
3b7b218
Message:

Refactored context menu setup for plots

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/UnitTesting/PlotterBaseTest.py

    r3b7b218 rc4e5400  
    11import sys 
    22import unittest 
    3 from mock import MagicMock 
     3from mock import patch, MagicMock 
    44 
    55from PyQt4 import QtGui 
     
    1515#import sas.qtgui.GuiUtils as GuiUtils 
    1616from sas.qtgui.GuiUtils import * 
     17import sas.qtgui.PlotHelper as PlotHelper 
    1718 
    1819# Tested module 
     
    3031            def perspective(self): 
    3132                return MyPerspective() 
    32  
    33         #PlotterBase.PlotterBase.updatePlotHelper = MagicMock() 
    34         #self.plotter = PlotterBase.PlotterBase(None, manager=dummy_manager()) 
    3533 
    3634        PlotterBase.PlotterBase.contextMenuQuickPlot = MagicMock() 
     
    6462    def testContextMenu(self): 
    6563        ''' Test the default context menu ''' 
    66         pass 
    67  
    68     def testContextMenuQuickPlot(self): 
    69         ''' Test the default quick context menu ''' 
    70         pass 
     64        with self.assertRaises(NotImplementedError): 
     65            self.plotter.contextMenu() 
    7166 
    7267    def testClean(self): 
    7368        ''' test the graph cleanup ''' 
    74         pass 
     69        self.plotter.figure.delaxes = MagicMock() 
     70        self.plotter.clean() 
     71        self.assertTrue(self.plotter.figure.delaxes.called) 
    7572 
    7673    def testPlot(self): 
     
    7976            self.plotter.plot() 
    8077 
    81     def testOnCloseEvent(self): 
     78    def notestOnCloseEvent(self): 
    8279        ''' test the plotter close behaviour ''' 
    83         pass 
     80        PlotHelper.deletePlot = MagicMock() 
     81        self.plotter.closeEvent(None) 
     82        self.assertTrue(PlotHelper.deletePlot.called) 
    8483 
    8584    def testOnImageSave(self): 
    8685        ''' test the workspace save ''' 
    87         pass 
     86        self.plotter.toolbar.save_figure = MagicMock() 
     87        self.plotter.onImageSave() 
     88        self.assertTrue(self.plotter.toolbar.save_figure.called) 
    8889 
    8990    def testOnImagePrint(self): 
    9091        ''' test the workspace print ''' 
    91         pass 
     92        QtGui.QPainter.end = MagicMock() 
     93        QtGui.QLabel.render = MagicMock() 
    9294 
    93     def tesOonClipboardCopy(self): 
    94         ''' test the workspace copy ''' 
    95         pass 
     95        # First, let's cancel printing 
     96        QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) 
     97        self.plotter.onImagePrint() 
     98        self.assertFalse(QtGui.QPainter.end.called) 
     99        self.assertFalse(QtGui.QLabel.render.called) 
     100 
     101        # Let's print now 
     102        QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Accepted) 
     103        self.plotter.onImagePrint() 
     104        self.assertTrue(QtGui.QPainter.end.called) 
     105        self.assertTrue(QtGui.QLabel.render.called) 
     106 
     107    def testOnClipboardCopy(self): 
     108        ''' test the workspace screen copy ''' 
     109        QtGui.QClipboard.setPixmap = MagicMock() 
     110        self.plotter.onClipboardCopy() 
     111        self.assertTrue(QtGui.QClipboard.setPixmap.called) 
    96112 
    97113    def testOnGridToggle(self): 
    98114        ''' test toggling the grid lines ''' 
    99         pass 
     115        # Check the toggle 
     116        orig_toggle = self.plotter.grid_on 
     117         
     118        FigureCanvas.draw_idle = MagicMock() 
     119        self.plotter.onGridToggle() 
     120 
     121        self.assertTrue(FigureCanvas.draw_idle.called) 
     122        self.assertTrue(self.plotter.grid_on != orig_toggle) 
     123 
     124    def testDefaultContextMenu(self): 
     125        """ Test the right click default menu """ 
     126 
     127        self.plotter.defaultContextMenu() 
     128 
     129        actions = self.plotter.contextMenu.actions() 
     130        self.assertEqual(len(actions), 4) 
     131 
     132        # Trigger Save Image and make sure the method is called 
     133        self.assertEqual(actions[0].text(), "Save Image") 
     134        self.plotter.toolbar.save_figure = MagicMock() 
     135        actions[0].trigger() 
     136        self.assertTrue(self.plotter.toolbar.save_figure.called) 
     137 
     138        # Trigger Print Image and make sure the method is called 
     139        self.assertEqual(actions[1].text(), "Print Image") 
     140        QtGui.QPrintDialog.exec_ = MagicMock(return_value=QtGui.QDialog.Rejected) 
     141        actions[1].trigger() 
     142        self.assertTrue(QtGui.QPrintDialog.exec_.called) 
     143 
     144        # Trigger Copy to Clipboard and make sure the method is called 
     145        self.assertEqual(actions[2].text(), "Copy to Clipboard") 
     146 
     147        # Spy on cliboard's dataChanged() signal 
     148        self.clipboard_called = False 
     149        def done(): 
     150            self.clipboard_called = True 
     151        QtCore.QObject.connect(app.clipboard(), QtCore.SIGNAL("dataChanged()"), done) 
     152        actions[2].trigger() 
     153        QtGui.qApp.processEvents() 
     154        # Make sure clipboard got updated. 
     155        self.assertTrue(self.clipboard_called) 
    100156 
    101157 
Note: See TracChangeset for help on using the changeset viewer.