bug 879963 - test for redundant downloading of a repeated @font-face resource. r=dbaron

This commit is contained in:
Jonathan Kew 2013-07-17 12:53:38 +01:00
parent 61edd65b1e
commit 620f28acc0
3 changed files with 192 additions and 0 deletions

View File

@ -145,6 +145,8 @@ MOCHITEST_FILES = test_acid3_test46.html \
test_property_database.html \
test_priority_preservation.html \
test_property_syntax_errors.html \
test_redundant_font_download.html \
redundant_font_download.sjs \
test_rem_unit.html \
test_rule_insertion.html \
test_rule_serialization.html \

View File

@ -0,0 +1,60 @@
const BinaryOutputStream =
Components.Constructor("@mozilla.org/binaryoutputstream;1",
"nsIBinaryOutputStream",
"setOutputStream");
// this is simply a hex dump of a red square .PNG image
const RED_SQUARE =
[
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00,
0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x20,
0x00, 0x00, 0x00, 0x20, 0x08, 0x02, 0x00, 0x00, 0x00, 0xFC,
0x18, 0xED, 0xA3, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47,
0x42, 0x00, 0xAE, 0xCE, 0x1C, 0xE9, 0x00, 0x00, 0x00, 0x28,
0x49, 0x44, 0x41, 0x54, 0x48, 0xC7, 0xED, 0xCD, 0x41, 0x0D,
0x00, 0x00, 0x08, 0x04, 0xA0, 0xD3, 0xFE, 0x9D, 0x35, 0x85,
0x0F, 0x37, 0x28, 0x40, 0x4D, 0x6E, 0x75, 0x04, 0x02, 0x81,
0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0xC1, 0x93, 0x60, 0x01,
0xA3, 0xC4, 0x01, 0x3F, 0x58, 0x1D, 0xEF, 0x27, 0x00, 0x00,
0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82
];
function handleRequest(request, response)
{
var query = {};
request.queryString.split('&').forEach(function (val) {
var [name, value] = val.split('=');
query[name] = unescape(value);
});
response.setHeader("Cache-Control", "no-cache");
response.setStatusLine(request.httpVersion, 200, "OK");
response.setHeader("Content-Type", "text/plain", false);
var log = getState("bug-879963-request-log") || "";
var stream = new BinaryOutputStream(response.bodyOutputStream);
if (query["q"] == "init") {
log = "init"; // initialize the log, and return a PNG image
response.setHeader("Content-Type", "image/png", false);
stream.writeByteArray(RED_SQUARE, RED_SQUARE.length);
} else if (query["q"] == "image") {
log = log + ";" + query["q"];
response.setHeader("Content-Type", "image/png", false);
stream.writeByteArray(RED_SQUARE, RED_SQUARE.length);
} else if (query["q"] == "font") {
log = log + ";" + query["q"];
// we don't provide a real font; that's ok, OTS will just reject it
response.write("Junk");
} else if (query["q"] == "report") {
// don't include the actual "report" request in the log we return
response.write(log);
} else {
log = log + ";" + query["q"];
response.setStatusLine(request.httpVersion, 404, "Not Found");
}
setState("bug-879963-request-log", log);
}

View File

@ -0,0 +1,130 @@
<!DOCTYPE HTML>
<html>
<!-- https://bugzilla.mozilla.org/show_bug.cgi?id=879963 -->
<head>
<meta charset="utf-8">
<title>Test for bug 879963</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<!-- Two <style> elements with identical @font-face rules.
Although multiple @font-face at-rules for the same family are legal,
and add faces to the family, we should NOT download the same resource
twice just because we have a duplicate face entry. -->
<style type="text/css">
@font-face {
font-family: foo;
src: url("redundant_font_download.sjs?q=font");
}
.test {
font-family: foo;
}
</style>
<style type="text/css">
@font-face {
font-family: foo;
src: url("redundant_font_download.sjs?q=font");
}
.test {
font-family: foo;
}
</style>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=879963">Mozilla Bug 879963</a>
<div>
<!-- the 'init' request returns an image (just so we can see it's working)
and initializes the request logging in our sjs server -->
<img src="redundant_font_download.sjs?q=init">
</div>
<div id="test">
Test
</div>
<div>
<img id="image2" src="">
</div>
<script type="application/javascript">
// helper to retrieve the server's request log as text
function getRequestLog() {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "redundant_font_download.sjs?q=report", false);
xmlHttp.send(null);
return xmlHttp.responseText;
}
// retrieve just the most recent request the server has seen
function getLastRequest() {
return getRequestLog().split(";").pop();
}
// poll the server at intervals of 'delay' ms until it has seen a specific request,
// or until maxTime ms have passed
function waitForRequest(request, delay, maxTime, func) {
timeLimit = Date.now() + maxTime;
var intervalId = window.setInterval(function() {
var req = getLastRequest();
if (req == request || Date.now() > timeLimit) {
window.clearInterval(intervalId);
func();
}
}.bind(this), delay);
}
// initially disable the second of the <style> elements,
// so we only have a single copy of the font-face
document.getElementsByTagName("style")[1].disabled = true;
SimpleTest.waitForExplicitFinish();
// We perform a series of actions that trigger requests to the .sjs server,
// and poll the server's request log at each stage to check that it has
// seen the request we expected before we proceed to the next step.
function startTest() {
is(getRequestLog(), "init", "request log has been initialized");
// this should trigger a font download
document.getElementById("test").className = "test";
// wait to confirm that the server has received the request
waitForRequest("font", 10, 5000, continueTest1);
}
function continueTest1() {
is(getRequestLog(), "init;font", "server received font request");
// trigger a request for the second image, to provide an explicit
// delimiter in the log before we enable the second @font-face rule
document.getElementById("image2").src = "redundant_font_download.sjs?q=image";
waitForRequest("image", 10, 5000, continueTest2);
}
function continueTest2() {
is(getRequestLog(), "init;font;image", "server received image request");
// enable the second <style> element: we should NOT see a second font request,
// we expect waitForRequest to time out instead
document.getElementsByTagName("style")[1].disabled = false;
waitForRequest("font", 10, 1000, continueTest3);
}
function continueTest3() {
is(getRequestLog(), "init;font;image", "should NOT have re-requested the font");
SimpleTest.finish();
}
waitForRequest("init", 10, 5000, startTest);
</script>
</body>
</html>