summaryrefslogtreecommitdiff
path: root/editor/libeditor/tests/test_bug549262.html
blob: fa1cbabc4c317c1d43534d613eabea7a15ff57da (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=549262
-->
<head>
  <title>Test for Bug 549262</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=549262">Mozilla Bug 549262</a>
<p id="display"></p>
<div id="content">
</div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 549262 **/

var smoothScrollPref = "general.smoothScroll";
SimpleTest.waitForExplicitFinish();
var win = window.open("file_bug549262.html", "_blank", 
                      "width=600,height=600,scrollbars=yes");

// grab the timer right at the start
var cwu = SpecialPowers.getDOMWindowUtils(win);
function step() {
  cwu.advanceTimeAndRefresh(100);
}
SimpleTest.waitForFocus(function() {
  SpecialPowers.pushPrefEnv({"set":[[smoothScrollPref, false]]}, startTest);
}, win);
function startTest() {
  // Make sure that pressing Space when a contenteditable element is not focused
  // will scroll the page.
  var ed = win.document.getElementById("editor");
  var sc = win.document.querySelector("a");
  sc.focus();
  is(win.scrollY, 0, "Sanity check");
  synthesizeKey(" ", {}, win);

  step();

  isnot(win.scrollY, 0, "Page is scrolled down");
  is(ed.textContent, "abc", "The content of the editable element has not changed");
  var oldY = win.scrollY;
  synthesizeKey(" ", {shiftKey: true}, win);

  step();

  ok(win.scrollY < oldY, "Page is scrolled up");
  is(ed.textContent, "abc", "The content of the editable element has not changed");

  // Make sure that pressing Space when a contenteditable element is focused
  // will not scroll the page, and will edit the element.
  ed.focus();
  win.getSelection().collapse(ed.firstChild, 1);
  oldY = win.scrollY;
  synthesizeKey(" ", {}, win);

  step();

  ok(win.scrollY <= oldY, "Page is not scrolled down");
  is(ed.textContent, "a bc", "The content of the editable element has changed");
  sc.focus();
  synthesizeKey(" ", {}, win);

  step();

  isnot(win.scrollY, 0, "Page is scrolled down");
  is(ed.textContent, "a bc", "The content of the editable element has not changed");
  ed.focus();
  win.getSelection().collapse(ed.firstChild, 3);
  synthesizeKey(" ", {shiftKey: true}, win);

  step();

  isnot(win.scrollY, 0, "Page is not scrolled up");
  is(ed.textContent, "a b c", "The content of the editable element has changed");

  // Now let's test the down/up keys
  sc = document.body;

  step();

  ed.blur();
  sc.focus();
  oldY = win.scrollY;
  synthesizeKey("VK_UP", {}, win);

  step();

  ok(win.scrollY < oldY, "Page is scrolled up");
  oldY = win.scrollY;
  ed.focus();
  win.getSelection().collapse(ed.firstChild, 3);
  synthesizeKey("VK_UP", {}, win);

  step();

  is(win.scrollY, oldY, "Page is not scrolled up");
  is(win.getSelection().focusNode, ed.firstChild, "Correct element selected");
  is(win.getSelection().focusOffset, 0, "Selection should be moved to the beginning");
  win.getSelection().removeAllRanges();
  synthesizeMouse(sc, 300, 300, {}, win);
  synthesizeKey("VK_DOWN", {}, win);

  step();

  ok(win.scrollY > oldY, "Page is scrolled down");
  ed.focus();
  win.getSelection().collapse(ed.firstChild, 3);
  oldY = win.scrollY;
  synthesizeKey("VK_DOWN", {}, win);

  step();

  is(win.scrollY, oldY, "Page is not scrolled down");
  is(win.getSelection().focusNode, ed.firstChild, "Correct element selected");
  is(win.getSelection().focusOffset, ed.textContent.length, "Selection should be moved to the end");

  win.close();
  cwu.restoreNormalRefresh();

  SimpleTest.finish();
}
</script>
</pre>
</body>
</html>