test_contentpolicy_block_window.html (3233B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1329288 5 --> 6 <head> 7 <title>Test for Bug 1329288</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=1329288">Mozilla Bug 1329288</a> 13 14 15 <!-- have a testlink which we can use for the test to open a new window --> 16 <a href="http://test1.example.org/tests/docshell/test/navigation/file_contentpolicy_block_window.html" 17 target="_blank" 18 id="testlink">This is a link</a> 19 20 <script class="testbody" type="text/javascript"> 21 /* 22 * Description of the test: 23 * The test tries to open a new window and makes sure that a registered contentPolicy 24 * gets called with the right (a non null) 'context' for the TYPE_DOCUMENT load. 25 */ 26 27 const Ci = SpecialPowers.Ci; 28 29 var categoryManager = SpecialPowers.Services.catMan; 30 var componentManager = SpecialPowers.Components.manager 31 .QueryInterface(Ci.nsIComponentRegistrar); 32 33 // Content policy / factory implementation for the test 34 var policyID = SpecialPowers.wrap(SpecialPowers.Components).ID("{b80e19d0-878f-d41b-2654-194714a4115c}"); 35 var policyName = "@mozilla.org/testpolicy;1"; 36 var policy = { 37 // nsISupports implementation 38 QueryInterface(iid) { 39 iid = SpecialPowers.wrap(iid); 40 if (iid.equals(Ci.nsISupports) || 41 iid.equals(Ci.nsIFactory) || 42 iid.equals(Ci.nsIContentPolicy)) 43 return this; 44 throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE; 45 }, 46 47 // nsIFactory implementation 48 createInstance(iid) { 49 return this.QueryInterface(iid); 50 }, 51 52 // nsIContentPolicy implementation 53 shouldLoad(contentLocation, loadInfo) { 54 let contentType = loadInfo.externalContentPolicyType; 55 let context = loadInfo.loadingContext; 56 57 if (SpecialPowers.wrap(contentLocation).spec !== document.getElementById("testlink").href) { 58 // not the URI we are looking for, allow the load 59 return Ci.nsIContentPolicy.ACCEPT; 60 } 61 62 is(contentType, Ci.nsIContentPolicy.TYPE_DOCUMENT, 63 "needs to be type document load"); 64 ok(context, "context is not allowed to be null"); 65 ok(context.name.endsWith("test_contentpolicy_block_window.html"), 66 "context should be the current window"); 67 68 // remove the policy and finish test. 69 categoryManager.deleteCategoryEntry("content-policy", policyName, false); 70 71 setTimeout(function() { 72 // Component must be unregistered delayed, otherwise other content 73 // policy will not be removed from the category correctly 74 componentManager.unregisterFactory(policyID, policy); 75 }, 0); 76 77 SimpleTest.finish(); 78 return Ci.nsIContentPolicy.REJECT_REQUEST; 79 }, 80 81 shouldProcess() { 82 return Ci.nsIContentPolicy.ACCEPT; 83 }, 84 }; 85 86 policy = SpecialPowers.wrapCallbackObject(policy); 87 componentManager.registerFactory(policyID, "Test content policy", policyName, policy); 88 categoryManager.addCategoryEntry("content-policy", policyName, policyName, false, true); 89 90 SimpleTest.waitForExplicitFinish(); 91 92 // now everything is set up, let's start the test 93 document.getElementById("testlink").click(); 94 95 </script> 96 </body> 97 </html>