2008-08-15 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 run(self):
53                 self._state = PASY_TEST_IN_PROGRESS
54                 self.entry()
55
56         def report(self):
57                 if self._state == PASY_TEST_WIN:
58                         return "%s - PASSED" % (self._name,)
59                 elif self._state == PASY_TEST_FAIL:
60                         return "%s - FAILED - %s" % (self._name, self.failMsg)
61                 else:
62                         return "%s - INCOMPLETE" %s (self._name,)
63
64         @property
65         def state(self):
66                 return self._state
67
68 class PasyTestFunc(PasyTestStep):
69         
70         def __init__(self, name, func):
71                 PasyTestStep.__init__(self, name)
72                 self._func = func
73
74         def entry(self):
75                 try:
76                         self._func()
77                 except Exception, e:
78                         self.fail(e.message)
79                         traceback.print_exc()
80                 self.win()
81
82 class PasyTest(PasyTestStep):
83
84         __tests__ = []
85
86         def __init__(self, name, cont):
87                 PasyTestStep.__init__(self, name)
88
89                 self._cont = cont
90                 self._tests = []
91
92                 for name in self.__tests__:
93                         func = getattr(self, name)
94                         self._addfunc(func.func_name, func)
95
96         def _addfunc(self, name, func):
97                 functest = PasyTestFunc(func.func_name, func)
98                 self._tests.append(functest)
99
100         def entry(self):
101                 self._iter = self._test_iterator()
102                 gobject.idle_add(self.idle_handler)
103
104         def idle_handler(self):
105                 try:
106                         step = self._iter.next()
107                         def finished_handler():
108                                 if step.state == PASY_TEST_WIN or self._cont == True:
109                                         gobject.idle_add(self.idle_handler)
110                                 elif step.state == PASY_TEST_FAIL and self._cont == False:
111                                         self.fail("Sub test %s Failed" % step._name)
112                         step.events.finished += finished_handler
113                         step.run()
114                 except StopIteration:
115                         # No More tests, check for success or fail
116                         succeeded = True
117                         for test in self._tests:
118                                 succeeded = succeeded and (test.state == PASY_TEST_WIN)
119                         if succeeded:
120                                 self.win()
121                         else:
122                                 self.fail()
123                 return False
124
125         def _test_iterator(self):
126                 for test in self._tests:
127                         yield test
128
129         def report(self):
130                 if self._state == PASY_TEST_WIN:
131                         header =  "%s - PASSED" % (self._name,)
132                 elif self._state == PASY_TEST_FAIL:
133                         header =  "%s - FAILED - %s" % (self._name, self.failMsg)
134                 else:
135                         header =  "%s - INCOMPLETE" %s (self._name,)
136
137                 step_messages = []
138                 for test in self._tests:
139                         step_messages.append(test.report())
140
141                 step_report =  "\n\t".join(step_messages)
142                 return header + "\n\t" + step_report