OLD | NEW |
| (Empty) |
1 // Test runner for the paint worklet to test invalidation behaviour. | |
2 // | |
3 // Registers an promise_test per test case, which: | |
4 // - Creates an element. | |
5 // - Invalidates the style of that element. | |
6 // - [worklet] Worklet code logs if it got the invalidation. | |
7 // - Asserts that it got the correct paint invalidation. | |
8 // | |
9 // Usage: | |
10 // testRunnerInvalidationLogging('background-image', [ | |
11 // { property: 'max-height', value: '100px' }, | |
12 // { property: 'color', prevValue: '#00F', value: 'blue', noInvalidation: t
rue }, | |
13 // { property: '-webkit-margin-start', invalidationProperty: 'margin-left',
prevValue: 'calc(50px + 50px)', value: '100px', noInvalidation: true } | |
14 // ]); | |
15 | |
16 function testRunnerInvalidationLogging(imageType, tests) { | |
17 const keys = tests.map(function(obj) { return obj.property; }); | |
18 const workletCode = 'const properties = ' + JSON.stringify(keys) + ';\n' + ` | |
19 for (let i = 0; i < properties.length; i++) { | |
20 registerPaint('paint-' + i, class { | |
21 static get inputProperties() { return [properties[i]]; } | |
22 constructor() { this.hasPainted= false; } | |
23 paint(ctx, geom) { | |
24 ctx.fillStyle = this.hasPainted ? 'green' : 'blue'; | |
25 ctx.fillRect(0, 0, geom.width, geom.height); | |
26 if (this.hasPainted) { | |
27 console.log('Successful invalidation for: ' + properties
[i]); | |
28 } | |
29 this.hasPainted = true; | |
30 } | |
31 }); | |
32 } | |
33 `; | |
34 | |
35 paintWorklet.addModule(URL.createObjectURL(new Blob([workletCode], {type: 't
ext/javascript'}))).then(function() { | |
36 for (let i = 0; i < tests.length; i++) { | |
37 tests[i].paintName = 'paint-' + i; | |
38 registerTest(imageType, tests[i]); | |
39 } | |
40 }); | |
41 } | |
42 | |
43 function registerTest(imageType, test) { | |
44 const testName = test.property + ': ' + (test.prevValue || '[inline not set]
') + ' => ' + (test.invalidationProperty || test.property) + ': ' + (test.value
|| '[inline not set]'); | |
45 | |
46 // We use a promise_test instead of a async_test as they run sequentially. | |
47 promise_test(function() { | |
48 return new Promise(function(resolve) { | |
49 | |
50 const msg = ['\n\nTest case:', testName]; | |
51 if (test.noInvalidation) { | |
52 msg.push('The worklet console should log nothing'); | |
53 } else { | |
54 msg.push('The worklet console should log: \'Successful invalidat
ion for: ' + test.property + '\''); | |
55 } | |
56 console.log(msg.join('\n')); | |
57 | |
58 // Create the test div. | |
59 const el = document.createElement('div'); | |
60 if (test.prevValue) el.style.setProperty(test.property, test.prevVal
ue); | |
61 el.style[imageType] = 'paint(' + test.paintName + ')'; | |
62 document.body.appendChild(el); | |
63 | |
64 runAfterLayoutAndPaint(function() { | |
65 if (window.internals) | |
66 internals.startTrackingRepaints(document); | |
67 | |
68 // Keep the BCR for the paint invalidation assertion, and invali
date paint. | |
69 const rect = el.getBoundingClientRect(); | |
70 if (test.invalidationProperty) { | |
71 el.style.setProperty(test.invalidationProperty, test.value); | |
72 } else { | |
73 el.style.setProperty(test.property, test.value); | |
74 } | |
75 | |
76 runAfterLayoutAndPaint(function() { | |
77 // Check that the we got the correct paint invalidation. | |
78 if (window.internals) { | |
79 const layers = JSON.parse(internals.layerTreeAsText(docu
ment, internals.LAYER_TREE_INCLUDES_PAINT_INVALIDATIONS)); | |
80 const paintInvalidations = layers['layers'][0]['paintInv
alidations']; | |
81 assert_equals(!paintInvalidations, !!test.noInvalidation
); | |
82 if (paintInvalidations) { | |
83 assert_equals(paintInvalidations.length, 1, 'There s
hould be only one paint invalidation.'); | |
84 assert_array_equals( | |
85 paintInvalidations[0]['rect'], | |
86 [rect.left, rect.top, rect.width, rect.heigh
t], | |
87 'The paint invalidation should cover the ent
ire element.'); | |
88 } | |
89 internals.stopTrackingRepaints(document); | |
90 } | |
91 | |
92 // Cleanup. | |
93 document.body.removeChild(el); | |
94 resolve(); | |
95 }); | |
96 }); | |
97 | |
98 }); | |
99 | |
100 }, testName); | |
101 } | |
OLD | NEW |