Commit ee0cdef5 authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

[css-typed-om] Test that styleMap.set rejects invalid values.

Currently, our per-property tests only test that styleMap.set accept
valid values. This patch refactors the per-property tests a bit to
also test that we reject invalid values.

Bug: 774887
Change-Id: I41b5c4c1a6dfe6e766ff37fdc1b1f890dfb81aac
Reviewed-on: https://chromium-review.googlesource.com/905523
Commit-Queue: Darren Shen <shend@chromium.org>
Reviewed-by: default avatarnainar <nainar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#536021}
parent b7fef392
This is a testharness.js-based test. This is a testharness.js-based test.
FAIL Can set 'margin-top' to unitless zero Failed to execute 'set' on 'StylePropertyMap': Invalid type for property FAIL Can set 'margin-top' to unitless zero Failed to execute 'set' on 'StylePropertyMap': Invalid type for property
PASS Can get a CSSKeywordValue from 'margin-top' PASS Can get a CSSKeywordValue from 'margin-top'
FAIL Can set 'margin-top' to a CSSKeywordValue Failed to execute 'set' on 'StylePropertyMap': Invalid type for property PASS Can set 'margin-top' to a CSSKeywordValue
PASS Can get a percent CSSUnitValue from 'margin-top' PASS Can get a percent from 'margin-top'
PASS Can set 'margin-top' to a percent CSSUnitValue PASS Can set 'margin-top' to a percent
PASS Can get a length CSSUnitValue from 'margin-top' PASS Can get a length from 'margin-top'
PASS Can set 'margin-top' to a length CSSUnitValue PASS Can set 'margin-top' to a length
PASS Setting 'margin-top' to a time throws TypeError
Harness: the test ran to completion. Harness: the test ran to completion.
const gTestSyntax = {
'0': {
description: 'unitless zero',
set: true,
examples: [
new CSSUnitValue(0, 'number'),
],
},
'<length>': {
description: 'a length',
get: true,
set: true,
examples: [
new CSSUnitValue(0, 'px'),
new CSSUnitValue(-3.14, 'em'),
new CSSUnitValue(3.14, 'cm'),
],
},
'<percentage>': {
description: 'a percent',
get: true,
set: true,
examples: [
new CSSUnitValue(0, 'percent'),
new CSSUnitValue(-3.14, 'percent'),
new CSSUnitValue(3.14, 'percent'),
],
},
'<time>': {
description: 'a time',
get: true,
set: true,
examples: [
new CSSUnitValue(0, 's'),
new CSSUnitValue(-3.14, 'ms'),
new CSSUnitValue(3.14, 's'),
],
},
'<ident>': {
description: 'a CSSKeywordValue',
set: true,
get: true,
// user-specified examples
examples: null,
},
};
function testGet(propertyName, values, description) { function testGet(propertyName, values, description) {
test(t => { test(t => {
let element = createDivWithStyle(t); let element = createDivWithStyle(t);
...@@ -27,36 +74,42 @@ function testSet(propertyName, values, description) { ...@@ -27,36 +74,42 @@ function testSet(propertyName, values, description) {
}, `Can set '${propertyName}' to ${description}`); }, `Can set '${propertyName}' to ${description}`);
} }
function testGetSet(propertyName, values, description) { function testSetInvalid(propertyName, values, description) {
testGet(propertyName, values, description); test(t => {
testSet(propertyName, values, description); let element = createDivWithStyle(t);
let styleMap = element.attributeStyleMap;
for (const styleValue of values) {
assert_throws(new TypeError(), () => styleMap.set(propertyName, styleValue));
}
}, `Setting '${propertyName}' to ${description} throws TypeError`);
} }
function runPropertyTests(propertyName, testCases) { function runPropertyTests(propertyName, testCases) {
let productionsTested = new Set();
for (const testCase of testCases) { for (const testCase of testCases) {
if (testCase.specified == '0') { const syntax = gTestSyntax[testCase.specified];
testSet(propertyName, [ if (!syntax)
new CSSUnitValue(0, 'number'), throw new Error(`'${testCase.specified}' is not a valid production`);
], 'unitless zero');
} else if (testCase.specified === '<length>') { const examples = testCase.examples || syntax.examples;
testGetSet(propertyName, [ if (!examples)
new CSSUnitValue(0, 'px'), throw new Error(`'${testCase.specified}' tests require explicit examples`);
new CSSUnitValue(-3.14, 'em'),
new CSSUnitValue(3.14, 'cm'), if (syntax.get)
], 'a length CSSUnitValue'); testGet(propertyName, examples, syntax.description);
} else if (testCase.specified == '<percentage>') { if (syntax.set)
testGetSet(propertyName, [ testSet(propertyName, examples, syntax.description);
new CSSUnitValue(0, 'percent'),
new CSSUnitValue(-3.14, 'percent'), productionsTested.add(testCase.specified);
new CSSUnitValue(3.14, 'percent'), }
], 'a percent CSSUnitValue');
} else if (testCase.specified == '<ident>') { // Also test that styleMap.set rejects invalid CSSStyleValues.
if (!testCase.examples) { for (const [production, syntax] of Object.entries(gTestSyntax)) {
throw new Error('<ident> tests require examples'); if (!productionsTested.has(production)) {
} if (syntax.set && syntax.examples)
testSetInvalid(propertyName, syntax.examples, syntax.description);
testGetSet(propertyName, testCase.examples,
'a CSSKeywordValue');
} }
} }
} }
...@@ -1704,6 +1704,7 @@ ...@@ -1704,6 +1704,7 @@
field_template: "<length>", field_template: "<length>",
default_value: "Length(kFixed)", default_value: "Length(kFixed)",
converter: "ConvertQuirkyLength", converter: "ConvertQuirkyLength",
keywords: ["auto"],
typedom_types: ["Length", "Percent"] typedom_types: ["Length", "Percent"]
}, },
{ {
......
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