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/.
13 from org
.libreoffice
.unotest
import UnoInProcess
15 class TestXText(unittest
.TestCase
):
19 cls
._uno
= UnoInProcess()
21 cls
._uno
.openEmptyWriterDoc()
24 def tearDownClass(cls
):
27 def test_insert_and_remove_annotations(self
):
28 x_text
= self
._uno
.getDoc().getText()
29 self
.assertIsNotNone(x_text
)
31 # Insert annotation field
32 x_annotation
= self
.create_annotation("John Doe")
33 x_cursor
= x_text
.createTextCursor()
34 x_text
.insertTextContent(x_cursor
, x_annotation
, False)
36 # And the same once again, actually not inserted
37 x_text
.insertTextContent(x_cursor
, x_annotation
, False)
39 # no exception if we try to replace object by itself:
40 # this did throw in the past, but only because the inserted
41 # UNO annotation had a core object assigned, but no document
42 # which insertTextContent then didn't like on another call.
43 x_text
.insertTextContent(x_cursor
, x_annotation
, True)
45 # We expect just one annotation actually
46 self
.check_annotations(["John Doe"])
48 x_annotation_2
= self
.create_annotation("Richard Roe")
49 x_text
.insertTextContent(x_cursor
, x_annotation_2
, True)
50 self
.check_annotations(["Richard Roe"])
52 x_annotation_3
= self
.create_annotation("Jane Roe")
53 x_text
.insertTextContent(x_cursor
, x_annotation_3
, True)
54 self
.check_annotations(["Jane Roe", "Richard Roe"])
57 x_text
.removeTextContent(x_annotation_3
)
58 self
.check_annotations(["Richard Roe"])
59 x_text
.removeTextContent(x_annotation_2
)
60 self
.check_annotations([])
62 # Remove _already removed_ ones again
63 # TODO: unexpected behaviour, it should throw an exception,
64 # but let's nail down current behaviour
65 # NOTE: reported as tdf#123404
66 x_text
.removeTextContent(x_annotation_2
)
67 x_text
.removeTextContent(x_annotation
)
69 self
.check_annotations([])
71 def create_annotation(self
, author
):
72 x_annotation
= self
._uno
.getDoc().createInstance("com.sun.star.text.TextField.Annotation")
73 self
.assertIsNotNone(x_annotation
)
74 x_annotation
.setPropertyValue("Author", author
)
77 def check_annotations(self
, authors
):
78 x_fields_enum
= self
._uno
.getDoc().getTextFields().createEnumeration()
82 for x_field
, author
in zip(x_fields_enum
, authors
):
83 self
.assertTrue(x_field
.supportsService("com.sun.star.text.TextField.Annotation"))
84 self
.assertEqual(x_field
.getPropertyValue("Author"), author
)
85 annotations_found
+= 1
87 self
.assertEqual(annotations_found
, len(authors
))
89 if __name__
== '__main__':
92 # vim: set shiftwidth=4 softtabstop=4 expandtab: