diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:06:44 +0000 |
commit | ed5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch) | |
tree | 7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /sw/qa/python/xtextrange.py | |
parent | Initial commit. (diff) | |
download | libreoffice-upstream/4%7.4.7.tar.xz libreoffice-upstream/4%7.4.7.zip |
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'sw/qa/python/xtextrange.py')
-rw-r--r-- | sw/qa/python/xtextrange.py | 115 |
1 files changed, 115 insertions, 0 deletions
diff --git a/sw/qa/python/xtextrange.py b/sw/qa/python/xtextrange.py new file mode 100644 index 000000000..583cabc30 --- /dev/null +++ b/sw/qa/python/xtextrange.py @@ -0,0 +1,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: |