From 34243a67eabd24d837d3b49f1a68a4015c3d5710 Mon Sep 17 00:00:00 2001 From: Dave Townsend Date: Wed, 28 Apr 2010 11:47:22 -0700 Subject: [PATCH] Bug 553113: Perform automatic updates for the default lightweight theme. r=robstrong --- toolkit/mozapps/extensions/AddonManager.jsm | 4 ++ .../extensions/LightweightThemeManager.jsm | 26 +++---- .../extensions/test/xpcshell/test_theme.js | 2 +- .../extensions/test/xpcshell/test_update.js | 72 +++++++++++++++++++ 4 files changed, 90 insertions(+), 14 deletions(-) diff --git a/toolkit/mozapps/extensions/AddonManager.jsm b/toolkit/mozapps/extensions/AddonManager.jsm index 8f5383f70f47..ff998604bc2a 100644 --- a/toolkit/mozapps/extensions/AddonManager.jsm +++ b/toolkit/mozapps/extensions/AddonManager.jsm @@ -274,6 +274,10 @@ var AddonManagerInternal = { if (!Services.prefs.getBoolPref(PREF_EM_UPDATE_ENABLED)) return; + let scope = {}; + Components.utils.import("resource://gre/modules/LightweightThemeManager.jsm", scope); + scope.LightweightThemeManager.updateCurrentTheme(); + this.getAddonsByTypes(null, function getAddonsCallback(addons) { addons.forEach(function BUC_forEachCallback(addon) { if (addon.permissions & AddonManager.PERM_CAN_UPGRADE) { diff --git a/toolkit/mozapps/extensions/LightweightThemeManager.jsm b/toolkit/mozapps/extensions/LightweightThemeManager.jsm index 6353e5b6677b..0bdc1e1fe6c3 100644 --- a/toolkit/mozapps/extensions/LightweightThemeManager.jsm +++ b/toolkit/mozapps/extensions/LightweightThemeManager.jsm @@ -516,24 +516,24 @@ function _setCurrentTheme(aData, aLocal) { if (aData) { let theme = LightweightThemeManager.getUsedTheme(aData.id); - // TODO detect if it is an install and act accordingly - if (!theme) { + let isInstall = !theme || theme.version != aData.version; + if (isInstall) { + var oldWrapper = theme ? new AddonWrapper(theme) : null; var wrapper = new AddonWrapper(aData); AddonManagerPrivate.callInstallListeners("onExternalInstall", null, - wrapper, null, false); + wrapper, oldWrapper, false); AddonManagerPrivate.callAddonListeners("onInstalling", wrapper, false); } - let current = LightweightThemeManager.currentTheme; - if (!current || current.id != aData.id) { - let usedThemes = _usedThemesExceptId(aData.id); - if (current) - usedThemes.splice(1, 0, aData); - else - usedThemes.unshift(aData); - _updateUsedThemes(usedThemes); - } - if (!theme) + let current = LightweightThemeManager.currentTheme; + let usedThemes = _usedThemesExceptId(aData.id); + if (current && current.id != aData.id) + usedThemes.splice(1, 0, aData); + else + usedThemes.unshift(aData); + _updateUsedThemes(usedThemes); + + if (isInstall) AddonManagerPrivate.callAddonListeners("onInstalled", wrapper); } diff --git a/toolkit/mozapps/extensions/test/xpcshell/test_theme.js b/toolkit/mozapps/extensions/test/xpcshell/test_theme.js index 4a3f52924a6a..e561fbee9c25 100644 --- a/toolkit/mozapps/extensions/test/xpcshell/test_theme.js +++ b/toolkit/mozapps/extensions/test/xpcshell/test_theme.js @@ -662,7 +662,7 @@ function run_test_13() { do_check_eq(install.version, "1.0"); do_check_eq(install.name, "Test Theme 1"); do_check_eq(install.state, AddonManager.STATE_DOWNLOADED); - + prepare_test({ "theme1@tests.mozilla.org": [ "onInstalling", diff --git a/toolkit/mozapps/extensions/test/xpcshell/test_update.js b/toolkit/mozapps/extensions/test/xpcshell/test_update.js index 2840ab1247cc..698623ce0e6d 100644 --- a/toolkit/mozapps/extensions/test/xpcshell/test_update.js +++ b/toolkit/mozapps/extensions/test/xpcshell/test_update.js @@ -7,6 +7,8 @@ // The test extension uses an insecure update url. Services.prefs.setBoolPref("extensions.checkUpdateSecurity", false); +Components.utils.import("resource://gre/modules/LightweightThemeManager.jsm"); + do_load_httpd_js(); var testserver; var profileDir; @@ -229,6 +231,76 @@ function check_test_4(install) { a1.uninstall(); restartManager(0); + run_test_5(); + }); +} + +// Test that background update checks work for lightweight themes +function run_test_5() { + LightweightThemeManager.currentTheme = { + id: "1", + version: "1", + name: "Test LW Theme", + description: "A test theme", + author: "Mozilla", + homepageURL: "http://localhost:4444/data/index.html", + headerURL: "http://localhost:4444/data/header.png", + footerURL: "http://localhost:4444/data/footer.png", + previewURL: "http://localhost:4444/data/preview.png", + iconURL: "http://localhost:4444/data/icon.png", + updateURL: "http://localhost:4444/data/lwtheme.js" + }; + + // XXX The lightweight theme manager strips non-https updateURLs so hack it + // back in. + let themes = JSON.parse(Services.prefs.getCharPref("lightweightThemes.usedThemes")); + do_check_eq(themes.length, 1); + themes[0].updateURL = "http://localhost:4444/data/lwtheme.js"; + Services.prefs.setCharPref("lightweightThemes.usedThemes", JSON.stringify(themes)); + + testserver.registerPathHandler("/data/lwtheme.js", function(request, response) { + response.write(JSON.stringify({ + id: "1", + version: "2", + name: "Updated Theme", + description: "A test theme", + author: "Mozilla", + homepageURL: "http://localhost:4444/data/index2.html", + headerURL: "http://localhost:4444/data/header.png", + footerURL: "http://localhost:4444/data/footer.png", + previewURL: "http://localhost:4444/data/preview.png", + iconURL: "http://localhost:4444/data/icon2.png", + updateURL: "http://localhost:4444/data/lwtheme.js" + })); + }); + + AddonManager.getAddon("1@personas.mozilla.org", function(p1) { + do_check_neq(p1, null); + do_check_eq(p1.version, "1"); + do_check_eq(p1.name, "Test LW Theme"); + do_check_true(p1.isActive); + + prepare_test({ + "1@personas.mozilla.org": [ + ["onInstalling", false], + "onInstalled" + ] + }, [ + "onExternalInstall" + ], check_test_5); + + // Fake a timer event to cause a background update and wait for the magic to + // happen + gInternalManager.notify(null); + }); +} + +function check_test_5() { + AddonManager.getAddon("1@personas.mozilla.org", function(p1) { + do_check_neq(p1, null); + do_check_eq(p1.version, "2"); + do_check_eq(p1.name, "Updated Theme"); + end_test(); }); }