test_context_properties_allowed_domains.html (3191B)
1 <!doctype html> 2 <meta charset="utf-8"> 3 <title>Bug 1699892 - SVG context properties for allowed domains</title> 4 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 5 <script src="chrome://mochikit/content/tests/SimpleTest/WindowSnapshot.js"></script> 6 <script> 7 /** 8 * Returns a Promise that resolves when target fires a load event. 9 */ 10 function waitForLoad(target) { 11 return new Promise(resolve => { 12 target.addEventListener("load", () => { 13 if (event.target == target) { 14 resolve(); 15 }}, { once: true }); 16 }); 17 } 18 19 function makeContextFillFrame(source) { 20 return ` 21 <style> 22 img { 23 -moz-context-properties: fill; 24 fill: green; 25 } 26 </style> 27 <img src="${source}" style="width: 100px; height: 100px;"> 28 `; 29 } 30 31 /** 32 * Creates an iframe, loads src in it, and waits for the load event 33 * for the iframe to fire. Then it snapshots the iframe and returns 34 * the snapshot (and removes the iframe from the document, to clean up). 35 * 36 * src can be a URL starting with http, or is otherwise assumed to be 37 * a srcdoc string. 38 */ 39 async function loadSrcImageAndSnapshot({ src, srcdoc }) { 40 let frame = document.createElement("iframe"); 41 document.body.appendChild(frame); 42 43 if (src) { 44 frame.src = src; 45 } else { 46 frame.srcdoc = srcdoc; 47 } 48 49 await waitForLoad(frame); 50 51 // ensure layout flushed, see Bug 1955324 comment 15 52 await new Promise(res => frame.contentWindow.requestAnimationFrame(res)); 53 ok(frame.contentWindow.innerHeight != 0, 'iframe has non-zero size'); 54 55 let snapshot = await snapshotWindow(frame, false); 56 document.body.removeChild(frame); 57 return snapshot; 58 } 59 60 add_task(async () => { 61 const ALLOWED_DOMAIN = "example.org"; 62 const DISALLOWED_DOMAIN = "example.com"; 63 64 await SpecialPowers.pushPrefEnv({ 65 set: [["svg.context-properties.content.allowed-domains", ALLOWED_DOMAIN]] 66 }); 67 68 // When the context properties are allowed, we expect a green 69 // square. When they are not allowed, we expected a red square. 70 71 let redReference = await loadSrcImageAndSnapshot({ 72 srcdoc: `<div style="width: 100px; height: 100px; background: red"></div>`, 73 }); 74 75 let greenReference = await loadSrcImageAndSnapshot({ 76 srcdoc: `<div style="width: 100px; height: 100px; background: green"></div>`, 77 }); 78 79 let allowedSnapshot = await loadSrcImageAndSnapshot({ 80 src: `file_context_fill_fallback_red.html?${ALLOWED_DOMAIN}` 81 }); 82 83 let disallowedSnapshot = await loadSrcImageAndSnapshot({ 84 src: `file_context_fill_fallback_red.html?${DISALLOWED_DOMAIN}` 85 }); 86 87 const kNoFuzz = null; 88 info("Reference snapshots should look different from each other"); 89 assertSnapshots(redReference, greenReference, false, kNoFuzz, "red", "green"); 90 91 info("Allowed domain should be green"); 92 assertSnapshots(allowedSnapshot, greenReference, true, kNoFuzz, ALLOWED_DOMAIN, "green"); 93 94 info("Disallowed domain should be red"); 95 assertSnapshots(disallowedSnapshot, redReference, true, kNoFuzz, DISALLOWED_DOMAIN, "red"); 96 }); 97 </script> 98 <body> 99 </body>