Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / qa / uitest / sort / sorting.py
blobb78eae4c77f00d290a40cc26462fdc13e34bdc72
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/.
9 from uitest.framework import UITestCase
10 from uitest.uihelper.calc import enter_text_to_cell
11 from uitest.uihelper.common import get_state_as_dict, select_pos
13 from libreoffice.calc.document import get_cell_by_position
14 from libreoffice.uno.propertyvalue import mkPropertyValues
17 # Testcases Sorting TCS_Sorting
18 class CalcSorting(UITestCase):
20 def test_Sortingbuttons_detect_columnheaders(self):
21 with self.ui_test.create_doc_in_start_center("calc") as document:
22 xCalcDoc = self.xUITest.getTopFocusWindow()
23 gridwin = xCalcDoc.getChild("grid_window")
24 #In column A enter: Number; 3; 4; 6; 2 / In column B enter: Misc; s; d; f; g
25 enter_text_to_cell(gridwin, "A1", "Number")
26 enter_text_to_cell(gridwin, "A2", "3")
27 enter_text_to_cell(gridwin, "A3", "4")
28 enter_text_to_cell(gridwin, "A4", "6")
29 enter_text_to_cell(gridwin, "A5", "2")
30 enter_text_to_cell(gridwin, "B1", "Misc")
31 enter_text_to_cell(gridwin, "B2", "s")
32 enter_text_to_cell(gridwin, "B3", "d")
33 enter_text_to_cell(gridwin, "B4", "f")
34 enter_text_to_cell(gridwin, "B5", "g")
35 #Select cell A3
36 gridwin.executeAction("SELECT", mkPropertyValues({"CELL": "A3"}))
37 #Press toolbarbutton for descending sorting .uno:SortDescending
38 self.xUITest.executeCommand(".uno:SortDescending")
39 #Verify that cell A1 still contains "Number" and B1 "Misc"
40 self.assertEqual(get_cell_by_position(document, 0, 0, 0).getString(), "Number")
41 self.assertEqual(get_cell_by_position(document, 0, 1, 0).getString(), "Misc")
42 #UNDO
43 self.xUITest.executeCommand(".uno:Undo")
44 #Select cell B3
45 gridwin.executeAction("SELECT", mkPropertyValues({"CELL": "B3"}))
46 #Press toolbar button for ascending sorting
47 self.xUITest.executeCommand(".uno:SortAscending")
48 #Verify that cell A1 still contains "Number" and B1 "Misc"
49 self.assertEqual(get_cell_by_position(document, 0, 0, 0).getString(), "Number")
50 self.assertEqual(get_cell_by_position(document, 0, 1, 0).getString(), "Misc")
51 #UNDO
52 self.xUITest.executeCommand(".uno:Undo")
53 #Select cell A3
54 gridwin.executeAction("SELECT", mkPropertyValues({"CELL": "A3"}))
55 #Open sort dialog by DATA - SORT /Switch to tabpage Options
56 with self.ui_test.execute_dialog_through_command(".uno:DataSort", close_button="cancel") as xDialog:
57 xTabs = xDialog.getChild("tabcontrol")
58 select_pos(xTabs, "1")
59 #Verify that option "Range contains column labels" is set
60 xHeader = xDialog.getChild("cbHeader")
61 self.assertEqual(get_state_as_dict(xHeader)["Selected"], "true")
62 #Cancel dialog
63 #Select Range A1:B5
64 gridwin.executeAction("SELECT", mkPropertyValues({"RANGE": "A1:B5"}))
65 #Press toolbarbutton for descending sorting
66 self.xUITest.executeCommand(".uno:SortDescending")
67 #Verify that cell A1 still contains "Number" and B1 "Misc"
68 self.assertEqual(get_cell_by_position(document, 0, 0, 0).getString(), "Number")
69 self.assertEqual(get_cell_by_position(document, 0, 1, 0).getString(), "Misc")
71 def test_Sortingbuttons_list_has_not_columnheaders(self):
72 with self.ui_test.create_doc_in_start_center("calc") as document:
73 xCalcDoc = self.xUITest.getTopFocusWindow()
74 gridwin = xCalcDoc.getChild("grid_window")
75 #In column A enter: 5; 3; 4; 6; 2 / In column B enter: e; s; d; f; g
76 enter_text_to_cell(gridwin, "A1", "5")
77 enter_text_to_cell(gridwin, "A2", "3")
78 enter_text_to_cell(gridwin, "A3", "4")
79 enter_text_to_cell(gridwin, "A4", "6")
80 enter_text_to_cell(gridwin, "A5", "2")
81 enter_text_to_cell(gridwin, "B1", "e")
82 enter_text_to_cell(gridwin, "B2", "s")
83 enter_text_to_cell(gridwin, "B3", "d")
84 enter_text_to_cell(gridwin, "B4", "f")
85 enter_text_to_cell(gridwin, "B5", "g")
86 #Select cell A3
87 gridwin.executeAction("SELECT", mkPropertyValues({"CELL": "A3"}))
88 #Press toolbar button for ascending sorting
89 self.xUITest.executeCommand(".uno:SortAscending")
90 #Verify that cell A1 no longer contains "5" and B1 no longer contains "e"
91 self.assertEqual(get_cell_by_position(document, 0, 0, 0).getString() != "5", True)
92 self.assertEqual(get_cell_by_position(document, 0, 1, 0).getString() != "e", True)
93 #UNDO
94 self.xUITest.executeCommand(".uno:Undo")
95 #Select cell B3
96 gridwin.executeAction("SELECT", mkPropertyValues({"CELL": "B3"}))
97 #Open sort dialog by DATA - SORT /Switch to tabpage Options
98 with self.ui_test.execute_dialog_through_command(".uno:DataSort", close_button="cancel") as xDialog:
99 xTabs = xDialog.getChild("tabcontrol")
100 select_pos(xTabs, "1")
101 #Verify that option "Range contains column labels" is not set
102 xHeader = xDialog.getChild("cbHeader")
103 self.assertEqual(get_state_as_dict(xHeader)["Selected"], "false")
104 #Cancel dialog
106 def test_Sorting_default_to_selected_column(self):
107 with self.ui_test.create_doc_in_start_center("calc") as document:
108 xCalcDoc = self.xUITest.getTopFocusWindow()
109 gridwin = xCalcDoc.getChild("grid_window")
110 #In column A enter: Number; 3; 4; 6; 2 / In column B enter: Misc; s; d; f; g
111 enter_text_to_cell(gridwin, "A1", "Number")
112 enter_text_to_cell(gridwin, "A2", "3")
113 enter_text_to_cell(gridwin, "A3", "4")
114 enter_text_to_cell(gridwin, "A4", "6")
115 enter_text_to_cell(gridwin, "A5", "2")
116 enter_text_to_cell(gridwin, "B1", "Misc")
117 enter_text_to_cell(gridwin, "B2", "s")
118 enter_text_to_cell(gridwin, "B3", "d")
119 enter_text_to_cell(gridwin, "B4", "f")
120 enter_text_to_cell(gridwin, "B5", "g")
121 #Select cell A3
122 gridwin.executeAction("SELECT", mkPropertyValues({"CELL": "A3"}))
123 #Press toolbarbutton for descending sorting .uno:SortDescending
124 self.xUITest.executeCommand(".uno:SortDescending")
125 #Verify that the sortorder was determined for column A (Number;2;3;4;6)
126 self.assertEqual(get_cell_by_position(document, 0, 0, 0).getString(), "Number")
127 self.assertEqual(get_cell_by_position(document, 0, 0, 1).getValue(), 6)
128 self.assertEqual(get_cell_by_position(document, 0, 0, 2).getValue(), 4)
129 self.assertEqual(get_cell_by_position(document, 0, 0, 3).getValue(), 3)
130 self.assertEqual(get_cell_by_position(document, 0, 0, 4).getValue(), 2)
131 #UNDO
132 self.xUITest.executeCommand(".uno:Undo")
133 #Select cell B3
134 gridwin.executeAction("SELECT", mkPropertyValues({"CELL": "B3"}))
135 #Press toolbar button for ascending sorting
136 self.xUITest.executeCommand(".uno:SortAscending")
137 #Verify that the sortorder was determined for column B (Misc;s;g;f;d)
138 self.assertEqual(get_cell_by_position(document, 0, 1, 0).getString(), "Misc")
139 self.assertEqual(get_cell_by_position(document, 0, 1, 1).getString(), "d")
140 self.assertEqual(get_cell_by_position(document, 0, 1, 2).getString(), "f")
141 self.assertEqual(get_cell_by_position(document, 0, 1, 3).getString(), "g")
142 self.assertEqual(get_cell_by_position(document, 0, 1, 4).getString(), "s")
145 def test_Sorting_default_to_selected_TAB_A_column(self):
146 with self.ui_test.create_doc_in_start_center("calc") as document:
147 xCalcDoc = self.xUITest.getTopFocusWindow()
148 gridwin = xCalcDoc.getChild("grid_window")
149 #In column A enter: Number; 3; 4; 6; 2 / In column B enter: Misc; s; d; f; g
150 enter_text_to_cell(gridwin, "A1", "Number")
151 enter_text_to_cell(gridwin, "A2", "3")
152 enter_text_to_cell(gridwin, "A3", "4")
153 enter_text_to_cell(gridwin, "A4", "6")
154 enter_text_to_cell(gridwin, "A5", "2")
155 enter_text_to_cell(gridwin, "B1", "Misc")
156 enter_text_to_cell(gridwin, "B2", "s")
157 enter_text_to_cell(gridwin, "B3", "d")
158 enter_text_to_cell(gridwin, "B4", "f")
159 enter_text_to_cell(gridwin, "B5", "g")
160 #Select Range A1:B5
161 gridwin.executeAction("SELECT", mkPropertyValues({"RANGE": "A1:B5"}))
162 #Move the active cell inside the range to column A by using the TAB key
163 gridwin.executeAction("TYPE", mkPropertyValues({"KEYCODE": "TAB"}))
164 gridwin.executeAction("TYPE", mkPropertyValues({"KEYCODE": "TAB"}))
165 #Press toolbar button for ascending sorting
166 self.xUITest.executeCommand(".uno:SortAscending")
167 #Verify that the sortorder was determined for column A (Number;2;3;4;6)
168 self.assertEqual(get_cell_by_position(document, 0, 0, 0).getString(), "Number")
169 self.assertEqual(get_cell_by_position(document, 0, 0, 1).getValue(), 2)
170 self.assertEqual(get_cell_by_position(document, 0, 0, 2).getValue(), 3)
171 self.assertEqual(get_cell_by_position(document, 0, 0, 3).getValue(), 4)
172 self.assertEqual(get_cell_by_position(document, 0, 0, 4).getValue(), 6)
175 def test_Sorting_default_to_selected_TAB_B_column(self):
176 with self.ui_test.create_doc_in_start_center("calc") as document:
177 xCalcDoc = self.xUITest.getTopFocusWindow()
178 gridwin = xCalcDoc.getChild("grid_window")
179 #In column A enter: Number; 3; 4; 6; 2 / In column B enter: Misc; s; d; f; g
180 enter_text_to_cell(gridwin, "A1", "Number")
181 enter_text_to_cell(gridwin, "A2", "3")
182 enter_text_to_cell(gridwin, "A3", "4")
183 enter_text_to_cell(gridwin, "A4", "6")
184 enter_text_to_cell(gridwin, "A5", "2")
185 enter_text_to_cell(gridwin, "B1", "Misc")
186 enter_text_to_cell(gridwin, "B2", "s")
187 enter_text_to_cell(gridwin, "B3", "d")
188 enter_text_to_cell(gridwin, "B4", "f")
189 enter_text_to_cell(gridwin, "B5", "g")
190 #Select Range A1:B5
191 gridwin.executeAction("SELECT", mkPropertyValues({"RANGE": "A1:B5"}))
192 #Move the active cell inside the range to column B by using the TAB key
193 gridwin.executeAction("TYPE", mkPropertyValues({"KEYCODE": "TAB"}))
194 #Press toolbar button for ascending sorting
195 self.xUITest.executeCommand(".uno:SortAscending")
196 #Verify that the sortorder was determined for column B (Misc;d;f;g;s)
197 self.assertEqual(get_cell_by_position(document, 0, 1, 0).getString(), "Misc")
198 self.assertEqual(get_cell_by_position(document, 0, 1, 1).getString(), "d")
199 self.assertEqual(get_cell_by_position(document, 0, 1, 2).getString(), "f")
200 self.assertEqual(get_cell_by_position(document, 0, 1, 3).getString(), "g")
201 self.assertEqual(get_cell_by_position(document, 0, 1, 4).getString(), "s")
204 def test_Sorting_sort_criteria(self):
205 with self.ui_test.create_doc_in_start_center("calc"):
206 xCalcDoc = self.xUITest.getTopFocusWindow()
207 gridwin = xCalcDoc.getChild("grid_window")
208 #In column A enter: Number; 3; 4; 6; 2 / In column B enter: Misc; s; d; f; g
209 enter_text_to_cell(gridwin, "A1", "Number")
210 enter_text_to_cell(gridwin, "A2", "3")
211 enter_text_to_cell(gridwin, "A3", "4")
212 enter_text_to_cell(gridwin, "A4", "6")
213 enter_text_to_cell(gridwin, "A5", "2")
214 enter_text_to_cell(gridwin, "B1", "Misc")
215 enter_text_to_cell(gridwin, "B2", "s")
216 enter_text_to_cell(gridwin, "B3", "d")
217 enter_text_to_cell(gridwin, "B4", "f")
218 enter_text_to_cell(gridwin, "B5", "g")
219 #Select cell A3
220 gridwin.executeAction("SELECT", mkPropertyValues({"CELL": "A3"}))
221 #Open sort dialog by DATA - SORT
222 with self.ui_test.execute_dialog_through_command(".uno:DataSort", close_button="cancel") as xDialog:
223 xTabs = xDialog.getChild("tabcontrol")
224 select_pos(xTabs, "0")
225 #Verify that the first sort criteria is set to "Number(ascending)"
226 xSortKey1 = xDialog.getChild("sortlb")
227 xAsc = xDialog.getChild("up")
228 self.assertEqual(get_state_as_dict(xSortKey1)["SelectEntryText"], "Number")
229 self.assertEqual(get_state_as_dict(xAsc)["Checked"], "true")
230 #Cancel dialog
231 #Select cell B3
232 gridwin.executeAction("SELECT", mkPropertyValues({"CELL": "B3"}))
233 #Open sort dialog by DATA - SORT
234 with self.ui_test.execute_dialog_through_command(".uno:DataSort", close_button="cancel") as xDialog:
235 xTabs = xDialog.getChild("tabcontrol")
236 select_pos(xTabs, "0")
237 #Verify that the first sort criteria is set to "Misc (ascending)"
238 xSortKey1 = xDialog.getChild("sortlb")
239 xAsc = xDialog.getChild("up")
240 self.assertEqual(get_state_as_dict(xSortKey1)["SelectEntryText"], "Misc")
241 self.assertEqual(get_state_as_dict(xAsc)["Checked"], "true")
242 #Cancel dialog
243 #Select Range A1:B5
244 gridwin.executeAction("SELECT", mkPropertyValues({"RANGE": "A1:B5"}))
245 #Move the active cell inside the range to column A by using the TAB key
246 gridwin.executeAction("TYPE", mkPropertyValues({"KEYCODE": "TAB"}))
247 gridwin.executeAction("TYPE", mkPropertyValues({"KEYCODE": "TAB"}))
248 #Open sort dialog by DATA - SORT
249 with self.ui_test.execute_dialog_through_command(".uno:DataSort", close_button="cancel") as xDialog:
250 xTabs = xDialog.getChild("tabcontrol")
251 select_pos(xTabs, "0")
252 #Verify that the first sort criteria is set to "Number(ascending)"
253 xSortKey1 = xDialog.getChild("sortlb")
254 xAsc = xDialog.getChild("up")
255 self.assertEqual(get_state_as_dict(xSortKey1)["SelectEntryText"], "Number")
256 self.assertEqual(get_state_as_dict(xAsc)["Checked"], "true")
257 #Cancel dialog
258 #Select Range A1:B5
259 gridwin.executeAction("SELECT", mkPropertyValues({"RANGE": "A1:B5"}))
260 #Move the active cell inside the range to column B by using the TAB key
261 gridwin.executeAction("TYPE", mkPropertyValues({"KEYCODE": "TAB"}))
262 #Open sort dialog by DATA - SORT
263 with self.ui_test.execute_dialog_through_command(".uno:DataSort", close_button="cancel") as xDialog:
264 xTabs = xDialog.getChild("tabcontrol")
265 select_pos(xTabs, "0")
266 #Verify that the first sort criteria is set to "Misc (ascending)"
267 xSortKey1 = xDialog.getChild("sortlb")
268 xAsc = xDialog.getChild("up")
269 self.assertEqual(get_state_as_dict(xSortKey1)["SelectEntryText"], "Misc")
270 self.assertEqual(get_state_as_dict(xAsc)["Checked"], "true")
271 #Cancel dialog
273 # vim: set shiftwidth=4 softtabstop=4 expandtab: