File: test_js_api.py

package info (click to toggle)
python-pywebview 5.0.5%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 33,680 kB
  • sloc: python: 9,592; javascript: 917; java: 158; cs: 130; sh: 16; makefile: 3
file content (100 lines) | stat: -rwxr-xr-x 2,511 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from concurrent.futures.thread import ThreadPoolExecutor

import webview

from .util import assert_js, run_test


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 NestedApi:
    @classmethod
    def get_int(cls):
        return 422

    def get_int_instance(self):
        return 423

class Api:
    class ApiTestException(Exception):
        pass

    nested = NestedApi
    nested_instance = NestedApi()

    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 Api.ApiTestException()

    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)
    assert_js(window, 'nested.get_int', 422)
    assert_js(window, 'nested_instance.get_int_instance', 423)


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