1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 from libreoffice
.uno
.propertyvalue
import mkPropertyValues
12 from uitest
.framework
import UITestCase
13 from uitest
.uihelper
.common
import type_text
, select_pos
14 from uitest
.uihelper
.common
import get_state_as_dict
15 from libreoffice
.calc
.document
import get_cell_by_position
16 from uitest
.uihelper
.calc
import enter_text_to_cell
18 class CreateRangeNameTest(UITestCase
):
20 def test_create_range_name(self
):
22 with self
.ui_test
.create_doc_in_start_center("calc"):
24 calcDoc
= self
.xUITest
.getTopFocusWindow()
25 xPosWindow
= calcDoc
.getChild('pos_window')
26 self
.assertEqual('A1', get_state_as_dict(xPosWindow
)['Text'])
28 with self
.ui_test
.execute_modeless_dialog_through_command(".uno:AddName", close_button
="add") as xAddNameDlg
:
31 xEdit
= xAddNameDlg
.getChild("edit")
32 type_text(xEdit
, "globalRangeName")
35 self
.assertEqual('globalRangeName', get_state_as_dict(xPosWindow
)['Text'])
38 def test_create_range_name_from_ui(self
):
40 with self
.ui_test
.create_doc_in_start_center("calc") as document
:
42 calcDoc
= self
.xUITest
.getTopFocusWindow()
43 gridwin
= calcDoc
.getChild("grid_window")
45 enter_text_to_cell(gridwin
, "A1", "1")
46 enter_text_to_cell(gridwin
, "B1", "1")
47 enter_text_to_cell(gridwin
, "C1", "1")
49 gridwin
.executeAction("SELECT", mkPropertyValues({"RANGE": "A1:C1"}))
50 xPosWindow
= calcDoc
.getChild('pos_window')
51 self
.assertEqual('A1:C1', get_state_as_dict(xPosWindow
)['Text'])
53 xPosWindow
.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
54 xPosWindow
.executeAction("TYPE", mkPropertyValues({"KEYCODE":"BACKSPACE"}))
55 xPosWindow
.executeAction("TYPE", mkPropertyValues({"TEXT":"RANGE1"}))
56 xPosWindow
.executeAction("TYPE", mkPropertyValues({"KEYCODE":"RETURN"}))
58 self
.assertEqual('RANGE1', get_state_as_dict(xPosWindow
)['Text'])
60 calcDoc
= self
.xUITest
.getTopFocusWindow()
61 gridwin
= calcDoc
.getChild("grid_window")
63 enter_text_to_cell(gridwin
, "A2", "=SUM(RANGE1)")
64 self
.assertEqual(3.0, get_cell_by_position(document
, 0, 0, 1).getValue())
67 with self
.ui_test
.execute_dialog_through_command(".uno:DefineName") as xDialog
:
68 xNamesList
= xDialog
.getChild('names')
69 self
.assertEqual(1, len(xNamesList
.getChildren()))
71 xName
= xDialog
.getChild('name')
72 self
.assertEqual( 'RANGE1', get_state_as_dict(xName
)["Text"])
74 xName
.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
75 xName
.executeAction("TYPE", mkPropertyValues({"KEYCODE":"BACKSPACE"}))
76 xName
.executeAction("TYPE", mkPropertyValues({"TEXT":"RANGE2"}))
79 # tdf#87474 check the formula is updated after changing the name
80 self
.assertEqual("=SUM(RANGE2)", get_cell_by_position(document
, 0, 0, 1).getFormula())
82 self
.xUITest
.executeCommand(".uno:Undo")
84 self
.assertEqual("=SUM(RANGE1)", get_cell_by_position(document
, 0, 0, 1).getFormula())
87 def test_create_local_range_name(self
):
89 with self
.ui_test
.create_doc_in_start_center("calc") as document
:
91 calcDoc
= self
.xUITest
.getTopFocusWindow()
92 xPosWindow
= calcDoc
.getChild('pos_window')
93 self
.assertEqual('A1', get_state_as_dict(xPosWindow
)['Text'])
95 with self
.ui_test
.execute_modeless_dialog_through_command(".uno:AddName", close_button
="add") as xAddNameDlg
:
98 xEdit
= xAddNameDlg
.getChild("edit")
99 type_text(xEdit
, "localRangeName")
101 xScope
= xAddNameDlg
.getChild("scope")
102 select_pos(xScope
, "1")
105 # tdf#67007: Without the fix in place, this test would have failed with
106 # AssertionError: 'localRangeName' != 'A1'
107 # Additionally, newly check a sheet-local scoped name has " (sheetname)" appended.
108 self
.assertEqual('localRangeName (Sheet1)', get_state_as_dict(xPosWindow
)['Text'])
110 gridwin
= calcDoc
.getChild("grid_window")
111 enter_text_to_cell(gridwin
, "A1", "1")
113 # Use the name range in the current sheet
114 gridwin
.executeAction("SELECT", mkPropertyValues({"CELL": "B1"}))
116 with self
.ui_test
.execute_dialog_through_command(".uno:InsertName", close_button
="paste") as xDialog
:
118 xCtrl
= xDialog
.getChild('ctrl')
119 self
.assertEqual(1, len(xCtrl
.getChildren()))
120 self
.assertEqual("localRangeName\t$Sheet1.$A$1\tSheet1", get_state_as_dict(xCtrl
.getChild('0'))['Text'])
121 xCtrl
.getChild('0').executeAction("SELECT", tuple())
123 # use return key to paste the name range
124 gridwin
.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"}))
126 self
.assertEqual("1", get_cell_by_position(document
, 0, 1, 0).getString())
127 self
.assertEqual("=localRangeName", get_cell_by_position(document
, 0, 1, 0).getFormula())
130 with self
.ui_test
.execute_dialog_through_command(".uno:Insert"):
133 # Use the name range in the new sheet
134 gridwin
.executeAction("SELECT", mkPropertyValues({"CELL": "B1"}))
136 with self
.ui_test
.execute_dialog_through_command(".uno:InsertName", close_button
="paste") as xDialog
:
138 xCtrl
= xDialog
.getChild('ctrl')
139 self
.assertEqual(1, len(xCtrl
.getChildren()))
140 self
.assertEqual("localRangeName\t$Sheet1.$A$1\tSheet1", get_state_as_dict(xCtrl
.getChild('0'))['Text'])
141 xCtrl
.getChild('0').executeAction("SELECT", tuple())
143 # use return key to paste the name range
144 gridwin
.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"}))
146 # tdf#137896: Without the fix in place, this test would have failed with
147 # AssertionError: '1' != '#NAME?'
148 self
.assertEqual("1", get_cell_by_position(document
, 0, 1, 0).getString())
150 # and AssertionError: '=Sheet1.localRangeName' != '=localrangename'
151 self
.assertEqual("=Sheet1.localRangeName", get_cell_by_position(document
, 0, 1, 0).getFormula())
153 with self
.ui_test
.execute_dialog_through_command(".uno:DefineName") as xDialog
:
155 # tdf#138851: Without the fix in place, this test would have failed with
156 # AssertionError: 'Sheet1' != 'Document (Global)'
157 xScope
= xDialog
.getChild("scope")
158 self
.assertEqual("Sheet1", get_state_as_dict(xScope
)['SelectEntryText'])
162 # vim: set shiftwidth=4 softtabstop=4 expandtab: