Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / uitest / libreoffice / uno / eventlistener.py
blob2dbdd68f086e359c72b697f078ebd21f01bb44a7
1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 try:
9 import pyuno
10 import uno
11 import unohelper
12 from com.sun.star.document import XDocumentEventListener
13 except ImportError:
14 print("pyuno not found: try to set PYTHONPATH and URE_BOOTSTRAP variables")
15 print("PYTHONPATH=/installation/opt/program")
16 print("URE_BOOTSTRAP=file:///installation/opt/program/fundamentalrc")
17 raise
19 class EventListener(XDocumentEventListener,unohelper.Base):
21 def __init__(self, xContext, eventNames, **kwargs):
22 self.xGEB = xContext.ServiceManager.createInstanceWithContext(
23 "com.sun.star.frame.GlobalEventBroadcaster", xContext)
24 self.xContext = xContext
25 self.executed = False
26 self.eventExecuted = []
27 self.printEvents = kwargs.get('printNames', False)
28 if isinstance(eventNames, str):
29 self.eventNames = [eventNames]
30 elif isinstance(eventNames, list):
31 self.eventNames = eventNames
33 def __enter__(self):
34 self.xGEB.addDocumentEventListener(self)
35 return self
37 def __exit__(self, type, value, traceback):
38 self.xGEB.removeDocumentEventListener(self)
40 def documentEventOccured(self, event):
41 if self.printEvents is True:
42 print(event.EventName)
44 if event.EventName in self.eventNames:
45 self.executed = True
46 self.eventExecuted.append(event.EventName)
48 def hasExecuted(self, eventName):
49 return eventName in self.eventExecuted
51 def disposing(event):
52 pass
54 # vim: set shiftwidth=4 softtabstop=4 expandtab: