2009-27-09 Mark Doffman <mark.doffman@codethink.co.uk>
[platform/core/uifw/at-spi2-atk.git] / tests / pyatspi / pasytest / Pasy.py
1 #Copyright (C) 2008 Codethink Ltd
2
3 #his program is free software; you can redistribute it and/or modify
4 #it under the terms of the GNU General Public License as published by
5 #the Free Software Foundation; either version 2 of the License, or
6 #(at your option) any later version.
7
8 #This program is distributed in the hope that it will be useful,
9 #but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #GNU General Public License for more details.
12 #You should have received a copy of the GNU General Public License
13 #along with this program; if not, write to the Free Software
14 #Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15
16 import gobject
17 from Events import Events
18
19 import traceback
20
21 PASY_TEST_NOT_STARTED = 0
22 PASY_TEST_IN_PROGRESS = 1
23 PASY_TEST_FAIL = 2
24 PASY_TEST_WIN = 3
25
26 class PasyEvents(Events):
27         __events__ = ('finished', )
28
29 class PasyTestStep(object):
30
31         def __init__(self, name):
32                 self.events = PasyEvents()
33                 self._state = PASY_TEST_NOT_STARTED
34
35                 self._name = name
36
37         def win(self):
38                 if self._state == PASY_TEST_IN_PROGRESS:
39                         self._state = PASY_TEST_WIN
40                         self.events.finished()
41         
42         def fail(self, msg):
43                 if self._state == PASY_TEST_IN_PROGRESS:
44                         self._state = PASY_TEST_FAIL
45                         self.failMsg = msg
46                         self.events.finished()
47
48         def assertEqual(self, a, b, msg):
49                 if a != b:
50                         self.fail(msg)
51
52         def assertNotEqual(self, a, b, msg):
53                 if a == b:
54                         self.fail(msg)
55
56         def run(self):
57                 self._state = PASY_TEST_IN_PROGRESS
58                 self.entry()
59
60         def report(self):
61                 if self._state == PASY_TEST_WIN:
62                         return "%s - PASSED" % (self._name,)
63                 elif self._state == PASY_TEST_FAIL:
64                         return "%s - FAILED - %s" % (self._name, self.failMsg)
65                 else:
66                         return "%s - INCOMPLETE" % (self._name,)
67
68         @property
69         def state(self):
70                 return self._state
71
72 class PasyTestFunc(PasyTestStep):
73         
74         def __init__(self, name, func):
75                 PasyTestStep.__init__(self, name)
76                 self._func = func
77
78         def entry(self):
79                 try:
80                         self._func(self)
81                 except Exception, e:
82                         self.fail(e.message)
83                         traceback.print_exc()
84                 self.win()
85
86 class PasyTest(PasyTestStep):
87
88         __tests__ = []
89
90         def __init__(self, name, cont):
91                 PasyTestStep.__init__(self, name)
92
93                 self._cont = cont
94                 self._tests = []
95
96                 for name in self.__tests__:
97                         func = getattr(self, name)
98                         self._addfunc(func.func_name, func)
99
100         def _addfunc(self, name, func):
101                 functest = PasyTestFunc(func.func_name, func)
102                 self._tests.append(functest)
103
104         def entry(self):
105                 self._iter = self._test_iterator()
106                 gobject.idle_add(self.idle_handler)
107
108         def idle_handler(self):
109                 try:
110                         step = self._iter.next()
111                         def finished_handler():
112                                 if step.state == PASY_TEST_WIN or self._cont == True:
113                                         gobject.idle_add(self.idle_handler)
114                                 elif step.state == PASY_TEST_FAIL and self._cont == False:
115                                         self.fail("Sub test %s Failed" % step._name)
116                         step.events.finished += finished_handler
117                         step.run()
118                 except StopIteration:
119                         # No More tests, check for success or fail
120                         succeeded = True
121                         for test in self._tests:
122                                 succeeded = succeeded and (test.state == PASY_TEST_WIN)
123                         if succeeded:
124                                 self.win()
125                         else:
126                                 self.fail()
127                 return False
128
129         def _test_iterator(self):
130                 for test in self._tests:
131                         yield test
132
133         def report(self):
134                 if self._state == PASY_TEST_WIN:
135                         header =  "%s - PASSED" % (self._name,)
136                 elif self._state == PASY_TEST_FAIL:
137                         header =  "%s - FAILED" % (self._name,)
138                 else:
139                         header =  "%s - INCOMPLETE" %s (self._name,)
140
141                 step_messages = []
142                 for test in self._tests:
143                         step_messages.append(test.report())
144
145                 step_report =  "\n\t".join(step_messages)
146                 return header + "\n\t" + step_report