Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sw / qa / python / var_fields.py
blobc2af7a40f408ae067562a6ee44fb1613339db091
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/.
10 import unittest
11 import os
12 from org.libreoffice.unotest import UnoInProcess
13 from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
16 class TestVarFields(unittest.TestCase):
18 @classmethod
19 def setUpClass(cls):
20 cls._uno = UnoInProcess()
21 cls._uno.setUp()
22 cls._xDoc = cls._uno.openEmptyWriterDoc()
24 @classmethod
25 def tearDownClass(cls):
26 cls._uno.tearDown()
28 def test_var_fields(self):
29 """Reproduce fdo#55814.
31 Note: this test was migrated from java (the steps numbering too)
32 sw/qa/complex/writer/VarFields.java
34 """
35 xDoc = self.__class__._xDoc
36 xBodyText = xDoc.getText()
37 xCursor = xBodyText.createTextCursor()
38 # 0. create text field
39 xField = xDoc.createInstance("com.sun.star.text.textfield.SetExpression")
40 # 1. fill it with properties
41 self.__class__._uno.setProperties(xField,
42 {"Content": "0",
43 "IsVisible": True,
44 "Hint": "trying to reproduce fdo#55814",
45 "SubType": 0, # VAR
46 "Value": 0.0})
47 # 2. create master field
48 xMaster = xDoc.createInstance("com.sun.star.text.fieldmaster.SetExpression")
49 # 3. set name of the master field to "foo"
50 xMaster.setPropertyValue("Name", "foo")
51 # 4. get Dependent Field
52 # no op in python ;-)
53 # 5. connect real field to the master
54 xField.attachTextFieldMaster(xMaster)
55 # 6. insert text field into the document
56 xBodyText.insertTextContent(xCursor, xField, False)
57 # 7. retrieve paragraph cursor
58 xParagraphCursor = xCursor
59 xParagraphCursor.gotoEndOfParagraph(False) # not selectd
60 # 8. enter new line
61 xBodyText.insertControlCharacter(xCursor, PARAGRAPH_BREAK, False)
62 # 9. create new text section
63 xTextSection = xDoc.createInstance("com.sun.star.text.TextSection")
64 # 10. fill the properties of section
65 self.__class__._uno.checkProperties(
66 xTextSection,
67 {"Condition": "foo EQ 1",
68 "IsVisible": False,
70 self
72 # 11. Insert some text to be content on the section
73 xBodyText.insertString(xCursor,
74 "The quick brown fox jumps over the lazy dog",
75 True)
76 # 12. insert section
77 xBodyText.insertTextContent(xCursor, xTextSection, True)
78 # 12.1 insert new paragraph. Note: that's here the difference
79 xParagraphCursor.gotoEndOfParagraph(False) # not select
80 # TODO: how to leave the section now?
81 xBodyText.insertControlCharacter(xCursor, PARAGRAPH_BREAK, False)
82 xBodyText.insertString(xCursor, "new paragraph", False)
83 # 13. Access fields to refresh the document
84 xTextFields = xDoc.getTextFields()
85 # 14. refresh document to update the fields
86 xTextFields.refresh()
87 # 15. retrieve the field
88 xFieldEnum = xTextFields.createEnumeration()
89 # Note: we have only one field here, that why nextElement() is just fine here
90 xField = xFieldEnum.nextElement()
91 # check
92 read_content = xField.getPropertyValue("Content")
93 self.assertEqual("0", read_content)
94 read_content = xField.getPropertyValue("Value")
95 self.assertEqual(0.0, read_content)
96 # 16. change the value of the field from 0 to 1 and check
97 self.__class__._uno.checkProperties(
98 xField,
99 {"Value": 1.0,
100 "Content": "1"
102 self
104 # 17. refresh document to update the fields again
105 xTextFields.refresh()
106 # 18. store document
107 url = os.path.join(os.environ["TestUserDir"], "VarFields.odt")
108 xDoc.storeToURL(url, tuple(list(range(0))))
109 # 19. retrieve the section
110 xSection = xDoc.getTextSections().getByIndex(0)
111 # 20. retrieve the condition property of that section
112 read_content = xSection.getPropertyValue("Condition")
113 # 21. check
114 # expected:
115 # self.assertEqual("foo EQ 1", readContent)
116 # reality:
117 self.assertEqual("0", read_content)
119 if __name__ == '__main__':
120 unittest.main()