OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 if (top === window) { |
| 6 testTop(); |
| 7 } else if (!parent.location.search.includes('end') || |
| 8 window.didRunAtDocumentEnd) { |
| 9 testChild(); |
| 10 } |
| 11 |
| 12 function testTop() { |
| 13 var testMessage = {}; |
| 14 function reportFrames() { |
| 15 testMessage.frameCount = frames.length; |
| 16 testMessage.frameHTML = window.frameHTML; |
| 17 chrome.runtime.sendMessage(testMessage); |
| 18 } |
| 19 |
| 20 if (window.frameHTML) { // Set by child frame... |
| 21 // about:blank frames are synchronously parsed, so their document_end script |
| 22 // injection happens before the main frame's injection. |
| 23 var expectChildBeforeMain = location.search.includes('?blankend'); |
| 24 if (!expectChildBeforeMain) { |
| 25 // Add a message to the test notification to cause the test to fail, |
| 26 // with some useful information for diagnostics. |
| 27 testMessage.warning = 'Content script in child frame was executed ' + |
| 28 'before the main frame\'s content script!'; |
| 29 } |
| 30 reportFrames(); |
| 31 } else { |
| 32 window.frameHTML = '(not set)'; |
| 33 window.onmessage = function(event) { |
| 34 chrome.test.assertEq('toBeRemoved', event.data); |
| 35 reportFrames(); |
| 36 }; |
| 37 } |
| 38 } |
| 39 |
| 40 function testChild() { |
| 41 var TEST_HOST = parent.location.hostname; |
| 42 |
| 43 if (TEST_HOST === 'synchronous') { |
| 44 doRemove(); |
| 45 } else if (TEST_HOST === 'microtask') { |
| 46 Promise.resolve().then(doRemove); |
| 47 } else if (TEST_HOST === 'macrotask') { |
| 48 setTimeout(doRemove, 0); |
| 49 } else if (TEST_HOST.startsWith('domnodeinserted')) { |
| 50 removeOnEvent('DOMNodeInserted'); |
| 51 } else if (TEST_HOST.startsWith('domsubtreemodified')) { |
| 52 removeOnEvent('DOMSubtreeModified'); |
| 53 } else { |
| 54 console.error('Unexpected test: ' + TEST_HOST); |
| 55 chrome.test.fail(); |
| 56 } |
| 57 |
| 58 function doRemove() { |
| 59 parent.frameHTML = document.documentElement.outerHTML || '(no outerHTML)'; |
| 60 parent.postMessage('toBeRemoved', '*'); |
| 61 // frameElement = <iframe> element in parent document. |
| 62 frameElement.remove(); |
| 63 } |
| 64 |
| 65 function removeOnEvent(eventName) { |
| 66 var expected = parseInt(TEST_HOST.match(/\d+/)[0]); |
| 67 document.addEventListener(eventName, function() { |
| 68 // Synchronously remove the frame in the mutation event. |
| 69 if (--expected === 0) |
| 70 doRemove(); |
| 71 }); |
| 72 |
| 73 // Fallback in case the mutation events are not triggered. |
| 74 new Promise(function(resolve) { |
| 75 // The window.onload event signals that the document and its resources |
| 76 // have finished loading, so we don't expect any other parser-initiated |
| 77 // DOM mutations after that point. |
| 78 if (document.readyState === 'complete') |
| 79 resolve(); |
| 80 else |
| 81 window.addEventListener('load', resolve); |
| 82 }).then(function() { |
| 83 if (expected > 0) { |
| 84 expected = 0; |
| 85 // Print this message to make it clear that the expected condition |
| 86 // (mutation event |eventName| triggered XXX times) did not happen. |
| 87 console.log('Mutation condition not triggered: ' + TEST_HOST); |
| 88 doRemove(); |
| 89 } |
| 90 }); |
| 91 |
| 92 } |
| 93 } |
OLD | NEW |