Skip to content

Commit be03b25

Browse files
committed
ENH: Update py_WebEngine self-test enabling python evaluation test
Explicitly setting a url is required to create a page and ensure QWebChannel class is available. git-svn-id: http://svn.slicer.org/Slicer4/trunk@27938 3bd1e089-480b-0410-8dfb-8563597acbee
1 parent f39b64e commit be03b25

File tree

2 files changed

+34
-42
lines changed

2 files changed

+34
-42
lines changed

Applications/SlicerApp/Testing/Python/WebEngine.py

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,10 @@ def test_WebEngine1(self):
148148

149149
webWidget = slicer.qSlicerWebWidget()
150150
webWidget.size = qt.QSize(1024,512)
151-
webView = webWidget.webView()
151+
webWidget.webView().url = qt.QUrl("")
152152
webWidget.show()
153153
self.delayDisplay('Showing widget')
154154

155-
156-
157155
webWidget.connect("evalResult(QString,QString)", self.onEvalResult)
158156

159157
webWidget.evalJS("""
@@ -183,55 +181,49 @@ def test_WebEngine1(self):
183181
if not self.gotCorrectResponse:
184182
raise AssertionError("Did not get back expected result!")
185183

186-
187184
#
188-
# Note - calling slicerPython code from JavaScript code works when
189-
# executed from the web page, but oddly not from within the
190-
# context of a self test, so the following tests are disabled for now.
185+
# Test python evaluation from javascript
191186
#
187+
self.delayDisplay('Call a python method')
192188

193-
if False:
194-
195-
self.delayDisplay('Call a python method')
196-
197-
webWidget.evalJS("""
198-
new QWebChannel(qt.webChannelTransport, channel => {
199-
const slicerPython = channel.objects.slicerPython;
200-
201-
let pythonCode = "dialog = qt.QInputDialog(slicer.util.mainWindow())\n";
202-
pythonCode += "dialog.setLabelText('hello')\n";
203-
pythonCode += "dialog.open()\n";
204-
pythonCode += "qt.QTimer.singleShot(1000, dialog.close)\n";
205-
206-
slicerPython.evalPython(pythonCode);
207-
});
208-
""")
209-
189+
slicer.app.settings().setValue("WebEngine/AllowPythonExecution", ctk.ctkMessageBox.AcceptRole)
210190

191+
webWidget.evalJS(r"""
192+
new QWebChannel(qt.webChannelTransport, channel => {
193+
const slicerPython = channel.objects.slicerPython;
211194
212-
self.delayDisplay('Test access to python via js')
195+
let pythonCode = "dialog = qt.QInputDialog(slicer.util.mainWindow())\n";
196+
pythonCode += "dialog.setLabelText('hello')\n";
197+
pythonCode += "dialog.open()\n";
198+
pythonCode += "qt.QTimer.singleShot(1000, dialog.close)\n";
213199
214-
if hasattr(slicer.modules, 'slicerPythonValueFromJS'):
215-
del slicer.modules.slicerPythonValueFromJS
200+
slicerPython.evalPython(pythonCode);
201+
});
202+
""")
216203

217-
webWidget.evalJS("""
218-
new QWebChannel(qt.webChannelTransport, channel => {
219-
channel.objects.slicerPython.evalPython("slicer.modules.slicerPythonValueFromJS = 42");
220-
});
221-
""")
204+
self.delayDisplay('Test access to python via js', msec=500)
222205

223-
iteration = 0
224-
while iteration < 5 and not hasattr(slicer.modules, 'slicerPythonValueFromJS'):
225-
self.delayDisplay('Waiting for python value from JS...')
226-
iteration += 1
206+
if hasattr(slicer.modules, 'slicerPythonValueFromJS'):
207+
del slicer.modules.slicerPythonValueFromJS
227208

228-
if iteration >= 5:
229-
raise RuntimeError("Couldn't get python value back from JS")
209+
webWidget.evalJS("""
210+
new QWebChannel(qt.webChannelTransport, channel => {
211+
out = channel.objects.slicerPython.evalPython("slicer.modules.slicerPythonValueFromJS = 42");
212+
});
213+
""")
230214

231-
del slicer.modules.slicerPythonValueFromJS
215+
iteration = 0
216+
while iteration < 3 and not hasattr(slicer.modules, 'slicerPythonValueFromJS'):
217+
# Specify an explicit delay to ensure async execution by the
218+
# webengine has completed.
219+
self.delayDisplay('Waiting for python value from JS...', msec=500)
220+
iteration += 1
232221

222+
if iteration >= 3:
223+
raise RuntimeError("Couldn't get python value back from JS")
233224

234-
self.delayDisplay('Value of %d receieved via javascipt' % slicerPythonValueFromJS)
225+
self.delayDisplay('Value of %d received via javascipt' % slicer.modules.slicerPythonValueFromJS)
235226

227+
del slicer.modules.slicerPythonValueFromJS
236228

237229
self.delayDisplay('Test passed!')

Base/QTGUI/qSlicerWebPythonProxy.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class Q_SLICER_BASE_QTGUI_EXPORT qSlicerWebPythonProxy
4141
public slots:
4242

4343
/// Convenient function to execute python code from
44-
/// the the JavaScript (when an instance of this class
45-
/// is registered with the WebChannel.
44+
/// the JavaScript (when an instance of this class
45+
/// is registered with the WebChannel).
4646
///
4747
/// Internally Slicer's python manager is used to execute
4848
/// the passed code, and the resulting QVariant is returned

0 commit comments

Comments
 (0)