Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar/tutorius
diff options
context:
space:
mode:
authormike <michael.jmontcalm@gmail.com>2009-04-06 02:41:41 (GMT)
committer mike <michael.jmontcalm@gmail.com>2009-04-06 17:18:13 (GMT)
commitbbc61e78c1f47537f9a7118073ca89f06ad53ea9 (patch)
tree2f7a47edb6edd387bf789eae282457563105bcc2 /src/sugar/tutorius
parent217ea2dca4f69def9811cf3ad09fc0888178b192 (diff)
LP 353185 Core : Create chain action
Diffstat (limited to 'src/sugar/tutorius')
-rw-r--r--src/sugar/tutorius/actions.py15
-rw-r--r--src/sugar/tutorius/tests/actiontests.py53
2 files changed, 66 insertions, 2 deletions
diff --git a/src/sugar/tutorius/actions.py b/src/sugar/tutorius/actions.py
index 60ccd8b..7681dea 100644
--- a/src/sugar/tutorius/actions.py
+++ b/src/sugar/tutorius/actions.py
@@ -213,4 +213,19 @@ class WidgetIdentifyAction(Action):
if self._dialog:
self._dialog.destroy()
+class ChainAction(Action):
+ """Utility class to allow executing actions in a specific order"""
+ def __init__(self, *actions):
+ """ChainAction(action1, ... ) builds a chain of actions"""
+ self._actions = actions
+
+ def do(self,**kwargs):
+ """do() each action in the chain"""
+ for act in self._actions:
+ act.do(**kwargs)
+
+ def undo(self):
+ """undo() each action in the chain, starting with the last"""
+ for act in reversed(self._actions):
+ act.undo()
diff --git a/src/sugar/tutorius/tests/actiontests.py b/src/sugar/tutorius/tests/actiontests.py
index 5fb3d0f..9c398d4 100644
--- a/src/sugar/tutorius/tests/actiontests.py
+++ b/src/sugar/tutorius/tests/actiontests.py
@@ -24,7 +24,7 @@ The behavior of the actions must be tested here.
import unittest
import gtk
-from sugar.tutorius.actions import Action, OnceWrapper
+from sugar.tutorius.actions import *
from sugar.tutorius.services import ObjectStore
class PropertyAction(Action):
@@ -104,6 +104,55 @@ class OnceWrapperTests(unittest.TestCase):
wrap.undo()
assert act.undo_count == 1, "undo() should have been called only once"
+class ChainTester(Action):
+ def __init__(self, witness):
+ self._witness = witness
+
+ def do(self, **kwargs):
+ self._witness.append([self,"do"])
+
+ def undo(self):
+ self._witness.append([self,"undo"])
+
+class ChainActionTest(unittest.TestCase):
+ """Tester for ChainAction"""
+ def test_empty(self):
+ """If the expected empty behavior (do nothing) changes
+ and starts throwing exceptions, this will flag it"""
+ a = ChainAction()
+ a.do()
+ a.undo()
+
+ def test_order(self):
+ witness = []
+ first = ChainTester(witness)
+ second = ChainTester(witness)
+
+ c = ChainAction(first, second)
+ assert witness == [], "Actions should not be triggered on init"""
+ c.do()
+
+ assert witness[0][0] is first, "First triggered action must be 'first'"
+ assert witness[0][1] is "do", "Action do() should be triggered"
+
+ assert witness[1][0] is second, "second triggered action must be 'second'"
+ assert witness[1][1] is "do", "Action do() should be triggered"
+
+ assert len(witness) is 2, "Two actions should give 2 do's"
+
+ #empty the witness list
+ while len(witness):
+ rm = witness.pop()
+
+ c.undo()
+ assert witness[1][0] is first, "second triggered action must be 'first'"
+ assert witness[1][1] is "undo", "Action undo() should be triggered"
+
+ assert witness[0][0] is second, "first triggered action must be 'second'"
+ assert witness[0][1] is "undo", "Action undo() should be triggered"
+
+ assert len(witness) is 2, "Two actions should give 2 undo's"
+
+
if __name__ == "__main__":
unittest.main()
-