1 # -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
3 # This file is part of the LibreOffice project.
5 # This Source Code Form is subject to the terms of the Mozilla Public
6 # License, v. 2.0. If a copy of the MPL was not distributed with this
7 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 from uitest
.framework
import UITestCase
11 from com
.sun
.star
.awt
import XMouseListener
12 from com
.sun
.star
.awt
import MouseButton
13 from com
.sun
.star
.awt
import MouseEvent
14 from com
.sun
.star
.awt
import KeyEvent
15 from com
.sun
.star
.awt
import XKeyListener
18 mouseListenerCount
= 0
19 mouseEventsIntercepted
= 0
20 mousePressedEventsIntercepted
= 0
21 mouseReleasedEventsIntercepted
= 0
22 mouseEnteredEventsIntercepted
= 0
23 mouseExitedEventsIntercepted
= 0
24 keymousePressedEventsIntercepted
= 0
25 keymouseReleasedEventsIntercepted
= 0
28 class XMouseListenerExtended(unohelper
.Base
, XMouseListener
):
30 global mouseListenerCount
31 mouseListenerCount
+= 1
34 # is invoked when a mouse button has been pressed on a window.
36 def mousePressed(self
, xMouseEvent
):
37 global mousePressedEventsIntercepted
38 mousePressedEventsIntercepted
+= 1
40 # is invoked when a mouse button has been released on a window.
42 def mouseReleased(self
, xMouseEvent
):
43 global mouseReleasedEventsIntercepted
44 mouseReleasedEventsIntercepted
+= 1
46 # is invoked when the mouse enters a window.
48 def mouseEntered(self
, xMouseEvent
):
49 global mouseEventsIntercepted
50 mouseEventsIntercepted
+= 1
51 return super(XMouseListenerExtended
, self
).mouseEntered(xMouseEvent
)
53 # is invoked when the mouse exits a window.
55 def mouseExited(self
, xMouseEvent
):
56 global mouseEventsIntercepted
57 mouseEventsIntercepted
+= 1
58 return super(XMouseListenerExtended
, self
).mouseExited(xMouseEvent
)
61 class XKeyListenerExtended(unohelper
.Base
, XKeyListener
):
62 # is invoked when a key has been pressed
64 def keyPressed(self
, xKeyEvent
):
65 global keymousePressedEventsIntercepted
66 keymousePressedEventsIntercepted
+= 1
67 return super(XKeyListenerExtended
, self
).keyPressed(xKeyEvent
)
69 # is invoked when a key has been released
71 def keyReleased(self
, xKeyEvent
):
72 global keymouseReleasedEventsIntercepted
73 keymouseReleasedEventsIntercepted
+= 1
74 return super(XKeyListenerExtended
, self
).keyReleased(xKeyEvent
)
76 # Test that registered mouse/key listeners for top window receive mouse/key events
77 class XWindow(UITestCase
):
78 def test_listeners(self
):
79 global mouseListenerCount
81 with self
.ui_test
.create_doc_in_start_center("writer") as xDoc
:
83 # create new mouse listener
84 xFrame
= xDoc
.getCurrentController().getFrame()
85 self
.assertIsNotNone(xFrame
)
86 xWindow
= xFrame
.getContainerWindow()
87 self
.assertIsNotNone(xWindow
)
89 # add new mouse listener
90 xMouseListener
= XMouseListenerExtended()
91 self
.assertIsNotNone(xMouseListener
)
92 xWindow
.addMouseListener(xMouseListener
)
93 self
.assertEqual(1, mouseListenerCount
)
95 # add new key listener
96 xKeyListener
= XKeyListenerExtended()
97 self
.assertIsNotNone(xKeyListener
)
98 xWindow
.addKeyListener(xKeyListener
)
100 # create dummy mouse event
101 xMouseEvent
= MouseEvent()
102 xMouseEvent
.Modifiers
= 0
103 xMouseEvent
.Buttons
= MouseButton
.LEFT
106 xMouseEvent
.ClickCount
= 1
107 xMouseEvent
.PopupTrigger
= False
108 xMouseEvent
.Source
= xWindow
110 xMouseEvent2
= MouseEvent()
111 xMouseEvent2
.Modifiers
= 0
112 xMouseEvent2
.Buttons
= MouseButton
.LEFT
115 xMouseEvent2
.ClickCount
= 1
116 xMouseEvent2
.PopupTrigger
= False
117 xMouseEvent2
.Source
= xWindow
120 xToolkitRobot
= xWindow
.getToolkit()
121 self
.assertIsNotNone(xToolkitRobot
)
123 # Click in the menubar/toolbar area
124 xToolkitRobot
.mouseMove(xMouseEvent
)
125 xToolkitRobot
.mousePress(xMouseEvent
)
126 xToolkitRobot
.mouseRelease(xMouseEvent
)
128 # Click into the document content
129 xToolkitRobot
.mousePress(xMouseEvent2
)
130 xToolkitRobot
.mouseRelease(xMouseEvent2
)
132 # send key press event
133 xKeyEvent
= KeyEvent()
134 xKeyEvent
.Modifiers
= 0
135 xKeyEvent
.KeyCode
= 70
136 xKeyEvent
.KeyChar
= 70
137 xKeyEvent
.Source
= xWindow
139 xToolkitRobot
.keyPress(xKeyEvent
)
140 xToolkitRobot
.keyRelease(xKeyEvent
)
142 # Wait for async events to be processed
143 xToolkit
= self
.xContext
.ServiceManager
.createInstance('com.sun.star.awt.Toolkit')
144 xToolkit
.processEventsToIdle()
146 # remove mouse listener
147 xWindow
.removeMouseListener(xMouseListener
)
148 self
.assertEqual(1, mouseListenerCount
)
151 # remove key listener
152 xWindow
.removeKeyListener(xKeyListener
)
155 global keymousePressedEventsIntercepted
156 # Not expected any interceptions
157 self
.assertEqual(0, keymousePressedEventsIntercepted
)
159 global keymouseReleasedEventsIntercepted
160 # Not expected any interceptions
161 self
.assertEqual(0, keymouseReleasedEventsIntercepted
)
163 global mousePressedEventsIntercepted
164 self
.assertEqual(0, mousePressedEventsIntercepted
)
166 global mouseReleasedEventsIntercepted
167 self
.assertEqual(0, mouseReleasedEventsIntercepted
)
169 global mouseEventsIntercepted
170 # Not expected 3 interceptions
171 self
.assertEqual(0, mouseEventsIntercepted
)
176 # vim: set shiftwidth=4 softtabstop=4 expandtab: