Bug 1584853 - When removing a category colour, set pref value to empty string instead of clearing it; r=pmorris
authorGeoff Lankow <geoff@darktrojan.net>
Tue, 14 Jan 2020 14:04:37 +1300
changeset 37972 8059037368711aca30213657c97b3ea62d2574c5
parent 37971 899c53e49dbd6ab97d0145f68c0fc9b94bfb4195
child 37973 328201fcf26982ca8e8918b4e0118a6dfd876f0a
push id398
push userclokep@gmail.com
push dateMon, 09 Mar 2020 19:10:28 +0000
reviewerspmorris
bugs1584853
Bug 1584853 - When removing a category colour, set pref value to empty string instead of clearing it; r=pmorris
calendar/base/content/preferences/categories.js
calendar/test/browser/preferences/browser_categoryColors.js
--- a/calendar/base/content/preferences/categories.js
+++ b/calendar/base/content/preferences/categories.js
@@ -1,17 +1,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 /* exported gCategoriesPane */
 
 /* import-globals-from ../../../lightning/content/messenger-overlay-preferences.js */
 // From categories.xhtml.
-/* globals noneLabel, newTitle, editTitle, overwrite, overwriteTitle, noBlankCategories */
+/* globals newTitle, editTitle, overwrite, overwriteTitle, noBlankCategories */
 
 var { cal } = ChromeUtils.import("resource://calendar/modules/calUtils.jsm");
 var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
 var { AppConstants } = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
 
 Preferences.add({ id: "calendar.categories.names", type: "string" });
 
 var gCategoryList;
@@ -87,22 +87,19 @@ var gCategoriesPane = {
       let categoryName = document.createXULElement("label");
       categoryName.setAttribute("id", gCategoryList[i]);
       categoryName.setAttribute("flex", "1");
       categoryName.setAttribute("value", gCategoryList[i]);
       let categoryNameFix = cal.view.formatStringForCSSRule(gCategoryList[i]);
 
       let categoryColor = document.createXULElement("box");
       categoryColor.setAttribute("width", "150");
-      try {
-        let colorCode = categoryPrefBranch.getCharPref(categoryNameFix);
-        categoryColor.setAttribute("id", colorCode);
-        categoryColor.setAttribute("style", "background-color: " + colorCode + ";");
-      } catch (ex) {
-        categoryColor.setAttribute("label", noneLabel);
+      let colorCode = categoryPrefBranch.getCharPref(categoryNameFix, "");
+      if (colorCode) {
+        categoryColor.style.backgroundColor = colorCode;
       }
 
       newListItem.appendChild(categoryName);
       newListItem.appendChild(categoryColor);
       listbox.appendChild(newListItem);
     }
   },
 
@@ -214,25 +211,17 @@ var gCategoriesPane = {
       this.backupData(categoryNameFix);
       gCategoryList.push(categoryName);
       if (categoryColor) {
         categoryPrefBranch.setCharPref(categoryNameFix, categoryColor);
       }
     } else {
       this.backupData(categoryNameFix);
       gCategoryList.splice(list.selectedIndex, 1, categoryName);
-      if (categoryColor) {
-        categoryPrefBranch.setCharPref(categoryNameFix, categoryColor);
-      } else {
-        try {
-          categoryPrefBranch.clearUserPref(categoryNameFix);
-        } catch (ex) {
-          dump("Exception caught in 'saveCategory': " + ex + "\n");
-        }
-      }
+      categoryPrefBranch.setCharPref(categoryNameFix, categoryColor || "");
     }
 
     // If 'Overwrite' was chosen, delete category that was being edited
     if (toBeDeleted != -1) {
       list.selectedIndex = toBeDeleted;
       this.deleteCategory();
     }
 
--- a/calendar/test/browser/preferences/browser_categoryColors.js
+++ b/calendar/test/browser/preferences/browser_categoryColors.js
@@ -25,17 +25,20 @@ add_task(function testCategoryColors() {
     listBox.scrollIntoView();
     Assert.equal(listBox.itemChildren.length, 22);
 
     for (let item of listBox.itemChildren) {
       dump(`${item.firstElementChild.value}: ${item.lastElementChild.style.backgroundColor}\n`);
       Assert.ok(item.lastElementChild.style.backgroundColor);
     }
 
+    // Edit the name and colour of a built-in category.
+
     controller.click(content_tab_eid(tab, "categorieslist"), 5, 5);
+    Assert.equal(listBox.selectedIndex, 0);
     controller.click(content_tab_eid(tab, "editCButton"));
 
     let editFrame = wait_for_frame_load(
       tab.browser.contentDocument.getElementById("dialogOverlay-0").querySelector("browser"),
       "chrome://calendar/content/preferences/editCategory.xhtml"
     );
     let { replaceText, lookup } = helpersForController(editFrame);
     let categoryName = editFrame.eid("categoryName");
@@ -43,18 +46,45 @@ add_task(function testCategoryColors() {
     editFrame.e("categoryColor").value = "#00CC00";
     editFrame.click(
       lookup(`
             id("editCategoryWindow")/id("editCategory")/shadow/{"class":"dialog-button-box"}/{"dlgtype":"accept"}
         `)
     );
 
     let listItem = listBox.itemChildren[listBox.itemCount - 1];
+    Assert.equal(listBox.selectedItem, listItem);
     Assert.equal(listItem.firstElementChild.value, "ZZZ Mozmill");
     Assert.equal(listItem.lastElementChild.style.backgroundColor, "rgb(0, 204, 0)");
+    Assert.equal(Services.prefs.getCharPref("calendar.category.color.zzz_mozmill"), "#00cc00");
+
+    // Remove the colour of a built-in category.
+
+    controller.click(content_tab_eid(tab, "categorieslist"), 5, 5);
+    controller.keypress(content_tab_eid(tab, "categorieslist"), "VK_HOME", {});
+    Assert.equal(listBox.selectedIndex, 0);
+    let itemName = listBox.itemChildren[0].firstElementChild.value;
+    controller.click(content_tab_eid(tab, "editCButton"));
+
+    editFrame = wait_for_frame_load(
+      tab.browser.contentDocument.getElementById("dialogOverlay-0").querySelector("browser"),
+      "chrome://calendar/content/preferences/editCategory.xhtml"
+    );
+    editFrame.click(editFrame.eid("useColor"));
+    editFrame.click(
+      lookup(`
+            id("editCategoryWindow")/id("editCategory")/shadow/{"class":"dialog-button-box"}/{"dlgtype":"accept"}
+        `)
+    );
+
+    listItem = listBox.itemChildren[0];
+    Assert.equal(listBox.selectedItem, listItem);
+    Assert.equal(listItem.firstElementChild.value, itemName);
+    Assert.equal(listItem.lastElementChild.style.backgroundColor, "");
+    Assert.equal(Services.prefs.getCharPref(`calendar.category.color.${itemName.toLowerCase()}`), "");
   }, controller);
 });
 
 registerCleanupFunction(function teardownModule() {
   if (prefTab) {
     closeLightningPrefs(prefTab);
   }
 });