Get the style color and number just once
[LibreOffice.git] / sw / qa / uitest / findReplace / findReplace.py
blob7c9f5315e380fa60fedd2d85b025c9a4eb967d7d
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.common import select_pos
11 from libreoffice.uno.propertyvalue import mkPropertyValues
12 from uitest.uihelper.common import get_state_as_dict, get_url_for_data_file, type_text
14 class findReplace(UITestCase):
15 def test_find_writer(self):
16 with self.ui_test.load_file(get_url_for_data_file("findReplace.odt")) as writer_doc:
17 xWriterDoc = self.xUITest.getTopFocusWindow()
18 xWriterEdit = xWriterDoc.getChild("writer_edit")
19 with self.ui_test.execute_modeless_dialog_through_command(".uno:SearchDialog", close_button="close") as xDialog:
21 searchterm = xDialog.getChild("searchterm")
22 searchterm.executeAction("TYPE", mkPropertyValues({"TEXT":"second"})) #2nd page
23 xsearch = xDialog.getChild("search")
24 xsearch.executeAction("CLICK", tuple())
25 #verify
26 self.assertEqual(get_state_as_dict(xWriterEdit)["CurrentPage"], "2")
28 searchterm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
29 searchterm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"BACKSPACE"}))
30 searchterm.executeAction("TYPE", mkPropertyValues({"TEXT":"third"}))
31 xsearch.executeAction("CLICK", tuple())
32 #verify
33 self.assertEqual(get_state_as_dict(xWriterEdit)["CurrentPage"], "3") #3rd page
36 #now open dialog and verify find="third" (remember last value); replace value with "First" ( click match case) with word "Replace" - click twice Replace button, check "Replace first first"
37 with self.ui_test.execute_modeless_dialog_through_command(".uno:SearchDialog", close_button="close") as xDialog:
38 searchterm = xDialog.getChild("searchterm")
39 self.assertEqual(get_state_as_dict(searchterm)["Text"], "third")
40 searchterm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
41 searchterm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"BACKSPACE"}))
42 searchterm.executeAction("TYPE", mkPropertyValues({"TEXT":"First"}))
43 matchcase = xDialog.getChild("matchcase")
44 matchcase.executeAction("CLICK", tuple()) #click match case
45 replaceterm = xDialog.getChild("replaceterm")
46 replaceterm.executeAction("TYPE", mkPropertyValues({"TEXT":"Replace"})) #replace textbox
47 replace = xDialog.getChild("replace")
48 replace.executeAction("CLICK", tuple())
49 replace.executeAction("CLICK", tuple()) #click twice Replace button
50 #verify
51 self.assertEqual(writer_doc.Text.String[0:19], "Replace first first")
53 #now replace first (uncheck match case) with word "aaa" - click once Replace All button, check "Replace aaa aaa"
54 replaceterm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
55 replaceterm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"BACKSPACE"}))
56 replaceterm.executeAction("TYPE", mkPropertyValues({"TEXT":"aaa"}))
57 matchcase.executeAction("CLICK", tuple()) #uncheck match case
58 replaceall = xDialog.getChild("replaceall")
59 replaceall.executeAction("CLICK", tuple())
60 #verify
61 self.assertEqual(writer_doc.Text.String[0:15], "Replace aaa aaa")
63 #now delete strings in textbox Find and textbox Replace, click button Format, select Size=16 pt, Close OK, Find Next; verify Page=2;
64 #click No format button; close dialog
65 replaceterm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
66 replaceterm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"BACKSPACE"}))
67 searchterm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
68 searchterm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"BACKSPACE"}))
70 format = xDialog.getChild("format")
71 with self.ui_test.execute_blocking_action(format.executeAction, args=('CLICK', ())) as dialog:
72 xTabs = dialog.getChild("tabcontrol")
73 select_pos(xTabs, "0")
74 xSizeFont = dialog.getChild("cbWestSize")
75 xSizeFont.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
76 xSizeFont.executeAction("TYPE", mkPropertyValues({"TEXT":"16"})) #set font size 16
78 # Verify these didn't get set again through SvxSearchController::StateChanged, timer-
79 # triggered from SfxBindings::NextJob while executing the Format dialog above:
80 self.assertEqual(get_state_as_dict(searchterm)["Text"], "")
81 self.assertEqual(get_state_as_dict(replaceterm)["Text"], "")
83 xsearch = xDialog.getChild("search")
84 xsearch.executeAction("CLICK", tuple())
85 #verify
86 self.assertEqual(get_state_as_dict(xWriterEdit)["CurrentPage"], "2")
87 noformat = xDialog.getChild("noformat")
88 noformat.executeAction("CLICK", tuple()) #click No format button
91 #Bug 39022 - find-replace->$1, not pattern
92 def test_tdf39022_replace_regexp(self):
93 with self.ui_test.create_doc_in_start_center("writer") as document:
94 xWriterDoc = self.xUITest.getTopFocusWindow()
95 xWriterEdit = xWriterDoc.getChild("writer_edit")
96 type_text(xWriterEdit, "test number1 testnot")
98 with self.ui_test.execute_modeless_dialog_through_command(".uno:SearchDialog", close_button="close") as xDialog:
99 searchterm = xDialog.getChild("searchterm")
100 searchterm.executeAction("TYPE", mkPropertyValues({"TEXT":"T(est|other)\\>"})) #find
101 replaceterm = xDialog.getChild("replaceterm")
102 replaceterm.executeAction("TYPE", mkPropertyValues({"TEXT":"replaced$1"})) #replace
104 # Deselect similarity before selecting regex
105 xSimilarity = xDialog.getChild("similarity")
106 if get_state_as_dict(xSimilarity)['Selected'] == 'true':
107 xSimilarity.executeAction("CLICK", tuple())
109 regexp = xDialog.getChild("regexp")
110 regexp.executeAction("CLICK", tuple())
111 self.assertEqual("true", get_state_as_dict(regexp)['Selected'])
112 replaceall = xDialog.getChild("replaceall")
113 replaceall.executeAction("CLICK", tuple())
114 #verify
115 self.assertEqual(document.Text.String[0:27], "replacedest number1 testnot")
117 # Deselect regex button, otherwise it might affect other tests
118 regexp.executeAction("CLICK", tuple())
119 self.assertEqual("false", get_state_as_dict(regexp)['Selected'])
121 #tdf116242 ţ ț
122 def test_tdf116242_replace_t_with_cedilla(self):
123 with self.ui_test.load_file(get_url_for_data_file("tdf116242.odt")) as writer_doc:
125 with self.ui_test.execute_modeless_dialog_through_command(".uno:SearchDialog", close_button="close") as xDialog:
126 searchterm = xDialog.getChild("searchterm")
127 searchterm.executeAction("TYPE", mkPropertyValues({"TEXT":"ţ"})) #find
128 replaceterm = xDialog.getChild("replaceterm")
129 replaceterm.executeAction("TYPE", mkPropertyValues({"TEXT":"ț"})) #replace
130 includediacritics = xDialog.getChild("includediacritics")
131 if (get_state_as_dict(includediacritics)["Selected"]) == "false":
132 includediacritics.executeAction("CLICK", tuple())
133 replaceall = xDialog.getChild("replaceall")
134 replaceall.executeAction("CLICK", tuple())
135 #verify
136 self.assertEqual(writer_doc.Text.String[0:13], "țoootdf116242")
139 #Bug 98417 - FIND & REPLACE: Add 'Find Previous' button
140 def test_tdf98417_find_previous_writer(self):
141 with self.ui_test.load_file(get_url_for_data_file("findReplace.odt")):
142 xWriterDoc = self.xUITest.getTopFocusWindow()
143 xWriterEdit = xWriterDoc.getChild("writer_edit")
144 with self.ui_test.execute_modeless_dialog_through_command(".uno:SearchDialog", close_button="close") as xDialog:
146 searchterm = xDialog.getChild("searchterm")
147 searchterm.executeAction("TYPE", mkPropertyValues({"TEXT":"third"}))
148 xsearch = xDialog.getChild("search")
149 xsearch.executeAction("CLICK", tuple())
150 #verify
151 self.assertEqual(get_state_as_dict(xWriterEdit)["CurrentPage"], "3")
152 xsearch.executeAction("CLICK", tuple())
153 #verify
154 self.assertEqual(get_state_as_dict(xWriterEdit)["CurrentPage"], "4") #page 4
155 backsearch = xDialog.getChild("backsearch")
156 backsearch.executeAction("CLICK", tuple())
157 #verify
158 self.assertEqual(get_state_as_dict(xWriterEdit)["CurrentPage"], "3")
160 def test_tdf136577(self):
161 with self.ui_test.create_doc_in_start_center("writer") as document:
163 xWriterDoc = self.xUITest.getTopFocusWindow()
164 xWriterEdit = xWriterDoc.getChild("writer_edit")
166 type_text(xWriterEdit, "x")
168 self.assertEqual(document.Text.String, "x")
170 with self.ui_test.execute_modeless_dialog_through_command(".uno:SearchDialog", close_button="close") as xDialog:
172 searchterm = xDialog.getChild("searchterm")
173 searchterm.executeAction("TYPE", mkPropertyValues({"TEXT":"x"}))
175 replaceall = xDialog.getChild("replaceall")
176 replaceall.executeAction("CLICK", tuple())
178 self.assertEqual(document.Text.String, "")
180 self.xUITest.executeCommand(".uno:Undo")
182 # Without the fix in place, this test would have failed with AssertionError: '' != 'x'
183 self.assertEqual(document.Text.String, "x")
185 def test_tdf143128(self):
186 with self.ui_test.create_doc_in_start_center("writer") as document:
188 xWriterDoc = self.xUITest.getTopFocusWindow()
189 xWriterEdit = xWriterDoc.getChild("writer_edit")
191 type_text(xWriterEdit, "ß")
193 self.assertEqual(document.Text.String, "ß")
195 with self.ui_test.execute_modeless_dialog_through_command(".uno:SearchDialog", close_button="close") as xDialog:
197 searchterm = xDialog.getChild("searchterm")
198 searchterm.executeAction("TYPE", mkPropertyValues({"TEXT":"ẞ"}))
200 replaceterm = xDialog.getChild("replaceterm")
201 replaceterm.executeAction("TYPE", mkPropertyValues({"TEXT":"SS"}))
203 replaceall = xDialog.getChild("replaceall")
204 replaceall.executeAction("CLICK", tuple())
206 # Without the fix in place, this test would have failed with
207 # AssertionError: 'ß' != 'SS'
208 self.assertEqual(document.Text.String, "SS")
210 self.xUITest.executeCommand(".uno:Undo")
212 self.assertEqual(document.Text.String, "ß")
214 def test_tdf154818_search_history(self):
215 with self.ui_test.create_doc_in_start_center("writer"):
216 with self.ui_test.execute_modeless_dialog_through_command(".uno:SearchDialog", close_button="close") as xDialog:
217 xSearchTerm = xDialog.getChild("searchterm")
218 # Search for an entry and check for the search history (A -> B -> C -> B)
219 searchTerms = ["A", "B", "C", "B"]
220 for searchTerm in searchTerms:
221 xSearchTerm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
222 xSearchTerm.executeAction("TYPE", mkPropertyValues({"KEYCODE":"BACKSPACE"}))
223 xSearchTerm.executeAction("TYPE", mkPropertyValues({"TEXT":searchTerm}))
224 xSearchNext = xDialog.getChild("search")
225 xSearchNext.executeAction("CLICK", tuple())
227 # Check if the search history was respected
228 searchTerms = ["B", "C", "A"]
229 for searchTerm in searchTerms:
230 select_pos(xSearchTerm, str(searchTerms.index(searchTerm)))
231 # Without the fix in place, this test would have failed with
232 # AssertionError: 'B' != 'C'
233 # i.e., the search history was not respected
234 self.assertEqual(searchTerm, get_state_as_dict(xSearchTerm)["Text"])
236 # vim: set shiftwidth=4 softtabstop=4 expandtab: