Bug 1393374 - Part 1. Introduce credit card helper functions in formautofill mochitest. r=lchang

MozReview-Commit-ID: Ir1usKRqx4R

--HG--
extra : rebase_source : d05b4acf288d6e3729ce23a9679ca4f3710bae66
This commit is contained in:
Ray Lin 2017-08-31 11:03:43 +08:00
Родитель 4d49b6baed
Коммит 0d1cc5ff15
4 изменённых файлов: 179 добавлений и 102 удалений

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

@ -37,7 +37,7 @@ function clickOnElement(selector) {
SimpleTest.executeSoon(() => element.click());
}
async function onAddressChanged(type) {
async function onStorageChanged(type) {
return new Promise(resolve => {
formFillChromeScript.addMessageListener("formautofill-storage-changed", function onChanged(data) {
formFillChromeScript.removeMessageListener("formautofill-storage-changed", onChanged);
@ -58,60 +58,58 @@ function checkMenuEntries(expectedValues, isFormAutofillResult = true) {
}
}
async function addAddress(address) {
function invokeAsyncChromeTask(message, response, payload = {}) {
return new Promise(resolve => {
formFillChromeScript.sendAsyncMessage("FormAutofillTest:AddAddress", {address});
formFillChromeScript.addMessageListener("FormAutofillTest:AddressAdded", function onAdded(data) {
formFillChromeScript.removeMessageListener("FormAutofillTest:AddressAdded", onAdded);
formFillChromeScript.sendAsyncMessage(message, payload);
formFillChromeScript.addMessageListener(response, function onReceived(data) {
formFillChromeScript.removeMessageListener(response, onReceived);
SimpleTest.requestFlakyTimeout("Ensure ProfileAutocomplete is registered");
setTimeout(resolve, 500);
resolve(data);
});
});
}
async function addAddress(address) {
await invokeAsyncChromeTask("FormAutofillTest:AddAddress", "FormAutofillTest:AddressAdded", {address});
await sleep();
}
async function removeAddress(guid) {
return new Promise(resolve => {
formFillChromeScript.sendAsyncMessage("FormAutofillTest:RemoveAddress", {guid});
formFillChromeScript.addMessageListener("FormAutofillTest:AddressRemoved", function onDeleted(data) {
formFillChromeScript.removeMessageListener("FormAutofillTest:AddressRemoved", onDeleted);
resolve();
});
});
return invokeAsyncChromeTask("FormAutofillTest:RemoveAddress", "FormAutofillTest:AddressRemoved", {guid});
}
async function updateAddress(guid, address) {
return new Promise(resolve => {
formFillChromeScript.sendAsyncMessage("FormAutofillTest:UpdateAddress", {address, guid});
formFillChromeScript.addMessageListener("FormAutofillTest:AddressUpdated", function onUpdated(data) {
formFillChromeScript.removeMessageListener("FormAutofillTest:AddressUpdated", onUpdated);
resolve();
});
});
return invokeAsyncChromeTask("FormAutofillTest:UpdateAddress", "FormAutofillTest:AddressUpdated", {address, guid});
}
async function checkAddresses(expectedAddresses) {
return new Promise(resolve => {
formFillChromeScript.sendAsyncMessage("FormAutofillTest:CheckAddresses", {expectedAddresses});
formFillChromeScript.addMessageListener("FormAutofillTest:areAddressesMatching", function onChecked(data) {
formFillChromeScript.removeMessageListener("FormAutofillTest:areAddressesMatching", onChecked);
resolve(data);
});
});
return invokeAsyncChromeTask("FormAutofillTest:CheckAddresses", "FormAutofillTest:areAddressesMatching", {expectedAddresses});
}
async function cleanUpAddress() {
return new Promise(resolve => {
formFillChromeScript.sendAsyncMessage("FormAutofillTest:CleanUpAddress", {});
formFillChromeScript.addMessageListener("FormAutofillTest:AddressCleanedUp", function onCleanedUp(data) {
formFillChromeScript.removeMessageListener("FormAutofillTest:AddressCleanedUp", onCleanedUp);
async function cleanUpAddresses() {
return invokeAsyncChromeTask("FormAutofillTest:CleanUpAddresses", "FormAutofillTest:AddressesCleanedUp");
}
resolve(data);
});
});
async function addCreditCard(creditcard) {
await invokeAsyncChromeTask("FormAutofillTest:AddCreditCard", "FormAutofillTest:CreditCardAdded", {creditcard});
await sleep();
}
async function removeCreditCard(guid) {
return invokeAsyncChromeTask("FormAutofillTest:RemoveCreditCard", "FormAutofillTest:CreditCardRemoved", {guid});
}
async function checkCreditCards(expectedCreditCards) {
return invokeAsyncChromeTask("FormAutofillTest:CheckCreditCards", "FormAutofillTest:areCreditCardsMatching", {expectedCreditCards});
}
async function cleanUpCreditCards() {
return invokeAsyncChromeTask("FormAutofillTest:CleanUpCreditCards", "FormAutofillTest:CreditCardsCleanedUp");
}
async function cleanUpStorage() {
await cleanUpAddresses();
await cleanUpCreditCards();
}
// Utils for registerPopupShownListener(in satchel_common.js) that handles dropdown popup

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

@ -7,113 +7,192 @@
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://formautofill/FormAutofillUtils.jsm");
let {profileStorage} = Cu.import("resource://formautofill/ProfileStorage.jsm", {});
const {ADDRESSES_COLLECTION_NAME, CREDITCARDS_COLLECTION_NAME} = FormAutofillUtils;
var ParentUtils = {
cleanUpAddress() {
Services.cpmm.addMessageListener("FormAutofill:Records", function getResult(result) {
Services.cpmm.removeMessageListener("FormAutofill:Records", getResult);
let addresses = result.data;
Services.cpmm.sendAsyncMessage("FormAutofill:RemoveAddresses",
{guids: addresses.map(address => address.guid)});
let count = addresses.length;
Services.obs.addObserver(function observer(subject, topic, data) {
if (!--count) {
Services.obs.removeObserver(observer, topic);
sendAsyncMessage("FormAutofillTest:AddressCleanedUp");
}
}, "formautofill-storage-changed");
async _getRecords(collectionName) {
return new Promise(resolve => {
Services.cpmm.addMessageListener("FormAutofill:Records", function getResult({data}) {
Services.cpmm.removeMessageListener("FormAutofill:Records", getResult);
resolve(data);
});
Services.cpmm.sendAsyncMessage("FormAutofill:GetRecords", {searchString: "", collectionName});
});
Services.cpmm.sendAsyncMessage("FormAutofill:GetRecords", {searchString: "", collectionName: "addresses"});
},
updateAddress(type, chromeMsg, msgData, contentMsg) {
Services.cpmm.sendAsyncMessage(chromeMsg, msgData);
Services.obs.addObserver(function observer(subject, topic, data) {
if (data != type) {
return;
async _storageChangeObserved({topic = "formautofill-storage-changed", type, times = 1}) {
let count = times;
return new Promise(resolve => {
Services.obs.addObserver(function observer(subject, obsTopic, data) {
if (type && data != type || !!--count) {
return;
}
Services.obs.removeObserver(observer, obsTopic);
resolve();
}, topic);
});
},
async _operateRecord(collectionName, type, msgData, contentMsg) {
let times, topic;
if (collectionName == ADDRESSES_COLLECTION_NAME) {
switch (type) {
case "add": {
Services.cpmm.sendAsyncMessage("FormAutofill:SaveAddress", msgData);
break;
}
case "update": {
Services.cpmm.sendAsyncMessage("FormAutofill:SaveAddress", msgData);
break;
}
case "remove": {
times = msgData.guids.length;
Services.cpmm.sendAsyncMessage("FormAutofill:RemoveAddresses", msgData);
break;
}
}
} else {
switch (type) {
case "add": {
const msgDataCloned = Object.assign({}, msgData);
Services.obs.removeObserver(observer, topic);
sendAsyncMessage(contentMsg);
}, "formautofill-storage-changed");
Services.cpmm.sendAsyncMessage("FormAutofill:SaveCreditCard", msgDataCloned);
break;
}
case "remove": {
times = msgData.guids.length;
Services.cpmm.sendAsyncMessage("FormAutofill:RemoveCreditCards", msgData);
break;
}
}
}
await this._storageChangeObserved({type, times, topic});
sendAsyncMessage(contentMsg);
},
observe(subject, topic, data) {
assert.ok(topic === "formautofill-storage-changed");
sendAsyncMessage("formautofill-storage-changed", {subject: null, topic, data});
async operateAddress(type, msgData, contentMsg) {
await this._operateRecord(ADDRESSES_COLLECTION_NAME, ...arguments);
},
cleanup() {
async operateCreditCard(type, msgData, contentMsg) {
await this._operateRecord(CREDITCARDS_COLLECTION_NAME, ...arguments);
},
async cleanUpAddresses() {
const guids = (await this._getRecords(ADDRESSES_COLLECTION_NAME)).map(record => record.guid);
await this.operateAddress("remove", {guids}, "FormAutofillTest:AddressesCleanedUp");
},
async cleanUpCreditCards() {
const guids = (await this._getRecords(CREDITCARDS_COLLECTION_NAME)).map(record => record.guid);
await this.operateCreditCard("remove", {guids}, "FormAutofillTest:CreditCardsCleanedUp");
},
async cleanup() {
Services.obs.removeObserver(this, "formautofill-storage-changed");
this.cleanUpAddress();
await this.cleanUpAddresses();
await this.cleanUpCreditCards();
},
areAddressesMatching(addressA, addressB) {
for (let field of profileStorage.addresses.VALID_FIELDS) {
if (addressA[field] !== addressB[field]) {
_areRecordsMatching(recordA, recordB, collectionName) {
for (let field of profileStorage[collectionName].VALID_FIELDS) {
if (recordA[field] !== recordB[field]) {
return false;
}
}
// Check the internal field if both addresses have valid value.
for (let field of profileStorage.INTERNAL_FIELDS) {
if (field in addressA && field in addressB && (addressA[field] !== addressB[field])) {
if (field in recordA && field in recordB && (recordA[field] !== recordB[field])) {
return false;
}
}
return true;
},
checkAddresses({expectedAddresses}) {
Services.cpmm.addMessageListener("FormAutofill:Records", function getResult(result) {
Services.cpmm.removeMessageListener("FormAutofill:Records", getResult);
let addresses = result.data;
if (addresses.length !== expectedAddresses.length) {
sendAsyncMessage("FormAutofillTest:areAddressesMatching", false);
return;
async _checkRecords(collectionName, expectedRecords) {
const records = await this._getRecords(collectionName);
if (records.length !== expectedRecords.length) {
return false;
}
for (let record of records) {
let matching = expectedRecords.some(expectedRecord => {
return ParentUtils._areRecordsMatching(record, expectedRecord, collectionName);
});
if (!matching) {
return false;
}
}
for (let address of addresses) {
let matching = expectedAddresses.some((expectedAddress) => {
return ParentUtils.areAddressesMatching(address, expectedAddress);
});
return true;
},
if (!matching) {
sendAsyncMessage("FormAutofillTest:areAddressesMatching", false);
return;
}
}
async checkAddresses({expectedAddresses}) {
const areMatched = await this._checkRecords(ADDRESSES_COLLECTION_NAME, expectedAddresses);
sendAsyncMessage("FormAutofillTest:areAddressesMatching", true);
});
sendAsyncMessage("FormAutofillTest:areAddressesMatching", areMatched);
},
Services.cpmm.sendAsyncMessage("FormAutofill:GetRecords", {searchString: "", collectionName: "addresses"});
async checkCreditCards({expectedCreditCards}) {
const areMatched = await this._checkRecords(CREDITCARDS_COLLECTION_NAME, expectedCreditCards);
sendAsyncMessage("FormAutofillTest:areCreditCardsMatching", areMatched);
},
observe(subject, topic, data) {
assert.ok(topic === "formautofill-storage-changed");
sendAsyncMessage("formautofill-storage-changed", {subject: null, topic, data});
},
};
Services.obs.addObserver(ParentUtils, "formautofill-storage-changed");
addMessageListener("FormAutofillTest:AddAddress", (msg) => {
ParentUtils.updateAddress("add", "FormAutofill:SaveAddress", msg, "FormAutofillTest:AddressAdded");
ParentUtils.operateAddress("add", msg, "FormAutofillTest:AddressAdded");
});
addMessageListener("FormAutofillTest:RemoveAddress", (msg) => {
ParentUtils.updateAddress("remove", "FormAutofill:RemoveAddress", msg, "FormAutofillTest:AddressRemoved");
ParentUtils.operateAddress("remove", msg, "FormAutofillTest:AddressRemoved");
});
addMessageListener("FormAutofillTest:UpdateAddress", (msg) => {
ParentUtils.updateAddress("update", "FormAutofill:SaveAddress", msg, "FormAutofillTest:AddressUpdated");
ParentUtils.operateAddress("update", msg, "FormAutofillTest:AddressUpdated");
});
addMessageListener("FormAutofillTest:CheckAddresses", (msg) => {
ParentUtils.checkAddresses(msg);
});
addMessageListener("FormAutofillTest:CleanUpAddress", (msg) => {
ParentUtils.cleanUpAddress();
addMessageListener("FormAutofillTest:CleanUpAddresses", (msg) => {
ParentUtils.cleanUpAddresses();
});
addMessageListener("FormAutofillTest:AddCreditCard", (msg) => {
ParentUtils.operateCreditCard("add", msg, "FormAutofillTest:CreditCardAdded");
});
addMessageListener("FormAutofillTest:RemoveCreditCard", (msg) => {
ParentUtils.operateCreditCard("remove", msg, "FormAutofillTest:CreditCardRemoved");
});
addMessageListener("FormAutofillTest:CheckCreditCards", (msg) => {
ParentUtils.checkCreditCards(msg);
});
addMessageListener("FormAutofillTest:CleanUpCreditCards", (msg) => {
ParentUtils.cleanUpCreditCards();
});
addMessageListener("cleanup", () => {

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

@ -117,7 +117,7 @@ add_task(async function all_saved_fields_less_than_threshold() {
await expectPopup();
checkMenuEntries(["foo@mozilla.com"], false);
await cleanUpAddress();
await cleanUpAddresses();
});
// Form with both history and address storage.

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

@ -45,7 +45,7 @@ add_task(async function check_storage_after_form_submitted() {
clickOnElement("input[type=submit]");
let expectedAddresses = TEST_ADDRESSES.slice(0, 1);
await onAddressChanged("add");
await onStorageChanged("add");
// Check if timesUsed is set correctly
expectedAddresses[0].timesUsed = 1;
let matching = await checkAddresses(expectedAddresses);
@ -67,7 +67,7 @@ add_task(async function check_storage_after_another_address_submitted() {
addressesInMenu.push(TEST_ADDRESSES[0]);
// let expectedAddresses = TEST_ADDRESSES.slice(0);
await onAddressChanged("add");
await onStorageChanged("add");
let matching = await checkAddresses(TEST_ADDRESSES);
ok(matching, "New address saved as expected");
@ -93,7 +93,7 @@ add_task(async function new_address_submitted_and_merged() {
let expectedAddresses = TEST_ADDRESSES.slice(0);
// Check if timesUsed is set correctly
expectedAddresses[0].timesUsed = 2;
await onAddressChanged("merge");
await onStorageChanged("merge");
let matching = await checkAddresses(expectedAddresses);
ok(matching, "Address merged as expected");
delete expectedAddresses[0].timesUsed;
@ -114,7 +114,7 @@ add_task(async function check_storage_after_form_submitted() {
clickOnElement("input[type=submit]");
let expectedAddresses = TEST_ADDRESSES.slice(0);
await onAddressChanged("merge");
await onStorageChanged("merge");
let matching = await checkAddresses(expectedAddresses);
ok(matching, "Updated address merged as expected");
});