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

Unified Diff: chrome/test/data/extensions/api_test/executescript/frame_id/test.js

Issue 1628423002: Add frameId to chrome.tabs.executeScript/insertCSS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@permissiondata-remove-process_id
Patch Set: Revert to wc->GetURL for chrome.tabs.update Created 4 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: chrome/test/data/extensions/api_test/executescript/frame_id/test.js
diff --git a/chrome/test/data/extensions/api_test/executescript/frame_id/test.js b/chrome/test/data/extensions/api_test/executescript/frame_id/test.js
new file mode 100644
index 0000000000000000000000000000000000000000..06499f134a3a0be15942909c81cf3dacda2b5afb
--- /dev/null
+++ b/chrome/test/data/extensions/api_test/executescript/frame_id/test.js
@@ -0,0 +1,372 @@
+// 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.
+
+var pass = chrome.test.callbackPass;
+var fail = chrome.test.callbackFail;
+var assertEq = chrome.test.assertEq;
+var assertTrue = chrome.test.assertTrue;
+var relativePath = '/extensions/api_test/executescript/frame_id/frames.html';
+var testOrigin = 'http://a.com:PORT';
+var testUrl = 'http://a.com:PORT' + relativePath;
+
+// Frame ID of every frame in this test, and the patterns of the frame URLs.
+// All patterns are mutually exclusive.
+
+var tabId;
+// Main frame.
+var id_frame_top = 0;
Devlin 2016/01/25 19:36:40 nit: js style
robwu 2016/01/26 11:03:02 Done.
+var r_frame_top = /frames\.html/;
+// Frame with (same-origin) about:srcdoc.
+var id_frame_srcdoc;
+var r_frame_srcdoc = /about:srcdoc/;
+// Frame with (unique-origin) sandboxed about:blank.
+var id_frame_unreachable;
+var r_frame_unreachable = /about:blank/;
+// Frame with same-origin page.
+var id_frame_second;
+var r_frame_second = /frame\.html/;
+// Same-origin child frame of |frame_second|.
+var id_frame_third;
+var r_frame_third = /nested\.html/;
+// Frame for which the extension does not have the right permissions.
+var id_frame_nopermission;
+var r_frame_nopermission = /empty\.html/;
+
+function matchesAny(urls, regex) {
+ return urls.some(function(url) { return regex.test(url); });
+}
+
+var g_cssCounter = 0;
+// Calls chrome.tabs.insertCSS and invoke the callback with a list of affected
+// URLs. This function assumes that the insertCSS call will succeed.
+function insertCSS(tabId, injectDetails, callback) {
+ var marker = (++g_cssCounter) + 'px';
+ injectDetails.code = 'body { min-width: ' + marker + ';}';
+ chrome.tabs.insertCSS(tabId, injectDetails, function() {
+ chrome.test.assertNoLastError();
+ chrome.tabs.executeScript(
+ tabId, {
+ code: '[getComputedStyle(document.body).minWidth, document.URL];',
+ allFrames: true,
+ matchAboutBlank: true
+ },
+ function(results) {
+ chrome.test.assertNoLastError();
+ results = results
+ .filter(function(result) {
+ return result && result[0] === marker;
+ })
+ .map(function(result) {
+ return result[1]; // "document.URL"
+ });
+ callback(results);
+ });
+ });
+}
+
+chrome.test.getConfig(function(config) {
+ testOrigin = testOrigin.replace(/PORT/, config.testServer.port);
+ testUrl = testUrl.replace(/PORT/, config.testServer.port);
+ chrome.tabs.onUpdated.addListener(function(_, changeInfo, tab) {
+ if (changeInfo.status != 'complete' || tab.id !== tabId)
+ return;
+
+ chrome.webNavigation.getAllFrames({tabId: tabId}, function(frames) {
+ function getFrameId(r_url) {
+ var filtered =
+ frames.filter(function(frame) { return r_url.test(frame.url); });
+ // Sanity check.
+ chrome.test.assertEq(1, filtered.length);
+ chrome.test.assertTrue(filtered[0].frameId > 0);
+ return filtered[0].frameId;
+ }
+
+ id_frame_srcdoc = getFrameId(r_frame_srcdoc);
+ id_frame_unreachable = getFrameId(r_frame_unreachable);
+ id_frame_second = getFrameId(r_frame_second);
+ id_frame_third = getFrameId(r_frame_third);
+ id_frame_nopermission = getFrameId(r_frame_nopermission);
+
+ runTests(config);
+ });
+ });
+
+ chrome.tabs.create({url: testUrl}, function(tab) { tabId = tab.id; });
+});
+
+function runTests(config) {
+ chrome.test.runTests([
+ function executeScriptFrameIdTop() {
+ chrome.tabs.executeScript(
+ tabId, {frameId: 0, code: 'document.URL'}, pass(function(results) {
+ assertEq(1, results.length);
+ assertTrue(matchesAny(results, r_frame_top));
+ }));
+ },
+
+ function executeScriptFrameIdTopAllFrames() {
+ chrome.tabs.executeScript(
+ tabId, {
+ frameId: 0,
+ matchAboutBlank: true,
+ allFrames: true,
+ code: 'document.URL'
+ },
+ pass(function(results) {
+ assertEq(4, results.length);
+ assertTrue(matchesAny(results, r_frame_top));
+ assertTrue(matchesAny(results, r_frame_srcdoc));
+ assertTrue(matchesAny(results, r_frame_second));
+ assertTrue(matchesAny(results, r_frame_third));
+ }));
+ },
+
+ function executeScriptFrameIdSrcdoc() {
+ chrome.tabs.executeScript(
+ tabId, {
+ frameId: id_frame_srcdoc,
+ matchAboutBlank: true,
+ code: 'document.URL'
+ },
+ pass(function(results) {
+ assertEq(1, results.length);
+ assertTrue(matchesAny(results, r_frame_srcdoc));
+ }));
+ },
+
+ function executeScriptFrameIdSrcdocWithoutMatchAboutBlank() {
+ // TODO(robwu): Why is the origin serialized as "about:blank" instead of
+ // "about:srcdoc"?
+ chrome.tabs.executeScript(
+ tabId, {frameId: id_frame_srcdoc, code: 'document.URL'},
+ fail(
+ 'Cannot access "about:blank" at origin "' + testOrigin + '". ' +
+ 'Extension must have permission to access the frame\'s origin, ' +
+ 'and matchAboutBlank must be true.'));
+ },
+
+ function executeScriptFrameIdSrcdocAllFrames() {
+ chrome.tabs.executeScript(
+ tabId, {
+ frameId: id_frame_srcdoc,
+ matchAboutBlank: true,
+ allFrames: true,
+ code: 'document.URL'
+ },
+ pass(function(results) {
+ assertEq(1, results.length);
+ assertTrue(matchesAny(results, r_frame_srcdoc));
+ }));
+ },
+
+ function executeScriptFrameIdSandboxedFrame() {
+ chrome.tabs.executeScript(
+ tabId, {
+ frameId: id_frame_unreachable,
+ matchAboutBlank: true,
+ code: 'document.URL'
+ },
+ fail(
+ 'Cannot access "about:blank" at origin "null". Extension must ' +
+ 'have permission to access the frame\'s origin, and ' +
+ 'matchAboutBlank must be true.'));
+ },
+
+ function executeScriptFrameIdSubframe() {
+ chrome.tabs.executeScript(
+ tabId, {frameId: id_frame_second, code: 'document.URL'},
+ pass(function(results) {
+ assertEq(1, results.length);
+ assertTrue(matchesAny(results, r_frame_second));
+ }));
+ },
+
+ function executeScriptFrameIdSubframeAllFrames() {
+ chrome.tabs.executeScript(
+ tabId,
+ {frameId: id_frame_second, allFrames: true, code: 'document.URL'},
+ pass(function(results) {
+ assertEq(2, results.length);
+ assertTrue(matchesAny(results, r_frame_second));
+ assertTrue(matchesAny(results, r_frame_third));
+ }));
+ },
+
+ function executeScriptFrameIdNestedFrame() {
+ chrome.tabs.executeScript(
+ tabId, {frameId: id_frame_third, code: 'document.URL'},
+ pass(function(results) {
+ assertEq(1, results.length);
+ assertTrue(matchesAny(results, r_frame_third));
+ }));
+ },
+
+ function executeScriptFrameIdNestedFrame() {
+ chrome.tabs.executeScript(
+ tabId,
+ {frameId: id_frame_third, allFrames: true, code: 'document.URL'},
+ pass(function(results) {
+ assertEq(1, results.length);
+ assertTrue(matchesAny(results, r_frame_third));
+ }));
+ },
+
+ function executeScriptFrameIdNoPermission() {
+ chrome.tabs.executeScript(
+ tabId, {frameId: id_frame_nopermission, code: 'document.URL'},
+ fail(
+ 'Cannot access contents of url "http://c.com:' +
+ config.testServer.port + '/empty.html". Extension manifest ' +
+ 'must request permission to access this host.'));
+ },
+
+ function executeScriptFrameIdNonExistent() {
+ chrome.tabs.executeScript(
+ tabId, {frameId: 999999999, code: 'document.URL'},
+ fail('No frame with id 999999999 in tab ' + tabId + '.'));
+ },
+
+ function executeScriptFrameIdNegative() {
+ try {
+ chrome.tabs.executeScript(
+ tabId, {frameId: -1, code: 'document.URL'}, function() {
+ chrome.test.fail(
+ 'executeScript should never have been executed!');
+ });
+ } catch (e) {
+ assertEq(
+ 'Invalid value for argument 2. Property \'frameId\': ' +
+ 'Value must not be less than 0.',
+ e.message);
+ chrome.test.succeed();
+ }
+ },
+
+ function insertCSSFrameIdTop() {
+ insertCSS(tabId, {frameId: 0}, pass(function(results) {
+ assertEq(1, results.length);
+ assertTrue(matchesAny(results, r_frame_top));
+ }));
+ },
+
+ function insertCSSFrameIdTopAllFrames() {
+ insertCSS(
+ tabId, {frameId: 0, matchAboutBlank: true, allFrames: true},
+ pass(function(results) {
+ assertEq(4, results.length);
+ assertTrue(matchesAny(results, r_frame_top));
+ assertTrue(matchesAny(results, r_frame_srcdoc));
+ assertTrue(matchesAny(results, r_frame_second));
+ assertTrue(matchesAny(results, r_frame_third));
+ }));
+ },
+
+ function insertCSSFrameIdSrcdoc() {
+ insertCSS(
+ tabId, {frameId: id_frame_srcdoc, matchAboutBlank: true},
+ pass(function(results) {
+ assertEq(1, results.length);
+ assertTrue(matchesAny(results, r_frame_srcdoc));
+ }));
+ },
+
+ function insertCSSFrameIdSrcdocWithoutMatchAboutBlank() {
+ // TODO(robwu): Why is the origin serialized as "about:blank" instead of
+ // "about:srcdoc"?
+ chrome.tabs.insertCSS(
+ tabId, {frameId: id_frame_srcdoc, code: 'body{color:red;}'},
+ fail(
+ 'Cannot access "about:blank" at origin "' + testOrigin + '". ' +
+ 'Extension must have permission to access the frame\'s origin, ' +
+ 'and matchAboutBlank must be true.'))
+ },
+
+ function insertCSSFrameIdSrcdocAllFrames() {
+ insertCSS(
+ tabId,
+ {frameId: id_frame_srcdoc, matchAboutBlank: true, allFrames: true},
+ pass(function(results) {
+ assertEq(1, results.length);
+ assertTrue(matchesAny(results, r_frame_srcdoc));
+ }));
+ },
+
+ function insertCSSFrameIdSandboxedFrame() {
+ chrome.tabs.insertCSS(
+ tabId, {
+ frameId: id_frame_unreachable,
+ matchAboutBlank: true,
+ code: 'body{color:red}'
+ },
+ fail(
+ 'Cannot access "about:blank" at origin "null". Extension must ' +
+ 'have permission to access the frame\'s origin, and ' +
+ 'matchAboutBlank must be true.'))
+ },
+
+ function insertCSSFrameIdSubframe() {
+ insertCSS(tabId, {frameId: id_frame_second}, pass(function(results) {
+ assertEq(1, results.length);
+ assertTrue(matchesAny(results, r_frame_second));
+ }));
+ },
+
+ function insertCSSFrameIdSubframeAllFrames() {
+ insertCSS(
+ tabId, {frameId: id_frame_second, allFrames: true},
+ pass(function(results) {
+ assertEq(2, results.length);
+ assertTrue(matchesAny(results, r_frame_second));
+ assertTrue(matchesAny(results, r_frame_third));
+ }));
+ },
+
+ function insertCSSFrameIdNestedFrame() {
+ insertCSS(tabId, {frameId: id_frame_third}, pass(function(results) {
+ assertEq(1, results.length);
+ assertTrue(matchesAny(results, r_frame_third));
+ }));
+ },
+
+ function insertCSSFrameIdNestedFrame() {
+ insertCSS(
+ tabId, {frameId: id_frame_third, allFrames: true},
+ pass(function(results) {
+ assertEq(1, results.length);
+ assertTrue(matchesAny(results, r_frame_third));
+ }));
+ },
+
+ function insertCSSFrameIdNoPermission() {
+ chrome.tabs.insertCSS(
+ tabId, {frameId: id_frame_nopermission, code: 'body{color:red}'},
+ fail(
+ 'Cannot access contents of url "http://c.com:' +
+ config.testServer.port + '/empty.html". Extension manifest ' +
+ 'must request permission to access this host.'));
+ },
+
+ function insertCSSFrameIdNonExistent() {
+ chrome.tabs.insertCSS(
+ tabId, {frameId: 999999999, code: 'body{color:red}'},
+ fail('No frame with id 999999999 in tab ' + tabId + '.'));
+ },
+
+ function insertCSSFrameIdNegative() {
+ try {
+ chrome.tabs.insertCSS(
+ tabId, {frameId: -1, code: 'body{color:red}'}, function() {
+ chrome.test.fail('insertCSS should never have been executed!');
+ });
+ } catch (e) {
+ assertEq(
+ 'Invalid value for argument 2. Property \'frameId\': ' +
+ 'Value must not be less than 0.',
+ e.message);
+ chrome.test.succeed();
+ }
+ },
+
+ ]);
+}

Powered by Google App Engine
This is Rietveld 408576698