summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/test_richlist_direction.xul
blob: f94f1b3ba69ba68a80539a3756f3c649e955a220 (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
<?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 listbox direction
  -->
<window title="Listbox direction test"
        onload="test_richlistbox()"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"></script>

  <richlistbox seltype="multiple" id="richlistbox" flex="1" minheight="80" maxheight="80" height="80" />

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml" style="height: 300px; overflow: auto;"/>

<script type="application/javascript">
<![CDATA[

SimpleTest.waitForExplicitFinish();

var richListBox = document.getElementById("richlistbox");

function getScrollIndexAmount(aDirection) {
  return (4 * aDirection + richListBox.currentIndex);
}

function test_richlistbox()
{
  richListBox.minHeight = richListBox.maxHeight = richListBox.height =
    80 + (80 - richListBox.scrollBoxObject.height);
  var height = richListBox.scrollBoxObject.height;
  var item;
  do {
    item = richListBox.appendItem("Test", "");
    item.height = item.minHeight = item.maxHeight = Math.floor(height / 4);
  } while (item.getBoundingClientRect().bottom < (height * 2))
  richListBox.appendItem("Test", "");
  richListBox.firstChild.nextSibling.id = "list-box-first";
  richListBox.lastChild.previousSibling.id = "list-box-last";

  // direction = "reverse", the values here are backwards due to the fact that
  // richlistboxes respond differently when a user initiates a selection
  richListBox.dir = "reverse";
  var count = richListBox.itemCount;
  richListBox.focus();
  richListBox.selectedIndex = count - 1;
  sendKey("DOWN");
  is(richListBox.currentIndex, count - 2, "Selection should move to the next item");
  sendKey("UP");
  is(richListBox.currentIndex, count - 1, "Selection should move to the previous item");
  sendKey("END");
  is(richListBox.currentIndex, 0, "Selection should move to the last item");
  sendKey("HOME");
  is(richListBox.currentIndex, count - 1, "Selection should move to the first item");
  var currentIndex = richListBox.currentIndex;
  var index = getScrollIndexAmount(-1);
  sendKey("PAGE_DOWN");
  is(richListBox.currentIndex, index, "Selection should move to one page down");
  ok(richListBox.currentIndex < currentIndex, "Selection should move downwards");
  sendKey("END");
  currentIndex = richListBox.currentIndex;
  index = getScrollIndexAmount(1);
  sendKey("PAGE_UP");
  is(richListBox.currentIndex, index, "Selection should move to one page up");
  ok(richListBox.currentIndex > currentIndex, "Selection should move upwards");
  richListBox.selectedItem = richListBox.lastChild;
  richListBox.focus();
  synthesizeKey("KEY_ArrowDown", { shiftKey: true, code: "ArrowDown" }, window);
  let items = [richListBox.selectedItems[0],
               richListBox.selectedItems[1]];
  is(items[0], richListBox.lastChild, "The last element should still be selected");
  is(items[1], richListBox.lastChild.previousSibling, "Both elements should now be selected");
  richListBox.clearSelection();
  richListBox.selectedItem = richListBox.lastChild;
  sendMouseEvent({type: "click", shiftKey: true, clickCount: 1},
                 "list-box-last",
                 window);
  items = [richListBox.selectedItems[0],
           richListBox.selectedItems[1]];
  is(items[0], richListBox.lastChild, "The last element should still be selected");
  is(items[1], richListBox.lastChild.previousSibling, "Both elements should now be selected");
  richListBox.addEventListener("keypress", function(aEvent) {
    richListBox.removeEventListener("keypress", arguments.callee, true);
    aEvent.preventDefault();
  }, true);
  richListBox.selectedIndex = 1;
  sendKey("HOME");
  is(richListBox.selectedIndex, 1, "A stopped event should return indexing to normal");

  // direction = "normal"
  richListBox.dir = "normal";
  richListBox.selectedIndex = 0;
  sendKey("DOWN");
  is(richListBox.currentIndex, 1, "Selection should move to the next item");
  sendKey("UP");
  is(richListBox.currentIndex, 0, "Selection should move to the previous item");
  sendKey("END");
  is(richListBox.currentIndex, count - 1, "Selection should move to the last item");
  sendKey("HOME");
  is(richListBox.currentIndex, 0, "Selection should move to the first item");
  var currentIndex = richListBox.currentIndex;
  var index = richListBox.scrollOnePage(1);
  sendKey("PAGE_DOWN");
  is(richListBox.currentIndex, index, "Selection should move to one page down");
  ok(richListBox.currentIndex > currentIndex, "Selection should move downwards");
  sendKey("END");
  currentIndex = richListBox.currentIndex;
  index = richListBox.scrollOnePage(-1) + richListBox.currentIndex;
  sendKey("PAGE_UP");
  is(richListBox.currentIndex, index, "Selection should move to one page up");
  ok(richListBox.currentIndex < currentIndex, "Selection should move upwards");
  richListBox.selectedItem = richListBox.firstChild;
  richListBox.focus();
  synthesizeKey("KEY_ArrowDown", { shiftKey: true, code: "ArrowDown" }, window);
  items = [richListBox.selectedItems[0],
           richListBox.selectedItems[1]];
  is(items[0], richListBox.firstChild, "The last element should still be selected");
  is(items[1], richListBox.firstChild.nextSibling, "Both elements should now be selected");
  richListBox.clearSelection();
  richListBox.selectedItem = richListBox.firstChild;
  sendMouseEvent({type: "click", shiftKey: true, clickCount: 1},
                 "list-box-first",
                 window);
  items = [richListBox.selectedItems[0],
           richListBox.selectedItems[1]];
  is(items[0], richListBox.firstChild, "The last element should still be selected");
  is(items[1], richListBox.firstChild.nextSibling, "Both elements should now be selected");
  SimpleTest.finish();
}

]]>
</script>

</window>