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.                                     # 
  6  #                                                                             # 
  7  # relax 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 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax 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 relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """Base classes for the GUI tests.""" 
 25   
 26  # Python module imports. 
 27  from math import pi    # This is needed for relax scripts as pi is located in the relax prompt namespace. 
 28  from os import sep 
 29  import Queue 
 30  from shutil import rmtree 
 31  from tempfile import mktemp, mkdtemp 
 32  from unittest import TestCase 
 33  import wx 
 34   
 35  # Dependency checks. 
 36  import dep_check 
 37   
 38  # relax module imports. 
 39  from data import Relax_data_store; ds = Relax_data_store() 
 40  from data.gui import Gui 
 41  from generic_fns.reset import reset 
 42  from prompt.interpreter import exec_script 
 43  from relax_errors import RelaxError 
 44  from relax_io import delete 
 45  from status import Status; status = Status() 
 46  from user_functions.data import Uf_info; uf_info = Uf_info() 
 47   
 48  # relax GUI module imports. 
 49  from gui.interpreter import Interpreter; interpreter = Interpreter() 
 50  from gui.wizard import Wiz_window 
 51  from gui.uf_objects import Uf_storage; uf_store = Uf_storage() 
 52   
 53   
54 -class GuiTestCase(TestCase):
55 """The GUI test base class.""" 56
57 - def __init__(self, methodName=None):
58 """Set up the test case class for the system tests.""" 59 60 # Execute the TestCase __init__ method. 61 super(GuiTestCase, self).__init__(methodName) 62 63 # A string used for classifying skipped tests. 64 self._skip_type = 'gui' 65 66 # Get the wx app, if the test suite is launched from the gui. 67 self.app = wx.GetApp() 68 69 # Flag for the GUI. 70 self._gui_launch = False 71 if self.app != None: 72 self._gui_launch = True
73 74
75 - def _execute_uf(self, *args, **kargs):
76 """Execute the given user function. 77 78 @keyword uf_name: The name of the user function. 79 @type uf_name: str 80 """ 81 82 # Checks. 83 if 'uf_name' not in kargs: 84 raise RelaxError("The user function name argument 'uf_name' has not been supplied.") 85 86 # Process the user function name. 87 uf_name = kargs.pop('uf_name') 88 89 # Get the user function data object. 90 uf_data = uf_info.get_uf(uf_name) 91 92 # Convert the args into keyword args. 93 for i in range(len(args)): 94 # The keyword name for this arg. 95 name = uf_data.kargs[i]['name'] 96 97 # Check. 98 if name in kargs: 99 raise RelaxError("The argument '%s' clashes with the %s keyword argument of '%s'." % (arg[i], name, kargs[name])) 100 101 # Set the keyword arg. 102 kargs[name] = args[i] 103 104 # Add the keyword args not supplied, using the default value. 105 for i in range(len(uf_data.kargs)): 106 # Alias. 107 arg = uf_data.kargs[i] 108 109 # Already set. 110 if arg['name'] in kargs: 111 continue 112 113 # Set the default. 114 kargs[arg['name']] = arg['default'] 115 116 # Merge the file and directory args, as needed. 117 for i in range(len(uf_data.kargs)): 118 # Alias. 119 arg = uf_data.kargs[i] 120 121 # File selection and associated directory arg. 122 if arg['arg_type'] == 'dir' and arg['name'] in kargs: 123 # Find the associated file selection arg name. 124 for j in range(len(uf_data.kargs)): 125 if uf_data.kargs[j]['arg_type'] == 'file sel': 126 file_sel_name = uf_data.kargs[j]['name'] 127 128 # Prepend the directory to the file, if needed and supplied. 129 if file_sel_name in kargs and kargs[arg['name']]: 130 kargs[file_sel_name] = kargs[arg['name']] + sep + kargs[file_sel_name] 131 132 # Remove the directory argument. 133 kargs.pop(arg['name']) 134 135 # The user function object. 136 uf = uf_store[uf_name] 137 138 # Force synchronous operation of the user functions. 139 status.gui_uf_force_sync = True 140 141 # Call the GUI user function object with all keyword args, but do not execute the wizard. 142 uf(**kargs) 143 144 # Execute the user function, by mimicking a click on 'ok'. 145 uf.wizard._ok() 146 147 # Restore the synchronous or asynchronous operation of the user functions so the GUI can return to normal. 148 status.gui_uf_force_sync = False
149 150
151 - def check_exceptions(self):
152 """Check that no exception has occurred.""" 153 154 # Check. 155 try: 156 # Get the exception from the queue. 157 index, exc = status.exception_queue.get(block=False) 158 159 # Fail. 160 self.fail() 161 162 # No exception. 163 except Queue.Empty: 164 pass
165 166
167 - def script_exec(self, script):
168 """Execute a GUI script within the GUI test framework. 169 170 @param script: The full path of the script to execute. 171 @type script: str 172 """ 173 174 # The namespace to pass into the script execution environment. 175 space = locals() 176 177 # Place some objects in the local namespace. 178 space.update({'pi': pi}) 179 180 # Execute the script. 181 exec_script(script, space)
182 183
184 - def setUp(self):
185 """Set up for all the functional tests.""" 186 187 # Create a temporary file for the tests that need it. 188 ds.tmpfile = mktemp() 189 190 # Create a temporary directory for the results. 191 ds.tmpdir = mkdtemp() 192 193 # Start the GUI if not launched from the GUI. 194 if not self._gui_launch: 195 self.app = wx.App(redirect=False) 196 197 # relax GUI imports (here to prevent a circular import from the test suite in the GUI). 198 if dep_check.wx_module: 199 from gui.relax_gui import Main 200 201 # Build the GUI. 202 self.app.gui = Main(parent=None, id=-1, title="")
203 204
205 - def tearDown(self):
206 """Default tearDown operation - delete temp directories and files and reset relax.""" 207 208 # 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. 209 wx.Yield() 210 211 # Remove the temporary directories. 212 if hasattr(ds, 'tmpdir'): 213 # Delete the directory. 214 rmtree(ds.tmpdir) 215 216 # Remove the variable. 217 del ds.tmpdir 218 219 # Remove the temporary directories. 220 if hasattr(self, 'tmpdir'): 221 # Delete the directory. 222 rmtree(self.tmpdir) 223 224 # Remove the variable. 225 del self.tmpdir 226 227 # Remove temporary files. 228 if hasattr(ds, 'tmpfile'): 229 # Delete the file. 230 delete(ds.tmpfile, fail=False) 231 232 # Remove the variable. 233 del ds.tmpfile 234 235 # Remove temporary files. 236 if hasattr(self, 'tmpfile'): 237 # Delete the file. 238 delete(self.tmpfile, fail=False) 239 240 # Remove the variable. 241 del self.tmpfile 242 243 # Delete all the GUI analysis tabs. 244 self.app.gui.analysis.delete_all() 245 246 # Reset relax. 247 reset() 248 249 # Reset the observers. 250 status._setup_observers() 251 252 # Destroy some GUI windows, if open. 253 windows = ['pipe_editor', 'relax_prompt', 'results_viewer', 'spin_viewer'] 254 for window in windows: 255 if hasattr(self.app.gui, window): 256 # Get the object. 257 win_obj = getattr(self.app.gui, window) 258 259 # Destroy the wxWidget part. 260 win_obj.Destroy() 261 262 # Destroy the Python object part. 263 delattr(self.app.gui, window) 264 265 # Flush all wx events to make sure the GUI is ready for the next test. 266 wx.Yield()
267