Пример #1
0
 def __exit__(self, exc_type, exc_val, exc_tb):
     __tracebackhide__ = True
     try:
         if exc_type is None:
             method = getattr(qt_api.QtTest.QTest, self._method_name)
             r = method(self._widget, self._timeout)
             if not r:
                 msg = "widget {} not {} in {} ms.".format(
                     self._widget, self._adjective_name, self._timeout)
                 raise TimeoutError(msg)
     finally:
         self._widget = None
Пример #2
0
 def wait(self):
     """
     Waits until either the returned callback is called or timeout is
     reached.
     """
     __tracebackhide__ = True
     if self.called:
         return
     if self._timer is not None:
         self._timer.timeout.connect(self._quit_loop_by_timeout)
         self._timer.start()
     self._loop.exec_()
     if not self.called and self.raising:
         raise TimeoutError("Callback wasn't called after %sms." %
                            self.timeout)
Пример #3
0
    def wait(self):
        """
        Waits until either a connected signal is triggered or timeout is reached.

        :raise ValueError: if no signals are connected and timeout is None; in
            this case it would wait forever.
        """
        __tracebackhide__ = True
        if self.signal_triggered:
            return
        if self.timeout is None and not self._signals:
            raise ValueError("No signals or timeout specified.")
        if self._timer is not None:
            self._timer.timeout.connect(self._quit_loop_by_timeout)
            self._timer.start()

        if self.timeout != 0:
            self._loop.exec_()

        if not self.signal_triggered and self.raising:
            raise TimeoutError(self._timeout_message)
Пример #4
0
    def waitUntil(self, callback, timeout=1000):
        """
        .. versionadded:: 2.0

        Wait in a busy loop, calling the given callback periodically until timeout is reached.

        ``callback()`` should raise ``AssertionError`` to indicate that the desired condition
        has not yet been reached, or just return ``None`` when it does. Useful to ``assert`` until
        some condition is satisfied:

        .. code-block:: python

            def view_updated():
                assert view_model.count() > 10


            qtbot.waitUntil(view_updated)

        Another possibility is for ``callback()`` to return ``True`` when the desired condition
        is met, ``False`` otherwise. Useful specially with ``lambda`` for terser code, but keep
        in mind that the error message in those cases is usually not very useful because it is
        not using an ``assert`` expression.

        .. code-block:: python

            qtbot.waitUntil(lambda: view_model.count() > 10)

        Note that this usage only accepts returning actual ``True`` and ``False`` values,
        so returning an empty list to express "falseness" raises a ``ValueError``.

        :param callback: callable that will be called periodically.
        :param timeout: timeout value in ms.
        :raises ValueError: if the return value from the callback is anything other than ``None``,
            ``True`` or ``False``.

        .. note:: This method is also available as ``wait_until`` (pep-8 alias)
        """
        __tracebackhide__ = True
        import time

        start = time.time()

        def timed_out():
            elapsed = time.time() - start
            elapsed_ms = elapsed * 1000
            return elapsed_ms > timeout

        timeout_msg = f"waitUntil timed out in {timeout} milliseconds"

        while True:
            try:
                result = callback()
            except AssertionError as e:
                if timed_out():
                    raise TimeoutError(timeout_msg) from e
            else:
                if result not in (None, True, False):
                    msg = "waitUntil() callback must return None, True or False, returned %r"
                    raise ValueError(msg % result)

                # 'assert' form
                if result is None:
                    return

                # 'True/False' form
                if result:
                    return
                if timed_out():
                    raise TimeoutError(timeout_msg)
            self.wait(10)