summaryrefslogtreecommitdiffstats
path: root/sw/qa/python/check_index.py
blob: 4d3076c43b9a82b9bae0a1886f19d8174b4762c9 (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
import unittest
import unohelper
import os
from org.libreoffice.unotest import UnoInProcess
from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
from com.sun.star.util import XRefreshListener

class RefreshListener(XRefreshListener, unohelper.Base):
 
    def __init__(self):
        self.m_bDisposed = False
        self.m_bRefreshed = False

    def disposing(self, event):
        self.m_bDisposed = True

    def refreshed(self, event):
        self.m_bRefreshed = True
    
    def assertRefreshed(self):
        assert(self.m_bRefreshed)
        self.m_bRefreshed = False
    
class CheckIndex(unittest.TestCase):
    _uno = None
    _xDoc = None

    @classmethod
    def setUpClass(cls):
        cls._uno = UnoInProcess()
        cls._uno.setUp()
        cls._xDoc = cls._uno.openEmptyWriterDoc()

    @classmethod
    def tearDownClass(cls):
        cls._uno.tearDown() 
   
    def test_check_index(self):
        xDoc = self.__class__._xDoc
        xIndex = xDoc.createInstance("com.sun.star.text.ContentIndex")
        xBodyText = xDoc.getText()
        xCursor = xBodyText.createTextCursor()
        xIndex.setPropertyValue("CreateFromOutline", True)
        xBodyText.insertTextContent(xCursor, xIndex, True)

    # register listener
        listener = RefreshListener()
        xIndex.addRefreshListener(listener)
        self.assertFalse(listener.m_bRefreshed)
        xIndex.refresh()
        listener.assertRefreshed()

    # insert some heading
        xCursor.gotoEnd(False)
        xBodyText.insertControlCharacter(xCursor, PARAGRAPH_BREAK, False)
        xCursor.gotoEnd(False)
        test_string = "a heading"
        xCursor.setString(test_string)
        xCursor.gotoStartOfParagraph(True)
        xCursor.setPropertyValue("ParaStyleName","Heading 1")

    # hope text is in last paragraph...
        xIndex.refresh()
        listener.assertRefreshed()
        xCursor.gotoRange(xIndex.getAnchor().getEnd(), False)
        xCursor.gotoStartOfParagraph(True)
        text = xCursor.getString()
    # check if we got text with 'test_string'
        assert( text.find(test_string) >= 0 )

    # insert some more heading
        xCursor.gotoEnd(False)
        xBodyText.insertControlCharacter(xCursor, PARAGRAPH_BREAK, False)
        xCursor.gotoEnd(False)
        test_string = "yet another heading"
        xCursor.setString(test_string)
        xCursor.gotoStartOfParagraph(True)
        xCursor.setPropertyValue("ParaStyleName","Heading 1")

    # try again with update
        xIndex.update()
        listener.assertRefreshed()
        xCursor.gotoRange(xIndex.getAnchor().getEnd(), False)
        xCursor.gotoStartOfParagraph(True)
        text = xCursor.getString()
    # check if we got text with 'test_string'
        assert( text.find(test_string) >= 0 )
     
    # dispose must call the listener
        assert(not listener.m_bDisposed)
        xIndex.dispose()
        assert(listener.m_bDisposed)
    
    # close the document
        xDoc.dispose()
if __name__ == "__main__":
    unittest.main()