docshell/test/chrome/test_bug909218.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial