Package test_suite :: Package gui_tests :: Module base_classes
[hide private]
[frames] | no frames]

Source Code for Module test_suite.gui_tests.base_classes

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2006-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax (http://www.nmr-relax.com).          # 
  6  #                                                                             # 
  7  # This program is free software: you can redistribute it and/or modify        # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation, either version 3 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # This program is distributed in the hope that it will be useful,             # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with this program.  If not, see <http://www.gnu.org/licenses/>.       # 
 19  #                                                                             # 
 20  ############################################################################### 
 21   
 22  # Module docstring. 
 23  """Base classes for the GUI tests.""" 
 24   
 25  # Python module imports. 
 26  from math import pi    # This is needed for relax scripts as pi is located in the relax prompt namespace. 
 27  from os import sep 
 28  try: 
 29      import queue 
 30  except ImportError: 
 31      import Queue as queue 
 32  from shutil import rmtree 
 33  from tempfile import mktemp, mkdtemp 
 34  from unittest import TestCase 
 35  import wx 
 36   
 37  # Dependency checks. 
 38  import dep_check 
 39   
 40  # relax module imports. 
 41  from data import Relax_data_store; ds = Relax_data_store() 
 42  from data.gui import Gui 
 43  from generic_fns.reset import reset 
 44  from prompt.interpreter import exec_script 
 45  from relax_errors import RelaxError 
 46  from relax_io import delete 
 47  from status import Status; status = Status() 
 48  from user_functions.data import Uf_info; uf_info = Uf_info() 
 49   
 50  # relax GUI module imports. 
 51  from gui.interpreter import Interpreter; interpreter = Interpreter() 
 52  from gui.wizard import Wiz_window 
 53  from gui.uf_objects import Uf_storage; uf_store = Uf_storage() 
 54   
 55   
56 -class GuiTestCase(TestCase):
57 """The GUI test base class.""" 58
59 - def __init__(self, methodName=None):
60 """Set up the test case class for the system tests.""" 61 62 # A string used for classifying skipped tests. 63 if not hasattr(self, '_skip_type'): 64 self._skip_type = 'gui' 65 66 # Execute the TestCase __init__ method. 67 super(GuiTestCase, self).__init__(methodName)
68 69
70 - def _execute_uf(self, *args, **kargs):
71 """Execute the given user function. 72 73 @keyword uf_name: The name of the user function. 74 @type uf_name: str 75 """ 76 77 # Checks. 78 if 'uf_name' not in kargs: 79 raise RelaxError("The user function name argument 'uf_name' has not been supplied.") 80 81 # Process the user function name. 82 uf_name = kargs.pop('uf_name') 83 84 # Get the user function data object. 85 uf_data = uf_info.get_uf(uf_name) 86 87 # Convert the args into keyword args. 88 for i in range(len(args)): 89 # The keyword name for this arg. 90 name = uf_data.kargs[i]['name'] 91 92 # Check. 93 if name in kargs: 94 raise RelaxError("The argument '%s' clashes with the %s keyword argument of '%s'." % (arg[i], name, kargs[name])) 95 96 # Set the keyword arg. 97 kargs[name] = args[i] 98 99 # Add the keyword args not supplied, using the default value. 100 for i in range(len(uf_data.kargs)): 101 # Alias. 102 arg = uf_data.kargs[i] 103 104 # Already set. 105 if arg['name'] in kargs: 106 continue 107 108 # Set the default. 109 kargs[arg['name']] = arg['default'] 110 111 # Merge the file and directory args, as needed. 112 for i in range(len(uf_data.kargs)): 113 # Alias. 114 arg = uf_data.kargs[i] 115 116 # File selection and associated directory arg. 117 if arg['arg_type'] == 'dir' and arg['name'] in kargs: 118 # Find the associated file selection arg name. 119 for j in range(len(uf_data.kargs)): 120 if uf_data.kargs[j]['arg_type'] == 'file sel': 121 file_sel_name = uf_data.kargs[j]['name'] 122 123 # Prepend the directory to the file, if needed and supplied. 124 if file_sel_name in kargs and kargs[arg['name']]: 125 kargs[file_sel_name] = kargs[arg['name']] + sep + kargs[file_sel_name] 126 127 # Remove the directory argument. 128 kargs.pop(arg['name']) 129 130 # The user function object. 131 uf = uf_store[uf_name] 132 133 # Force synchronous operation of the user functions. 134 status.gui_uf_force_sync = True 135 136 # Call the GUI user function object with all keyword args, but do not execute the wizard. 137 uf(wx_wizard_run=False, **kargs) 138 139 # Execute the user function, by mimicking a click on 'ok'. 140 uf.wizard._ok() 141 142 # Restore the synchronous or asynchronous operation of the user functions so the GUI can return to normal. 143 status.gui_uf_force_sync = False
144 145
146 - def check_exceptions(self):
147 """Check that no exception has occurred.""" 148 149 # Check. 150 try: 151 # Get the exception from the queue. 152 index, exc = status.exception_queue.get(block=False) 153 154 # Fail. 155 self.fail() 156 157 # No exception. 158 except queue.Empty: 159 pass
160 161
162 - def script_exec(self, script):
163 """Execute a GUI script within the GUI test framework. 164 165 @param script: The full path of the script to execute. 166 @type script: str 167 """ 168 169 # The namespace to pass into the script execution environment. 170 space = locals() 171 172 # Place some objects in the local namespace. 173 space.update({'pi': pi}) 174 175 # Execute the script. 176 exec_script(script, space)
177 178
179 - def setUp(self):
180 """Set up for all the functional tests.""" 181 182 # Create a temporary file for the tests that need it. 183 ds.tmpfile = mktemp() 184 185 # Create a temporary directory for the results. 186 ds.tmpdir = mkdtemp() 187 188 # Get the wx app. 189 self.app = wx.GetApp()
190 191
192 - def tearDown(self):
193 """Default tearDown operation - delete temp directories and files and reset relax.""" 194 195 # Flush all wx events prior to the clean up operations of this method. This prevents these events from occurring after the GUI elements have been deleted. 196 wx.Yield() 197 198 # Remove the temporary directories. 199 if hasattr(ds, 'tmpdir'): 200 # Delete the directory. 201 rmtree(ds.tmpdir) 202 203 # Remove the variable. 204 del ds.tmpdir 205 206 # Remove the temporary directories. 207 if hasattr(self, 'tmpdir'): 208 # Delete the directory. 209 rmtree(self.tmpdir) 210 211 # Remove the variable. 212 del self.tmpdir 213 214 # Remove temporary files. 215 if hasattr(ds, 'tmpfile'): 216 # Delete the file. 217 delete(ds.tmpfile, fail=False) 218 219 # Remove the variable. 220 del ds.tmpfile 221 222 # Remove temporary files. 223 if hasattr(self, 'tmpfile'): 224 # Delete the file. 225 delete(self.tmpfile, fail=False) 226 227 # Remove the variable. 228 del self.tmpfile 229 230 # Reset relax. 231 reset()
232