Skip to content

Commit a737d26

Browse files
committed
Enhance unit tests reliability of js bindings (cztomczak#59)
1 parent e72609c commit a737d26

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

unittests/_common.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
MESSAGE_LOOP_RANGE = 100 # each iteration is 0.01 sec
1414

1515
g_subtests_ran = 0
16+
g_js_code_completed = False
1617

1718

1819
def subtest_message(message):
@@ -50,7 +51,21 @@ def do_message_loop_work(work_loops):
5051
time.sleep(0.01)
5152

5253

54+
def js_code_completed():
55+
"""Sometimes window.onload can execute before javascript bindings
56+
are ready if the document loads very fast. When setting javascript
57+
bindings it can take some time, because these bindings are sent
58+
via IPC messaging to the Renderer process."""
59+
global g_js_code_completed
60+
assert not g_js_code_completed
61+
g_js_code_completed = True
62+
subtest_message("js_code_completed() ok")
63+
64+
5365
def check_auto_asserts(test_case, objects):
66+
# Check if js code completed
67+
test_case.assertTrue(g_js_code_completed)
68+
5469
# Automatic check of asserts in handlers and in external
5570
for obj in objects:
5671
test_for_True = False # Test whether asserts are working correctly
@@ -170,4 +185,3 @@ def Visit(self, value):
170185
self.load_handler.FrameSourceVisitor_True = True
171186
self.test_case.assertIn("747ef3e6011b6a61e6b3c6e54bdd2dee",
172187
value)
173-

unittests/main_test.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@
3131
msg = msg.replace("error", "<b style='color:red'>error</b>");
3232
document.getElementById("console").innerHTML += msg+"<br>";
3333
}
34-
window.onload = function(){
35-
print("window.onload() ok");
36-
34+
function onload_helper() {
35+
if (!window.hasOwnProperty("cefpython_version")) {
36+
// Sometimes page could finish loading before javascript
37+
// bindings are available. Javascript bindings are sent
38+
// from the browser process to the renderer process via
39+
// IPC messaging and it can take some time (5-10ms). If
40+
// the page loads very fast window.onload could execute
41+
// before bindings are available.
42+
setTimeout(onload_helper, 10);
43+
return;
44+
}
3745
version = cefpython_version
3846
print("CEF Python: <b>"+version.version+"</b>");
3947
print("Chrome: <b>"+version.chrome_version+"</b>");
@@ -78,7 +86,12 @@
7886
py_callback("String sent from Javascript");
7987
print("py_callback() ok");
8088
});
81-
};
89+
js_code_completed();
90+
}
91+
window.onload = function() {
92+
print("window.onload() ok");
93+
onload_helper();
94+
}
8295
</script>
8396
</head>
8497
<body>
@@ -133,6 +146,7 @@ def test_main(self):
133146
external = External(self)
134147
bindings = cef.JavascriptBindings(
135148
bindToFrames=False, bindToPopups=False)
149+
bindings.SetFunction("js_code_completed", js_code_completed)
136150
bindings.SetFunction("test_function", external.test_function)
137151
bindings.SetProperty("test_property1", external.test_property1)
138152
bindings.SetProperty("test_property2", external.test_property2)
@@ -181,6 +195,7 @@ def test_main(self):
181195
# and calling shutdown.
182196
do_message_loop_work(25)
183197

198+
# Asserts before shutdown
184199
# noinspection PyTypeChecker
185200
check_auto_asserts(self, [] + client_handlers
186201
+ [global_handler,

unittests/osr_test.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,26 @@
3131
msg = msg.replace("error", "<b style='color:red'>error</b>");
3232
document.getElementById("console").innerHTML += msg+"<br>";
3333
}
34-
window.onload = function(){
35-
print("window.onload() ok");
36-
34+
function onload_helper() {
35+
if (!window.hasOwnProperty("cefpython_version")) {
36+
// Sometimes page could finish loading before javascript
37+
// bindings are available. Javascript bindings are sent
38+
// from the browser process to the renderer process via
39+
// IPC messaging and it can take some time (5-10ms). If
40+
// the page loads very fast window.onload could execute
41+
// before bindings are available.
42+
setTimeout(onload_helper, 10);
43+
return;
44+
}
3745
version = cefpython_version
3846
print("CEF Python: <b>"+version.version+"</b>");
3947
print("Chrome: <b>"+version.chrome_version+"</b>");
4048
print("CEF: <b>"+version.cef_version+"</b>");
49+
js_code_completed();
50+
}
51+
window.onload = function() {
52+
print("window.onload() ok");
53+
onload_helper();
4154
}
4255
</script>
4356
</head>
@@ -95,6 +108,7 @@ def test_osr(self):
95108
# Javascript bindings
96109
bindings = cef.JavascriptBindings(
97110
bindToFrames=False, bindToPopups=False)
111+
bindings.SetFunction("js_code_completed", js_code_completed)
98112
bindings.SetProperty("cefpython_version", cef.GetVersion())
99113
browser.SetJavascriptBindings(bindings)
100114
subtest_message("browser.SetJavascriptBindings() ok")
@@ -126,7 +140,7 @@ def test_osr(self):
126140
# and calling shutdown.
127141
do_message_loop_work(25)
128142

129-
# Automatic check of asserts in handlers
143+
# Asserts before shutdown
130144
# noinspection PyTypeChecker
131145
check_auto_asserts(self, [] + client_handlers
132146
+ [global_handler,

0 commit comments

Comments
 (0)