Ejemplo n.º 1
0
    def test_pygame2_sdl_time_remove_timer(self):

        # __doc__ (as of 2010-01-06) for pygame2.sdl.time.remove_timer:

        # remove_timer (timerobj) -> None
        #
        # Removes a previously added timer callback.
        #
        # Removes a previously added timer callback and throws an
        # exception, if the passed object is not a matching timer
        # object.

        def _timercb(flag):
            flag.append(1)
            return 10

        self.assertRaises(pygame2.Error, sdltime.remove_timer, _timercb)
        sdltime.init()
        self.assertRaises(TypeError, sdltime.remove_timer, None)

        flag1 = []
        tobj = sdltime.add_timer(10, _timercb, (flag1,))
        self.assertTrue(tobj != None)
        t1 = t2 = sdltime.get_ticks()
        while t2 - t1 < 50:
            sdltime.delay(1)
            t2 = sdltime.get_ticks()
        sdltime.remove_timer(tobj)
        self.assertRaises(ValueError, sdltime.remove_timer, tobj)

        flag2 = []
        tobj = sdltime.add_timer(100, _timercb, (flag2,))
        sdltime.remove_timer(tobj)
        self.assertRaises(ValueError, sdltime.remove_timer, tobj)
        self.assertTrue(tobj != None)
        t1 = t2 = sdltime.get_ticks()
        while t2 - t1 < 50:
            sdltime.delay(1)
            t2 = sdltime.get_ticks()
        sdltime.quit()
        self.assertRaises(pygame2.Error, sdltime.remove_timer, _timercb)

        # This is important. If the test
        self.assertTrue(len(flag1) != 0)
        self.assertTrue(len(flag2) == 0)
Ejemplo n.º 2
0
    def test_pygame2_sdl_time_add_timer(self):

        # __doc__ (as of 2010-01-06) for pygame2.sdl.time.add_timer:

        # add_timer (interval, callable[, data]) -> CObject
        #
        # Adds a timer callback to be called periodically.
        #
        # Adds a timer callback to be called periodically using the
        # specified *interval*. *callable* can be any callable objet,
        # method or function. On invocation, the optional *data* will be
        # passed to the callable.
        #
        # This will return an CObject that acts as unique id for the
        # timer callback.
        setargs = []

        def _timercb(l, arg1, arg2):
            l.append(arg1)
            l.append(arg2)
            return 10

        self.assertRaises(pygame2.Error, sdltime.add_timer, _timercb)

        sdltime.init()
        tobj = sdltime.add_timer(10, _timercb, (setargs, "Hello", "World"))
        if tobj is None:
            sdltime.quit()
            self.fail()
        t1 = t2 = sdltime.get_ticks()
        while t2 - t1 < 50:
            sdltime.delay(1)
            t2 = sdltime.get_ticks()
        sdltime.quit()
        self.assertRaises(pygame2.Error, sdltime.add_timer, _timercb)

        # This is important - if we run the assertion test before quitting,
        # the time module won't be quitted and the timers will still exist,
        # causing thread issues in other tests.
        self.assertTrue(len(setargs) > 5)