From b108cc78b645f5a448f7274375215b80459bc9d2 Mon Sep 17 00:00:00 2001 From: Vincent Vinet Date: Thu, 26 Mar 2009 16:52:08 +0000 Subject: Add a bit of tests for filters --- (limited to 'src/sugar/tutorius/tests') diff --git a/src/sugar/tutorius/tests/coretests.py b/src/sugar/tutorius/tests/coretests.py index 9ac17a0..f9125ce 100644 --- a/src/sugar/tutorius/tests/coretests.py +++ b/src/sugar/tutorius/tests/coretests.py @@ -18,9 +18,12 @@ 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 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 +# +# 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/run-tests.py b/src/sugar/tutorius/tests/run-tests.py index 74b32fe..8c9c5e2 100755 --- a/src/sugar/tutorius/tests/run-tests.py +++ b/src/sugar/tutorius/tests/run-tests.py @@ -30,7 +30,8 @@ if __name__=='__main__': import linear_creatortests import actiontests import uamtests - + import filterstests + suite = unittest.TestSuite() suite.addTests(unittest.findTestCases(coretests)) suite.addTests(unittest.findTestCases(servicestests)) @@ -39,6 +40,7 @@ if __name__=='__main__': suite.addTests(unittest.findTestCases(linear_creatortests)) suite.addTests(unittest.findTestCases(actiontests)) suite.addTests(unittest.findTestCases(uamtests)) + suite.addTests(unittest.findTestCases(filterstests)) runner = unittest.TextTestRunner() runner.run(suite) @@ -54,5 +56,6 @@ if __name__=='__main__': from actiontests import * from linear_creatortests import * from uamtests import * + from filterstests import * unittest.main() -- cgit v0.9.1