Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/tests/enginetests.py
blob: a035c2f5420177dec530d9309dfb952312f55929 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# Copyright (C) 2009, Tutorius.org
# Copyright (C) 2009, Erick Lavoie <erick.lavoie@gmail.com> 
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
"""
Engine Tests



Usage of actions and event filters is tested, but not the concrete actions
and event filters. Those are in their separate test module

"""

import unittest

from sugar.tutorius.tutorial import Tutorial
from sugar.tutorius.engine import TutorialRunner
from sugar.tutorius.filters import EventFilter

from actiontests import CountAction

class MockProbeMgr(object):
    def __init__(self):
        self.action = None
        self.event = None
        self.cB = None

    def doCB(self):
        self.cB(self.event)

    currentActivity = property(fget=lambda s:s, fset=lambda s, v: v)

    def install(self, action, action_installed_cb, error_cb):
        self.action = action
        action_installed_cb(action, 'Action1')

    def update(self, action_address, newaction):
        self.action = newaction

    def uninstall(self, action_address):
        self.action = None

    def subscribe(self, event, notif_cb, event_sub_cb, error_cb):
        self.event = event
        self.cB = notif_cb
        self.event.install_handlers(notif_cb)
        # Trigger the installation callback
        self.event_sub_cB = event_sub_cb
        self._subscribe_error_cb = error_cb
        return str(event)

    def unsubscribe(self, address):
        self.event = None

class MockEvent(EventFilter):
    pass
    
    

class TutorialRunnerTest(unittest.TestCase):
    """
    This class needs to test the TutorialRunner
    """
    def setUp(self):
        self.pM = MockProbeMgr()

    def tearDown(self):
        self.pM = None
    
    # Basic interface cases
    def testOneStateTutorial(self):
        tutorial = Tutorial("TutorialRunner")
        state_name = tutorial.add_state()
        tutorial.update_transition(Tutorial.INITIAL_TRANSITION_NAME,
                                        None, state_name)
        event = MockEvent() 
        tutorial.add_transition(state_name, (event, Tutorial.END))

        runner = TutorialRunner(tutorial, self.pM)

        runner.start()
        self.pM.event_sub_cB('event1')

        assert runner._state == state_name, "Current state is: %s"%runner._state
        assert self.pM.action == None
        assert self.pM.event == event
        
        event.do_callback()

        assert runner._state == Tutorial.END, "Current state is: %s"%runner._state
        assert self.pM.action == None, "Current action is %s"%str(self.pM.action)
        assert self.pM.event == None, "Current event is %s"%str(self.pM.event)
        
    # Limit cases 
    def testEmptyTutorial(self):
        tutorial = Tutorial("TutorialRunner")
        runner = TutorialRunner(tutorial, self.pM)
        runner.start()

        assert runner._state == Tutorial.END, "Current state is: %s"%runner._state
        assert self.pM.action == None
        assert self.pM.event == None
    
    # Error cases
    
if __name__ == "__main__":
    unittest.main()