summaryrefslogtreecommitdiffstats
path: root/sw/qa/python/xtextrange.py
blob: 583cabc30abaa9bf8a75dd31a84009f7b2506dfd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#! /usr/bin/env python
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
import unittest
from org.libreoffice.unotest import UnoInProcess


class TestXTextRange(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        cls._uno = UnoInProcess()
        cls._uno.setUp()
        cls._uno.openDocFromTDOC("xtextrange.odt")

    @classmethod
    def tearDownClass(cls):
        cls._uno.tearDown()

    def test_textRangesInPlainText(self):
        xCursor = self._uno.getDoc().getText().createTextCursor()
        xCursor.goRight(10, True)
        xTextRange = xCursor
        self.assertEqual(xTextRange.getString(), "0123456789")

        # getStart
        xTextRangeStart = xTextRange.getStart()
        self.assertIsNotNone(xTextRangeStart)
        self.assertEqual(xTextRangeStart.getString(), "")
        xTextRangeStart.setString("before")
        self.assertEqual(xTextRangeStart.getString(), "before")

        # Overwrite strings
        xTextRangeStart.setString("beforebeforebeforebefore")
        xTextRangeStart.setString("before2")
        xTextRangeStart.setString("before3")
        xTextRangeStart.setString("before")
        self.assertEqual(xTextRangeStart.getString(), "before")
        xCursor = self._uno.getDoc().getText().createTextCursor()
        xCursor.goRight(16, True)
        self.assertEqual(xCursor.getString(), "before0123456789")

        # getEnd
        xTextRangeEnd = xTextRange.getEnd()
        self.assertIsNotNone(xTextRangeEnd)
        self.assertEqual(xTextRangeEnd.getString(), "")
        xTextRangeEnd.setString("after")
        self.assertEqual(xTextRangeEnd.getString(), "after")

        # getText
        xText = xTextRange.getText()
        self.assertIsNotNone(xText)

        # Final check of what we have inserted
        xCursor = self._uno.getDoc().getText().createTextCursor()
        xCursor.goRight(21, True)
        self.assertEqual(xCursor.getString(), "before0123456789after")

    def test_textRangesInTable(self):
        xTextTables = self._uno.getDoc().getTextTables()
        self.assertIsNotNone(xTextTables)
        xTextTable = xTextTables[0]
        self.assertIsNotNone(xTextTable)

        xTextRange = xTextTable.getCellByName("A1")
        self.assertIsNotNone(xTextRange)
        self.assertEqual(xTextRange.getString(), "C1")

        xTextRangeStart = xTextRange.getStart()
        self.assertIsNotNone(xTextRangeStart)
        self.assertEqual(xTextRangeStart.getString(), "")
        xTextRangeStart.setString("before")
        self.assertEqual(xTextRangeStart.getString(), "before")

        xTextRangeEnd = xTextRange.getEnd()
        self.assertIsNotNone(xTextRangeEnd)
        self.assertEqual(xTextRangeEnd.getString(), "")
        xTextRangeEnd.setString("after")
        self.assertEqual(xTextRangeEnd.getString(), "after")

        # Ensure that what we inserted is in cell
        xTextRange2 = xTextTable.getCellByName("A1")
        self.assertEqual(xTextRange2.getString(), "beforeC1after")

    def test_textRangesCompare(self):
        doc = self._uno.getDoc()
        # Bookmark in body text
        bookmark1 = doc.getBookmarks().getByIndex(0).getAnchor()

        # Bookmarks in table
        bookmark2 = doc.getBookmarks().getByIndex(1).getAnchor()
        bookmark3 = doc.getBookmarks().getByIndex(2).getAnchor()

        res = doc.Text.compareRegionStarts(bookmark1, bookmark2)
        self.assertEqual(res, 1)

        res = doc.Text.compareRegionStarts(bookmark2, bookmark1)
        self.assertEqual(res, -1)

        res = doc.Text.compareRegionStarts(bookmark2, bookmark3)
        self.assertEqual(res, 1)

        res = doc.Text.compareRegionStarts(bookmark1, bookmark3)
        self.assertEqual(res, 1)

if __name__ == '__main__':
    unittest.main()

# vim: set shiftwidth=4 softtabstop=4 expandtab: