summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/browser/browser_exceptions_dialog.js
blob: 854f0656b8a33d0a88e984c75b093eb71d56ab56 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"use strict";

const LOGIN_HOST = "https://example.com";

function openExceptionsDialog() {
  return window.openDialog(
    "chrome://browser/content/preferences/dialogs/permissions.xhtml",
    "Toolkit:PasswordManagerExceptions",
    "",
    {
      blockVisible: true,
      sessionVisible: false,
      allowVisible: false,
      hideStatusColumn: true,
      prefilledHost: "",
      permissionType: "login-saving",
    }
  );
}

function countDisabledHosts(dialog) {
  return dialog.document.getElementById("permissionsBox").itemCount;
}

function promiseStorageChanged(expectedData) {
  function observer(subject, data) {
    return (
      data == expectedData &&
      subject.QueryInterface(Ci.nsISupportsString).data == LOGIN_HOST
    );
  }

  return TestUtils.topicObserved("passwordmgr-storage-changed", observer);
}

add_task(async function test_disable() {
  let dialog = openExceptionsDialog();
  let promiseChanged = promiseStorageChanged("hostSavingDisabled");

  await BrowserTestUtils.waitForEvent(dialog, "load");
  await new Promise(resolve => {
    waitForFocus(resolve, dialog);
  });
  Services.logins.setLoginSavingEnabled(LOGIN_HOST, false);
  await promiseChanged;
  Assert.equal(countDisabledHosts(dialog), 1, "Verify disabled host added");
  await BrowserTestUtils.closeWindow(dialog);
});

add_task(async function test_enable() {
  let dialog = openExceptionsDialog();
  let promiseChanged = promiseStorageChanged("hostSavingEnabled");

  await BrowserTestUtils.waitForEvent(dialog, "load");
  await new Promise(resolve => {
    waitForFocus(resolve, dialog);
  });
  Services.logins.setLoginSavingEnabled(LOGIN_HOST, true);
  await promiseChanged;
  Assert.equal(countDisabledHosts(dialog), 0, "Verify disabled host removed");
  await BrowserTestUtils.closeWindow(dialog);
});

add_task(async function test_block_button_with_enter_key() {
  // Test ensures that the Enter/Return key does not activate the "Allow" button
  // in the "Saved Logins" exceptions dialog

  let dialog = openExceptionsDialog();

  await BrowserTestUtils.waitForEvent(dialog, "load");
  await new Promise(resolve => waitForFocus(resolve, dialog));
  let btnBlock = dialog.document.getElementById("btnBlock");
  let btnCookieSession = dialog.document.getElementById("btnCookieSession");
  let btnHttpsOnlyOff = dialog.document.getElementById("btnHttpsOnlyOff");
  let btnHttpsOnlyOffTmp = dialog.document.getElementById("btnHttpsOnlyOffTmp");
  let btnAllow = dialog.document.getElementById("btnAllow");

  Assert.ok(!btnBlock.hidden, "Block button is visible");
  Assert.ok(btnCookieSession.hidden, "Cookie session button is not visible");
  Assert.ok(btnAllow.hidden, "Allow button is not visible");
  Assert.ok(btnHttpsOnlyOff.hidden, "HTTPS-Only session button is not visible");
  Assert.ok(
    btnHttpsOnlyOffTmp.hidden,
    "HTTPS-Only session button is not visible"
  );
  Assert.ok(btnBlock.disabled, "Block button is initially disabled");
  Assert.ok(
    btnCookieSession.disabled,
    "Cookie session button is initially disabled"
  );
  Assert.ok(btnAllow.disabled, "Allow button is initially disabled");
  Assert.ok(
    btnHttpsOnlyOff.disabled,
    "HTTPS-Only off-button is initially disabled"
  );
  Assert.ok(
    btnHttpsOnlyOffTmp.disabled,
    "HTTPS-Only temporary off-button is initially disabled"
  );

  EventUtils.sendString(LOGIN_HOST, dialog);

  Assert.ok(
    !btnBlock.disabled,
    "Block button is enabled after entering text in the URL input"
  );
  Assert.ok(
    btnCookieSession.disabled,
    "Cookie session button is still disabled after entering text in the URL input"
  );
  Assert.ok(
    btnAllow.disabled,
    "Allow button is still disabled after entering text in the URL input"
  );
  Assert.ok(
    btnHttpsOnlyOff.disabled,
    "HTTPS-Only off-button is still disabled after entering text in the URL input"
  );
  Assert.ok(
    btnHttpsOnlyOffTmp.disabled,
    "HTTPS-Only session off-button is still disabled after entering text in the URL input"
  );

  Assert.equal(
    countDisabledHosts(dialog),
    0,
    "No blocked hosts should be present before hitting the Enter/Return key"
  );
  EventUtils.sendKey("return", dialog);

  Assert.equal(
    countDisabledHosts(dialog),
    1,
    "Verify the blocked host was added"
  );
  Assert.ok(
    btnBlock.disabled,
    "Block button is disabled after submitting to the list"
  );
  await BrowserTestUtils.closeWindow(dialog);
});