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">
9 SimpleTest.waitForExplicitFinish();
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;
18 var TEST_URL =
"http://mochi.test:8888/chrome/docshell/test/chrome/bug909218.html";
20 // These are the requests we expect to see loading TEST_URL into our iframe.
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.
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);
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;
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
52 this.requestCounts = {};
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",
62 this.requestCounts[url] =
0;
66 // Finalize the test after we detect a completed load. We check we saw the
67 // correct requests and make a callback to exit.
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");
80 QueryInterface(Ci.nsIInterfaceRequestor).
81 getInterface(Ci.nsIWebProgress).
82 removeProgressListener(this);
86 onStateChange(webProgress, req, flags, status) {
87 // We are checking requests - if there isn't one, ignore it.
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:")) {
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) {
109 QueryInterface: ChromeUtils.generateQI([
110 "nsIWebProgressListener",
111 "nsISupportsWeakReference",