| 1 |
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*- |
| 2 |
# |
| 3 |
# This file is part of the LibreOffice project. |
| 4 |
# |
| 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/. |
| 8 |
# |
| 9 |
|
| 10 |
from uitest.framework import UITestCase |
| 11 |
from libreoffice.uno.propertyvalue import mkPropertyValues |
| 12 |
from uitest.uihelper.common import get_state_as_dict |
| 13 |
from com.sun.star.view import XSelectionChangeListener |
| 14 |
import unohelper |
| 15 |
|
| 16 |
selectionChangedResult = 0 |
| 17 |
|
| 18 |
class XSelectionChangeListenerExtended(unohelper.Base, XSelectionChangeListener): |
| 19 |
# is invoked when a text selected, e.g. by selecting a bookmark in the Navigator |
| 20 |
@classmethod |
| 21 |
def selectionChanged(self, event): |
| 22 |
global selectionChangedResult |
| 23 |
selection = event.Source.getSelection() |
| 24 |
if selection.supportsService("com.sun.star.text.TextRanges"): |
| 25 |
textRange = selection[0] |
| 26 |
if textRange.getString() == "foo": |
| 27 |
selectionChangedResult = 1 |
| 28 |
else: |
| 29 |
selectionChangedResult = -1 |
| 30 |
|
| 31 |
@classmethod |
| 32 |
def disposing(self, event): |
| 33 |
pass |
| 34 |
|
| 35 |
class tdf154521(UITestCase): |
| 36 |
|
| 37 |
def test_tdf154521(self): |
| 38 |
global selectionChangedResult |
| 39 |
with self.ui_test.create_doc_in_start_center("writer") as xDoc: |
| 40 |
|
| 41 |
xWriterDoc = self.xUITest.getTopFocusWindow() |
| 42 |
xWriterEdit = xWriterDoc.getChild("writer_edit") |
| 43 |
|
| 44 |
self.xUITest.executeCommand(".uno:Sidebar") |
| 45 |
xWriterEdit.executeAction("SIDEBAR", mkPropertyValues({"PANEL": "SwNavigatorPanel"})) |
| 46 |
|
| 47 |
xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel") |
| 48 |
xToolBar = xNavigatorPanel.getChild("content5") |
| 49 |
xToolBar.executeAction("CLICK", mkPropertyValues({"POS": "0"})) # 'root' button |
| 50 |
|
| 51 |
# type "foo", and create a bookmark on it |
| 52 |
|
| 53 |
self.xUITest.executeCommand(".uno:Escape") |
| 54 |
|
| 55 |
xDoc.Text.insertString(xDoc.Text.getStart(), "foo", False) |
| 56 |
self.xUITest.executeCommand(".uno:SelectAll") |
| 57 |
|
| 58 |
with self.ui_test.execute_dialog_through_command(".uno:InsertBookmark", close_button="insert"): |
| 59 |
pass |
| 60 |
|
| 61 |
xWriterEdit.executeAction("FOCUS", tuple()) |
| 62 |
|
| 63 |
# add XSelectionChangeListener |
| 64 |
|
| 65 |
xListener = XSelectionChangeListenerExtended() |
| 66 |
self.assertIsNotNone(xListener) |
| 67 |
xDoc.CurrentController.addSelectionChangeListener(xListener) |
| 68 |
|
| 69 |
xContentTree = xNavigatorPanel.getChild("contenttree") |
| 70 |
|
| 71 |
self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 1") |
| 72 |
self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 1") |
| 73 |
self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1") |
| 74 |
|
| 75 |
# select bookmark to trigger selectionChanged event |
| 76 |
|
| 77 |
xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"})) |
| 78 |
|
| 79 |
# wait for handling the selectionChanged event |
| 80 |
while selectionChangedResult == 0: |
| 81 |
pass |
| 82 |
|
| 83 |
# This was -1 (missing selection because of early broadcasting) |
| 84 |
self.assertEqual(selectionChangedResult, 1) |
| 85 |
|
| 86 |
self.xUITest.executeCommand(".uno:Sidebar") |
| 87 |
|
| 88 |
def getTitle(self, document): |
| 89 |
xController = document.getCurrentController() |
| 90 |
xSidebar = xController.getSidebar() |
| 91 |
xDecks = xSidebar.getDecks() |
| 92 |
xNavigator = xDecks['NavigatorDeck'] |
| 93 |
xPanels = xNavigator.getPanels() |
| 94 |
xPanel = xPanels['SwNavigatorPanel'] |
| 95 |
title = xPanel.getTitle() |
| 96 |
# empty title of SwNavigatorPanel to allow to query the name of the selected bookmark |
| 97 |
xPanel.setTitle("") |
| 98 |
return title |
| 99 |
|
| 100 |
def test_query_selected_bookmark(self): |
| 101 |
global selectionChangedResult |
| 102 |
with self.ui_test.create_doc_in_start_center("writer") as xDoc: |
| 103 |
|
| 104 |
# click on the bookmark name in the Navigator |
| 105 |
|
| 106 |
xWriterDoc = self.xUITest.getTopFocusWindow() |
| 107 |
xWriterEdit = xWriterDoc.getChild("writer_edit") |
| 108 |
|
| 109 |
self.xUITest.executeCommand(".uno:Sidebar") |
| 110 |
xWriterEdit.executeAction("SIDEBAR", mkPropertyValues({"PANEL": "SwNavigatorPanel"})) |
| 111 |
|
| 112 |
xNavigatorPanel = xWriterEdit.getChild("NavigatorPanel") |
| 113 |
xToolBar = xNavigatorPanel.getChild("content5") |
| 114 |
xToolBar.executeAction("CLICK", mkPropertyValues({"POS": "0"})) # 'root' button |
| 115 |
|
| 116 |
# type "foo", and create 3 bookmarks on it |
| 117 |
|
| 118 |
self.xUITest.executeCommand(".uno:Escape") |
| 119 |
|
| 120 |
xDoc.Text.insertString(xDoc.Text.getStart(), "foo", False) |
| 121 |
self.xUITest.executeCommand(".uno:SelectAll") |
| 122 |
|
| 123 |
for i in range(3): |
| 124 |
with self.ui_test.execute_dialog_through_command(".uno:InsertBookmark", close_button="insert"): |
| 125 |
pass |
| 126 |
|
| 127 |
# check selected bookmarks in Navigator |
| 128 |
|
| 129 |
xWriterEdit.executeAction("FOCUS", tuple()) |
| 130 |
|
| 131 |
# disable flakey UITest |
| 132 |
# xContentTree = xNavigatorPanel.getChild("contenttree") |
| 133 |
|
| 134 |
# self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 1") |
| 135 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 1") |
| 136 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1") |
| 137 |
|
| 138 |
## self.xUITest.executeCommand(".uno:Escape") |
| 139 |
|
| 140 |
# # get the title of SwNavigatorPanel with emptying it to access to the selected bookmark |
| 141 |
# self.assertEqual(self.getTitle(xDoc), "Navigator") |
| 142 |
# # title was emptied |
| 143 |
# self.assertEqual(self.getTitle(xDoc), "") |
| 144 |
|
| 145 |
# # Select nested bookmark in Navigator |
| 146 |
|
| 147 |
# xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"})) |
| 148 |
|
| 149 |
# # This jumped to Bookmark 1 after selection |
| 150 |
# self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 1") |
| 151 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 1") |
| 152 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1") |
| 153 |
|
| 154 |
# # This was "Navigator" |
| 155 |
# self.assertEqual(self.getTitle(xDoc), "Bookmark 1") |
| 156 |
|
| 157 |
# # Try the same selection with Bookmark 2 |
| 158 |
# xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"})) |
| 159 |
# self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 2") |
| 160 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 2") |
| 161 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1") |
| 162 |
# xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"})) |
| 163 |
# self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 2") |
| 164 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 2") |
| 165 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1") |
| 166 |
|
| 167 |
# # This was "Navigator" |
| 168 |
# self.assertEqual(self.getTitle(xDoc), "Bookmark 2") |
| 169 |
|
| 170 |
# # Try the same selection with Bookmark 3 |
| 171 |
|
| 172 |
# # why we need this extra UP? |
| 173 |
# xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"})) |
| 174 |
# self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 2") |
| 175 |
# xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"})) |
| 176 |
# self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 3") |
| 177 |
|
| 178 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 3") |
| 179 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1") |
| 180 |
# xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"})) |
| 181 |
# self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 3") |
| 182 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmark 3") |
| 183 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectionCount"], "1") |
| 184 |
|
| 185 |
# # This was "Navigator" |
| 186 |
# self.assertEqual(self.getTitle(xDoc), "Bookmark 3") |
| 187 |
|
| 188 |
# # go to the previous item |
| 189 |
|
| 190 |
# # why we need this extra UP? |
| 191 |
# xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"})) |
| 192 |
# self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmark 3") |
| 193 |
# xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"})) |
| 194 |
# self.ui_test.wait_until_property_is_updated(xContentTree, "SelectEntryText", "Bookmarks") |
| 195 |
# self.assertEqual(get_state_as_dict(xContentTree)["SelectEntryText"], "Bookmarks") |
| 196 |
# xContentTree.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"})) |
| 197 |
|
| 198 |
# # This was "Navigator" |
| 199 |
# self.assertEqual(self.getTitle(xDoc), "") |
| 200 |
|
| 201 |
# self.xUITest.executeCommand(".uno:Sidebar") |
| 202 |
|
| 203 |
# vim: set shiftwidth=4 softtabstop=4 expandtab: |