Bug 1581765 - Migrate existing Mork address books to JS provider - tests; r=mkmelin
☠☠ backed out by 3d3686bfb2ef ☠ ☠
authorGeoff Lankow <geoff@darktrojan.net>
Wed, 02 Oct 2019 17:14:17 +1300
changeset 37089 72b1cec1260e68511a8b99d0e72fd8b0cefd44dc
parent 37088 f8e09ea17f2a495ae77717a81eb35dacd861f344
child 37090 a9df26051fec724a5abb232f47e2d98cabca7a2a
push id395
push userclokep@gmail.com
push dateMon, 02 Dec 2019 19:38:57 +0000
reviewersmkmelin
bugs1581765
Bug 1581765 - Migrate existing Mork address books to JS provider - tests; r=mkmelin
mailnews/addrbook/test/unit/head_migration.js
mailnews/addrbook/test/unit/test_migration1.js
mailnews/addrbook/test/unit/test_migration2.js
mailnews/addrbook/test/unit/test_migration3.js
mailnews/addrbook/test/unit/xpcshell-jsaddrbook.ini
new file mode 100644
--- /dev/null
+++ b/mailnews/addrbook/test/unit/head_migration.js
@@ -0,0 +1,44 @@
+/* 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/. */
+
+var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
+var { MailMigrator } = ChromeUtils.import(
+  "resource:///modules/MailMigrator.jsm"
+);
+var { MailServices } = ChromeUtils.import(
+  "resource:///modules/MailServices.jsm"
+);
+
+// Ensure the profile directory is set up
+var profileDir = do_get_profile();
+
+// OSX Address Book deactivation (Bug 955842)
+Services.prefs.deleteBranch("ldap_2.servers.osx.");
+
+registerCleanupFunction(function() {
+  load("../../../resources/mailShutdown.js");
+});
+
+/**
+ * Copies a file into the profile directory.
+ *
+ * @param {String} path      Path to the source data
+ * @param {String} leafName  Final file name in the profile
+ */
+function copyABFile(path, leafName) {
+  let file = do_get_file(path);
+  file.copyTo(profileDir, leafName);
+}
+
+/**
+ * Checks that a file exists or doesn't exist in the profile directory.
+ *
+ * @param {String} leafName      File name that should be checked
+ * @param {boolean} shouldExist  Whether the file should exist
+ */
+function checkFileExists(leafName, shouldExist) {
+  let file = profileDir.clone();
+  file.append(leafName);
+  equal(file.exists(), shouldExist);
+}
new file mode 100644
--- /dev/null
+++ b/mailnews/addrbook/test/unit/test_migration1.js
@@ -0,0 +1,84 @@
+/* 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/. */
+
+/*
+ * Test auto-migration from Mork address books to JS/SQLite address books.
+ *
+ * This test profile has only the two default address books to migrate, and
+ * neither have any mailing lists.
+ */
+function run_test() {
+  // Copy address books to be migrated into the profile.
+
+  copyABFile("data/cardForEmail.mab", "abook.mab");
+  copyABFile("data/collect.mab", "history.mab");
+
+  // Do the migration.
+
+  MailMigrator._migrateAddressBooks();
+
+  // Check new files have been created, and old ones renamed.
+
+  checkFileExists("abook.sqlite", true);
+  checkFileExists("abook.mab", false);
+  checkFileExists("abook.mab.bak", true);
+  checkFileExists("history.sqlite", true);
+  checkFileExists("history.mab", false);
+  checkFileExists("history.mab.bak", true);
+
+  // Check that the default preferences are untouched.
+
+  equal(Services.prefs.getIntPref("ldap_2.servers.pab.dirType"), 101);
+  equal(
+    Services.prefs.getStringPref("ldap_2.servers.pab.filename"),
+    "abook.sqlite"
+  );
+  equal(Services.prefs.getIntPref("ldap_2.servers.history.dirType"), 101);
+  equal(
+    Services.prefs.getStringPref("ldap_2.servers.history.filename"),
+    "history.sqlite"
+  );
+
+  // Check the new address books.
+
+  let directories = [...MailServices.ab.directories];
+  equal(directories.length, 2);
+  equal(directories[0].dirType, 101);
+  equal(directories[1].dirType, 101);
+
+  let [personalBook, historyBook] = directories;
+
+  // For this directory, just check we have all the right cards.
+
+  let personalCards = [...personalBook.childCards];
+  equal(personalCards.length, 4);
+  Assert.deepEqual(personalCards.map(card => card.displayName).sort(), [
+    "DisplayName1",
+    "Empty Email",
+    "Jane Doe",
+    "John Doe",
+  ]);
+
+  let personalLists = [...personalBook.addressLists.enumerate()];
+  equal(personalLists.length, 0);
+
+  // More detailed check.
+
+  let historyCards = [...historyBook.childCards];
+  equal(historyCards.length, 1);
+  equal(historyCards[0].firstName, "Other");
+  equal(historyCards[0].lastName, "Book");
+  equal(historyCards[0].primaryEmail, "other@book.invalid");
+  equal(historyCards[0].displayName, "Other Book");
+  equal(historyCards[0].getProperty("LastModifiedDate", "bad"), "0");
+  equal(historyCards[0].getProperty("AllowRemoteContent", "bad"), "0");
+  equal(historyCards[0].getProperty("PopularityIndex", "bad"), "0");
+  equal(historyCards[0].getProperty("PreferMailFormat", "bad"), "0");
+  // This property exists in the .mab file but should not be copied to the
+  // .sqlite file. It's not wrong, but we don't use them any more.
+  equal(historyCards[0].getProperty("LowercasePrimaryEmail", "bad"), "bad");
+
+  let historyLists = [...historyBook.addressLists.enumerate()];
+  equal(historyLists.length, 0);
+}
new file mode 100644
--- /dev/null
+++ b/mailnews/addrbook/test/unit/test_migration2.js
@@ -0,0 +1,111 @@
+/* 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/. */
+
+/*
+ * Test auto-migration from Mork address books to JS/SQLite address books.
+ *
+ * This test profile has a personal address book with contacts and mailing
+ * lists. The history book doesn't exist, which is unusual but still
+ * shouldn't cause any problems.
+ */
+
+const { fixIterator } = ChromeUtils.import(
+  "resource:///modules/iteratorUtils.jsm"
+);
+
+function run_test() {
+  // Copy address book to be migrated into the profile.
+
+  copyABFile("../../../data/abLists1.mab", "abook.mab");
+
+  // Do the migration.
+
+  MailMigrator._migrateAddressBooks();
+
+  // Check new files have been created, and old ones renamed.
+
+  checkFileExists("abook.sqlite", true);
+  checkFileExists("abook.mab", false);
+  checkFileExists("abook.mab.bak", true);
+  checkFileExists("history.sqlite", true);
+  checkFileExists("history.mab", false);
+  checkFileExists("history.mab.bak", false);
+
+  // Check that the default preferences are untouched.
+
+  equal(Services.prefs.getIntPref("ldap_2.servers.pab.dirType"), 101);
+  equal(
+    Services.prefs.getStringPref("ldap_2.servers.pab.filename"),
+    "abook.sqlite"
+  );
+  equal(Services.prefs.getIntPref("ldap_2.servers.history.dirType"), 101);
+  equal(
+    Services.prefs.getStringPref("ldap_2.servers.history.filename"),
+    "history.sqlite"
+  );
+
+  // Check the new address books.
+
+  let directories = [...MailServices.ab.directories];
+  equal(directories.length, 2);
+  equal(directories[0].dirType, 101);
+  equal(directories[1].dirType, 101);
+
+  let [personalBook, historyBook] = directories;
+
+  // Check we have all the right cards.
+
+  let personalCards = [...personalBook.childCards];
+  equal(personalCards.length, 8);
+
+  let lists = personalCards.slice(0, 3);
+
+  for (let i = 0; i < 3; i++) {
+    ok(lists[i].isMailList);
+    equal(lists[i].displayName, `TestList${i + 1}`);
+    equal(lists[i].getProperty("NickName", "bad"), "");
+    equal(lists[i].getProperty("Notes", "bad"), "");
+  }
+
+  let contacts = personalCards.slice(3);
+
+  for (let i = 0; i < 5; i++) {
+    ok(!contacts[i].isMailList);
+    equal(contacts[i].primaryEmail, `test${i + 1}@foo.invalid`);
+  }
+
+  // Check the lists have the right members.
+
+  let personalLists = [
+    ...fixIterator(personalBook.addressLists.enumerate(), Ci.nsIAbDirectory),
+  ];
+  equal(personalLists.length, 3);
+
+  let listCards = [
+    ...fixIterator(personalLists[0].addressLists.enumerate(), Ci.nsIAbCard),
+  ].map(c => c.primaryEmail);
+  Assert.deepEqual(listCards, [
+    "test1@foo.invalid",
+    "test2@foo.invalid",
+    "test3@foo.invalid",
+  ]);
+
+  listCards = [
+    ...fixIterator(personalLists[1].addressLists.enumerate(), Ci.nsIAbCard),
+  ].map(c => c.primaryEmail);
+  Assert.deepEqual(listCards, ["test4@foo.invalid"]);
+
+  listCards = [
+    ...fixIterator(personalLists[2].addressLists.enumerate(), Ci.nsIAbCard),
+  ].map(c => c.primaryEmail);
+  Assert.deepEqual(listCards, ["test5@foo.invalid"]);
+
+  // Check the history book, which should be empty.
+
+  let historyCards = [...historyBook.childCards];
+  equal(historyCards.length, 0);
+
+  let historyLists = [...historyBook.addressLists.enumerate()];
+  equal(historyLists.length, 0);
+}
new file mode 100644
--- /dev/null
+++ b/mailnews/addrbook/test/unit/test_migration3.js
@@ -0,0 +1,125 @@
+/* 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/. */
+
+/*
+ * Test auto-migration from Mork address books to JS/SQLite address books.
+ *
+ * This test profile has a non-default Mork address book, and no default
+ * address books.
+ */
+
+const { fixIterator } = ChromeUtils.import(
+  "resource:///modules/iteratorUtils.jsm"
+);
+
+function run_test() {
+  // Copy address book to be migrated into the profile.
+
+  copyABFile("data/existing.mab", "test.mab");
+
+  Services.prefs.setStringPref(
+    "ldap_2.servers.Test.description",
+    "This is a test!"
+  );
+  Services.prefs.setIntPref("ldap_2.servers.Test.dirType", 2);
+  Services.prefs.setStringPref("ldap_2.servers.Test.filename", "test.mab");
+  Services.prefs.setStringPref(
+    "ldap_2.servers.Test.uid",
+    "12345678-1234-1234-1234-123456789012"
+  );
+  Services.prefs.setStringPref(
+    "ldap_2.servers.Test.uri",
+    "moz-abmdbdirectory://test.mab"
+  );
+  Services.prefs.setStringPref(
+    "mail.collect_addressbook",
+    "moz-abmdbdirectory://test.mab"
+  );
+  Services.prefs.setStringPref(
+    "mail.server.default.whiteListAbURI",
+    "moz-abmdbdirectory://test.mab"
+  );
+
+  // Do the migration.
+
+  MailMigrator._migrateAddressBooks();
+
+  // Check new files have been created, and old ones renamed.
+
+  checkFileExists("abook.sqlite", true);
+  checkFileExists("abook.mab", false);
+  checkFileExists("abook.mab.bak", false);
+  checkFileExists("history.sqlite", true);
+  checkFileExists("history.mab", false);
+  checkFileExists("history.mab.bak", false);
+  checkFileExists("test.sqlite", true);
+  checkFileExists("test.mab", false);
+  checkFileExists("test.mab.bak", true);
+
+  // Check that the default preferences are untouched.
+
+  equal(Services.prefs.getIntPref("ldap_2.servers.pab.dirType"), 101);
+  equal(
+    Services.prefs.getStringPref("ldap_2.servers.pab.filename"),
+    "abook.sqlite"
+  );
+  equal(Services.prefs.getIntPref("ldap_2.servers.history.dirType"), 101);
+  equal(
+    Services.prefs.getStringPref("ldap_2.servers.history.filename"),
+    "history.sqlite"
+  );
+
+  // Check that the test book's preferences are updated, or not updated.
+
+  equal(
+    Services.prefs.getStringPref("ldap_2.servers.Test.description"),
+    "This is a test!"
+  );
+  equal(Services.prefs.getIntPref("ldap_2.servers.Test.dirType"), 101);
+  equal(
+    Services.prefs.getStringPref("ldap_2.servers.Test.filename"),
+    "test.sqlite"
+  );
+  equal(
+    Services.prefs.getStringPref("ldap_2.servers.Test.uid"),
+    "12345678-1234-1234-1234-123456789012"
+  );
+  equal(
+    Services.prefs.getStringPref("ldap_2.servers.Test.uri"),
+    "jsaddrbook://test.sqlite"
+  );
+
+  // Check that references to the book are updated.
+
+  equal(
+    Services.prefs.getStringPref("mail.collect_addressbook"),
+    "jsaddrbook://test.sqlite"
+  );
+  equal(
+    Services.prefs.getStringPref("mail.server.default.whiteListAbURI"),
+    "jsaddrbook://test.sqlite"
+  );
+
+  // Check the new address books.
+
+  let directories = [...MailServices.ab.directories];
+  equal(directories.length, 3);
+  equal(directories[0].dirType, 101);
+  equal(directories[1].dirType, 101);
+  equal(directories[2].dirType, 101);
+
+  let [testBook] = directories;
+
+  // Check we have all the right cards.
+
+  let testCards = [...testBook.childCards];
+  equal(testCards.length, 2);
+
+  ok(testCards[0].isMailList);
+  equal(testCards[0].displayName, "List");
+
+  ok(!testCards[1].isMailList);
+  equal(testCards[1].displayName, "First Last");
+  equal(testCards[1].primaryEmail, "first@last");
+}
--- a/mailnews/addrbook/test/unit/xpcshell-jsaddrbook.ini
+++ b/mailnews/addrbook/test/unit/xpcshell-jsaddrbook.ini
@@ -1,8 +1,14 @@
 [DEFAULT]
 dupe-manifest =
 head = head_jsaddrbook.js
 tail =
 support-files = data/*
 tags = jsaddrbook
 
 [include:xpcshell.ini]
+[test_migration1.js]
+head = head_migration.js
+[test_migration2.js]
+head = head_migration.js
+[test_migration3.js]
+head = head_migration.js