test_bug909218.html (4698B)
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> 6 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/> 7 <script type="application/javascript"> 8 9 SimpleTest.waitForExplicitFinish(); 10 addLoadEvent(test); 11 12 // The default flags we will stick on the docShell - every request made by the 13 // docShell should include those flags. 14 const TEST_FLAGS = Ci.nsIRequest.LOAD_ANONYMOUS | 15 Ci.nsIRequest.LOAD_BYPASS_CACHE | 16 Ci.nsIRequest.INHIBIT_CACHING; 17 18 var TEST_URL = "http://mochi.test:8888/chrome/docshell/test/chrome/bug909218.html"; 19 20 // These are the requests we expect to see loading TEST_URL into our iframe. 21 22 // The test entry-point. The basic outline is: 23 // * Create an iframe and set defaultLoadFlags on its docShell. 24 // * Add a web progress listener to observe each request as the iframe is 25 // loaded, and check that each request has the flags we specified. 26 // * Load our test URL into the iframe and wait for the load to complete. 27 function test() { 28 var iframe = document.createElement("iframe"); 29 document.body.appendChild(iframe); 30 var docShell = iframe.contentWindow.docShell; 31 // Add our progress listener - when it notices the top-level document is 32 // complete, the test will end. 33 RequestWatcher.init(docShell, SimpleTest.finish); 34 // Set the flags we care about, then load our test URL. 35 docShell.defaultLoadFlags = TEST_FLAGS; 36 iframe.setAttribute("src", TEST_URL); 37 } 38 39 // an nsIWebProgressListener that checks all requests made by the docShell 40 // have the flags we expect. 41 var RequestWatcher = { 42 init(docShell, callback) { 43 this.callback = callback; 44 this.docShell = docShell; 45 docShell. 46 QueryInterface(Ci.nsIInterfaceRequestor). 47 getInterface(Ci.nsIWebProgress). 48 addProgressListener(this, Ci.nsIWebProgress.NOTIFY_STATE_REQUEST | 49 Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT); 50 // These are the requests we expect to see - initialize each to have a 51 // count of zero. 52 this.requestCounts = {}; 53 for (var url of [ 54 TEST_URL, 55 // content loaded by the above test html. 56 "http://mochi.test:8888/chrome/docshell/test/chrome/bug909218.js", 57 "http://mochi.test:8888/tests/SimpleTest/test.css", 58 "http://mochi.test:8888/tests/docshell/test/chrome/red.png", 59 // the content of an iframe in the test html. 60 "http://mochi.test:8888/chrome/docshell/test/chrome/generic.html", 61 ]) { 62 this.requestCounts[url] = 0; 63 } 64 }, 65 66 // Finalize the test after we detect a completed load. We check we saw the 67 // correct requests and make a callback to exit. 68 finalize() { 69 ok(Object.keys(this.requestCounts).length, "we expected some requests"); 70 for (var url in this.requestCounts) { 71 var count = this.requestCounts[url]; 72 // As we are looking at all request states, we expect more than 1 for 73 // each URL - 0 or 1 would imply something went wrong - >1 just means 74 // multiple states for each request were recorded, which we don't care 75 // about (we do care they all have the correct flags though - but we 76 // do that in onStateChange) 77 ok(count > 1, url + " saw " + count + " requests"); 78 } 79 this.docShell. 80 QueryInterface(Ci.nsIInterfaceRequestor). 81 getInterface(Ci.nsIWebProgress). 82 removeProgressListener(this); 83 this.callback(); 84 }, 85 86 onStateChange(webProgress, req, flags) { 87 // We are checking requests - if there isn't one, ignore it. 88 if (!req) { 89 return; 90 } 91 // We will usually see requests for 'about:document-onload-blocker' not 92 // have the flag, so we just ignore them. 93 // We also see, eg, resource://gre-resources/loading-image.png, so 94 // skip resource:// URLs too. 95 // We may also see, eg, chrome://global/skin/icons/chevron.svg, so 96 // skip chrome:// URLs too. 97 if (req.name.startsWith("about:") || req.name.startsWith("resource:") || 98 req.name.startsWith("chrome:") || req.name.startsWith("documentchannel:")) { 99 return; 100 } 101 is(req.loadFlags & TEST_FLAGS, TEST_FLAGS, "request " + req.name + " has the expected flags"); 102 this.requestCounts[req.name] += 1; 103 var stopFlags = Ci.nsIWebProgressListener.STATE_STOP | 104 Ci.nsIWebProgressListener.STATE_IS_DOCUMENT; 105 if (req.name == TEST_URL && (flags & stopFlags) == stopFlags) { 106 this.finalize(); 107 } 108 }, 109 QueryInterface: ChromeUtils.generateQI([ 110 "nsIWebProgressListener", 111 "nsISupportsWeakReference", 112 ]), 113 }; 114 115 </script> 116 </head> 117 </html>