Commit ca6b60fb authored by Paul McCarthy's avatar Paul McCarthy
Browse files

Unit tests for autotextctrl module

parent 0cdbf68c
......@@ -45,3 +45,49 @@ def compare_images(img1, img2, threshold):
passed = ttlDiff <= threshold
return passed, ttlDiff
def run_with_wx(func, *args, **kwargs):
propagateRaise = kwargs.pop('propagateRaise', True)
startingDelay = kwargs.pop('startingDelay', 500)
finishingDelay = kwargs.pop('finishingDelay', 500)
callAfterApp = kwargs.pop('callAfterApp', None)
import wx
result = [None]
raised = [None]
app = wx.App()
frame = wx.Frame(None)
if callAfterApp is not None:
callAfterApp()
def wrap():
try:
if func is not None:
result[0] = func(*args, **kwargs)
except Exception as e:
print(e)
raised[0] = e
finally:
def finish():
frame.Destroy()
app.ExitMainLoop()
wx.CallLater(finishingDelay, finish)
frame.Show()
wx.CallLater(startingDelay, wrap)
app.MainLoop()
if raised[0] and propagateRaise:
raise raised[0]
return result[0]
#!/usr/bin/env python
#
# test_autotextctrl.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#
from . import run_with_wx
import wx
import fsleyes_widgets.autotextctrl as autott
def sendEvent(target, evType, source=None):
if source is None:
source = target
target.ProcessEvent(wx.CommandEvent(evType, source.GetId()))
wx.Yield()
# Simple test - programmatically
# set, then retrieve the value
def test_AutoTextCtrl_getSet():
run_with_wx( _test_AutoTextCtrl_getSet)
def _test_AutoTextCtrl_getSet():
parent = wx.GetApp().GetTopWindow()
atc = autott.AutoTextCtrl(parent)
atc.ChangeValue('a')
assert atc.GetValue() == 'a'
atc.SetValue('b')
assert atc.GetValue() == 'b'
# Make sure that when the cotrol receives
# focus, its insertion point is at the end
def test_AutoTextCtrl_onFocus():
run_with_wx(_test_AutoTextCtrl_onFocus)
def _test_AutoTextCtrl_onFocus():
parent = wx.GetApp().GetTopWindow()
atc = autott.AutoTextCtrl(parent)
atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])
atc.ChangeValue('a')
atc.SetInsertionPoint(0)
sendEvent(atc, wx.wxEVT_KILL_FOCUS)
assert atc.GetInsertionPoint() == 0
sendEvent(atc, wx.wxEVT_SET_FOCUS)
assert atc.GetInsertionPoint() == 1
# Test showing the popup and selecting a value
def test_AutoTextCtrl_popup_select1():
run_with_wx(_test_AutoTextCtrl_popup_select1)
def _test_AutoTextCtrl_popup_select1():
sim = wx.UIActionSimulator()
parent = wx.GetApp().GetTopWindow()
atc = autott.AutoTextCtrl(parent)
atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])
atc.SetFocus()
sim.KeyDown(wx.WXK_RETURN)
sim.KeyDown(wx.WXK_DOWN)
sim.KeyDown(wx.WXK_DOWN)
sim.KeyDown(wx.WXK_RETURN)
wx.Yield()
assert atc.GetValue() == 'aab'
def test_AutoTextCtrl_popup_select2():
run_with_wx(_test_AutoTextCtrl_popup_select2)
def _test_AutoTextCtrl_popup_select2():
sim = wx.UIActionSimulator()
parent = wx.GetApp().GetTopWindow()
atc = autott.AutoTextCtrl(parent)
atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])
atc.SetFocus()
sim.Char(ord('b'))
sim.KeyDown(wx.WXK_DOWN)
sim.KeyDown(wx.WXK_RETURN)
wx.Yield()
assert atc.GetValue() == 'bcc'
def test_AutoTextCtrl_popup_select3():
run_with_wx(_test_AutoTextCtrl_popup_select3)
def _test_AutoTextCtrl_popup_select3():
sim = wx.UIActionSimulator()
parent = wx.GetApp().GetTopWindow()
atc = autott.AutoTextCtrl(parent)
atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])
atc.SetFocus()
sim.Char(ord('a'))
sim.Char(ord('b'))
sim.KeyDown(wx.WXK_DOWN)
sim.KeyDown(wx.WXK_RETURN)
wx.Yield()
assert atc.GetValue() == 'aba'
def test_AutoTextCtrl_popup_cancel():
run_with_wx(_test_AutoTextCtrl_popup_cancel)
def _test_AutoTextCtrl_popup_cancel():
sim = wx.UIActionSimulator()
parent = wx.GetApp().GetTopWindow()
atc = autott.AutoTextCtrl(parent)
atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])
atc.SetFocus()
sim.KeyDown(wx.WXK_RETURN)
sim.KeyDown(wx.WXK_DOWN)
sim.KeyDown(wx.WXK_ESCAPE)
wx.Yield()
assert atc.GetValue() == ''
def test_AutoTextCtrl_popup_focusback():
run_with_wx(_test_AutoTextCtrl_popup_focusback)
def _test_AutoTextCtrl_popup_focusback():
sim = wx.UIActionSimulator()
parent = wx.GetApp().GetTopWindow()
atc = autott.AutoTextCtrl(parent)
atc.AutoComplete(['aaa', 'aab', 'aba', 'bcc'])
atc.SetFocus()
sim.KeyDown(wx.WXK_RETURN)
sim.KeyDown(wx.WXK_DOWN)
sim.KeyDown(wx.WXK_UP)
sim.Text('abc')
sim.KeyDown(wx.WXK_RETURN)
wx.Yield()
assert atc.GetValue() == 'abc'
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment