Skip to content

Commit 0fc384f

Browse files
committed
MNT: add shared test helper function clickcell
Factor out `TestParametersPanel._clickcell` to a shared function.
1 parent b7e395b commit 0fc384f

2 files changed

Lines changed: 39 additions & 24 deletions

File tree

src/diffpy/pdfgui/tests/TestParametersPanel.py

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
from diffpy.pdfgui.gui.parameterspanel import ParametersPanel
2525
from diffpy.pdfgui.control.parameter import Parameter
26-
from diffpy.pdfgui.tests.testutils import GUITestCase
26+
from diffpy.pdfgui.tests.testutils import GUITestCase, clickcell
2727

2828
# ----------------------------------------------------------------------------
2929

@@ -48,22 +48,6 @@ def tearDown(self):
4848
return
4949

5050

51-
def _clickcell(self, leftright, row, col, **kw):
52-
gp = self.panel.grid_parameters
53-
assert leftright in ('left', 'right')
54-
if leftright == "left":
55-
eventtype = wx.grid.EVT_GRID_CELL_LEFT_CLICK.typeId
56-
else:
57-
eventtype = wx.grid.EVT_GRID_CELL_RIGHT_CLICK.typeId
58-
kbd = {'kbd': wx.KeyboardState(**kw)}
59-
# TODO: remove this after deprecations of wxpython 3
60-
if wx.VERSION[0] == 3:
61-
kbd = {k.replace('Down', ''): v for k, v in kw.items()}
62-
e = wx.grid.GridEvent(gp.Id, eventtype, gp, row, col, **kbd)
63-
gp.ProcessEvent(e)
64-
return
65-
66-
6751
def test_onPopupFixFree(self):
6852
"Check ParametersPanel.onPopupFixFree"
6953
# event is not used, we just generate and reuse dummy event.
@@ -108,21 +92,21 @@ def test_onCellLeftClick(self):
10892
self.assertFalse(self.panel.mainFrame.altered)
10993
self.assertEqual("0", gp.GetCellValue(0, 1))
11094
self.assertFalse(p.fixed)
111-
self._clickcell("left", 0, 1)
95+
clickcell(gp, "left", 0, 1)
11296
self.assertEqual("1", gp.GetCellValue(0, 1))
11397
self.assertTrue(p.fixed)
11498
self.assertTrue(self.panel.mainFrame.altered)
115-
self._clickcell("left", 0, 1)
99+
clickcell(gp, "left", 0, 1)
116100
self.assertEqual("0", gp.GetCellValue(0, 1))
117-
self._clickcell("left", 0, 1, controlDown=True)
101+
clickcell(gp, "left", 0, 1, controlDown=True)
118102
self.assertEqual("0", gp.GetCellValue(0, 1))
119-
self._clickcell("left", 0, 1, shiftDown=True)
103+
clickcell(gp, "left", 0, 1, shiftDown=True)
120104
self.assertEqual("0", gp.GetCellValue(0, 1))
121105
gp.SelectAll()
122-
self._clickcell("left", 0, 1)
106+
clickcell(gp, "left", 0, 1)
123107
self.assertEqual("0", gp.GetCellValue(0, 1))
124108
gp.ClearSelection()
125-
self._clickcell("left", 0, 1)
109+
clickcell(gp, "left", 0, 1)
126110
self.assertEqual("1", gp.GetCellValue(0, 1))
127111
return
128112

@@ -133,7 +117,7 @@ def test_onCellRightClick(self):
133117
gp = self.panel.grid_parameters
134118
gp.PopupMenu = lambda menu, pos: None
135119
try:
136-
self._clickcell("right", 0, 1)
120+
clickcell(gp, "right", 0, 1)
137121
finally:
138122
del gp.PopupMenu
139123
self.assertTrue(self.panel.did_popupIDs)

src/diffpy/pdfgui/tests/testutils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,37 @@ def tooltiptext(widget):
7676
tt = widget.GetToolTip()
7777
return tt.GetTip()
7878

79+
80+
def clickcell(grid, leftright, row, col, **kw):
81+
"""Simulate left or right mouse click over wx.grid.Grid
82+
83+
Parameters
84+
----------
85+
grid : wx.grid.Grid
86+
The grid object to be clicked
87+
leftright : str
88+
The button that is clicked, possible values are ("left", "right").
89+
row : int
90+
The zero-based row of the clicked cell.
91+
col : int
92+
The zero-based columnt of the clicked cell.
93+
kw : misc, optional
94+
Keyword arguments for the wx.GridEvent constructor.
95+
Typically a keyboard modifier for the click.
96+
"""
97+
assert leftright in ('left', 'right')
98+
if leftright == "left":
99+
eventtype = wx.grid.EVT_GRID_CELL_LEFT_CLICK.typeId
100+
else:
101+
eventtype = wx.grid.EVT_GRID_CELL_RIGHT_CLICK.typeId
102+
kbd = {'kbd': wx.KeyboardState(**kw)}
103+
# TODO: remove this after deprecations of wxpython 3
104+
if wx.VERSION[0] == 3:
105+
kbd = {k.replace('Down', ''): v for k, v in kw.items()}
106+
e = wx.grid.GridEvent(grid.Id, eventtype, grid, row, col, **kbd)
107+
grid.ProcessEvent(e)
108+
return
109+
79110
# GUI-specialized TestCase ---------------------------------------------------
80111

81112
class GUITestCase(TestCase):

0 commit comments

Comments
 (0)