diff options
Diffstat (limited to 'browser/components/extensions/test/xpcshell')
8 files changed, 1305 insertions, 0 deletions
diff --git a/browser/components/extensions/test/xpcshell/.eslintrc.js b/browser/components/extensions/test/xpcshell/.eslintrc.js new file mode 100644 index 000000000..2bfe540ea --- /dev/null +++ b/browser/components/extensions/test/xpcshell/.eslintrc.js @@ -0,0 +1,9 @@ +"use strict"; + +module.exports = { // eslint-disable-line no-undef + "extends": "../../../../../testing/xpcshell/xpcshell.eslintrc.js", + + "globals": { + "browser": false, + }, +}; diff --git a/browser/components/extensions/test/xpcshell/head.js b/browser/components/extensions/test/xpcshell/head.js new file mode 100644 index 000000000..de4a4a3f6 --- /dev/null +++ b/browser/components/extensions/test/xpcshell/head.js @@ -0,0 +1,55 @@ +"use strict"; + +const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components; + +/* exported createHttpServer */ + +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); + +XPCOMUtils.defineLazyModuleGetter(this, "AppConstants", + "resource://gre/modules/AppConstants.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "Extension", + "resource://gre/modules/Extension.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "ExtensionData", + "resource://gre/modules/Extension.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "ExtensionManagement", + "resource://gre/modules/ExtensionManagement.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "ExtensionTestUtils", + "resource://testing-common/ExtensionXPCShellUtils.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "FileUtils", + "resource://gre/modules/FileUtils.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "HttpServer", + "resource://testing-common/httpd.js"); +XPCOMUtils.defineLazyModuleGetter(this, "NetUtil", + "resource://gre/modules/NetUtil.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "Schemas", + "resource://gre/modules/Schemas.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "Services", + "resource://gre/modules/Services.jsm"); + +ExtensionTestUtils.init(this); + + +/** + * Creates a new HttpServer for testing, and begins listening on the + * specified port. Automatically shuts down the server when the test + * unit ends. + * + * @param {integer} [port] + * The port to listen on. If omitted, listen on a random + * port. The latter is the preferred behavior. + * + * @returns {HttpServer} + */ +function createHttpServer(port = -1) { + let server = new HttpServer(); + server.start(port); + + do_register_cleanup(() => { + return new Promise(resolve => { + server.stop(resolve); + }); + }); + + return server; +} diff --git a/browser/components/extensions/test/xpcshell/test_ext_bookmarks.js b/browser/components/extensions/test/xpcshell/test_ext_bookmarks.js new file mode 100644 index 000000000..142c0a37c --- /dev/null +++ b/browser/components/extensions/test/xpcshell/test_ext_bookmarks.js @@ -0,0 +1,601 @@ +/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set sts=2 sw=2 et tw=80: */ +"use strict"; + +function backgroundScript() { + let unsortedId, ourId; + let initialBookmarkCount = 0; + let createdBookmarks = new Set(); + let createdFolderId; + let collectedEvents = []; + const nonExistentId = "000000000000"; + const bookmarkGuids = { + menuGuid: "menu________", + toolbarGuid: "toolbar_____", + unfiledGuid: "unfiled_____", + }; + + function checkOurBookmark(bookmark) { + browser.test.assertEq(ourId, bookmark.id, "Bookmark has the expected Id"); + browser.test.assertTrue("parentId" in bookmark, "Bookmark has a parentId"); + browser.test.assertEq(0, bookmark.index, "Bookmark has the expected index"); // We assume there are no other bookmarks. + browser.test.assertEq("http://example.org/", bookmark.url, "Bookmark has the expected url"); + browser.test.assertEq("test bookmark", bookmark.title, "Bookmark has the expected title"); + browser.test.assertTrue("dateAdded" in bookmark, "Bookmark has a dateAdded"); + browser.test.assertFalse("dateGroupModified" in bookmark, "Bookmark does not have a dateGroupModified"); + browser.test.assertFalse("unmodifiable" in bookmark, "Bookmark is not unmodifiable"); + } + + function checkBookmark(expected, bookmark) { + browser.test.assertEq(expected.url, bookmark.url, "Bookmark has the expected url"); + browser.test.assertEq(expected.title, bookmark.title, "Bookmark has the expected title"); + browser.test.assertEq(expected.index, bookmark.index, "Bookmark has expected index"); + if ("parentId" in expected) { + browser.test.assertEq(expected.parentId, bookmark.parentId, "Bookmark has the expected parentId"); + } + } + + function expectedError() { + browser.test.fail("Did not get expected error"); + } + + function checkOnCreated(id, parentId, index, title, url, dateAdded) { + let createdData = collectedEvents.pop(); + browser.test.assertEq("onCreated", createdData.event, "onCreated was the last event received"); + browser.test.assertEq(id, createdData.id, "onCreated event received the expected id"); + let bookmark = createdData.bookmark; + browser.test.assertEq(id, bookmark.id, "onCreated event received the expected bookmark id"); + browser.test.assertEq(parentId, bookmark.parentId, "onCreated event received the expected bookmark parentId"); + browser.test.assertEq(index, bookmark.index, "onCreated event received the expected bookmark index"); + browser.test.assertEq(title, bookmark.title, "onCreated event received the expected bookmark title"); + browser.test.assertEq(url, bookmark.url, "onCreated event received the expected bookmark url"); + browser.test.assertEq(dateAdded, bookmark.dateAdded, "onCreated event received the expected bookmark dateAdded"); + } + + function checkOnChanged(id, url, title) { + // If both url and title are changed, then url is fired last. + let changedData = collectedEvents.pop(); + browser.test.assertEq("onChanged", changedData.event, "onChanged was the last event received"); + browser.test.assertEq(id, changedData.id, "onChanged event received the expected id"); + browser.test.assertEq(url, changedData.info.url, "onChanged event received the expected url"); + // title is fired first. + changedData = collectedEvents.pop(); + browser.test.assertEq("onChanged", changedData.event, "onChanged was the last event received"); + browser.test.assertEq(id, changedData.id, "onChanged event received the expected id"); + browser.test.assertEq(title, changedData.info.title, "onChanged event received the expected title"); + } + + function checkOnMoved(id, parentId, oldParentId, index, oldIndex) { + let movedData = collectedEvents.pop(); + browser.test.assertEq("onMoved", movedData.event, "onMoved was the last event received"); + browser.test.assertEq(id, movedData.id, "onMoved event received the expected id"); + let info = movedData.info; + browser.test.assertEq(parentId, info.parentId, "onMoved event received the expected parentId"); + browser.test.assertEq(oldParentId, info.oldParentId, "onMoved event received the expected oldParentId"); + browser.test.assertEq(index, info.index, "onMoved event received the expected index"); + browser.test.assertEq(oldIndex, info.oldIndex, "onMoved event received the expected oldIndex"); + } + + function checkOnRemoved(id, parentId, index, url) { + let removedData = collectedEvents.pop(); + browser.test.assertEq("onRemoved", removedData.event, "onRemoved was the last event received"); + browser.test.assertEq(id, removedData.id, "onRemoved event received the expected id"); + let info = removedData.info; + browser.test.assertEq(parentId, removedData.info.parentId, "onRemoved event received the expected parentId"); + browser.test.assertEq(index, removedData.info.index, "onRemoved event received the expected index"); + let node = info.node; + browser.test.assertEq(id, node.id, "onRemoved event received the expected node id"); + browser.test.assertEq(parentId, node.parentId, "onRemoved event received the expected node parentId"); + browser.test.assertEq(index, node.index, "onRemoved event received the expected node index"); + browser.test.assertEq(url, node.url, "onRemoved event received the expected node url"); + } + + browser.bookmarks.onChanged.addListener((id, info) => { + collectedEvents.push({event: "onChanged", id, info}); + }); + + browser.bookmarks.onCreated.addListener((id, bookmark) => { + collectedEvents.push({event: "onCreated", id, bookmark}); + }); + + browser.bookmarks.onMoved.addListener((id, info) => { + collectedEvents.push({event: "onMoved", id, info}); + }); + + browser.bookmarks.onRemoved.addListener((id, info) => { + collectedEvents.push({event: "onRemoved", id, info}); + }); + + browser.bookmarks.get(["not-a-bookmark-guid"]).then(expectedError, invalidGuidError => { + browser.test.assertTrue( + invalidGuidError.message.includes("Invalid value for property 'guid': not-a-bookmark-guid"), + "Expected error thrown when trying to get a bookmark using an invalid guid" + ); + + return browser.bookmarks.get([nonExistentId]).then(expectedError, nonExistentIdError => { + browser.test.assertTrue( + nonExistentIdError.message.includes("Bookmark not found"), + "Expected error thrown when trying to get a bookmark using a non-existent Id" + ); + }); + }).then(() => { + return browser.bookmarks.search({}); + }).then(results => { + initialBookmarkCount = results.length; + return browser.bookmarks.create({title: "test bookmark", url: "http://example.org"}); + }).then(result => { + ourId = result.id; + checkOurBookmark(result); + browser.test.assertEq(1, collectedEvents.length, "1 expected event received"); + checkOnCreated(ourId, bookmarkGuids.unfiledGuid, 0, "test bookmark", "http://example.org/", result.dateAdded); + + return browser.bookmarks.get(ourId); + }).then(results => { + browser.test.assertEq(results.length, 1); + checkOurBookmark(results[0]); + + unsortedId = results[0].parentId; + return browser.bookmarks.get(unsortedId); + }).then(results => { + let folder = results[0]; + browser.test.assertEq(1, results.length, "1 bookmark was returned"); + + browser.test.assertEq(unsortedId, folder.id, "Folder has the expected id"); + browser.test.assertTrue("parentId" in folder, "Folder has a parentId"); + browser.test.assertTrue("index" in folder, "Folder has an index"); + browser.test.assertFalse("url" in folder, "Folder does not have a url"); + browser.test.assertEq("Other Bookmarks", folder.title, "Folder has the expected title"); + browser.test.assertTrue("dateAdded" in folder, "Folder has a dateAdded"); + browser.test.assertTrue("dateGroupModified" in folder, "Folder has a dateGroupModified"); + browser.test.assertFalse("unmodifiable" in folder, "Folder is not unmodifiable"); // TODO: Do we want to enable this? + + return browser.bookmarks.getChildren(unsortedId); + }).then(results => { + browser.test.assertEq(1, results.length, "The folder has one child"); + checkOurBookmark(results[0]); + + return browser.bookmarks.update(nonExistentId, {title: "new test title"}).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("No bookmarks found for the provided GUID"), + "Expected error thrown when trying to update a non-existent bookmark" + ); + + return browser.bookmarks.update(ourId, {title: "new test title", url: "http://example.com/"}); + }); + }).then(result => { + browser.test.assertEq("new test title", result.title, "Updated bookmark has the expected title"); + browser.test.assertEq("http://example.com/", result.url, "Updated bookmark has the expected URL"); + browser.test.assertEq(ourId, result.id, "Updated bookmark has the expected id"); + + browser.test.assertEq(2, collectedEvents.length, "2 expected events received"); + checkOnChanged(ourId, "http://example.com/", "new test title"); + + return Promise.resolve().then(() => { + return browser.bookmarks.update(ourId, {url: "this is not a valid url"}); + }).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("Invalid bookmark:"), + "Expected error thrown when trying update with an invalid url" + ); + return browser.bookmarks.getTree(); + }); + }).then(results => { + browser.test.assertEq(1, results.length, "getTree returns one result"); + let bookmark = results[0].children.find(bookmarkItem => bookmarkItem.id == unsortedId); + browser.test.assertEq( + "Other Bookmarks", + bookmark.title, + "Folder returned from getTree has the expected title" + ); + + return browser.bookmarks.create({parentId: "invalid"}).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("Invalid bookmark"), + "Expected error thrown when trying to create a bookmark with an invalid parentId" + ); + browser.test.assertTrue( + error.message.includes(`"parentGuid":"invalid"`), + "Expected error thrown when trying to create a bookmark with an invalid parentId" + ); + }); + }).then(() => { + return browser.bookmarks.remove(ourId); + }).then(result => { + browser.test.assertEq(undefined, result, "Removing a bookmark returns undefined"); + + browser.test.assertEq(1, collectedEvents.length, "1 expected events received"); + checkOnRemoved(ourId, bookmarkGuids.unfiledGuid, 0, "http://example.com/"); + + return browser.bookmarks.get(ourId).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("Bookmark not found"), + "Expected error thrown when trying to get a removed bookmark" + ); + }); + }).then(() => { + return browser.bookmarks.remove(nonExistentId).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("No bookmarks found for the provided GUID"), + "Expected error thrown when trying removed a non-existent bookmark" + ); + }); + }).then(() => { + // test bookmarks.search + return Promise.all([ + browser.bookmarks.create({title: "MØzillä", url: "http://møzîllä.örg/"}), + browser.bookmarks.create({title: "Example", url: "http://example.org/"}), + browser.bookmarks.create({title: "Mozilla Folder"}), + browser.bookmarks.create({title: "EFF", url: "http://eff.org/"}), + browser.bookmarks.create({title: "Menu Item", url: "http://menu.org/", parentId: bookmarkGuids.menuGuid}), + browser.bookmarks.create({title: "Toolbar Item", url: "http://toolbar.org/", parentId: bookmarkGuids.toolbarGuid}), + ]); + }).then(results => { + browser.test.assertEq(6, collectedEvents.length, "6 expected events received"); + checkOnCreated(results[5].id, bookmarkGuids.toolbarGuid, 0, "Toolbar Item", "http://toolbar.org/", results[5].dateAdded); + checkOnCreated(results[4].id, bookmarkGuids.menuGuid, 0, "Menu Item", "http://menu.org/", results[4].dateAdded); + checkOnCreated(results[3].id, bookmarkGuids.unfiledGuid, 0, "EFF", "http://eff.org/", results[3].dateAdded); + checkOnCreated(results[2].id, bookmarkGuids.unfiledGuid, 0, "Mozilla Folder", undefined, results[2].dateAdded); + checkOnCreated(results[1].id, bookmarkGuids.unfiledGuid, 0, "Example", "http://example.org/", results[1].dateAdded); + checkOnCreated(results[0].id, bookmarkGuids.unfiledGuid, 0, "MØzillä", "http://møzîllä.örg/", results[0].dateAdded); + + for (let result of results) { + if (result.title !== "Mozilla Folder") { + createdBookmarks.add(result.id); + } + } + let folderResult = results[2]; + createdFolderId = folderResult.id; + return Promise.all([ + browser.bookmarks.create({title: "Mozilla", url: "http://allizom.org/", parentId: createdFolderId}), + browser.bookmarks.create({title: "Mozilla Corporation", url: "http://allizom.com/", parentId: createdFolderId}), + browser.bookmarks.create({title: "Firefox", url: "http://allizom.org/firefox/", parentId: createdFolderId}), + ]).then(newBookmarks => { + browser.test.assertEq(3, collectedEvents.length, "3 expected events received"); + checkOnCreated(newBookmarks[2].id, createdFolderId, 0, "Firefox", "http://allizom.org/firefox/", newBookmarks[2].dateAdded); + checkOnCreated(newBookmarks[1].id, createdFolderId, 0, "Mozilla Corporation", "http://allizom.com/", newBookmarks[1].dateAdded); + checkOnCreated(newBookmarks[0].id, createdFolderId, 0, "Mozilla", "http://allizom.org/", newBookmarks[0].dateAdded); + + return browser.bookmarks.create({ + title: "About Mozilla", + url: "http://allizom.org/about/", + parentId: createdFolderId, + index: 1, + }); + }).then(result => { + browser.test.assertEq(1, collectedEvents.length, "1 expected events received"); + checkOnCreated(result.id, createdFolderId, 1, "About Mozilla", "http://allizom.org/about/", result.dateAdded); + + // returns all items on empty object + return browser.bookmarks.search({}); + }).then(bookmarksSearchResults => { + browser.test.assertTrue(bookmarksSearchResults.length >= 9, "At least as many bookmarks as added were returned by search({})"); + + return Promise.resolve().then(() => { + return browser.bookmarks.remove(createdFolderId); + }).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("Cannot remove a non-empty folder"), + "Expected error thrown when trying to remove a non-empty folder" + ); + return browser.bookmarks.getSubTree(createdFolderId); + }); + }); + }).then(results => { + browser.test.assertEq(1, results.length, "Expected number of nodes returned by getSubTree"); + browser.test.assertEq("Mozilla Folder", results[0].title, "Folder has the expected title"); + let children = results[0].children; + browser.test.assertEq(4, children.length, "Expected number of bookmarks returned by getSubTree"); + browser.test.assertEq("Firefox", children[0].title, "Bookmark has the expected title"); + browser.test.assertEq("About Mozilla", children[1].title, "Bookmark has the expected title"); + browser.test.assertEq(1, children[1].index, "Bookmark has the expected index"); + browser.test.assertEq("Mozilla Corporation", children[2].title, "Bookmark has the expected title"); + browser.test.assertEq("Mozilla", children[3].title, "Bookmark has the expected title"); + + // throws an error for invalid query objects + Promise.resolve().then(() => { + return browser.bookmarks.search(); + }).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("Incorrect argument types for bookmarks.search"), + "Expected error thrown when trying to search with no arguments" + ); + }); + + Promise.resolve().then(() => { + return browser.bookmarks.search(null); + }).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("Incorrect argument types for bookmarks.search"), + "Expected error thrown when trying to search with null as an argument" + ); + }); + + Promise.resolve().then(() => { + return browser.bookmarks.search(function() {}); + }).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("Incorrect argument types for bookmarks.search"), + "Expected error thrown when trying to search with a function as an argument" + ); + }); + + Promise.resolve().then(() => { + return browser.bookmarks.search({banana: "banana"}); + }).then(expectedError, error => { + let substr = `an unexpected "banana" property`; + browser.test.assertTrue( + error.message.includes(substr), + `Expected error ${JSON.stringify(error.message)} to contain ${JSON.stringify(substr)}`); + }); + + Promise.resolve().then(() => { + return browser.bookmarks.search({url: "spider-man vs. batman"}); + }).then(expectedError, error => { + let substr = 'must match the format "url"'; + browser.test.assertTrue( + error.message.includes(substr), + `Expected error ${JSON.stringify(error.message)} to contain ${JSON.stringify(substr)}`); + }); + + // queries the full url + return browser.bookmarks.search("http://example.org/"); + }).then(results => { + browser.test.assertEq(1, results.length, "Expected number of results returned for url search"); + checkBookmark({title: "Example", url: "http://example.org/", index: 2}, results[0]); + + // queries a partial url + return browser.bookmarks.search("example.org"); + }).then(results => { + browser.test.assertEq(1, results.length, "Expected number of results returned for url search"); + checkBookmark({title: "Example", url: "http://example.org/", index: 2}, results[0]); + + // queries the title + return browser.bookmarks.search("EFF"); + }).then(results => { + browser.test.assertEq(1, results.length, "Expected number of results returned for title search"); + checkBookmark({title: "EFF", url: "http://eff.org/", index: 0, parentId: bookmarkGuids.unfiledGuid}, results[0]); + + // finds menu items + return browser.bookmarks.search("Menu Item"); + }).then(results => { + browser.test.assertEq(1, results.length, "Expected number of results returned for menu item search"); + checkBookmark({title: "Menu Item", url: "http://menu.org/", index: 0, parentId: bookmarkGuids.menuGuid}, results[0]); + + // finds toolbar items + return browser.bookmarks.search("Toolbar Item"); + }).then(results => { + browser.test.assertEq(1, results.length, "Expected number of results returned for toolbar item search"); + checkBookmark({title: "Toolbar Item", url: "http://toolbar.org/", index: 0, parentId: bookmarkGuids.toolbarGuid}, results[0]); + + // finds folders + return browser.bookmarks.search("Mozilla Folder"); + }).then(results => { + browser.test.assertEq(1, results.length, "Expected number of folders returned"); + browser.test.assertEq("Mozilla Folder", results[0].title, "Folder has the expected title"); + + // is case-insensitive + return browser.bookmarks.search("corporation"); + }).then(results => { + browser.test.assertEq(1, results.length, "Expected number of results returnedfor case-insensitive search"); + browser.test.assertEq("Mozilla Corporation", results[0].title, "Bookmark has the expected title"); + + // is case-insensitive for non-ascii + return browser.bookmarks.search("MøZILLÄ"); + }).then(results => { + browser.test.assertEq(1, results.length, "Expected number of results returned for non-ascii search"); + browser.test.assertEq("MØzillä", results[0].title, "Bookmark has the expected title"); + + // returns multiple results + return browser.bookmarks.search("allizom"); + }).then(results => { + browser.test.assertEq(4, results.length, "Expected number of multiple results returned"); + browser.test.assertEq("Mozilla", results[0].title, "Bookmark has the expected title"); + browser.test.assertEq("Mozilla Corporation", results[1].title, "Bookmark has the expected title"); + browser.test.assertEq("Firefox", results[2].title, "Bookmark has the expected title"); + browser.test.assertEq("About Mozilla", results[3].title, "Bookmark has the expected title"); + + // accepts a url field + return browser.bookmarks.search({url: "http://allizom.com/"}); + }).then(results => { + browser.test.assertEq(1, results.length, "Expected number of results returned for url field"); + checkBookmark({title: "Mozilla Corporation", url: "http://allizom.com/", index: 2}, results[0]); + + // normalizes urls + return browser.bookmarks.search({url: "http://allizom.com"}); + }).then(results => { + browser.test.assertEq(results.length, 1, "Expected number of results returned for normalized url field"); + checkBookmark({title: "Mozilla Corporation", url: "http://allizom.com/", index: 2}, results[0]); + + // normalizes urls even more + return browser.bookmarks.search({url: "http:allizom.com"}); + }).then(results => { + browser.test.assertEq(results.length, 1, "Expected number of results returned for normalized url field"); + checkBookmark({title: "Mozilla Corporation", url: "http://allizom.com/", index: 2}, results[0]); + + // accepts a title field + return browser.bookmarks.search({title: "Mozilla"}); + }).then(results => { + browser.test.assertEq(results.length, 1, "Expected number of results returned for title field"); + checkBookmark({title: "Mozilla", url: "http://allizom.org/", index: 3}, results[0]); + + // can combine title and query + return browser.bookmarks.search({title: "Mozilla", query: "allizom"}); + }).then(results => { + browser.test.assertEq(1, results.length, "Expected number of results returned for title and query fields"); + checkBookmark({title: "Mozilla", url: "http://allizom.org/", index: 3}, results[0]); + + // uses AND conditions + return browser.bookmarks.search({title: "EFF", query: "allizom"}); + }).then(results => { + browser.test.assertEq( + 0, + results.length, + "Expected number of results returned for non-matching title and query fields" + ); + + // returns an empty array on item not found + return browser.bookmarks.search("microsoft"); + }).then(results => { + browser.test.assertEq(0, results.length, "Expected number of results returned for non-matching search"); + + return Promise.resolve().then(() => { + return browser.bookmarks.getRecent(""); + }).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("Incorrect argument types for bookmarks.getRecent"), + "Expected error thrown when calling getRecent with an empty string" + ); + }); + }).then(() => { + return Promise.resolve().then(() => { + return browser.bookmarks.getRecent(1.234); + }).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("Incorrect argument types for bookmarks.getRecent"), + "Expected error thrown when calling getRecent with a decimal number" + ); + }); + }).then(() => { + return Promise.all([ + browser.bookmarks.search("corporation"), + browser.bookmarks.getChildren(bookmarkGuids.menuGuid), + ]); + }).then(results => { + let corporationBookmark = results[0][0]; + let childCount = results[1].length; + + browser.test.assertEq(2, corporationBookmark.index, "Bookmark has the expected index"); + + return browser.bookmarks.move(corporationBookmark.id, {index: 0}).then(result => { + browser.test.assertEq(0, result.index, "Bookmark has the expected index"); + + browser.test.assertEq(1, collectedEvents.length, "1 expected events received"); + checkOnMoved(corporationBookmark.id, createdFolderId, createdFolderId, 0, 2); + + return browser.bookmarks.move(corporationBookmark.id, {parentId: bookmarkGuids.menuGuid}); + }).then(result => { + browser.test.assertEq(bookmarkGuids.menuGuid, result.parentId, "Bookmark has the expected parent"); + browser.test.assertEq(childCount, result.index, "Bookmark has the expected index"); + + browser.test.assertEq(1, collectedEvents.length, "1 expected events received"); + checkOnMoved(corporationBookmark.id, bookmarkGuids.menuGuid, createdFolderId, 1, 0); + + return browser.bookmarks.move(corporationBookmark.id, {index: 0}); + }).then(result => { + browser.test.assertEq(bookmarkGuids.menuGuid, result.parentId, "Bookmark has the expected parent"); + browser.test.assertEq(0, result.index, "Bookmark has the expected index"); + + browser.test.assertEq(1, collectedEvents.length, "1 expected events received"); + checkOnMoved(corporationBookmark.id, bookmarkGuids.menuGuid, bookmarkGuids.menuGuid, 0, 1); + + return browser.bookmarks.move(corporationBookmark.id, {parentId: bookmarkGuids.toolbarGuid, index: 1}); + }).then(result => { + browser.test.assertEq(bookmarkGuids.toolbarGuid, result.parentId, "Bookmark has the expected parent"); + browser.test.assertEq(1, result.index, "Bookmark has the expected index"); + + browser.test.assertEq(1, collectedEvents.length, "1 expected events received"); + checkOnMoved(corporationBookmark.id, bookmarkGuids.toolbarGuid, bookmarkGuids.menuGuid, 1, 0); + + createdBookmarks.add(corporationBookmark.id); + }); + }).then(() => { + return browser.bookmarks.getRecent(4); + }).then(results => { + browser.test.assertEq(4, results.length, "Expected number of results returned by getRecent"); + let prevDate = results[0].dateAdded; + for (let bookmark of results) { + browser.test.assertTrue(bookmark.dateAdded <= prevDate, "The recent bookmarks are sorted by dateAdded"); + prevDate = bookmark.dateAdded; + } + let bookmarksByTitle = results.sort((a, b) => { + return a.title.localeCompare(b.title); + }); + browser.test.assertEq("About Mozilla", bookmarksByTitle[0].title, "Bookmark has the expected title"); + browser.test.assertEq("Firefox", bookmarksByTitle[1].title, "Bookmark has the expected title"); + browser.test.assertEq("Mozilla", bookmarksByTitle[2].title, "Bookmark has the expected title"); + browser.test.assertEq("Mozilla Corporation", bookmarksByTitle[3].title, "Bookmark has the expected title"); + + return browser.bookmarks.search({}); + }).then(results => { + let startBookmarkCount = results.length; + + return browser.bookmarks.search({title: "Mozilla Folder"}).then(result => { + return browser.bookmarks.removeTree(result[0].id); + }).then(() => { + browser.test.assertEq(1, collectedEvents.length, "1 expected events received"); + checkOnRemoved(createdFolderId, bookmarkGuids.unfiledGuid, 1); + + return browser.bookmarks.search({}).then(searchResults => { + browser.test.assertEq( + startBookmarkCount - 4, + searchResults.length, + "Expected number of results returned after removeTree"); + }); + }); + }).then(() => { + return browser.bookmarks.create({title: "Empty Folder"}); + }).then(result => { + let emptyFolderId = result.id; + + browser.test.assertEq(1, collectedEvents.length, "1 expected events received"); + checkOnCreated(emptyFolderId, bookmarkGuids.unfiledGuid, 3, "Empty Folder", undefined, result.dateAdded); + + browser.test.assertEq("Empty Folder", result.title, "Folder has the expected title"); + return browser.bookmarks.remove(emptyFolderId).then(() => { + browser.test.assertEq(1, collectedEvents.length, "1 expected events received"); + checkOnRemoved(emptyFolderId, bookmarkGuids.unfiledGuid, 3); + + return browser.bookmarks.get(emptyFolderId).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("Bookmark not found"), + "Expected error thrown when trying to get a removed folder" + ); + }); + }); + }).then(() => { + return browser.bookmarks.getChildren(nonExistentId).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("root is null"), + "Expected error thrown when trying to getChildren for a non-existent folder" + ); + }); + }).then(() => { + return Promise.resolve().then(() => { + return browser.bookmarks.move(nonExistentId, {}); + }).then(expectedError, error => { + browser.test.assertTrue( + error.message.includes("No bookmarks found for the provided GUID"), + "Expected error thrown when calling move with a non-existent bookmark" + ); + }); + }).then(() => { + // remove all created bookmarks + let promises = Array.from(createdBookmarks, guid => browser.bookmarks.remove(guid)); + return Promise.all(promises); + }).then(() => { + browser.test.assertEq(createdBookmarks.size, collectedEvents.length, "expected number of events received"); + + return browser.bookmarks.search({}); + }).then(results => { + browser.test.assertEq(initialBookmarkCount, results.length, "All created bookmarks have been removed"); + + return browser.test.notifyPass("bookmarks"); + }).catch(error => { + browser.test.fail(`Error: ${String(error)} :: ${error.stack}`); + browser.test.notifyFail("bookmarks"); + }); +} + +let extensionData = { + background: `(${backgroundScript})()`, + manifest: { + permissions: ["bookmarks"], + }, +}; + +add_task(function* test_contentscript() { + let extension = ExtensionTestUtils.loadExtension(extensionData); + yield extension.startup(); + yield extension.awaitFinish("bookmarks"); + yield extension.unload(); +}); diff --git a/browser/components/extensions/test/xpcshell/test_ext_history.js b/browser/components/extensions/test/xpcshell/test_ext_history.js new file mode 100644 index 000000000..78df33151 --- /dev/null +++ b/browser/components/extensions/test/xpcshell/test_ext_history.js @@ -0,0 +1,487 @@ +/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set sts=2 sw=2 et tw=80: */ +"use strict"; + +XPCOMUtils.defineLazyModuleGetter(this, "PlacesTestUtils", + "resource://testing-common/PlacesTestUtils.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils", + "resource://gre/modules/PlacesUtils.jsm"); +XPCOMUtils.defineLazyModuleGetter(this, "ExtensionUtils", + "resource://gre/modules/ExtensionUtils.jsm"); + +add_task(function* test_delete() { + function background() { + let historyClearedCount = 0; + let removedUrls = []; + + browser.history.onVisitRemoved.addListener(data => { + if (data.allHistory) { + historyClearedCount++; + browser.test.assertEq(0, data.urls.length, "onVisitRemoved received an empty urls array"); + } else { + browser.test.assertEq(1, data.urls.length, "onVisitRemoved received one URL"); + removedUrls.push(data.urls[0]); + } + }); + + browser.test.onMessage.addListener((msg, arg) => { + if (msg === "delete-url") { + browser.history.deleteUrl({url: arg}).then(result => { + browser.test.assertEq(undefined, result, "browser.history.deleteUrl returns nothing"); + browser.test.sendMessage("url-deleted"); + }); + } else if (msg === "delete-range") { + browser.history.deleteRange(arg).then(result => { + browser.test.assertEq(undefined, result, "browser.history.deleteRange returns nothing"); + browser.test.sendMessage("range-deleted", removedUrls); + }); + } else if (msg === "delete-all") { + browser.history.deleteAll().then(result => { + browser.test.assertEq(undefined, result, "browser.history.deleteAll returns nothing"); + browser.test.sendMessage("history-cleared", [historyClearedCount, removedUrls]); + }); + } + }); + + browser.test.sendMessage("ready"); + } + + const BASE_URL = "http://mozilla.com/test_history/"; + + let extension = ExtensionTestUtils.loadExtension({ + manifest: { + permissions: ["history"], + }, + background: `(${background})()`, + }); + + yield extension.startup(); + yield extension.awaitMessage("ready"); + yield PlacesTestUtils.clearHistory(); + + let historyClearedCount; + let visits = []; + let visitDate = new Date(1999, 9, 9, 9, 9).getTime(); + + function pushVisit(subvisits) { + visitDate += 1000; + subvisits.push({date: new Date(visitDate)}); + } + + // Add 5 visits for one uri and 3 visits for 3 others + for (let i = 0; i < 4; ++i) { + let visit = { + url: `${BASE_URL}${i}`, + title: "visit " + i, + visits: [], + }; + if (i === 0) { + for (let j = 0; j < 5; ++j) { + pushVisit(visit.visits); + } + } else { + pushVisit(visit.visits); + } + visits.push(visit); + } + + yield PlacesUtils.history.insertMany(visits); + equal((yield PlacesTestUtils.visitsInDB(visits[0].url)), 5, "5 visits for uri found in history database"); + + let testUrl = visits[2].url; + ok((yield PlacesTestUtils.isPageInDB(testUrl)), "expected url found in history database"); + + extension.sendMessage("delete-url", testUrl); + yield extension.awaitMessage("url-deleted"); + equal((yield PlacesTestUtils.isPageInDB(testUrl)), false, "expected url not found in history database"); + + // delete 3 of the 5 visits for url 1 + let filter = { + startTime: visits[0].visits[0].date, + endTime: visits[0].visits[2].date, + }; + + extension.sendMessage("delete-range", filter); + let removedUrls = yield extension.awaitMessage("range-deleted"); + ok(!removedUrls.includes(visits[0].url), `${visits[0].url} not received by onVisitRemoved`); + ok((yield PlacesTestUtils.isPageInDB(visits[0].url)), "expected uri found in history database"); + equal((yield PlacesTestUtils.visitsInDB(visits[0].url)), 2, "2 visits for uri found in history database"); + ok((yield PlacesTestUtils.isPageInDB(visits[1].url)), "expected uri found in history database"); + equal((yield PlacesTestUtils.visitsInDB(visits[1].url)), 1, "1 visit for uri found in history database"); + + // delete the rest of the visits for url 1, and the visit for url 2 + filter.startTime = visits[0].visits[0].date; + filter.endTime = visits[1].visits[0].date; + + extension.sendMessage("delete-range", filter); + yield extension.awaitMessage("range-deleted"); + + equal((yield PlacesTestUtils.isPageInDB(visits[0].url)), false, "expected uri not found in history database"); + equal((yield PlacesTestUtils.visitsInDB(visits[0].url)), 0, "0 visits for uri found in history database"); + equal((yield PlacesTestUtils.isPageInDB(visits[1].url)), false, "expected uri not found in history database"); + equal((yield PlacesTestUtils.visitsInDB(visits[1].url)), 0, "0 visits for uri found in history database"); + + ok((yield PlacesTestUtils.isPageInDB(visits[3].url)), "expected uri found in history database"); + + extension.sendMessage("delete-all"); + [historyClearedCount, removedUrls] = yield extension.awaitMessage("history-cleared"); + equal(PlacesUtils.history.hasHistoryEntries, false, "history is empty"); + equal(historyClearedCount, 2, "onVisitRemoved called for each clearing of history"); + equal(removedUrls.length, 3, "onVisitRemoved called the expected number of times"); + for (let i = 1; i < 3; ++i) { + let url = visits[i].url; + ok(removedUrls.includes(url), `${url} received by onVisitRemoved`); + } + yield extension.unload(); +}); + +add_task(function* test_search() { + const SINGLE_VISIT_URL = "http://example.com/"; + const DOUBLE_VISIT_URL = "http://example.com/2/"; + const MOZILLA_VISIT_URL = "http://mozilla.com/"; + const REFERENCE_DATE = new Date(); + // pages/visits to add via History.insert + const PAGE_INFOS = [ + { + url: SINGLE_VISIT_URL, + title: `test visit for ${SINGLE_VISIT_URL}`, + visits: [ + {date: new Date(Number(REFERENCE_DATE) - 1000)}, + ], + }, + { + url: DOUBLE_VISIT_URL, + title: `test visit for ${DOUBLE_VISIT_URL}`, + visits: [ + {date: REFERENCE_DATE}, + {date: new Date(Number(REFERENCE_DATE) - 2000)}, + ], + }, + { + url: MOZILLA_VISIT_URL, + title: `test visit for ${MOZILLA_VISIT_URL}`, + visits: [ + {date: new Date(Number(REFERENCE_DATE) - 3000)}, + ], + }, + ]; + + function background(BGSCRIPT_REFERENCE_DATE) { + const futureTime = Date.now() + 24 * 60 * 60 * 1000; + + browser.test.onMessage.addListener(msg => { + browser.history.search({text: ""}).then(results => { + browser.test.sendMessage("empty-search", results); + return browser.history.search({text: "mozilla.com"}); + }).then(results => { + browser.test.sendMessage("text-search", results); + return browser.history.search({text: "example.com", maxResults: 1}); + }).then(results => { + browser.test.sendMessage("max-results-search", results); + return browser.history.search({text: "", startTime: BGSCRIPT_REFERENCE_DATE - 2000, endTime: BGSCRIPT_REFERENCE_DATE - 1000}); + }).then(results => { + browser.test.sendMessage("date-range-search", results); + return browser.history.search({text: "", startTime: futureTime}); + }).then(results => { + browser.test.assertEq(0, results.length, "no results returned for late start time"); + return browser.history.search({text: "", endTime: 0}); + }).then(results => { + browser.test.assertEq(0, results.length, "no results returned for early end time"); + return browser.history.search({text: "", startTime: Date.now(), endTime: 0}); + }).then(results => { + browser.test.fail("history.search rejects with startTime that is after the endTime"); + }, error => { + browser.test.assertEq( + "The startTime cannot be after the endTime", + error.message, + "history.search rejects with startTime that is after the endTime"); + }).then(() => { + browser.test.notifyPass("search"); + }); + }); + + browser.test.sendMessage("ready"); + } + + let extension = ExtensionTestUtils.loadExtension({ + manifest: { + permissions: ["history"], + }, + background: `(${background})(${Number(REFERENCE_DATE)})`, + }); + + function findResult(url, results) { + return results.find(r => r.url === url); + } + + function checkResult(results, url, expectedCount) { + let result = findResult(url, results); + notEqual(result, null, `history.search result was found for ${url}`); + equal(result.visitCount, expectedCount, `history.search reports ${expectedCount} visit(s)`); + equal(result.title, `test visit for ${url}`, "title for search result is correct"); + } + + yield extension.startup(); + yield extension.awaitMessage("ready"); + yield PlacesTestUtils.clearHistory(); + + yield PlacesUtils.history.insertMany(PAGE_INFOS); + + extension.sendMessage("check-history"); + + let results = yield extension.awaitMessage("empty-search"); + equal(results.length, 3, "history.search with empty text returned 3 results"); + checkResult(results, SINGLE_VISIT_URL, 1); + checkResult(results, DOUBLE_VISIT_URL, 2); + checkResult(results, MOZILLA_VISIT_URL, 1); + + results = yield extension.awaitMessage("text-search"); + equal(results.length, 1, "history.search with specific text returned 1 result"); + checkResult(results, MOZILLA_VISIT_URL, 1); + + results = yield extension.awaitMessage("max-results-search"); + equal(results.length, 1, "history.search with maxResults returned 1 result"); + checkResult(results, DOUBLE_VISIT_URL, 2); + + results = yield extension.awaitMessage("date-range-search"); + equal(results.length, 2, "history.search with a date range returned 2 result"); + checkResult(results, DOUBLE_VISIT_URL, 2); + checkResult(results, SINGLE_VISIT_URL, 1); + + yield extension.awaitFinish("search"); + yield extension.unload(); + yield PlacesTestUtils.clearHistory(); +}); + +add_task(function* test_add_url() { + function background() { + const TEST_DOMAIN = "http://example.com/"; + + browser.test.onMessage.addListener((msg, testData) => { + let [details, type] = testData; + details.url = details.url || `${TEST_DOMAIN}${type}`; + if (msg === "add-url") { + details.title = `Title for ${type}`; + browser.history.addUrl(details).then(() => { + return browser.history.search({text: details.url}); + }).then(results => { + browser.test.assertEq(1, results.length, "1 result found when searching for added URL"); + browser.test.sendMessage("url-added", {details, result: results[0]}); + }); + } else if (msg === "expect-failure") { + let expectedMsg = testData[2]; + browser.history.addUrl(details).then(() => { + browser.test.fail(`Expected error thrown for ${type}`); + }, error => { + browser.test.assertTrue( + error.message.includes(expectedMsg), + `"Expected error thrown when trying to add a URL with ${type}` + ); + browser.test.sendMessage("add-failed"); + }); + } + }); + + browser.test.sendMessage("ready"); + } + + let addTestData = [ + [{}, "default"], + [{visitTime: new Date()}, "with_date"], + [{visitTime: Date.now()}, "with_ms_number"], + [{visitTime: new Date().toISOString()}, "with_iso_string"], + [{transition: "typed"}, "valid_transition"], + ]; + + let failTestData = [ + [{transition: "generated"}, "an invalid transition", "|generated| is not a supported transition for history"], + [{visitTime: Date.now() + 1000000}, "a future date", "cannot be a future date"], + [{url: "about.config"}, "an invalid url", "about.config is not a valid URL"], + ]; + + function* checkUrl(results) { + ok((yield PlacesTestUtils.isPageInDB(results.details.url)), `${results.details.url} found in history database`); + ok(PlacesUtils.isValidGuid(results.result.id), "URL was added with a valid id"); + equal(results.result.title, results.details.title, "URL was added with the correct title"); + if (results.details.visitTime) { + equal(results.result.lastVisitTime, + Number(ExtensionUtils.normalizeTime(results.details.visitTime)), + "URL was added with the correct date"); + } + } + + let extension = ExtensionTestUtils.loadExtension({ + manifest: { + permissions: ["history"], + }, + background: `(${background})()`, + }); + + yield PlacesTestUtils.clearHistory(); + yield extension.startup(); + yield extension.awaitMessage("ready"); + + for (let data of addTestData) { + extension.sendMessage("add-url", data); + let results = yield extension.awaitMessage("url-added"); + yield checkUrl(results); + } + + for (let data of failTestData) { + extension.sendMessage("expect-failure", data); + yield extension.awaitMessage("add-failed"); + } + + yield extension.unload(); +}); + +add_task(function* test_get_visits() { + function background() { + const TEST_DOMAIN = "http://example.com/"; + const FIRST_DATE = Date.now(); + const INITIAL_DETAILS = { + url: TEST_DOMAIN, + visitTime: FIRST_DATE, + transition: "link", + }; + + let visitIds = new Set(); + + function checkVisit(visit, expected) { + visitIds.add(visit.visitId); + browser.test.assertEq(expected.visitTime, visit.visitTime, "visit has the correct visitTime"); + browser.test.assertEq(expected.transition, visit.transition, "visit has the correct transition"); + browser.history.search({text: expected.url}).then(results => { + // all results will have the same id, so we only need to use the first one + browser.test.assertEq(results[0].id, visit.id, "visit has the correct id"); + }); + } + + let details = Object.assign({}, INITIAL_DETAILS); + + browser.history.addUrl(details).then(() => { + return browser.history.getVisits({url: details.url}); + }).then(results => { + browser.test.assertEq(1, results.length, "the expected number of visits were returned"); + checkVisit(results[0], details); + details.url = `${TEST_DOMAIN}/1/`; + return browser.history.addUrl(details); + }).then(() => { + return browser.history.getVisits({url: details.url}); + }).then(results => { + browser.test.assertEq(1, results.length, "the expected number of visits were returned"); + checkVisit(results[0], details); + details.visitTime = FIRST_DATE - 1000; + details.transition = "typed"; + return browser.history.addUrl(details); + }).then(() => { + return browser.history.getVisits({url: details.url}); + }).then(results => { + browser.test.assertEq(2, results.length, "the expected number of visits were returned"); + checkVisit(results[0], INITIAL_DETAILS); + checkVisit(results[1], details); + }).then(() => { + browser.test.assertEq(3, visitIds.size, "each visit has a unique visitId"); + browser.test.notifyPass("get-visits"); + }); + } + + let extension = ExtensionTestUtils.loadExtension({ + manifest: { + permissions: ["history"], + }, + background: `(${background})()`, + }); + + yield PlacesTestUtils.clearHistory(); + yield extension.startup(); + + yield extension.awaitFinish("get-visits"); + yield extension.unload(); +}); + +add_task(function* test_on_visited() { + const SINGLE_VISIT_URL = "http://example.com/1/"; + const DOUBLE_VISIT_URL = "http://example.com/2/"; + let visitDate = new Date(1999, 9, 9, 9, 9).getTime(); + + // pages/visits to add via History.insertMany + const PAGE_INFOS = [ + { + url: SINGLE_VISIT_URL, + title: `visit to ${SINGLE_VISIT_URL}`, + visits: [ + {date: new Date(visitDate)}, + ], + }, + { + url: DOUBLE_VISIT_URL, + title: `visit to ${DOUBLE_VISIT_URL}`, + visits: [ + {date: new Date(visitDate += 1000)}, + {date: new Date(visitDate += 1000)}, + ], + }, + ]; + + function background() { + let onVisitedData = []; + + browser.history.onVisited.addListener(data => { + if (data.url.includes("moz-extension")) { + return; + } + onVisitedData.push(data); + if (onVisitedData.length == 3) { + browser.test.sendMessage("on-visited-data", onVisitedData); + } + }); + + browser.test.sendMessage("ready"); + } + + let extension = ExtensionTestUtils.loadExtension({ + manifest: { + permissions: ["history"], + }, + background: `(${background})()`, + }); + + yield PlacesTestUtils.clearHistory(); + yield extension.startup(); + yield extension.awaitMessage("ready"); + + yield PlacesUtils.history.insertMany(PAGE_INFOS); + + let onVisitedData = yield extension.awaitMessage("on-visited-data"); + + function checkOnVisitedData(index, expected) { + let onVisited = onVisitedData[index]; + ok(PlacesUtils.isValidGuid(onVisited.id), "onVisited received a valid id"); + equal(onVisited.url, expected.url, "onVisited received the expected url"); + // Title will be blank until bug 1287928 lands + // https://bugzilla.mozilla.org/show_bug.cgi?id=1287928 + equal(onVisited.title, "", "onVisited received a blank title"); + equal(onVisited.lastVisitTime, expected.time, "onVisited received the expected time"); + equal(onVisited.visitCount, expected.visitCount, "onVisited received the expected visitCount"); + } + + let expected = { + url: PAGE_INFOS[0].url, + title: PAGE_INFOS[0].title, + time: PAGE_INFOS[0].visits[0].date.getTime(), + visitCount: 1, + }; + checkOnVisitedData(0, expected); + + expected.url = PAGE_INFOS[1].url; + expected.title = PAGE_INFOS[1].title; + expected.time = PAGE_INFOS[1].visits[0].date.getTime(); + checkOnVisitedData(1, expected); + + expected.time = PAGE_INFOS[1].visits[1].date.getTime(); + expected.visitCount = 2; + checkOnVisitedData(2, expected); + + yield extension.unload(); +}); diff --git a/browser/components/extensions/test/xpcshell/test_ext_manifest_commands.js b/browser/components/extensions/test/xpcshell/test_ext_manifest_commands.js new file mode 100644 index 000000000..4de7afe01 --- /dev/null +++ b/browser/components/extensions/test/xpcshell/test_ext_manifest_commands.js @@ -0,0 +1,24 @@ +/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set sts=2 sw=2 et tw=80: */ +"use strict"; + + +add_task(function* test_manifest_commands() { + let normalized = yield ExtensionTestUtils.normalizeManifest({ + "commands": { + "toggle-feature": { + "suggested_key": {"default": "Shifty+Y"}, + "description": "Send a 'toggle-feature' event to the extension", + }, + }, + }); + + let expectedError = ( + String.raw`commands.toggle-feature.suggested_key.default: Value must either: ` + + String.raw`match the pattern /^\s*(Alt|Ctrl|Command|MacCtrl)\s*\+\s*(Shift\s*\+\s*)?([A-Z0-9]|Comma|Period|Home|End|PageUp|PageDown|Space|Insert|Delete|Up|Down|Left|Right)\s*$/, or ` + + String.raw`match the pattern /^(MediaNextTrack|MediaPlayPause|MediaPrevTrack|MediaStop)$/` + ); + + ok(normalized.error.includes(expectedError), + `The manifest error ${JSON.stringify(normalized.error)} must contain ${JSON.stringify(expectedError)}`); +}); diff --git a/browser/components/extensions/test/xpcshell/test_ext_manifest_omnibox.js b/browser/components/extensions/test/xpcshell/test_ext_manifest_omnibox.js new file mode 100644 index 000000000..2cb141235 --- /dev/null +++ b/browser/components/extensions/test/xpcshell/test_ext_manifest_omnibox.js @@ -0,0 +1,61 @@ +/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set sts=2 sw=2 et tw=80: */ +"use strict"; + +function* testKeyword(params) { + let normalized = yield ExtensionTestUtils.normalizeManifest({ + "omnibox": { + "keyword": params.keyword, + }, + }); + + if (params.expectError) { + let expectedError = ( + String.raw`omnibox.keyword: String "${params.keyword}" ` + + String.raw`must match /^[^?\s:]([^\s:]*[^/\s:])?$/` + ); + ok(normalized.error.includes(expectedError), + `The manifest error ${JSON.stringify(normalized.error)} ` + + `must contain ${JSON.stringify(expectedError)}`); + } else { + equal(normalized.error, undefined, "Should not have an error"); + equal(normalized.errors.length, 0, "Should not have warnings"); + } +} + +add_task(function* test_manifest_commands() { + // accepted single character keywords + yield testKeyword({keyword: "a", expectError: false}); + yield testKeyword({keyword: "-", expectError: false}); + yield testKeyword({keyword: "嗨", expectError: false}); + yield testKeyword({keyword: "*", expectError: false}); + yield testKeyword({keyword: "/", expectError: false}); + + // rejected single character keywords + yield testKeyword({keyword: "?", expectError: true}); + yield testKeyword({keyword: " ", expectError: true}); + yield testKeyword({keyword: ":", expectError: true}); + + // accepted multi-character keywords + yield testKeyword({keyword: "aa", expectError: false}); + yield testKeyword({keyword: "http", expectError: false}); + yield testKeyword({keyword: "f?a", expectError: false}); + yield testKeyword({keyword: "fa?", expectError: false}); + yield testKeyword({keyword: "f/x", expectError: false}); + yield testKeyword({keyword: "/fx", expectError: false}); + + // rejected multi-character keywords + yield testKeyword({keyword: " a", expectError: true}); + yield testKeyword({keyword: "a ", expectError: true}); + yield testKeyword({keyword: " ", expectError: true}); + yield testKeyword({keyword: " a ", expectError: true}); + yield testKeyword({keyword: "?fx", expectError: true}); + yield testKeyword({keyword: "fx/", expectError: true}); + yield testKeyword({keyword: "f:x", expectError: true}); + yield testKeyword({keyword: "fx:", expectError: true}); + yield testKeyword({keyword: "f x", expectError: true}); + + // miscellaneous tests + yield testKeyword({keyword: "こんにちは", expectError: false}); + yield testKeyword({keyword: "http://", expectError: true}); +}); diff --git a/browser/components/extensions/test/xpcshell/test_ext_manifest_permissions.js b/browser/components/extensions/test/xpcshell/test_ext_manifest_permissions.js new file mode 100644 index 000000000..2c436535d --- /dev/null +++ b/browser/components/extensions/test/xpcshell/test_ext_manifest_permissions.js @@ -0,0 +1,57 @@ +/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set sts=2 sw=2 et tw=80: */ +"use strict"; + +/* globals chrome */ + +function* testPermission(options) { + function background(bgOptions) { + browser.test.sendMessage("typeof-namespace", { + browser: typeof browser[bgOptions.namespace], + chrome: typeof chrome[bgOptions.namespace], + }); + } + + let extensionDetails = { + background: `(${background})(${JSON.stringify(options)})`, + }; + + let extension = ExtensionTestUtils.loadExtension(extensionDetails); + + yield extension.startup(); + + let types = yield extension.awaitMessage("typeof-namespace"); + equal(types.browser, "undefined", `Type of browser.${options.namespace} without manifest entry`); + equal(types.chrome, "undefined", `Type of chrome.${options.namespace} without manifest entry`); + + yield extension.unload(); + + extensionDetails.manifest = options.manifest; + extension = ExtensionTestUtils.loadExtension(extensionDetails); + + yield extension.startup(); + + types = yield extension.awaitMessage("typeof-namespace"); + equal(types.browser, "object", `Type of browser.${options.namespace} with manifest entry`); + equal(types.chrome, "object", `Type of chrome.${options.namespace} with manifest entry`); + + yield extension.unload(); +} + +add_task(function* test_browserAction() { + yield testPermission({ + namespace: "browserAction", + manifest: { + browser_action: {}, + }, + }); +}); + +add_task(function* test_pageAction() { + yield testPermission({ + namespace: "pageAction", + manifest: { + page_action: {}, + }, + }); +}); diff --git a/browser/components/extensions/test/xpcshell/xpcshell.ini b/browser/components/extensions/test/xpcshell/xpcshell.ini new file mode 100644 index 000000000..b9148a697 --- /dev/null +++ b/browser/components/extensions/test/xpcshell/xpcshell.ini @@ -0,0 +1,11 @@ +[DEFAULT] +head = head.js +tail = +firefox-appdir = browser +tags = webextensions + +[test_ext_bookmarks.js] +[test_ext_history.js] +[test_ext_manifest_commands.js] +[test_ext_manifest_omnibox.js] +[test_ext_manifest_permissions.js] |