var TEST_HOST = location.search.slice(1);
chrome.test.assertTrue(
TEST_HOST !== '',
'The subtest type must be specified in the query string.');
var config;
var tabId;
function getTestUrl(page) {
return 'http://' + TEST_HOST + ':' + config.testServer.port +
'/extensions/api_test/executescript/destructive/' + page;
}
chrome.test.getConfig(function(config) {
window.config = config;
chrome.tabs.create({url: 'about:blank'}, function(tab) {
tabId = tab.id;
startTest();
});
});
function startTest() {
var kEmptyHtmlBodyPattern = /<body><\/body>/;
var allTests = [
function removeHttpFrameAtDocumentStart() {
testRemoveSelf('empty_frame.html?start');
},
function removeHttpFrameAtDocumentEnd() {
testRemoveSelf('empty_frame.html?end', kEmptyHtmlBodyPattern);
},
function removeAboutBlankAtDocumentStart() {
testRemoveSelf('about_blank_frame.html?blankstart');
},
function removeAboutBlankAtDocumentEnd() {
testRemoveSelf('about_blank_frame.html?blankend', kEmptyHtmlBodyPattern);
},
function removeAboutSrcdocHtmlAtDocumentStart() {
testRemoveSelf('srcdoc_html_frame.html?blankstart');
},
function removeAboutSrcdocHtmlAtDocumentEnd() {
testRemoveSelf('srcdoc_html_frame.html?blankend', /<br>/);
},
function removeAboutSrcdocTextOnlyAtDocumentStart() {
testRemoveSelf('srcdoc_text_frame.html?blankstart');
},
function removeAboutSrcdocTextOnlyAtDocumentEnd() {
testRemoveSelf('srcdoc_text_frame.html?blankend', /text/);
},
function removeFrameWithoutUrlAtDocumentStart() {
testRemoveSelf('no_url_frame.html?blankstart');
},
function removeFrameWithoutUrlAtDocumentEnd() {
testRemoveSelf('no_url_frame.html?blankend', kEmptyHtmlBodyPattern);
},
function removeImageAtDocumentStart() {
testRemoveSelf('image_frame.html?start');
},
function removeImageAtDocumentEnd() {
testRemoveSelf('image_frame.html?end', /<img/);
},
function removeAudioAtDocumentStart() {
testRemoveSelf('audio_frame.html?start');
},
function removeAudioAtDocumentEnd() {
testRemoveSelf('audio_frame.html?end', /<video.+ type="audio\//);
},
function removeVideoAtDocumentStart() {
testRemoveSelf('video_frame.html?start');
},
function removeVideoAtDocumentEnd() {
testRemoveSelf('video_frame.html?end', /<video.+ type="video\//);
},
function removePluginAtDocumentStart() {
if (maybeSkipPluginTest())
return;
testRemoveSelf('plugin_frame.html?start');
},
function removePluginAtDocumentEnd() {
if (maybeSkipPluginTest())
return;
testRemoveSelf('plugin_frame.html?end');
},
function removePlainTextAtDocumentStart() {
testRemoveSelf('txt_frame.html?start');
},
function removePlainTextAtDocumentEnd() {
testRemoveSelf('txt_frame.html?end', /<pre/);
},
function removeXhtmlAtDocumentStart() {
testRemoveSelf(
'xhtml_frame.html?start',
/<html xmlns="http:\/\/www\.w3\.org\/1999\/xhtml"><\/html>/);
},
function removeXhtmlAtDocumentEnd() {
testRemoveSelf(
'xhtml_frame.html?end',
/<html xmlns="http:\/\/www\.w3\.org\/1999\/xhtml">/);
},
function removeXmlAtDocumentStart() {
testRemoveSelf('xml_frame.html?start', /<root\/>/);
},
function removeXmlAtDocumentEnd() {
testRemoveSelf('xml_frame.html?end', /<root><child\/><\/root>/);
},
];
var testParams = new URLSearchParams(location.hash.slice(1));
var kBucketCount = parseInt(testParams.get('bucketcount'));
var kBucketIndex = parseInt(testParams.get('bucketindex'));
var kTestsPerBucket = Math.ceil(allTests.length / kBucketCount);
chrome.test.assertTrue(kBucketCount * kTestsPerBucket >= allTests.length,
'To cover all tests, the number of buckets multiplied by the number of ' +
'tests per bucket must be at least as big as the number of tests.');
chrome.test.assertTrue(0 <= kBucketIndex >= 0 && kBucketIndex < kBucketCount,
'There are only ' + kBucketCount + ' buckets, so the bucket index must ' +
'be between 0 and ' + kBucketCount + ', but it was ' + kBucketIndex);
var filteredTests =
allTests.slice(kBucketIndex * kTestsPerBucket).slice(0, kTestsPerBucket);
if (TEST_HOST.startsWith('dom')) {
filteredTests = filteredTests.filter(function(testfn) {
return !testfn.name.endsWith('DocumentEnd');
});
}
chrome.test.runTests(filteredTests);
}
function testRemoveSelf(page, pattern) {
var kDefaultPattern = /</;
if (page.includes('start')) {
pattern = pattern || /^<\s*html[^>]*><\/html>$/;
pattern = TEST_HOST === 'synchronous' ? pattern : kDefaultPattern;
} else if (page.includes('end')) {
pattern = pattern || kDefaultPattern;
} else {
chrome.test.fail('URL must contain "start" or "end": ' + page);
}
chrome.test.listenOnce(chrome.runtime.onMessage, function(msg, sender) {
chrome.test.assertEq(tabId, sender.tab && sender.tab.id);
chrome.test.assertEq(0, sender.frameId);
var frameHTML = msg.frameHTML;
delete msg.frameHTML;
chrome.test.assertEq({frameCount: 0}, msg);
chrome.test.assertTrue(
pattern.test(frameHTML),
'The pattern ' + pattern + ' should be matched by: ' + frameHTML);
});
chrome.tabs.update(tabId, {url: getTestUrl(page)});
}
function maybeSkipPluginTest() {
var kPluginMimeType = 'application/pdf';
for (var i = 0; i < navigator.plugins.length; ++i) {
var plugin = navigator.plugins[i];
for (var j = 0; j < plugin.length; ++j) {
var mimeType = plugin[j];
if (mimeType.type === kPluginMimeType)
return false;
}
}
var kMessage = 'Plugin not found for ' + kPluginMimeType + ', skipping test.';
console.log(kMessage);
chrome.test.log(kMessage);
chrome.test.succeed();
return true;
}