TestCenter Reference
RemoteRenderingEventReplay.py
Go to the documentation of this file.
2# Copyright 2013, MeVis Medical Solutions AG
3#
4# The user may use this file in accordance with the license agreement provided with
5# the Software or, alternatively, in accordance with the terms contained in a
6# written agreement between the user and MeVis Medical Solutions AG.
7#
8# For further information use the contact form at https://www.mevislab.de/contact
9#
10
11from TestSupport import Base, Logging
12from TestSupport.Macros import EXPECT_TRUE
13from mevis import MLAB, MLABFileManager
14from PythonQt import QtCore
15import json
16import time
17import os.path
18
19# A helper class to replay events recorded by the RemoteRenderingEventRecorder
21
22 # init the replay with a given remote rendering slave and a *.events filename
23 def __init__(self, slave, filename, resizeHandler = None):
24 self._baseName = os.path.splitext(os.path.basename(filename))[0]
25 self._slave = slave
26 f = open(filename, "rb")
27 self._events = json.load(f)
28 f.close()
30 self.sceneHasChanged = False
31 self.resizeHandler = resizeHandler
32 self._slave.sceneChanged.connect(self.sceneChangedsceneChanged)
33
34 # create an additional screenshot
36 self._slave.imageReceived.connect(self.imageReceivedimageReceived)
37 self._slave.sendRenderRequest(self._slave.PNG)
38 self._slave.imageReceived.disconnect(self.imageReceivedimageReceived)
39
40 # notice if the scene has changed
41 def sceneChanged(self):
42 self.sceneHasChanged = True
43
44 # handle the receiving of an image
45 def imageReceived(self, img):
46 name = self._baseName + str(self.screenshotCount)
47 filename = Base.getResultDirectory() + "/" + name + ".png"
48 EXPECT_TRUE(img.save(filename))
49
50 referenceImage = Base.getDataDirectory() + "/" + name + ".png"
51 if not MLABFileManager.exists(referenceImage):
52 EXPECT_TRUE(img.save(referenceImage))
53
54 Logging.showImage(name + " (reference image on the right)", filename, referenceImage)
55 self.screenshotCount += 1
56
57 def doPause(self, delay):
58 start_time = time.time()
59 while time.time() - start_time < delay / 1000.0:
60 MLAB.msleep(10)
61 MLAB.processEvents()
62
63 # replay all events
64 def replay(self, redrawAfterResize=False, redrawAfterSceneChanged=False):
65 for event in self._events:
66 shouldRedraw = False
67 eventType = event["event"]
68 hasPosition = "x" in event and "y" in event
69 if eventType == "Resize":
70 w = event["width"]
71 h = event["height"]
72 if self.resizeHandler:
73 self.resizeHandler(w,h)
74 self._slave.resize(w, h)
75 if redrawAfterResize:
76 shouldRedraw = True
77 elif eventType == "Pause":
78 self.doPause(event["delay"])
79 elif eventType == "Enter":
80 self._slave.enter()
81 elif eventType == "Leave":
82 self._slave.leave()
83 elif eventType == "MouseMove":
84 self._slave.mouseMoveTo(event["x"],event["y"])
85 elif eventType == "MouseButtonPress":
86 if hasPosition:
87 self._slave.mousePress(event["button"],event["x"],event["y"])
88 else:
89 self._slave.mousePress(event["button"])
90 elif eventType == "MouseButtonRelease":
91 if hasPosition:
92 self._slave.mouseRelease(event["button"],event["x"],event["y"])
93 else:
94 self._slave.mouseRelease(event["button"])
95 elif eventType == "MouseButtonDblClick":
96 if hasPosition:
97 self._slave.mouseDoubleClickPress(event["button"],event["x"],event["y"])
98 else:
99 self._slave.mouseDoubleClickPress(event["button"])
100 elif eventType == "Wheel":
101 if hasPosition:
102 self._slave.mouseWheel(event["delta"],event["x"],event["y"])
103 else:
104 self._slave.mouseWheel(event["delta"])
105 elif eventType == "KeyPress":
106 self._slave.keyPress(event["key"])
107 elif eventType == "KeyRelease":
108 self._slave.keyRelease(event["key"])
109 elif eventType == "Screenshot":
110 self.createScreenshot()
111 else:
112 MLAB.logWarning("Unknown event: " + event)
113
114 # it was required to use QCoreApplication.processEvents instead of MLAB.processEvent()
115 # so that events are reliably delivered to QGraphicsView/RemotePanelRendering:
116 QtCore.QCoreApplication.processEvents()
117
118 if redrawAfterSceneChanged and self.sceneHasChanged:
119 shouldRedraw = True
120 self.sceneHasChanged = False
121 if shouldRedraw:
122 self._slave.sendRenderRequest(self._slave.PNG)
123 MLAB.processEvents()
124
replay(self, redrawAfterResize=False, redrawAfterSceneChanged=False)
__init__(self, slave, filename, resizeHandler=None)
Adds GoogleTest like methods.
Definition Macros.py:1