Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: chrome/test/data/extensions/api_test/executescript/removed_frames/test.js

Issue 1216453002: [Extensions] Handle some funny cases in script injection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Ben's Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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 var responsesReceived = 0;
6 chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
7 if (request == 'fail') {
8 chrome.test.fail();
9 } else {
10 chrome.test.assertEq('complete', request);
11 ++responsesReceived;
12 }
13 });
14
15 var waitForCommittedAndRun = function(functionToRun, numCommits, url) {
16 responsesReceived = 0;
17 var committedCount = 0;
18 var onCommitted = function(details) {
19 if (++committedCount == numCommits) {
20 functionToRun(details.tabId);
21 chrome.webNavigation.onCommitted.removeListener(onCommitted);
22 }
23 };
24 chrome.webNavigation.onCommitted.addListener(onCommitted);
25 chrome.tabs.create({url: url});
26 };
27
28 chrome.test.getConfig(function(config) {
29 var url = 'http://a.com:' + config.testServer.port +
30 '/extensions/api_test/executescript/removed_frames/outer.html';
31 // Regression tests for crbug.com/500574.
32 chrome.test.runTests([
33 function() {
34 waitForCommittedAndRun(injectAndDeleteIframeFromMainFrame, 2, url);
35 }
36 // This is another great test to have, but currently it crashes in blink.
37 // TODO(devlin): Fix the crash in blink and enable this!
38 // function() {
39 // waitForCommittedAndRun(injectAndDeleteIframeFromIframe, 2, url);
40 // }
41 ]);
42 });
43
44 function injectAndDeleteIframeFromMainFrame(tabId) {
45 // Inject code into each frame. If it's the parent frame, it removes the child
46 // frame from the DOM (invalidating it). The child frame's code shouldn't
47 // finish executing, since it's been removed.
48 var injectFrameCode = [
49 'if (window !== window.top) {',
50 // There's a (good) chance the child frame reaches "idle" first, since it
51 // is a subresource of the main frame. But it shouldn't stick around for
52 // more than 200ms or so, since that's the max delay for idle. If the
53 // script still tries to execute, it's probably bad.
54 ' window.setTimeout(function() {',
55 ' chrome.runtime.sendMessage("fail");',
56 ' }, 250);',
57 '} else {',
58 ' iframe = document.getElementsByTagName("iframe")[0];',
59 ' iframe.parentElement.removeChild(iframe);',
60 ' chrome.runtime.sendMessage("complete");',
61 '}'
62 ].join('\n');
63 chrome.tabs.executeScript(
64 tabId,
65 {code: injectFrameCode, allFrames: true, runAt: 'document_idle'},
66 function() {
67 chrome.test.assertEq(1, responsesReceived);
68 chrome.test.succeed();
69 });
70 };
71
72 function injectAndDeleteIframeFromIframe(tabId) {
73 responsesReceived = 0;
74 // Inject code into each frame. Have the child frame remove itself, deleting
75 // the frame while it's still executing.
76 var injectFrameCode =
77 'if (window.self !== window.top) {' +
78 ' var iframe = window.top.document.getElementsByTagName("iframe")[0];' +
79 ' if (!iframe || iframe.contentWindow !== window)' +
80 ' chrome.runtime.sendMessage("fail");' +
81 ' else' +
82 ' window.top.document.body.removeChild(iframe);' +
83 '} else {' +
84 ' chrome.runtime.sendMessage("complete");' +
85 '}';
86 // We also use two "executeScript" calls here so that we have a pending script
87 // execution on a frame that gets deleted.
88 chrome.tabs.executeScript(
89 tabId,
90 {code: injectFrameCode, allFrames: true, runAt: 'document_idle'});
91 chrome.tabs.executeScript(
92 tabId,
93 {code: injectFrameCode, allFrames: true, runAt: 'document_idle'},
94 function() {
95 // Script execution, all other things equal, should happen in the order it
96 // was received, so we only need a check in the second callback.
97 chrome.test.assertEq(2, responsesReceived);
98 chrome.test.succeed();
99 });
100 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698