1#
2# This Source Code Form is subject to the terms of the Mozilla Public
3# License, v. 2.0. If a copy of the MPL was not distributed with this
4# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5#
6
7import re
8
9from uitest.framework import UITestCase
10from uitest.uihelper.common import get_state_as_dict
11
12from libreoffice.linguistic.linguservice import get_spellchecker
13
14class SpellingAndGrammarDialog(UITestCase):
15
16    def is_supported_locale(self, language, country):
17        xSpellChecker = get_spellchecker(self.ui_test._xContext)
18        locales = xSpellChecker.getLocales()
19        for locale in locales:
20            if language != None:
21                if locale.Language != language:
22                    continue
23
24            if country != None:
25                if locale.Country != country:
26                    continue
27
28            # we found the correct combination
29            return True
30
31    def launch_dialog(self):
32        self.ui_test.execute_modeless_dialog_through_command(
33            ".uno:SpellingAndGrammarDialog")
34
35        return self.xUITest.getTopFocusWindow()
36
37    TDF46852_INPUT = """\
38dogg
39dogg
40catt dogg
41frogg frogg
42frogg catt dogg
43dogg catt
44frog, dogg, catt"""
45
46    TDF46852_REGEX = """\
47([a-z]+)
48\\1
49([a-z]+) \\1
50([a-z]+) \\3
51\\3 \\2 \\1
52\\1 \\2
53\\3, \\1, \\2"""
54
55    def test_tdf46852(self):
56        supported_locale = self.is_supported_locale("en", "US")
57        if not supported_locale:
58            self.skipTest("no dictionary support for en_US available")
59        # This automates the steps described in the bug report tdf#46852
60
61        # Step 1: Create a document with repetitious misspelled words
62        self.ui_test.create_doc_in_start_center("writer")
63        document = self.ui_test.get_component()
64        cursor = document.getCurrentController().getViewCursor()
65        input_text = self.TDF46852_INPUT.replace('\n', '\r') # \r = para break
66        document.Text.insertString(cursor, input_text, False)
67
68        # Step 2: Place cursor on 4th line after second "frogg"
69        cursor.goUp(2, False)
70        cursor.goLeft(1, False)
71
72        # Step 3: Initiate spellchecking, and make sure "Check grammar" is
73        # unchecked
74        spell_dialog = self.launch_dialog()
75        checkgrammar = spell_dialog.getChild('checkgrammar')
76        if get_state_as_dict(checkgrammar)['Selected'] == 'true':
77            checkgrammar.executeAction('CLICK', ())
78        self.assertTrue(get_state_as_dict(checkgrammar)['Selected'] == 'false')
79
80        # Step 4: Repetitively click on "Correct all" for each misspelling
81        #         prompt until end of document is reached.
82        changeall = spell_dialog.getChild('changeall')
83        changeall.executeAction("CLICK", ())
84        changeall.executeAction("CLICK", ())
85        # The third time we click on changeall, the click action is going to
86        # block while two message boxes are shown, so we need to do this third
87        # click specially:
88        self.ui_test.execute_blocking_action(
89            changeall.executeAction, args=('CLICK', ()),
90            # Step 5: Confirm to "Continue check at beginning of document"
91            dialog_handler=lambda dialog :
92                self.ui_test.execute_blocking_action(
93                    dialog.getChild('yes').executeAction, 'ok', ('CLICK', ())
94                )
95            )
96
97        self.assertTrue(re.match(self.TDF46852_REGEX, document.Text.getString()))
98
99