/usr/lib/python2.7/dist-packages/wxglade/tests/test_gui.py is in python-wxglade 0.6.8-2.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | """
@copyright: 2012 Carsten Grohmann
@license: MIT (see license.txt) - THIS PROGRAM COMES WITH NO WARRANTY
"""
# import test base class
from tests import WXGladeBaseTest
# import general python modules
import cStringIO
import sys
import wx
# import project modules
import config
import common
class TestGui(WXGladeBaseTest):
"""\
Test GUI functionality
Since Python created an own instance for each test, we use class varaibles
for persistent storing L{app} and L{frame}.
@cvar app: Reference to a wx.App object. he object is persistent after the creation in L{setUp()}.
@cvar frame: Reference to L{main.wxGladeFrame}. The object is persistent after the creation in L{setUp()}.
@ivar orig_stdout: Original fd for stdout.
"""
init_stage1 = True
init_use_gui = True
app = None
frame = None
orig_stdout = None
def mockMessageBox(self, message, caption, *args, **kwargs):
"""\
Mock object for wx.MessageBox
"""
self._messageBox = [message, caption]
def setUp(self):
# redirect stdout
self.orig_stdout = sys.stdout
sys.stdout = cStringIO.StringIO()
# initialse base class
WXGladeBaseTest.setUp(self)
import main
import wx
# inject mock object for wxMessageBox
self._messageBox = []
wx.MessageBox = self.mockMessageBox
# create an simply application
if not TestGui.app:
TestGui.app = wx.PySimpleApp()
wx.ArtProvider.PushProvider(main.wxGladeArtProvider())
if not TestGui.frame:
self.frame = main.wxGladeFrame()
# hide all windows
self.frame.Hide()
self.frame.hide_all()
TestGui.frame = self.frame
# show dialog "Code generation completed successfully"
config.preferences.show_completion = True
def tearDown(self):
# restore original stdout
if self.orig_stdout:
sys.stdout = self.orig_stdout
# initialse base class
WXGladeBaseTest.tearDown(self)
def _FindWindowByName(self, name):
"""\
Search and return a widget with the given name in the top window
widget tree.
"""
app = wx.GetApp()
top = app.GetTopWindow()
return top.FindWindowByName(name)
def _generate_code(self):
"""\
Search button with label "Generate code" and press it
"""
# search wx.Button "Generate code"
btn_codegen = self._FindWindowByName("BtnGenerateCode")
self.failUnless(
btn_codegen,
'Button with label "Generate code" not found'
)
# press button to generate code
self._press_button(btn_codegen)
def _press_button(self, button):
"""\
Simulate pressing the button by sending a button clicked event
"""
event = wx.CommandEvent(
wx.wxEVT_COMMAND_BUTTON_CLICKED,
button.GetId()
)
button.GetEventHandler().ProcessEvent(event)
def _set_lang(self, lang):
"""\
Set "Language" and simulate clicking radio button
@param lang: Language to set
@type lang: StringIO
"""
radiobox= common.app_tree.app.codewriters_prop.options
radiobox.SetStringSelection(lang)
event = wx.CommandEvent(
wx.wxEVT_COMMAND_RADIOBOX_SELECTED,
radiobox.GetId()
)
radiobox.GetEventHandler().ProcessEvent(event)
def testNotebookWithoutTabs(self):
"""\
Test loading Notebook without tabs
"""
self._messageBox = None
infile = cStringIO.StringIO(
self._load_file('Notebook_wo_tabs.wxg')
)
self.frame._open_app(
infilename=infile,
use_progress_dialog=False,
is_filelike=True,
add_to_history=False,
)
err_msg = u'Error loading file None: Notebook widget' \
' "notebook_1" does not have any tabs! ' \
'_((line: 18, column: 20))'
err_caption = u'Error'
self.failUnless(
[err_msg, err_caption] == self._messageBox,
'''Expected wxMessageBox(message=%s, caption=%s)''' % (
err_msg,
err_caption
)
)
def testNotebookWithTabs(self):
"""\
Test loading Notebook with tabs
"""
self._messageBox = None
infile = cStringIO.StringIO(
self._load_file('Notebook_w_tabs.wxg')
)
self.frame._open_app(
infilename=infile,
use_progress_dialog=False,
is_filelike=True,
add_to_history=False,
)
self.failIf(
self._messageBox,
'Loading test wxg file caused an error message: %s' % \
self._messageBox
)
def testCodeGeneration(self):
"""\
Test GUI code generation
"""
source = self._load_file('FontColour.wxg')
source = self._modify_attrs(
source,
path='',
)
infile = cStringIO.StringIO(source)
self.frame._open_app(
infilename=infile,
use_progress_dialog=False,
is_filelike=True,
add_to_history=False,
)
# generate code
self._generate_code()
# first test should fail because no output file is given
#print self._messageBox
err_msg = u'You must specify an output file\n' \
'before generating any code'
err_caption = u'Error'
self.failUnless(
[err_msg, err_caption] == self._messageBox,
'''Expected wxMessageBox(message=%s, caption=%s)''' % (
err_msg,
err_caption
)
)
self._messageBox = None
# now test full code generation
for filename, language in [
['FontColour.lisp', 'lisp'],
['FontColour.pl', 'perl'],
['FontColour.py', 'python'],
['FontColour.xrc', 'XRC'],
['FontColour', 'C++'],
]:
# check for langage first
self.failUnless(
language in common.code_writers,
"No codewriter loaded for %s" % language
)
# prepare and open wxg
source = self._prepare_wxg(language, source)
infile = cStringIO.StringIO(source)
self.frame._open_app(
infilename=infile,
use_progress_dialog=False,
is_filelike=True,
add_to_history=False,
)
# set "Output path", language and generate code
common.app_tree.app.output_path = filename
self._set_lang(language)
self._generate_code()
success_msg = u'Code generation completed successfully'
success_caption = u'Information'
self.failUnless(
[success_msg, success_caption] == self._messageBox,
'''Expected wxMessageBox(message=%s, caption=%s)''' % (
success_msg,
success_caption
)
)
self._messageBox = None
if language == 'C++':
name_h = '%s.h' % filename
name_cpp = '%s.cpp' % filename
result_cpp = self._load_file(name_cpp)
result_h = self._load_file(name_h)
generated_cpp = self.vFiles[name_cpp].getvalue()
generated_h = self.vFiles[name_h].getvalue()
self._compare(result_cpp, generated_cpp, 'C++ source')
self._compare(result_h, generated_h, 'C++ header')
else:
expected = self._load_file(filename)
generated = self.vFiles[filename].getvalue()
self._compare(expected, generated)
def test_StylelessDialog(self):
"""\
Test code generation for a style less dialog
"""
source = self._load_file('styleless-dialog.wxg')
# now test full code generation
for filename, language in [
['styleless-dialog.lisp', 'lisp'],
['styleless-dialog.pl', 'perl'],
['styleless-dialog.py', 'python'],
['styleless-dialog.xrc', 'XRC'],
['styleless-dialog', 'C++'],
]:
# check for langage first
self.failUnless(
language in common.code_writers,
"No codewriter loaded for %s" % language
)
# prepare and open wxg
source = self._prepare_wxg(language, source)
infile = cStringIO.StringIO(source)
self.frame._open_app(
infilename=infile,
use_progress_dialog=False,
is_filelike=True,
add_to_history=False,
)
# set "Output path", language and generate code
common.app_tree.app.output_path = filename
self._set_lang(language)
self._generate_code()
success_msg = u'Code generation completed successfully'
success_caption = u'Information'
self.failUnless(
[success_msg, success_caption] == self._messageBox,
'''Expected wxMessageBox(message=%s, caption=%s)''' % (
success_msg,
success_caption
)
)
self._messageBox = None
if language == 'C++':
name_h = '%s.h' % filename
name_cpp = '%s.cpp' % filename
result_cpp = self._load_file(name_cpp)
result_h = self._load_file(name_h)
generated_cpp = self.vFiles[name_cpp].getvalue()
generated_h = self.vFiles[name_h].getvalue()
self._compare(result_cpp, generated_cpp, 'C++ source')
self._compare(result_h, generated_h, 'C++ header')
else:
expected = self._load_file(filename)
generated = self.vFiles[filename].getvalue()
self._compare(expected, generated)
|