diff --git a/mozilla/extensions/python/dom/test/pyxultest/pytester.py b/mozilla/extensions/python/dom/test/pyxultest/pytester.py index f3a2c1ad513..bc7d6367ef1 100644 --- a/mozilla/extensions/python/dom/test/pyxultest/pytester.py +++ b/mozilla/extensions/python/dom/test/pyxultest/pytester.py @@ -1,14 +1,79 @@ -# Runs non-ui related test code. +# Runs test code in a UI environment. Generally anything which can be tested +# without visual confirmation is suitable for running here. +# Note too that the simple fact we *can* display a UI and run tests is +# exercising lots of things. +import sys from xpcom import components import xpcom -def do_onload(): - # Why can't I get args?? - # Intention is to look at window.arguments and determine what 'test' - # we are being asked to run. - print "OnLoad called" - raise "foo" +close_on_success = True -def do_onerror(*arguments): - print "OnError called" - print "Args are", arguments +# Utility functions +# Write something to the textbox - eg, error or trace messages. +def write( msg, *args): + tb = this.document.getElementById("output-box") + val = tb.getAttribute("value") + val += (msg % args) + "\n" + tb.setAttribute("value", val) + +def success(): + # This gets called if the test works. If it is not called, it must not + # have worked :) + write("The test worked!") + if close_on_success: + print "Closing the window - nothing interesting to see here!" + window.close() + +def cause_error(): + write("Raising an error") + raise "Testing Testing" + +def handle_error_with_cancel(errMsg, source, lineno): + success() + +def test_error_explicit(): + "Test an explicit assignment of a function object to onerror" + # Assign a function to an onerror attribute + write("Assigning a function to window.onerror") + window.onerror = handle_error_with_cancel + cause_error() + +def test_error_explicit_string(): + "Test an explicit assignment of a string to onerror" + # Assign a string to an onerror attribute + write("Assigning a string to window.onerror") + window.onerror = "handle_error_with_cancel(event, source, lineno)" + cause_error() + +class EventListener: + _com_interfaces_ = components.interfaces.nsIDOMEventListener + def handleEvent(self, event): + write("nsIDOMEventListener caught the event") + success() + +def test_error_eventhandler(): + """Test we can explicitly hook the error with our own nsIDOMEventListener""" + write("Creating an nsIDOMEventListener to listen for the event") + window.addEventListener("onerror", EventListener(), False) + write("For some reason 'onerror' is not working. Onload appears to?") + write("I need to check out if this works from JS first") + cause_error() + +# The general event handlers and test runner code. +def do_onload(event): + global close_on_success + klasses = [] + if len(this.arguments) and this.arguments[0]: + try: + func = globals()[this.arguments[0]] + except KeyError: + print "*** No such test", this.arguments[0] + func = None + else: + func = test_error_explicit_string + close_on_success = False + if len(this.arguments)>1 and this.arguments[1]=="-k": + close_on_success = False + if func: + write(func.__doc__ or func.__name__) + func() diff --git a/mozilla/extensions/python/dom/test/pyxultest/pytester.xul b/mozilla/extensions/python/dom/test/pyxultest/pytester.xul index 33355a52c3b..31b65f48d87 100644 --- a/mozilla/extensions/python/dom/test/pyxultest/pytester.xul +++ b/mozilla/extensions/python/dom/test/pyxultest/pytester.xul @@ -2,7 +2,8 @@ + + diff --git a/mozilla/extensions/python/dom/test/pyxultest/pyxultest.py b/mozilla/extensions/python/dom/test/pyxultest/pyxultest.py index 4bf502dce65..1c794b23042 100644 --- a/mozilla/extensions/python/dom/test/pyxultest/pyxultest.py +++ b/mozilla/extensions/python/dom/test/pyxultest/pyxultest.py @@ -3,10 +3,18 @@ import sys import xpcom from xpcom import components +import nsdom -# This EventListener class is to work around the fact that -# addEventListener can not automagically wrap a string or function like -# JS can. +# Utility functions +# Write something to the textbox - eg, error or trace messages. +def write( msg, *args): + tb = this.document.getElementById("output_box") + val = tb.getAttribute("value") + val += (msg % args) + "\n" + tb.setAttribute("value", val) + +# An event listener class to test explicit hooking of events via objects. +# Note Python can now just call addEventListener a-la JS class EventListener: _com_interfaces_ = components.interfaces.nsIDOMEventListener def __init__(self, handler, globs = None): @@ -22,22 +30,25 @@ class EventListener: # An event function to handle onload - but hooked up manually rather than via # having the magic name 'onload' def do_load(): - input = this.document.getElementById("input-box") - val = "This is the Python on XUL demo using\nPython " + sys.version - input.setAttribute("value", val) - - # Sadly no inline event handlers yet - hook up a click event. - button = this.document.getElementById("button1") - button.addEventListener('click', EventListener('print "hello from the click event"'), False) + input = this.document.getElementById("output_box") + # Clear the text in the XUL + input.setAttribute("value", "") + write("This is the Python on XUL demo using\nPython " + sys.version) -# Add the event listener. -this.window.addEventListener('load', EventListener(do_load), False) + # Sadly no inline event handlers yet - hook up a click event. + button = this.document.getElementById("but_dialog") + button.addEventListener('click', 'write("hello from the click event for the dialog button")', False) + +# Add an event listener as a function +this.window.addEventListener('load', do_load, False) # Add another one just to test passing a string instead of a function. -this.window.addEventListener('load', EventListener('print "hello from the event " + str(this.document)'), False) +this.window.addEventListener('load', "print 'hello from string event handler'", False) +# And yet another with an explicit EventListener instance. (on error tests can't do this?) +this.window.addEventListener('load', EventListener('print "hello from an object event handler"'), False) # Some other little functions called by the chrome -def on_button_click(): - print "Button clicked from", this.window.location.href +def on_but_dialog_click(): + write("Button clicked from %s" % this.window.location.href) # window.open doesn't work as JS has special arg handling :( # for now, use our hacky (but quite wonderful) JSExec function. import nsdom @@ -45,8 +56,19 @@ def on_button_click(): #new = this.window.open("chrome://pyxultest/content/dialog.xul", "my-dialog", "chrome") #print "The new window is", new -def test_error_event(): - import nsdom - nsdom.JSExec(this, 'this.window.openDialog("chrome://pyxultest/content/pytester.xul", "my-dialog", "modal", "test_error_event")') - #new = this.window.open("chrome://pyxultest/content/dialog.xul", "my-dialog", "chrome") - #print "The new window is", new +def run_tests(): + tests = """test_error_explicit test_error_explicit_string + test_error_eventhandler""".split() + keep_open = this.document.getElementById("keep_tests_open").getAttribute("checked") + for test in tests: + write("Running test %s" % test) + suffix = ', "%s"' % test + if keep_open: + suffix += ', "-k"' + cmd = 'this.window.openDialog("chrome://pyxultest/content/pytester.xul", "my-dialog", "modal"%s)' % suffix + print "cmd is", cmd + nsdom.JSExec(this, cmd) + if keep_open: + write("Ran all the tests - the windows told you if the tests worked") + else: + write("Ran all the tests - if you saw no window, it worked!") diff --git a/mozilla/extensions/python/dom/test/pyxultest/pyxultest.xul b/mozilla/extensions/python/dom/test/pyxultest/pyxultest.xul index 19b5071c81b..00d11226feb 100644 --- a/mozilla/extensions/python/dom/test/pyxultest/pyxultest.xul +++ b/mozilla/extensions/python/dom/test/pyxultest/pyxultest.xul @@ -8,9 +8,10 @@ xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="main-window" orient="vertical" screenX="100" screenY="100" - height="300" width="500" + height="450" width="600" script-type="application/x-python" onload="print 'hello from the window load event'" + title="Python in XUL demos and tests" > - -