Bug 1390816 - Part 2: Add browser test for sync enabled case. r=lchang, a=lizzard
authorsteveck-chung <schung@mozilla.com>
Thu, 17 Aug 2017 12:18:24 +0800
changeset 655626 2c15a6ed11f7912081bd7e029fab8ddfb2b41145
parent 655625 cb236dfdd7bbffb31d9fc33e22d9645f768f9a42
child 655627 8b4fbaa0a0de8752ec212789f465eb62ca17f3b9
push id76942
push userbmo:scwwu@mozilla.com
push dateWed, 30 Aug 2017 08:27:15 +0000
reviewerslchang, lizzard
bugs1390816
milestone56.0
Bug 1390816 - Part 2: Add browser test for sync enabled case. r=lchang, a=lizzard MozReview-Commit-ID: FpsaoaMc6U4
browser/extensions/formautofill/test/browser/browser_first_time_use_doorhanger.js
browser/extensions/formautofill/test/browser/browser_submission_in_private_mode.js
browser/extensions/formautofill/test/browser/browser_update_doorhanger.js
browser/extensions/formautofill/test/browser/head.js
--- a/browser/extensions/formautofill/test/browser/browser_first_time_use_doorhanger.js
+++ b/browser/extensions/formautofill/test/browser/browser_first_time_use_doorhanger.js
@@ -1,14 +1,10 @@
 "use strict";
 
-const FORM_URL = "http://mochi.test:8888/browser/browser/extensions/formautofill/test/browser/autocomplete_basic.html";
-const FTU_PREF = "extensions.formautofill.firstTimeUse";
-const ENABLED_PREF = "extensions.formautofill.addresses.enabled";
-
 add_task(async function test_first_time_save() {
   let addresses = await getAddresses();
   is(addresses.length, 0, "No address in storage");
   await SpecialPowers.pushPrefEnv({
     "set": [
       [FTU_PREF, true],
       [ENABLED_PREF, true],
     ],
@@ -27,16 +23,18 @@ add_task(async function test_first_time_
         form.querySelector("#tel").value = "1-345-345-3456";
 
         // Wait 500ms before submission to make sure the input value applied
         await new Promise(resolve => setTimeout(resolve, 500));
         form.querySelector("input[type=submit]").click();
       });
 
       await promiseShown;
+      let cb = getDoorhangerCheckbox();
+      ok(cb.hidden, "Sync checkbox should be hidden");
       // Open the panel via main button
       await clickDoorhangerButton(MAIN_BUTTON_INDEX);
       let tab = await tabPromise;
       ok(tab, "Privacy panel opened");
       await BrowserTestUtils.removeTab(tab);
     }
   );
 
@@ -69,8 +67,56 @@ add_task(async function test_non_first_t
       await sleep(1000);
       is(PopupNotifications.panel.state, "closed", "Doorhanger is hidden");
     }
   );
 
   addresses = await getAddresses();
   is(addresses.length, 2, "Another address saved");
 });
+
+add_task(async function test_first_time_save_with_sync_account() {
+  await SpecialPowers.pushPrefEnv({
+    "set": [
+      [FTU_PREF, true],
+      [ENABLED_PREF, true],
+      [SYNC_USERNAME_PREF, "foo@bar.com"],
+    ],
+  });
+
+  await BrowserTestUtils.withNewTab({gBrowser, url: FORM_URL},
+    async function(browser) {
+      let promiseShown = BrowserTestUtils.waitForEvent(PopupNotifications.panel,
+                                                       "popupshown");
+      let tabPromise = BrowserTestUtils.waitForNewTab(gBrowser, "about:preferences#privacy");
+      await ContentTask.spawn(browser, null, async function() {
+        let form = content.document.getElementById("form");
+        form.querySelector("#organization").focus();
+        form.querySelector("#organization").value = "Foobar";
+        form.querySelector("#email").value = "foo@bar.com";
+        form.querySelector("#tel").value = "1-234-567-8900";
+
+        // Wait 500ms before submission to make sure the input value applied
+        await new Promise(resolve => setTimeout(resolve, 500));
+        form.querySelector("input[type=submit]").click();
+      });
+
+      await promiseShown;
+      let cb = getDoorhangerCheckbox();
+      ok(!cb.hidden, "Sync checkbox should be visible");
+      is(SpecialPowers.getBoolPref(SYNC_ADDRESSES_PREF), false,
+         "addresses sync should be disabled at first");
+
+      is(cb.checked, false, "Checkbox state should match addresses sync state");
+      cb.click();
+      is(SpecialPowers.getBoolPref(SYNC_ADDRESSES_PREF), true,
+         "addresses sync should be enabled after checked");
+      // Open the panel via main button
+      await clickDoorhangerButton(MAIN_BUTTON_INDEX);
+      let tab = await tabPromise;
+      ok(tab, "Privacy panel opened");
+      await BrowserTestUtils.removeTab(tab);
+    }
+  );
+
+  let ftuPref = SpecialPowers.getBoolPref(FTU_PREF);
+  is(ftuPref, false, "First time use flag is false");
+});
--- a/browser/extensions/formautofill/test/browser/browser_submission_in_private_mode.js
+++ b/browser/extensions/formautofill/test/browser/browser_submission_in_private_mode.js
@@ -1,12 +1,10 @@
 "use strict";
 
