summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/test_mousescroll.xul
blob: 91ccf56831d154f668f1bb11311c83aaba783346 (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
<?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"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=378028
-->
<window title="Mozilla Bug 378028"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/paint_listener.js"/>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=378028"
     target="_blank">Mozilla Bug 378028</a>
  </body>

  <!-- richlistbox currently has no way of giving us a defined number of
       rows, so we just choose an arbitrary height limit that should give
       us plenty of vertical scrollability -->
  <richlistbox id="richlistbox" style="height:50px;">
    <richlistitem id="richlistbox_item0" hidden="true"><label value="Item 0"/></richlistitem>
    <richlistitem id="richlistbox_item1"><label value="Item 1"/></richlistitem>
    <richlistitem id="richlistbox_item2"><label value="Item 2"/></richlistitem>
    <richlistitem id="richlistbox_item3"><label value="Item 3"/></richlistitem>
    <richlistitem id="richlistbox_item4"><label value="Item 4"/></richlistitem>
    <richlistitem id="richlistbox_item5"><label value="Item 5"/></richlistitem>
    <richlistitem id="richlistbox_item6"><label value="Item 6"/></richlistitem>
    <richlistitem id="richlistbox_item7"><label value="Item 7"/></richlistitem>
    <richlistitem id="richlistbox_item8"><label value="Item 8"/></richlistitem>
  </richlistbox>

  <listbox id="listbox" rows="2">
    <listitem id="listbox_item0" label="Item 0" hidden="true"/>
    <listitem id="listbox_item1" label="Item 1"/>
    <listitem id="listbox_item2" label="Item 2"/>
    <listitem id="listbox_item3" label="Item 3"/>
    <listitem id="listbox_item4" label="Item 4"/>
    <listitem id="listbox_item5" label="Item 5"/>
    <listitem id="listbox_item6" label="Item 6"/>
    <listitem id="listbox_item7" label="Item 7"/>
    <listitem id="listbox_item8" label="Item 8"/>
  </listbox>

  <box orient="horizontal">
    <arrowscrollbox id="hscrollbox" clicktoscroll="true" orient="horizontal"
     smoothscroll="false" style="max-width:80px;" flex="1">
      <hbox style="width:40px; height:20px; background:black;" hidden="true"/>
      <hbox style="width:40px; height:20px; background:white;"/>
      <hbox style="width:40px; height:20px; background:black;"/>
      <hbox style="width:40px; height:20px; background:white;"/>
      <hbox style="width:40px; height:20px; background:black;"/>
      <hbox style="width:40px; height:20px; background:white;"/>
      <hbox style="width:40px; height:20px; background:black;"/>
      <hbox style="width:40px; height:20px; background:white;"/>
      <hbox style="width:40px; height:20px; background:black;"/>
    </arrowscrollbox>
  </box>

  <arrowscrollbox id="vscrollbox" clicktoscroll="true" orient="vertical"
   smoothscroll="false" style="max-height:80px;" flex="1">
      <vbox style="width:100px; height:40px; background:black;" hidden="true"/>
      <vbox style="width:100px; height:40px; background:white;"/>
      <vbox style="width:100px; height:40px; background:black;"/>
      <vbox style="width:100px; height:40px; background:white;"/>
      <vbox style="width:100px; height:40px; background:black;"/>
      <vbox style="width:100px; height:40px; background:white;"/>
      <vbox style="width:100px; height:40px; background:black;"/>
      <vbox style="width:100px; height:40px; background:white;"/>
      <vbox style="width:100px; height:40px; background:black;"/>
      <vbox style="width:100px; height:40px; background:white;"/>
      <vbox style="width:100px; height:40px; background:black;"/>
  </arrowscrollbox>

  <!-- test code goes here -->
  <script type="application/javascript"><![CDATA[

/** Test for Bug 378028 **/
/*   and for Bug 350471 **/
var smoothScrollPref = "general.smoothScroll";
SpecialPowers.setBoolPref(smoothScrollPref, false);
SimpleTest.waitForExplicitFinish();

const deltaModes = [
  WheelEvent.DOM_DELTA_PIXEL,  // 0
  WheelEvent.DOM_DELTA_LINE,   // 1
  WheelEvent.DOM_DELTA_PAGE    // 2
];

function testListbox(id)
{
  var listbox = document.getElementById(id);

  function helper(aStart, aDelta, aIntDelta, aDeltaMode)
  {
    listbox.scrollToIndex(aStart);
    synthesizeWheel(listbox, 10, 10,
                    { deltaMode: aDeltaMode, deltaY: aDelta,
                      lineOrPageDeltaY: aIntDelta });
    var expectedPos = aStart;
    if (aIntDelta) {
      if (aDeltaMode == WheelEvent.DOM_DELTA_PAGE) {
        expectedPos += aIntDelta > 0 ? listbox.getNumberOfVisibleRows() :
                                       -listbox.getNumberOfVisibleRows();
      } else {
        expectedPos += aIntDelta;
      }
    }
    is(listbox.getIndexOfFirstVisibleRow(), expectedPos,
       "testListbox(" + id +  "): vertical, starting " + aStart +
         " delta " + aDelta + " lineOrPageDelta " + aIntDelta +
         " aDeltaMode " + aDeltaMode);

    // Check that horizontal scrolling has no effect
    listbox.scrollToIndex(aStart);
    synthesizeWheel(listbox, 10, 10,
                    { deltaMode: aDeltaMode, deltaX: aDelta,
                      lineOrPageDeltaX: aIntDelta });
    is(listbox.getIndexOfFirstVisibleRow(), aStart,
       "testListbox(" + id +  "): horizontal, starting " + aStart +
         " delta " + aDelta + " lineOrPageDelta " + aIntDelta +
         " aDeltaMode " + aDeltaMode);
  }
  deltaModes.forEach(function(aDeltaMode) {
    let delta = (aDeltaMode == WheelEvent.DOM_DELTA_PIXEL) ? 5.0 : 0.3;
    helper(5, -delta,  0, aDeltaMode);
    helper(5, -delta, -1, aDeltaMode);
    helper(5,  delta,  1, aDeltaMode);
    helper(5,  delta,  0, aDeltaMode);
  });
}

function testRichListbox(id, andThen)
{
  var listbox = document.getElementById(id);
  var tests = [];

  var winUtils = SpecialPowers.getDOMWindowUtils(window);
  winUtils.advanceTimeAndRefresh(100);

  function nextTest() {
    var [aStart, aDelta, aIntDelta, aDeltaMode] = tests.shift();
    listbox.scrollToIndex(aStart);

    let event = {
      deltaMode: aDeltaMode,
      deltaY: aDelta,
      lineOrPageDeltaY: aIntDelta
    };
    sendWheelAndPaint(listbox, 10, 10, event, function() {
      var change = listbox.getIndexOfFirstVisibleRow() - aStart;
      var direction = (change > 0) - (change < 0);
      var expected = (aDelta > 0) - (aDelta < 0);
      is(direction, expected,
       "testRichListbox(" + id +  "): vertical, starting " + aStart +
         " delta " + aDelta + " lineOrPageDeltaY " + aIntDelta +
         " aDeltaMode " + aDeltaMode);

      // Check that horizontal scrolling has no effect
      let event = {
        deltaMode: aDeltaMode,
        deltaX: aDelta,
        lineOrPageDeltaX: aIntDelta
      };

      listbox.scrollToIndex(aStart);
      sendWheelAndPaint(listbox, 10, 10, event, function() {
        is(listbox.getIndexOfFirstVisibleRow(), aStart,
           "testRichListbox(" + id +  "): horizontal, starting " + aStart +
             " delta " + aDelta + " lineOrPageDeltaX " + aIntDelta +
             " aDeltaMode " + aDeltaMode);

        if (!tests.length) {
          winUtils.restoreNormalRefresh();
          andThen();
          return;
        }

        nextTest();
      });
    });
  }

  // richlistbox currently uses native XUL scrolling, so the "line"
  // amounts don't necessarily correspond 1-to-1 with listbox items. So
  // we just check that scrolling up/down scrolls in the right direction.
  deltaModes.forEach(function(aDeltaMode) {
    let delta = (aDeltaMode == WheelEvent.DOM_DELTA_PIXEL) ? 32.0 : 2.0;
    tests.push([5, -delta, -1, aDeltaMode]);
    tests.push([5, -delta,  0, aDeltaMode]);
    tests.push([5,  delta,  1, aDeltaMode]);
    tests.push([5,  delta,  0, aDeltaMode]);
  });

  nextTest();
}

function testArrowScrollbox(id)
{
  var scrollbox = document.getElementById(id);
  var scrollBoxObject = scrollbox.scrollBoxObject;
  var orient = scrollbox.getAttribute("orient");

  function helper(aStart, aDelta, aDeltaMode, aExpected)
  {
    var lineOrPageDelta = (aDeltaMode == WheelEvent.DOM_DELTA_PIXEL) ? aDelta / 10 : aDelta;
    var orientIsHorizontal = (orient == "horizontal");

    scrollBoxObject.scrollTo(aStart, aStart);

    for (var i = orientIsHorizontal ? 2 : 0; i >= 0; i--) {
      synthesizeWheel(scrollbox, 5, 5,
                      { deltaMode: aDeltaMode, deltaY: aDelta,
                        lineOrPageDeltaY: lineOrPageDelta });

      var pos = orientIsHorizontal ? scrollBoxObject.positionX :
                                     scrollBoxObject.positionY;

      // Note, vertical mouse scrolling is allowed to scroll horizontal
      // arrowscrollboxes, because many users have no horizontal mouse scroll
      // capability
      let expected = !i ? aExpected : aStart;
      is(pos, expected,
         "testArrowScrollbox(" + id +  "): vertical, starting " + aStart +
           " delta " + aDelta + " lineOrPageDelta " + lineOrPageDelta +
           " aDeltaMode " + aDeltaMode);
    }

    scrollBoxObject.scrollTo(aStart, aStart);
    for (var i = orientIsHorizontal ? 2 : 0; i >= 0; i--) {
      synthesizeWheel(scrollbox, 5, 5,
                      { deltaMode: aDeltaMode, deltaX: aDelta,
                        lineOrPageDeltaX: lineOrPageDelta });
      // horizontal mouse scrolling is never allowed to scroll vertical
      // arrowscrollboxes
      var pos = orientIsHorizontal ? scrollBoxObject.positionX :
                                     scrollBoxObject.positionY;
      let expected = (!i && orientIsHorizontal) ? aExpected : aStart;
      is(pos, expected,
         "testArrowScrollbox(" + id +  "): horizontal, starting " + aStart +
           " delta " + aDelta + " lineOrPageDelta " + lineOrPageDelta +
           " aDeltaMode " + aDeltaMode);
    }
  }

  var scrolledWidth = scrollBoxObject.scrolledWidth;
  var scrolledHeight = scrollBoxObject.scrolledHeight;
  var scrollMaxX = scrolledWidth - scrollBoxObject.width;
  var scrollMaxY = scrolledHeight - scrollBoxObject.height;
  var scrollMax = orient == "horizontal" ? scrollMaxX : scrollMaxY;

  deltaModes.forEach(function(aDeltaMode) {
    helper(50, -1000, aDeltaMode, 0);
    helper(50,  1000, aDeltaMode, scrollMax);
    helper(50,     0, aDeltaMode, 50);
    helper(50,     0, aDeltaMode, 50);
  });
}

function runTests()
{
  testRichListbox("richlistbox", function() {
    testListbox("listbox");
    testArrowScrollbox("hscrollbox");
    testArrowScrollbox("vscrollbox");
    SpecialPowers.clearUserPref(smoothScrollPref);
    SimpleTest.finish();
  });
}

window.onload = function() { setTimeout(runTests, 0); };
  ]]></script>
</window>