def setUp(self, *args, **kwargs):
        """
        Adds Qt modules to tank.platform.qt and initializes QApplication
        """
        from tank.authentication.ui.qt_abstraction import QtGui

        # See if a QApplication instance exists, and if not create one.  Use the
        # QApplication.instance() method, since qApp can contain a non-None
        # value even if no QApplication has been constructed on PySide2.
        if not QtGui.QApplication.instance():
            self._app = QtGui.QApplication(sys.argv)
        super(InteractiveTests, self).setUp()
    def _print_message(self, text, test_console):
        if test_console:
            print()
            print("=" * len(text))
            print(text)
            print("=" * len(text))
        else:
            from tank.authentication.ui.qt_abstraction import QtGui

            mb = QtGui.QMessageBox()
            mb.setText(text)
            mb.exec_()
    def setUp(self):
        """
        We need a QApplication to run these tests.
        """
        super(TestDialogCreation, self).setUp()

        # Engine is not started yet, so can't rely on sgtk.platform.qt for imports.
        from tank.authentication.ui.qt_abstraction import QtGui

        if QtGui.QApplication.instance() is None:
            QtGui.QApplication([])

        sgtk.platform.start_engine("test_engine", self.tk, self.context)
    def setUp(self):
        """
        Starts up an engine and makes sure Qt is ready to be used.
        """
        super(TestExecuteInMainThread, self).setUp()

        # Engine is not started yet, so can't rely on sgtk.platform.qt for imports.
        from tank.authentication.ui.qt_abstraction import QtGui

        # See if a QApplication instance exists, and if not create one.  Use the
        # QApplication.instance() method, since qApp can contain a non-None
        # value even if no QApplication has been constructed on PySide2.
        if not QtGui.QApplication.instance():
            self._app = QtGui.QApplication(sys.argv)
        else:
            self._app = QtGui.QApplication.instance()

        tank.platform.start_engine("test_engine", self.tk, self.context)
Beispiel #5
0
    def setUp(self):
        """
        Prepares the engine and makes sure Qt is ready.
        """
        super(TestShowDialog, self).setUp()
        self.setup_fixtures()

        self.engine = sgtk.platform.start_engine("test_engine", self.tk,
                                                 self.context)
        # Create an application instance so we can take control of the execution
        # of the dialog.
        from sgtk.platform.qt import QtGui

        if QtGui.QApplication.instance() is None:
            self._app = QtGui.QApplication(sys.argv)
        else:
            self._app = QtGui.QApplication.instance()

        self._dialog_dimissed = False
    def setUp(self):
        """
        Prepares the engine and makes sure Qt is ready.
        """
        super(TestShowDialog, self).setUp()
        self.setup_fixtures()

        self.engine = sgtk.platform.start_engine("test_engine", self.tk,
                                                 self.context)

        # Engine is not started yet, so can't rely on sgtk.platform.qt for imports.
        from tank.authentication.ui.qt_abstraction import QtGui

        # Create an application instance so we can take control of the execution
        # of the dialog.
        if QtGui.QApplication.instance() is None:
            self._app = QtGui.QApplication(sys.argv)
        else:
            self._app = QtGui.QApplication.instance()

        self._dialog_dimissed = False
    def test_invoker_rethrows_exception(self):
        """
        Makes sure that the invoker will carry the exception back to the calling thread.
        This test is a bit convoluted but it's written in a way to make sure that the test fails
        in the main thread.

        From the background thread, we will create an invoker and use it to invoke the thrower
        method in the main thread. This thrower method will throw a FromMainThreadException.
        If everything works as planned, the exception will be caught by the invoker and rethrown
        in the background thread. The background thread will then raise an exception and when the
        main thread calls wait it will assert that the exception that was thrown was coming
        from the thrower function.
        """
        class FromMainThreadException(Exception):
            """
            Exception that will be thrown from the main thead.
            """

            pass

        from tank.authentication.ui.qt_abstraction import QtCore, QtGui

        # Create a QApplication instance.
        if not QtGui.QApplication.instance():
            QtGui.QApplication(sys.argv)

        def thrower():
            """
            Method that will throw.
            :throws: FromMainThreadException
            """
            if QtGui.QApplication.instance().thread(
            ) != QtCore.QThread.currentThread():
                raise Exception(
                    "This should have been invoked in the main thread.")
            raise FromMainThreadException()

        class BackgroundThread(QtCore.QThread):
            """
            Thread that will invoke a method that will throw from the invoked thread.
            """
            def __init__(self):
                """
                Constructor.
                """
                QtCore.QThread.__init__(self)
                self._exception = Exception("No exception was caught!")

            def run(self):
                """
                Calls the thrower method using the invoker and catches an exception if one is
                thrown.
                """
                try:
                    invoker_obj = invoker.create()
                    # Make sure we have a QObject derived object and not a regular Python function.
                    if not isinstance(invoker_obj, QtCore.QObject):
                        raise Exception("Invoker is not a QObject")
                    if invoker_obj.thread() != QtGui.QApplication.instance(
                    ).thread():
                        raise Exception(
                            "Invoker should be of the same thread as the QApplication."
                        )
                    if QtCore.QThread.currentThread() != self:
                        raise Exception("Current thread not self.")
                    if QtGui.QApplication.instance().thread == self:
                        raise Exception(
                            "QApplication should be in the main thread, not self."
                        )
                    invoker_obj(thrower)
                except Exception as e:
                    self._exception = e
                finally:
                    QtGui.QApplication.instance().exit()

            def wait(self):
                """
                Waits for the thread to complete and rethrows the exception that was caught in the
                thread.
                """
                QtCore.QThread.wait(self)
                if self._exception:
                    raise self._exception

        # Launch a background thread
        bg = BackgroundThread()
        bg.start()
        # process events
        QtGui.QApplication.instance().exec_()

        # Make sure the thread got the exception that was thrown from the main thread.
        with self.assertRaises(FromMainThreadException):
            bg.wait()