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-2013 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  from shutil import rmtree 
 29  from tempfile import mktemp, mkdtemp 
 30  from unittest import TestCase 
 31  import wx 
 32   
 33  # Dependency checks. 
 34  import dep_check 
 35   
 36  # relax module imports. 
 37  from compat import queue 
 38  from data import Relax_data_store; ds = Relax_data_store() 
 39  from data.gui import Gui 
 40  from generic_fns.reset import reset 
 41  from prompt.interpreter import exec_script 
 42  from relax_errors import RelaxError 
 43  from relax_io import delete 
 44  from status import Status; status = Status() 
 45  from user_functions.data import Uf_info; uf_info = Uf_info() 
 46   
 47  # relax GUI module imports. 
 48  from gui.interpreter import Interpreter; interpreter = Interpreter() 
 49  from gui.wizard import Wiz_window 
 50  from gui.uf_objects import Uf_storage; uf_store = Uf_storage() 
 51   
 52   
53 -class GuiTestCase(TestCase):
54 """The GUI test base class.""" 55
56 - def __init__(self, methodName=None):
57 """Set up the test case class for the system tests.""" 58 59 # A string used for classifying skipped tests. 60 if not hasattr(self, '_skip_type'): 61 self._skip_type = 'gui' 62 63 # Execute the TestCase __init__ method. 64 super(GuiTestCase, self).__init__(methodName)
65 66
67 - def _execute_uf(self, *args, **kargs):
68 """Execute the given user function. 69 70 @keyword uf_name: The name of the user function. 71 @type uf_name: str 72 """ 73 74 # Checks. 75 if 'uf_name' not in kargs: 76 raise RelaxError("The user function name argument 'uf_name' has not been supplied.") 77 78 # Process the user function name. 79 uf_name = kargs.pop('uf_name') 80 81 # Get the user function data object. 82 uf_data = uf_info.get_uf(uf_name) 83 84 # Convert the args into keyword args. 85 for i in range(len(args)): 86 # The keyword name for this arg. 87 name = uf_data.kargs[i]['name'] 88 89 # Check. 90 if name in kargs: 91 raise RelaxError("The argument '%s' clashes with the %s keyword argument of '%s'." % (arg[i], name, kargs[name])) 92 93 # Set the keyword arg. 94 kargs[name] = args[i] 95 96 # Add the keyword args not supplied, using the default value. 97 for i in range(len(uf_data.kargs)): 98 # Alias. 99 arg = uf_data.kargs[i] 100 101 # Already set. 102 if arg['name'] in kargs: 103 continue 104 105 # Set the default. 106 kargs[arg['name']] = arg['default'] 107 108 # Merge the file and directory args, as needed. 109 for i in range(len(uf_data.kargs)): 110 # Alias. 111 arg = uf_data.kargs[i] 112 113 # File selection and associated directory arg. 114 if arg['arg_type'] == 'dir' and arg['name'] in kargs: 115 # Find the associated file selection arg name. 116 for j in range(len(uf_data.kargs)): 117 if uf_data.kargs[j]['arg_type'] == 'file sel': 118 file_sel_name = uf_data.kargs[j]['name'] 119 120 # Prepend the directory to the file, if needed and supplied. 121 if file_sel_name in kargs and kargs[arg['name']]: 122 kargs[file_sel_name] = kargs[arg['name']] + sep + kargs[file_sel_name] 123 124 # Remove the directory argument. 125 kargs.pop(arg['name']) 126 127 # The user function object. 128 uf = uf_store[uf_name] 129 130 # Force synchronous operation of the user functions. 131 status.gui_uf_force_sync = True 132 133 # Call the GUI user function object with all keyword args, but do not execute the wizard. 134 uf(wx_wizard_run=False, **kargs) 135 136 # Execute the user function, by mimicking a click on 'ok'. 137 uf.wizard._ok() 138 139 # Restore the synchronous or asynchronous operation of the user functions so the GUI can return to normal. 140 status.gui_uf_force_sync = False
141 142
143 - def check_exceptions(self):
144 """Check that no exception has occurred.""" 145 146 # Check. 147 try: 148 # Get the exception from the queue. 149 index, exc = status.exception_queue.get(block=False) 150 151 # Print out. 152 print("Exception found, failing the test with an AssertionError:\n") 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