Bug 1330567 - Part 2: Add xpcshell test for the pref fallback, r?MattN draft
authorsteveck-chung <schung@mozilla.com>
Thu, 09 Feb 2017 18:10:11 +0800
changeset 481630 130a5d0b3795674ab1adba5b31c1c034b561ca9b
parent 481171 2e1d7607afb367d483b9cca8b4701f9a792dc890
child 545257 4f3a9c2853ee12a266586506968ebcf2f50317e8
push id44886
push userbmo:schung@mozilla.com
push dateFri, 10 Feb 2017 08:03:48 +0000
reviewersMattN
bugs1330567
milestone54.0a1
Bug 1330567 - Part 2: Add xpcshell test for the pref fallback, r?MattN MozReview-Commit-ID: BmjcwiW0EcN
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/extensions/formautofill/test/unit/head.js
+++ b/browser/extensions/formautofill/test/unit/head.js
@@ -1,28 +1,42 @@
 /**
  * Provides infrastructure for automated formautofill components tests.
  */
 
+/* global sinon */
 /* exported loadFormAutofillContent, getTempFile */
 
 "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");
 
+// Create a window polyfill so sinon can load
+Cu.import("resource://gre/modules/Timer.jsm");
+let window = {
+  document: {},
+  location: {},
+  setTimeout,
+  setInterval,
+  clearTimeout,
+  clearinterval: clearInterval,
+};
+let self = window; // eslint-disable-line no-unused-vars
+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()) {
new file mode 100644
--- /dev/null
+++ b/browser/extensions/formautofill/test/unit/test_enabledStatus.js
@@ -0,0 +1,54 @@
+/*
+ * Test for status handling in Form Autofill Parent.
+ */
+
+"use strict";
+
+Cu.import("resource://formautofill/FormAutofillParent.jsm");
+
+do_get_profile();
+
+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();
+
+  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);
+
+  Services.prefs.clearUserPref("browser.formautofill.enabled");
+});
--- a/browser/extensions/formautofill/test/unit/test_populateFieldValues.js
+++ b/browser/extensions/formautofill/test/unit/test_populateFieldValues.js
@@ -1,14 +1,12 @@
 /*
  * Test for populating field values in Form Autofill Parent.
  */
 
-/* global FormAutofillParent */
-
 "use strict";
 
 Cu.import("resource://formautofill/FormAutofillParent.jsm");
 
 do_get_profile();
 
 const TEST_FIELDS = [
   {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "organization"},
@@ -35,34 +33,35 @@ const TEST_PROFILE = {
   email: "timbl@w3.org",
 };
 
 function camelCase(str) {
   return str.toLowerCase().replace(/-([a-z])/g, s => s[1].toUpperCase());
 }
 
 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: function(name, data) {
           do_check_eq(name, "FormAutofill:fillForm");
@@ -79,28 +78,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]