| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 (function() | 1 (function() |
| 2 { | 2 { |
| 3 let tabs = SDK.require("sdk/tabs"); | |
| 4 let modelFor = SDK.require("sdk/model/core").modelFor; | |
|
Wladimir Palant
2016/01/06 12:54:36
So, do you want to remove this? ;)
| |
| 3 let server = null; | 5 let server = null; |
| 4 let wnd = null; | |
| 5 let tab = null; | 6 let tab = null; |
| 6 | 7 |
| 7 module("Pop-up blocker", { | 8 module("Pop-up blocker", { |
| 8 setup: function() | 9 beforeEach: function() |
| 9 { | 10 { |
| 10 prepareFilterComponents.call(this, true); | 11 prepareFilterComponents.call(this, true); |
| 11 preparePrefs.call(this); | 12 preparePrefs.call(this); |
| 12 | 13 |
| 13 server = new nsHttpServer(); | 14 server = new nsHttpServer(); |
| 14 server.start(1234); | 15 server.start(1234); |
| 15 | 16 |
| 17 // '/test' serves an html page with a single link | |
| 16 server.registerPathHandler("/test", function(metadata, response) | 18 server.registerPathHandler("/test", function(metadata, response) |
| 17 { | 19 { |
| 18 response.setStatusLine("1.1", "200", "OK"); | 20 response.setStatusLine("1.1", "200", "OK"); |
| 19 response.setHeader("Content-Type", "text/html; charset=utf-8"); | 21 response.setHeader("Content-Type", "text/html; charset=utf-8"); |
| 20 | 22 |
| 21 let body = | 23 let body = |
| 22 '<body onload="document.dispatchEvent(new CustomEvent(\'abp:frameready \', {bubbles: true}));">' + | 24 '<body>' + |
| 23 '<a id="link" href="/redirect" target="_blank">link</a>' + | 25 '<a id="link" href="/redirect" target="_blank">link</a>' + |
| 24 '</body>'; | 26 '</body>'; |
| 25 response.bodyOutputStream.write(body, body.length); | 27 response.bodyOutputStream.write(body, body.length); |
| 26 }); | 28 }); |
| 29 | |
| 30 // redirects '/redirect' to '/target' | |
| 27 server.registerPathHandler("/redirect", function(metadata, response) | 31 server.registerPathHandler("/redirect", function(metadata, response) |
| 28 { | 32 { |
| 29 response.setStatusLine("1.1", "302", "Moved Temporarily"); | 33 response.setStatusLine("1.1", "302", "Moved Temporarily"); |
| 30 response.setHeader("Location", "http://127.0.0.1:1234/target"); | 34 response.setHeader("Location", "http://127.0.0.1:1234/target"); |
| 31 }); | 35 }); |
| 36 | |
| 37 // '/target' serves an html page with 'OK' message | |
| 32 server.registerPathHandler("/target", function(metadata, response) | 38 server.registerPathHandler("/target", function(metadata, response) |
| 33 { | 39 { |
| 34 response.setHeader("Content-Type", "text/html; charset=utf-8"); | 40 response.setHeader("Content-Type", "text/html; charset=utf-8"); |
| 35 | 41 |
| 36 let body = '<html><body>OK</body></html>'; | 42 let body = '<html><body>OK</body></html>'; |
| 37 response.bodyOutputStream.write(body, body.length); | 43 response.bodyOutputStream.write(body, body.length); |
| 38 }); | 44 }); |
| 39 | 45 |
| 40 wnd = UI.currentWindow; | 46 tabs.open({ |
| 41 tab = wnd.gBrowser.loadOneTab("http://127.0.0.1:1234/test", {inBackground: false}); | 47 url: "http://127.0.0.1:1234/test", |
| 42 wnd.gBrowser.getBrowserForTab(tab).addEventListener("abp:frameready", func tion(event) | 48 inBackground: false, |
| 43 { | 49 onReady: function(aTab) |
| 44 start(); | 50 { |
| 45 }, false, true); | 51 tab = aTab; |
| 52 var worker = tab.attach({ | |
| 53 contentScript: "(" + function() | |
| 54 { | |
| 55 if (document.getElementById("link")) | |
| 56 self.port.emit("done"); | |
| 57 } + ")()" | |
| 58 }); | |
|
Wladimir Palant
2016/01/06 12:54:36
As mentioned before, I don't think that clicking t
| |
| 59 | |
| 60 worker.port.once("done", start) | |
|
Wladimir Palant
2016/01/06 12:54:36
Nit: Missing semicolon here.
| |
| 61 } | |
| 62 }); | |
| 46 | 63 |
| 47 stop(); | 64 stop(); |
| 48 }, | 65 }, |
| 49 teardown: function() | 66 afterEach: function() |
| 50 { | 67 { |
| 51 restoreFilterComponents.call(this); | 68 restoreFilterComponents.call(this); |
| 52 restorePrefs.call(this); | 69 restorePrefs.call(this); |
| 53 | 70 |
| 54 stop(); | 71 stop(); |
| 55 server.stop(function() | 72 server.stop(function() |
| 56 { | 73 { |
| 57 wnd.gBrowser.removeTab(tab); | 74 tab.close(function() |
| 58 | 75 { |
| 59 server = null; | 76 server = null; |
| 60 frame = null; | 77 start(); |
| 61 | 78 }); |
| 62 start(); | |
| 63 }); | 79 }); |
| 64 } | 80 } |
| 65 }); | 81 }); |
| 66 | 82 |
| 67 let tests = [ | 83 let tests = [ |
| 68 ["||127.0.0.1:1234/target$popup", false], | 84 ["||127.0.0.1:1234/target$popup", false], |
| 69 ["||127.0.0.1:1234/target$~subdocument", true], | 85 ["||127.0.0.1:1234/target$~subdocument", true], |
| 70 ["||127.0.0.1:1234/target$popup,domain=127.0.0.1", false], | 86 ["||127.0.0.1:1234/target$popup,domain=127.0.0.1", false], |
| 71 ["||127.0.0.1:1234/target$popup,domain=128.0.0.1", true], | 87 ["||127.0.0.1:1234/target$popup,domain=128.0.0.1", true], |
| 72 ["||127.0.0.1:1234/redirect$popup", false], | 88 ["||127.0.0.1:1234/redirect$popup", false], |
| 73 ["||127.0.0.1:1234/redirect$~subdocument", true], | 89 ["||127.0.0.1:1234/redirect$~subdocument", true], |
| 74 ["||127.0.0.1:1234/redirect$popup,domain=127.0.0.1", false], | 90 ["||127.0.0.1:1234/redirect$popup,domain=127.0.0.1", false], |
| 75 ["||127.0.0.1:1234/redirect$popup,domain=128.0.0.1", true], | 91 ["||127.0.0.1:1234/redirect$popup,domain=128.0.0.1", true], |
| 76 ]; | 92 ]; |
| 77 | 93 |
| 78 function runTest(filter, result) | 94 function runTest(filter, result) |
| 79 { | 95 { |
| 80 FilterStorage.addFilter(filter); | 96 FilterStorage.addFilter(filter); |
| 81 | 97 |
| 82 let successful = false; | 98 let successful = false; |
| 83 | 99 |
| 84 function onTabOpen(event) | 100 function onTabOpen(tab) |
| 85 { | 101 { |
| 102 tabs.off("ready", onTabOpen); | |
| 103 | |
| 104 // link in '/test' was clicked | |
| 105 tab.on("close", onTabClose); | |
| 86 window.clearTimeout(timeout); | 106 window.clearTimeout(timeout); |
| 87 wnd.gBrowser.tabContainer.removeEventListener("TabOpen", onTabOpen, false) ; | |
| 88 | 107 |
| 89 let tab = event.target; | 108 var worker = tab.attach({ |
| 90 let browser = wnd.gBrowser.getBrowserForTab(tab); | 109 contentScriptWhen: "ready", |
| 91 Utils.runAsync(function() | 110 contentScript: "self.port.emit('done', document.body.textContent);" |
| 111 }); | |
| 112 | |
| 113 worker.port.once("done", function(bodyText) | |
| 92 { | 114 { |
| 93 browser.contentWindow.addEventListener("load", function(event) | 115 if (bodyText.indexOf("OK") >= 0) |
| 94 { | 116 successful = true; |
| 95 if (browser.contentDocument.body.textContent.indexOf("OK") >= 0) | |
| 96 successful = true; | |
| 97 | 117 |
| 98 browser.contentWindow.close(); | 118 // pop-up was not blocked so close it |
| 99 }, false); | 119 tab.close(); |
| 100 }); | 120 }); |
| 101 } | 121 } |
| 122 tabs.on("ready", onTabOpen); | |
| 102 | 123 |
| 103 function onTabClose(event) | 124 function onTabClose(tab) |
| 104 { | 125 { |
| 105 wnd.gBrowser.tabContainer.removeEventListener("TabClose", onTabClose, fals e); | 126 tabs.off("ready", onTabOpen); |
| 127 if (tab) | |
| 128 tab.off("close", onTabClose); | |
| 129 | |
| 106 ok(result == successful, "Opening tab with filter " + filter.text); | 130 ok(result == successful, "Opening tab with filter " + filter.text); |
| 107 var keys = []; | |
| 108 for (let key in defaultMatcher.blacklist.keywordByFilter) | |
| 109 keys.push(key); | |
| 110 | 131 |
| 111 FilterStorage.removeFilter(filter); | 132 FilterStorage.removeFilter(filter); |
| 133 | |
| 112 start(); | 134 start(); |
| 113 } | 135 } |
| 114 | 136 |
| 115 wnd.gBrowser.tabContainer.addEventListener("TabOpen", onTabOpen, false); | 137 // In case the tab isn't opened |
| 116 wnd.gBrowser.tabContainer.addEventListener("TabClose", onTabClose, false); | 138 let timeout = window.setTimeout(onTabClose, 1000, null); |
| 117 let timeout = window.setTimeout(onTabClose, 1000); // In case the tab isn 't opened | |
| 118 | 139 |
| 119 wnd.gBrowser.getBrowserForTab(tab).contentDocument.getElementById("link").cl ick(); | 140 // click the link in the '/test' tab opened before the test |
| 141 var worker = tab.attach({ | |
| 142 contentScriptWhen: "ready", | |
| 143 contentScript: "(" + function() | |
| 144 { | |
| 145 document.getElementById('link').click(); | |
| 146 } + ")()" | |
| 147 }); | |
| 120 } | 148 } |
| 121 | 149 |
| 122 for (let [filter, result] of tests) | 150 for (let [filter, result] of tests) |
| 123 asyncTest(filter, runTest.bind(null, Filter.fromText(filter), result)); | 151 asyncTest(filter, runTest.bind(null, Filter.fromText(filter), result)); |
| 124 })(); | 152 })(); |
| OLD | NEW |