Bug 1492399 - honor preserveFieldValues when generating billing address options. r=MattN
authorSam Foster <sfoster@mozilla.com>
Wed, 07 Nov 2018 18:41:37 +0000
changeset 501336 12afa29e9c8cde597314f072c6f0c1f81d681b40
parent 501335 7d6a30e3e400cf5420169b97d7153891ff9b9c16
child 501460 5836a60614764631436bf5030c5baa34c676c7a2
child 501461 ade95bb476e517052867a5de93b38550f318815c
push id10290
push userffxbld-merge
push dateMon, 03 Dec 2018 16:23:23 +0000
treeherdermozilla-beta@700bed2445e6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN
bugs1492399
milestone65.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1492399 - honor preserveFieldValues when generating billing address options. r=MattN Differential Revision: https://phabricator.services.mozilla.com/D11129
browser/components/payments/test/browser/browser_card_edit.js
browser/extensions/formautofill/content/autofillEditForms.js
--- a/browser/components/payments/test/browser/browser_card_edit.js
+++ b/browser/components/payments/test/browser/browser_card_edit.js
@@ -397,17 +397,17 @@ add_task(async function test_opt_out_per
     expectAddressPersist: true,
     prefilledGuids,
   });
   Services.prefs.clearUserPref(SAVE_CREDITCARD_DEFAULT_PREF);
 });
 
 add_task(async function test_edit_link() {
   // add an address and card linked to this address
-  let prefilledGuids = await setup([PTU.Addresses.TimBL]);
+  let prefilledGuids = await setup([PTU.Addresses.TimBL, PTU.Addresses.TimBL2]);
   {
     let card = Object.assign({}, PTU.BasicCards.JohnDoe,
                              { billingAddressGUID: prefilledGuids.address1GUID });
     await addCardRecord(card);
   }
 
   const args = {
     methodData: [PTU.MethodData.basicCard],
@@ -427,17 +427,17 @@ add_task(async function test_edit_link()
       editLink.click();
 
       let state = await PTU.DialogContentUtils.waitForState(content, (state) => {
         return state.page.id == "basic-card-page" && state["basic-card-page"].guid;
       }, "Check edit page state");
 
       state = await PTU.DialogContentUtils.waitForState(content, (state) => {
         return Object.keys(state.savedBasicCards).length == 1 &&
-               Object.keys(state.savedAddresses).length == 1;
+               Object.keys(state.savedAddresses).length == 2;
       }, "Check card and address present at beginning of test");
 
       let title = content.document.querySelector("basic-card-form h2");
       is(title.textContent, "Edit Credit Card", "Edit title should be set");
 
       let saveButton = content.document.querySelector("basic-card-form .save-button");
       is(saveButton.textContent, "Update", "Save button has the correct label");
 
@@ -455,22 +455,22 @@ add_task(async function test_edit_link()
       }
       ok(content.document.getElementById("cc-number").disabled,
          "cc-number field should be disabled");
 
       let billingAddressPicker = Cu.waiveXrays(
         content.document.querySelector("basic-card-form billing-address-picker"));
 
       let initialSelectedAddressGuid = billingAddressPicker.dropdown.value;
-      is(billingAddressPicker.options.length, 2,
-         "Two options should exist in the billingAddressPicker");
+      is(billingAddressPicker.options.length, 3,
+         "Three options should exist in the billingAddressPicker");
       is(initialSelectedAddressGuid, prefilledGuids.address1GUID,
          "The prefilled billing address should be selected by default");
 
-      info("Test clicking 'add' on the empty option first");
+      info("Test clicking 'add' with the empty option first");
       billingAddressPicker.dropdown.popupBox.focus();
       content.fillField(billingAddressPicker.dropdown.popupBox, "");
 
       let addressEditLink = content.document.querySelector(".billingAddressRow .edit-link");
       ok(addressEditLink && !content.isVisible(addressEditLink),
          "The edit link is hidden when empty option is selected");
 
       let addressAddLink = content.document.querySelector(".billingAddressRow .add-link");
@@ -482,21 +482,41 @@ add_task(async function test_edit_link()
       let addressTitle = content.document.querySelector("address-form h2");
       is(addressTitle.textContent, "Add Billing Address",
          "Address on add address page should be correct");
 
       let addressBackButton = content.document.querySelector("address-form .back-button");
       addressBackButton.click();
       state = await PTU.DialogContentUtils.waitForState(content, (state) => {
         return state.page.id == "basic-card-page" && state["basic-card-page"].guid &&
-               Object.keys(state.savedAddresses).length == 1;
+               Object.keys(state.savedAddresses).length == 2;
       }, "Check we're back at basic-card page with no state changed after adding");
 
+      info("Inspect a different address and ensure it remains selected when we go back");
+      content.fillField(billingAddressPicker.dropdown.popupBox, prefilledGuids.address2GUID);
+
+      addressEditLink.click();
+      state = await PTU.DialogContentUtils.waitForState(content, (state) => {
+        return state.page.id == "address-page" && state["address-page"].guid;
+      }, "Clicking edit button with selected option will go to 'edit' page");
+
+      let countryPicker = content.document.querySelector("address-form #country");
+      is(countryPicker.value, PTU.Addresses.TimBL2.country, "The country value matches");
+
+      addressBackButton.click();
+      state = await PTU.DialogContentUtils.waitForState(content, (state) => {
+        return state.page.id == "basic-card-page" && state["basic-card-page"].guid &&
+               Object.keys(state.savedAddresses).length == 2;
+      }, "Check we're back at basic-card page with no state changed after editing");
+
+      is(billingAddressPicker.dropdown.value, prefilledGuids.address2GUID,
+         "The selected billing address is correct");
+
       info("Go back to previously selected option before clicking 'edit' now");
-      billingAddressPicker.dropdown.value = initialSelectedAddressGuid;
+      content.fillField(billingAddressPicker.dropdown.popupBox, initialSelectedAddressGuid);
 
       let selectedOption = billingAddressPicker.dropdown.selectedOption;
       ok(selectedOption && selectedOption.value, "select should have a selected option value");
 
       addressEditLink.click();
       state = await PTU.DialogContentUtils.waitForState(content, (state) => {
         return state.page.id == "address-page" && state["address-page"].guid;
       }, "Check address page state (editing)");
@@ -506,17 +526,17 @@ add_task(async function test_edit_link()
 
       state = await PTU.DialogContentUtils.waitForState(content, (state) => {
         return Object.keys(state.savedBasicCards).length == 1;
       }, "Check card was not added again when clicking the 'edit' address button");
 
       addressBackButton.click();
       state = await PTU.DialogContentUtils.waitForState(content, (state) => {
         return state.page.id == "basic-card-page" && state["basic-card-page"].guid &&
-               Object.keys(state.savedAddresses).length == 1;
+               Object.keys(state.savedAddresses).length == 2;
       }, "Check we're back at basic-card page with no state changed after editing");
 
       for (let [key, val] of Object.entries(card)) {
         let field = content.document.getElementById(key);
         is(field.value, val, "Field should still have previous value entered");
       }
 
       selectedOption = billingAddressPicker.dropdown.selectedOption;
@@ -537,18 +557,18 @@ add_task(async function test_edit_link()
         EventUtils.sendKey("BACK_SPACE", content.window);
         EventUtils.sendString("7", content.window);
         ok(!field.disabled, `Field #${key} shouldn't be disabled`);
       }
 
       content.document.querySelector("address-form button.save-button").click();
       state = await PTU.DialogContentUtils.waitForState(content, (state) => {
         return state.page.id == "basic-card-page" && state["basic-card-page"].guid &&
-               Object.keys(state.savedAddresses).length == 1;
-      }, "Check still only one address and we're back on basic-card page");
+               Object.keys(state.savedAddresses).length == 2;
+      }, "Check still only 2 addresses and we're back on basic-card page");
 
       is(Object.values(state.savedAddresses)[0].tel, PTU.Addresses.TimBL.tel.slice(0, -1) + "7",
          "Check that address was edited and saved");
 
       content.document.querySelector("basic-card-form button.save-button").click();
 
       state = await PTU.DialogContentUtils.waitForState(content, (state) => {
         let cards = Object.entries(state.savedBasicCards);
--- a/browser/extensions/formautofill/content/autofillEditForms.js
+++ b/browser/extensions/formautofill/content/autofillEditForms.js
@@ -349,17 +349,17 @@ class EditCreditCard extends EditAutofil
     this.loadRecord(record, addresses);
     this.attachEventListeners();
   }
 
   loadRecord(record, addresses, preserveFieldValues) {
     // _record must be updated before generateYears and generateBillingAddressOptions are called.
     this._record = record;
     this._addresses = addresses;
-    this.generateBillingAddressOptions();
+    this.generateBillingAddressOptions(preserveFieldValues);
     if (!preserveFieldValues) {
       // Re-populating the networks will reset the selected option.
       this.populateNetworks();
       // Re-generating the years will reset the selected option.
       this.generateYears();
       super.loadRecord(record);
 
       // Resetting the form in the super.loadRecord won't clear custom validity
@@ -407,18 +407,23 @@ class EditCreditCard extends EditAutofil
       let option = new Option();
       option.value = id;
       option.dataset.localization = "cardNetwork." + id;
       frag.appendChild(option);
     }
     this._elements.ccType.appendChild(frag);
   }
 
-  generateBillingAddressOptions() {
-    let billingAddressGUID = this._record && this._record.billingAddressGUID;
+  generateBillingAddressOptions(preserveFieldValues) {
+    let billingAddressGUID;
+    if (preserveFieldValues && this._elements.billingAddress.value) {
+      billingAddressGUID = this._elements.billingAddress.value;
+    } else if (this._record) {
+      billingAddressGUID = this._record.billingAddressGUID;
+    }
 
     this._elements.billingAddress.textContent = "";
 
     this._elements.billingAddress.appendChild(new Option("", ""));
 
     let hasAddresses = false;
     for (let [guid, address] of Object.entries(this._addresses)) {
       hasAddresses = true;