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