2008-08-18 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/upstream/at-spi2-core.git] / tests / pyatspi / testrunner.py
1 #!/usr/bin/python
2
3 import gobject
4 import dbus
5 import sys
6 import time
7 from random import randint
8
9 from optparse import OptionParser
10 from pasytest import PasyTest
11
12 from dbus.mainloop.glib import DBusGMainLoop
13
14 DBusGMainLoop(set_as_default=True)
15
16 def run_test_app(module_name, dbus_name=None, wait_for_debug=False):
17         import os
18         from subprocess import Popen
19
20         test_data_directory = os.environ["TEST_DATA_DIRECTORY"]
21         test_modules_directory = os.environ["TEST_MODULES_DIRECTORY"]
22         test_atspi_library = os.environ["TEST_ATSPI_LIBRARY"]
23         test_application = os.environ["TEST_APPLICATION"]
24
25         test_module = os.path.join(test_modules_directory, module_name)
26
27         if (dbus_name):
28                 print " ".join([test_application,
29                         "--atspi-dbus-name", dbus_name,
30                         "--test-atspi-library", test_atspi_library,
31                         "--test-module", test_module,
32                         "--test-data-directory", test_data_directory,])
33                 pop = Popen([test_application,
34                         "--atspi-dbus-name", dbus_name,
35                         "--test-atspi-library", test_atspi_library,
36                         "--test-module", test_module,
37                         "--test-data-directory", test_data_directory,])
38         else:
39                 print " ".join([test_application,
40                         "--test-atspi-library", test_atspi_library,
41                         "--test-module", test_module,
42                         "--test-data-directory", test_data_directory,])
43                 pop = Popen([test_application,
44                         "--test-atspi-library", test_atspi_library,
45                         "--test-module", test_module,
46                         "--test-data-directory", test_data_directory,])
47
48         wait_message = """
49         The test application %s has been started with PID %d.
50         
51         To continue the test press ENTER.\n\n
52         """
53         if (wait_for_debug):
54                 raw_input(wait_message % (module_name, pop.pid))
55
56 def main(argv):
57         parser = OptionParser()
58         parser.add_option("-l", "--library", dest="test_library")
59         parser.add_option("-m", "--module", dest="test_module")
60         parser.add_option("-n", "--name", dest="test_name")
61         parser.add_option("-w", "--wait", action="store_true", dest="wait", default=False)
62         (options, args) = parser.parse_args()
63
64         bus = dbus.SessionBus()
65         name = "test.atspi.R" + str(randint(1, 1000)) 
66
67         app = run_test_app(options.test_library, name, wait_for_debug=options.wait)
68         time.sleep(1)
69         print "Started test app on bus name %s" % (name,)
70
71         to = bus.get_object(name, "/org/codethink/atspi/test")
72         test = dbus.Interface(to, "org.codethink.atspi.test")
73
74         # Run the test script here
75         module = __import__(options.test_module)
76         test_class = getattr(module, options.test_name)
77         test_object = test_class(bus, name)
78         test_object.run()
79
80         loop = gobject.MainLoop()
81
82         def finished_handler():
83                 loop.quit()
84                 print "\n" + test_object.report() + "\n"
85                 test.finish()
86
87         test_object.events.finished += finished_handler
88
89         loop.run()
90
91 if __name__=="__main__":
92         sys.exit(main(sys.argv))