summaryrefslogtreecommitdiffstats
path: root/docshell/test/chrome/test_bug909218.html
blob: 7b20b3a8adcfef76c973aacf5ffbe98a903683dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
  <script type="application/javascript">

SimpleTest.waitForExplicitFinish();
addLoadEvent(test);

// The default flags we will stick on the docShell - every request made by the
// docShell should include those flags.
const TEST_FLAGS = Ci.nsIRequest.LOAD_ANONYMOUS |
                   Ci.nsIRequest.LOAD_BYPASS_CACHE |
                   Ci.nsIRequest.INHIBIT_CACHING;

var TEST_URL = "http://mochi.test:8888/chrome/docshell/test/chrome/bug909218.html";

// These are the requests we expect to see loading TEST_URL into our iframe.

// The test entry-point.  The basic outline is:
// * Create an iframe and set defaultLoadFlags on its docShell.
// * Add a web progress listener to observe each request as the iframe is
//   loaded, and check that each request has the flags we specified.
// * Load our test URL into the iframe and wait for the load to complete.
function test() {
  var iframe = document.createElement("iframe");
  document.body.appendChild(iframe);
  var docShell = iframe.contentWindow.docShell;
  // Add our progress listener - when it notices the top-level document is
  // complete, the test will end.
  RequestWatcher.init(docShell, SimpleTest.finish);
  // Set the flags we care about, then load our test URL.
  docShell.defaultLoadFlags = TEST_FLAGS;
  iframe.setAttribute("src", TEST_URL);
}

// an nsIWebProgressListener that checks all requests made by the docShell
// have the flags we expect.
var RequestWatcher = {
  init(docShell, callback) {
    this.callback = callback;
    this.docShell = docShell;
    docShell.
          QueryInterface(Ci.nsIInterfaceRequestor).
          getInterface(Ci.nsIWebProgress).
          addProgressListener(this, Ci.nsIWebProgress.NOTIFY_STATE_REQUEST |
                                    Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
    // These are the requests we expect to see - initialize each to have a
    // count of zero.
    this.requestCounts = {};
    for (var url of [
        TEST_URL,
        // content loaded by the above test html.
        "http://mochi.test:8888/chrome/docshell/test/chrome/bug909218.js",
        "http://mochi.test:8888/tests/SimpleTest/test.css",
        "http://mochi.test:8888/tests/docshell/test/chrome/red.png",
        // the content of an iframe in the test html.
        "http://mochi.test:8888/chrome/docshell/test/chrome/generic.html",
      ]) {
      this.requestCounts[url] = 0;
    }
  },

  // Finalize the test after we detect a completed load.  We check we saw the
  // correct requests and make a callback to exit.
  finalize() {
    ok(Object.keys(this.requestCounts).length, "we expected some requests");
    for (var url in this.requestCounts) {
      var count = this.requestCounts[url];
      // As we are looking at all request states, we expect more than 1 for
      // each URL - 0 or 1 would imply something went wrong - >1 just means
      // multiple states for each request were recorded, which we don't care
      // about (we do care they all have the correct flags though - but we
      // do that in onStateChange)
      ok(count > 1, url + " saw " + count + " requests");
    }
    this.docShell.
          QueryInterface(Ci.nsIInterfaceRequestor).
          getInterface(Ci.nsIWebProgress).
          removeProgressListener(this);
    this.callback();
  },

  onStateChange(webProgress, req, flags) {
    // We are checking requests - if there isn't one, ignore it.
    if (!req) {
      return;
    }
    // We will usually see requests for 'about:document-onload-blocker' not
    // have the flag, so we just ignore them.
    // We also see, eg, resource://gre-resources/loading-image.png, so
    // skip resource:// URLs too.
    // We may also see, eg, chrome://global/skin/icons/chevron.svg, so
    // skip chrome:// URLs too.
    if (req.name.startsWith("about:") || req.name.startsWith("resource:") ||
        req.name.startsWith("chrome:") || req.name.startsWith("documentchannel:")) {
      return;
    }
    is(req.loadFlags & TEST_FLAGS, TEST_FLAGS, "request " + req.name + " has the expected flags");
    this.requestCounts[req.name] += 1;
    var stopFlags = Ci.nsIWebProgressListener.STATE_STOP |
                    Ci.nsIWebProgressListener.STATE_IS_DOCUMENT;
    if (req.name == TEST_URL && (flags & stopFlags) == stopFlags) {
      this.finalize();
    }
  },
  QueryInterface: ChromeUtils.generateQI([
    "nsIWebProgressListener",
    "nsISupportsWeakReference",
  ]),
};

</script>
</head>
</html>