Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / qa / python / var_fields.py
blob0c88b5357732fc948880c22c494692103527c92a
1 #! /usr/bin/env python
2 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
4 # This file is part of the LibreOffice project.
6 # This Source Code Form is subject to the terms of the Mozilla Public
7 # License, v. 2.0. If a copy of the MPL was not distributed with this
8 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 import unittest
12 import os
14 from org.libreoffice.unotest import UnoInProcess
15 from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
17 class TestVarFields(unittest.TestCase):
19 @classmethod
20 def setUpClass(cls):
21 cls._uno = UnoInProcess()
22 cls._uno.setUp()
24 @classmethod
25 def tearDownClass(cls):
26 cls._uno.tearDown()
28 def test_var_fields(self):
29 """
30 Reproduce fdo#55814.
32 Note: this test was migrated from java (the steps numbering too)
33 sw/qa/complex/writer/VarFields.java
35 """
37 xDoc = self.__class__._uno.openEmptyWriterDoc()
38 xBodyText = xDoc.getText()
39 xCursor = xBodyText.createTextCursor()
41 # 0. create text field
42 xField = xDoc.createInstance("com.sun.star.text.textfield.SetExpression")
44 # 1. fill it with properties
45 self.__class__._uno.setProperties(xField,
46 {"Content": "0",
47 "IsVisible": True,
48 "Hint": "trying to reproduce fdo#55814",
49 "SubType": 0, # VAR
50 "Value": 0.0})
52 # 2. create master field
53 xMaster = xDoc.createInstance("com.sun.star.text.fieldmaster.SetExpression")
55 # 3. set name of the master field to "foo"
56 xMaster.setPropertyValue("Name", "foo")
58 # 4. get Dependent Field
59 # no op in python ;-)
61 # 5. connect real field to the master
62 xField.attachTextFieldMaster(xMaster)
64 # 6. insert text field into the document
65 xBodyText.insertTextContent(xCursor, xField, False)
67 # 7. retrieve paragraph cursor
68 xParagraphCursor = xCursor
69 xParagraphCursor.gotoEndOfParagraph(False) # not selected
71 # 8. enter new line
72 xBodyText.insertControlCharacter(xCursor, PARAGRAPH_BREAK, False)
74 # 9. create new text section
75 xTextSection = xDoc.createInstance("com.sun.star.text.TextSection")
77 # 10. fill the properties of section
78 self.__class__._uno.checkProperties(xTextSection,
79 {"Condition": "foo EQ 1",
80 "IsVisible": False},
81 self)
83 # 11. Insert some text to be content on the section
84 xBodyText.insertString(xCursor,
85 "The quick brown fox jumps over the lazy dog",
86 True)
88 # 12. insert section
89 xBodyText.insertTextContent(xCursor, xTextSection, True)
91 # 12.1 insert new paragraph. Note: that's here the difference
92 xParagraphCursor.gotoEndOfParagraph(False) # not select
94 # TODO: how to leave the section now?
95 xBodyText.insertControlCharacter(xCursor, PARAGRAPH_BREAK, False)
96 xBodyText.insertString(xCursor, "new paragraph", False)
98 # 13. Access fields to refresh the document
99 xTextFields = xDoc.getTextFields()
101 # 14. refresh document to update the fields
102 xTextFields.refresh()
104 # 15. retrieve the field
105 xFieldEnum = xTextFields.createEnumeration()
107 # Note: we have only one field here, that why nextElement() is just fine here
108 xField = xFieldEnum.nextElement()
110 # check
111 read_content = xField.getPropertyValue("Content")
112 self.assertEqual("0", read_content)
113 read_content = xField.getPropertyValue("Value")
114 self.assertEqual(0.0, read_content)
116 # 16. change the value of the field from 0 to 1 and check
117 self.__class__._uno.checkProperties(xField, {"Value": 1.0,
118 "Content": "1"},
119 self)
121 # 17. refresh document to update the fields again
122 xTextFields.refresh()
124 # 18. store document
125 url = os.path.join(os.environ["TestUserDir"], "VarFields.odt")
126 xDoc.storeToURL(url, tuple(list(range(0))))
128 # 19. retrieve the section
129 xSection = xDoc.getTextSections()[0]
131 # 20. retrieve the condition property of that section
132 read_content = xSection.getPropertyValue("Condition")
134 # 21. check
135 self.assertEqual("foo EQ 1", read_content)
137 if __name__ == '__main__':
138 unittest.main()