From 44cfffc7931a5c394aa45724cc9ea752ccf9c6c9 Mon Sep 17 00:00:00 2001 From: Matthew Wein Date: Thu, 9 Mar 2017 16:56:46 -0500 Subject: [PATCH] Bug 1338525 - Add test coverage for theme validation r=mikedeboer MozReview-Commit-ID: FLYMpceZYhA --HG-- extra : rebase_source : 4c11ce0ee80a53326709ddeb10527ce83d6b9846 --- .../test/browser/browser-common.ini | 1 + .../browser/browser_ext_themes_validation.js | 56 +++++++++++++++ .../test_ext_themes_supported_properties.js | 71 +++++++++++++++++++ .../extensions/test/xpcshell/xpcshell.ini | 1 + 4 files changed, 129 insertions(+) create mode 100644 browser/components/extensions/test/browser/browser_ext_themes_validation.js create mode 100644 toolkit/components/extensions/test/xpcshell/test_ext_themes_supported_properties.js diff --git a/browser/components/extensions/test/browser/browser-common.ini b/browser/components/extensions/test/browser/browser-common.ini index 0f06aaf881fa..a7c17e4bd0ce 100644 --- a/browser/components/extensions/test/browser/browser-common.ini +++ b/browser/components/extensions/test/browser/browser-common.ini @@ -121,6 +121,7 @@ support-files = [browser_ext_tabs_zoom.js] [browser_ext_tabs_update_url.js] [browser_ext_themes_icons.js] +[browser_ext_themes_validation.js] [browser_ext_topwindowid.js] [browser_ext_url_overrides_newtab.js] [browser_ext_url_overrides_home.js] diff --git a/browser/components/extensions/test/browser/browser_ext_themes_validation.js b/browser/components/extensions/test/browser/browser_ext_themes_validation.js new file mode 100644 index 000000000000..1c62aa8dcde9 --- /dev/null +++ b/browser/components/extensions/test/browser/browser_ext_themes_validation.js @@ -0,0 +1,56 @@ +"use strict"; + +add_task(function* setup() { + yield SpecialPowers.pushPrefEnv({ + set: [["extensions.webextensions.themes.enabled", true]], + }); +}); + +/** + * Helper function for testing a theme with invalid properties. + * @param {object} invalidProps The invalid properties to load the theme with. + */ +function* testThemeWithInvalidProperties(invalidProps) { + let manifest = { + "theme": {}, + }; + + invalidProps.forEach(prop => { + // Some properties require additional information: + switch (prop) { + case "background": + manifest[prop] = {"scripts": ["background.js"]}; + break; + case "permissions": + manifest[prop] = ["tabs"]; + break; + case "omnibox": + manifest[prop] = {"keyword": "test"}; + break; + default: + manifest[prop] = {}; + } + }); + + let extension = ExtensionTestUtils.loadExtension({manifest}); + + SimpleTest.waitForExplicitFinish(); + let waitForConsole = new Promise(resolve => { + SimpleTest.monitorConsole(resolve, [{ + message: /Reading manifest: Themes defined in the manifest may only contain static resources/, + }]); + }); + + yield Assert.rejects(extension.startup(), null, "Theme should fail to load if it contains invalid properties"); + + SimpleTest.endMonitorConsole(); + yield waitForConsole; +} + +add_task(function* test_that_theme_with_invalid_properties_fails_to_load() { + let invalidProps = ["page_action", "browser_action", "background", "permissions", "omnibox", "commands"]; + for (let prop in invalidProps) { + yield testThemeWithInvalidProperties([prop]); + } + yield testThemeWithInvalidProperties(invalidProps); +}); diff --git a/toolkit/components/extensions/test/xpcshell/test_ext_themes_supported_properties.js b/toolkit/components/extensions/test/xpcshell/test_ext_themes_supported_properties.js new file mode 100644 index 000000000000..93a7319b7f0a --- /dev/null +++ b/toolkit/components/extensions/test/xpcshell/test_ext_themes_supported_properties.js @@ -0,0 +1,71 @@ +/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim: set sts=2 sw=2 et tw=80: */ +"use strict"; + +Cu.import("resource://gre/modules/ExtensionUtils.jsm"); +Cu.import("resource://gre/modules/Schemas.jsm"); + +const { + validateThemeManifest, +} = ExtensionUtils; + +const BASE_SCHEMA_URL = "chrome://extensions/content/schemas/manifest.json"; +const CATEGORY_EXTENSION_SCHEMAS = "webextension-schemas"; + +const baseManifestProperties = [ + "manifest_version", + "minimum_chrome_version", + "applications", + "browser_specific_settings", + "name", + "short_name", + "description", + "author", + "version", + "homepage_url", + "icons", + "incognito", + "background", + "options_ui", + "content_scripts", + "permissions", + "web_accessible_resources", + "developer", +]; + +function* getAdditionalInvalidManifestProperties() { + let invalidProperties = []; + yield Schemas.load(BASE_SCHEMA_URL); + for (let [name, url] of XPCOMUtils.enumerateCategoryEntries(CATEGORY_EXTENSION_SCHEMAS)) { + if (name !== "theme") { + yield Schemas.load(url); + let types = Schemas.schemaJSON.get(url)[0].types; + types.forEach(type => { + if (type.$extend == "WebExtensionManifest") { + let properties = Object.getOwnPropertyNames(type.properties); + invalidProperties.push(...properties); + } + }); + } + } + + // Also test an unrecognized property. + invalidProperties.push("unrecognized_property"); + + return invalidProperties; +} + +function checkProperties(actual, expected) { + Assert.equal(actual.length, expected.length, `Should have found ${expected.length} invalid properties`); + for (let i = 0; i < expected.length; i++) { + Assert.ok(actual.includes(expected[i]), `The invalid properties should contain "${expected[i]}"`); + } +} + +add_task(function* test_theme_supported_properties() { + let additionalInvalidProperties = yield getAdditionalInvalidManifestProperties(); + let actual = validateThemeManifest([...baseManifestProperties, ...additionalInvalidProperties]); + let expected = ["background", "permissions", "content_scripts", ...additionalInvalidProperties]; + checkProperties(actual, expected); +}); + diff --git a/toolkit/components/extensions/test/xpcshell/xpcshell.ini b/toolkit/components/extensions/test/xpcshell/xpcshell.ini index b9cabc3a9922..4e70b1ef616c 100644 --- a/toolkit/components/extensions/test/xpcshell/xpcshell.ini +++ b/toolkit/components/extensions/test/xpcshell/xpcshell.ini @@ -77,6 +77,7 @@ head = head.js head_sync.js skip-if = os == "android" [test_ext_storage_sync_crypto.js] skip-if = os == "android" +[test_ext_themes_supported_properties.js] [test_ext_topSites.js] skip-if = os == "android" [test_getAPILevelForWindow.js]