diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /comm/mailnews/db/msgdb/test/unit | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
8 files changed, 482 insertions, 0 deletions
diff --git a/comm/mailnews/db/msgdb/test/unit/head_maildb.js b/comm/mailnews/db/msgdb/test/unit/head_maildb.js new file mode 100644 index 0000000000..5b52dbb304 --- /dev/null +++ b/comm/mailnews/db/msgdb/test/unit/head_maildb.js @@ -0,0 +1,21 @@ +var { MailServices } = ChromeUtils.import( + "resource:///modules/MailServices.jsm" +); +var { XPCOMUtils } = ChromeUtils.importESModule( + "resource://gre/modules/XPCOMUtils.sys.mjs" +); +var { mailTestUtils } = ChromeUtils.import( + "resource://testing-common/mailnews/MailTestUtils.jsm" +); +var { localAccountUtils } = ChromeUtils.import( + "resource://testing-common/mailnews/LocalAccountUtils.jsm" +); + +var CC = Components.Constructor; + +// Ensure the profile directory is set up +do_get_profile(); + +registerCleanupFunction(function () { + load("../../../../../mailnews/resources/mailShutdown.js"); +}); diff --git a/comm/mailnews/db/msgdb/test/unit/test_enumerator_cleanup.js b/comm/mailnews/db/msgdb/test/unit/test_enumerator_cleanup.js new file mode 100644 index 0000000000..dfb5aa5285 --- /dev/null +++ b/comm/mailnews/db/msgdb/test/unit/test_enumerator_cleanup.js @@ -0,0 +1,56 @@ +/* + * Test nsMsgDatabase's cleanup of nsMsgDBEnumerators + */ + +var { MailServices } = ChromeUtils.import( + "resource:///modules/MailServices.jsm" +); + +var anyOldMessage = do_get_file("../../../../data/bugmail1"); + +/** + * Test closing a db with an outstanding enumerator. + */ +function test_enumerator_cleanup() { + let db = localAccountUtils.inboxFolder.msgDatabase; + let enumerator = db.enumerateMessages(); + Cc["@mozilla.org/msgDatabase/msgDBService;1"] + .getService(Ci.nsIMsgDBService) + .forceFolderDBClosed(localAccountUtils.inboxFolder); + localAccountUtils.inboxFolder.msgDatabase = null; + db = null; + gc(); + [...enumerator]; + do_test_finished(); +} + +/* + * This infrastructure down here exists just to get + * test_references_header_parsing its message header. + */ + +function run_test() { + localAccountUtils.loadLocalMailAccount(); + do_test_pending(); + MailServices.copy.copyFileMessage( + anyOldMessage, + localAccountUtils.inboxFolder, + null, + false, + 0, + "", + messageHeaderGetterListener, + null + ); + return true; +} + +var messageHeaderGetterListener = { + OnStartCopy() {}, + OnProgress(aProgress, aProgressMax) {}, + GetMessageId(aMessageId) {}, + SetMessageKey(aKey) {}, + OnStopCopy(aStatus) { + do_timeout(0, test_enumerator_cleanup); + }, +}; diff --git a/comm/mailnews/db/msgdb/test/unit/test_filter_enumerator.js b/comm/mailnews/db/msgdb/test/unit/test_filter_enumerator.js new file mode 100644 index 0000000000..914b5afd29 --- /dev/null +++ b/comm/mailnews/db/msgdb/test/unit/test_filter_enumerator.js @@ -0,0 +1,100 @@ +/* import-globals-from ../../../../test/resources/MessageGenerator.jsm */ +load("../../../../resources/MessageGenerator.jsm"); + +var gMessages = []; + +const kSetCount = 13; +const kNumExpectedMatches = 10; + +function setupGlobals() { + localAccountUtils.loadLocalMailAccount(); + // Create a message generator + let messageGenerator = new MessageGenerator(); + let localInbox = localAccountUtils.inboxFolder.QueryInterface( + Ci.nsIMsgLocalMailFolder + ); + + for (let i = 0; i < kSetCount; i++) { + let message = messageGenerator.makeMessage(); + gMessages.push(message); + localInbox.addMessage(message.toMboxString()); + } +} + +function run_test() { + setupGlobals(); + do_test_pending(); + let inboxDB = localAccountUtils.inboxFolder.msgDatabase; + + // give messages 1,3,5 gloda-ids. These won't end up in our search hits. + let msgHdr1 = inboxDB.getMsgHdrForMessageID(gMessages[0].messageId); + msgHdr1.setUint32Property("gloda-id", 11111); + let msgHdr3 = inboxDB.getMsgHdrForMessageID(gMessages[2].messageId); + msgHdr3.setUint32Property("gloda-id", 33333); + let msgHdr5 = inboxDB.getMsgHdrForMessageID(gMessages[4].messageId); + msgHdr5.setUint32Property("gloda-id", 5555); + // set up a search term array that will give us the array of messages + // that gloda should index, as defined by this function: + let searchSession = Cc[ + "@mozilla.org/messenger/searchSession;1" + ].createInstance(Ci.nsIMsgSearchSession); + let searchTerms = []; + + searchSession.addScopeTerm( + Ci.nsMsgSearchScope.offlineMail, + localAccountUtils.inboxFolder + ); + let searchTerm = searchSession.createTerm(); + + // Create the following search term: + // (folderFlag & Mail && folderFlag != ImapBox) && + // msg property.gloda-id isEmpty + + searchTerm.beginsGrouping = true; + searchTerm.booleanAnd = true; + searchTerm.attrib = Ci.nsMsgSearchAttrib.FolderFlag; + searchTerm.op = Ci.nsMsgSearchOp.Is; + let value = searchTerm.value; + value.status = Ci.nsMsgFolderFlags.Mail; + value.attrib = Ci.nsMsgSearchAttrib.FolderFlag; + searchTerm.value = value; + searchTerms.push(searchTerm); + + searchTerm = searchSession.createTerm(); + searchTerm.booleanAnd = true; + searchTerm.attrib = Ci.nsMsgSearchAttrib.FolderFlag; + searchTerm.op = Ci.nsMsgSearchOp.Isnt; + value = searchTerm.value; + value.status = Ci.nsMsgFolderFlags.ImapBox; + value.attrib = Ci.nsMsgSearchAttrib.FolderFlag; + searchTerm.value = value; + searchTerm.endsGrouping = true; + searchTerms.push(searchTerm); + + searchTerm = searchSession.createTerm(); + searchTerm.booleanAnd = true; + searchTerm.attrib = Ci.nsMsgSearchAttrib.HdrProperty; + searchTerm.hdrProperty = "gloda-id"; + searchTerm.op = Ci.nsMsgSearchOp.IsEmpty; + value = searchTerm.value; + value.str = "gloda-id"; + value.attrib = Ci.nsMsgSearchAttrib.HdrProperty; + searchTerm.value = value; + searchTerms.push(searchTerm); + + let msgEnumerator = inboxDB.getFilterEnumerator(searchTerms); + let matchingHdrs = [...msgEnumerator]; + Assert.equal(kNumExpectedMatches, matchingHdrs.length); + Assert.equal(matchingHdrs[0].messageId, gMessages[1].messageId); + Assert.equal(matchingHdrs[1].messageId, gMessages[3].messageId); + + // try it backwards, with roller skates: + msgEnumerator = inboxDB.getFilterEnumerator(searchTerms, true); + matchingHdrs = [...msgEnumerator]; + Assert.equal(kNumExpectedMatches, matchingHdrs.length); + Assert.equal(matchingHdrs[0].messageId, gMessages[12].messageId); + Assert.equal(matchingHdrs[1].messageId, gMessages[11].messageId); + Assert.equal(matchingHdrs[9].messageId, gMessages[1].messageId); + + do_test_finished(); +} diff --git a/comm/mailnews/db/msgdb/test/unit/test_mailTelemetry.js b/comm/mailnews/db/msgdb/test/unit/test_mailTelemetry.js new file mode 100644 index 0000000000..c0bc034bad --- /dev/null +++ b/comm/mailnews/db/msgdb/test/unit/test_mailTelemetry.js @@ -0,0 +1,38 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/** + * Test telemetry related to mails read. + */ + +let { TelemetryTestUtils } = ChromeUtils.importESModule( + "resource://testing-common/TelemetryTestUtils.sys.mjs" +); + +/** + * Check that we're counting mails read. + */ +add_task(async function test_mails_read() { + Services.telemetry.clearScalars(); + + localAccountUtils.loadLocalMailAccount(); + + const NUM_MAILS = 5; + let headers = + "from: alice@t1.example.com\r\n" + + "to: bob@t2.example.net\r\n" + + "return-path: alice@t1.example.com\r\n" + + "Disposition-Notification-To: alice@t1.example.com\r\n"; + for (let i = 0; i < NUM_MAILS; i++) { + localAccountUtils.inboxFolder.addMessage( + "From \r\n" + headers + "\r\nhello\r\n" + ); + } + localAccountUtils.inboxFolder.markAllMessagesRead(null); + const scalars = TelemetryTestUtils.getProcessScalars("parent"); + Assert.equal( + scalars["tb.mails.read"], + NUM_MAILS, + "Count of mails read must be correct." + ); +}); diff --git a/comm/mailnews/db/msgdb/test/unit/test_maildb.js b/comm/mailnews/db/msgdb/test/unit/test_maildb.js new file mode 100644 index 0000000000..9b6bca9303 --- /dev/null +++ b/comm/mailnews/db/msgdb/test/unit/test_maildb.js @@ -0,0 +1,67 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* + * Test suite for msg database functions. + */ + +/* import-globals-from ../../../../test/resources/MessageGenerator.jsm */ +load("../../../../resources/MessageGenerator.jsm"); + +var dbService; +var gTestFolder; +var gCurTestNum = 0; +var kNumTestMessages = 10; + +var gTestArray = [ + function test_db_open() { + dbService = Cc["@mozilla.org/msgDatabase/msgDBService;1"].getService( + Ci.nsIMsgDBService + ); + // Get the root folder + let root = localAccountUtils.incomingServer.rootFolder; + root.createSubfolder("dbTest", null); + gTestFolder = root.getChildNamed("dbTest"); + let db = dbService.openFolderDB(gTestFolder, true); + Assert.notEqual(db, null); + db.dBFolderInfo.highWater = 10; + db.close(true); + db = dbService.openFolderDB(gTestFolder, true); + Assert.notEqual(db, null); + Assert.equal(db.dBFolderInfo.highWater, 10); + db.dBFolderInfo.onKeyAdded(15); + Assert.equal(db.dBFolderInfo.highWater, 15); + db.close(true); + db.forceClosed(); + db = null; + doTest(++gCurTestNum); + }, +]; + +function doTest(test) { + if (test <= gTestArray.length) { + dump("Doing test " + test + "\n"); + gCurTestNum = test; + + var testFn = gTestArray[test - 1]; + // Set a limit of 10 seconds; if the notifications haven't arrived by then there's a problem. + do_timeout(10000, function () { + if (gCurTestNum == test) { + do_throw( + "Notifications not received in 10000 ms for operation " + testFn.name + ); + } + }); + try { + testFn(); + } catch (ex) { + do_throw(ex); + } + } else { + do_test_finished(); // for the one in run_test() + } +} + +function run_test() { + localAccountUtils.loadLocalMailAccount(); + do_test_pending(); + doTest(1); +} diff --git a/comm/mailnews/db/msgdb/test/unit/test_propertyEnumerator.js b/comm/mailnews/db/msgdb/test/unit/test_propertyEnumerator.js new file mode 100644 index 0000000000..57fb2605bd --- /dev/null +++ b/comm/mailnews/db/msgdb/test/unit/test_propertyEnumerator.js @@ -0,0 +1,66 @@ +/* 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/. */ + +// tests properties in nsIMsgDBHdr; + +var { MailServices } = ChromeUtils.import( + "resource:///modules/MailServices.jsm" +); + +var gHdr; + +function run_test() { + localAccountUtils.loadLocalMailAccount(); + // Get a message into the local filestore. + // Function continue_test() continues the testing after the copy. + var bugmail1 = do_get_file("../../../../data/bugmail1"); + do_test_pending(); + MailServices.copy.copyFileMessage( + bugmail1, + localAccountUtils.inboxFolder, + null, + false, + 0, + "", + copyListener, + null + ); +} + +var copyListener = { + OnStartCopy() {}, + OnProgress(aProgress, aProgressMax) {}, + SetMessageKey(aKey) { + gHdr = localAccountUtils.inboxFolder.GetMessageHeader(aKey); + }, + SetMessageId(aMessageId) {}, + OnStopCopy(aStatus) { + continue_test(); + }, +}; + +function continue_test() { + // test some of the default properties + let properties = gHdr.properties; + Assert.ok(properties.includes("flags")); + Assert.ok(properties.includes("size")); + // this will be added in the next section, but does not exist yet + Assert.ok(!properties.includes("iamnew")); + + // add a new property, and make sure that it appears + gHdr.setStringProperty("iamnew", "somevalue"); + + properties = []; + for (let property of gHdr.properties) { + // dump("\nProperty 2 is " + property); + properties.push(property); + } + Assert.ok(properties.includes("flags")); + Assert.ok(properties.includes("size")); + Assert.ok(properties.includes("iamnew")); + Assert.ok(!properties.includes("idonotexist")); + + gHdr = null; + do_test_finished(); +} diff --git a/comm/mailnews/db/msgdb/test/unit/test_references_parsing.js b/comm/mailnews/db/msgdb/test/unit/test_references_parsing.js new file mode 100644 index 0000000000..fdfa76dd6d --- /dev/null +++ b/comm/mailnews/db/msgdb/test/unit/test_references_parsing.js @@ -0,0 +1,124 @@ +/* + * Test nsMsgHdr's In-Reply-To/References parsing logic. + */ + +var { MailServices } = ChromeUtils.import( + "resource:///modules/MailServices.jsm" +); + +var anyOldMessage = do_get_file("../../../../data/bugmail1"); + +var refsAndResults = [ + // an empty string is not a reference. + ["", []], + // super valid things + ["<abc@def>", ["abc@def"]], + [ + "<up@down> <left@right> <ying@yang>", + ["up@down", "left@right", "ying@yang"], + ], + // whitespace type things + [" ", []], + [" <left@space>", ["left@space"]], + ["<space@right> ", ["space@right"]], + [" <space@space> ", ["space@space"]], + ["\t<tab@tab>\t", ["tab@tab"]], + ["<a@b>\n\t<tab@newline.n>", ["a@b", "tab@newline.n"]], + ["<a@b>\r\t<tab@newline.r>", ["a@b", "tab@newline.r"]], + ["<a@b>\n\t<tab@newline.nr>", ["a@b", "tab@newline.nr"]], + [ + "<a@1>\n<a@2> <a@3>\t <a@4>\n <a@5>\r\t<a@6>\r\n <a@7>\r\n\t ", + ["a@1", "a@2", "a@3", "a@4", "a@5", "a@6", "a@7"], + ], + // be backwards compatible with old-school things that make some sense + ["i am a stupid message-id", ["i am a stupid message-id"]], + [" those were spaces!", ["those were spaces!"]], + // be backwards compatible with things that make no sense + [" seriously\n who does this?", ["seriously\n who does this?"]], + // handle things we used to be stupid about + ["<z@1a> was an awesome message!", ["z@1a"]], + [" <z@1b> was an awesomer message!", ["z@1b"]], + ["I can't get enough of <z@2a>", ["z@2a"]], + [" nor of I can enough get <z@2b> ", ["z@2b"]], + ["let's talk about <z@3a> shall we", ["z@3a"]], + ["and then let us speak of <z@3b> and its\n many points", ["z@3b"]], + // be backwards compatible with things that just seem malicious + [" 4 < 5", ["4 < 5"]], + [" 6 > 3", ["6 > 3"]], + [" look ma!\n newlines!", ["look ma!\n newlines!"]], +]; + +/** + * Parse the references in refsAndResults and ensure their references match + * the corresponding results. + * + * @param {nsIMsgDBHdr} aMsgHdr - A message header that you don't mind if we + * mess with. + */ +function test_references_header_parsing(aMsgHdr) { + var iCase, iResult, refString, results; + for (iCase = 0; iCase < refsAndResults.length; iCase++) { + refString = refsAndResults[iCase][0]; + results = refsAndResults[iCase][1]; + + dump("Setting references to: '" + refString + "'\n"); + aMsgHdr.setReferences(refString); + if (aMsgHdr.numReferences != results.length) { + dump("Length mismatch! Was expecting:\n"); + for (iResult = 0; iResult < results.length; iResult++) { + dump("'" + results[iResult] + "'\n"); + } + + dump("Got:\n"); + + for (iResult = 0; iResult < aMsgHdr.numReferences; iResult++) { + dump("'" + aMsgHdr.getStringReference(iResult) + "'\n"); + } + + Assert.equal(aMsgHdr.numReferences, results.length); + } + + for (iResult = 0; iResult < results.length; iResult++) { + Assert.equal(aMsgHdr.getStringReference(iResult), results[iResult]); + } + } + + do_test_finished(); +} + +/* + * This infrastructure down here exists just to get + * test_references_header_parsing its message header. + */ + +function run_test() { + localAccountUtils.loadLocalMailAccount(); + do_test_pending(); + MailServices.copy.copyFileMessage( + anyOldMessage, + localAccountUtils.inboxFolder, + null, + false, + 0, + "", + messageHeaderGetterListener, + null + ); + return true; +} + +var messageHeaderGetterListener = { + msgKey: null, + + OnStartCopy() {}, + OnProgress(aProgress, aProgressMax) {}, + GetMessageId(aMessageId) {}, + SetMessageKey(aKey) { + this.msgKey = aKey; + }, + OnStopCopy(aStatus) { + test_references_header_parsing( + localAccountUtils.inboxFolder.GetMessageHeader(this.msgKey) + ); + }, +}; diff --git a/comm/mailnews/db/msgdb/test/unit/xpcshell.ini b/comm/mailnews/db/msgdb/test/unit/xpcshell.ini new file mode 100644 index 0000000000..d32c984408 --- /dev/null +++ b/comm/mailnews/db/msgdb/test/unit/xpcshell.ini @@ -0,0 +1,10 @@ +[DEFAULT] +head = head_maildb.js +tail = + +[test_enumerator_cleanup.js] +[test_filter_enumerator.js] +[test_maildb.js] +[test_mailTelemetry.js] +[test_propertyEnumerator.js] +[test_references_parsing.js] |