|
| 1 | + |
| 2 | +import webview |
| 3 | +import json |
| 4 | +from .util import run_test |
| 5 | +import pytest |
| 6 | + |
| 7 | +test_values = [ |
| 8 | + ('int', 1), |
| 9 | + ('string', 'test'), |
| 10 | + ('null_value', None), |
| 11 | + ('object', {'key1': 'value', 'key2': 420}), |
| 12 | + ('array', [1, 2, 3]), |
| 13 | + ('mixed', {'key1': 'value', 'key2': [ 1, 2, {'id': 2}], 'nullValue': None}), |
| 14 | + ('boolean', True), |
| 15 | +] |
| 16 | + |
| 17 | +test_value_string = '\n'.join([ f"var {name} = {json.dumps(value)}" for name, value in test_values]) |
| 18 | + |
| 19 | +HTML = f""" |
| 20 | +<!DOCTYPE html> |
| 21 | +<html> |
| 22 | +<head> |
| 23 | +
|
| 24 | +</head> |
| 25 | +<body style="width: 100%; height: 100vh; display: flex;"> |
| 26 | +
|
| 27 | +<script> |
| 28 | +{test_value_string} |
| 29 | +var circular = {{key: "test"}} |
| 30 | +circular.circular = circular |
| 31 | +var testObject = {{id: 1}} |
| 32 | +var nonCircular = [testObject, testObject] |
| 33 | +
|
| 34 | +var nodes = document.getElementsByTagName("div") |
| 35 | +</script> |
| 36 | +<div>1</div> |
| 37 | +<div style="font-family: Helvetica, Arial, sans-serif; font-size: 34px; font-style: italic; font-weight: 800; width: 100%; display: flex; justify-content; center; align-items: center;"> |
| 38 | +<h1>THIS IS ONLY A TEST</h1> |
| 39 | +</h1></div> |
| 40 | +<div>3</div> |
| 41 | +</body> |
| 42 | +</html> |
| 43 | +""" |
| 44 | + |
| 45 | +def test_basic_serialization(): |
| 46 | + window = webview.create_window('Basic serialization test', html=HTML) |
| 47 | + run_test(webview, window, serialization, debug=True) |
| 48 | + |
| 49 | + |
| 50 | +def test_circular_serialization(): |
| 51 | + window = webview.create_window('Circular reference test', html=HTML) |
| 52 | + run_test(webview, window, circular_reference) |
| 53 | + |
| 54 | + |
| 55 | +def test_dom_serialization(): |
| 56 | + window = webview.create_window('DOM serialization test', html=HTML) |
| 57 | + run_test(webview, window, dom_serialization, debug=True) |
| 58 | + |
| 59 | + |
| 60 | +def serialization(window): |
| 61 | + for name, expected_value in test_values: |
| 62 | + result = window.evaluate_js(name) |
| 63 | + assert result == expected_value |
| 64 | + |
| 65 | +def circular_reference(window): |
| 66 | + result = window.evaluate_js('circular') |
| 67 | + assert result == {'key': 'test', 'circular': '[Circular Reference]'} |
| 68 | + |
| 69 | + result = window.evaluate_js('nonCircular') |
| 70 | + assert result == [{'id': 1}, {'id': 1}] |
| 71 | + |
| 72 | + |
| 73 | +def dom_serialization(window): |
| 74 | + result = window.evaluate_js('nodes') |
| 75 | + assert len(result) == 3 |
| 76 | + assert result[0]['innerText'] == '1' |
| 77 | + assert result[1]['innerText'].strip() == 'THIS IS ONLY A TEST' |
| 78 | + assert result[2]['innerText'] == '3' |
0 commit comments