1#############################################################################
2##
3## Copyright (C) 2016 The Qt Company Ltd.
4## Contact: https://www.qt.io/licensing/
5##
6## This file is part of the test suite of Qt for Python.
7##
8## $QT_BEGIN_LICENSE:GPL-EXCEPT$
9## Commercial License Usage
10## Licensees holding valid commercial Qt licenses may use this file in
11## accordance with the commercial license agreement provided with the
12## Software or, alternatively, in accordance with the terms contained in
13## a written agreement between you and The Qt Company. For licensing terms
14## and conditions see https://www.qt.io/terms-conditions. For further
15## information use the contact form at https://www.qt.io/contact-us.
16##
17## GNU General Public License Usage
18## Alternatively, this file may be used under the terms of the GNU
19## General Public License version 3 as published by the Free Software
20## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21## included in the packaging of this file. Please review the following
22## information to ensure the GNU General Public License requirements will
23## be met: https://www.gnu.org/licenses/gpl-3.0.html.
24##
25## $QT_END_LICENSE$
26##
27#############################################################################
28
29from PySide2.QtCore import QObject, Slot, QTimer
30from PySide2.QtWebKit import QWebView
31from PySide2.QtWidgets import QApplication
32from PySide2 import QtCore
33
34import os
35import sys
36import unittest
37
38sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
39from init_paths import init_test_paths
40init_test_paths(False)
41
42from helper.usesqapplication import UsesQApplication
43
44functionID = -1
45currentWebView = None
46
47class JSFuncs(QObject):
48    @Slot(str,result=str)
49    def slot_str_str(self, x):
50        global functionID
51        functionID = 0
52        return x.upper()
53
54    @Slot(str,result='QVariant')
55    def slot_str_list(self, x):
56        global functionID
57        functionID = 1
58        return [x, x]
59
60    @Slot('QStringList',result=str)
61    def slot_strlist_str(self, x):
62        global functionID
63        functionID = 2
64        return x[-1]
65
66    @Slot('QVariant',result=str)
67    def slot_variant_str(self, x):
68        global functionID
69        functionID = 3
70        return str(x)
71
72    @Slot('QVariantList',result=str)
73    def slot_variantlist_str(self, x):
74        global functionID
75        functionID = 4
76        return str(x[-1])
77
78    @Slot('QVariantMap',result=str)
79    def slot_variantmap_str(self, x):
80        global functionID
81        functionID = 5
82        return str(x["foo"])
83
84
85
86PAGE_DATA = "data:text/html,<!doctype html><html><body onload='%s'></body></html>"
87FUNCTIONS_LIST = ['jsfuncs.slot_str_str("hello")',
88                  'jsfuncs.slot_str_list("hello")',
89                  'jsfuncs.slot_strlist_str(["hello","world"])',
90                  'jsfuncs.slot_variant_str("hello")',
91                  'jsfuncs.slot_variantlist_str(["hello","world"])',
92                  'jsfuncs.slot_variantmap_str({"foo": "bar"})']
93
94
95def onLoadFinished( result ):
96    QTimer.singleShot( 100, createNextWebView )
97
98def createNextWebView():
99    global functionID
100
101    nListCount = len(FUNCTIONS_LIST) - 1
102    functionID = functionID + 1
103    print functionID
104
105    if functionID < nListCount:
106        createWebView( functionID )
107    else:
108        QTimer.singleShot(300, QApplication.instance().quit)
109
110
111def createWebView( nIndex ):
112    global functionID
113    global currentWebView
114
115    functionID = nIndex
116    currentWebView = QWebView()
117    currentWebView._jsfuncs = JSFuncs()
118    currentWebView.page().mainFrame().addToJavaScriptWindowObject("jsfuncs", currentWebView._jsfuncs)
119    QObject.connect( currentWebView, QtCore.SIGNAL('loadFinished( bool )'), onLoadFinished )
120    currentWebView.load(PAGE_DATA % FUNCTIONS_LIST[ nIndex ])
121    currentWebView.show()
122
123class Bug959(UsesQApplication):
124
125    def testJavaScriptInWebViewForCrash( self ):
126        # wait for the webview load to be finished before creating the next webview
127        # don't create the webview inside of onLoadFinished
128        # also call onLoadFinished with the correct number of variables
129        createNextWebView()
130        self.app.exec_()
131
132if __name__ == "__main__":
133    unittest.main()
134