Skip to content

Commit

Permalink
[Extensions] Convert more tabs API tests to run as service workers.
Browse files Browse the repository at this point in the history
This CL mostly moves the .js file for each test into a separate
extension.

There were tests in crud.js that are not compatible with running in
a background page. These tests were moved to crud2.js with some
modified expectations. Once all of the tests are converted, I plan
to collect all of the incompatible tests in a single test suite that
will still run in a window context.

Bug: 1093066
Change-Id: I91f423dfe9e281a82a814745479d8e295cae1cc3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4197303
Auto-Submit: David Bertoni <dbertoni@chromium.org>
Commit-Queue: David Bertoni <dbertoni@chromium.org>
Reviewed-by: Emilia Paz <emiliapaz@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1100818}
  • Loading branch information
David Bertoni authored and Chromium LUCI CQ committed Feb 3, 2023
1 parent 5c618fe commit 000eea3
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 77 deletions.
43 changes: 27 additions & 16 deletions chrome/browser/extensions/api/tabs/tabs_apitest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ExtensionApiTabBackForwardCacheTest : public ExtensionApiTabTest {

class ExtensionApiNewTabTest : public ExtensionApiTabTest {
public:
ExtensionApiNewTabTest() {}
ExtensionApiNewTabTest() : ExtensionApiTabTest(ContextType::kServiceWorker) {}
void SetUpCommandLine(base::CommandLine* command_line) override {
ExtensionApiTabTest::SetUpCommandLine(command_line);
// Override the default which InProcessBrowserTest adds if it doesn't see a
Expand All @@ -96,15 +96,31 @@ class ExtensionApiNewTabTest : public ExtensionApiTabTest {
}
};

IN_PROC_BROWSER_TEST_F(ExtensionApiNewTabTest, Tabs) {
#if 0
INSTANTIATE_TEST_SUITE_P(PersistentBackground,
ExtensionApiNewTabTest,
::testing::Values(ContextType::kPersistentBackground));

INSTANTIATE_TEST_SUITE_P(ServiceWorker,
ExtensionApiNewTabTest,
::testing::Values(ContextType::kServiceWorker));
#else
class ExtensionApiNewTabFlakyTest : public ExtensionApiNewTabTest,
public testing::WithParamInterface<int> {};

INSTANTIATE_TEST_SUITE_P(AAAA,
ExtensionApiNewTabFlakyTest,
testing::Range(0, 500));
#endif

IN_PROC_BROWSER_TEST_P(ExtensionApiNewTabFlakyTest, Tabs) {
// The test creates a tab and checks that the URL of the new tab
// is that of the new tab page. Make sure the pref that controls
// this is set.
browser()->profile()->GetPrefs()->SetBoolean(
prefs::kHomePageIsNewTabPage, true);

ASSERT_TRUE(RunExtensionTest("tabs/basics", {.extension_url = "crud.html"}))
<< message_;
ASSERT_TRUE(RunExtensionTest("tabs/basics/crud")) << message_;
}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, TabAudible) {
Expand All @@ -129,35 +145,30 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, TabDuplicate) {
<< message_;
}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, TabSize) {
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Size) {
// TODO(crbug.com/1240482): the test expectations fail if the window gets CSD
// and becomes smaller because of that. Investigate this and remove the line
// below if possible.
ui::ScopedDisableClientSideDecorationsForTest scoped_disabled_csd;

ASSERT_TRUE(
RunExtensionTest("tabs/basics", {.extension_url = "tab_size.html"}))
<< message_;
ASSERT_TRUE(RunExtensionTest("tabs/basics/tab_size")) << message_;
}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, TabUpdate) {
ASSERT_TRUE(RunExtensionTest("tabs/basics", {.extension_url = "update.html"}))
<< message_;
}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, TabPinned) {
ASSERT_TRUE(RunExtensionTest("tabs/basics", {.extension_url = "pinned.html"}))
<< message_;
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Pinned) {
ASSERT_TRUE(RunExtensionTest("tabs/basics/pinned")) << message_;
}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, TabMove) {
ASSERT_TRUE(RunExtensionTest("tabs/basics", {.extension_url = "move.html"}))
<< message_;
ASSERT_TRUE(RunExtensionTest("tabs/basics/move")) << message_;
}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, TabEvents) {
ASSERT_TRUE(RunExtensionTest("tabs/basics", {.extension_url = "events.html"}))
<< message_;
IN_PROC_BROWSER_TEST_P(ExtensionApiTabTestWithContextType, Events) {
ASSERT_TRUE(RunExtensionTest("tabs/basics/events")) << message_;
}

IN_PROC_BROWSER_TEST_F(ExtensionApiTabTest, TabRelativeURLs) {
Expand Down
7 changes: 0 additions & 7 deletions chrome/test/data/extensions/api_test/tabs/basics/crud.html

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
// found in the LICENSE file.

var firstWindowId;
var lastWindowId;

const scriptUrl = '_test_resources/api_test/tabs/basics/tabs_util.js';
let loadScript = chrome.test.loadScript(scriptUrl);

loadScript.then(async function() {
chrome.test.runTests([
function getSelected() {
chrome.tabs.getSelected(null, pass(function(tab) {
assertEq(location.href, tab.url);
assertEq(location.href, tab.title);
assertEq('about:blank', tab.url);
assertEq('about:blank', tab.title);
firstWindowId = tab.windowId;
}));
},
Expand Down Expand Up @@ -102,21 +107,7 @@ chrome.test.runTests([
assertEq(tab.id, win.tabs[0].id);
assertEq(win.id, win.tabs[0].windowId);
assertEq(pageUrl('a'), win.tabs[0].pendingUrl);
}));
}));
},

function getAllInWindowNullArg() {
chrome.tabs.getAllInWindow(null, pass(function(tabs) {
assertEq(6, tabs.length);
assertEq(firstWindowId, tabs[0].windowId);
}));
},

function detectLanguage() {
chrome.tabs.getAllInWindow(firstWindowId, pass(function(tabs) {
chrome.tabs.detectLanguage(tabs[0].id, pass(function(lang) {
assertEq("und", lang);
lastWindowId = win.id;
}));
}));
},
Expand All @@ -133,13 +124,4 @@ chrome.test.runTests([
}));
},

