summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/browser_webconsole_bug_601667_filter_buttons.js
blob: 6dae0a7b7ea38ddfa620f3ab8d4b033d8621edb7 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Tests that the filter button UI logic works correctly.

"use strict";

const TEST_URI = "http://example.com/";
const FILTER_PREF_DOMAIN = "devtools.webconsole.filter.";

var hud, hudId, hudBox;
var prefs = {};

add_task(function* () {
  yield loadTab(TEST_URI);

  hud = yield openConsole();
  hudId = hud.hudId;
  hudBox = hud.ui.rootElement;

  savePrefs();

  testFilterButtons();

  restorePrefs();

  hud = hudId = hudBox = null;
});

function savePrefs() {
  let branch = Services.prefs.getBranch(FILTER_PREF_DOMAIN);
  let children = branch.getChildList("");
  for (let child of children) {
    prefs[child] = branch.getBoolPref(child);
  }
}

function restorePrefs() {
  let branch = Services.prefs.getBranch(FILTER_PREF_DOMAIN);
  for (let p in prefs) {
    branch.setBoolPref(p, prefs[p]);
  }
}

function testFilterButtons() {
  testMenuFilterButton("net");
  testMenuFilterButton("css");
  testMenuFilterButton("js");
  testMenuFilterButton("logging");
  testMenuFilterButton("security");
  testMenuFilterButton("server");

  testIsolateFilterButton("net");
  testIsolateFilterButton("css");
  testIsolateFilterButton("js");
  testIsolateFilterButton("logging");
  testIsolateFilterButton("security");
  testIsolateFilterButton("server");
}

function testMenuFilterButton(category) {
  let selector = ".webconsole-filter-button[category=\"" + category + "\"]";
  let button = hudBox.querySelector(selector);
  ok(button, "we have the \"" + category + "\" button");

  let firstMenuItem = button.querySelector("menuitem");
  ok(firstMenuItem, "we have the first menu item for the \"" + category +
     "\" button");

  // Turn all the filters off, if they were on.
  let menuItem = firstMenuItem;
  while (menuItem != null) {
    if (menuItem.hasAttribute("prefKey") && isChecked(menuItem)) {
      chooseMenuItem(menuItem);
    }
    menuItem = menuItem.nextSibling;
  }

  // Turn all the filters on; make sure the button gets checked.
  menuItem = firstMenuItem;
  let prefKey;
  while (menuItem) {
    if (menuItem.hasAttribute("prefKey")) {
      prefKey = menuItem.getAttribute("prefKey");
      chooseMenuItem(menuItem);
      ok(isChecked(menuItem), "menu item " + prefKey + " for category " +
         category + " is checked after clicking it");
      ok(hud.ui.filterPrefs[prefKey], prefKey + " messages are " +
         "on after clicking the appropriate menu item");
    }
    menuItem = menuItem.nextSibling;
  }
  ok(isChecked(button), "the button for category " + category + " is " +
     "checked after turning on all its menu items");

  // Turn one filter off; make sure the button is still checked.
  prefKey = firstMenuItem.getAttribute("prefKey");
  chooseMenuItem(firstMenuItem);
  ok(!isChecked(firstMenuItem), "the first menu item for category " +
     category + " is no longer checked after clicking it");
  ok(!hud.ui.filterPrefs[prefKey], prefKey + " messages are " +
     "turned off after clicking the appropriate menu item");
  ok(isChecked(button), "the button for category " + category + " is still " +
     "checked after turning off its first menu item");

  // Turn all the filters off by clicking the main part of the button.
  let subbutton = getMainButton(button);
  ok(subbutton, "we have the subbutton for category " + category);

  clickButton(subbutton);
  ok(!isChecked(button), "the button for category " + category + " is " +
     "no longer checked after clicking its main part");

  menuItem = firstMenuItem;
  while (menuItem) {
    prefKey = menuItem.getAttribute("prefKey");
    if (prefKey) {
      ok(!isChecked(menuItem), "menu item " + prefKey + " for category " +
         category + " is no longer checked after clicking the button");
      ok(!hud.ui.filterPrefs[prefKey], prefKey + " messages are " +
         "off after clicking the button");
    }
    menuItem = menuItem.nextSibling;
  }

  // Turn all the filters on by clicking the main part of the button.
  clickButton(subbutton);

  ok(isChecked(button), "the button for category " + category + " is " +
     "checked after clicking its main part");

  menuItem = firstMenuItem;
  while (menuItem) {
    if (menuItem.hasAttribute("prefKey")) {
      prefKey = menuItem.getAttribute("prefKey");
      // The CSS/Log menu item should not be checked. See bug 971798.
      if (category == "css" && prefKey == "csslog") {
        ok(!isChecked(menuItem), "menu item " + prefKey + " for category " +
           category + " should not be checked after clicking the button");
        ok(!hud.ui.filterPrefs[prefKey], prefKey + " messages are " +
           "off after clicking the button");
      } else {
        ok(isChecked(menuItem), "menu item " + prefKey + " for category " +
           category + " is checked after clicking the button");
        ok(hud.ui.filterPrefs[prefKey], prefKey + " messages are " +
           "on after clicking the button");
      }
    }
    menuItem = menuItem.nextSibling;
  }

  // Uncheck the main button by unchecking all the filters
  menuItem = firstMenuItem;
  while (menuItem) {
    // The csslog menu item is already unchecked at this point.
    // Make sure it is not selected. See bug 971798.
    prefKey = menuItem.getAttribute("prefKey");
    if (prefKey && prefKey != "csslog") {
      chooseMenuItem(menuItem);
    }
    menuItem = menuItem.nextSibling;
  }

  ok(!isChecked(button), "the button for category " + category + " is " +
     "unchecked after unchecking all its filters");

  // Turn all the filters on again by clicking the button.
  clickButton(subbutton);
}

