summaryrefslogtreecommitdiffstats
path: root/browser/components/preferences/tests/browser_dns_over_https_exceptions_subdialog.js
blob: c24c13e9e87f1d970a5a6172968451d9ff9e5d7d (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
async function dohExceptionsSubdialogOpened(dialogOverlay) {
  const promiseSubDialogLoaded = promiseLoadSubDialog(
    "chrome://browser/content/preferences/dialogs/dohExceptions.xhtml"
  );
  const contentDocument = gBrowser.contentDocument;
  contentDocument.getElementById("dohExceptionsButton").click();
  const win = await promiseSubDialogLoaded;
  dialogOverlay = content.gSubDialog._topDialog._overlay;
  ok(!BrowserTestUtils.isHidden(dialogOverlay), "The dialog is visible.");
  return win;
}

function acceptDoHExceptionsSubdialog(win) {
  const button = win.document.querySelector("dialog").getButton("accept");
  button.doCommand();
}

function cancelDoHExceptionsSubdialog(win) {
  const button = win.document.querySelector("dialog").getButton("cancel");
  button.doCommand();
}

function addNewException(domain, dialog) {
  let url = dialog.document.getElementById("url");
  let addButton = dialog.document.getElementById("btnAddException");

  ok(
    addButton.disabled,
    "The Add button is disabled when domain's input box is empty"
  );

  url.focus();
  EventUtils.sendString(domain);

  ok(
    !addButton.disabled,
    "The Add button is enabled when some text is on domain's input box"
  );

  addButton.click();

  is(
    url.value,
    "",
    "Domain input box is empty after adding a new domain to the list"
  );
  ok(
    addButton.disabled,
    "The Add button is disabled after exception has been added to the list"
  );
}

add_task(async function () {
  Services.prefs.lockPref("network.trr.excluded-domains");

  await openPreferencesViaOpenPreferencesAPI("panePrivacy", {
    leaveOpen: true,
  });
  let dialogOverlay = content.gSubDialog._preloadDialog._overlay;
  let win = await dohExceptionsSubdialogOpened(dialogOverlay);

  ok(
    win.document.getElementById("btnAddException").disabled,
    "The Add button is disabled when preference is locked"
  );
  ok(
    win.document.getElementById("url").disabled,
    "The url input box is disabled when preference is locked"
  );

  cancelDoHExceptionsSubdialog(win);
  Services.prefs.unlockPref("network.trr.excluded-domains");
  win = await dohExceptionsSubdialogOpened(dialogOverlay);

  ok(
    win.document.getElementById("btnAddException").disabled,
    "The Add button is disabled when preference is not locked"
  );
  ok(
    !win.document.getElementById("url").disabled,
    "The url input box is enabled when preference is not locked"
  );

  cancelDoHExceptionsSubdialog(win);
  gBrowser.removeCurrentTab();
});

add_task(async function () {
  await openPreferencesViaOpenPreferencesAPI("panePrivacy", {
    leaveOpen: true,
  });
  let dialogOverlay = content.gSubDialog._preloadDialog._overlay;

  ok(BrowserTestUtils.isHidden(dialogOverlay), "The dialog is invisible.");
  let win = await dohExceptionsSubdialogOpened(dialogOverlay);
  acceptDoHExceptionsSubdialog(win);
  ok(BrowserTestUtils.isHidden(dialogOverlay), "The dialog is invisible.");

  win = await dohExceptionsSubdialogOpened(dialogOverlay);
  Assert.equal(
    win.document.getElementById("permissionsBox").itemCount,
    0,
    "There are no exceptions set."
  );
  ok(
    win.document.getElementById("removeException").disabled,
    "The Remove button is disabled when there are no exceptions on the list"
  );
  ok(
    win.document.getElementById("removeAllExceptions").disabled,
    "The Remove All button is disabled when there are no exceptions on the list"
  );
  ok(
    win.document.getElementById("btnAddException").disabled,
    "The Add button is disabled when dialog box has just been opened"
  );

  addNewException("test1.com", win);
  Assert.equal(
    win.document.getElementById("permissionsBox").itemCount,
    1,
    "List shows 1 new item"
  );
  let activeExceptions = win.document.getElementById("permissionsBox").children;
  is(
    activeExceptions[0].getAttribute("domain"),
    "test1.com",
    "test1.com added to the list"
  );
  ok(
    !win.document.getElementById("removeAllExceptions").disabled,
    "The Remove All button is enabled when there is one exception on the list"
  );
  addNewException("test2.com", win);
  addNewException("test3.com", win);
  Assert.equal(
    win.document.getElementById("permissionsBox").itemCount,
    3,
    "List shows 3 domain items"
  );
  ok(
    win.document.getElementById("removeException").disabled,
    "The Remove button is disabled when no exception has been selected"
  );
  win.document.getElementById("permissionsBox").selectedIndex = 1;
  ok(
    !win.document.getElementById("removeException").disabled,
    "The Remove button is enabled when an exception has been selected"
  );
  win.document.getElementById("removeException").doCommand();
  Assert.equal(
    win.document.getElementById("permissionsBox").itemCount,
    2,
    "List shows 2 domain items after removing one of the three"
  );
  activeExceptions = win.document.getElementById("permissionsBox").children;
  ok(
    win.document.getElementById("permissionsBox").itemCount == 2 &&
      activeExceptions[0].getAttribute("domain") == "test1.com" &&
      activeExceptions[1].getAttribute("domain") == "test3.com",
    "test1.com and test3.com are the only items left on the list"
  );
  is(
    win.document.getElementById("permissionsBox").selectedIndex,
    -1,
    "There is no selected item after removal"
  );
  addNewException("test2.com", win);
  activeExceptions = win.document.getElementById("permissionsBox").children;
  ok(
    win.document.getElementById("permissionsBox").itemCount == 3 &&
      activeExceptions[1].getAttribute("domain") == "test2.com",
    "test2.com has been added as the second item"
  );
  win.document.getElementById("removeAllExceptions").doCommand();
  is(
    win.document.getElementById("permissionsBox").itemCount,
    0,
    "There are no elements on the list after clicking Remove All"
  );

  acceptDoHExceptionsSubdialog(win);

  gBrowser.removeCurrentTab();
});