UITest: introduce guarded context managers

They should be used wherever we need to make sure to execute
some action in tests regardless of the assertion success. They
follow their plain counterparts, and execute finalizing code
at all outcomes. This e.g. allows to assert when a dialog is
open, and have it properly closed on failure, so that the test
is not left in hung state.

Only two wrappers are implemented now: load_file and
execute_dialog_through_action.

sc/qa/uitest/chart/chartLegend.py is updated to use them.

Change-Id: Ib9cf44304f0d3ab8fa3377922ed36d2d866031b0
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116692
Tested-by: Jenkins
Reviewed-by: Mike Kaganski <mike.kaganski@collabora.com>
This commit is contained in:
Mike Kaganski
2021-06-06 17:38:15 +02:00
parent 3b75f9add7
commit 89aaa17a0a
2 changed files with 101 additions and 97 deletions
+70 -97
View File
@@ -11,124 +11,97 @@ from uitest.uihelper.calc import enter_text_to_cell
from libreoffice.calc.document import get_cell_by_position
from libreoffice.uno.propertyvalue import mkPropertyValues
from uitest.uihelper.common import get_state_as_dict, get_url_for_data_file, type_text
from uitest.uihelper import guarded
#Chart Display Legend dialog
class chartLegend(UITestCase):
def test_chart_display_legend_dialog(self):
calc_doc = self.ui_test.load_file(get_url_for_data_file("tdf98390.ods"))
xCalcDoc = self.xUITest.getTopFocusWindow()
gridwin = xCalcDoc.getChild("grid_window")
document = self.ui_test.get_component()
with guarded.load_file(self, get_url_for_data_file("tdf98390.ods")) as calc_doc:
xCalcDoc = self.xUITest.getTopFocusWindow()
gridwin = xCalcDoc.getChild("grid_window")
document = self.ui_test.get_component()
gridwin.executeAction("SELECT", mkPropertyValues({"OBJECT": "Object 1"}))
gridwin.executeAction("ACTIVATE", tuple())
xChartMainTop = self.xUITest.getTopFocusWindow()
xChartMain = xChartMainTop.getChild("chart_window")
xSeriesObj = xChartMain.getChild("CID/D=0:CS=0:CT=0:Series=0")
self.ui_test.execute_dialog_through_action(xSeriesObj, "COMMAND", mkPropertyValues({"COMMAND": "InsertMenuLegend"}))
xDialog = self.xUITest.getTopFocusWindow()
gridwin.executeAction("SELECT", mkPropertyValues({"OBJECT": "Object 1"}))
gridwin.executeAction("ACTIVATE", tuple())
xChartMainTop = self.xUITest.getTopFocusWindow()
xChartMain = xChartMainTop.getChild("chart_window")
xSeriesObj = xChartMain.getChild("CID/D=0:CS=0:CT=0:Series=0")
with guarded.execute_dialog_through_action(self, xSeriesObj, "COMMAND", mkPropertyValues({"COMMAND": "InsertMenuLegend"})) as xDialog:
left = xDialog.getChild("left")
right = xDialog.getChild("right")
top = xDialog.getChild("top")
bottom = xDialog.getChild("bottom")
left = xDialog.getChild("left")
right = xDialog.getChild("right")
top = xDialog.getChild("top")
bottom = xDialog.getChild("bottom")
left.executeAction("CLICK", tuple())
left.executeAction("CLICK", tuple())
#reopen and verify InsertMenuLegend dialog
gridwin.executeAction("SELECT", mkPropertyValues({"OBJECT": "Object 1"}))
gridwin.executeAction("ACTIVATE", tuple())
xChartMainTop = self.xUITest.getTopFocusWindow()
xChartMain = xChartMainTop.getChild("chart_window")
xSeriesObj = xChartMain.getChild("CID/D=0:CS=0:CT=0:Series=0")
with guarded.execute_dialog_through_action(self, xSeriesObj, "COMMAND", mkPropertyValues({"COMMAND": "InsertMenuLegend"})) as xDialog:
left = xDialog.getChild("left")
right = xDialog.getChild("right")
top = xDialog.getChild("top")
bottom = xDialog.getChild("bottom")
show = xDialog.getChild("show")
xOKBtn = xDialog.getChild("ok")
self.ui_test.close_dialog_through_button(xOKBtn)
self.assertEqual(get_state_as_dict(left)["Checked"], "true")
self.assertEqual(get_state_as_dict(right)["Checked"], "false")
self.assertEqual(get_state_as_dict(top)["Checked"], "false")
self.assertEqual(get_state_as_dict(bottom)["Checked"], "false")
#reopen and verify InsertMenuLegend dialog
gridwin.executeAction("SELECT", mkPropertyValues({"OBJECT": "Object 1"}))
gridwin.executeAction("ACTIVATE", tuple())
xChartMainTop = self.xUITest.getTopFocusWindow()
xChartMain = xChartMainTop.getChild("chart_window")
xSeriesObj = xChartMain.getChild("CID/D=0:CS=0:CT=0:Series=0")
self.ui_test.execute_dialog_through_action(xSeriesObj, "COMMAND", mkPropertyValues({"COMMAND": "InsertMenuLegend"}))
xDialog = self.xUITest.getTopFocusWindow()
show.executeAction("CLICK", tuple())
left = xDialog.getChild("left")
right = xDialog.getChild("right")
top = xDialog.getChild("top")
bottom = xDialog.getChild("bottom")
show = xDialog.getChild("show")
#reopen and verify InsertMenuLegend dialog
gridwin.executeAction("SELECT", mkPropertyValues({"OBJECT": "Object 1"}))
gridwin.executeAction("ACTIVATE", tuple())
xChartMainTop = self.xUITest.getTopFocusWindow()
xChartMain = xChartMainTop.getChild("chart_window")
xSeriesObj = xChartMain.getChild("CID/D=0:CS=0:CT=0:Series=0")
with guarded.execute_dialog_through_action(self, xSeriesObj, "COMMAND", mkPropertyValues({"COMMAND": "InsertMenuLegend"})) as xDialog:
left = xDialog.getChild("left")
right = xDialog.getChild("right")
top = xDialog.getChild("top")
bottom = xDialog.getChild("bottom")
show = xDialog.getChild("show")
self.assertEqual(get_state_as_dict(left)["Checked"], "true")
self.assertEqual(get_state_as_dict(right)["Checked"], "false")
self.assertEqual(get_state_as_dict(top)["Checked"], "false")
self.assertEqual(get_state_as_dict(bottom)["Checked"], "false")
self.assertEqual(get_state_as_dict(left)["Checked"], "true")
self.assertEqual(get_state_as_dict(right)["Checked"], "false")
self.assertEqual(get_state_as_dict(top)["Checked"], "false")
self.assertEqual(get_state_as_dict(bottom)["Checked"], "false")
show.executeAction("CLICK", tuple())
xOKBtn = xDialog.getChild("ok")
self.ui_test.close_dialog_through_button(xOKBtn)
#reopen and verify InsertMenuLegend dialog
gridwin.executeAction("SELECT", mkPropertyValues({"OBJECT": "Object 1"}))
gridwin.executeAction("ACTIVATE", tuple())
xChartMainTop = self.xUITest.getTopFocusWindow()
xChartMain = xChartMainTop.getChild("chart_window")
xSeriesObj = xChartMain.getChild("CID/D=0:CS=0:CT=0:Series=0")
self.ui_test.execute_dialog_through_action(xSeriesObj, "COMMAND", mkPropertyValues({"COMMAND": "InsertMenuLegend"}))
xDialog = self.xUITest.getTopFocusWindow()
left = xDialog.getChild("left")
right = xDialog.getChild("right")
top = xDialog.getChild("top")
bottom = xDialog.getChild("bottom")
show = xDialog.getChild("show")
self.assertEqual(get_state_as_dict(left)["Checked"], "true")
self.assertEqual(get_state_as_dict(right)["Checked"], "false")
self.assertEqual(get_state_as_dict(top)["Checked"], "false")
self.assertEqual(get_state_as_dict(bottom)["Checked"], "false")
self.assertEqual(get_state_as_dict(show)["Selected"], "false")
xOKBtn = xDialog.getChild("ok")
self.ui_test.close_dialog_through_button(xOKBtn)
self.ui_test.close_doc()
self.assertEqual(get_state_as_dict(show)["Selected"], "false")
def test_legends_move_with_arrows_keys(self):
calc_doc = self.ui_test.load_file(get_url_for_data_file("dataLabels.ods"))
xCalcDoc = self.xUITest.getTopFocusWindow()
gridwin = xCalcDoc.getChild("grid_window")
with guarded.load_file(self, get_url_for_data_file("dataLabels.ods")) as calc_doc:
xCalcDoc = self.xUITest.getTopFocusWindow()
gridwin = xCalcDoc.getChild("grid_window")
change_measurement_unit(self, "Centimeter")
change_measurement_unit(self, "Centimeter")
gridwin.executeAction("SELECT", mkPropertyValues({"OBJECT": "Object 1"}))
gridwin.executeAction("ACTIVATE", tuple())
xChartMainTop = self.xUITest.getTopFocusWindow()
xChartMain = xChartMainTop.getChild("chart_window")
gridwin.executeAction("SELECT", mkPropertyValues({"OBJECT": "Object 1"}))
gridwin.executeAction("ACTIVATE", tuple())
xChartMainTop = self.xUITest.getTopFocusWindow()
xChartMain = xChartMainTop.getChild("chart_window")
# Select the legends
xLegends = xChartMain.getChild("CID/D=0:Legend=")
xLegends.executeAction("SELECT", tuple())
# Select the legends
xLegends = xChartMain.getChild("CID/D=0:Legend=")
xLegends.executeAction("SELECT", tuple())
self.ui_test.execute_dialog_through_action(xLegends, "COMMAND", mkPropertyValues({"COMMAND": "TransformDialog"}))
with guarded.execute_dialog_through_action(self, xLegends, "COMMAND", mkPropertyValues({"COMMAND": "TransformDialog"})) as xDialog:
self.assertEqual("4.61", get_state_as_dict(xDialog.getChild("MTR_FLD_POS_X"))['Value'])
self.assertEqual("1.54", get_state_as_dict(xDialog.getChild("MTR_FLD_POS_Y"))['Value'])
xDialog = self.xUITest.getTopFocusWindow()
self.assertEqual("4.61", get_state_as_dict(xDialog.getChild("MTR_FLD_POS_X"))['Value'])
self.assertEqual("1.54", get_state_as_dict(xDialog.getChild("MTR_FLD_POS_Y"))['Value'])
xChartMain.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"}))
xChartMain.executeAction("TYPE", mkPropertyValues({"KEYCODE": "LEFT"}))
xOkBtn = xDialog.getChild("ok")
xOkBtn.executeAction("CLICK", tuple())
xChartMain.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"}))
xChartMain.executeAction("TYPE", mkPropertyValues({"KEYCODE": "LEFT"}))
self.ui_test.execute_dialog_through_action(xLegends, "COMMAND", mkPropertyValues({"COMMAND": "TransformDialog"}))
# Check the position has changed after moving the label using the arrows keys
xDialog = self.xUITest.getTopFocusWindow()
self.assertEqual("4.51", get_state_as_dict(xDialog.getChild("MTR_FLD_POS_X"))['Value'])
self.assertEqual("1.44", get_state_as_dict(xDialog.getChild("MTR_FLD_POS_Y"))['Value'])
xOkBtn = xDialog.getChild("ok")
xOkBtn.executeAction("CLICK", tuple())
self.ui_test.close_doc()
# Check the position has changed after moving the label using the arrows keys
with guarded.execute_dialog_through_action(self, xLegends, "COMMAND", mkPropertyValues({"COMMAND": "TransformDialog"})) as xDialog:
self.assertEqual("4.51", get_state_as_dict(xDialog.getChild("MTR_FLD_POS_X"))['Value'])
self.assertEqual("1.44", get_state_as_dict(xDialog.getChild("MTR_FLD_POS_Y"))['Value'])
# vim: set shiftwidth=4 softtabstop=4 expandtab:
+31
View File
@@ -0,0 +1,31 @@
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
from contextlib import contextmanager
# Calls UITest.close_doc at exit
@contextmanager
def load_file(testCase, url):
component = testCase.ui_test.load_file(url)
try:
yield component
finally:
testCase.ui_test.close_doc()
# Calls UITest.close_dialog_through_button at exit
@contextmanager
def execute_dialog_through_action(testCase, ui_object, action, parameters = None, event_name = "DialogExecute", close_button = "ok"):
testCase.ui_test.execute_dialog_through_action(ui_object, action, parameters, event_name)
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: