Bug 1686668 [wpt PR 27185] - Update wpt metadata, a=testonly
[gecko.git] / dom / base / test / test_bug375314.html
blob02935949d4fec970c541f275f198794bb3005681
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=375314
5 -->
6 <head>
7 <title>Test for Bug 375314</title>
8 <script src="/tests/SimpleTest/SimpleTest.js"></script>
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
10 </head>
11 <body>
12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=375314">Mozilla Bug 375314</a>
13 <p id="display"></p>
14 <div id="content" style="display: none">
16 </div>
17 <pre id="test">
18 <script class="testbody" type="text/javascript">
20 /** Test for Bug 375314 **/
22 var lastContentType = -1;
23 const testURL = window.location.href + "/this/is/the/test/url";
24 const Cc = SpecialPowers.Cc;
25 const Ci = SpecialPowers.Ci;
26 var {AppConstants} = SpecialPowers.Cu.import("resource://gre/modules/AppConstants.jsm", {});
28 // Content policy / factory implementation for the test
29 var policyID = SpecialPowers.wrap(SpecialPowers.Components).ID("{b80e19d0-878f-d41b-2654-194714a4115c}");
30 var policyName = "@mozilla.org/testpolicy;1";
31 var policy = {
32 // nsISupports implementation
33 QueryInterface(iid) {
35 iid = SpecialPowers.wrap(iid);
36 if (iid.equals(Ci.nsISupports) ||
37 iid.equals(Ci.nsIFactory) ||
38 iid.equals(Ci.nsIContentPolicy))
39 return this;
41 throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
44 // nsIFactory implementation
45 createInstance(outer, iid) {
46 return this.QueryInterface(iid);
49 // nsIContentPolicy implementation
50 shouldLoad(contentLocation, loadInfo, mimeTypeGuess) {
51 let contentType = loadInfo.externalContentPolicyType;
52 // Remember last content type seen for the test url
53 if (SpecialPowers.wrap(contentLocation).spec == testURL) {
54 lastContentType = contentType;
55 return Ci.nsIContentPolicy.REJECT_REQUEST;
58 return Ci.nsIContentPolicy.ACCEPT;
61 shouldProcess(contentLocation, loadInfo, mimeTypeGuess) {
62 return Ci.nsIContentPolicy.ACCEPT;
65 policy = SpecialPowers.wrapCallbackObject(policy);
67 // Register content policy
68 var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager
69 .QueryInterface(Ci.nsIComponentRegistrar);
71 componentManager.registerFactory(policyID, "Test content policy", policyName, policy);
73 var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
74 categoryManager.addCategoryEntry("content-policy", policyName, policyName, false, true);
76 // Try creating different request types
77 var tests = ["SCRIPT", "IMAGE", "STYLESHEET", "XMLHTTPREQUEST"];
78 var curTest = -1;
79 var div;
81 SimpleTest.waitForExplicitFinish();
82 setTimeout(runNextTest, 0);
84 function runNextTest() {
86 if (curTest >= 0) {
87 var type = "TYPE_" + tests[curTest];
88 is(lastContentType, Ci.nsIContentPolicy[type], "Content policies triggered for " + type);
91 curTest++;
92 if (curTest < tests.length) {
93 var method = "request_" + tests[curTest].toLowerCase();
94 try {
95 window[method]();
96 } catch(e) {}
97 setTimeout(runNextTest, 0);
99 else {
100 // Unregister content policy
101 categoryManager.deleteCategoryEntry("content-policy", policyName, false);
103 setTimeout(function() {
104 // Component must be unregistered delayed, otherwise other content
105 // policy will not be removed from the category correctly
106 componentManager.unregisterFactory(policyID, policy);
107 }, 0);
109 SimpleTest.finish();
113 // Request creating functions
115 function request_script() {
116 var content = $("content");
118 var script = document.createElement("script");
119 script.setAttribute("type", "text/javascript")
120 script.setAttribute("src", testURL)
121 content.appendChild(script);
124 function request_image() {
125 var content = $("content");
127 var image = new Image();
128 image.src = testURL;
131 function request_stylesheet() {
132 var content = $("content");
134 var stylesheet = document.createElement("link");
135 stylesheet.setAttribute("rel", "stylesheet");
136 stylesheet.setAttribute("type", "text/css");
137 stylesheet.setAttribute("href", testURL);
138 content.appendChild(stylesheet);
141 function request_object() {
142 var content = $("content");
144 var object = document.createElement("embed");
145 object.setAttribute("src", testURL);
146 content.appendChild(object);
149 function request_xmlhttprequest() {
150 var request = new XMLHttpRequest();
151 request.open("GET", testURL, false);
152 request.send(null);
155 </script>
156 </pre>
157 </body>
158 </html>