test_bug1222633_link_update.html (4252B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1222633 5 --> 6 <head> 7 <title>Test for Bug 1222633</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 10 </head> 11 <body> 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1222633">Mozilla Bug 1222633</a> 13 <p id="display"></p> 14 <div id="content" style="display: none"></div> 15 <script class="testbody" type="text/javascript"> 16 17 /** Test for Bug 1222633 */ 18 19 // Test changing as attribute from an empty value to "foo" to "image". The empty value and "foo" will 20 // not trigger any event and "image" should trigger an load event. 21 function testPreloadEventAsAttributeChange(url) { 22 return new Promise((resolve) => { 23 var link = document.createElement("LINK"); 24 link.setAttribute("rel", "preload"); 25 link.setAttribute("href", url); 26 27 link.addEventListener("load", () => { 28 ok(link.as == "image", "Only image will trigger a load event"); 29 link.remove(); 30 resolve(); 31 }); 32 link.addEventListener("error", () => { 33 ok(false, "We should not get an error event."); 34 }); 35 36 document.head.appendChild(link); 37 link.setAttribute("as", "foo"); 38 link.setAttribute("as", "image"); 39 }); 40 } 41 42 function testPreloadEventAttributeChange(url, attr, invalidValue, validValue) { 43 return new Promise((resolve) => { 44 var count = 0; 45 var link = document.createElement("LINK"); 46 link.setAttribute("rel", "preload"); 47 link.setAttribute("href", url); 48 link.setAttribute("as", "image"); 49 50 link.setAttribute(attr, invalidValue); 51 52 let firedLoad = false; 53 link.addEventListener("load", () => { 54 ok(!firedLoad, "expecting first load event for " + url); 55 firedLoad = true; 56 link.remove(); 57 resolve(); 58 }); 59 link.addEventListener("error", () => { 60 ok(false, "shouldn't fire error events"); 61 }); 62 document.head.appendChild(link); 63 SimpleTest.executeSoon(function() { 64 ok(!firedLoad, "Invalid value shouldn't fire load event"); 65 link.setAttribute(attr, validValue); 66 }) 67 }); 68 } 69 70 function testPreloadEventSetCrossOrigin(url) { 71 return new Promise((resolve) => { 72 var link = document.createElement("LINK"); 73 link.setAttribute("rel", "preload"); 74 link.setAttribute("href", url); 75 link.setAttribute("as", "fetch"); 76 count = 0; 77 78 link.addEventListener("load", () => { 79 count++; 80 if ((count == 1) || (count == 3) || (count == 5)) { 81 ok(true, "expecting " + count + ". load event for " + url); 82 } else { 83 ok(false, "expecting " + count + ". event for " + url); 84 } 85 if ((count == 1) || (count == 3)) { 86 link.setAttribute("crossorigin", ""); 87 } else { 88 link.remove(); 89 resolve(); 90 } 91 }); 92 93 link.addEventListener("error", () => { 94 count++; 95 if ((count == 2) || (count == 4)) { 96 ok(true, "expecting " + count + ". error event for " + url); 97 } else { 98 ok(false, "expecting " + count + ". error event for " + url); 99 } 100 link.removeAttribute("crossorigin"); 101 }); 102 document.head.appendChild(link); 103 }); 104 } 105 106 const IMAGE_PATH = window.location.pathname.replace(/[^/]+$/, "file_mozfiledataurl_img.jpg"); 107 const SJS_PATH = window.location.pathname.replace(/[^/]+$/, "file_bug1268962.sjs"); 108 const SAME_ORIGIN = "http://mochi.test:8888"; 109 const CROSS_ORIGIN = "http://example.com"; 110 111 SimpleTest.waitForExplicitFinish(); 112 113 // Test changing as parameter from a wrong to a correct one. 114 testPreloadEventAsAttributeChange(SAME_ORIGIN + IMAGE_PATH) 115 // Test changing type parameter from a wrong to a correct one for given as parameter. 116 .then(() => testPreloadEventAttributeChange(SAME_ORIGIN + IMAGE_PATH, "type", "text/vtt", "image/png")) 117 // Test changing media parameter from a wrong to a correct one. 118 .then(() => testPreloadEventAttributeChange(SAME_ORIGIN + IMAGE_PATH, "media", "foo", "all")) 119 // Test changing crossorigin parameter. 120 .then(() => testPreloadEventSetCrossOrigin(CROSS_ORIGIN + SJS_PATH + "?statusCode=404&cacheControl=max-age%3D120&allowOrigin=*")) 121 122 .catch((err) => ok(false, "promise rejected: " + err)) 123 .then(() => SimpleTest.finish()); 124 125 </script> 126 </body> 127 </html>