File: test_js_api.py

package info (click to toggle)
python-pywebview 3.3.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 29,536 kB
  • sloc: python: 5,703; javascript: 888; cs: 130; sh: 55; makefile: 3
file content (81 lines) | stat: -rwxr-xr-x 2,151 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from concurrent.futures.thread import ThreadPoolExecutor
import webview
from .util import run_test, assert_js


def test_js_bridge():
    api = Api()
    window = webview.create_window('JSBridge test', js_api=api)
    run_test(webview, window, js_bridge)


def test_exception():
    api = Api()
    window = webview.create_window('JSBridge test', js_api=api)
    run_test(webview, window, exception)

# This test randomly fails on Windows
def test_concurrent():
    api = Api()
    window = webview.create_window('JSBridge test', js_api=api)
    run_test(webview, window, concurrent)


class Api:
    def get_int(self):
        return 420

    def get_float(self):
        return 3.141

    def get_string(self):
        return 'test'

    def get_object(self):
        return {'key1': 'value', 'key2': 420}

    def get_objectlike_string(self):
        return '{"key1": "value", "key2": 420}'

    def get_single_quote(self):
        return "te'st"

    def get_double_quote(self):
        return 'te"st'

    def raise_exception(self):
        raise Exception()

    def echo(self, param):
        return param

    def multiple(self, param1, param2, param3):
        return param1, param2, param3


def js_bridge(window):
    window.load_html('<html><body>TEST</body></html>')
    assert_js(window, 'get_int', 420)
    assert_js(window, 'get_float', 3.141)
    assert_js(window, 'get_string', 'test')
    assert_js(window, 'get_object', {'key1': 'value', 'key2': 420})
    assert_js(window, 'get_objectlike_string', '{"key1": "value", "key2": 420}')
    assert_js(window, 'get_single_quote', 'te\'st')
    assert_js(window, 'get_double_quote', 'te"st')
    assert_js(window, 'echo', 'test', 'test')
    assert_js(window, 'multiple', [1, 2, 3], 1, 2, 3)


def exception(window):
    assert_js(window, 'raise_exception', 'error')


def concurrent(window):
    with ThreadPoolExecutor(max_workers=5) as executor:
        futures = []
        for i in range(5):
            future = executor.submit(assert_js, window, 'echo', i, i)
            futures.append(future)

    for e in filter(lambda r: r, [f.exception() for f in futures]):
        raise e