|
| 1 | +/* |
| 2 | + * Copyright (c) 2014 The Polymer Project Authors. All rights reserved. |
| 3 | + * This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt |
| 4 | + * The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 5 | + * The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt |
| 6 | + * Code distributed by Google as part of the polymer project is also |
| 7 | + * subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt |
| 8 | + */ |
| 9 | + |
| 10 | +(function() { |
| 11 | + var thisFile = 'lib/mocha-htmltest.js'; |
| 12 | + var base = ''; |
| 13 | + |
| 14 | + mocha.htmlbase = function(htmlbase) { |
| 15 | + base = htmlbase; |
| 16 | + }; |
| 17 | + |
| 18 | + (function() { |
| 19 | + var s$ = document.querySelectorAll('script[src]'); |
| 20 | + Array.prototype.forEach.call(s$, function(s) { |
| 21 | + var src = s.getAttribute('src'); |
| 22 | + var re = new RegExp(thisFile + '[^\\\\]*'); |
| 23 | + var match = src.match(re); |
| 24 | + if (match) { |
| 25 | + base = src.slice(0, -match[0].length); |
| 26 | + } |
| 27 | + }); |
| 28 | + })(); |
| 29 | + |
| 30 | + var next, iframe; |
| 31 | + |
| 32 | + var listener = function(event) { |
| 33 | + if (event.data === 'ok') { |
| 34 | + next(); |
| 35 | + } else if (event.data && event.data.error) { |
| 36 | + // errors cannot be cloned via postMessage according to spec, so we re-errorify them |
| 37 | + throw new Error(event.data.error); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + function htmlSetup() { |
| 42 | + window.addEventListener("message", listener); |
| 43 | + iframe = document.createElement('iframe'); |
| 44 | + iframe.style.cssText = 'position: absolute; left: -9000em; width:768px; height: 1024px'; |
| 45 | + document.body.appendChild(iframe); |
| 46 | + } |
| 47 | + |
| 48 | + function htmlTeardown() { |
| 49 | + window.removeEventListener('message', listener); |
| 50 | + document.body.removeChild(iframe); |
| 51 | + } |
| 52 | + |
| 53 | + function htmlTest(src) { |
| 54 | + test(src, function(done) { |
| 55 | + next = done; |
| 56 | + var url = base + src; |
| 57 | + var delimiter = url.indexOf('?') < 0 ? '?' : '&'; |
| 58 | + var docSearch = location.search.slice(1); |
| 59 | + iframe.src = url + delimiter + Math.random() + '&' + docSearch; |
| 60 | + }); |
| 61 | + }; |
| 62 | + |
| 63 | + function htmlSuite(inName, inFn) { |
| 64 | + suite(inName, function() { |
| 65 | + setup(htmlSetup); |
| 66 | + teardown(htmlTeardown); |
| 67 | + inFn(); |
| 68 | + }); |
| 69 | + }; |
| 70 | + |
| 71 | + window.htmlTest = htmlTest; |
| 72 | + window.htmlSuite = htmlSuite; |
| 73 | +})(); |
0 commit comments