michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: * http://creativecommons.org/publicdomain/zero/1.0/ michael@0: */ michael@0: michael@0: const LIST_UPDATED_TOPIC = "plugins-list-updated"; michael@0: michael@0: // We need to use the same algorithm for generating IDs for plugins michael@0: var { getIDHashForString } = Components.utils.import("resource://gre/modules/addons/PluginProvider.jsm"); michael@0: michael@0: function PluginTag(name, description) { michael@0: this.name = name; michael@0: this.description = description; michael@0: } michael@0: michael@0: PluginTag.prototype = { michael@0: name: null, michael@0: description: null, michael@0: version: "1.0", michael@0: filename: null, michael@0: fullpath: null, michael@0: disabled: false, michael@0: blocklisted: false, michael@0: clicktoplay: false, michael@0: michael@0: mimeTypes: [], michael@0: michael@0: getMimeTypes: function(count) { michael@0: count.value = this.mimeTypes.length; michael@0: return this.mimeTypes; michael@0: } michael@0: }; michael@0: michael@0: PLUGINS = [ michael@0: // A standalone plugin michael@0: new PluginTag("Java", "A mock Java plugin"), michael@0: michael@0: // A plugin made up of two plugin files michael@0: new PluginTag("Flash", "A mock Flash plugin"), michael@0: new PluginTag("Flash", "A mock Flash plugin") michael@0: ]; michael@0: michael@0: gPluginHost = { michael@0: // nsIPluginHost michael@0: getPluginTags: function(count) { michael@0: count.value = PLUGINS.length; michael@0: return PLUGINS; michael@0: }, michael@0: michael@0: QueryInterface: XPCOMUtils.generateQI([AM_Ci.nsIPluginHost]) michael@0: }; michael@0: michael@0: var PluginHostFactory = { michael@0: createInstance: function (outer, iid) { michael@0: if (outer != null) michael@0: throw Components.results.NS_ERROR_NO_AGGREGATION; michael@0: return gPluginHost.QueryInterface(iid); michael@0: } michael@0: }; michael@0: michael@0: var registrar = Components.manager.QueryInterface(AM_Ci.nsIComponentRegistrar); michael@0: registrar.registerFactory(Components.ID("{aa6f9fef-cbe2-4d55-a2fa-dcf5482068b9}"), "PluginHost", michael@0: "@mozilla.org/plugin/host;1", PluginHostFactory); michael@0: michael@0: // This verifies that when the list of plugins changes the add-ons manager michael@0: // correctly updates michael@0: function run_test() { michael@0: do_test_pending(); michael@0: createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2"); michael@0: michael@0: startupManager(); michael@0: AddonManager.addAddonListener(AddonListener); michael@0: AddonManager.addInstallListener(InstallListener); michael@0: michael@0: run_test_1(); michael@0: } michael@0: michael@0: function end_test() { michael@0: do_execute_soon(do_test_finished); michael@0: } michael@0: michael@0: function sortAddons(addons) { michael@0: addons.sort(function(a, b) { michael@0: return a.name.localeCompare(b.name); michael@0: }); michael@0: } michael@0: michael@0: // Basic check that the mock object works michael@0: function run_test_1() { michael@0: AddonManager.getAddonsByTypes(["plugin"], function(addons) { michael@0: sortAddons(addons); michael@0: michael@0: do_check_eq(addons.length, 2); michael@0: michael@0: do_check_eq(addons[0].name, "Flash"); michael@0: do_check_false(addons[0].userDisabled); michael@0: do_check_eq(addons[1].name, "Java"); michael@0: do_check_false(addons[1].userDisabled); michael@0: michael@0: run_test_2(); michael@0: }); michael@0: } michael@0: michael@0: // No change to the list should not trigger any events or changes in the API michael@0: function run_test_2() { michael@0: // Reorder the list a bit michael@0: let tag = PLUGINS[0]; michael@0: PLUGINS[0] = PLUGINS[2]; michael@0: PLUGINS[2] = PLUGINS[1]; michael@0: PLUGINS[1] = tag; michael@0: michael@0: Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); michael@0: michael@0: AddonManager.getAddonsByTypes(["plugin"], function(addons) { michael@0: sortAddons(addons); michael@0: michael@0: do_check_eq(addons.length, 2); michael@0: michael@0: do_check_eq(addons[0].name, "Flash"); michael@0: do_check_false(addons[0].userDisabled); michael@0: do_check_eq(addons[1].name, "Java"); michael@0: do_check_false(addons[1].userDisabled); michael@0: michael@0: run_test_3(); michael@0: }); michael@0: } michael@0: michael@0: // Tests that a newly detected plugin shows up in the API and sends out events michael@0: function run_test_3() { michael@0: let tag = new PluginTag("Quicktime", "A mock Quicktime plugin"); michael@0: PLUGINS.push(tag); michael@0: let id = getIDHashForString(tag.name + tag.description); michael@0: michael@0: let test_params = {}; michael@0: test_params[id] = [ michael@0: ["onInstalling", false], michael@0: "onInstalled" michael@0: ]; michael@0: michael@0: prepare_test(test_params, [ michael@0: "onExternalInstall" michael@0: ]); michael@0: michael@0: Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); michael@0: michael@0: ensure_test_completed(); michael@0: michael@0: AddonManager.getAddonsByTypes(["plugin"], function(addons) { michael@0: sortAddons(addons); michael@0: michael@0: do_check_eq(addons.length, 3); michael@0: michael@0: do_check_eq(addons[0].name, "Flash"); michael@0: do_check_false(addons[0].userDisabled); michael@0: do_check_eq(addons[1].name, "Java"); michael@0: do_check_false(addons[1].userDisabled); michael@0: do_check_eq(addons[2].name, "Quicktime"); michael@0: do_check_false(addons[2].userDisabled); michael@0: michael@0: run_test_4(); michael@0: }); michael@0: } michael@0: michael@0: // Tests that a removed plugin disappears from in the API and sends out events michael@0: function run_test_4() { michael@0: let tag = PLUGINS.splice(1, 1)[0]; michael@0: let id = getIDHashForString(tag.name + tag.description); michael@0: michael@0: let test_params = {}; michael@0: test_params[id] = [ michael@0: ["onUninstalling", false], michael@0: "onUninstalled" michael@0: ]; michael@0: michael@0: prepare_test(test_params); michael@0: michael@0: Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); michael@0: michael@0: ensure_test_completed(); michael@0: michael@0: AddonManager.getAddonsByTypes(["plugin"], function(addons) { michael@0: sortAddons(addons); michael@0: michael@0: do_check_eq(addons.length, 2); michael@0: michael@0: do_check_eq(addons[0].name, "Flash"); michael@0: do_check_false(addons[0].userDisabled); michael@0: do_check_eq(addons[1].name, "Quicktime"); michael@0: do_check_false(addons[1].userDisabled); michael@0: michael@0: run_test_5(); michael@0: }); michael@0: } michael@0: michael@0: // Removing part of the flash plugin should have no effect michael@0: function run_test_5() { michael@0: PLUGINS.splice(0, 1); michael@0: michael@0: Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); michael@0: michael@0: ensure_test_completed(); michael@0: michael@0: AddonManager.getAddonsByTypes(["plugin"], function(addons) { michael@0: sortAddons(addons); michael@0: michael@0: do_check_eq(addons.length, 2); michael@0: michael@0: do_check_eq(addons[0].name, "Flash"); michael@0: do_check_false(addons[0].userDisabled); michael@0: do_check_eq(addons[1].name, "Quicktime"); michael@0: do_check_false(addons[1].userDisabled); michael@0: michael@0: run_test_6(); michael@0: }); michael@0: } michael@0: michael@0: // Replacing flash should be detected michael@0: function run_test_6() { michael@0: let oldTag = PLUGINS.splice(0, 1)[0]; michael@0: let newTag = new PluginTag("Flash 2", "A new crash-free Flash!"); michael@0: newTag.disabled = true; michael@0: PLUGINS.push(newTag); michael@0: michael@0: let test_params = {}; michael@0: test_params[getIDHashForString(oldTag.name + oldTag.description)] = [ michael@0: ["onUninstalling", false], michael@0: "onUninstalled" michael@0: ]; michael@0: test_params[getIDHashForString(newTag.name + newTag.description)] = [ michael@0: ["onInstalling", false], michael@0: "onInstalled" michael@0: ]; michael@0: michael@0: prepare_test(test_params, [ michael@0: "onExternalInstall" michael@0: ]); michael@0: michael@0: Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); michael@0: michael@0: ensure_test_completed(); michael@0: michael@0: AddonManager.getAddonsByTypes(["plugin"], function(addons) { michael@0: sortAddons(addons); michael@0: michael@0: do_check_eq(addons.length, 2); michael@0: michael@0: do_check_eq(addons[0].name, "Flash 2"); michael@0: do_check_true(addons[0].userDisabled); michael@0: do_check_eq(addons[1].name, "Quicktime"); michael@0: do_check_false(addons[1].userDisabled); michael@0: michael@0: run_test_7(); michael@0: }); michael@0: } michael@0: michael@0: // If new tags are detected and the disabled state changes then we should send michael@0: // out appropriate notifications michael@0: function run_test_7() { michael@0: PLUGINS[0] = new PluginTag("Quicktime", "A mock Quicktime plugin"); michael@0: PLUGINS[0].disabled = true; michael@0: PLUGINS[1] = new PluginTag("Flash 2", "A new crash-free Flash!"); michael@0: michael@0: let test_params = {}; michael@0: test_params[getIDHashForString(PLUGINS[0].name + PLUGINS[0].description)] = [ michael@0: ["onDisabling", false], michael@0: "onDisabled" michael@0: ]; michael@0: test_params[getIDHashForString(PLUGINS[1].name + PLUGINS[1].description)] = [ michael@0: ["onEnabling", false], michael@0: "onEnabled" michael@0: ]; michael@0: michael@0: prepare_test(test_params); michael@0: michael@0: Services.obs.notifyObservers(null, LIST_UPDATED_TOPIC, null); michael@0: michael@0: ensure_test_completed(); michael@0: michael@0: AddonManager.getAddonsByTypes(["plugin"], function(addons) { michael@0: sortAddons(addons); michael@0: michael@0: do_check_eq(addons.length, 2); michael@0: michael@0: do_check_eq(addons[0].name, "Flash 2"); michael@0: do_check_false(addons[0].userDisabled); michael@0: do_check_eq(addons[1].name, "Quicktime"); michael@0: do_check_true(addons[1].userDisabled); michael@0: michael@0: end_test(); michael@0: }); michael@0: }