def _QTest_qWaitForWindowActive(widget, timeout=1000): # A Qt5 compatible (probably) QTest.qWaitForWindowActive(QWidget, int) # (mostly copied from qtestsystem.h in qt5/qtbase) from AnyQt.QtCore import \ Qt, QCoreApplication, QEventLoop, QElapsedTimer, QEvent window = widget.window() timer = QElapsedTimer() timer.start() while not window.isActiveWindow(): remaining = timeout - timer.elapsed() if remaining <= 0: break QCoreApplication.processEvents(QEventLoop.AllEvents, remaining) QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete) QTest.qSleep(10) # See the explanation in qtestsystem.h if window.isActiveWindow(): wait_no = 0 while window.pos().isNull(): if wait_no > timeout // 10: break wait_no += 1 QTest.qWait(10) return window.isActiveWindow()
def test_methodinvoke(self): executor = ThreadExecutor() state = [None, None] class StateSetter(QObject): @Slot(object) def set_state(self, value): state[0] = value state[1] = QThread.currentThread() def func(callback): callback(QThread.currentThread()) obj = StateSetter() f1 = executor.submit(func, methodinvoke(obj, "set_state", (object,))) f1.result() # So invoked method can be called QCoreApplication.processEvents() self.assertIs(state[1], QThread.currentThread(), "set_state was called from the wrong thread") self.assertIsNot(state[0], QThread.currentThread(), "set_state was invoked in the main thread") executor.shutdown(wait=True)
def test_methodinvoke(self): executor = ThreadExecutor() state = [None, None] class StateSetter(QObject): @Slot(object) def set_state(self, value): state[0] = value state[1] = QThread.currentThread() def func(callback): callback(QThread.currentThread()) obj = StateSetter() f1 = executor.submit(func, methodinvoke(obj, "set_state", (object, ))) f1.result() # So invoked method can be called QCoreApplication.processEvents() self.assertIs(state[1], QThread.currentThread(), "set_state was called from the wrong thread") self.assertIsNot(state[0], QThread.currentThread(), "set_state was invoked in the main thread") executor.shutdown(wait=True)
class CoreAppTestCase(unittest.TestCase): def setUp(self): self.app = QCoreApplication.instance() if self.app is None: self.app = QCoreApplication([]) def tearDown(self): self.app.processEvents() del self.app
class TestTask(unittest.TestCase): def setUp(self): self.app = QCoreApplication([]) def test_task(self): results = [] task = Task(function=QThread.currentThread) task.resultReady.connect(results.append) task.start() self.app.processEvents() self.assertSequenceEqual(results, [QThread.currentThread()]) results = [] thread = QThread() thread.start() task = Task(function=QThread.currentThread) task.moveToThread(thread) self.assertIsNot(task.thread(), QThread.currentThread()) self.assertIs(task.thread(), thread) task.resultReady.connect(results.append, Qt.DirectConnection) task.start() f = task.future() self.assertIsNot(f.result(3), QThread.currentThread()) self.assertIs(f.result(3), results[-1]) def test_executor(self): executor = ThreadExecutor() f = executor.submit(QThread.currentThread) self.assertIsNot(f.result(3), QThread.currentThread()) f = executor.submit(lambda: 1 / 0) with self.assertRaises(ZeroDivisionError): f.result() results = [] task = Task(function=QThread.currentThread) task.resultReady.connect(results.append, Qt.DirectConnection) f = executor.submit(task) self.assertIsNot(f.result(3), QThread.currentThread()) executor.shutdown()
class CoreAppTestCase(unittest.TestCase): def setUp(self): self.app = QCoreApplication.instance() if self.app is None: self.app = QCoreApplication([]) def tearDown(self): self.app.processEvents() del self.app
class TestTask(unittest.TestCase): def setUp(self): self.app = QCoreApplication([]) def test_task(self): results = [] task = Task(function=QThread.currentThread) task.resultReady.connect(results.append) task.start() self.app.processEvents() self.assertSequenceEqual(results, [QThread.currentThread()]) results = [] thread = QThread() thread.start() task = Task(function=QThread.currentThread) task.moveToThread(thread) self.assertIsNot(task.thread(), QThread.currentThread()) self.assertIs(task.thread(), thread) task.resultReady.connect(results.append, Qt.DirectConnection) task.start() f = task.future() self.assertIsNot(f.result(3), QThread.currentThread()) self.assertIs(f.result(3), results[-1]) def test_executor(self): executor = ThreadExecutor() f = executor.submit(QThread.currentThread) self.assertIsNot(f.result(3), QThread.currentThread()) f = executor.submit(lambda: 1 / 0) with self.assertRaises(ZeroDivisionError): f.result() results = [] task = Task(function=QThread.currentThread) task.resultReady.connect(results.append, Qt.DirectConnection) f = executor.submit_task(task) self.assertIsNot(f.result(3), QThread.currentThread()) executor.shutdown()
def _QTest_qWait(timeout): from AnyQt.QtCore import Qt, QCoreApplication, QEvent, QEventLoop, QDeadlineTimer remaining = timeout deadline = QDeadlineTimer(remaining, Qt.PreciseTimer) while True: QCoreApplication.processEvents(QEventLoop.AllEvents, remaining) QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete) remaining = deadline.remainingTime() if remaining <= 0: break QTest.qSleep(min(10, remaining)) remaining = deadline.remainingTime() if remaining <= 0: break
def _QTest_qWaitFor(predicate, timeout=5000): # type: (Callable[[], bool], int) -> bool # Copied and adapted from Qt from AnyQt.QtCore import Qt, QCoreApplication, QEvent, QEventLoop, QDeadlineTimer if predicate(): return True deadline = QDeadlineTimer(Qt.PreciseTimer) deadline.setRemainingTime(timeout) while True: QCoreApplication.processEvents(QEventLoop.AllEvents) QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete) if predicate(): return True remaining = deadline.remainingTime() if remaining > 0: QTest.qSleep(min(10, remaining)) remaining = deadline.remainingTime() if remaining <= 0: break return predicate() # Last chance
def _QTest_qWaitForWindowExposed(widget, timeout=1000): # A Qt5 compatible (probably) QTest.qWaitForWindowExposed(QWidget, int) # (mostly copied from qtestsystem.h in qt5/qtbase) from AnyQt.QtCore import \ Qt, QCoreApplication, QEventLoop, QElapsedTimer, QEvent window = widget.window() timer = QElapsedTimer() timer.start() # Is widget.testAttribute(Qt.WA_Mapped) a suitable replacement for # QWindow.isExposed in Qt5?? # Not exactly. In Qt5 # window().testAttribute(Qt.WA_Mapped) == window().windowHandle.isExposed() # but both are False if a window is fully obscured by other windows, # in Qt4 there is no difference if a window is obscured. while not window.testAttribute(Qt.WA_Mapped): remaining = timeout - timer.elapsed() if remaining <= 0: break QCoreApplication.processEvents(QEventLoop.AllEvents, remaining) QCoreApplication.sendPostedEvents(None, QEvent.DeferredDelete) QTest.qSleep(10) return window.testAttribute(Qt.WA_Mapped)