[Content Filtering] Determine navigation and content policy before continuing to...
[WebKit-https.git] / LayoutTests / contentfiltering / resources / contentfiltering.js
1 function _doTest(decisionPoint, decision, decideAfterUnblockRequest)
2 {
3     var settings = window.internals.mockContentFilterSettings;
4     settings.enabled = true;
5     settings.decisionPoint = decisionPoint;
6     settings.decision = (decideAfterUnblockRequest ? settings.DECISION_BLOCK : decision);
7     
8     var blockedStringText;
9     if (decisionPoint === settings.DECISION_POINT_NEVER || decision === settings.DECISION_ALLOW)
10         blockedStringText = "FAIL";
11     else
12         blockedStringText =  "PASS";
13
14     if (decideAfterUnblockRequest) {
15         settings.unblockRequestDecision = decision;
16         settings.blockedString = "<!DOCTYPE html><script>function unblockRequestDenied() { window.top.postMessage('unblockrequestdenied', '*'); }</script><body>" + blockedStringText;
17     } else
18         settings.blockedString = "<!DOCTYPE html><body>" + blockedStringText;
19
20     var isUnblocking = false;
21     var iframe = document.createElement("iframe");
22     document.body.appendChild(iframe);
23     iframe.addEventListener("load", function(event) {
24         if (isUnblocking || !decideAfterUnblockRequest) {
25             window.testRunner.notifyDone();
26             return;
27         }
28
29         isUnblocking = true;
30         window.addEventListener("message", function(event) {
31             if (event.data === "unblockrequestdenied")
32                 window.testRunner.notifyDone();
33         }, false);
34         iframe.contentDocument.location = settings.unblockRequestURL;
35     }, false);
36     iframe.src = "data:text/html,<!DOCTYPE html><body>" + (blockedStringText === "FAIL" ? "PASS" : "FAIL");
37 }
38
39 function testContentFiltering(decisionPoint, decision, decideAfterUnblockRequest)
40 {
41     if (!window.internals) {
42         console.log("This test requires window.internals");
43         return;
44     }
45
46     if (!window.testRunner) {
47         console.log("This test requires window.testRunner");
48         return;
49     }
50
51     window.testRunner.waitUntilDone();
52     window.addEventListener("load", function(event) {
53         _doTest(decisionPoint, decision, decideAfterUnblockRequest);
54     }, false);
55 }