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 from org
.libreoffice
.unotest
import UnoInProcess
14 class TestXTextRange(unittest
.TestCase
):
18 cls
._uno
= UnoInProcess()
20 cls
._uno
.openDocFromTDOC("xtextrange.odt")
23 def tearDownClass(cls
):
26 def test_textRangesInPlainText(self
):
27 xCursor
= self
._uno
.getDoc().getText().createTextCursor()
28 xCursor
.goRight(10, True)
30 self
.assertEqual(xTextRange
.getString(), "0123456789")
33 xTextRangeStart
= xTextRange
.getStart()
34 self
.assertIsNotNone(xTextRangeStart
)
35 self
.assertEqual(xTextRangeStart
.getString(), "")
36 xTextRangeStart
.setString("before")
37 self
.assertEqual(xTextRangeStart
.getString(), "before")
40 xTextRangeStart
.setString("beforebeforebeforebefore")
41 xTextRangeStart
.setString("before2")
42 xTextRangeStart
.setString("before3")
43 xTextRangeStart
.setString("before")
44 self
.assertEqual(xTextRangeStart
.getString(), "before")
45 xCursor
= self
._uno
.getDoc().getText().createTextCursor()
46 xCursor
.goRight(16, True)
47 self
.assertEqual(xCursor
.getString(), "before0123456789")
50 xTextRangeEnd
= xTextRange
.getEnd()
51 self
.assertIsNotNone(xTextRangeEnd
)
52 self
.assertEqual(xTextRangeEnd
.getString(), "")
53 xTextRangeEnd
.setString("after")
54 self
.assertEqual(xTextRangeEnd
.getString(), "after")
57 xText
= xTextRange
.getText()
58 self
.assertIsNotNone(xText
)
60 # Final check of what we have inserted
61 xCursor
= self
._uno
.getDoc().getText().createTextCursor()
62 xCursor
.goRight(21, True)
63 self
.assertEqual(xCursor
.getString(), "before0123456789after")
65 def test_textRangesInTable(self
):
66 xTextTables
= self
._uno
.getDoc().getTextTables()
67 self
.assertIsNotNone(xTextTables
)
68 xTextTable
= xTextTables
[0]
69 self
.assertIsNotNone(xTextTable
)
71 xTextRange
= xTextTable
.getCellByName("A1")
72 self
.assertIsNotNone(xTextRange
)
73 self
.assertEqual(xTextRange
.getString(), "C1")
75 xTextRangeStart
= xTextRange
.getStart()
76 self
.assertIsNotNone(xTextRangeStart
)
77 self
.assertEqual(xTextRangeStart
.getString(), "")
78 xTextRangeStart
.setString("before")
79 self
.assertEqual(xTextRangeStart
.getString(), "before")
81 xTextRangeEnd
= xTextRange
.getEnd()
82 self
.assertIsNotNone(xTextRangeEnd
)
83 self
.assertEqual(xTextRangeEnd
.getString(), "")
84 xTextRangeEnd
.setString("after")
85 self
.assertEqual(xTextRangeEnd
.getString(), "after")
87 # Ensure that what we inserted is in cell
88 xTextRange2
= xTextTable
.getCellByName("A1")
89 self
.assertEqual(xTextRange2
.getString(), "beforeC1after")
91 def test_textRangesCompare(self
):
92 doc
= self
._uno
.getDoc()
93 # Bookmark in body text
94 bookmark1
= doc
.getBookmarks()[0].getAnchor()
97 bookmark2
= doc
.getBookmarks()[1].getAnchor()
98 bookmark3
= doc
.getBookmarks()[2].getAnchor()
100 res
= doc
.Text
.compareRegionStarts(bookmark1
, bookmark2
)
101 self
.assertEqual(res
, 1)
103 res
= doc
.Text
.compareRegionStarts(bookmark2
, bookmark1
)
104 self
.assertEqual(res
, -1)
106 res
= doc
.Text
.compareRegionStarts(bookmark2
, bookmark3
)
107 self
.assertEqual(res
, 1)
109 res
= doc
.Text
.compareRegionStarts(bookmark1
, bookmark3
)
110 self
.assertEqual(res
, 1)
112 if __name__
== '__main__':
115 # vim: set shiftwidth=4 softtabstop=4 expandtab: