github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/sockjs-client-1.1.0/tests/lib/iframe.js (about) 1 /* eslint quotes: 0, camelcase: 0 */ 2 'use strict'; 3 4 var expect = require('expect.js') 5 , eventUtils = require('../../lib/utils/event') 6 , browser = require('../../lib/utils/browser') 7 , transportList = require('../../lib/transport-list') 8 , testUtils = require('./test-utils') 9 , echoTests = require('./echo-tests') 10 , IframeTransport = require('../../lib/transport/iframe') 11 ; 12 13 function onunloadTest (code, done) { 14 var hook = testUtils.createIframe(); 15 var i = 0; 16 hook.open = function () { 17 i++; 18 return hook.callback(code); 19 }; 20 hook.load = function () { 21 i++; 22 return setTimeout(function () { 23 hook.iobj.cleanup(); 24 }, 1); 25 }; 26 hook.unload = function () { 27 try { 28 expect(i).to.equal(2); 29 } catch (e) { 30 done(e); 31 hook.del(); 32 return; 33 } 34 35 hook.del(); 36 done(); 37 }; 38 } 39 40 describe('iframe', function () { 41 if (!IframeTransport.enabled()) { 42 it('[unsupported]'); 43 return; 44 } 45 46 if (browser.isOpera()) { 47 it('onunload [unsupported]'); 48 } else { 49 it('onunload', function (done) { 50 this.timeout(5000); 51 onunloadTest("function attachEvent(event, listener) {" + 52 " if (typeof window.addEventListener !== 'undefined') {" + 53 " window.addEventListener(event, listener, false);" + 54 " } else {" + 55 " document.attachEvent('on' + event, listener);" + 56 " window.attachEvent('on' + event, listener);" + 57 " }" + 58 "}" + 59 "attachEvent('load', function(){" + 60 " hook.load();" + 61 "});" + 62 "var w = 0;" + 63 "var run = function(){" + 64 " if(w === 0) {" + 65 " w = 1;" + 66 " hook.unload();" + 67 " }" + 68 "};" + 69 "attachEvent('beforeunload', run);" + 70 "attachEvent('unload', run);", done); 71 }); 72 } 73 74 it('onmessage', function (done) { 75 var hook = testUtils.createIframe(); 76 var i = 0; 77 hook.open = function () { 78 i++; 79 hook.callback("" + 80 "function attachEvent(event, listener) {" + 81 " if (typeof window.addEventListener !== 'undefined') {" + 82 " window.addEventListener(event, listener, false);" + 83 " } else {" + 84 " document.attachEvent('on' + event, listener);" + 85 " window.attachEvent('on' + event, listener);" + 86 " }" + 87 "}" + 88 "attachEvent('message', function(e) {" + 89 " var b = e.data;" + 90 " parent.postMessage(window_id + ' ' + 'e', '*');" + 91 "});" + 92 "parent.postMessage(window_id + ' ' + 's', '*');"); 93 }; 94 eventUtils.attachEvent('message', function (e) { 95 var msgParts = e.data.split(' ') 96 , windowId = msgParts[0] 97 , data = msgParts[1] 98 ; 99 if (windowId === hook.id) { 100 switch (data) { 101 case 's': 102 hook.iobj.loaded(); 103 i++; 104 hook.iobj.post(hook.id + ' ' + 's', testUtils.getSameOriginUrl()); 105 break; 106 case 'e': 107 try { 108 expect(i).to.equal(2); 109 } catch (err) { 110 done(err); 111 hook.iobj.cleanup(); 112 hook.del(); 113 return; 114 } 115 116 hook.iobj.cleanup(); 117 hook.del(); 118 done(); 119 break; 120 } 121 } 122 }); 123 }); 124 }); 125 126 describe('Transports', function () { 127 transportList.forEach(function (Trans) { 128 describe(Trans.transportName, function () { 129 if (!Trans.enabled({ cookie_needed: false, nullOrigin: false, sameScheme: true, sameOrigin: true })) { 130 it('[unsupported]'); 131 return; 132 } 133 134 var transport = Trans.transportName; 135 var soUrl = testUtils.getSameOriginUrl(); 136 describe('same origin', function () { 137 echoTests.echoFromChild(soUrl, transport); 138 }); 139 140 // var corsUrl = testUtils.getCrossOriginUrl(); 141 // if (corsUrl && corsUrl !== soUrl) { 142 // describe('cross origin', function () { 143 // echoTests.echoFromChild(corsUrl, transport); 144 // }); 145 // } 146 }); 147 }); 148 });