зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1351111 - Add unit tests for setTitle and getTitle r=mixedpuppy
MozReview-Commit-ID: KsdqHBzuWJi --HG-- extra : rebase_source : d237063ec7ad3dc007529463ffde4cf1d81ee23e
This commit is contained in:
Родитель
c003484018
Коммит
aedc70e0fc
|
@ -4,6 +4,7 @@ support-files =
|
||||||
../../../../../../toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js
|
../../../../../../toolkit/components/extensions/test/mochitest/chrome_cleanup_script.js
|
||||||
tags = webextensions
|
tags = webextensions
|
||||||
|
|
||||||
|
[test_ext_browserAction_getTitle_setTitle.html]
|
||||||
[test_ext_browserAction_onClicked.html]
|
[test_ext_browserAction_onClicked.html]
|
||||||
[test_ext_pageAction.html]
|
[test_ext_pageAction.html]
|
||||||
[test_ext_pageAction_popup.html]
|
[test_ext_pageAction_popup.html]
|
||||||
|
|
|
@ -0,0 +1,130 @@
|
||||||
|
<!DOCTYPE HTML>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>BrowserAction Test</title>
|
||||||
|
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
|
||||||
|
<script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
|
||||||
|
<script src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
|
||||||
|
<script type="text/javascript" src="head.js"></script>
|
||||||
|
<link rel="stylesheet" href="chrome://mochikit/contents/tests/SimpleTest/test.css"/>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
var {BrowserActions} = SpecialPowers.Cu.import("resource://gre/modules/BrowserActions.jsm", {});
|
||||||
|
|
||||||
|
add_task(async function test_setTitle_and_getTitle() {
|
||||||
|
async function background() {
|
||||||
|
let tabCreatedPromise = new Promise(resolve => {
|
||||||
|
let onTabCreated = tab => {
|
||||||
|
browser.tabs.onCreated.removeListener(onTabCreated);
|
||||||
|
resolve();
|
||||||
|
};
|
||||||
|
browser.tabs.onCreated.addListener(onTabCreated);
|
||||||
|
});
|
||||||
|
|
||||||
|
async function createAndTestNewTab(title, url) {
|
||||||
|
// First make sure the default title is correct.
|
||||||
|
let defaultTitle = await browser.browserAction.getTitle({});
|
||||||
|
browser.test.assertEq("Browser Action", defaultTitle, `Expected the default title to be ${defaultTitle}`);
|
||||||
|
|
||||||
|
// Create a tab.
|
||||||
|
let [tab] = await Promise.all([
|
||||||
|
browser.tabs.create({url}),
|
||||||
|
tabCreatedPromise,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Test that the default title is returned before the title is set for the tab.
|
||||||
|
let tabTitle = await browser.browserAction.getTitle({tabId: tab.id});
|
||||||
|
browser.test.assertEq("Browser Action", tabTitle, "Expected the default title to be returned");
|
||||||
|
|
||||||
|
// Set the title for the new tab and test that getTitle returns the correct title.
|
||||||
|
await browser.browserAction.setTitle({tabId: tab.id, title});
|
||||||
|
tabTitle = await browser.browserAction.getTitle({tabId: tab.id});
|
||||||
|
browser.test.assertEq(title, tabTitle, `Expected the new tab title to be ${title}`);
|
||||||
|
|
||||||
|
return tab;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create and test 3 new tabs.
|
||||||
|
let tab1 = await createAndTestNewTab("tab 1", "about:blank");
|
||||||
|
let tab2 = await createAndTestNewTab("tab 2", "about:blank");
|
||||||
|
let tab3 = await createAndTestNewTab("tab 3", "about:blank");
|
||||||
|
|
||||||
|
// Test the default title again.
|
||||||
|
let title = await browser.browserAction.getTitle({});
|
||||||
|
browser.test.assertEq("Browser Action", title, `Expected the default title to be "Browser Action"`);
|
||||||
|
|
||||||
|
// Update the default title and confirm that the new title is returned.
|
||||||
|
await browser.browserAction.setTitle({title: "Updated Title"});
|
||||||
|
title = await browser.browserAction.getTitle({});
|
||||||
|
browser.test.assertEq("Updated Title", title, `Expected the default title to be "Updated Title"`);
|
||||||
|
|
||||||
|
// Try setting the default title to an empty string and confirm that the original title is still used.
|
||||||
|
browser.browserAction.setTitle({title: ""});
|
||||||
|
title = await browser.browserAction.getTitle({});
|
||||||
|
browser.test.assertEq("Updated Title", title, `Expected the default title to still be "Updated Title"`);
|
||||||
|
|
||||||
|
// Check all of the created tabs now.
|
||||||
|
title = await browser.browserAction.getTitle({tabId: tab1.id});
|
||||||
|
browser.test.assertEq("tab 1", title, `Expected the first tab title to be ${title}`);
|
||||||
|
title = await browser.browserAction.getTitle({tabId: tab2.id});
|
||||||
|
browser.test.assertEq("tab 2", title, `Expected the second tab title to be ${title}`);
|
||||||
|
title = await browser.browserAction.getTitle({tabId: tab3.id});
|
||||||
|
browser.test.assertEq("tab 3", title, `Expected the third tab title to be ${title}`);
|
||||||
|
|
||||||
|
// Unset the title for the first tab and confirm that it is unset.
|
||||||
|
browser.browserAction.setTitle({tabId: tab1.id, title: ""});
|
||||||
|
title = await browser.browserAction.getTitle({tabId: tab1.id});
|
||||||
|
browser.test.assertEq("Updated Title", title, `Expected the default title to be returned`);
|
||||||
|
|
||||||
|
browser.test.onMessage.addListener(async (msg, data) => {
|
||||||
|
if (msg === "select-tab") {
|
||||||
|
await browser.tabs.update(data, {active: true});
|
||||||
|
browser.test.sendMessage("tab-selected");
|
||||||
|
} else if (msg == "finish") {
|
||||||
|
// Close the tabs
|
||||||
|
await browser.tabs.remove([tab1.id, tab2.id, tab3.id]);
|
||||||
|
browser.test.notifyPass("browserAction.setTitleAndGetTitle");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
browser.test.sendMessage("tabs", {tab1, tab2, tab3});
|
||||||
|
}
|
||||||
|
|
||||||
|
const extension = ExtensionTestUtils.loadExtension({
|
||||||
|
background,
|
||||||
|
manifest: {
|
||||||
|
"name": name,
|
||||||
|
"browser_action": {
|
||||||
|
"default_title": "Browser Action",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await extension.startup();
|
||||||
|
|
||||||
|
let tabs = await extension.awaitMessage("tabs");
|
||||||
|
|
||||||
|
async function checkTab(tab, name) {
|
||||||
|
extension.sendMessage("select-tab", tab.id);
|
||||||
|
await extension.awaitMessage("tab-selected");
|
||||||
|
is(BrowserActions.getNameForActiveTab(`{${extension.uuid}}`), name, "Got the expected name for the active browser action");
|
||||||
|
}
|
||||||
|
|
||||||
|
await checkTab(tabs.tab1, "Updated Title");
|
||||||
|
await checkTab(tabs.tab2, "tab 2");
|
||||||
|
await checkTab(tabs.tab3, "tab 3");
|
||||||
|
await checkTab(tabs.tab1, "Updated Title");
|
||||||
|
|
||||||
|
extension.sendMessage("finish");
|
||||||
|
await extension.awaitFinish("browserAction.setTitleAndGetTitle");
|
||||||
|
|
||||||
|
await extension.unload();
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
</body>
|
||||||
|
</html>
|
Загрузка…
Ссылка в новой задаче