function testIsolateFilterButton(category) {
  let selector = ".webconsole-filter-button[category=\"" + category + "\"]";
  let targetButton = hudBox.querySelector(selector);
  ok(targetButton, "we have the \"" + category + "\" button");

  // Get the main part of the filter button.
  let subbutton = getMainButton(targetButton);
  ok(subbutton, "we have the subbutton for category " + category);

  // Turn on all the filters by alt clicking the main part of the button.
  altClickButton(subbutton);
  ok(isChecked(targetButton), "the button for category " + category +
     " is checked after isolating for filter");

  // Check if all the filters for the target button are on.
  let menuItems = targetButton.querySelectorAll("menuitem");
  Array.forEach(menuItems, (item) => {
    let prefKey = item.getAttribute("prefKey");
    // The CSS/Log filter should not be checked. See bug 971798.
    if (category == "css" && prefKey == "csslog") {
      ok(!isChecked(item), "menu item " + prefKey + " for category " +
        category + " should not be checked after isolating for " + category);
      ok(!hud.ui.filterPrefs[prefKey], prefKey + " messages should be " +
        "turned off after isolating for " + category);
    } else if (prefKey) {
      ok(isChecked(item), "menu item " + prefKey + " for category " +
        category + " is checked after isolating for " + category);
      ok(hud.ui.filterPrefs[prefKey], prefKey + " messages are " +
        "turned on after isolating for " + category);
    }
  });

  // Ensure all other filter buttons are toggled off and their
  // associated filters are turned off
  let buttons = hudBox.querySelectorAll(".webconsole-filter-button[category]");
  Array.forEach(buttons, (filterButton) => {
    if (filterButton !== targetButton) {
      let categoryBtn = filterButton.getAttribute("category");
      ok(!isChecked(filterButton), "the button for category " +
         categoryBtn + " is unchecked after isolating for " + category);

      menuItems = filterButton.querySelectorAll("menuitem");
      Array.forEach(menuItems, (item) => {
        let prefKey = item.getAttribute("prefKey");
        if (prefKey) {
          ok(!isChecked(item), "menu item " + prefKey + " for category " +
            category + " is unchecked after isolating for " + category);
          ok(!hud.ui.filterPrefs[prefKey], prefKey + " messages are " +
            "turned off after isolating for " + category);
        }
      });

      // Turn all the filters on again by clicking the button.
      let mainButton = getMainButton(filterButton);
      clickButton(mainButton);
    }
  });
}

/**
 * Return the main part of the target filter button.
 */
function getMainButton(targetButton) {
  let anonymousNodes = hud.ui.document.getAnonymousNodes(targetButton);
  let subbutton;

  for (let i = 0; i < anonymousNodes.length; i++) {
    let node = anonymousNodes[i];
    if (node.classList.contains("toolbarbutton-menubutton-button")) {
      subbutton = node;
      break;
    }
  }

  return subbutton;
}

function clickButton(node) {
  EventUtils.sendMouseEvent({ type: "click" }, node);
}

function altClickButton(node) {
  EventUtils.sendMouseEvent({ type: "click", altKey: true }, node);
}

function chooseMenuItem(node) {
  let event = document.createEvent("XULCommandEvent");
  event.initCommandEvent("command", true, true, window, 0, false, false, false,
                         false, null);
  node.dispatchEvent(event);
}

function isChecked(node) {
  return node.getAttribute("checked") === "true";
}