Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / uitest / libreoffice / uno / eventlistener.py
blob54076efe337eef0f75b5fd32574569ea6a16d660
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 unohelper
10 from com.sun.star.document import XDocumentEventListener
11 except ImportError:
12 print("pyuno not found: try to set PYTHONPATH and URE_BOOTSTRAP variables")
13 print("PYTHONPATH=/installation/opt/program")
14 print("URE_BOOTSTRAP=file:///installation/opt/program/fundamentalrc")
15 raise
17 class EventListener(XDocumentEventListener,unohelper.Base):
19 def __init__(self, xContext, eventNames, **kwargs):
20 self.xGEB = xContext.ServiceManager.createInstanceWithContext(
21 "com.sun.star.frame.GlobalEventBroadcaster", xContext)
22 self.xContext = xContext
23 self.executed = False
24 self.eventExecuted = []
25 self.printEvents = kwargs.get('printNames', False)
26 if isinstance(eventNames, str):
27 self.eventNames = [eventNames]
28 elif isinstance(eventNames, list):
29 self.eventNames = eventNames
31 def __enter__(self):
32 self.xGEB.addDocumentEventListener(self)
33 return self
35 def __exit__(self, type, value, traceback):
36 self.xGEB.removeDocumentEventListener(self)
38 def documentEventOccured(self, event):
39 if self.printEvents is True:
40 print(event.EventName)
42 if event.EventName in self.eventNames:
43 self.executed = True
44 self.eventExecuted.append(event.EventName)
45 else:
46 print(self.eventNames)
47 print(event.EventName)
49 def hasExecuted(self, eventName):
50 return eventName in self.eventExecuted
52 def disposing(event):
53 pass
55 # vim: set shiftwidth=4 softtabstop=4 expandtab: