Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / qa / uitest / calc_tests9 / tdf156611_hyperlink_interoper.py
blob87ee09aa5d71221126e35c69aaa10d7786c5c14d
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 from uitest.framework import UITestCase
11 from uitest.uihelper.common import get_url_for_data_file
12 from libreoffice.uno.propertyvalue import mkPropertyValues
13 from libreoffice.calc.document import get_cell_by_position
14 from uitest.uihelper.calc import enter_text_to_cell
15 from uitest.uihelper.common import get_state_as_dict, select_pos
17 class tdf156611(UITestCase):
18 def test_tdf156611_insert_hyperlink_like_excel(self):
19 # The hyperlink interoperability setting, that this test is testing,
20 # works only if MS document type is opened.
21 # but it does not need any data from the file, any xlsx or xls file can be opened for this test
22 with self.ui_test.load_file(get_url_for_data_file("tdf126541_GridOff.xlsx")) as document:
24 # data that we will check against when hyperlink is inserted
25 urls =[["",""],["https://www.documentfoundation.org/",""]]
26 texts =[["aaa bbb","bbb"],["cccc ddd","ddd"],["eeee","aaa cccc eeee"]]
28 # 1. run, we want hyperlink insertion work like in MS excel (only 1 hyperlink/cell is allowed)
29 # 2. run, we want hyperlink insertion work as it did in calc (more hyperlinks can be in 1 cell)
30 for i in range(2):
31 xCalcDoc = self.xUITest.getTopFocusWindow()
32 xGridWindow = xCalcDoc.getChild("grid_window")
34 #Change hyperlink interoperability setting
35 #Go to Tools -> Options -> LibreofficeDev Calc -> Compatibility
36 with self.ui_test.execute_dialog_through_command(".uno:OptionsTreeDialog", close_button="cancel") as xDialogOpt:
38 xPages = xDialogOpt.getChild("pages")
39 xChartEntry = xPages.getChild('3') # LibreofficeDev Calc
40 xChartEntry.executeAction("EXPAND", tuple())
41 xChartGeneralEntry = xChartEntry.getChild('7')
42 xChartGeneralEntry.executeAction("SELECT", tuple()) #Compatibility
44 xLinks = xDialogOpt.getChild("cellLinkCB")
45 xLinks.executeAction("CLICK", tuple())
46 xApply = xDialogOpt.getChild("apply")
47 xApply.executeAction("CLICK", tuple())
49 enter_text_to_cell(xGridWindow, "A1", "aaa bbb")
51 # Select last word of the cell text: "bbb"
52 xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": "A1"}))
53 xGridWindow.executeAction("TYPE", mkPropertyValues({"KEYCODE":"F2"}))
54 xGridWindow.executeAction("TYPE", mkPropertyValues({"KEYCODE":"SHIFT+CTRL+LEFT"}))
56 # Insert hyperlink
57 with self.ui_test.execute_dialog_through_command(".uno:HyperlinkDialog") as xDialog:
58 xTab = xDialog.getChild("tabcontrol")
59 select_pos(xTab, "0")
61 xTarget = xDialog.getChild("target")
62 self.assertEqual(get_state_as_dict(xTarget)["Text"], "")
63 xIndication = xDialog.getChild("indication")
64 self.assertEqual(get_state_as_dict(xIndication)["Text"], texts[0][i])
65 # 1. run "aaa bbb" The whole cell text
66 # 2. run "bbb" Only the selected text
68 # Insert a sample hyperlink, and change text
69 xTarget.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
70 xTarget.executeAction("TYPE", mkPropertyValues({"TEXT": "https://www.documentfoundation.org/"}))
71 xIndication.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
72 xIndication.executeAction("TYPE", mkPropertyValues({"TEXT": "cccc"}))
74 # Edit cell text: insert " ddd" in the end
75 xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": "A2"}))
76 xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": "A1"}))
77 xGridWindow.executeAction("TYPE", mkPropertyValues({"KEYCODE":"F2"}))
78 xGridWindow.executeAction("TYPE", mkPropertyValues({"TEXT": " ddd"}))
79 xGridWindow.executeAction("TYPE", mkPropertyValues({"KEYCODE":"RETURN"}))
81 # Select the last word of cell text: "ddd"
82 xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": "A2"}))
83 xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": "A1"}))
84 xGridWindow.executeAction("TYPE", mkPropertyValues({"KEYCODE":"F2"}))
85 xGridWindow.executeAction("TYPE", mkPropertyValues({"KEYCODE":"SHIFT+CTRL+LEFT"}))
87 # Insert hyperlink
88 with self.ui_test.execute_dialog_through_command(".uno:HyperlinkDialog") as xDialog2:
89 xTab = xDialog2.getChild("tabcontrol")
90 select_pos(xTab, "0")
92 xTarget = xDialog2.getChild("target")
93 self.assertEqual(get_state_as_dict(xTarget)["Text"], urls[1][i])
94 # 1. run: "https://www.documentfoundation.org/" the cell already have this url.
95 # 2. run: "" The selected text is not a hyperlink yet.
96 xIndication = xDialog2.getChild("indication")
97 self.assertEqual(get_state_as_dict(xIndication)["Text"], texts[1][i])
98 # 1. run: "cccc ddd" The whole cell text
99 # 2. run: "ddd" Only the selected text
101 # Insert a sample hyperlink, and change text
102 xTarget.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
103 xTarget.executeAction("TYPE", mkPropertyValues({"TEXT": "https://aWrongLink/"}))
104 xIndication.executeAction("TYPE", mkPropertyValues({"KEYCODE":"CTRL+A"}))
105 xIndication.executeAction("TYPE", mkPropertyValues({"TEXT": "eeee"}))
107 # Move focus to ensure cell is not in edit mode
108 xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": "A2"}))
109 xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": "A1"}))
111 # Check contents of the cell
112 xCell = get_cell_by_position(document, 0, 0, 0)
113 self.assertEqual(xCell.getString(), texts[2][i])
114 # 1. run: "eeee" last hyperlink insertion overwritten the whole cell text with "eeee"
115 # 2. run: "aaa cccc eeee" as every hyperlink insertion only overwritten the actually selected text
116 xTextFields = xCell.getTextFields()
117 self.assertEqual(xTextFields.getCount(), i+1)
118 self.assertEqual(xTextFields.getByIndex(i).URL, "https://aWrongLink/")
119 if (i==1):
120 self.assertEqual(xTextFields.getByIndex(0).URL, "https://www.documentfoundation.org/")
121 # 1. run: only the last inserted hyperlink will remain: "https://aWrongLink/"
122 # 2. run: both links will be in the cell
124 # vim: set shiftwidth=4 softtabstop=4 expandtab: