Bug 1370474 - (Part 2) editProfile and manageProfiles dialogs close on ESC key press. r=lchang draft
authorScott Wu <scottcwwu@gmail.com>
Thu, 22 Jun 2017 18:28:56 +0800
changeset 598925 95ab8954faab403efbda1e235d2047f5741136dc
parent 598924 c84c5723d3e715e52c3feb7b9a40c5cad7e3c082
child 634614 184169b471b4bdd8b339c7fdfd665abfe02dc50e
push id65355
push userbmo:scwwu@mozilla.com
push dateThu, 22 Jun 2017 10:59:31 +0000
reviewerslchang
bugs1370474
milestone56.0a1
Bug 1370474 - (Part 2) editProfile and manageProfiles dialogs close on ESC key press. r=lchang MozReview-Commit-ID: DDzu19dOtyq
browser/extensions/formautofill/content/editProfile.js
browser/extensions/formautofill/content/manageProfiles.js
browser/extensions/formautofill/test/browser/browser_editProfileDialog.js
browser/extensions/formautofill/test/browser/browser_manageProfilesDialog.js
--- a/browser/extensions/formautofill/content/editProfile.js
+++ b/browser/extensions/formautofill/content/editProfile.js
@@ -94,16 +94,20 @@ EditDialog.prototype = {
           this._elements.save.removeAttribute("disabled");
         }
         break;
       }
       case "unload": {
         this.uninit();
         break;
       }
+      case "keypress": {
+        this.handleKeyPress(event);
+        break;
+      }
     }
   },
 
   /**
    * Handle click events
    *
    * @param  {DOMEvent} event
    */
@@ -122,25 +126,38 @@ EditDialog.prototype = {
           address: this.buildAddressObject(),
         });
       }
       window.close();
     }
   },
 
   /**
+   * Handle key press events
+   *
+   * @param  {DOMEvent} event
+   */
+  handleKeyPress(event) {
+    if (event.keyCode == KeyEvent.DOM_VK_ESCAPE) {
+      window.close();
+    }
+  },
+
+  /**
    * Attach event listener
    */
   attachEventListeners() {
+    window.addEventListener("keypress", this);
     this._elements.controlsContainer.addEventListener("click", this);
     document.addEventListener("input", this);
   },
 
   /**
    * Remove event listener
    */
   detachEventListeners() {
+    window.removeEventListener("keypress", this);
     this._elements.controlsContainer.removeEventListener("click", this);
     document.removeEventListener("input", this);
   },
 };
 
 new EditDialog();
--- a/browser/extensions/formautofill/content/manageProfiles.js
+++ b/browser/extensions/formautofill/content/manageProfiles.js
@@ -212,16 +212,20 @@ ManageProfileDialog.prototype = {
       case "change": {
         this.updateButtonsStates(this._selectedOptions.length);
         break;
       }
       case "unload": {
         this.uninit();
         break;
       }
+      case "keypress": {
+        this.handleKeyPress(event);
+        break;
+      }
     }
   },
 
   /**
    * Handle click events
    *
    * @param  {DOMEvent} event
    */
@@ -230,16 +234,27 @@ ManageProfileDialog.prototype = {
       this.removeAddresses(this._selectedOptions.map(option => option.value));
     } else if (event.target == this._elements.add) {
       this.openEditDialog();
     } else if (event.target == this._elements.edit) {
       this.openEditDialog(this._selectedOptions[0].address);
     }
   },
 
+  /**
+   * Handle key press events
+   *
+   * @param  {DOMEvent} event
+   */
+  handleKeyPress(event) {
+    if (event.keyCode == KeyEvent.DOM_VK_ESCAPE) {
+      window.close();
+    }
+  },
+
   observe(subject, topic, data) {
     switch (topic) {
       case "formautofill-storage-changed": {
         if (this._pendingChangeCount) {
           this._pendingChangeCount -= 1;
           return;
         }
         this.loadAddresses();
@@ -247,24 +262,26 @@ ManageProfileDialog.prototype = {
     }
   },
 
   /**
    * Attach event listener
    */
   attachEventListeners() {
     window.addEventListener("unload", this, {once: true});
+    window.addEventListener("keypress", this);
     this._elements.addresses.addEventListener("change", this);
     this._elements.controlsContainer.addEventListener("click", this);
     Services.obs.addObserver(this, "formautofill-storage-changed");
   },
 
   /**
    * Remove event listener
    */
   detachEventListeners() {
+    window.removeEventListener("keypress", this);
     this._elements.addresses.removeEventListener("change", this);
     this._elements.controlsContainer.removeEventListener("click", this);
     Services.obs.removeObserver(this, "formautofill-storage-changed");
   },
 };
 
 new ManageProfileDialog();
--- a/browser/extensions/formautofill/test/browser/browser_editProfileDialog.js
+++ b/browser/extensions/formautofill/test/browser/browser_editProfileDialog.js
@@ -8,16 +8,29 @@ add_task(async function test_cancelEditP
         ok(true, "Edit profile dialog is closed");
         resolve();
       }, {once: true});
       win.document.querySelector("#cancel").click();
     }, {once: true});
   });
 });
 
+add_task(async function test_cancelEditProfileDialogWithESC() {
+  await new Promise(resolve => {
+    let win = window.openDialog(EDIT_PROFILE_DIALOG_URL);
+    win.addEventListener("load", () => {
+      win.addEventListener("unload", () => {
+        ok(true, "Edit profile dialog is closed with ESC key");
+        resolve();
+      }, {once: true});
+      EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
+    }, {once: true});
+  });
+});
+
 add_task(async function test_saveAddress() {
   await new Promise(resolve => {
     let win = window.openDialog(EDIT_PROFILE_DIALOG_URL, null, null, null);
     win.addEventListener("load", () => {
       win.addEventListener("unload", () => {
         ok(true, "Edit profile dialog is closed");
         resolve();
       }, {once: true});
--- a/browser/extensions/formautofill/test/browser/browser_manageProfilesDialog.js
+++ b/browser/extensions/formautofill/test/browser/browser_manageProfilesDialog.js
@@ -30,16 +30,29 @@ add_task(async function test_manageProfi
       is(selAddresses.length, 0, "No address");
       is(btnAdd.disabled, false, "Add button enabled");
       is(btnRemove.disabled, true, "Remove button disabled");
       is(btnEdit.disabled, true, "Edit button disabled");
     });
   });
 });
 
+add_task(async function test_cancelManageProfileDialogWithESC() {
+  await new Promise(resolve => {
+    let win = window.openDialog(MANAGE_PROFILES_DIALOG_URL);
+    win.addEventListener("load", () => {
+      win.addEventListener("unload", () => {
+        ok(true, "Manage profiles dialog is closed with ESC key");
+        resolve();
+      }, {once: true});
+      EventUtils.synthesizeKey("VK_ESCAPE", {}, win);
+    }, {once: true});
+  });
+});
+
 add_task(async function test_removingSingleAndMultipleProfiles() {
   await saveAddress(TEST_ADDRESS_1);
   await saveAddress(TEST_ADDRESS_2);
   await saveAddress(TEST_ADDRESS_3);
 
   let win = window.openDialog(MANAGE_PROFILES_DIALOG_URL, null, DIALOG_SIZE);
   await waitForAddresses();