Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / LayoutTests / http / tests / security / contentSecurityPolicy / resources / child-src-test.js
1 var EXPECT_BLOCK = true;
2 var EXPECT_LOAD = false;
3
4 window.jsTestIsAsync = true;
5 window.wasPostTestScriptParsed = true;
6
7 var iframe;
8 function injectFrame(url, shouldBlock) {
9     window.onload = function () {
10         iframe = document.createElement('iframe');
11         iframe.onload = iframeLoaded(shouldBlock);
12         iframe.src = url;
13         document.body.appendChild(iframe);
14     };
15 }
16
17 function iframeLoaded(expectBlock) {
18     return function(ev) {
19         var failed = true;
20         try {
21             console.log("IFrame load event fired: the IFrame's location is '" + ev.target.contentWindow.location.href + "'.");
22             if (expectBlock) {
23                 testFailed("The IFrame should have been blocked (or cross-origin). It wasn't.");
24                 failed = true;
25             } else {
26                 testPassed("The IFrame should not have been blocked. It wasn't.");
27                 failed = false;
28             }
29         } catch (ex) {
30             debug("IFrame load event fired: the IFrame is cross-origin (or was blocked).");
31             if (expectBlock) {
32                 testPassed("The IFrame should have been blocked (or cross-origin). It was.");
33                 failed = false;
34             } else {
35                 testFailed("The IFrame should not have been blocked. It was.");
36                 failed = true;
37             }
38         }
39         finishJSTest();
40     };
41 }
42
43 function injectFrameRedirectingTo(url, shouldBlock) {
44     injectFrame("/security/contentSecurityPolicy/resources/redir.php?url=" + url, shouldBlock);
45 }
46
47 function injectWorker(url, expectBlock) {
48     window.onload = function() {
49         if (expectBlock == EXPECT_BLOCK)
50             shouldThrow("var w = new Worker('" + url + "');");
51         else
52             shouldNotThrow("var w = new Worker('" + url + "');");
53         finishJSTest();
54     };
55 }
56
57 function injectSharedWorker(url, expectBlock) {
58     window.onload = function() {
59         if (expectBlock == EXPECT_BLOCK)
60             shouldThrow("var w = new SharedWorker('" + url + "');");
61         else
62             shouldNotThrow("var w = new SharedWorker('" + url + "');");
63         finishJSTest();
64     };
65 }