Chromium Code Reviews| Index: chrome/test/data/extensions/api_test/executescript/destructive/remove_self.js |
| diff --git a/chrome/test/data/extensions/api_test/executescript/destructive/remove_self.js b/chrome/test/data/extensions/api_test/executescript/destructive/remove_self.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..286b7c253645f53e0439d6de20c6bfb94626f435 |
| --- /dev/null |
| +++ b/chrome/test/data/extensions/api_test/executescript/destructive/remove_self.js |
| @@ -0,0 +1,90 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +if (top === window) { |
| + testTop(); |
| +} else { |
| + testChild(); |
| +} |
| + |
| +function testTop() { |
| + var testMessage = {}; |
| + function reportFrames() { |
| + if (reportFrames.didRun) |
| + return; |
| + reportFrames.didRun = true; |
| + testMessage.frameCount = frames.length; |
| + testMessage.frameHTML = window.frameHTML; |
| + chrome.runtime.sendMessage(testMessage); |
| + } |
| + window.onmessage = function(event) { |
| + if (event.data === 'toBeRemoved') |
|
Devlin
2016/03/15 16:34:34
Do we expect any other messages? Can this be an a
robwu
2016/03/16 22:03:32
Done.
|
| + reportFrames(); |
| + }; |
| + window.onload = function() { |
| + // Fall back, in case the content script never ran. |
|
Devlin
2016/03/15 16:34:35
If the content script never runs, shouldn't this b
robwu
2016/03/16 22:03:32
This event was too defensive and is not necessary.
|
| + setTimeout(reportFrames, 3000); |
| + }; |
| + |
| + if (window.frameHTML) { // Set by child frame... |
| + // about:blank frames are synchronously parsed, so their document_end script |
| + // injection happens before the main frame's injection. |
| + var expectChildBeforeMain = location.search.includes('?blankend'); |
| + if (!expectChildBeforeMain) { |
| + // Add a message to the test notification to cause the test to fail, |
| + // with some useful information for diagnostics. |
| + testMessage.warning = 'Content script in child frame was executed ' + |
| + 'before the main frame\'s content script!'; |
|
Devlin
2016/03/15 00:53:32
nit: indentation
robwu
2016/03/16 22:03:32
Done.
|
| + } |
| + reportFrames(); |
| + } |
| + window.frameHTML = '(not set)'; |
| +} |
| + |
| +function testChild() { |
| + var TEST_HOST = parent.location.hostname; |
| + |
| + if (TEST_HOST === 'synchronous') { |
| + doRemove(); |
| + } else if (TEST_HOST === 'microtask') { |
| + Promise.resolve().then(doRemove); |
| + } else if (TEST_HOST === 'macrotask') { |
| + setTimeout(doRemove, 0); |
| + } else if (TEST_HOST.startsWith('domnodeinserted')) { |
| + removeOnEvent('DOMNodeInserted'); |
| + } else if (TEST_HOST.startsWith('domsubtreemodified')) { |
| + removeOnEvent('DOMSubtreeModified'); |
| + } else { |
| + console.error('Unexpected test: ' + TEST_HOST); |
| + chrome.test.fail(); |
| + } |
| + |
| + function doRemove() { |
| + parent.frameHTML = document.documentElement.outerHTML || '(no outerHTML)'; |
| + parent.postMessage('toBeRemoved', '*'); |
| + // frameElement = <iframe> element in parent document. |
| + frameElement.remove(); |
| + } |
| + |
| + function removeOnEvent(eventName) { |
| + // For document_start scripts, DOM mutation events are likely, so pick a |
| + // generous time-out delay to make sure that all parser-initiated DOM |
| + // mutations have been processed. |
| + // DOM mutation events are not really expected in document_end scripts, so |
| + // use a very small time-out delay. |
| + var delay = parent.location.search.includes('?start') ? 1000 : 50; |
| + var expected = parseInt(TEST_HOST.match(/\d+/)[0]); |
| + // Fallback in case the mutation events are not triggered. |
| + setTimeout(function() { |
| + if (expected > 0) { |
| + expected = 0; |
| + doRemove(); |
|
Devlin
2016/03/15 16:34:34
I'm not quite sure I understand the reasoning here
robwu
2016/03/16 22:03:32
I replaced the timer with a window.onload notifica
|
| + } |
| + }, delay); |
| + document.addEventListener(eventName, function() { |
| + if (--expected === 0) |
| + doRemove(); |
| + }); |
| + } |
| +} |