Bug 1330567 - Part 2: Add xpcshell test for the autofill pref fallback, r=MattN
authorsteveck-chung <schung@mozilla.com>
Thu, 09 Feb 2017 18:10:11 +0800
changeset 342337 8d52a2b47ce65d739937bcbf654600e95353c31f
parent 342336 72ecf19a8ec01a113702a479ed28ba3f971017bd
child 342338 367692beef13216212dccaa48505150ad1b930f2
push id86837
push usermozilla@noorenberghe.ca
push dateSat, 11 Feb 2017 10:47:01 +0000
treeherdermozilla-inbound@8a2f028e6943 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN
bugs1330567
milestone54.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 1330567 - Part 2: Add xpcshell test for the autofill pref fallback, r=MattN MozReview-Commit-ID: BmjcwiW0EcN
browser/components/syncedtabs/test/xpcshell/head.js
browser/extensions/formautofill/test/unit/head.js
browser/extensions/formautofill/test/unit/test_enabledStatus.js
browser/extensions/formautofill/test/unit/test_populateFieldValues.js
browser/extensions/formautofill/test/unit/xpcshell.ini
--- a/browser/components/syncedtabs/test/xpcshell/head.js
+++ b/browser/components/syncedtabs/test/xpcshell/head.js
@@ -8,22 +8,22 @@ XPCOMUtils.defineLazyGetter(this, "FxAcc
 });
 
 Cu.import("resource://gre/modules/Timer.jsm");
 
 do_get_profile(); // fxa needs a profile directory for storage.
 
 // Create a window polyfill so sinon can load
 let window = {
-    document: {},
-    location: {},
-    setTimeout,
-    setInterval,
-    clearTimeout,
-    clearinterval: clearInterval
+  document: {},
+  location: {},
+  setTimeout,
+  setInterval,
+  clearTimeout,
+  clearInterval,
 };
 let self = window;
 
 // Load mocking/stubbing library, sinon
 // docs: http://sinonjs.org/docs/
 /* global sinon */
 let loader = Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader);
 loader.loadSubScript("resource://testing-common/sinon-1.16.1.js");
--- a/browser/extensions/formautofill/test/unit/head.js
+++ b/browser/extensions/formautofill/test/unit/head.js
@@ -1,28 +1,36 @@
 /**
  * Provides infrastructure for automated formautofill components tests.
  */
 
-/* exported loadFormAutofillContent, getTempFile */
+/* exported loadFormAutofillContent, getTempFile, sinon */
 
 "use strict";
 
 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
 
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/NetUtil.jsm");
 Cu.import("resource://testing-common/MockDocument.jsm");
 
 XPCOMUtils.defineLazyModuleGetter(this, "DownloadPaths",
                                   "resource://gre/modules/DownloadPaths.jsm");
 XPCOMUtils.defineLazyModuleGetter(this, "FileUtils",
                                   "resource://gre/modules/FileUtils.jsm");
 
+do_get_profile();
+
+// Setup the environment for sinon.
+Cu.import("resource://gre/modules/Timer.jsm");
+let self = {}; // eslint-disable-line no-unused-vars
+var sinon;
+Services.scriptloader.loadSubScript("resource://testing-common/sinon-1.16.1.js");
+
 // Load our bootstrap extension manifest so we can access our chrome/resource URIs.
 const EXTENSION_ID = "formautofill@mozilla.org";
 let extensionDir = Services.dirsvc.get("GreD", Ci.nsIFile);
 extensionDir.append("browser");
 extensionDir.append("features");
 extensionDir.append(EXTENSION_ID);
 // If the unpacked extension doesn't exist, use the packed version.
 if (!extensionDir.exists()) {
@@ -80,18 +88,18 @@ function getTempFile(leafName) {
     if (file.exists()) {
       file.remove(false);
     }
   });
 
   return file;
 }
 
