Bug 1579639 - Save calendar sort order pref when a new calendar is added. r=pmorris
authorGeoff Lankow <geoff@darktrojan.net>
Mon, 09 Sep 2019 20:01:45 +1200
changeset 36762 96ab8f83895f7438d86ed4d007baf7041f4d6a41
parent 36761 cca546ac666743fa248cb741ad5b319566a495ea
child 36763 0a09a1ce4f8deca477c8ed8baa242b63682da056
push id395
push userclokep@gmail.com
push dateMon, 02 Dec 2019 19:38:57 +0000
reviewerspmorris
bugs1579639
Bug 1579639 - Save calendar sort order pref when a new calendar is added. r=pmorris
calendar/base/content/calendar-management.js
calendar/test/browser/browser_calendarList.js
--- a/calendar/base/content/calendar-management.js
+++ b/calendar/base/content/calendar-management.js
@@ -167,16 +167,26 @@ function loadCalendarManager() {
     item.appendChild(image);
 
     calendarList.appendChild(item);
     if (calendar.getProperty("calendar-main-default")) {
       calendarList.selectedItem = item;
     }
   }
 
+  function saveSortOrder() {
+    let order = [...calendarList.children].map(i => i.getAttribute("calendar-id"));
+    Services.prefs.setStringPref("calendar.list.sortOrder", order.join(" "));
+    try {
+      Services.prefs.savePrefFile(null);
+    } catch (ex) {
+      cal.ERROR(ex);
+    }
+  }
+
   calendarList.addEventListener("click", event => {
     if (!event.target.matches("checkbox.calendar-displayed")) {
       return;
     }
 
     let item = event.target.closest("richlistitem");
     let calendarId = item.getAttribute("calendar-id");
     let calendar = calendarManager.getCalendarById(calendarId);
@@ -281,23 +291,17 @@ function loadCalendarManager() {
     }
 
     let item = calendarList.getElementsByAttribute("calendar-id", calendarId)[0];
     if (position == "bottom") {
       existing = null;
     }
     calendarList.insertBefore(item, existing);
 
-    let order = [...calendarList.children].map(i => i.getAttribute("calendar-id"));
-    Services.prefs.setStringPref("calendar.list.sortOrder", order.join(" "));
-    try {
-      Services.prefs.savePrefFile(null);
-    } catch (ex) {
-      cal.ERROR(ex);
-    }
+    saveSortOrder();
   });
   calendarList.addEventListener("keypress", event => {
     let item = calendarList.selectedItem;
     let calendarId = item.getAttribute("calendar-id");
     let calendar = calendarManager.getCalendarById(calendarId);
 
     switch (event.key) {
       case "Delete":
@@ -376,21 +380,23 @@ function loadCalendarManager() {
     QueryInterface: ChromeUtils.generateQI([Ci.calICalendarManagerObserver]),
 
     onCalendarRegistered(calendar) {
       let inComposite = calendar.getProperty("calendar-main-in-composite");
       if (inComposite === null) {
         compositeCalendar.addCalendar(calendar);
       }
       addCalendarItem(calendar);
+      saveSortOrder();
     },
     onCalendarUnregistering(calendar) {
       compositeCalendar.removeCalendar(calendar);
       let item = calendarList.getElementsByAttribute("calendar-id", calendar.id)[0];
       item.remove();
+      saveSortOrder();
     },
     onCalendarDeleting(calendar) {},
   };
   calendarManager.addObserver(calendarList._calendarManagerObserver);
 }
 
 /**
  * Creates the initial "Home" calendar if no calendar exists.
--- a/calendar/test/browser/browser_calendarList.js
+++ b/calendar/test/browser/browser_calendarList.js
@@ -76,47 +76,59 @@ add_task(async () => {
           is(item.querySelector(".calendar-name").value, expectedValue);
           break;
       }
     }
   }
 
   function checkDisplayed(...expected) {
     let calendarList = document.getElementById("calendar-list");
+    ok(calendarList.itemCount > Math.max(...expected));
     for (let i = 0; i < calendarList.itemCount; i++) {
       is(calendarList.itemChildren[i].querySelector("checkbox").checked, expected.includes(i));
     }
   }
 
+  function checkSortOrder(...expected) {
+    let orderPref = Services.prefs.getStringPref("calendar.list.sortOrder", "wrong");
+    isnot(orderPref, "wrong");
+    let order = orderPref.split(" ");
+    is(order.length, expected.length);
+    for (let i = 0; i < expected.length; i++) {
+      is(order[i], calendars[expected[i]].id);
+    }
+  }
+
   let calendarList = document.getElementById("calendar-list");
   let contextMenu = document.getElementById("list-calendars-context-menu");
   let manager = cal.getCalendarManager();
   let composite = cal.view.getCompositeCalendar(window);
 
   await openCalendarTab();
 
   // Check the default calendar.
-  is(manager.getCalendars({}).length, 1);
+  let calendars = manager.getCalendars({});
+  is(calendars.length, 1);
   is(calendarList.itemCount, 1);
   checkProperties(0, {
     color: "rgb(168, 194, 225)",
     name: "Home",
   });
+  checkSortOrder(0);
 
   // Test adding calendars.
 
   // Show the new calendar wizard. Don't use it, just prove the context menu works.
   await withModalDialog(
     () => calendarListContextMenu(calendarList, "list-calendars-context-new"),
     undefined,
     "cancel"
   );
 
   // Add some new calendars, check their properties.
-  let calendars = [];
   let uri = Services.io.newURI("moz-memory-calendar://");
   for (let i = 1; i <= 3; i++) {
     calendars[i] = manager.createCalendar("memory", uri);
     calendars[i].name = `Mochitest ${i}`;
     manager.registerCalendar(calendars[i]);
   }
 
   is(manager.getCalendars({}).length, 4);
@@ -125,16 +137,17 @@ add_task(async () => {
   for (let i = 1; i <= 3; i++) {
     checkProperties(i, {
       id: calendars[i].id,
       displayed: true,
       color: "rgb(168, 194, 225)",
       name: `Mochitest ${i}`,
     });
   }
+  checkSortOrder(0, 1, 2, 3);
 
   // Test the context menu.
 
   await new Promise(resolve => setTimeout(resolve));
   EventUtils.synthesizeMouseAtCenter(calendarList.itemChildren[1], {});
   await new Promise(resolve => setTimeout(resolve));
   await calendarListContextMenu(calendarList.itemChildren[1]);
   await new Promise(resolve => setTimeout(resolve));
@@ -147,47 +160,47 @@ add_task(async () => {
 
   // Test show/hide.
   // TODO: Check events on calendars are hidden/shown.
 
   EventUtils.synthesizeMouseAtCenter(calendarList.itemChildren[2].querySelector("checkbox"), {});
   is(document.activeElement, calendarList);
   is(calendarList.selectedItem, calendarList.itemChildren[2]);
   is(composite.getCalendarById(calendars[2].id), null);
-  checkDisplayed(0, 1, 3, 4);
+  checkDisplayed(0, 1, 3);
 
   composite.removeCalendar(calendars[1]);
-  checkDisplayed(0, 3, 4);
+  checkDisplayed(0, 3);
 
   await calendarListContextMenu(
     calendarList.itemChildren[3],
     "list-calendars-context-togglevisible"
   );
-  checkDisplayed(0, 4);
+  checkDisplayed(0);
 
   EventUtils.synthesizeMouseAtCenter(calendarList.itemChildren[2].querySelector("checkbox"), {});
   is(composite.getCalendarById(calendars[2].id), calendars[2]);
   is(document.activeElement, calendarList);
   is(calendarList.selectedItem, calendarList.itemChildren[2]);
-  checkDisplayed(0, 2, 4);
+  checkDisplayed(0, 2);
 
   composite.addCalendar(calendars[1]);
-  checkDisplayed(0, 1, 2, 4);
+  checkDisplayed(0, 1, 2);
 
   await calendarListContextMenu(
     calendarList.itemChildren[3],
     "list-calendars-context-togglevisible"
   );
-  checkDisplayed(0, 1, 2, 3, 4);
+  checkDisplayed(0, 1, 2, 3);
 
   await calendarListContextMenu(calendarList.itemChildren[1], "list-calendars-context-showonly");
   checkDisplayed(1);
 
   await calendarListContextMenu(calendarList, "list-calendars-context-showall");
-  checkDisplayed(0, 1, 2, 3, 4);
+  checkDisplayed(0, 1, 2, 3);
 
   // Test editing calendars.
 
   await withModalDialog(
     () => {
       EventUtils.synthesizeMouseAtCenter(calendarList.itemChildren[1], { clickCount: 2 });
     },
     win => {
@@ -246,39 +259,55 @@ add_task(async () => {
 
   is(document.activeElement, calendarList);
   is(calendarList.selectedItem, calendarList.itemChildren[1]);
   checkProperties(1, { disabled: true });
 
   calendars[1].setProperty("disabled", false);
   checkProperties(1, { disabled: false });
 
+  // Test reordering calendars.
+
+  await EventUtils.synthesizePlainDragAndDrop({
+    srcElement: calendarList.itemChildren[3],
+    destElement: calendarList.itemChildren[0],
+  });
+  checkSortOrder(0, 3, 1, 2);
+
+  is(document.activeElement, calendarList);
+  is(calendarList.selectedItem, calendarList.itemChildren[1]);
+
   // Test deleting calendars.
 
   // Delete a calendar by unregistering it.
   manager.unregisterCalendar(calendars[3]);
   is(manager.getCalendars({}).length, 3);
   is(calendarList.itemCount, 3);
+  checkSortOrder(0, 1, 2);
 
   // Start to remove a calendar. Cancel the prompt.
+  EventUtils.synthesizeMouseAtCenter(calendarList.itemChildren[1], {});
   await withMockPromptService(1, () => {
     EventUtils.synthesizeKey("VK_DELETE");
   });
   is(manager.getCalendars({}).length, 3);
   is(calendarList.itemCount, 3);
+  checkSortOrder(0, 1, 2);
 
   // Remove a calendar with the keyboard.
   await withMockPromptService(0, () => {
     EventUtils.synthesizeKey("VK_DELETE");
   });
   is(manager.getCalendars({}).length, 2);
   is(calendarList.itemCount, 2);
+  checkSortOrder(0, 2);
 
   // Remove a calendar with the context menu.
   await withMockPromptService(0, async () => {
     EventUtils.synthesizeMouseAtCenter(calendarList.itemChildren[1], {});
     await calendarListContextMenu(calendarList.itemChildren[1], "list-calendars-context-delete");
   });
   is(manager.getCalendars({}).length, 1);
   is(calendarList.itemCount, 1);
+  checkSortOrder(0);
 
   await closeCalendarTab();
 });