summaryrefslogtreecommitdiffstats
path: root/browser/extensions/formautofill/test/unit/test_populateFieldValues.js
blob: 1215cbd16dbf11f4c83742489f95895c3226184d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
 * Test for populating field values in Form Autofill Parent.
 */

/* global FormAutofillParent */

"use strict";

importAutofillModule("FormAutofillParent.jsm");

do_get_profile();

const TEST_FIELDS = [
  {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "organization"},
  {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "street-address"},
  {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "address-level2"},
  {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "address-level1"},
  {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "postal-code"},
  {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "country"},
  {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "tel"},
  {"section": "", "addressType": "shipping", "contactType": "", "fieldName": "email"},
];

const TEST_GUID = "test-guid";

const TEST_PROFILE = {
  guid: TEST_GUID,
  organization: "World Wide Web Consortium",
  streetAddress: "32 Vassar Street\nMIT Room 32-G524",
  addressLevel2: "Cambridge",
  addressLevel1: "MA",
  postalCode: "02139",
  country: "US",
  tel: "+1 617 253 5702",
  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 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({
      name: "FormAutofill:PopulateFieldValues",
      data: {
        guid: TEST_GUID,
        fields: TEST_FIELDS,
      },
      target: {
        sendAsyncMessage: function(name, data) {
          do_check_eq(name, "FormAutofill:fillForm");

          let fields = data.fields;
          do_check_eq(fields.length, TEST_FIELDS.length);

          for (let i = 0; i < fields.length; i++) {
            do_check_eq(fields[i].fieldName, TEST_FIELDS[i].fieldName);
            do_check_eq(fields[i].value,
              TEST_PROFILE[camelCase(fields[i].fieldName)]);
          }

          resolve();
        },
      },
    });
  });

  do_check_eq(notifyUsedCalledCount, 1);

  FormAutofillParent._uninit();
  do_check_null(FormAutofillParent.getProfileStore());
});

add_task(function* test_populateFieldValues_with_invalid_guid() {
  FormAutofillParent.init();

  Assert.throws(() => {
    FormAutofillParent.receiveMessage({
      name: "FormAutofill:PopulateFieldValues",
      data: {
        guid: "invalid-guid",
        fields: TEST_FIELDS,
      },
      target: {},
    });
  }, /No matching profile\./);

  FormAutofillParent._uninit();
});