-add_task(function* test_common_initialize() {
+add_task(function* head_initialize() {
   Services.prefs.setBoolPref("browser.formautofill.experimental", true);
   Services.prefs.setBoolPref("dom.forms.autocomplete.experimental", true);
 
   // Clean up after every test.
-  do_register_cleanup(() => {
+  do_register_cleanup(function head_cleanup() {
     Services.prefs.clearUserPref("browser.formautofill.experimental");
     Services.prefs.clearUserPref("dom.forms.autocomplete.experimental");
   });
 });
new file mode 100644
--- /dev/null
+++ b/browser/extensions/formautofill/test/unit/test_enabledStatus.js
@@ -0,0 +1,52 @@
+/*
+ * Test for status handling in Form Autofill Parent.
+ */
+
+"use strict";
+
+Cu.import("resource://formautofill/FormAutofillParent.jsm");
+
+add_task(function* test_enabledStatus_init() {
+  let formAutofillParent = new FormAutofillParent();
+  sinon.spy(formAutofillParent, "_onStatusChanged");
+
+  // Default status is false before initialization
+  do_check_eq(formAutofillParent._enabled, false);
+
+  formAutofillParent.init();
+  do_check_eq(formAutofillParent._onStatusChanged.called, true);
+
+  formAutofillParent._uninit();
+});
+
+add_task(function* test_enabledStatus_observe() {
+  let formAutofillParent = new FormAutofillParent();
+  sinon.stub(formAutofillParent, "_getStatus");
+  sinon.spy(formAutofillParent, "_onStatusChanged");
+
+  // _enabled = _getStatus() => No need to trigger onStatusChanged
+  formAutofillParent._enabled = true;
+  formAutofillParent._getStatus.returns(true);
+  formAutofillParent.observe();
+  do_check_eq(formAutofillParent._onStatusChanged.called, false);
+
+  // _enabled != _getStatus() => Need to trigger onStatusChanged
+  formAutofillParent._getStatus.returns(false);
+  formAutofillParent.observe();
+  do_check_eq(formAutofillParent._onStatusChanged.called, true);
+
+  formAutofillParent._uninit();
+});
+
+add_task(function* test_enabledStatus_getStatus() {
+  let formAutofillParent = new FormAutofillParent();
+  do_register_cleanup(function cleanup() {
+    Services.prefs.clearUserPref("browser.formautofill.enabled");
+  });
+
+  Services.prefs.setBoolPref("browser.formautofill.enabled", true);
+  do_check_eq(formAutofillParent._getStatus(), true);
+
+  Services.prefs.setBoolPref("browser.formautofill.enabled", false);
+  do_check_eq(formAutofillParent._getStatus(), false);
+});
--- a/browser/extensions/formautofill/test/unit/test_populateFieldValues.js
+++ b/browser/extensions/formautofill/test/unit/test_populateFieldValues.js
@@ -29,34 +29,35 @@ const TEST_PROFILE = {
   "address-level1": "MA",
   postalCode: "02139",
   country: "US",
   tel: "+1 617 253 5702",
   email: "timbl@w3.org",
 };
 
 add_task(function* test_populateFieldValues() {
-  FormAutofillParent.init();
+  let formAutofillParent = new FormAutofillParent();
+  formAutofillParent.init();
 
-  let store = FormAutofillParent.getProfileStore();
+  let store = formAutofillParent.getProfileStore();
   do_check_neq(store, null);
 
   store.get = function(guid) {
     do_check_eq(guid, TEST_GUID);
     return store._clone(TEST_PROFILE);
   };
 
   let notifyUsedCalledCount = 0;
   store.notifyUsed = function(guid) {
     do_check_eq(guid, TEST_GUID);
     notifyUsedCalledCount++;
   };
 
   yield new Promise((resolve) => {
-    FormAutofillParent.receiveMessage({
+    formAutofillParent.receiveMessage({
       name: "FormAutofill:PopulateFieldValues",
       data: {
         guid: TEST_GUID,
         fields: TEST_FIELDS,
       },
       target: {
         sendAsyncMessage(name, data) {
           do_check_eq(name, "FormAutofill:fillForm");
@@ -73,28 +74,29 @@ add_task(function* test_populateFieldVal
           resolve();
         },
       },
     });
   });
 
   do_check_eq(notifyUsedCalledCount, 1);
 
-  FormAutofillParent._uninit();
-  do_check_null(FormAutofillParent.getProfileStore());
+  formAutofillParent._uninit();
+  do_check_null(formAutofillParent.getProfileStore());
 });
 
 add_task(function* test_populateFieldValues_with_invalid_guid() {
-  FormAutofillParent.init();
+  let formAutofillParent = new FormAutofillParent();
+  formAutofillParent.init();
 
   Assert.throws(() => {
-    FormAutofillParent.receiveMessage({
+    formAutofillParent.receiveMessage({
       name: "FormAutofill:PopulateFieldValues",
       data: {
         guid: "invalid-guid",
         fields: TEST_FIELDS,
       },
       target: {},
     });
   }, /No matching profile\./);
 
-  FormAutofillParent._uninit();
+  formAutofillParent._uninit();
 });
--- a/browser/extensions/formautofill/test/unit/xpcshell.ini
+++ b/browser/extensions/formautofill/test/unit/xpcshell.ini
@@ -1,12 +1,13 @@
 [DEFAULT]
 firefox-appdir = browser
 head = head.js
 support-files =
 
 [test_autofillFormFields.js]
 [test_collectFormFields.js]
+[test_enabledStatus.js]
 [test_getFormInputDetails.js]
 [test_markAsAutofillField.js]
 [test_populateFieldValues.js]
 [test_profileAutocompleteResult.js]
 [test_profileStorage.js]