andrej@1811: #!/usr/bin/env python
andrej@1811: # -*- coding: utf-8 -*-
andrej@1811: 
andrej@1811: # This file is part of Beremiz, a Integrated Development Environment for
andrej@1811: # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
andrej@1811: #
andrej@1811: # Copyright (C) 2017: Andrey Skvortsov
andrej@1811: #
andrej@1811: # See COPYING file for copyrights details.
andrej@1811: #
andrej@1811: # This program is free software; you can redistribute it and/or
andrej@1811: # modify it under the terms of the GNU General Public License
andrej@1811: # as published by the Free Software Foundation; either version 2
andrej@1811: # of the License, or (at your option) any later version.
andrej@1811: #
andrej@1811: # This program is distributed in the hope that it will be useful,
andrej@1811: # but WITHOUT ANY WARRANTY; without even the implied warranty of
andrej@1811: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
andrej@1811: # GNU General Public License for more details.
andrej@1811: #
andrej@1811: # You should have received a copy of the GNU General Public License
andrej@1811: # along with this program; if not, write to the Free Software
andrej@1811: # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
andrej@1811: 
andrej@1881: 
andrej@1881: from __future__ import absolute_import
andrej@1811: import unittest
andrej@1832: import time
andrej@1832: 
andrej@1811: import wx
andrej@1811: import conftest
andrej@1811: import controls.CustomIntCtrl
andrej@1811: 
andrej@1811: 
andrej@1811: class TestCustomIntCtrl(unittest.TestCase):
andrej@1811:     def setUp(self):
andrej@1811:         self.app = wx.App()
andrej@1811:         self.frame = wx.Frame(None)
andrej@1811: 
andrej@1811:     def tearDown(self):
andrej@1811:         self.frame.Destroy()
andrej@1811:         wx.CallAfter(self.app.Exit)
andrej@1811:         self.app.MainLoop()
andrej@1811: 
andrej@1811:     def testMaxLimit(self):
andrej@1811:         """Test working upper bound"""
andrej@1811:         self.AddControls()
andrej@1811:         self.int_ctrl.SetValue(self.max_val + 100)
andrej@1811:         self.ProcessEvents()
andrej@1811: 
andrej@1811:         self.txt_ctrl.SetFocus()
andrej@1811:         self.ProcessEvents()
andrej@1811:         self.assertEqual(self.int_ctrl.GetValue(), self.max_val)
andrej@1811: 
andrej@1811:     def testMinLimit(self):
andrej@1811:         """Test working lower bound"""
andrej@1811:         self.AddControls()
andrej@1811:         self.int_ctrl.SetValue(self.min_val - 100)
andrej@1811:         self.ProcessEvents()
andrej@1811: 
andrej@1811:         self.txt_ctrl.SetFocus()
andrej@1811:         self.ProcessEvents()
andrej@1811: 
andrej@1811:         self.assertEqual(self.int_ctrl.GetValue(), self.min_val)
andrej@1811: 
andrej@1811:     def testCorrectValue(self):
andrej@1811:         """Test case if no limiting is necessary"""
andrej@1811:         self.AddControls()
andrej@1811:         val = (self.max_val + self.min_val) / 2
andrej@1811:         self.int_ctrl.SetValue(val)
andrej@1811:         self.ProcessEvents()
andrej@1811: 
andrej@1811:         self.txt_ctrl.SetFocus()
andrej@1811:         self.ProcessEvents()
andrej@1811: 
andrej@1811:         self.assertEqual(self.int_ctrl.GetValue(), val)
andrej@1811: 
andrej@1811:     def testEventBinding(self):
andrej@1811:         """Test event sending after edit and bound checks are done"""
andrej@1811:         self.AddControls()
andrej@1811:         self.event_happend = False
andrej@1811: 
andrej@1811:         def EventHandler(event):
andrej@1811:             self.event_happend = True
andrej@1811:             event.Skip()
andrej@1811: 
andrej@1811:         self.int_ctrl.Bind(controls.CustomIntCtrl.EVT_CUSTOM_INT, EventHandler)
andrej@1811: 
andrej@1811:         val = (self.max_val + self.min_val) / 2
andrej@1811: 
andrej@1811:         self.int_ctrl.SetValue(val)
andrej@1811:         self.ProcessEvents()
andrej@1811:         self.txt_ctrl.SetFocus()
andrej@1811: 
andrej@1811:         self.ProcessEvents()
andrej@1811:         self.txt_ctrl.SetFocus()
andrej@1811:         self.ProcessEvents()
andrej@1811: 
andrej@1811:         self.assertEqual(self.int_ctrl.GetValue(), val)
andrej@1811:         self.assertTrue(self.event_happend)
andrej@1811: 
andrej@1811:     def testLongNumbers(self):
andrej@1811:         """Test support of long integer"""
andrej@1811:         self.AddControls()
andrej@1811:         val = 40000000000
andrej@1811:         self.int_ctrl.SetMax(val)
andrej@1811:         self.int_ctrl.SetValue(val)
andrej@1811:         self.ProcessEvents()
andrej@1811: 
andrej@1811:         self.txt_ctrl.SetFocus()
andrej@1811:         self.ProcessEvents()
andrej@1811: 
andrej@1811:         self.assertEqual(val, val)
andrej@1811: 
andrej@1811:     def ProcessEvents(self):
andrej@1847:         for dummy in range(0, 10):
andrej@1811:             wx.Yield()
andrej@1811:             time.sleep(0.01)
andrej@1811: 
andrej@1811:     def AddControls(self):
andrej@1811:         vs = wx.BoxSizer(wx.VERTICAL)
andrej@1811:         self.int_ctrl = controls.CustomIntCtrl(self.frame)
andrej@1811:         self.txt_ctrl = wx.TextCtrl(self.frame)
andrej@1811:         vs.Add(self.int_ctrl, 0, wx.ALIGN_CENTRE | wx.ALL, 5)
andrej@1811:         vs.Add(self.txt_ctrl, 0, wx.ALIGN_CENTRE | wx.ALL, 5)
andrej@1811:         self.frame.SetSizer(vs)
andrej@1811:         vs.Fit(self.frame)
andrej@1811:         self.frame.Show()
andrej@1811:         self.frame.Raise()
andrej@1811: 
andrej@1811:         self.min_val = 50
andrej@1811:         self.max_val = 100
andrej@1811:         self.int_ctrl.SetBounds(self.min_val, self.max_val)
andrej@1811:         self.ProcessEvents()
andrej@1811: 
andrej@1811: 
andrej@1811: if __name__ == '__main__':
andrej@1850:     conftest.init_environment()
andrej@1811:     unittest.main()