docshell/test/chrome/test_bug909218.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/docshell/test/chrome/test_bug909218.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,127 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<head>
     1.7 +  <meta charset="utf-8">
     1.8 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
     1.9 +  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
    1.10 +  <script type="application/javascript">
    1.11 +
    1.12 +SimpleTest.waitForExplicitFinish();
    1.13 +addLoadEvent(test);
    1.14 +
    1.15 +const Ci = Components.interfaces;
    1.16 +const Cu = Components.utils;
    1.17 +
    1.18 +Cu.import('resource://gre/modules/XPCOMUtils.jsm');
    1.19 +
    1.20 +// The default flags we will stick on the docShell - every request made by the
    1.21 +// docShell should include those flags.
    1.22 +const TEST_FLAGS = Ci.nsIRequest.LOAD_ANONYMOUS |
    1.23 +                   Ci.nsIRequest.LOAD_BYPASS_CACHE |
    1.24 +                   Ci.nsIRequest.INHIBIT_CACHING |
    1.25 +                   Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_HISTORY;
    1.26 +
    1.27 +var TEST_URL = "http://mochi.test:8888/chrome/docshell/test/chrome/bug909218.html";
    1.28 +
    1.29 +// These are the requests we expect to see loading TEST_URL into our iframe.
    1.30 +
    1.31 +// The test entry-point.  The basic outline is:
    1.32 +// * Create an iframe and set defaultLoadFlags on its docShell.
    1.33 +// * Add a web progress listener to observe each request as the iframe is
    1.34 +//   loaded, and check that each request has the flags we specified.
    1.35 +// * Load our test URL into the iframe and wait for the load to complete.
    1.36 +function test() {
    1.37 +  var iframe = document.createElement("iframe");
    1.38 +  document.body.appendChild(iframe);
    1.39 +  var docShell = docshellForWindow(iframe.contentWindow);
    1.40 +  // Add our progress listener - when it notices the top-level document is
    1.41 +  // complete, the test will end.
    1.42 +  RequestWatcher.init(docShell, SimpleTest.finish);
    1.43 +  // Set the flags we care about, then load our test URL.
    1.44 +  docShell.defaultLoadFlags = TEST_FLAGS;
    1.45 +  iframe.setAttribute("src", TEST_URL);
    1.46 +}
    1.47 +
    1.48 +// an nsIWebProgressListener that checks all requests made by the docShell
    1.49 +// have the flags we expect.
    1.50 +RequestWatcher = {
    1.51 +  init: function(docShell, callback) {
    1.52 +    this.callback = callback;
    1.53 +    this.docShell = docShell;
    1.54 +    docShell.
    1.55 +          QueryInterface(Ci.nsIInterfaceRequestor).
    1.56 +          getInterface(Ci.nsIWebProgress).
    1.57 +          addProgressListener(this, Ci.nsIWebProgress.NOTIFY_STATE_REQUEST |
    1.58 +                                    Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
    1.59 +    // These are the requests we expect to see - initialize each to have a
    1.60 +    // count of zero.
    1.61 +    this.requestCounts = {};
    1.62 +    for (var url of [
    1.63 +        TEST_URL,
    1.64 +        // content loaded by the above test html.
    1.65 +        "http://mochi.test:8888/chrome/docshell/test/chrome/bug909218.js",
    1.66 +        "http://mochi.test:8888/tests/SimpleTest/test.css",
    1.67 +        "http://mochi.test:8888/tests/docshell/test/chrome/red.png",
    1.68 +        // the content of an iframe in the test html.
    1.69 +        "http://mochi.test:8888/chrome/docshell/test/chrome/generic.html"
    1.70 +      ]) {
    1.71 +      this.requestCounts[url] = 0;
    1.72 +    }
    1.73 +  },
    1.74 +
    1.75 +  // Finalize the test after we detect a completed load.  We check we saw the
    1.76 +  // correct requests and make a callback to exit.
    1.77 +  finalize: function() {
    1.78 +    ok(Object.keys(this.requestCounts).length, "we expected some requests");
    1.79 +    for (var url in this.requestCounts) {
    1.80 +      var count = this.requestCounts[url];
    1.81 +      // As we are looking at all request states, we expect more than 1 for
    1.82 +      // each URL - 0 or 1 would imply something went wrong - >1 just means
    1.83 +      // multiple states for each request were recorded, which we don't care
    1.84 +      // about (we do care they all have the correct flags though - but we
    1.85 +      // do that in onStateChange)
    1.86 +      ok(count > 1, url + " saw " + count + " requests");
    1.87 +    }
    1.88 +    this.docShell.
    1.89 +          QueryInterface(Ci.nsIInterfaceRequestor).
    1.90 +          getInterface(Ci.nsIWebProgress).
    1.91 +          removeProgressListener(this);
    1.92 +    this.callback();
    1.93 +  },
    1.94 +
    1.95 +  onStateChange: function (webProgress, req, flags, status) {
    1.96 +    // We are checking requests - if there isn't one, ignore it.
    1.97 +    if (!req) {
    1.98 +      return;
    1.99 +    }
   1.100 +    // We will usually see requests for 'about:document-onload-blocker' not
   1.101 +    // have the flag, so we just ignore them.
   1.102 +    // We also see, eg, resource://gre-resources/loading-image.png, so
   1.103 +    // skip resource:// URLs too.
   1.104 +    if (req.name.startsWith("about:") || req.name.startsWith("resource:")) {
   1.105 +      return;
   1.106 +    }
   1.107 +    is(req.loadFlags & TEST_FLAGS, TEST_FLAGS, "request " + req.name + " has the expected flags");
   1.108 +    this.requestCounts[req.name] += 1;
   1.109 +    var stopFlags = Ci.nsIWebProgressListener.STATE_STOP |
   1.110 +                    Ci.nsIWebProgressListener.STATE_IS_DOCUMENT;
   1.111 +    if (req.name == TEST_URL && (flags & stopFlags) == stopFlags) {
   1.112 +      this.finalize();
   1.113 +    }
   1.114 +  },
   1.115 +  QueryInterface: XPCOMUtils.generateQI([
   1.116 +    Ci.nsIWebProgressListener,
   1.117 +    Ci.nsISupportsWeakReference,
   1.118 +  ])
   1.119 +}
   1.120 +
   1.121 +function docshellForWindow(win) {
   1.122 +  return win.
   1.123 +         QueryInterface(Ci.nsIInterfaceRequestor).
   1.124 +         getInterface(Ci.nsIWebNavigation).
   1.125 +         QueryInterface(Ci.nsIDocShell);
   1.126 +}
   1.127 +
   1.128 +</script>
   1.129 +</head>
   1.130 +</html>

mercurial