Skip to content

Commit

Permalink
Implement test variants using Jinja.
Browse files Browse the repository at this point in the history
This is a follow up of crrev.com/c/4544667. That CL had converted the
main test templates to Jinja, but the test variants we still using
basic Python string formatting. With this CL, Jinja is now used
everywhere.

Change-Id: I5df9d1b37aece45d33e131ef7d9243f289b6e719
Bug: 1275750
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4558564
Reviewed-by: Yi Xu <yiyix@chromium.org>
Commit-Queue: Jean-Philippe Gravel <jpgravel@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1150271}
  • Loading branch information
graveljp authored and Chromium LUCI CQ committed May 29, 2023
1 parent 6b09e5d commit 3e1b0ac
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,12 @@ def _write_reference_test(jinja_env: jinja2.Environment,
jinja_env.get_template("reftest_worker.html").render(params),
'utf-8')

js_ref = params.get('reference')
html_ref = params.get('html_reference')
js_ref = params.get('reference', '')
html_ref = params.get('html_reference', '')
ref_params = dict(params)
ref_params.update({
'is_test_reference': True,
'code': js_ref or html_ref
'code': jinja_env.from_string(js_ref or html_ref).render(params)
})
ref_template_name = 'reftest_element.html' if js_ref else 'reftest.html'
if TestType.HTML_CANVAS in enabled_tests:
Expand Down Expand Up @@ -383,6 +383,8 @@ def _generate_test(test: Mapping[str, Any], jinja_env: jinja2.Environment,
'expected_img': expected_img
})

params['code'] = jinja_env.from_string(params['code']).render(params)

canvas_path = os.path.join(html_canvas_cfg.out_dir, sub_dir, name)
offscreen_path = os.path.join(offscreen_canvas_cfg.out_dir, sub_dir, name)
if 'manual' in test:
Expand Down Expand Up @@ -456,12 +458,6 @@ def genTestUtils_union(NAME2DIRFILE: str) -> None:
test = original_test.copy()
if variant_name or variant_params:
test['name'] += '.' + variant_name
test['code'] = test['code'] % variant_params
if 'reference' in test:
test['reference'] = test['reference'] % variant_params
if 'html_reference' in test:
test['html_reference'] = (
test['html_reference'] % variant_params)
test.update(variant_params)

name = test['name']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
circle.arc(90, 90, 45, 0, 2 * Math.PI);
ctx.fill(circle);
%(render-states)s
{{ render_states }}
ctx.beginLayer();
Expand All @@ -29,7 +29,7 @@
circle.arc(90, 90, 45, 0, 2 * Math.PI);
ctx.fill(circle);
%(render-states)s
{{ render_states }}
canvas2 = document.createElement("canvas");
ctx2 = canvas2.getContext("2d");
Expand All @@ -43,54 +43,54 @@
ctx.drawImage(canvas2, 0, 0);
variants: &global-state-variants
no-global-states:
render-states: // No global states.
render_states: // No global states.
alpha:
render-states: ctx.globalAlpha = 0.6;
render_states: ctx.globalAlpha = 0.6;
blending:
render-states: ctx.globalCompositeOperation = 'multiply';
render_states: ctx.globalCompositeOperation = 'multiply';
composite:
render-states: ctx.globalCompositeOperation = 'source-in';
render_states: ctx.globalCompositeOperation = 'source-in';
shadow:
render-states: |-
render_states: |-
ctx.shadowOffsetX = -10;
ctx.shadowOffsetY = 10;
ctx.shadowColor = 'rgba(255, 165, 0, 0.5)';
alpha.blending:
render-states: |-
render_states: |-
ctx.globalAlpha = 0.6;
ctx.globalCompositeOperation = 'multiply';
alpha.composite:
render-states: |-
render_states: |-
ctx.globalAlpha = 0.6;
ctx.globalCompositeOperation = 'source-in';
alpha.shadow:
render-states: |-
render_states: |-
ctx.globalAlpha = 0.5;
ctx.shadowOffsetX = -10;
ctx.shadowOffsetY = 10;
ctx.shadowColor = 'rgba(255, 165, 0, 0.5)';
alpha.blending.shadow:
render-states: |-
render_states: |-
ctx.globalAlpha = 0.6;
ctx.globalCompositeOperation = 'multiply';
ctx.shadowOffsetX = -10;
ctx.shadowOffsetY = 10;
ctx.shadowColor = 'rgba(255, 165, 0, 0.5)';
alpha.composite.shadow:
render-states: |-
render_states: |-
ctx.globalAlpha = 0.6;
ctx.globalCompositeOperation = 'source-in';
ctx.shadowOffsetX = -10;
ctx.shadowOffsetY = 10;
ctx.shadowColor = 'rgba(255, 165, 0, 0.5)';
blending.shadow:
render-states: |-
render_states: |-
ctx.globalCompositeOperation = 'multiply';
ctx.shadowOffsetX = -10;
ctx.shadowOffsetY = 10;
ctx.shadowColor = 'rgba(255, 165, 0, 0.5)';
composite.shadow:
render-states: |-
render_states: |-
ctx.globalCompositeOperation = 'source-in';
ctx.shadowOffsetX = -10;
ctx.shadowOffsetY = 10;
Expand All @@ -107,7 +107,7 @@
circle.arc(90, 90, 45, 0, 2 * Math.PI);
ctx.fill(circle);
%(render-states)s
{{ render_states }}
ctx.beginLayer([
{filter: 'colorMatrix', values: [0.393, 0.769, 0.189, 0, 0,
Expand All @@ -129,7 +129,7 @@
<svg xmlns="http://www.w3.org/2000/svg"
width="200" height="200"
color-interpolation-filters="sRGB">
<filter id="filter" x="-100%%" y="-100%%" width="300%%" height="300%%">
<filter id="filter" x="-100%" y="-100%" width="300%" height="300%">
<feColorMatrix
type="matrix"
values="0.393 0.769 0.189 0 0
Expand Down Expand Up @@ -157,7 +157,7 @@
circle.arc(90, 90, 45, 0, 2 * Math.PI);
ctx.fill(circle);
%(render-states)s
{{ render_states }}
ctx.drawImage(img, 0, 0);
};
Expand Down Expand Up @@ -286,7 +286,7 @@
ctx.fillRect(50, 50, 75, 50);
// Force a flush and restoration of the state stack:
%(flush_canvas)s
{{ flush_canvas }}
ctx.fillRect(70, 70, 75, 50);
ctx.fillStyle = 'orange';
Expand Down

0 comments on commit 3e1b0ac

Please sign in to comment.