pjs/layout/style/test/test_value_storage.html

253 строки
8.3 KiB
HTML

<!DOCTYPE HTML>
<html>
<!--
-->
<head>
<title>Test for parsing, storage, and serialization of CSS values</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="property_database.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<style type="text/css" id="prereqsheet">
#testnode {}
</style>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none">
<div id="testnode"></div>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
/** Test for parsing, storage, and serialization of CSS values **/
/*
* The idempotence tests here deserve a little bit of explanation. What
* we're testing here are the following operations:
* parse: string -> CSS rule
* serialize: CSS rule -> string (normalization 1)
* (this actually has two variants that go through partly different
* codepaths, which we exercise with getPropertyValue and cssText)
* compute: CSS rule -> computed style
* cserialize: computed style -> string (normalization 2)
*
* Both serialize and cserialize do some normalization, so we can't test
* for pure round-tripping, and we also can't compare their output since
* they could normalize differently. (We might at some point in the
* future want to guarantee that any output of cserialize is
* untouched by going through parse+serialize, though.)
*
* So we test idempotence of parse + serialize by running the whole
* operation twice. Likewise for parse + compute + cserialize.
*
* Slightly more interestingly, we test that serialize + parse is the
* identity transform by comparing the output of parse + compute +
* cserialize to the output of parse + serialize + parse + compute +
* cserialize.
*/
var gSystemFont = {
"caption": true,
"icon": true,
"menu": true,
"message-box": true,
"small-caption": true,
"status-bar": true,
};
var gBadCompute = {
// output wrapped around to positive, in exponential notation
"-moz-box-ordinal-group": [ "-1", "-1000" ],
};
function xfail_compute(property, value)
{
if (property in gBadCompute &&
gBadCompute[property].indexOf(value) != -1)
return true;
return false;
}
var gBadSerialize = {
// Font-families which include quotes don't get those escaped when
//serializing: see bug 660397
"font-family": ["\\\"Times New Roman", "Times, \\\"Times New Roman"],
};
function xfail_serialize(property, value)
{
if (property in gBadSerialize &&
gBadSerialize[property].indexOf(value) != -1)
return true;
return false;
}
var gElement = document.getElementById("testnode");
var gDeclaration = gElement.style;
var gComputedStyle = window.getComputedStyle(gElement, "");
var gPrereqDeclaration =
document.getElementById("prereqsheet").sheet.cssRules[0].style;
function test_property(property)
{
var info = gCSSProperties[property];
var test_computed = !("backend_only" in info);
function test_value(value) {
gDeclaration.setProperty(property, value, "");
var idx;
var step1val = gDeclaration.getPropertyValue(property);
var step1vals = [];
var step1ser = gDeclaration.cssText;
if ("subproperties" in info)
for (idx in info.subproperties)
step1vals.push(gDeclaration.getPropertyValue(info.subproperties[idx]));
var step1comp;
var step1comps = [];
if (test_computed && info.type != CSS_TYPE_TRUE_SHORTHAND)
step1comp = gComputedStyle.getPropertyValue(property);
if (test_computed && "subproperties" in info)
for (idx in info.subproperties)
step1comps.push(gComputedStyle.getPropertyValue(info.subproperties[idx]));
isnot(step1val, "", "setting '" + value + "' on '" + property + "'");
if ("subproperties" in info)
for (idx in info.subproperties) {
var subprop = info.subproperties[idx];
isnot(gDeclaration.getPropertyValue(subprop), "",
"setting '" + value + "' on '" + property + "'");
}
// We don't care particularly about the whitespace or the placement of
// semicolons, but for simplicity we'll test the current behavior.
var expected_serialization = "";
if (step1val != "")
expected_serialization = property + ": " + step1val + ";";
is(step1ser, expected_serialization,
"serialization should match property value");
gDeclaration.removeProperty(property);
gDeclaration.setProperty(property, step1val, "");
var serialize_func = xfail_serialize(property, value) ? todo_is : is;
serialize_func(gDeclaration.getPropertyValue(property), step1val,
"parse+serialize should be idempotent for '" +
property + ": " + value + "'");
if (test_computed && info.type != CSS_TYPE_TRUE_SHORTHAND) {
serialize_func(gComputedStyle.getPropertyValue(property), step1comp,
"serialize+parse should be identity transform for '" +
property + ": " + value + "'");
}
if ("subproperties" in info &&
// Using setProperty over subproperties is not sufficient for
// system fonts, since the shorthand does more than its parts.
(property != "font" || !(value in gSystemFont))) {
gDeclaration.removeProperty(property);
for (idx in info.subproperties) {
var subprop = info.subproperties[idx];
gDeclaration.setProperty(subprop, step1vals[idx], "");
}
// Now that all the subprops are set, check their values. Note that we
// need this in a separate loop, in case parts of the shorthand affect
// the computed values of other parts.
for (idx in info.subproperties) {
var subprop = info.subproperties[idx];
if (test_computed && !("backend_only" in gCSSProperties[subprop])) {
is(gComputedStyle.getPropertyValue(subprop), step1comps[idx],
"serialize(" + subprop + ")+parse should be the identity " +
"transform for '" + property + ": " + value + "'");
}
}
is(gDeclaration.getPropertyValue(property), step1val,
"parse+split+serialize should be idempotent for '" +
property + ": " + value + "'");
}
if (test_computed && info.type != CSS_TYPE_TRUE_SHORTHAND) {
gDeclaration.removeProperty(property);
gDeclaration.setProperty(property, step1comp, "");
var func = (xfail_compute(property, value) ||
xfail_serialize(property, value)) ? todo_is : is;
func(gComputedStyle.getPropertyValue(property), step1comp,
"parse+compute+serialize should be idempotent for '" +
property + ": " + value + "'");
}
if (test_computed && "subproperties" in info) {
gDeclaration.removeProperty(property);
for (idx in info.subproperties) {
var subprop = info.subproperties[idx];
if ("backend_only" in gCSSProperties[subprop])
continue;
gDeclaration.setProperty(subprop, step1comps[idx], "");
}
// Now that all the subprops are set, check their values. Note that we
// need this in a separate loop, in case parts of the shorthand affect
// the computed values of other parts.
for (idx in info.subproperties) {
var subprop = info.subproperties[idx];
if ("backend_only" in gCSSProperties[subprop])
continue;
is(gComputedStyle.getPropertyValue(subprop), step1comps[idx],
"parse+compute+serialize(" + subprop + ") should be idempotent for '" +
property + ": " + value + "'");
}
}
gDeclaration.removeProperty(property);
}
if ("prerequisites" in info) {
var prereqs = info.prerequisites;
for (var prereq in prereqs) {
gPrereqDeclaration.setProperty(prereq, prereqs[prereq], "");
}
}
var idx;
for (idx in info.initial_values)
test_value(info.initial_values[idx]);
for (idx in info.other_values)
test_value(info.other_values[idx]);
if ("prerequisites" in info) {
for (var prereq in info.prerequisites) {
gPrereqDeclaration.removeProperty(prereq);
}
}
}
// To avoid triggering the slow script dialog, we have to test one
// property at a time.
SimpleTest.waitForExplicitFinish();
var props = [];
for (var prop in gCSSProperties)
props.push(prop);
props = props.reverse();
function do_one() {
if (props.length == 0) {
SimpleTest.finish();
return;
}
test_property(props.pop());
SimpleTest.executeSoon(do_one);
}
SimpleTest.executeSoon(do_one);
</script>
</pre>
</body>
</html>