# 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 from sugar.tutorius import addon 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 = addon.create('TimerEvent', "Next", 2) # 2 seconds 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 = addon.create('TimerEvent', "Next", 2) # 2 seconds 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 = addon.create('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 = addon.create('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"