docshell/test/chrome/test_bug909218.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial