def assertTraitChangesInEventLoop(self, obj, trait, condition, count=1, timeout=10.0): """Runs the real Qt event loop, collecting trait change events until the provided condition evaluates to True. Parameters ---------- obj : HasTraits The HasTraits instance whose trait will change. trait : str The extended trait name of trait changes to listen too. condition : callable A callable to determine if the stop criteria have been met. This should accept no arguments. count : int The expected number of times the event should be fired. The default is to expect one event. timeout : float Number of seconds to run the event loop in the case that the trait change does not occur. """ condition_ = lambda: condition(obj) collector = TraitsChangeCollector(obj=obj, trait=trait) collector.start_collecting() try: try: yield collector self.event_loop_helper.event_loop_until_condition( condition_, timeout=timeout) except ConditionTimeoutError: actual_event_count = collector.event_count msg = ("Expected {} event on {} to be fired at least {} " "times, but the event was only fired {} times " "before timeout ({} seconds).") msg = msg.format(trait, obj, count, actual_event_count, timeout) self.fail(msg) finally: collector.stop_collecting()
def assertTraitChangesInEventLoop(self, obj, trait, condition, count=1, timeout=10.0): """Runs the real Qt event loop, collecting trait change events until the provided condition evaluates to True. Parameters ---------- obj : HasTraits The HasTraits instance whose trait will change. trait : str The extended trait name of trait changes to listen too. condition : callable A callable to determine if the stop criteria have been met. This should accept no arguments. count : int The expected number of times the event should be fired. The default is to expect one event. timeout : float Number of seconds to run the event loop in the case that the trait change does not occur. """ condition_ = lambda: condition(obj) collector = TraitsChangeCollector(obj=obj, trait=trait) collector.start_collecting() try: try: yield collector self.event_loop_helper.event_loop_until_condition( condition_, timeout=timeout) except ConditionTimeoutError: actual_event_count = collector.event_count msg = ("Expected {} event on {} to be fired at least {} " "times, but the event was only fired {} times " "before timeout ({} seconds).") msg = msg.format( trait, obj, count, actual_event_count, timeout) self.fail(msg) finally: collector.stop_collecting()