Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / qa / uitest / writer_tests4 / spellDialog.py
blob39ef8eaebe42d25c24da6072e05048cb09b6ae19
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 import re
11 from uitest.framework import UITestCase
12 from uitest.uihelper.common import get_state_as_dict, get_url_for_data_file
13 from uitest.uihelper.common import type_text
15 from libreoffice.linguistic.linguservice import get_spellchecker
16 from com.sun.star.lang import Locale
18 class SpellingAndGrammarDialog(UITestCase):
20 def is_supported_locale(self, language, country):
21 xSpellChecker = get_spellchecker(self.ui_test._xContext)
22 locales = xSpellChecker.getLocales()
23 for locale in locales:
24 if language != None:
25 if locale.Language != language:
26 continue
28 if country != None:
29 if locale.Country != country:
30 continue
32 # we found the correct combination
33 return True
35 TDF46852_INPUT = """\
36 dogg
37 dogg
38 catt dogg
39 frogg frogg
40 frogg catt dogg
41 dogg catt
42 frog, dogg, catt"""
44 TDF46852_REGEX = """\
45 ([a-z]+)
46 \\1
47 ([a-z]+) \\1
48 ([a-z]+) \\3
49 \\3 \\2 \\1
50 \\1 \\2
51 \\3, \\1, \\2"""
53 def test_tdf46852(self):
54 supported_locale = self.is_supported_locale("en", "US")
55 if not supported_locale:
56 self.skipTest("no dictionary support for en_US available")
57 # This automates the steps described in the bug report tdf#46852
59 # Step 1: Create a document with repetitious misspelled words
60 with self.ui_test.create_doc_in_start_center("writer") as document:
61 cursor = document.getCurrentController().getViewCursor()
62 # Inserted text must be en_US, so make sure to set language in current location
63 cursor.CharLocale = Locale("en", "US", "")
64 input_text = self.TDF46852_INPUT.replace('\n', '\r') # \r = para break
65 document.Text.insertString(cursor, input_text, False)
67 # Step 2: Place cursor on 4th line after second "frogg"
68 cursor.goUp(2, False)
69 cursor.goLeft(1, False)
71 # Step 3: Initiate spellchecking, and make sure "Check grammar" is
72 # unchecked
73 with self.ui_test.execute_modeless_dialog_through_command(".uno:SpellingAndGrammarDialog", close_button="") as xDialog:
74 checkgrammar = xDialog.getChild('checkgrammar')
75 if get_state_as_dict(checkgrammar)['Selected'] == 'true':
76 checkgrammar.executeAction('CLICK', ())
77 self.assertTrue(get_state_as_dict(checkgrammar)['Selected'] == 'false')
79 # Step 4: Repetitively click on "Correct all" for each misspelling
80 # prompt until end of document is reached.
81 changeall = xDialog.getChild('changeall')
82 changeall.executeAction("CLICK", ())
83 changeall.executeAction("CLICK", ())
84 # The third time we click on changeall, the click action is going to
85 # block while two message boxes are shown, so we need to do this third
86 # click specially
87 # Use empty close_button to open consecutive dialogs
88 with self.ui_test.execute_blocking_action(
89 changeall.executeAction, args=('CLICK', ()), close_button="") as dialog:
90 # Step 5: Confirm to "Continue check at beginning of document"
91 xYesBtn = dialog.getChild("yes")
93 with self.ui_test.execute_blocking_action(
94 xYesBtn.executeAction, args=('CLICK', ())):
95 pass
97 output_text = document.Text.getString().replace('\r\n', '\n')
98 self.assertTrue(re.match(self.TDF46852_REGEX, output_text))
100 def test_tdf136855(self):
101 supported_locale = self.is_supported_locale("en", "US")
102 if not supported_locale:
103 self.skipTest("no dictionary support for en_US available")
105 with self.ui_test.load_file(get_url_for_data_file("tdf136855.odt")) as writer_doc:
107 with self.ui_test.execute_modeless_dialog_through_command(".uno:SpellingAndGrammarDialog", close_button="close") as xDialog:
109 xChangeBtn = xDialog.getChild('change')
110 for i in range(6):
111 # Without the fix in place, this test would have crashed here
112 xChangeBtn.executeAction("CLICK", ())
114 output_text = writer_doc.Text.getString().replace('\n', '').replace('\r', '')
115 self.assertTrue(output_text.startswith("xx xx xx xxxxxxxxxxix xxxxxxxxxxxxxxviii"))
117 def test_tdf66043(self):
118 supported_locale = self.is_supported_locale("en", "US")
119 if not supported_locale:
120 self.skipTest("no dictionary support for en_US available")
121 with self.ui_test.load_file(get_url_for_data_file("tdf66043.fodt")) as writer_doc:
122 # Step 1: Initiate spellchecking, and make sure "Check grammar" is
123 # unchecked
124 with self.ui_test.execute_modeless_dialog_through_command(".uno:SpellingAndGrammarDialog", close_button="close") as xDialog:
125 checkgrammar = xDialog.getChild('checkgrammar')
126 if get_state_as_dict(checkgrammar)['Selected'] == 'true':
127 checkgrammar.executeAction('CLICK', ())
128 self.assertTrue(get_state_as_dict(checkgrammar)['Selected'] == 'false')
130 # Step 2: Click on "Correct all" for each misspelling
131 # prompt until end of document is reached.
132 changeall = xDialog.getChild('changeall')
133 changeall.executeAction("CLICK", ())
135 output_text = writer_doc.Text.getString().replace('\r\n', '\n')
136 # This was "gooodgood baaad eeend" ("goood" is a deletion,
137 # "good" is an insertion by fixing the first misspelling),
138 # but now "goood" is not a misspelling because it is accepted
139 # correctly without the redline containing a deleted "o"
140 self.assertEqual(output_text, 'goood baaadbaaed eeend')
142 def test_DoNotCheckURL(self):
143 supported_locale = self.is_supported_locale("en", "US")
144 if not supported_locale:
145 self.skipTest("no dictionary support for en_US available")
147 with self.ui_test.create_doc_in_start_center("writer") as document:
148 cursor = document.getCurrentController().getViewCursor()
149 # Inserted text must be en_US, so make sure to set language in current location
150 cursor.CharLocale = Locale("en", "US", "")
152 xMainWindow = self.xUITest.getTopFocusWindow()
153 xEdit = xMainWindow.getChild("writer_edit")
155 # URL is recognized during typing
156 type_text(xEdit, "baaad http://www.baaad.org baaad baaad")
158 with self.ui_test.execute_modeless_dialog_through_command(".uno:SpellingAndGrammarDialog", close_button="close") as xDialog:
159 checkgrammar = xDialog.getChild('checkgrammar')
160 if get_state_as_dict(checkgrammar)['Selected'] == 'true':
161 checkgrammar.executeAction('CLICK', ())
162 self.assertTrue(get_state_as_dict(checkgrammar)['Selected'] == 'false')
164 change = xDialog.getChild('change')
165 change.executeAction("CLICK", ())
166 change.executeAction("CLICK", ())
168 output_text = document.Text.getString()
169 # This was "Baaed HTTP://www.baaad.org baaad baaad" (spelling URLs)
170 self.assertEqual("Baaed http://www.baaad.org baaed baaad", output_text)
172 def test_tdf45949(self):
173 supported_locale = self.is_supported_locale("en", "US")
174 if not supported_locale:
175 self.skipTest("no dictionary support for en_US available")
177 with self.ui_test.create_doc_in_start_center("writer") as document:
178 cursor = document.getCurrentController().getViewCursor()
179 # Inserted text must be en_US, so make sure to set language in current location
180 cursor.CharLocale = Locale("en", "US", "")
182 xMainWindow = self.xUITest.getTopFocusWindow()
183 xEdit = xMainWindow.getChild("writer_edit")
185 # URL is recognized during typing
186 type_text(xEdit, "baaad http://www.baaad.org baaad")
188 # add spaces before and after the word "baaad" within the URL
189 cursor.goLeft(10, False)
190 type_text(xEdit, " ")
191 cursor.goLeft(6, False)
192 type_text(xEdit, " ")
194 with self.ui_test.execute_modeless_dialog_through_command(".uno:SpellingAndGrammarDialog", close_button="close") as xDialog:
195 checkgrammar = xDialog.getChild('checkgrammar')
196 if get_state_as_dict(checkgrammar)['Selected'] == 'true':
197 checkgrammar.executeAction('CLICK', ())
198 self.assertTrue(get_state_as_dict(checkgrammar)['Selected'] == 'false')
200 change = xDialog.getChild('change')
201 change.executeAction("CLICK", ())
202 change.executeAction("CLICK", ())
204 output_text = document.Text.getString()
205 # This was "Baaed HTTP://www. baaad .org baaed" (skipped non-URL words of hypertext)
206 self.assertEqual("Baaed http://www. baaed .org baaad", output_text)
208 def test_tdf65535(self):
209 supported_locale = self.is_supported_locale("en", "US")
210 if not supported_locale:
211 self.skipTest("no dictionary support for en_US available")
213 with self.ui_test.load_file(get_url_for_data_file("tdf65535.fodt")) as document:
214 cursor = document.getCurrentController().getViewCursor()
215 # Inserted text must be en_US, so make sure to set language in current location
216 cursor.CharLocale = Locale("en", "US", "")
218 xMainWindow = self.xUITest.getTopFocusWindow()
219 xEdit = xMainWindow.getChild("writer_edit")
221 # type a bad word after the word with comment
222 cursor.goRight(5, False)
223 type_text(xEdit, " baad")
224 cursor.goLeft(10, False)
226 # fix the first word using the spelling dialog
227 with self.ui_test.execute_modeless_dialog_through_command(".uno:SpellingAndGrammarDialog", close_button="close") as xDialog:
228 checkgrammar = xDialog.getChild('checkgrammar')
229 if get_state_as_dict(checkgrammar)['Selected'] == 'true':
230 checkgrammar.executeAction('CLICK', ())
231 self.assertTrue(get_state_as_dict(checkgrammar)['Selected'] == 'false')
233 change = xDialog.getChild('change')
234 change.executeAction("CLICK", ())
236 # FIXME: disabled for the sake of testing on lo-upsan build
237 # output_text = document.Text.getString()
238 # self.assertEqual(fixed_word, output_text)
240 # check the original comment
241 has_comment = False
242 textfields = document.getTextFields()
243 for textfield in textfields:
244 if textfield.supportsService("com.sun.star.text.TextField.Annotation"):
245 has_comment = True
247 # This was False (lost comment)
248 self.assertEqual(True, has_comment)
250 # vim: set shiftwidth=4 softtabstop=4 expandtab: