sw/qa/uitest: close the dialog if thrown an exception

if a unit test opens a dialog and exception
is thrown if does not exist a control, the
terminate() method fails, the unit test will
wait indefinitely for the subprocess to terminate.

Change-Id: I6dc77b2db8ce042ead78d13ce57e91892cd2db90
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113896
Tested-by: Jenkins
Reviewed-by: Henry Castro <hcastro@collabora.com>
diff --git a/sw/qa/uitest/writer_tests2/fontworks.py b/sw/qa/uitest/writer_tests2/fontworks.py
index 4d4044a..d9ef76c 100644
--- a/sw/qa/uitest/writer_tests2/fontworks.py
+++ b/sw/qa/uitest/writer_tests2/fontworks.py
@@ -10,6 +10,7 @@
from uitest.framework import UITestCase
from uitest.uihelper.common import get_state_as_dict
from libreoffice.uno.propertyvalue import mkPropertyValues
from uitest.uihelper import guarded
#test FontWorks dialog
class fontWorksDialog(UITestCase):

@@ -18,26 +19,21 @@ class fontWorksDialog(UITestCase):
        xWriterDoc = self.xUITest.getTopFocusWindow()
        xWriterEdit = xWriterDoc.getChild("writer_edit")

        self.ui_test.execute_dialog_through_command(".uno:FontworkGalleryFloater")
        xDialog = self.xUITest.getTopFocusWindow()
        with guarded.execute_dialog_through_command(self, ".uno:FontworkGalleryFloater", close_button="cancel") as xDialog:
            FontWorkSelector = xDialog.getChild("ctlFavoriteswin")
            # Select element with id (3)
            element3 = FontWorkSelector.getChild("2")
            element3.executeAction("SELECT", mkPropertyValues({}))
            print(get_state_as_dict(FontWorkSelector))
            self.assertEqual(get_state_as_dict(FontWorkSelector)["SelectedItemPos"], "2")
            self.assertEqual(get_state_as_dict(FontWorkSelector)["SelectedItemId"], "3")
            self.assertEqual(get_state_as_dict(FontWorkSelector)["VisibleCount"], "36")

        FontWorkSelector = xDialog.getChild("ctlFavoriteswin")
        # Select element with id (3)
        element3 = FontWorkSelector.getChild("2")
        element3.executeAction("SELECT", mkPropertyValues({}))
        print(get_state_as_dict(FontWorkSelector))
        self.assertEqual(get_state_as_dict(FontWorkSelector)["SelectedItemPos"], "2")
        self.assertEqual(get_state_as_dict(FontWorkSelector)["SelectedItemId"], "3")
        self.assertEqual(get_state_as_dict(FontWorkSelector)["VisibleCount"], "36")

        # Select element with id (7)
        element7 = FontWorkSelector.getChild("6")
        element7.executeAction("SELECT", mkPropertyValues({}))
        self.assertEqual(get_state_as_dict(FontWorkSelector)["SelectedItemPos"], "6")
        self.assertEqual(get_state_as_dict(FontWorkSelector)["SelectedItemId"], "7")

        xCloseBtn = xDialog.getChild("cancel")
        self.ui_test.close_dialog_through_button(xCloseBtn)
            # Select element with id (7)
            element7 = FontWorkSelector.getChild("6")
            element7.executeAction("SELECT", mkPropertyValues({}))
            self.assertEqual(get_state_as_dict(FontWorkSelector)["SelectedItemPos"], "6")
            self.assertEqual(get_state_as_dict(FontWorkSelector)["SelectedItemId"], "7")

        self.ui_test.close_doc()

diff --git a/uitest/uitest/uihelper/guarded.py b/uitest/uitest/uihelper/guarded.py
index b7e5ce0..871fa69 100644
--- a/uitest/uitest/uihelper/guarded.py
+++ b/uitest/uitest/uihelper/guarded.py
@@ -28,4 +28,14 @@ def execute_dialog_through_action(testCase, ui_object, action, parameters = None
    finally:
        testCase.ui_test.close_dialog_through_button(xDialog.getChild(close_button))

# Calls UITest.close_dialog_through_button at exit
@contextmanager
def execute_dialog_through_command(testCase, unoCommand, close_button = "ok"):
    testCase.ui_test.execute_dialog_through_command(unoCommand)
    xDialog = testCase.xUITest.getTopFocusWindow()
    try:
        yield xDialog
    finally:
        testCase.ui_test.close_dialog_through_button(xDialog.getChild(close_button))

# vim: set shiftwidth=4 softtabstop=4 expandtab: