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