summaryrefslogtreecommitdiffstats
path: root/sw/qa/uitest/navigator/tdf154521.py
blob: 048efb0212c2df0cac6da6142a9593f0a8361f50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

from uitest.framework import UITestCase
from libreoffice.uno.propertyvalue import mkPropertyValues
from uitest.uihelper.common import get_state_as_dict
from com.sun.star.view import XSelectionChangeListener
import unohelper

selectionChangedResult = 0

class XSelectionChangeListenerExtended(unohelper.Base, XSelectionChangeListener):
    # is invoked when a text selected, e.g. by selecting a bookmark in the Navigator
    @classmethod
    def selectionChanged(self, event):
        global selectionChangedResult
        selection = event.Source.getSelection()
        if selection.supportsService("com.sun.star.text.TextRanges"):
            textRange = selection.getByIndex(0)
            if textRange.getString() == "foo":
                selectionChangedResult = 1
            else:
                selectionChangedResult = -1

    @classmethod
    def disposing(self, event):
        pass

class tdf154521(UITestCase):

    def test_tdf154521(self):
        global selectionChangedResult
        with self.ui_test.create_doc_in_start_center("writer") as xDoc:

            xWriterDoc = self.xUITest.getTopFocusWindow()
            xWriterEdit = xWriterDoc.getChild("writer_edit")

            self.xUITest.executeCommand(".uno:Sidebar")
            xWriterEdit.executeAction("SIDEBAR", mkPropertyValues({"PANEL": "SwNavigatorPanel"}))

            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBar = xNavigatorPanel.getChild("content5")
            xToolBar.executeAction("CLICK", mkPropertyValues({"POS": "0"})) # 'root' button

            # type "foo", and create a bookmark on it

            self.xUITest.executeCommand(".uno:Escape")

            xDoc.Text.insertString(xDoc.Text.getStart(), "foo", False)
            self.xUITest.executeCommand(".uno:SelectAll")

            with self.ui_test.execute_dialog_through_command(".uno:InsertBookmark", close_button="insert"):
                pass

            xWriterEdit.executeAction("FOCUS", tuple())

            # add XSelectionChangeListener

            xListener = XSelectionChangeListenerExtended()
            self.assertIsNotNone(xListener)
            xDoc.CurrentController.addSelectionChangeListener(xListener)

            xContentTree = xNavigatorPanel.getChild("contenttree")

            self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 1")
            self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 1")
            self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1")

            # select bookmark to trigger selectionChanged event

            xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"}))

            # wait for handling the selectionChanged event
            while selectionChangedResult == 0:
                pass

            # This was -1 (missing selection because of early broadcasting)
            self.assertEqual(selectionChangedResult, 1)

            self.xUITest.executeCommand(".uno:Sidebar")

    def getTitle(self, document):
        xController = document.getCurrentController()
        xSidebar = xController.getSidebar()
        xDecks = xSidebar.getDecks()
        xNavigator = xDecks['NavigatorDeck']
        xPanels = xNavigator.getPanels()
        xPanel = xPanels['SwNavigatorPanel']
        title = xPanel.getTitle()
        # empty title of SwNavigatorPanel to allow to query the name of the selected bookmark
        xPanel.setTitle("")
        return title

    def test_query_selected_bookmark(self):
        global selectionChangedResult
        with self.ui_test.create_doc_in_start_center("writer") as xDoc:

            # click on the bookmark name in the Navigator

            xWriterDoc = self.xUITest.getTopFocusWindow()
            xWriterEdit = xWriterDoc.getChild("writer_edit")

            self.xUITest.executeCommand(".uno:Sidebar")
            xWriterEdit.executeAction("SIDEBAR", mkPropertyValues({"PANEL": "SwNavigatorPanel"}))

            xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel")
            xToolBar = xNavigatorPanel.getChild("content5")
            xToolBar.executeAction("CLICK", mkPropertyValues({"POS": "0"})) # 'root' button

            # type "foo", and create 3 bookmarks on it

            self.xUITest.executeCommand(".uno:Escape")

            xDoc.Text.insertString(xDoc.Text.getStart(), "foo", False)
            self.xUITest.executeCommand(".uno:SelectAll")

            for i in range(3):
                with self.ui_test.execute_dialog_through_command(".uno:InsertBookmark", close_button="insert"):
                    pass

            # check selected bookmarks in Navigator

            xWriterEdit.executeAction("FOCUS", tuple())

            xContentTree = xNavigatorPanel.getChild("contenttree")

# disable flakey UITest
#            self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 1")
#            self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 1")
#            self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1")

##            self.xUITest.executeCommand(".uno:Escape")

#            # get the title of SwNavigatorPanel with emptying it to access to the selected bookmark
#            self.assertEqual(self.getTitle(xDoc), "Navigator")
#            # title was emptied
#            self.assertEqual(self.getTitle(xDoc), "")

#            # Select nested bookmark in Navigator

#            xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"}))

#            # This jumped to Bookmark 1 after selection
#            self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 1")
#            self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 1")
#            self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1")

#            # This was "Navigator"
#            self.assertEqual(self.getTitle(xDoc), "Bookmark 1")

#            # Try the same selection with Bookmark 2
#            xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"}))
#            self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 2")
#            self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 2")
#            self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1")
#            xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"}))
#            self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 2")
#            self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 2")
#            self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1")

#            # This was "Navigator"
#            self.assertEqual(self.getTitle(xDoc), "Bookmark 2")

#            # Try the same selection with Bookmark 3

#            # why we need this extra UP?
#            xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"}))
#            self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 2")
#            xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"}))
#            self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 3")

#            self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 3")
#            self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1")
#            xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"}))
#            self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 3")
#            self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 3")
#            self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1")

#            # This was "Navigator"
#            self.assertEqual(self.getTitle(xDoc), "Bookmark 3")

#            # go to the previous item

#            # why we need this extra UP?
#            xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"}))
#            self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 3")
#            xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"}))
#            self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmarks")
#            self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmarks")
#            xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"}))

#            # This was "Navigator"
#            self.assertEqual(self.getTitle(xDoc), "")

#            self.xUITest.executeCommand(".uno:Sidebar")

# vim: set shiftwidth=4 softtabstop=4 expandtab: