summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/test_scrollbar.xhtml
blob: c16e9f2980f4d2c77dc0dfd3eb424f0c8ef1d18b (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
<?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"?>
<!--
  XUL Widget Test for scrollbars
  -->
<window title="Scrollbar"
  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml"/>
  
  <hbox>
    <scrollbar orient="horizontal"
               id="scroller"
               curpos="0"
               maxpos="600"
               pageincrement="400"
               style="width: 500px; margin: 0"/>
  </hbox>
  
  <!-- test code goes here -->
  <script type="application/javascript"><![CDATA[

/** Test for Scrollbar **/
var scrollbarTester = {
  scrollbar: null,
  middlePref: false,
  startTest() {
    this.scrollbar = $("scroller");
    this.middlePref = this.getMiddlePref();
    var self = this;
    [0, 1, 2].map(function(button) {
      [false, true].map(function(alt) {
        [false, true].map(function(shift) {
          self.testThumbDragging(button, alt, shift);
        })
      })
    });
    SimpleTest.finish();
  },
  testThumbDragging(button, withAlt, withShift) {
    this.reset();
    var x = 160; // on the right half of the thumb
    var y = 5;

    var isMac = navigator.platform.includes("Mac");
    let runtime = SpecialPowers.Services.appinfo;
    var isGtk = runtime.widgetToolkit.includes("gtk");
    
    // Start the drag.
    this.mousedown(x, y, button, withAlt, withShift);
    var newPos = this.getPos();
    var scrollToClick = (newPos != 0);
    if (isMac || isGtk) {
      ok(!scrollToClick, "On Linux and Mac OS X, clicking the scrollbar thumb "+
                         "should never move it.");
    } else if (button == 0 && withShift) {
      ok(scrollToClick, "On platforms other than Linux and Mac OS X, holding "+
                        "shift should enable scroll-to-click on the scrollbar thumb.");
    } else if (button == 1 && this.middlePref) {
      ok(scrollToClick, "When middlemouse.scrollbarPosition is on, clicking the "+
                        "thumb with the middle mouse button should center it "+
                        "around the cursor.")
    }

    // Move one pixel to the right.
    this.mousemove(x+1, y, button, withAlt, withShift);
    var newPos2 = this.getPos();
    if (newPos2 != newPos) {
      ok(newPos2 > newPos, "Scrollbar thumb should follow the mouse when dragged.");
      ok(newPos2 - newPos < 3, "Scrollbar shouldn't move further than the mouse when dragged.");
      ok(button == 0 || (button == 1 && this.middlePref) || (button == 2 && isGtk),
         "Dragging the scrollbar should only be possible with the left mouse button.");
    } else if (button == 0) {
      // Dragging had no effect.
      ok(false, "Dragging the scrollbar thumb should work.");
    } else if (button == 1 && this.middlePref && (!isGtk && !isMac)) {
      ok(false, "When middlemouse.scrollbarPosition is on, dragging the "+
                "scrollbar thumb should be possible using the middle mouse button.");
    } else {
      ok(true, "Dragging works correctly.");
    }

    // Release the mouse button.
    this.mouseup(x+1, y, button, withAlt, withShift);
    var newPos3 = this.getPos();
    ok(newPos3 == newPos2,
       "Releasing the mouse button after dragging the thumb shouldn't move it.");
  },
  getMiddlePref() {
    // It would be better to test with different middlePref settings,
    // but the setting is only queried once, at browser startup, so
    // changing it here wouldn't have any effect
    var mouseBranch = SpecialPowers.Services.prefs.getBranch("middlemouse.");
    return mouseBranch.getBoolPref("scrollbarPosition");
  },
  setPos(pos) {
    this.scrollbar.setAttribute("curpos", pos);
  },
  getPos() {
    return this.scrollbar.getAttribute("curpos");
  },
  reset() {
    this.setPos(0);
  },
  mousedown(x, y, button, alt, shift) {
    synthesizeMouse(this.scrollbar, x, y, { type: "mousedown", 'button': button,
                                                altKey: alt, shiftKey: shift });
  },
  mousemove(x, y, button, alt, shift) {
    synthesizeMouse(this.scrollbar, x, y, { type: "mousemove", 'button': button,
                                                altKey: alt, shiftKey: shift });
  },
  mouseup(x, y, button, alt, shift) {
    synthesizeMouse(this.scrollbar, x, y, { type: "mouseup", 'button': button,
                                                altKey: alt, shiftKey: shift });
  }
}

function doTest() {
  setTimeout(function() { scrollbarTester.startTest(); }, 0);
}

SimpleTest.waitForExplicitFinish();
addLoadEvent(doTest);

]]></script>
</window>