Bug 553113: Perform automatic updates for the default lightweight theme. r=robstrong
authorDave Townsend <dtownsend@oxymoronical.com>
Wed, 28 Apr 2010 11:47:22 -0700
changeset 41571 241824a1a2d261d376948c9f6006fb4bc970506c
parent 41570 cf1b0b0b303836171eed84f3951f3c815e6053bb
child 41572 f1e27d61946b9713761f84573e672c8c384424e2
push idunknown
push userunknown
push dateunknown
reviewersrobstrong
bugs553113
milestone1.9.3a5pre
Bug 553113: Perform automatic updates for the default lightweight theme. r=robstrong
toolkit/mozapps/extensions/AddonManager.jsm
toolkit/mozapps/extensions/LightweightThemeManager.jsm
toolkit/mozapps/extensions/test/xpcshell/test_theme.js
toolkit/mozapps/extensions/test/xpcshell/test_update.js
--- a/toolkit/mozapps/extensions/AddonManager.jsm
+++ b/toolkit/mozapps/extensions/AddonManager.jsm
@@ -269,16 +269,20 @@ var AddonManagerInternal = {
   /**
    * Performs a background update check by starting an update for all add-ons
    * that can be updated.
    */
   backgroundUpdateCheck: function AMI_backgroundUpdateCheck() {
     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) {
           addon.findUpdates({
             onUpdateAvailable: function BUC_onUpdateAvailable(addon, install) {
               install.install();
             }
           }, AddonManager.UPDATE_WHEN_PERIODIC_UPDATE);
--- a/toolkit/mozapps/extensions/LightweightThemeManager.jsm
+++ b/toolkit/mozapps/extensions/LightweightThemeManager.jsm
@@ -511,34 +511,34 @@ function _setCurrentTheme(aData, aLocal)
 
   let cancel = Cc["@mozilla.org/supports-PRBool;1"].createInstance(Ci.nsISupportsPRBool);
   cancel.data = false;
   _observerService.notifyObservers(cancel, "lightweight-theme-change-requested",
                                    JSON.stringify(aData));
 
   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);
-    }
+    let usedThemes = _usedThemesExceptId(aData.id);
+    if (current && current.id != aData.id)
+      usedThemes.splice(1, 0, aData);
+    else
+      usedThemes.unshift(aData);
+    _updateUsedThemes(usedThemes);
 
-    if (!theme)
+    if (isInstall)
        AddonManagerPrivate.callAddonListeners("onInstalled", wrapper);
   }
 
   if (cancel.data)
     return null;
 
   AddonManagerPrivate.notifyAddonChanged(aData ? aData.id + ID_SUFFIX : null,
                                          ADDON_TYPE, needsRestart);
--- a/toolkit/mozapps/extensions/test/xpcshell/test_theme.js
+++ b/toolkit/mozapps/extensions/test/xpcshell/test_theme.js
@@ -657,17 +657,17 @@ function run_test_13() {
     AddonManager.getInstallForFile(do_get_addon("test_theme"), function(install) {
       ensure_test_completed();
 
       do_check_neq(install, null);
       do_check_eq(install.type, "theme");
       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",
         ]
       }, [
         "onInstallStarted",
         "onInstallEnded",
       ], check_test_13);
--- a/toolkit/mozapps/extensions/test/xpcshell/test_update.js
+++ b/toolkit/mozapps/extensions/test/xpcshell/test_update.js
@@ -2,16 +2,18 @@
  * http://creativecommons.org/publicdomain/zero/1.0/
  */
 
 // This verifies that add-on update checks work
 
 // 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;
 
 function run_test() {
   createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9.2");
 
   // Create and configure the HTTP server.
@@ -224,11 +226,81 @@ function check_test_4(install) {
 
   restartManager(1);
   AddonManager.getAddon("addon1@tests.mozilla.org", function(a1) {
     do_check_neq(a1, null);
     do_check_eq(a1.version, "2.0");
     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();
   });
 }