Commit 346f81f8 authored by Anders Hartvoll Ruud's avatar Anders Hartvoll Ruud Committed by Commit Bot

[@property] Improve test coverage for valid/invalid rules

This adds new tests that verify that invalid rules are ignored,
and don't affect other valid rules.

I also rewrote this test file to use utils.js more liberally. It makes
individual tests harder to understand for the uninitiated, but it
scales so much better as the number of test functions increases.

Bug: 973830
Change-Id: Ie19d85042a0475180cc4bbee4ad202ea08adba73
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2161005
Commit-Queue: Anders Hartvoll Ruud <andruud@chromium.org>
Reviewed-by: default avatarIan Kilpatrick <ikilpatrick@chromium.org>
Cr-Commit-Position: refs/heads/master@{#761969}
parent 4ad2fd06
...@@ -3,132 +3,230 @@ ...@@ -3,132 +3,230 @@
<script src="/resources/testharness.js"></script> <script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script> <script src="/resources/testharnessreport.js"></script>
<script src="./resources/utils.js"></script> <script src="./resources/utils.js"></script>
<style>
@property --a {
syntax: "<length>";
inherits: false;
initial-value: 1px;
}
@property --b {
syntax: "<length>";
inherits: false;
initial-value: 2px;
}
@property --c {
syntax: "<length>";
inherits: false;
initial-value: 3px;
}
@property --d {
syntax: "<length>";
inherits: false;
initial-value: 4px;
}
@property --d {
syntax: "<color>";
inherits: false;
initial-value: red;
}
</style>
<style>
@property --c {
syntax: "<integer>";
inherits: false;
initial-value: 6;
}
</style>
<div id=outer> <div id=outer>
<div id=div></div> <div id=div></div>
</div> </div>
<script> <script>
CSS.registerProperty({ test_with_at_property({
name: '--b', syntax: '"<length>"',
syntax: '<color>',
inherits: false, inherits: false,
initialValue: 'green' initialValue: '1px'
}); }, (name) => {
assert_equals(getComputedStyle(div).getPropertyValue(name), '1px');
CSS.registerProperty({
name: '--e',
syntax: '<color>',
inherits: false,
initialValue: 'blue'
});
test(() => {
assert_equals(getComputedStyle(div).getPropertyValue('--a'), '1px');
}, '@property determines the registration when uncontested'); }, '@property determines the registration when uncontested');
test(() => { test_with_at_property({
assert_equals(getComputedStyle(div).getPropertyValue('--b'), 'rgb(0, 128, 0)'); syntax: '"<length>"',
inherits: false,
initialValue: '2px'
}, (name) => {
CSS.registerProperty({
name: name,
syntax: '<color>',
inherits: false,
initialValue: 'green'
});
assert_equals(getComputedStyle(div).getPropertyValue(name), 'rgb(0, 128, 0)');
}, 'CSS.registerProperty wins over @property'); }, 'CSS.registerProperty wins over @property');
test(() => { test_with_at_property({
assert_equals(getComputedStyle(div).getPropertyValue('--c'), '6'); syntax: '"<length>"',
inherits: false,
initialValue: '3px'
}, (name1) => {
with_at_property({
name: name1,
syntax: '"<integer>"',
inherits: false,
initialValue: '6'
}, (name2) => {
assert_equals(name1, name2);
assert_equals(getComputedStyle(div).getPropertyValue(name2), '6');
});
}, '@property later in document order wins'); }, '@property later in document order wins');
test(() => { test(() => {
assert_equals(getComputedStyle(div).getPropertyValue('--d'), 'rgb(255, 0, 0)'); let name = generate_name();
with_style_node(`
@property ${name} {
syntax: "<length>";
inherits: false;
initial-value: 4px;
}
@property ${name} {
syntax: "<color>";
inherits: false;
initial-value: red;
}
`, () => {
assert_equals(getComputedStyle(div).getPropertyValue(name), 'rgb(255, 0, 0)');
});
}, '@property later in stylesheet wins'); }, '@property later in stylesheet wins');
test(() => { test(() => {
assert_equals(getComputedStyle(div).getPropertyValue('--e'), 'rgb(0, 0, 255)'); let name = generate_name();
CSS.registerProperty({
name: name,
syntax: '<color>',
inherits: false,
initialValue: 'green'
});
assert_equals(getComputedStyle(div).getPropertyValue(name), 'rgb(0, 128, 0)');
}, 'CSS.registerProperty determines the registration when uncontested'); }, 'CSS.registerProperty determines the registration when uncontested');
test(() => { test(() => {
// --f is initially not registered, hence has no initial value. let name = generate_name();
assert_equals(getComputedStyle(div).getPropertyValue('--f'), '');
// ${name} is initially not registered, hence has no initial value.
assert_equals(getComputedStyle(div).getPropertyValue(name), '');
with_at_property({ with_at_property({
name: '--f', name: name,
syntax: '"<length>"', syntax: '"<length>"',
inherits: false, inherits: false,
initialValue: '10px' initialValue: '10px'
}, () => { }, () => {
assert_equals(getComputedStyle(div).getPropertyValue('--f'), '10px'); assert_equals(getComputedStyle(div).getPropertyValue(name), '10px');
}); });
// When the style node is removed, --f should be unregistered again. // When the style node is removed, ${name} should be unregistered again.
assert_equals(getComputedStyle(div).getPropertyValue('--f'), ''); assert_equals(getComputedStyle(div).getPropertyValue(name), '');
}, '@property registrations are cleared when rule removed'); }, '@property registrations are cleared when rule removed');
test_with_style_node('div { --g: calc(1px + 1px); }', () => { test(() => {
// --g should be a token sequence at this point. let name = generate_name();
assert_equals(getComputedStyle(div).getPropertyValue('--g'), ' calc(1px + 1px)');
with_style_node(`div { ${name}: calc(1px + 1px); }`, () => {
// ${name} should be a token sequence at this point.
assert_equals(getComputedStyle(div).getPropertyValue(name), ' calc(1px + 1px)');
with_at_property({
name: name,
syntax: '"<length>"',
inherits: false,
initialValue: '0px'
}, () => {
// ${name} is now a <length>, hence the calc() should be simplified.
assert_equals(getComputedStyle(div).getPropertyValue(name), '2px');
});
// ${name} should be a token sequence again.
assert_equals(getComputedStyle(div).getPropertyValue(name), ' calc(1px + 1px)');
});
}, 'Computed value becomes token sequence when @property is removed');
with_at_property({ test(() => {
name: '--g', let name = generate_name();
syntax: '"<length>"',
inherits: false, with_style_node(`#outer { ${name}: 10px; }`, () => {
initialValue: '0px' assert_equals(getComputedStyle(div).getPropertyValue(name), ' 10px');
}, () => {
// --g is now a <length>, hence the calc() should be simplified. with_at_property({
assert_equals(getComputedStyle(div).getPropertyValue('--g'), '2px'); name: name,
syntax: '"<length>"',
inherits: false,
initialValue: '0px'
}, () => {
// ${name} is no longer inherited
assert_equals(getComputedStyle(div).getPropertyValue(name), '0px');
});
assert_equals(getComputedStyle(div).getPropertyValue(name), ' 10px');
}); });
}, 'Inherited status is reflected in computed styles when @property is removed');
// --g should be a token sequence again. test(() => {
assert_equals(getComputedStyle(div).getPropertyValue('--g'), ' calc(1px + 1px)'); let name = generate_name();
}, 'Computed value becomes token sequence when @property is removed');
with_style_node(`
@property ${name} {
syntax: "<length>";
inherits: false;
initial-value: 1px;
}
@property ${name} {
inherits: false;
initial-value: green;
}
`, () => {
assert_equals(getComputedStyle(div).getPropertyValue(name), '1px');
});
}, 'Invalid @property rule (missing syntax) does not overwrite previous valid rule');
test_with_style_node('#outer { --h: 10px; }', () => { test(() => {
assert_equals(getComputedStyle(div).getPropertyValue('--h'), ' 10px'); let name = generate_name();
with_style_node(`
@property ${name} {
syntax: "<length>";
inherits: false;
initial-value: 1px;
}
@property ${name} {
syntax: "<color>";
initial-value: green;
}
`, () => {
assert_equals(getComputedStyle(div).getPropertyValue(name), '1px');
});
}, 'Invalid @property rule (missing inherits descriptor) does not overwrite previous valid rule');
with_at_property({ test(() => {
name: '--h', let name = generate_name();
syntax: '"<length>"',
inherits: false, with_style_node(`
initialValue: '0px' @property ${name} {
}, () => { syntax: "<length>";
// --h is no longer inherited inherits: false;
assert_equals(getComputedStyle(div).getPropertyValue('--h'), '0px'); initial-value: 1px;
}
@property ${name} {
syntax: "<color>";
inherits: false;
}
`, () => {
assert_equals(getComputedStyle(div).getPropertyValue(name), '1px');
}); });
}, 'Invalid @property rule (missing initial-value) does not overwrite previous valid rule');
assert_equals(getComputedStyle(div).getPropertyValue('--h'), ' 10px'); test(() => {
}, 'Inherited status is reflected in computed styles when @property is removed'); let name = generate_name();
with_style_node(`
@property ${name} {
syntax: "<color>";
inherits: false;
}
@property ${name} {
syntax: "<length>";
inherits: false;
initial-value: 1px;
}
`, () => {
assert_equals(getComputedStyle(div).getPropertyValue(name), '1px');
});
}, 'Previous invalid rule does not prevent valid rule from causing registration');
test(() => {
let name = generate_name();
with_style_node(`
@property ${name} {
syntax: "<length>";
inherits: false;
initial-value: 1px;
quite-unknown: 200;
}
`, () => {
assert_equals(getComputedStyle(div).getPropertyValue(name), '1px');
});
}, 'Unknown descriptors are ignored and do not invalidate rule');
</script> </script>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment