summaryrefslogtreecommitdiffstats
path: root/comm/suite/components/tests/chrome/test_idcheck.xul
blob: 213fda008a997a75b79b1cb64e9b44f1b2490838 (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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?xml version="1.0"?>

<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>

<window title="Chrome Window ID Checking Tests"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        onload="RunTest();">
  <description>Chrome Window ID Checking Tests</description>

  <script src="chrome://mochikit/content/MochiKit/packed.js"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script src="chrome://mochikit/content/chrome-harness.js"/>

  <script>
  <![CDATA[
    let scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
                getService(Ci.mozIJSSubScriptLoader);

    let rootDir = getRootDirectory(window.location.href);
    scriptLoader.loadSubScript(rootDir + "mailTestUtils.js", this);

    var gLoadedWindows = {};

    // Have all loaded windows been closed again?
    function AllWindowsClosed()
    {
      return Object.keys(window.gLoadedWindows).length == 0;
    }

    function DumpElementTree(aElement)
    {
      // walk upwards from element to DOM root
      while (aElement)
      {
        // print nodeName, id and index in parent
        let s = "    " + aElement.nodeName + " " + aElement.id + " ";
        let ix = 0;
        while (aElement.previousSibling)
        {
          aElement = aElement.previousSibling;
          ++ix;
        }
        dump(s + "[" + ix + "]\n");
        aElement = aElement.parentNode;
      }
    }

    function CheckIDs(aDocument, aIgnorableIDs)
    {
      var filename = aDocument.location.href.match(/[^/]+$/);
      // all panes are loaded, now check the ids
      // first, get the list of all used ids
      var idList = aDocument.getElementsByAttribute("id", "*");
      // then store them in another object, checking if it's already there
      var checkedList = {};
      var ignoredList = {};
      for (let i = 0; i < idList.length; ++i)
      {
        let id = idList[i].id;
        let duplicate = (id in checkedList);
        if (!duplicate)
        {
          checkedList[id] = idList[i];
        }
        else
        {
          // always dump DOM trees of the conflicting elements
          dump("Double id='" + id + "' detected in " + aDocument.location.href + ":\n");
          dump("  Tree 0:\n");
          DumpElementTree(checkedList[id]);
          dump("  Tree 1:\n");
          DumpElementTree(idList[i]);
        }
        if (!aIgnorableIDs.includes(id))
        {
          // if the id is not in our ignore list, show its status
          ok(!duplicate, "check id: " + filename + "#" + id);
        }
        else if (!(id in ignoredList))
        {
          // mark ignored id tests as todo,
          // even though we may never (be able to) fix them
          ignoredList[id] = idList[i];
          todo(false, "disabled id checks: " + filename + "#" + id);
        }
      }

      // finally, close the loaded window
      aDocument.defaultView.close();
    }

    function DisambiguateCharsetMenulist(aDocument, aListID, aPrefix)
    {
      let menulist = aDocument.getElementById(aListID)
                              .getElementsByTagName("menuitem");
      for (let menuitem of menulist)
      {
        menuitem.id = aPrefix + menuitem.id;
        menuitem = menuitem.nextSibling;
      }
    }

    function LoadPaneLoop(aDocument, aPanes, aPaneIndex, aForceLoad)
    {
      if (aPaneIndex < aPanes.length)
      {
        const WAIT_CYCLE = 10;
        // may need to load this pane
        let pane = aPanes[aPaneIndex];
        if (pane.loaded)
        {
          // okay, check/load next one
          setTimeout(LoadPaneLoop, WAIT_CYCLE, aDocument, aPanes, aPaneIndex + 1, true);
        }
        else
        {
          // force load once and wait until done
          if (aForceLoad)
          {
            try
            {
              aDocument.documentElement.showPane.call(aDocument.documentElement, pane);
            }
            catch (ignored) {}
          }
          setTimeout(LoadPaneLoop, WAIT_CYCLE, aDocument, aPanes, aPaneIndex, false);
        }
      }
      else
      {
        // All preference panes are loaded now!

        // The character_encoding_pane contains two template driven menulists
        // (viewDefaultCharsetList and sendDefaultCharsetList),
        // both of which autogenerate the *same* ids for their menuitems.
        // The same ids are generated by the charset list (defaultCharsetList)
        // on the languages_pane, too.
        // We alter two of these sets here to avoid unnecessary test failure.
        // (We probably should remove those RDF templates?)
        DisambiguateCharsetMenulist(aDocument, "viewDefaultCharsetList", "test_idcheck.1.");
        DisambiguateCharsetMenulist(aDocument, "sendDefaultCharsetList", "test_idcheck.2.");

        // now check the ids
        CheckIDs(aDocument, window.gLoadedWindows[aDocument.location.href]);
      }
    }

    function CheckPreferences()
    {
      this.removeEventListener("load", window.CheckPreferences, false);

      // Prefpanes are loaded lazily, thus we need to trigger each panel manually
      // before we can check for doubled ids...
      var panes = this.document.getElementsByTagName("prefpane");
      setTimeout(LoadPaneLoop, 0, this.document, panes, 0, true);
    }

    function CheckGenerics()
    {
      this.removeEventListener("load", window.CheckGenerics, false);
      CheckIDs(this.document, window.gLoadedWindows[this.location.href]);
    }

    function UncountWindow()
    {
      if (this.location.href in window.gLoadedWindows)
      {
        this.removeEventListener("unload", window.UncountWindow, false);
        delete window.gLoadedWindows[this.location.href];
      }
    }

    function InitTest()
    {
      // fake a mail account to avoid the account creation wizard
      loadLocalMailAccount();
    }

    function ExitTest()
    {
      // remove the mailnews data from the test profile
      Services.prefs.resetPrefs();
    }

    function FinishTest()
    {
      if (AllWindowsClosed())
      {
        // commented out to fix test failures after this due to missing prefs
        //ExitTest();
        SimpleTest.finish();
      }
      else
      {
        setTimeout(FinishTest, 1000);
      }
    }

    function RunTest()
    {
      SimpleTest.waitForExplicitFinish();
      InitTest();

      // Basically, this test framework is generic enough to check arbitrary
      // chrome windows for doubled ids. But certain stuff like preferences
      // needs some extra processing.
      // The uriList members have the following format:
      //    "chrome://uri/of/xul.window":
      //    [
      //      check function,
      //      array of IDs to be ignored during in the test
      //    ],
      var uriList =
      {
        // Preferences
        "chrome://communicator/content/pref/preferences.xul":
          [
            window.CheckPreferences,
            []
          ],

        // Browser
        "chrome://navigator/content/navigator.xul":
          [
            window.CheckGenerics,
            ["contentAreaContextSet"]
          ],

        // MailNews (needs at least one mail account)
        "chrome://messenger/content/messenger.xul":
          [
            window.CheckGenerics,
            []
          ],
        "chrome://messenger/content/messageWindow.xul":
          [
            window.CheckGenerics,
            []
          ],
        "chrome://messenger/content/messengercompose/messengercompose.xul":
          [
            window.CheckGenerics,
            []
          ],

        // Addressbook (needs at least one mail account)
        "chrome://messenger/content/addressbook/addressbook.xul":
          [
            window.CheckGenerics,
            []
          ],

        // Composer
        "chrome://editor/content/editor.xul":
          [
            window.CheckGenerics,
            []
          ],

        // Error Console
        "chrome://communicator/content/console/console.xul":
          [
            window.CheckGenerics,
            []
          ],

        // Chatzilla
        "chrome://chatzilla/content/chatzilla.xul":
          [
            window.CheckGenerics,
            []
          ],
      };

      // run test
      for (var uri in uriList)
      {
        // load the window, but postpone the id check until it's fully loaded
        window.gLoadedWindows[uri] = uriList[uri][1]; // ignore these ids
        var win = openDialog(uri, "", "chrome,titlebar,dialog=no,resizable");
        win.addEventListener("load", uriList[uri][0], false);
        win.addEventListener("unload", window.UncountWindow, false);
      }

      // wait for all tests to finish
      SimpleTest.executeSoon(FinishTest);
    }
  ]]>
  </script>

  <body xmlns="http://www.w3.org/1999/xhtml">
    <p id="display"></p>
    <div id="content" style="display: none"></div>
    <pre id="test"></pre>
  </body>

</window>