Bug 773127 - Implement SettingsImportHelper. r=mconley

This commit is contained in:
Hiroyuki Ikezoe 2012-07-18 21:20:07 -04:00
Родитель f79aecf0dd
Коммит c4b9f64f89
3 изменённых файлов: 137 добавлений и 146 удалений

Просмотреть файл

@ -20,7 +20,7 @@ var gGenericImportHelper;
function GenericImportHelper(aModuleType, aModuleSearchString, aFile)
{
gGenericImportHelper = null;
if (aModuleType != "addressbook" && aModuleType != "mail")
if (["addressbook", "mail", "settings"].indexOf(aModuleType) == -1)
do_throw("Unexpected type passed to the GenericImportHelper constructor");
this.mModuleType = aModuleType;
this.mModuleSearchString = aModuleSearchString;
@ -32,6 +32,7 @@ function GenericImportHelper(aModuleType, aModuleSearchString, aFile)
GenericImportHelper.prototype =
{
interfaceType: Ci.nsIImportGeneric,
/**
* GenericImportHelper.beginImport
* Imports the given address book export or mail data and invoke
@ -71,7 +72,7 @@ GenericImportHelper.prototype =
if (importService.GetModuleName(this.mModuleType, i).indexOf(this.mModuleSearchString) != -1) {
return importService.GetModule(this.mModuleType, i)
.GetImportInterface(this.mModuleType)
.QueryInterface(Ci.nsIImportGeneric);
.QueryInterface(this.interfaceType);
}
}
return null; // it wasn't found
@ -345,6 +346,7 @@ function MailImportHelper(aFile, aModuleSearchString, aExpected)
MailImportHelper.prototype =
{
interfaceType: Ci.nsIImportGeneric,
_checkEqualFolder: function(expectedFolder, actualFolder) {
do_check_eq(expectedFolder.leafName, actualFolder.name);
let expectedSubFolderCount = 0;
@ -380,4 +382,135 @@ MailImportHelper.prototype =
MailImportHelper.prototype.__proto__ = GenericImportHelper.prototype;
/**
* SettingsImportHelper
* A helper for settings imports.
*
* @param aFile An instance of nsIFile to import, can be null.
* @param aModuleSearchString
* The string to search the module names for, such as
* "Outlook Express" or "Eudora mail" etc. etc.
* @param aExpected An array of object which has incomingServer, identity
* and smtpSever to compare with imported nsIMsgAccount.
*
* @constructor
* @class
*/
function SettingsImportHelper(aFile, aModuleSearchString, aExpected)
{
GenericImportHelper.call(this, "settings", aModuleSearchString, aFile);
this.mExpected = aExpected;
this.mInterface = this._findInterface();
do_check_neq(this.mInterface, null);
this.mFile = aFile;
}
SettingsImportHelper.prototype =
{
interfaceType: Ci.nsIImportSettings,
/**
* SettingsImportHelper.beginImport
* Imports settings from a specific file or auto-located if the file is null,
* and compare the import results with the expected array.
*/
beginImport: function() {
this._ensureNoAccounts();
if (this.mFile)
this.mInterface.SetLocation(this.mFile)
else
do_check_eq(true, this.mInterface.AutoLocate({}, {}));
do_check_eq(true, this.mInterface.Import({}));
this.checkResults();
},
_ensureNoAccounts: function() {
let accounts = MailServices.accounts.accounts;
for (let i = 0; i < accounts.Count(); i++) {
let account = accounts.QueryElementAt(i, Ci.nsIMsgAccount);
MailServices.accounts.removeAccount(account);
}
},
_checkSmtpServer: function(expected, actual) {
do_check_eq(expected.port, actual.port);
do_check_eq(expected.username, actual.username);
do_check_eq(expected.authMethod, actual.authMethod);
do_check_eq(expected.socketType, actual.socketType);
},
_checkIdentity: function(expected, actual) {
do_check_eq(expected.fullName, actual.fullName);
do_check_eq(expected.email, actual.email);
do_check_eq(expected.replyTo, actual.replyTo);
do_check_eq(expected.organization, actual.organization);
},
_checkPop3IncomingServer: function(expected, actual) {
do_check_eq(expected.leaveMessagesOnServer, actual.leaveMessagesOnServer);
do_check_eq(expected.deleteMailLeftOnServer, actual.deleteMailLeftOnServer);
do_check_eq(expected.deleteByAgeFromServer, actual.deleteByAgeFromServer);
do_check_eq(expected.numDaysToLeaveOnServer, actual.numDaysToLeaveOnServer);
},
_checkIncomingServer: function(expected, actual) {
do_check_eq(expected.type, actual.type);
do_check_eq(expected.port, actual.port);
do_check_eq(expected.username, actual.username);
do_check_eq(expected.isSecure, actual.isSecure);
do_check_eq(expected.hostName, actual.hostName);
do_check_eq(expected.prettyName, actual.prettyName);
do_check_eq(expected.authMethod, actual.authMethod);
do_check_eq(expected.socketType, actual.socketType);
do_check_eq(expected.doBiff, actual.doBiff);
do_check_eq(expected.biffMinutes, actual.biffMinutes);
if (expected.type == "pop3")
this._checkPop3IncomingServer(expected, actual.QueryInterface(Ci.nsIPop3IncomingServer));
},
_checkAccount: function(expected, actual) {
this._checkIncomingServer(expected.incomingServer, actual.incomingServer);
do_check_eq(1, actual.identities.Count());
let actualIdentity = actual.identities.QueryElementAt(0, Ci.nsIMsgIdentity);
this._checkIdentity(expected.identity, actualIdentity);
if (expected.incomingServer.type != "nntp") {
let actualSmtpServer = MailServices.smtp.getServerByKey(actualIdentity.smtpServerKey);
this._checkSmtpServer(expected.smtpServer, actualSmtpServer);
}
},
_isLocalMailAccount: function(account) {
return (account.incomingServer.type == "none" &&
account.incomingServer.username == "nobody" &&
account.incomingServer.hostName == "Local Folders");
},
_findExpectedAccount: function(account) {
return this.mExpected.filter(function(expectedAccount) {
return (expectedAccount.incomingServer.type == account.incomingServer.type &&
expectedAccount.incomingServer.username == account.incomingServer.username &&
expectedAccount.incomingServer.hostName == account.incomingServer.hostName);
});
},
checkResults: function() {
accounts = MailServices.accounts.accounts;
for (let i = 0; i < accounts.Count() - 1; i++) {
let actualAccount = accounts.QueryElementAt(i, Ci.nsIMsgAccount);
if (this._isLocalMailAccount(actualAccount))
continue;
let expectedAccounts = this._findExpectedAccount(actualAccount);
do_check_neq(null, expectedAccounts);
do_check_eq(1, expectedAccounts.length);
this._checkAccount(expectedAccounts[0], actualAccount);
}
}
}
SettingsImportHelper.prototype.__proto__ = GenericImportHelper.prototype;
do_load_manifest("resources/TestMailImporter.manifest");

Просмотреть файл

@ -186,75 +186,7 @@ let expectedNntpAccount = {
}
};
function check_smtp_server(expected, actual) {
do_check_eq(expected.port, actual.port);
do_check_eq(expected.username, actual.username);
do_check_eq(expected.authMethod, actual.authMethod);
do_check_eq(expected.socketType, actual.socketType);
}
function check_identity(expected, actual) {
do_check_eq(expected.fullName, actual.fullName);
do_check_eq(expected.email, actual.email);
do_check_eq(expected.replyTo, actual.replyTo);
do_check_eq(expected.organization, actual.organization);
}
function check_pop3_incoming_server(expected, actual) {
do_check_eq(expected.leaveMessagesOnServer, actual.leaveMessagesOnServer);
do_check_eq(expected.deleteMailLeftOnServer, actual.deleteMailLeftOnServer);
do_check_eq(expected.deleteByAgeFromServer, actual.deleteByAgeFromServer);
do_check_eq(expected.numDaysToLeaveOnServer, actual.numDaysToLeaveOnServer);
}
function check_incoming_server(expected, actual) {
do_check_eq(expected.type, actual.type);
do_check_eq(expected.port, actual.port);
do_check_eq(expected.username, actual.username);
do_check_eq(expected.isSecure, actual.isSecure);
do_check_eq(expected.authMethod, actual.authMethod);
do_check_eq(expected.socketType, actual.socketType);
do_check_eq(expected.doBiff, actual.doBiff);
do_check_eq(expected.biffMinutes, actual.biffMinutes);
if (expected.type == "pop3")
check_pop3_incoming_server(expected, actual.QueryInterface(Ci.nsIPop3IncomingServer));
}
function check_account(expected, actual) {
check_incoming_server(expected.incomingServer, actual.incomingServer);
do_check_eq(1, actual.identities.Count());
let actualIdentity = actual.identities.QueryElementAt(0, Ci.nsIMsgIdentity);
check_identity(expected.identity, actualIdentity);
if (expected.incomingServer.type != "nntp") {
let actualSmtpServer = MailServices.smtp.getServerByKey(actualIdentity.smtpServerKey);
check_smtp_server(expected.smtpServer, actualSmtpServer);
}
}
function get_interface() {
let importService = Cc["@mozilla.org/import/import-service;1"]
.getService(Ci.nsIImportService);
let count = importService.GetModuleCount("settings");
let settingsInterface;
for (let i = 0; i < count; i++) {
if (importService.GetModuleName("settings", i) == "Outlook Express") {
return importService.GetModule("settings", i)
.GetImportInterface("settings")
.QueryInterface(Ci.nsIImportSettings);
}
}
}
function teardown() {
let accounts = MailServices.accounts.accounts;
for (let i = 0; i < accounts.Count(); i++) {
let account = accounts.QueryElementAt(i, Ci.nsIMsgAccount);
MailServices.accounts.removeAccount(account);
}
let smtpServers = MailServices.smtp.smtpServers;
while (smtpServers.hasMoreElements()) {
@ -265,20 +197,10 @@ function teardown() {
teardown_mock_registry();
}
function _import() {
let settings = get_interface();
do_check_eq(true, settings.AutoLocate({}, {}));
do_check_eq(true, settings.Import({}));
}
function _test(registry, expectedAccount) {
try {
setup_mock_registry(registry);
_import();
let accounts = MailServices.accounts.accounts;
let lastIndex = accounts.Count() - 1;
let actualAccount = accounts.QueryElementAt(lastIndex, Ci.nsIMsgAccount);
check_account(expectedAccount, actualAccount);
new SettingsImportHelper(null, "Outlook Express", [expectedAccount]).beginImport();
} catch(e) {
teardown();
do_throw(e);

Просмотреть файл

@ -97,61 +97,7 @@ let expectedImapAccount = {
}
};
function check_smtp_server(expected, actual) {
do_check_eq(expected.username, actual.username);
}
function check_identity(expected, actual) {
do_check_eq(expected.fullName, actual.fullName);
do_check_eq(expected.email, actual.email);
do_check_eq(expected.replyTo, actual.replyTo);
do_check_eq(expected.organization, actual.organization);
}
function check_pop3_incoming_server(expected, actual) {
do_check_eq(expected.leaveMessagesOnServer, actual.leaveMessagesOnServer);
}
function check_incoming_server(expected, actual) {
do_check_eq(expected.type, actual.type);
do_check_eq(expected.username, actual.username);
if (expected.type == "pop3")
check_pop3_incoming_server(expected, actual.QueryInterface(Ci.nsIPop3IncomingServer));
}
function check_account(expected, actual) {
check_incoming_server(expected.incomingServer, actual.incomingServer);
do_check_eq(1, actual.identities.Count());
let actualIdentity = actual.identities.QueryElementAt(0, Ci.nsIMsgIdentity);
check_identity(expected.identity, actualIdentity);
let actualSmtpServer = MailServices.smtp.getServerByKey(actualIdentity.smtpServerKey);
check_smtp_server(expected.smtpServer, actualSmtpServer);
}
function get_interface() {
let importService = Cc["@mozilla.org/import/import-service;1"]
.getService(Ci.nsIImportService);
let count = importService.GetModuleCount("settings");
let settingsInterface;
for (let i = 0; i < count; i++) {
if (importService.GetModuleName("settings", i) == "Outlook") {
return importService.GetModule("settings", i)
.GetImportInterface("settings")
.QueryInterface(Ci.nsIImportSettings);
}
}
}
function teardown() {
let accounts = MailServices.accounts.accounts;
for (let i = 0; i < accounts.Count(); i++) {
let account = accounts.QueryElementAt(i, Ci.nsIMsgAccount);
MailServices.accounts.removeAccount(account);
}
let smtpServers = MailServices.smtp.smtpServers;
while (smtpServers.hasMoreElements()) {
@ -162,20 +108,10 @@ function teardown() {
teardown_mock_registry();
}
function _import() {
let settings = get_interface();
do_check_eq(true, settings.AutoLocate({}, {}));
do_check_eq(true, settings.Import({}));
}
function _test(registry, expectedAccount) {
try {
setup_mock_registry(registry);
_import();
let accounts = MailServices.accounts.accounts;
let lastIndex = accounts.Count() - 1;
let actualAccount = accounts.QueryElementAt(lastIndex, Ci.nsIMsgAccount);
check_account(expectedAccount, actualAccount);
new SettingsImportHelper(null, "Outlook", [expectedAccount]).beginImport();
} catch(e) {
teardown();
do_throw(e);