2008-07-28 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 from Events import Events
17
18 PASY_TEST_NOT_STARTED = 0
19 PASY_TEST_IN_PROGRESS = 1
20 PASY_TEST_FAIL = 2
21 PASY_TEST_WIN = 3
22
23 class PasyEvents(Events):
24         __events__ = ('finished', )
25
26 class PasyTestStep(object):
27
28         def __init__(self, name):
29                 self.events = PasyEvents()
30                 self._state = PASY_TEST_NOT_STARTED
31
32                 self._name = name
33                 self._test = test
34
35         def win(self):
36                 if self._state == PASY_TEST_IN_PROGRESS:
37                         self._state = PASY_TEST_WIN
38                         self.events.finished()
39         
40         def fail(self, msg):
41                 if self._state == PASY_TEST_IN_PROGRESS:
42                         self._state = PASY_TEST_FAIL
43                         self.failMsg = msg
44                         self.events.finished()
45
46         def assertEqual(self, a, b, msg):
47                 if a != b:
48                         self.fail(msg)
49
50         def run(self):
51                 self._state = PASY_TEST_IN_PROGRESS
52                 self.entry(self)
53
54         @property
55         def state(self):
56                 return self._state
57
58 class PasyTestFunc(PasyTestStep):
59         
60         def __init__(self, name, func):
61                 PasyTestStep.__init__(self, name)
62                 self._func = func
63
64         def entry(self):
65                 self._func(self)
66
67 class PasyTest(PasyTestStep):
68
69         __tests__ = []
70
71         def __init__(self, name, cont):
72                 PasyTest.__init__(self, name)
73
74                 self._cont = cont
75                 self._tests = []
76
77                 for name in __tests__:
78                         func = self.__getattr__(name)
79                         self._addfunc(func.func_name, func)
80
81         def _addfunc(self, name, func):
82                 functest = PasyTestFunc(self, func.func_name, func)
83                 self.add(functest)
84                 self._tests.append(functest)
85
86         def entry(self):
87                 self._iter = self._test_iterator
88                 gobject.idle_add(self.idle_handler)
89
90         def idle_handler(self):
91                 try:
92                         step = self._iter.next()
93                         def finished_handler():
94                                 if step.state == PASY_TEST_WIN or self._cont == True:
95                                         gobject.idle_add(self.idle_handler)
96                                 elif step.state == PASY_TEST_FAIL and self._cont == False:
97                                         self.fail()
98                         step.events.failed += finished_handler
99                         step.run()
100                 except StopIteration:
101                         # No More tests, check for success or fail
102                         succeeded = True
103                         for test in self._tests:
104                                 succeeded = succeeded and (test.state == PASY_TEST_WIN)
105                         if succeeded:
106                                 self.win()
107                         else:
108                                 self.fail()
109                 return False
110
111         def _test_iterator(self):
112                 for test in self._tests:
113                         yield test