tests/tools/test_CustomIntCtrl.py
branchwxPython4
changeset 3438 24fbd4d1fe80
parent 3437 ce366d67a5b7
child 3442 29dbdb09da2e
equal deleted inserted replaced
3437:ce366d67a5b7 3438:24fbd4d1fe80
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 # This file is part of Beremiz, a Integrated Development Environment for
       
     5 # programming IEC 61131-3 automates supporting plcopen standard and CanFestival.
       
     6 #
       
     7 # Copyright (C) 2017: Andrey Skvortsov
       
     8 #
       
     9 # See COPYING file for copyrights details.
       
    10 #
       
    11 # This program is free software; you can redistribute it and/or
       
    12 # modify it under the terms of the GNU General Public License
       
    13 # as published by the Free Software Foundation; either version 2
       
    14 # of the License, or (at your option) any later version.
       
    15 #
       
    16 # This program is distributed in the hope that it will be useful,
       
    17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    19 # GNU General Public License for more details.
       
    20 #
       
    21 # You should have received a copy of the GNU General Public License
       
    22 # along with this program; if not, write to the Free Software
       
    23 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
       
    24 
       
    25 
       
    26 from __future__ import absolute_import
       
    27 from __future__ import division
       
    28 import unittest
       
    29 import time
       
    30 
       
    31 import wx
       
    32 import conftest
       
    33 import controls.CustomIntCtrl
       
    34 
       
    35 
       
    36 class TestCustomIntCtrl(unittest.TestCase):
       
    37     def setUp(self):
       
    38         self.app = wx.App()
       
    39         self.frame = wx.Frame(None)
       
    40 
       
    41     def tearDown(self):
       
    42         self.frame.Destroy()
       
    43         wx.CallAfter(self.app.Exit)
       
    44         self.app.MainLoop()
       
    45 
       
    46     def testMaxLimit(self):
       
    47         """Test working upper bound"""
       
    48         self.AddControls()
       
    49         self.int_ctrl.SetValue(self.max_val + 100)
       
    50         self.ProcessEvents()
       
    51 
       
    52         self.txt_ctrl.SetFocus()
       
    53         self.ProcessEvents()
       
    54         self.assertEqual(self.int_ctrl.GetValue(), self.max_val)
       
    55 
       
    56     def testMinLimit(self):
       
    57         """Test working lower bound"""
       
    58         self.AddControls()
       
    59         self.int_ctrl.SetValue(self.min_val - 100)
       
    60         self.ProcessEvents()
       
    61 
       
    62         self.txt_ctrl.SetFocus()
       
    63         self.ProcessEvents()
       
    64 
       
    65         self.assertEqual(self.int_ctrl.GetValue(), self.min_val)
       
    66 
       
    67     def testCorrectValue(self):
       
    68         """Test case if no limiting is necessary"""
       
    69         self.AddControls()
       
    70         val = (self.max_val + self.min_val) // 2
       
    71         self.int_ctrl.SetValue(val)
       
    72         self.ProcessEvents()
       
    73 
       
    74         self.txt_ctrl.SetFocus()
       
    75         self.ProcessEvents()
       
    76 
       
    77         self.assertEqual(self.int_ctrl.GetValue(), val)
       
    78 
       
    79     def testEventBinding(self):
       
    80         """Test event sending after edit and bound checks are done"""
       
    81         self.AddControls()
       
    82         self.event_happend = False
       
    83 
       
    84         def EventHandler(event):
       
    85             self.event_happend = True
       
    86             event.Skip()
       
    87 
       
    88         self.int_ctrl.Bind(controls.CustomIntCtrl.EVT_CUSTOM_INT, EventHandler)
       
    89 
       
    90         val = (self.max_val + self.min_val) // 2
       
    91 
       
    92         self.int_ctrl.SetValue(val)
       
    93         self.ProcessEvents()
       
    94         self.txt_ctrl.SetFocus()
       
    95 
       
    96         self.ProcessEvents()
       
    97         self.txt_ctrl.SetFocus()
       
    98         self.ProcessEvents()
       
    99 
       
   100         self.assertEqual(self.int_ctrl.GetValue(), val)
       
   101         self.assertTrue(self.event_happend)
       
   102 
       
   103     def testLongNumbers(self):
       
   104         """Test support of long integer"""
       
   105         self.AddControls()
       
   106         val = 40000000000
       
   107         self.int_ctrl.SetMax(val)
       
   108         self.int_ctrl.SetValue(val)
       
   109         self.ProcessEvents()
       
   110 
       
   111         self.txt_ctrl.SetFocus()
       
   112         self.ProcessEvents()
       
   113 
       
   114         self.assertEqual(val, val)
       
   115 
       
   116     def ProcessEvents(self):
       
   117         for dummy in range(0, 10):
       
   118             wx.Yield()
       
   119             time.sleep(0.01)
       
   120 
       
   121     def AddControls(self):
       
   122         vs = wx.BoxSizer(wx.VERTICAL)
       
   123         self.int_ctrl = controls.CustomIntCtrl(self.frame)
       
   124         self.txt_ctrl = wx.TextCtrl(self.frame)
       
   125         vs.Add(self.int_ctrl, 0, wx.ALIGN_CENTRE | wx.ALL, 5)
       
   126         vs.Add(self.txt_ctrl, 0, wx.ALIGN_CENTRE | wx.ALL, 5)
       
   127         self.frame.SetSizer(vs)
       
   128         vs.Fit(self.frame)
       
   129         self.frame.Show()
       
   130         self.frame.Raise()
       
   131 
       
   132         self.min_val = 50
       
   133         self.max_val = 100
       
   134         self.int_ctrl.SetBounds(self.min_val, self.max_val)
       
   135         self.ProcessEvents()
       
   136 
       
   137 
       
   138 if __name__ == '__main__':
       
   139     conftest.init_environment()
       
   140     unittest.main()