function getCurrentWindow() {
var errorMsg = "No window with id: -1.";
chrome.windows.get(chrome.windows.WINDOW_ID_NONE, fail(errorMsg));
chrome.windows.get(chrome.windows.WINDOW_ID_CURRENT, pass(function(win1) {
chrome.windows.getCurrent(pass(function(win2) {
assertEq(win1.id, win2.id);
}));
}));
}
]);
])});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "chrome.tabs",
"version": "0.1",
"manifest_version": 2,
"description": "end-to-end browser test for chrome.tabs API",
"permissions": ["tabs"],
"background": {
"scripts": ["crud.js"],
"persistent": true
}
}
43 changes: 43 additions & 0 deletions chrome/test/data/extensions/api_test/tabs/basics/crud2.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var newTabUrls = [
'chrome-search://local-ntp/local-ntp.html',
];

var firstWindowId;
var secondWindowId;
var thirdWindowId;
var testTabId;
Expand All @@ -20,6 +21,12 @@ function clickLink(id) {

chrome.test.runTests([

function getFirstWindowId() {
chrome.windows.getCurrent(pass(function(window) {
firstWindowId = window.id;
}));
},

function setupTwoWindows() {
createWindow(["about:blank", "chrome://newtab/", pageUrl("a")], {},
pass(function(winId, tabIds) {
Expand Down Expand Up @@ -65,6 +72,16 @@ chrome.test.runTests([
}));
},

// TODO(crbug.com/1093066): This test must either be run in a window context
// where the current window is guaranteed, or be moved to the interactive
// test so there's consistent focus to guarantee which window is the default.
function getAllInWindowNullArg() {
chrome.tabs.getAllInWindow(null, pass(function(tabs) {
assertEq(1, tabs.length);
assertEq(firstWindowId, tabs[0].windowId);
}));
},

function updateSelect() {
chrome.tabs.getAllInWindow(secondWindowId, pass(function(tabs) {
assertEq(true, tabs[0].active && tabs[0].selected);
Expand Down Expand Up @@ -194,4 +211,30 @@ chrome.test.runTests([
}));
},

// TODO(crbug.com/1093066): This test only runs in a window context.
// We should collect it with other such tests and put them together
// in a new test suite. Also, chrome.tabs.detectLanguage has been
// broken for a while, so this test really isn't accomplishing
// anything right now. See crbug.com/1410643.
function detectLanguage() {
chrome.tabs.getAllInWindow(null, pass(function(tabs) {
chrome.tabs.detectLanguage(tabs[0].id, pass(function(lang) {
assertEq("und", lang);
}));
}));
},

// TODO(crbug.com/1093066): This test must either be run in a window context
// where the current window is guaranteed, or be moved to the interactive
// test so there's consistent focus to guarantee which window is the default.
function getCurrentWindow() {
var errorMsg = "No window with id: -1.";
chrome.windows.get(chrome.windows.WINDOW_ID_NONE, fail(errorMsg));
chrome.windows.get(chrome.windows.WINDOW_ID_CURRENT, pass(function(win1) {
chrome.windows.getCurrent(pass(function(win2) {
assertEq(win1.id, win2.id);
}));
}));
}

]);
7 changes: 0 additions & 7 deletions chrome/test/data/extensions/api_test/tabs/basics/events.html

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ var otherTabId;
var firstWindowId;
var secondWindowId;

const scriptUrl = '_test_resources/api_test/tabs/basics/tabs_util.js';
let loadScript = chrome.test.loadScript(scriptUrl);

loadScript.then(async function() {
chrome.test.runTests([
function init() {
chrome.tabs.getSelected(null, pass(function(tab) {
Expand Down Expand Up @@ -212,4 +216,4 @@ chrome.test.runTests([
}));
}
*/
]);
])});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "chrome.tabs",
"version": "0.1",
"manifest_version": 2,
"description": "end-to-end browser test for chrome.tabs API",
"permissions": ["tabs"],
"background": {
"scripts": ["events.js"],
"persistent": true
}
}
7 changes: 0 additions & 7 deletions chrome/test/data/extensions/api_test/tabs/basics/move.html

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "chrome.tabs",
"version": "0.1",
"manifest_version": 2,
"description": "end-to-end browser test for chrome.tabs API",
"permissions": ["tabs"],
"background": {
"scripts": ["move.js"],
"persistent": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ function moveTab(tabId, toWindowId, toIndex) {
});
}

const scriptUrl = '_test_resources/api_test/tabs/basics/tabs_util.js';
let loadScript = chrome.test.loadScript(scriptUrl);

loadScript.then(async function() {
chrome.test.runTests([
// Do a series of moves and removes so that we get the following
//
Expand Down Expand Up @@ -414,4 +418,4 @@ chrome.test.runTests([
}).catch(() => {});
});
}
]);
])});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "chrome.tabs",
"version": "0.1",
"manifest_version": 2,
"description": "end-to-end browser test for chrome.tabs API",
"permissions": ["tabs"],
"background": {
"scripts": ["pinned.js"],
"persistent": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

var firstWindowId;

const scriptUrl = '_test_resources/api_test/tabs/basics/tabs_util.js';
let loadScript = chrome.test.loadScript(scriptUrl);

loadScript.then(async function() {
chrome.test.runTests([
function setupWindow() {
createWindow(["about:blank", "chrome://newtab/", pageUrl("a")], {},
Expand Down Expand Up @@ -82,4 +86,4 @@ chrome.test.runTests([
});
}));
}
]);
])});

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "chrome.tabs",
"version": "0.1",
"manifest_version": 2,
"description": "end-to-end browser test for chrome.tabs API",
"permissions": ["tabs"],
"background": {
"scripts": ["tab_size.js"],
"persistent": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
var firstTabId;
var firstTab;

const scriptUrl = '_test_resources/api_test/tabs/basics/tabs_util.js';
let loadScript = chrome.test.loadScript(scriptUrl);

loadScript.then(async function() {
chrome.test.runTests([
function setupWindow() {
createWindow([pageUrl("a")], {}, pass(function(winId, tabIds) {
Expand All @@ -23,4 +27,4 @@ chrome.test.runTests([
assertEq(firstTab.height, tab.height);
}));
}
]);
])});
5 changes: 3 additions & 2 deletions chrome/test/data/extensions/api_test/tabs/basics/tabs_util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ var assertEq = chrome.test.assertEq;
var assertFalse = chrome.test.assertFalse;
var assertTrue = chrome.test.assertTrue;

function pageUrl(letter) {
return chrome.extension.getURL(letter + ".html");
function pageUrl(name) {
return chrome.extension.getURL(
'_test_resources/api_test/tabs/basics/' + name + '.html');
}

// Creates one window with tabs set to the urls in the array |tabUrls|.
Expand Down

0 comments on commit 000eea3

Please sign in to comment.