Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sw / qa / python / check_index.py
blob807f3e319754487154576da4075273d90880168e
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 unohelper
12 from org.libreoffice.unotest import UnoInProcess
13 from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
14 from com.sun.star.util import XRefreshListener
17 class RefreshListener(XRefreshListener, unohelper.Base):
18 def __init__(self):
19 self.m_refreshed = False
20 self.m_disposed = False
22 # Gets called when index is disposed
23 def disposing(self, event):
24 self.m_disposed = True
26 # Gets called when index is refreshed
27 def refreshed(self, event):
28 self.m_refreshed = True
31 class CheckIndex(unittest.TestCase):
32 @classmethod
33 def setUpClass(cls):
34 cls._uno = UnoInProcess()
35 cls._uno.setUp()
37 @classmethod
38 def tearDownClass(cls):
39 cls._uno.tearDown()
41 # Gets called every time a new test is run
42 def setUp(self):
43 """
44 Every test should run with a new Instance of an Index and
45 Refresh Listener
46 So we need to reload the text of the document and initialize
47 the corresponding Cursor
48 """
49 self.xDoc = self.__class__._uno.openEmptyWriterDoc()
50 self.xIndex = self.xDoc.createInstance(
51 "com.sun.star.text.ContentIndex")
52 self.xBodyText = self.xDoc.getText()
53 self.xCursor = self.xBodyText.createTextCursor()
54 self.xIndex.setPropertyValue("CreateFromOutline", True)
55 self.xBodyText.insertTextContent(self.xCursor, self.xIndex, True)
56 # Create Refresh Listener and bind it to the Index
57 self.listener = RefreshListener()
58 self.xIndex.addRefreshListener(self.listener)
60 def tearDown(self):
61 """
62 Dispose Index and Document and check if the index was
63 deleted while the tests
64 """
65 self.assertFalse(self.listener.m_disposed,
66 "Unexpected disparue of the Refresh Listener!")
67 self.xIndex.dispose()
68 self.assertTrue(self.listener.m_disposed,
69 "Could not dispose Refresh Listener")
70 self.xDoc.dispose()
72 def insert_heading(self, text):
73 """
74 Insert a Heading at the end of the document
75 """
76 self.xCursor.gotoEnd(False)
77 self.xBodyText.insertControlCharacter(self.xCursor,
78 PARAGRAPH_BREAK, False)
79 self.xCursor.gotoEnd(False)
80 self.xCursor.setString(text)
81 self.xCursor.gotoStartOfParagraph(True)
82 self.xCursor.setPropertyValue("ParaStyleName", "Heading 1")
84 def test_index_refresh(self):
85 """
86 Try to insert a heading at the index, refresh the index and
87 retrieve the heading again
88 """
89 heading = "The best test heading you have seen in your entire life"
90 self.insert_heading(heading)
91 self.xIndex.refresh()
92 self.assertTrue(self.listener.m_refreshed, "Failed to refresh index!")
93 self.listener.m_refreshed = False
94 self.xCursor.gotoRange(self.xIndex.getAnchor().getEnd(), False)
95 self.xCursor.gotoStartOfParagraph(True)
96 # Get String at current position and search for the heading
97 text = self.xCursor.getString()
98 self.assertGreaterEqual(text.find(heading), 0,
99 "Failed to insert heading at index "
100 "and retrieve it again!")
102 def test_index_update(self):
104 Try to insert a heading at the index, update the index
105 and retrieve the heading again
107 heading = "Heading to test the index update"
108 self.insert_heading(heading)
109 self.xIndex.update()
110 self.assertTrue(self.listener.m_refreshed, "Failed to update index!")
111 self.listener.m_refreshed = False
112 self.xCursor.gotoRange(self.xIndex.getAnchor().getEnd(), False)
113 self.xCursor.gotoStartOfParagraph(True)
114 # Get String at current position and search for the heading
115 text = self.xCursor.getString()
116 self.assertGreaterEqual(text.find(heading), 0,
117 "Failed to insert a heading at Index and "
118 "retrieve it again!")
121 if __name__ == "__main__":
122 unittest.main()