diff options
Diffstat (limited to 'src/sugar/tutorius/tests')
-rw-r--r-- | src/sugar/tutorius/tests/actiontests.py | 173 | ||||
-rw-r--r-- | src/sugar/tutorius/tests/constraintstests.py | 211 | ||||
-rw-r--r-- | src/sugar/tutorius/tests/coretests.py | 174 | ||||
-rw-r--r-- | src/sugar/tutorius/tests/filterstests.py | 200 | ||||
-rw-r--r-- | src/sugar/tutorius/tests/gtkutilstests.py | 91 | ||||
-rw-r--r-- | src/sugar/tutorius/tests/linear_creatortests.py | 16 | ||||
-rw-r--r-- | src/sugar/tutorius/tests/propertiestests.py | 348 | ||||
-rw-r--r-- | src/sugar/tutorius/tests/uamtests.py | 61 |
8 files changed, 1217 insertions, 57 deletions
diff --git a/src/sugar/tutorius/tests/actiontests.py b/src/sugar/tutorius/tests/actiontests.py new file mode 100644 index 0000000..ab9cdba --- /dev/null +++ b/src/sugar/tutorius/tests/actiontests.py @@ -0,0 +1,173 @@ +# Copyright (C) 2009, Tutorius.org +# Copyright (C) 2009, Michael Janelle-Montcalm <michael.jmontcalm@gmail.com> +# Copyright (C) 2009, Vincent Vinet <vince.vinet@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 +""" +Action tests + +The behavior of the actions must be tested here. +""" + +import unittest +import gtk + +from sugar.tutorius.actions import * +from sugar.tutorius.services import ObjectStore + +class PropertyAction(Action): + def __init__(self, na): + self._a = na + + def set_a(self, na): + self._a = na + + def get_a(self): + return self._a + + a = property(fget=get_a, fset=set_a) + +class PropsTest(unittest.TestCase): + + def test_get_properties(self): + prop = PropertyAction(8) + + assert prop.get_properties() == ['a'], "Action does not contain property 'a'" + +class CountAction(Action): + """ + This action counts how many times it's do and undo methods get called + """ + def __init__(self): + self.do_count = 0 + self.undo_count = 0 + + def do(self): + self.do_count += 1 + + def undo(self): + self.undo_count += 1 + + +class BaseActionTests(unittest.TestCase): + def test_do_unimplemented(self): + act = Action() + try: + act.do() + assert False, "do() should trigger a NotImplemented" + except NotImplementedError: + assert True, "do() should trigger a NotImplemented" + + def test_undo(self): + act = Action() + act.undo() + assert True, "undo() should never fail on the base action" + + +class OnceWrapperTests(unittest.TestCase): + def test_onceaction_toggle(self): + """ + Validate that the OnceWrapper wrapper works properly using the + CountAction + """ + act = CountAction() + wrap = OnceWrapper(act) + + assert act.do_count == 0, "do() should not have been called in __init__()" + assert act.undo_count == 0, "undo() should not have been called in __init__()" + + wrap.undo() + + assert act.undo_count == 0, "undo() should not be called if do() has not been called" + + wrap.do() + assert act.do_count == 1, "do() should have been called once" + + wrap.do() + assert act.do_count == 1, "do() should have been called only once" + + wrap.undo() + assert act.undo_count == 1, "undo() should have been called once" + + 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" + +class DisableWidgetActionTests(unittest.TestCase): + def test_disable(self): + btn = gtk.Button() + ObjectStore().activity = btn + btn.set_sensitive(True) + + assert btn.props.sensitive is True, "Callback should have been called" + + act = DisableWidgetAction("0") + assert btn.props.sensitive is True, "Callback should have been called again" + act.do() + assert btn.props.sensitive is False, "Callback should not have been called again" + act.undo() + assert btn.props.sensitive is True, "Callback should have been called again" + +if __name__ == "__main__": + unittest.main() + diff --git a/src/sugar/tutorius/tests/constraintstests.py b/src/sugar/tutorius/tests/constraintstests.py new file mode 100644 index 0000000..407cc24 --- /dev/null +++ b/src/sugar/tutorius/tests/constraintstests.py @@ -0,0 +1,211 @@ +# Copyright (C) 2009, Tutorius.org +# +# 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 + +import unittest + +from sugar.tutorius.constraints import * + +class ConstraintTest(unittest.TestCase): + def test_base_class(self): + cons = Constraint() + try: + cons.validate(1) + assert False, "Base class should throw an assertion" + except NotImplementedError: + pass + +class ValueConstraintTest(unittest.TestCase): + def test_limit_set(self): + cons = ValueConstraint(12) + + assert cons.limit == 12 + +class UpperLimitConstraintTest(unittest.TestCase): + def test_empty_constraint(self): + cons = UpperLimitConstraint(None) + try: + cons.validate(20) + except UpperLimitConstraintError: + assert False, "Empty contraint should not raise an exception" + + def test_validate(self): + cons = UpperLimitConstraint(10) + + try: + cons.validate(20) + assert False, "Validation of UpperLimit(10) on 20 should raise an exception" + except UpperLimitConstraintError: + pass + + try: + cons.validate(5) + except UpperLimitConstraintError: + assert True, "Validation of UpperLimit(10) on 5 should not raise an exception" + +class LowerLimitConstraintTest(unittest.TestCase): + def test_empty_constraint(self): + cons = LowerLimitConstraint(None) + try: + cons.validate(20) + except LowerLimitConstraintError: + assert False, "Empty contraint should not raise an exception" + + def test_validate(self): + cons = LowerLimitConstraint(10) + + try: + cons.validate(5) + assert False, "Validation of LowerLimit(10) on 5 should raise an exception" + except LowerLimitConstraintError: + pass + + try: + cons.validate(20) + except LowerLimitConstraintError: + assert True, "Validation of LowerLimit(10) on 20 should not raise an exception" + +class SizeConstraintTest(unittest.TestCase): + def test_empty_constraint(self): + cons = SizeConstraint(None) + try: + cons.validate(20) + except SizeConstraintError: + assert False, "Empty contraint should not raise an exception" + + def test_validate(self): + cons = SizeConstraint(10) + + try: + cons.validate(range(0, 20)) + assert False, "Validation of SizeLimit(10) on list of length 20 should raise an exception" + except SizeConstraintError: + pass + + try: + cons.validate(range(0,5)) + except SizeConstraintError: + assert True, "Validation of SizeLimit(10) on list of length 5 should not raise an exception" + +class ColorConstraintTest(unittest.TestCase): + def test_validate(self): + cons = ColorConstraint() + + try: + cons.validate([0, 0]) + assert False, "ColorConstraint on list of length 2 should raise an exception" + except ColorArraySizeError: + pass + except ColorConstraintError: + assert False, "ColorConstraint threw the wrong type of error" + + try: + cons.validate([0, 0, "str"]) + assert False, "ColorConstraint on with non-integers values should raise an exception" + except ColorTypeError: + pass + except ColorConstraintError: + assert False, "ColorConstraint threw the wrong type of error" + + try: + cons.validate([0, "str", 0]) + assert False, "ColorConstraint on with non-integers values should raise an exception" + except ColorTypeError: + pass + except ColorConstraintError: + assert False, "ColorConstraint threw the wrong type of error" + + try: + cons.validate(["str", 0, 0]) + assert False, "ColorConstraint on with non-integers values should raise an exception" + except ColorTypeError: + pass + except ColorConstraintError: + assert False, "ColorConstraint threw the wrong type of error" + + try: + cons.validate([1, 2, 300]) + assert False, "ColorConstraint on with non-integers values should raise an exception" + except ColorValueError: + pass + except ColorConstraintError: + assert False, "ColorConstraint threw the wrong type of error" + + try: + cons.validate([1, -100, 30]) + assert False, "ColorConstraint on with non-integers values should raise an exception" + except ColorValueError: + pass + except ColorConstraintError: + assert False, "ColorConstraint threw the wrong type of error" + + try: + cons.validate([999999, 2, 300]) + assert False, "ColorConstraint on with non-integers values should raise an exception" + except ColorValueError: + pass + except ColorConstraintError: + assert False, "ColorConstraint threw the wrong type of error" + + try: + cons.validate([12, 23, 34]) + except LowerLimitConstraintError: + assert True, "ColorConstraint on expected input should not raise an exception" + +class BooleanConstraintTest(unittest.TestCase): + def test_validate(self): + cons = BooleanConstraint() + + cons.validate(True) + cons.validate(False) + + try: + cons.validate(18) + assert False, "Setting integer on constraint should raise an error" + except BooleanConstraintError: + pass + except: + assert False, "Wrong exception type raised when setting wrong type" + +class EnumConstraintTest(unittest.TestCase): + def test_validate(self): + cons = EnumConstraint([1,2,3,7,8,9, "ex"]) + + cons.validate(8) + + cons.validate("ex") + + try: + cons.validate(4) + assert False, "There should be an exception on setting a value out of the enum" + except EnumConstraintError: + pass + except: + assert False, "Wrong exception type thrown" + +class FileConstraintTest(unittest.TestCase): + def test_validate(self): + cons = FileConstraint() + + cons.validate("run-tests.py") + + try: + cons.validate("unknown/file.py") + assert False, "Non-existing file check should throw an exception" + except FileConstraintError: + pass + +if __name__ == "__main__": + unittest.main()
\ No newline at end of file diff --git a/src/sugar/tutorius/tests/coretests.py b/src/sugar/tutorius/tests/coretests.py index ec730f2..5f91a64 100644 --- a/src/sugar/tutorius/tests/coretests.py +++ b/src/sugar/tutorius/tests/coretests.py @@ -18,18 +18,24 @@ 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 +Core. This means that the the Finite State Machine, States and all the related elements and interfaces are tested here. +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 import logging -from sugar.tutorius.actions import Action, OnceWrapper +from sugar.tutorius.actions import Action, ClickAction, TypeTextAction from sugar.tutorius.core import * from sugar.tutorius.filters import * + +from actiontests import CountAction + # Helper classes to help testing class SimpleTutorial(Tutorial): """ @@ -58,21 +64,46 @@ class TrueWhileActiveAction(Action): def undo(self): self.active = False + +class ClickableWidget(): + """ + This class fakes a widget with a clicked() method + """ + def __init__(self): + self.click_count = 0 + + def clicked(self): + self.click_count += 1 + +class FakeTextEntry(): + """ + This class fakes a widget with an insert_text() method + """ + def __init__(self): + self.text_lines = [] + self.last_entered_line = "" + self.displayed_text = "" + + def insert_text(self, text, index): + self.last_entered_line = text + self.text_lines.append(text) + self.displayed_text = self.displayed_text[0:index] + text + self.displayed_text[index+1:] - -class CountAction(Action): +class FakeParentWidget(): """ - This action counts how many times it's do and undo methods get called + This class fakes a widet container, it implements the get_children() method """ def __init__(self): - self.do_count = 0 - self.undo_count = 0 + self._children = [] + + def add_child(self, child): + self._children.append(child) + + def get_children(self): + return self._children + - def do(self): - self.do_count += 1 - def undo(self): - self.undo_count += 1 class TriggerEventFilter(EventFilter): """ @@ -110,49 +141,96 @@ class FakeEventFilter(TriggerEventFilter): self.tutorial.set_state(event_filter.get_next_state()) -class BaseActionTests(unittest.TestCase): - def test_do_unimplemented(self): - act = Action() - try: - act.do() - assert False, "do() should trigger a NotImplemented" - except NotImplementedError: - assert True, "do() should trigger a NotImplemented" +class ClickActionTests(unittest.TestCase): + """ + Test class for click action + """ + def test_do_action(self): + activity = FakeParentWidget() + widget = ClickableWidget() + activity.add_child(widget) + ObjectStore().activity = activity + + action = ClickAction("0.0") + + assert widget == ObjectStore().activity.get_children()[0],\ + "The clickable widget isn't reachable from the object store \ + the test cannot pass" + + action.do() + + assert widget.click_count == 1, "clicked() should have been called by do()" + + action.do() + + assert widget.click_count == 2, "clicked() should have been called by do()" def test_undo(self): - act = Action() - act.undo() - assert True, "undo() should never fail on the base action" - - -class OnceWrapperTests(unittest.TestCase): - def test_onceaction_toggle(self): - """ - Validate that the OnceWrapper wrapper works properly using the - CountAction - """ - act = CountAction() - wrap = OnceWrapper(act) - - assert act.do_count == 0, "do() should not have been called in __init__()" - assert act.undo_count == 0, "undo() should not have been called in __init__()" - - wrap.undo() - - assert act.undo_count == 0, "undo() should not be called if do() has not been called" - - wrap.do() - assert act.do_count == 1, "do() should have been called once" + activity = FakeParentWidget() + widget = ClickableWidget() + activity.add_child(widget) + ObjectStore().activity = activity + + action = ClickAction("0.0") + + assert widget == ObjectStore().activity.get_children()[0],\ + "The clickable widget isn't reachable from the object store \ + the test cannot pass" + + action.undo() + + #There is no undo for this action so the test should not fail + assert True + + - wrap.do() - assert act.do_count == 1, "do() should have been called only once" +class TypeTextActionTests(unittest.TestCase): + """ + Test class for type text action + """ + def test_do_action(self): + activity = FakeParentWidget() + widget = FakeTextEntry() + activity.add_child(widget) + ObjectStore().activity = activity - wrap.undo() - assert act.undo_count == 1, "undo() should have been called once" + test_text = "This is text" + + + action = TypeTextAction("0.0", test_text) + + assert widget == ObjectStore().activity.get_children()[0],\ + "The clickable widget isn't reachable from the object store \ + the test cannot pass" + + action.do() + + assert widget.last_entered_line == test_text, "insert_text() should have been called by do()" + + action.do() + + assert widget.last_entered_line == test_text, "insert_text() should have been called by do()" + assert len(widget.text_lines) == 2, "insert_text() should have been called twice" - wrap.undo() - assert act.undo_count == 1, "undo() should have been called only once" + def test_undo(self): + activity = FakeParentWidget() + widget = FakeTextEntry() + activity.add_child(widget) + ObjectStore().activity = activity + test_text = "This is text" + + + action = TypeTextAction("0.0", test_text) + + assert widget == ObjectStore().activity.get_children()[0],\ + "The clickable widget isn't reachable from the object store \ + the test cannot pass" + + action.undo() + + #There is no undo for this action so the test should not fail + assert True # State testing class class StateTest(unittest.TestCase): diff --git a/src/sugar/tutorius/tests/filterstests.py b/src/sugar/tutorius/tests/filterstests.py new file mode 100644 index 0000000..8ee6cc8 --- /dev/null +++ b/src/sugar/tutorius/tests/filterstests.py @@ -0,0 +1,200 @@ +# Copyright (C) 2009, Tutorius.org +# Copyright (C) 2009, Vincent Vinet <vince.vinet@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 +""" +Filters Tests + +This module contains all the tests that pertain to the usage of the Tutorius +Event Filters +""" + +import unittest +import time +import gobject +import gtk + +from sugar.tutorius.filters import EventFilter, TimerEvent, GtkWidgetEventFilter, GtkWidgetTypeFilter +from gtkutilstests import SignalCatcher + +class BaseEventFilterTests(unittest.TestCase): + """Test the behavior of the Base EventFilter class""" + def test_properties(self): + """Test EventFilter properties""" + e = EventFilter("NEXTSTATE") + + assert e.next_state == "NEXTSTATE", "next_state should have value used in constructor" + + e.next_state = "NEWSTATE" + + assert e.next_state == "NEWSTATE", "next_state should have been changed by setter" + + + def test_callback(self): + """Test the callback mechanism""" + e = EventFilter("Next") + s = SignalCatcher() + + #Trigger the do_callback, shouldn't do anything + e.do_callback() + + #Install the handler + e.install_handlers(s.callback) + + #Trigger the do_callback, s should receive e + e.do_callback() + assert s.data[0] is e + + s.data = None + + e.remove_handlers() + + #Trigger callback, nothing should happen again + e.do_callback() + + assert s.data is None + + + + + +class TestTimerEvent(unittest.TestCase): + """Tests for timer""" + def test_timer(self): + """Make sure timer gets called once, and only once""" + gobject.threads_init() + ctx = gobject.MainContext() + main = gobject.MainLoop(ctx) + + e = TimerEvent("Next",1) #1 second should be enough :s + s = SignalCatcher() + + e.install_handlers(s.callback) + + assert s.data is None, "Callback should not have been called yet" + + #process events + while gtk.events_pending(): + gtk.main_iteration(block=False) + while ctx.pending(): + ctx.iteration(may_block=False) + + #Wait 1.4 sec + time.sleep(1.4) + + #process events + while gtk.events_pending(): + gtk.main_iteration(block=False) + while ctx.pending(): + ctx.iteration(may_block=False) + + assert not s.data is None, "Callback should have been called" + + s.data = None + + #Wait 1.4 sec + time.sleep(1.4) + + #process events + while gtk.events_pending(): + gtk.main_iteration(block=False) + while ctx.pending(): + ctx.iteration(may_block=False) + + assert s.data is None, "Callback should not have been called again" + + def test_timer_stop(self): + """Make sure timer can be stopped""" + gobject.threads_init() + ctx = gobject.MainContext() + main = gobject.MainLoop(ctx) + + e = TimerEvent("Next",1) #1 second should be enough :s + s = SignalCatcher() + + e.install_handlers(s.callback) + + assert s.data is None, "Callback should not have been called yet" + + #process events + while gtk.events_pending(): + gtk.main_iteration(block=False) + while ctx.pending(): + ctx.iteration(may_block=False) + + assert s.data is None, "Callback should not have been called yet" + + #Wait 0.5 sec + time.sleep(0.5) + + e.remove_handlers() + + #Wait 0.5 sec + time.sleep(0.7) + + #process events + while gtk.events_pending(): + gtk.main_iteration(block=False) + while ctx.pending(): + ctx.iteration(may_block=False) + + assert s.data is None, "Callback should not have been called" + + s.data = None + + +class TestGtkWidgetEventFilter(unittest.TestCase): + """Tests for GtkWidgetEventFilter""" + def __init__(self,*args): + unittest.TestCase.__init__(self,*args) + self.top=None + self.btn1=None + + def setUp(self): + self.top = gtk.Window() + self.btn1 = gtk.Button() + self.top.add(self.btn1) + + def test_install(self): + h = GtkWidgetEventFilter("Next","0","whatever") + try: + h.install_handlers(None) + + assert False, "Install handlers should have failed" + except TypeError: + assert True, "Install should have failed" + + def test_button_clicks(self): + h = GtkWidgetEventFilter("Next","0.0","clicked") + s = SignalCatcher() + + h.install_handlers(s.callback, activity=self.top) + + assert s.data is None, "no callback to call yet" + + self.btn1.clicked() + assert not s.data is None, "callback should have been called" + s.data = None + + h.remove_handlers() + + assert s.data is None, "callback must not be called again" + + self.btn1.clicked() + + assert s.data is None, "callback must not be called again" + + + diff --git a/src/sugar/tutorius/tests/gtkutilstests.py b/src/sugar/tutorius/tests/gtkutilstests.py index fb9a20b..41634ae 100644 --- a/src/sugar/tutorius/tests/gtkutilstests.py +++ b/src/sugar/tutorius/tests/gtkutilstests.py @@ -26,16 +26,22 @@ import unittest import logging import gtk, gobject -from sugar.tutorius.gtkutils import find_widget, register_signals_numbered, register_signals +from sugar.tutorius.gtkutils import find_widget, register_signals_numbered, register_signals, get_children class SignalCatcher(object): + """Test class that store arguments received on it's callback method. + Useful for testing callbacks""" def __init__(self): - self.data = [] + """Constructor""" + self.data = None def callback(self, *args): + """Callback function, stores argument list in self.data""" self.data = args def disconnect_handlers(hlist): + """Disconnect handles in handler list. hlist must be a list of + two-tuples (widget, handler_id)""" for widget, handler in hlist: try: widget.handler_disconnect(handler) @@ -97,6 +103,8 @@ class GtkUtilsTests(unittest.TestCase): def test_named(self): #def register_signals(target, handler, prefix=None, max_depth=None): s=SignalCatcher() + + #Test 0 depth handler_list = register_signals(self.top, s.callback, max_depth=0) #remove duplicates in widget list @@ -104,12 +112,36 @@ class GtkUtilsTests(unittest.TestCase): assert len(widget_list) == 1, "register_signals should not have recursed (%d objects registered)" % len(widget_list) - while gtk.events_pending(): - gtk.main_iteration(block=False) + assert widget_list[0] == self.top, "register_signals should have gotten only the top" + + disconnect_handlers(handler_list) + + #Test 2 depth + handler_list = register_signals(self.top, s.callback, max_depth=2) + + #remove duplicates in widget list + widget_list = dict.fromkeys([w for w, h in handler_list]).keys() + + assert len(widget_list) == 5, "expected %d objects (got %d)" % (len(widget_list), 5) + + disconnect_handlers(handler_list) + + #Test Infinite depth + handler_list = register_signals(self.top, s.callback, max_depth=None) + + #remove duplicates in widget list + widget_list = dict.fromkeys([w for w, h in handler_list]).keys() + + assert len(widget_list) == 7, "expected %d objects (got %d)" % (len(widget_list), 7) + + disconnect_handlers(handler_list) + def test_numbered(self): s=SignalCatcher() #def register_signals_numbered(target, handler, prefix="0", max_depth=None): + + #Test 0 depth handler_list = register_signals_numbered(self.top, s.callback, max_depth=0) #remove duplicates in widget list @@ -117,15 +149,62 @@ class GtkUtilsTests(unittest.TestCase): assert len(widget_list) == 1, "register_signals should not have recursed (%d objects registered)" % len(widget_list) - while gtk.events_pending(): - gtk.main_iteration(block=False) + assert widget_list[0] == self.top, "register_signals should have gotten only the top" + + disconnect_handlers(handler_list) + + #Test 1 depth + handler_list = register_signals_numbered(self.top, s.callback, max_depth=1) + + #remove duplicates in widget list + widget_list = dict.fromkeys([w for w, h in handler_list]).keys() + + assert len(widget_list) == 2, "expected %d objects (got %d)" % (len(widget_list), 2) + + disconnect_handlers(handler_list) + + #Test Infinite depth + handler_list = register_signals_numbered(self.top, s.callback, max_depth=None) + + #remove duplicates in widget list + widget_list = dict.fromkeys([w for w, h in handler_list]).keys() + + assert len(widget_list) == 7, "expected %d objects (got %d)" % (len(widget_list), 7) + + disconnect_handlers(handler_list) + def test_find_widget(self): + #Test individual values in the defined widgets for widget in self.widgets.values(): f = find_widget(self.top, widget["numbered"]) assert f is widget["widget"], "Widget %s found with path %s, expected %s" % (f, widget["numbered"], widget["widget"]) + #Test out of index + f = find_widget(self.top, "0.99.1.2") + assert f is self.top, "Should have returned top widget" + + def test_register_args_numbered(self): + #Need to check the signal catcher and stuff... grreat + while gtk.events_pending(): + gtk.main_iteration(block=False) + + + def test_register_args_normal(self): + #Need to check the signal catcher and stuff... grreat + while gtk.events_pending(): + gtk.main_iteration(block=False) + + def test_notwidget(self): + """Test the get_children function""" + o = object() + res = get_children(o) + + assert len(res) == 0, "object has no children" + top_children = get_children(self.top) + expected = [self.widgets["hbox0"]["widget"],] + assert top_children == expected, "expected %s for top's children, got %s" % (str(expected),str(top_children)) if __name__ == "__main__": unittest.main() diff --git a/src/sugar/tutorius/tests/linear_creatortests.py b/src/sugar/tutorius/tests/linear_creatortests.py index 3bc06f9..f9ffbe7 100644 --- a/src/sugar/tutorius/tests/linear_creatortests.py +++ b/src/sugar/tutorius/tests/linear_creatortests.py @@ -19,8 +19,8 @@ from sugar.tutorius.core import * from sugar.tutorius.actions import * from sugar.tutorius.filters import * from sugar.tutorius.linear_creator import * -from coretests import TriggerEventFilter, CountAction - +from coretests import TriggerEventFilter +from actiontests import CountAction import unittest class CreatorTests(unittest.TestCase): @@ -64,6 +64,16 @@ class CreatorTests(unittest.TestCase): assert len(state2.get_action_list()) == 0, "Creator inserted extra actions on wrong state" assert len(state2.get_event_filter_list()) == 0, "Creator assigner events to the final state" + + creator.action(CountAction()) + + fsm = creator.generate_fsm() + + state2 = fsm.get_state_by_name("State2") + + assert len(state2.get_action_list()) == 1, "Creator did not add the action" + + assert len(state2.get_event_filter_list()) == 0, "Creator assigner events to the final state" if __name__ == '__main__': - unittest.main()
\ No newline at end of file + unittest.main() diff --git a/src/sugar/tutorius/tests/propertiestests.py b/src/sugar/tutorius/tests/propertiestests.py new file mode 100644 index 0000000..52a9a75 --- /dev/null +++ b/src/sugar/tutorius/tests/propertiestests.py @@ -0,0 +1,348 @@ +# Copyright (C) 2009, Tutorius.org +# +# 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 + +import unittest + +from sugar.tutorius.constraints import * +from sugar.tutorius.properties import * + +# Helper function to test the wrong types on a property, given its type +def try_wrong_values(prop): + if prop.type != "int": + try: + prop.set(3) + assert False, "Able to insert int value in property of type %s"%prop.type + except: + pass + + if prop.type != "float": + try: + prop.set(1.1) + assert False, "Able to insert float value in property of type %s"%prop.type + except: + pass + + if prop.type != "string": + try: + prop.set("Fake string") + assert False, "Able to insert string value in property of type %s"%prop.type + except: + pass + + if prop.type != "array": + try: + prop.set([1, 2000, 3, 400]) + assert False, "Able to insert array value in property of type %s"%prop.type + except: + pass + + if prop.type != "color": + try: + prop.set([1,2,3]) + if prop.type != "array": + assert False, "Able to insert color value in property of type %s"%prop.type + except: + pass + + if prop.type != "boolean": + try: + prop.set(True) + if prop.type != "boolean": + assert False, "Able to set boolean value in property of type %s"%prop.type + except: + pass + +class BasePropertyTest(unittest.TestCase): + def test_base_class(self): + prop = TutoriusProperty() + + assert prop.value == None, "There should not be an initial value in the base property" + + assert prop.type == None, "There should be no type associated with the base property" + + assert prop.get_constraints() == [], "There should be no constraints on the base property" + + prop.set(2) + + assert prop.value == 2, "Unable to set a value on base class" + +class TIntPropertyTest(unittest.TestCase): + def test_int_property(self): + prop = TIntProperty(22) + + assert prop.value == 22, "Could not set value on property via constructor" + + assert prop.type == "int", "Wrong type on int property : %s" % prop.type + cons = prop.get_constraints() + assert len(cons) == 2, "Not enough constraints on the int property" + + prop.set(12) + + assert prop.value == 12, "Could not set value" + + def test_wrong_values(self): + prop = TIntProperty(33) + + # Try setting values of other types + try_wrong_values(prop) + + def test_limit_constructor(self): + prop = TIntProperty(22, 0, 30) + + try: + prop.set(-22) + assert False, "Assigning an out-of-range value should trigger LowerLimitConstraint" + except LowerLimitConstraintError: + pass + except Exception: + assert False, "Wrong exception triggered by assignation" + + try: + prop.set(222) + assert False, "Assigning an out-of-range value should trigger UpperLimitConstraint" + except UpperLimitConstraintError: + pass + except Exception: + assert False, "Wrong exception triggered by assignation" + + def test_failing_constructor(self): + try: + prop = TIntProperty(100, 0, 20) + assert False, "Creation of the property should fail." + except UpperLimitConstraintError: + pass + except: + assert False, "Wrong exception type on failed constructor" + + try: + prop = TIntProperty(-100, 0, 20) + assert False, "Creation of the property should fail." + except LowerLimitConstraintError: + pass + except: + assert False, "Wrong exception type on failed constructor" + +class TFloatPropertyTest(unittest.TestCase): + def test_float_property(self): + prop = TFloatProperty(22) + + assert prop.value == 22, "Could not set value on property via constructor" + + assert prop.type == "float", "Wrong type on float property : %s" % prop.type + cons = prop.get_constraints() + assert len(cons) == 2, "Not enough constraints on the float property" + + prop.set(12) + + assert prop.value == 12, "Could not set value" + + def test_wrong_values(self): + prop = TFloatProperty(33) + # Try setting values of other types + try_wrong_values(prop) + + def test_limit_constructor(self): + prop = TFloatProperty(22.4, 0.1, 30.5223) + + try: + prop.set(-22.8) + assert False, "Assigning an out-of-range value should trigger LowerLimitConstraint" + except LowerLimitConstraintError: + pass + except Exception: + assert False, "Wrong exception triggered by assignation" + + try: + prop.set(222.2) + assert False, "Assigning an out-of-range value should trigger UpperLimitConstraint" + except UpperLimitConstraintError: + pass + except Exception: + assert False, "Wrong exception triggered by assignation" + + def test_failing_constructor(self): + try: + prop = TFloatProperty(100, 0, 20) + assert False, "Creation of the property should fail." + except UpperLimitConstraintError: + pass + except: + assert False, "Wrong exception type on failed constructor" + + try: + prop = TFloatProperty(-100, 0, 20) + assert False, "Creation of the property should fail." + except LowerLimitConstraintError: + pass + except: + assert False, "Wrong exception type on failed constructor" + +class TStringPropertyTest(unittest.TestCase): + def test_basic_string(self): + prop = TStringProperty("Starter string") + + assert prop.value == "Starter string", "Could not set string value via constructor" + + assert prop.type == "string", "Wrong type for string property : %s" % prop.type + + def test_size_limit(self): + prop = TStringProperty("Small", 10) + + try: + prop.set("My string is too big!") + assert False, "String should not set to longer than max size" + except SizeConstraintError: + pass + except: + assert False, "Wrong exception type thrown" + + def test_wrong_values(self): + prop = TStringProperty("Beginning") + + try_wrong_values(prop) + + def test_failing_constructor(self): + try: + prop = TStringProperty("This is normal", 5) + assert False, "Creation of the property should fail." + except SizeConstraintError: + pass + except: + assert False, "Wrong exception type on failed constructor" + +class TArrayPropertyTest(unittest.TestCase): + def test_basic_array(self): + prop = TArrayProperty([1, 2, 3, 4]) + + assert prop.value == [1,2,3,4], "Unable to set initial value via constructor" + + assert prop.type == "array", "Wrong type for array : %s"%prop.type + + def test_wrong_values(self): + prop = TArrayProperty([1,2,3,4,5]) + + try_wrong_values(prop) + + def test_size_limit(self): + prop = TArrayProperty([1,2], 4) + + try: + prop.set([1,2,4,5,6,7]) + assert False, "Size limit constraint was not properly applied" + except SizeConstraintError: + pass + except: + assert False, "Wrong type of exception thrown" + + + def test_failing_constructor(self): + try: + prop = TArrayProperty([100, 0, 20], 2) + assert False, "Creation of the property should fail." + except SizeConstraintError: + pass + except: + assert False, "Wrong exception type on failed constructor" + +class TColorPropertyTest(unittest.TestCase): + def test_basic_color(self): + prop = TColorProperty(20, 40, 60) + + assert prop.value == [20, 40, 60], "Could not set initial value with constructor" + + assert prop.type == "color", "Wrong type on color : %s"%prop.type + + def test_wrong_values(self): + prop = TColorProperty(250, 250, 250) + + try_wrong_values(prop) + + def test_failing_constructor(self): + try: + prop = TColorProperty(0, "str", 0) + assert False, "Creation of the property should fail." + except ColorTypeError: + pass + except: + assert False, "Wrong exception type on failed constructor" + +class TBooleanPropertyTest(unittest.TestCase): + def setUp(self): + self.prop = TBooleanProperty(False) + + def test_basic_boolean(self): + assert self.prop.value == False, "Could not set initial value via constructor" + + self.prop.set(True) + + assert self.prop.value == True, "Could not change the value via set" + + self.prop.set(False) + + assert self.prop.value == False, "Could not change the value via set" + + def test_wrong_types(self): + try_wrong_values(self.prop) + + def test_failing_constructor(self): + try: + prop = TBooleanProperty(64) + assert False, "Creation of the property should fail with non-boolean value" + except BooleanConstraintError: + pass + except: + assert False, "Wrong exception type on failed constructor" + +class TEnumPropertyTest(unittest.TestCase): + def setUp(self): + self.prop = TEnumProperty("hello", [1, 2, "hello", "world", True, None]) + + def test_basic_enum(self): + assert self.prop.value == "hello", "Could not set initial value on property" + + self.prop.set(True) + + assert self.prop.value, "Could not change the value via set" + + try: + self.prop.set(4) + assert False, "Switched to a value outside the enum" + except EnumConstraintError: + pass + + def test_wrong_type(self): + try_wrong_values(self.prop) + +class TFilePropertyTest(unittest.TestCase): + def setUp(self): + self.prop = TFileProperty("propertiestests.py") + + def test_basic_file(self): + assert self.prop.value == "propertiestests.py", "Could not set initial value" + + self.prop.set("run-tests.py") + + assert self.prop.value == "run-tests.py", "Could not change value" + + try: + self.prop.set("unknown/file/on/disk.gif") + assert False, "An exception should be thrown on unknown file" + except FileConstraintError: + pass + +if __name__ == "__main__": + unittest.main() + diff --git a/src/sugar/tutorius/tests/uamtests.py b/src/sugar/tutorius/tests/uamtests.py new file mode 100644 index 0000000..b2a5901 --- /dev/null +++ b/src/sugar/tutorius/tests/uamtests.py @@ -0,0 +1,61 @@ +# Copyright (C) 2009, Tutorius.org +# Copyright (C) 2009, Michael Janelle-Montcalm <michael.jmontcalm@gmail.com> +# Copyright (C) 2009, Vincent Vinet <vince.vinet@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 + +import unittest + +from sugar.tutorius.uam import parse_uri, SchemeError + +PARSE_SUITE={ +#URI SCHEME HOST PARAMS PATH QUERY FRAGMENT +"tap://act.tut.org/": ["tap", "act.tut.org","", "/", "", ""], +"tap.gtk://a.t.o/0/1": ["tap.gtk","a.t.o","","/0/1","","",""], +"tap.gobject://a.t.o/Timer?timeout=5":["tap.gobject","a.t.o","","/Timer","timeout=5",""], +} + +class ParseUriTests(unittest.TestCase): + """Tests the UAM parsers""" + def test_parse_uri(self): + """Test parsing results""" + for uri, test in PARSE_SUITE.items(): + res = parse_uri(uri) + + assert res.scheme == test[0], "%s : Expected scheme %s, got %s" % (uri, test[0], res.scheme) + assert res.netloc == test[1], "%s : Expected netloc %s, got %s" % (uri, test[1], res.netloc) + assert res.params == test[2], "%s : Expected params %s, got %s" % (uri, test[2], res.params) + assert res.path == test[3], "%s : Expected path %s, got %s" % (uri, test[3], res.path) + assert res.query == test[4], "%s : Expected query %s, got %s" % (uri, test[4], res.query) + assert res.fragment == test[5], "%s : Expected fragment %s, got %s" % (uri, test[5], res.fragment) + + def test_errors(self): + """Test exceptions""" + try: + parse_uri("http://something.org/path") + assert False, "Parsing http should fail" + except SchemeError: + pass + + try: + parse_uri("tap.notarealsubscheme://something.org/path") + assert False, "Invalid Subscheme should fail" + except SchemeError: + pass + + +if __name__ == "__main__": + unittest.main() + |