From 5cdfcba9e827c2acf7dbad7890e8fcea241714a8 Mon Sep 17 00:00:00 2001 From: mike Date: Wed, 04 Mar 2009 18:57:27 +0000 Subject: TutoriusV1 : Adding basic State tests, cleaned some code bits lying around --- (limited to 'src/sugar/tutorius') diff --git a/src/sugar/tutorius/core.py b/src/sugar/tutorius/core.py index e41682e..f817ba9 100644 --- a/src/sugar/tutorius/core.py +++ b/src/sugar/tutorius/core.py @@ -66,13 +66,7 @@ class Tutorial (object): """ Detach from the current activity """ -## #Remove handlers -## for eventfilter in self.state_machine.get(self.state,{}).get("EventFilters",()): -## eventfilter.remove_handlers() -## -## #Undo actions -## for act in self.state_machine.get(self.state,{}).get("Actions",()): -## act.undo() + # Uninstall the whole FSM self.state_machine.teardown() @@ -84,31 +78,10 @@ class Tutorial (object): """ Switch to a new state """ -## if not self.state_machine.has_key(name): -## return logger.debug("====NEW STATE: %s====" % name) -## #Remove handlers -## for eventfilter in self.state_machine.get(self.state,{}).get("EventFilters",()): -## eventfilter.remove_handlers() - -## #Undo actions -## for act in self.state_machine.get(self.state,{}).get("Actions",()): -## act.undo() self.state_machine.set_state(name) - -## #Switch to new state -## self.state = name -## newstate = self.state_machine.get(name) -## #Register handlers for eventfilters -## for eventfilter in newstate["EventFilters"]: -## eventfilter.install_handlers(self._eventfilter_state_done, -## activity=self.activity) - -## #Do actions -## for act in newstate.get("Actions",()): -## act.do() # Currently unused -- equivalent function is in each state def _eventfilter_state_done(self, eventfilter): @@ -121,21 +94,6 @@ class Tutorial (object): #Swith to the next state pointed by the eventfilter self.set_state(eventfilter.get_next_state()) -# def register_signal(self, handler, obj_fqdn, signal_name): -# """Register a signal handler onto a specific widget -# @param handler function to attach as a handler -# @param obj_fqdn fqdn-style object name -# @param signal_name signal name to connect to -# -# Side effects: -# the object found and the handler id obtained by connect() are -# appended in self.handlers -# """ -# obj = find_widget(self.activity, obj_fqdn) -# self.handlers.append( \ -# (obj, obj.connect(signal_name, handler, (signal_name, obj_fqdn) ))\ -# ) - class State: """ This is a step in a tutorial. The state represents a collection of actions diff --git a/src/sugar/tutorius/tests/coretests.py b/src/sugar/tutorius/tests/coretests.py new file mode 100644 index 0000000..8c9a194 --- /dev/null +++ b/src/sugar/tutorius/tests/coretests.py @@ -0,0 +1,137 @@ +# Copyright (C) 2009, Tutorius.org +# Copyright (C) 2009, Michael Janelle-Montcalm +# +# 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 +""" +Core Tests + +This module contains all the tests that pertain to the usage of the Tutorius +Core. This means that the Event Filters, the Finite State Machine and all the +related elements and interfaces are tested here. + +""" + +import unittest + +import logging +from sugar.tutorius.actions import Action +from sugar.tutorius.core import * +from sugar.tutorius.filters import * + +# Helper classes to help testing +class SimpleTutorial(Tutorial): + """ + Fake tutorial + """ + def __init__(self, start_name="INIT"): + #Tutorial.__init__(self, "Simple Tutorial", None) + self.current_state_name = start_name + self.activity = "TODO : This should be an activity" + + def set_state(self, name): + self.current_state_name = name + +class TrueWhileActiveAction(Action): + """ + This action's active member is set to True after a do and to False after + an undo. + + Used to verify that a State correctly triggers the do and undo actions. + """ + def __init__(self): + self.active = False + + def do(self): + self.active = True + + def undo(self): + self.active = False + +class TriggerEventFilter(EventFilter): + """ + This event filter can be triggered by simply calling its execute function. + + Used to fake events and see the effect on the FSM. + """ + def __init__(self, next_state): + EventFilter.__init__(self, next_state) + self.toggle_on_callback = False + + def install_handlers(self, callback, **kwargs): + """ + Forsakes the incoming callback function and just set the inner one. + """ + self._callback = self._inner_cb + + def _inner_cb(self, event_filter): + self.toggle_on_callback = not self.toggle_on_callback + +# State testing class +class StateTest(unittest.TestCase): + """ + This class has to test the State interface as well as the expected + functionality. + """ + + def test_action_toggle(self): + """ + Validate that the actions are properly done on setup and undone on + teardown. + + Pretty awesome. + """ + act = TrueWhileActiveAction() + + state = State("action_test", action_list=[act]) + + assert act.active == False, "Action is not initialized properly" + + state.setup() + + assert act.active == True, "Action was not triggered properly" + + state.teardown() + + assert act.active == False, "Action was not undone properly" + + def test_event_filter(self): + """ + Tests the fact that the event filters are correctly installed on setup + and uninstalled on teardown. + """ + event_filter = TriggerEventFilter("second_state") + + state = State("event_test", event_filter_list=[event_filter]) + state.set_tutorial(SimpleTutorial()) + + assert event_filter.toggle_on_callback == False, "Wrong init of event_filter" + assert event_filter._callback == None, "Event filter has a registered callback before installing handlers" + + state.setup() + + assert event_filter._callback != None, "Event filter did not register callback!" + + # 'Trigger' the event - This is more like a EventFilter test. + event_filter.do_callback() + + assert event_filter.toggle_on_callback == True, "Event filter did not execute callback" + + state.teardown() + + assert event_filter._callback == None, "Event filter did not remove callback properly" + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file -- cgit v0.9.1