content/base/test/test_bug375314.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_bug375314.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,187 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=375314
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 375314</title>
    1.11 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.13 +</head>
    1.14 +<body>
    1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=375314">Mozilla Bug 375314</a>
    1.16 +<p id="display"></p>
    1.17 +<div id="content" style="display: none">
    1.18 +  
    1.19 +</div>
    1.20 +<pre id="test">
    1.21 +<script class="testbody" type="text/javascript">
    1.22 +
    1.23 +/** Test for Bug 375314 **/
    1.24 +
    1.25 +var lastContentType = -1;
    1.26 +const testURL = window.location.href + "/this/is/the/test/url";
    1.27 +const Cc = SpecialPowers.Cc;
    1.28 +const Ci = SpecialPowers.Ci;
    1.29 +
    1.30 +// Content policy / factory implementation for the test
    1.31 +var policyID = SpecialPowers.wrap(SpecialPowers.Components).ID("{b80e19d0-878f-d41b-2654-194714a4115c}");
    1.32 +var policyName = "@mozilla.org/testpolicy;1";
    1.33 +var policy = {
    1.34 +  // nsISupports implementation
    1.35 +  QueryInterface: function(iid) {
    1.36 +
    1.37 +    iid = SpecialPowers.wrap(iid);
    1.38 +    if (iid.equals(Ci.nsISupports) ||
    1.39 +        iid.equals(Ci.nsIFactory) ||
    1.40 +        iid.equals(Ci.nsIContentPolicy))
    1.41 +      return this;
    1.42 +
    1.43 +    throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
    1.44 +  },
    1.45 +
    1.46 +  // nsIFactory implementation
    1.47 +  createInstance: function(outer, iid) {
    1.48 +    return this.QueryInterface(iid);
    1.49 +  },
    1.50 +
    1.51 +  // nsIContentPolicy implementation
    1.52 +  shouldLoad: function(contentType, contentLocation, requestOrigin, context, mimeTypeGuess, extra) {
    1.53 +
    1.54 +    // Remember last content type seen for the test url
    1.55 +    if (SpecialPowers.wrap(contentLocation).spec == testURL) {
    1.56 +      lastContentType = contentType;
    1.57 +      return Ci.nsIContentPolicy.REJECT_REQUEST;
    1.58 +    }
    1.59 +
    1.60 +    return Ci.nsIContentPolicy.ACCEPT;
    1.61 +  },
    1.62 +
    1.63 +  shouldProcess: function(contentType, contentLocation, requestOrigin, context, mimeTypeGuess, extra) {
    1.64 +
    1.65 +    return Ci.nsIContentPolicy.ACCEPT;
    1.66 +  }
    1.67 +}
    1.68 +policy = SpecialPowers.wrapCallbackObject(policy);
    1.69 +
    1.70 +// Register content policy
    1.71 +var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager
    1.72 +                                                     .QueryInterface(Ci.nsIComponentRegistrar);
    1.73 +
    1.74 +componentManager.registerFactory(policyID, "Test content policy", policyName, policy);
    1.75 +
    1.76 +var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
    1.77 +categoryManager.addCategoryEntry("content-policy", policyName, policyName, false, true);
    1.78 +
    1.79 +// Try creating different request types
    1.80 +var tests = ["SCRIPT", "IMAGE", "STYLESHEET", "OBJECT", "DOCUMENT", "SUBDOCUMENT", "XBL", "XMLHTTPREQUEST"];
    1.81 +var curTest = -1;
    1.82 +
    1.83 +var div;
    1.84 +
    1.85 +SimpleTest.waitForExplicitFinish();
    1.86 +setTimeout(runNextTest, 0);
    1.87 +
    1.88 +function runNextTest() {
    1.89 +
    1.90 +  if (curTest >= 0) {
    1.91 +    var type = "TYPE_" + tests[curTest];
    1.92 +    is(lastContentType, Ci.nsIContentPolicy[type], "Content policies triggered for " + type);
    1.93 +
    1.94 +    if (tests[curTest] == "XBL")
    1.95 +    {
    1.96 +      //XXX Removing binding to work-around a memory leak (bugs 478528, 499735).
    1.97 +      div.style.MozBinding = "";
    1.98 +    }
    1.99 +  }
   1.100 +
   1.101 +  curTest++;
   1.102 +  if (curTest < tests.length) {
   1.103 +    var method = "request_" + tests[curTest].toLowerCase();
   1.104 +    try {
   1.105 +      window[method]();
   1.106 +    } catch(e) {}
   1.107 +    setTimeout(runNextTest, 0);
   1.108 +  }
   1.109 +  else {
   1.110 +    // Unregister content policy
   1.111 +    categoryManager.deleteCategoryEntry("content-policy", policyName, false);
   1.112 +
   1.113 +    setTimeout(function() {
   1.114 +      // Component must be unregistered delayed, otherwise other content
   1.115 +      // policy will not be removed from the category correctly
   1.116 +      componentManager.unregisterFactory(policyID, policy);
   1.117 +    }, 0);
   1.118 +
   1.119 +    SimpleTest.finish();
   1.120 +  }
   1.121 +}
   1.122 +
   1.123 +// Request creating functions
   1.124 +
   1.125 +function request_script() {
   1.126 +  var content = $("content");
   1.127 +
   1.128 +  var script = document.createElement("script");
   1.129 +  script.setAttribute("type", "text/javascript")
   1.130 +  script.setAttribute("src", testURL)
   1.131 +  content.appendChild(script);
   1.132 +}
   1.133 +
   1.134 +function request_image() {
   1.135 +  var content = $("content");
   1.136 +
   1.137 +  var image = new Image();
   1.138 +  image.src = testURL;
   1.139 +}
   1.140 +
   1.141 +function request_stylesheet() {
   1.142 +  var content = $("content");
   1.143 +
   1.144 +  var stylesheet = document.createElement("link");
   1.145 +  stylesheet.setAttribute("rel", "stylesheet");
   1.146 +  stylesheet.setAttribute("type", "text/css");
   1.147 +  stylesheet.setAttribute("href", testURL);
   1.148 +  content.appendChild(stylesheet);
   1.149 +}
   1.150 +
   1.151 +function request_object() {
   1.152 +  var content = $("content");
   1.153 +
   1.154 +  var object = document.createElement("embed");
   1.155 +  object.setAttribute("src", testURL);
   1.156 +  content.appendChild(object);
   1.157 +}
   1.158 +
   1.159 +function request_document() {
   1.160 +  top.location.href = testURL;
   1.161 +}
   1.162 +
   1.163 +function request_subdocument() {
   1.164 +  var content = $("content");
   1.165 +
   1.166 +  var frame = document.createElement("iframe");
   1.167 +  frame.setAttribute("src", testURL);
   1.168 +  content.appendChild(frame);
   1.169 +}
   1.170 +
   1.171 +function request_xbl() {
   1.172 +  var content = $("content");
   1.173 +
   1.174 +  div = document.createElement("div");
   1.175 +  div.style.MozBinding = "url(" + testURL + ")";
   1.176 +  $('test').appendChild(div);
   1.177 +  div.offsetLeft; // Flush styles.
   1.178 +}
   1.179 +
   1.180 +function request_xmlhttprequest() {
   1.181 +  var request = new XMLHttpRequest();
   1.182 +  request.open("GET", testURL, false);
   1.183 +  request.send(null);
   1.184 +}
   1.185 +
   1.186 +</script>
   1.187 +</pre>
   1.188 +</body>
   1.189 +</html>
   1.190 +

mercurial