-const FORM_URL = "http://mochi.test:8888/browser/browser/extensions/formautofill/test/browser/autocomplete_basic.html";
-
 add_task(async function test_add_address() {
   let privateWin = await BrowserTestUtils.openNewBrowserWindow({private: true});
   let addresses = await getAddresses();
 
   is(addresses.length, 0, "No address in storage");
 
   await BrowserTestUtils.withNewTab(
     {gBrowser: privateWin.gBrowser, url: FORM_URL},
--- a/browser/extensions/formautofill/test/browser/browser_update_doorhanger.js
+++ b/browser/extensions/formautofill/test/browser/browser_update_doorhanger.js
@@ -1,12 +1,10 @@
 "use strict";
 
-const FORM_URL = "http://mochi.test:8888/browser/browser/extensions/formautofill/test/browser/autocomplete_basic.html";
-
 add_task(async function test_update_address() {
   await saveAddress(TEST_ADDRESS_1);
   let addresses = await getAddresses();
   is(addresses.length, 1, "1 address in storage");
 
   await BrowserTestUtils.withNewTab({gBrowser, url: FORM_URL},
     async function(browser) {
       let promiseShown = BrowserTestUtils.waitForEvent(PopupNotifications.panel,
--- a/browser/extensions/formautofill/test/browser/head.js
+++ b/browser/extensions/formautofill/test/browser/head.js
@@ -1,20 +1,26 @@
 /* exported MANAGE_ADDRESSES_DIALOG_URL, EDIT_ADDRESS_DIALOG_URL, BASE_URL,
             TEST_ADDRESS_1, TEST_ADDRESS_2, TEST_ADDRESS_3, TEST_ADDRESS_4, TEST_ADDRESS_5,
-            TEST_CREDIT_CARD_1, TEST_CREDIT_CARD_2, TEST_CREDIT_CARD_3,
+            TEST_CREDIT_CARD_1, TEST_CREDIT_CARD_2, TEST_CREDIT_CARD_3, FORM_URL,
+            FTU_PREF, ENABLED_PREF, SYNC_USERNAME_PREF, SYNC_ADDRESSES_PREF,
             sleep, expectPopupOpen, openPopupOn, expectPopupClose, closePopup, clickDoorhangerButton,
             getAddresses, saveAddress, removeAddresses, saveCreditCard,
-            getDisplayedPopupItems */
+            getDisplayedPopupItems, getDoorhangerCheckbox */
 
 "use strict";
 
 const MANAGE_ADDRESSES_DIALOG_URL = "chrome://formautofill/content/manageAddresses.xhtml";
 const EDIT_ADDRESS_DIALOG_URL = "chrome://formautofill/content/editAddress.xhtml";
 const BASE_URL = "http://mochi.test:8888/browser/browser/extensions/formautofill/test/browser/";
+const FORM_URL = "http://mochi.test:8888/browser/browser/extensions/formautofill/test/browser/autocomplete_basic.html";
+const FTU_PREF = "extensions.formautofill.firstTimeUse";
+const ENABLED_PREF = "extensions.formautofill.addresses.enabled";
+const SYNC_USERNAME_PREF = "services.sync.username";
+const SYNC_ADDRESSES_PREF = "services.sync.engine.addresses";
 
 const TEST_ADDRESS_1 = {
   "given-name": "John",
   "additional-name": "R.",
   "family-name": "Smith",
   organization: "World Wide Web Consortium",
   "street-address": "32 Vassar Street\nMIT Room 32-G524",
   "address-level2": "Cambridge",
@@ -187,14 +193,21 @@ async function clickDoorhangerButton(but
     EventUtils.synthesizeMouseAtCenter(notification.secondaryButton, {});
   } else if (notification.childNodes[buttonIndex - 1]) {
     ok(true, "Triggering secondary action with index " + buttonIndex);
     EventUtils.synthesizeMouseAtCenter(notification.childNodes[buttonIndex - 1], {});
   }
   await popuphidden;
 }
 
+function getDoorhangerCheckbox() {
+  let notifications = PopupNotifications.panel.childNodes;
+  ok(notifications.length > 0, "at least one notification displayed");
+  ok(true, notifications.length + " notification(s)");
+  return notifications[0].checkbox;
+}
+
 registerCleanupFunction(async function() {
   let addresses = await getAddresses();
   if (addresses.length) {
     await removeAddresses(addresses.map(address => address.guid));
   }
 });