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/.
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
):
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
):
34 cls
._uno
= UnoInProcess()
38 def tearDownClass(cls
):
41 # Gets called every time a new test is run
44 Every test should run with a new Instance of an Index and
46 So we need to reload the text of the document and initialize
47 the corresponding Cursor
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
)
62 Dispose Index and Document and check if the index was
63 deleted while the tests
65 self
.assertFalse(self
.listener
.m_disposed
,
66 "Unexpected disparue of the Refresh Listener!")
68 self
.assertTrue(self
.listener
.m_disposed
,
69 "Could not dispose Refresh Listener")
72 def insert_heading(self
, text
):
74 Insert a Heading at the end of the document
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
):
86 Try to insert a heading at the index, refresh the index and
87 retrieve the heading again
89 heading
= "The best test heading you have seen in your entire life"
90 self
.insert_heading(heading
)
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
)
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__":