summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/test_toolbar.xul
blob: 7adaa6aa3320c2c0745b7f474b2610a2a0f880a5 (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
<?xml version="1.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="Toolbar" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        onload="startTest();">

  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>

  <toolbox id="toolbox">
    <toolbarpalette>
      <toolbarbutton id="p1" label="p1"/>
      <toolbarbutton id="p2" label="p2"/>
      <toolbarbutton id="p3" label="p3"/>
      <toolbarbutton id="p4" label="p4"/>
      <toolbarbutton id="p5" label="p5"/>
      <toolbarbutton id="p6" label="p6"/>
      <toolbarbutton id="p7" label="p7"/>
      <toolbarbutton id="p8" label="p8"/>
      <toolbarbutton id="p9" label="p9"/>
      <toolbarbutton id="p10" label="p10"/>
      <toolbarbutton id="p11" label="p11"/>
      <toolbarbutton id="p12" label="p12"/>
    </toolbarpalette>

    <toolbar id="tb1" defaultset="p1,p2"/>
    <toolbar id="tb2" defaultset="p4,p3"/>
    <toolbar id="tb3" defaultset="p5,p6,t31">
      <toolbarbutton id="t31" label="t31" removable="true"/>
    </toolbar>
    <toolbar id="tb4" defaultset="t41,p7,p8">
      <toolbarbutton id="t41" label="t41" removable="true"/>
    </toolbar>
    <toolbar id="tb5" defaultset="p9,t51,p10">
      <toolbarbutton id="t51" label="t51" removable="true"/>
    </toolbar>

    <toolbar id="tb-test" defaultset="p11,p12"/>
    <toolbar id="tb-test2" defaultset=""/>
    <!-- fixed toolbarbuttons always have 'fixed' in their id -->
    <toolbar id="tb-test3" defaultset="">
      <toolbarbutton id="tb-fixed-1" label="tb-test3-1"/>
      <toolbarbutton id="tb-fixed-2" label="tb-test3-2" removable="false"/>
      <toolbarbutton id="tb-fixed-3" label="tb-test3-3"/>
    </toolbar>
  </toolbox>

  <toolbar id="notoolbox"/>

  <!-- test resuls are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml"
        style="height: 300px; overflow: auto;"/>
  
  <!-- test code goes here -->
  <script type="text/javascript"><![CDATA[
    const SPACER = /^spacer\d+/;
    const SEPARATOR = /^separator\d+/;
    const SPRING = /^spring\d+/;

    function testSet(aTb, aIDs, aResultIDs, aUseFixed) {
      // build a list of the fixed items in the order they appear on the toolbar
      var fixedSet = [];
      if (aUseFixed) {
        for (let i = 0; i < aTb.childNodes.length; i++) {
          var id = aTb.childNodes[i].id;
          if (id.indexOf("fixed") >= 0)
            fixedSet.push(id);
        }
      }

      var currentSet = aIDs.join(",");
      ok(currentSet, "setting currentSet: " + currentSet);
      aTb.currentSet = currentSet;
      var resultIDs = aResultIDs || aIDs;
      checkSet(aTb, resultIDs, fixedSet);
    }

    var checkSetCount = 0;
    function checkSet(aTb, aResultIDs, aFixedSet) {
      checkSetCount++;
      var testID = "checkSet(" + checkSetCount + ") ";

      for (let i = 0; i < aTb.childNodes.length; i++) {
        let id = aTb.childNodes[i].id;
        if (aResultIDs[i] instanceof RegExp) {
          ok(aResultIDs[i].test(id),
             testID + "correct ID " + aResultIDs[i] + " for toolbar " + aTb.id + "; got: " + id);
        }
        else if (aResultIDs[i] == "*") {
          is(id, aFixedSet.shift(), testID + "is fixed with ID " + id + " for toolbar " + aTb.id);
        }
        else {
          is(id, aResultIDs[i],
             testID + "correct ID " + aResultIDs[i] + " for toolbar " + aTb.id + 
             "****" + aResultIDs + "," + i + ",");
          // remove the item from the fixed set once found
          if (aFixedSet && id.indexOf("fixed") >= 0)
            aFixedSet.splice(aFixedSet.indexOf(id), 1);
        }
      }

      if (aFixedSet)
        is(aFixedSet.length, 0, testID + "extra fixed items for " + aTb.id);
      is(aTb.childNodes.length, aResultIDs.length,
         testID + "correct number of children for " + aTb.id);
    }

    function test_defaultSet() {
      checkSet($("tb1"), ["p1", "p2"]);
      checkSet($("tb2"), ["p4", "p3"]);
      checkSet($("tb3"), ["p5", "p6", "t31"]);
      checkSet($("tb4"), ["t41", "p7", "p8"]);
      checkSet($("tb5"), ["p9", "t51", "p10"]);
    }

    function test_currentSet(aTb) {
      ok(aTb, "have toolbar");
      var defaultSet = aTb.getAttribute("defaultset");
      var setLength = (defaultSet && defaultSet.split(",").length) || 0;
      is(setLength, aTb.childNodes.length, "correct # of children initially");

      var emptySet = [["__empty"], []];
      var testSets = [
         emptySet,
         [["p11"]],
         [["p11","p12"]],
         [["p11","p12","bogus"], ["p11","p12"]],
         [["p11"]],
         emptySet,
         [["spacer"],    [SPACER]],
         [["spring"],    [SPRING]],
         [["separator"], [SEPARATOR]],
         [["p11", "p11", "p12", "spacer", "p11"], ["p11", "p12", SPACER]],
         [["separator", "separator", "p11", "spring", "spacer"],
          [SEPARATOR, SEPARATOR, "p11", SPRING, SPACER]],
         [["separator", "spacer", "separator", "p11", "spring", "spacer", "p12", "spring"],
          [SEPARATOR, SPACER, SEPARATOR, "p11", SPRING, SPACER, "p12", SPRING]],
         emptySet
      ];

      cycleSets(aTb, testSets, emptySet, false);
    }

    function test_currentSet_nonremovable() {
      var tb = $("tb-test3");
      ok(tb, "have tb-test-3");

      // the * used in the tests below means that any fixed item can appear in that position
      var emptySet = [["__empty"], ["*", "*", "*"]];
      var testSets = [
        [["p1", "tb-fixed-1", "p2"],
         ["p1", "tb-fixed-1", "p2", "*", "*"]],
        [["p1", "tb-fixed-2", "p2"],
         ["p1", "tb-fixed-2", "p2", "*", "*"]],
        [["p1", "tb-fixed-3", "p2"],
         ["p1", "tb-fixed-3", "p2", "*", "*"]],
        emptySet,

        [["tb-fixed-1", "tb-fixed-2", "tb-fixed-3"],
         ["tb-fixed-1", "tb-fixed-2", "tb-fixed-3"]],
        [["tb-fixed-3", "tb-fixed-2", "tb-fixed-1"],
         ["tb-fixed-3", "tb-fixed-2", "tb-fixed-1"]],

        [["tb-fixed-1", "tb-fixed-2", "tb-fixed-3", "p1", "p2"],
         ["tb-fixed-1", "tb-fixed-2", "tb-fixed-3", "p1", "p2"]],

        [["tb-fixed-1", "p2", "p1"],
         ["tb-fixed-1", "p2", "p1", "*", "*"]],

        [["tb-fixed-1", "p2"],
         ["tb-fixed-1", "p2", "*", "*"]],

        [["p1", "p2"], ["p1", "p2", "*", "*", "*"]],
        [["p2", "p1"], ["p2", "p1", "*", "*", "*"]],

        [["tb-fixed-3", "spacer", "p1"],
         ["tb-fixed-3", SPACER, "p1", "*", "*"]]
      ];

      cycleSets(tb, testSets, emptySet, true);
    }

    function cycleSets(aTb, aSets, aEmptySet, aUseFixed) {
      // Since a lot of the tricky cases handled in the currentSet setter
      // depend on going from one state to another, run through the test set
      // multiple times in different orders.
      var length = aSets.length;

      for (var i = 0; i < length; i++) {
        testSet(aTb, aSets[i][0], aSets[i][1], aUseFixed);
      }
      for (var i = length - 1; i >= 0; i--) {
        testSet(aTb, aSets[i][0], aSets[i][1], aUseFixed);
      }
      for (var i = 0; i < length; i++) {
        testSet(aTb, aSets[i][0], aSets[i][1], aUseFixed);
        testSet(aTb, aSets[length - i - 1][0], aSets[length - i - 1][1], aUseFixed);
        testSet(aTb, aSets[i][0], aSets[i][1], aUseFixed);
        testSet(aTb, aSets[i][0], aSets[i][1], aUseFixed);
      }
      for (var i = 0; i < length; i++) {
        testSet(aTb, aEmptySet[0], aEmptySet[1], aUseFixed);
        testSet(aTb, aSets[i][0], aSets[i][1], aUseFixed);
      }
    }

    SimpleTest.waitForExplicitFinish();
    function startTest() {
      test_defaultSet();
      test_currentSet($("tb-test"));
      test_currentSet($("tb-test2"));
      test_currentSet_nonremovable();

      var toolbox = $("toolbox");
      var toolbars = document.getElementsByTagName("toolbar");
      for (var t = 0; t < toolbars.length; t++) {
        var toolbar = toolbars[t];
        is(toolbar.toolbox, toolbar.id == "notoolbox" ? null : toolbox,
           "toolbar " + toolbar.id + " has correct toolbox");
      }

      $("tb1").toolbox = document.documentElement;
      is($("tb1").toolbox, toolbox, "toolbox still correct after set");
      SimpleTest.finish();
    }
  ]]></script>
</window>