summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_bug640321.html
blob: 984ea295ad6a00aa326725c58609e527f24f62d1 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=640321
-->
<head>
  <title>Test for Bug 640321</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=640321">Mozilla Bug 640321</a>
<p id="display"></p>
<div id="content" contenteditable style="text-align: center">
  <img src="green.png">
</div>
<div id="clickaway" style="width: 10px; height: 10px"></div>
<pre id="test">
<script type="application/javascript">

/** Test for Bug 640321 **/
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(function() {
  var img = document.querySelector("img");

  function cancel(e) { e.stopPropagation(); }
  var content = document.getElementById("content");
  content.addEventListener("mousedown", cancel, false);
  content.addEventListener("mousemove", cancel, false);
  content.addEventListener("mouseup", cancel, false);

  /**
   * This function is a generic resizer test.
   * We have 8 resizers that we'd like to test, and each can be moved in 8 different directions.
   * In specifying baseX, W can be considered to be the width of the image, and for baseY, H
   * can be considered to be the height of the image. deltaX and deltaY are regular pixel values
   * which can be positive or negative.
   */
  const W = 1;
  const H = 1;
  function testResizer(baseX, baseY, deltaX, deltaY, expectedDeltaX, expectedDeltaY) {
    ok(true, "testResizer(" + [baseX, baseY, deltaX, deltaY, expectedDeltaX, expectedDeltaY].join(", ") + ")");

    // Reset the dimensions of the image
    img.style.width = "100px";
    img.style.height = "100px";
    var rect = img.getBoundingClientRect();
    is(rect.width, 100, "Sanity check the length");
    is(rect.height, 100, "Sanity check the height");

    // Click on the image to show the resizers
    synthesizeMouseAtCenter(img, {});

    // Determine which resizer we're dealing with
    var basePosX = rect.width * baseX;
    var basePosY = rect.height * baseY;

    // Click on the correct resizer
    synthesizeMouse(img, basePosX, basePosY, {type: "mousedown"});
    // Drag it delta pixels to the right and bottom (or maybe left and top!)
    synthesizeMouse(img, basePosX + deltaX, basePosY + deltaY, {type: "mousemove"});
    // Release the mouse button
    synthesizeMouse(img, basePosX + deltaX, basePosY + deltaY, {type: "mouseup"});
    // Move the mouse delta more pixels to the same direction to make sure that the
    // resize operation has stopped.
    synthesizeMouse(img, basePosX + deltaX * 2, basePosY + deltaY * 2, {type: "mousemove"});
    // Click outside of the image to hide the resizers
    synthesizeMouseAtCenter(document.getElementById("clickaway"), {});

    // Get the new dimensions for the image
    var newRect = img.getBoundingClientRect();
    is(newRect.width, rect.width + expectedDeltaX, "The width should be increased by " + expectedDeltaX + " pixels");
    is(newRect.height, rect.height + expectedDeltaY, "The height should be increased by " + expectedDeltaY + "pixels");
  }

  function runTests(preserveRatio) {
    // Account for changes in the resizing behavior when we're trying to preserve
    // the aspect ration.
    // ignoredGrowth means we don't change the size of a dimension because otherwise
    // the aspect ratio would change undesirably.
    // needlessGrowth means that we change the size of a dimension perpendecular to
    // the mouse movement axis in order to preserve the aspect ratio.
    // reversedGrowth means that we change the size of a dimension in the opposite
    // direction to the mouse movement in order to maintain the aspect ratio.
    const ignoredGrowth = preserveRatio ? 0 : 1;
    const needlessGrowth = preserveRatio ? 1 : 0;
    const reversedGrowth = preserveRatio ? -1 : 1;

    // top resizer
    testResizer(W/2,   0, -10, -10,   0,  10);
    testResizer(W/2,   0, -10,   0,   0,   0);
    testResizer(W/2,   0, -10,  10,   0, -10);
    testResizer(W/2,   0,   0, -10,   0,  10);
    testResizer(W/2,   0,   0,   0,   0,   0);
    testResizer(W/2,   0,   0,  10,   0, -10);
    testResizer(W/2,   0,  10, -10,   0,  10);
    testResizer(W/2,   0,  10,   0,   0,   0);
    testResizer(W/2,   0,  10,  10,   0, -10);

    // top right resizer
    testResizer(  W,   0, -10, -10, -10 * reversedGrowth, 10);
    testResizer(  W,   0, -10,   0, -10 * ignoredGrowth,   0);
    testResizer(  W,   0, -10,  10, -10, -10);
    testResizer(  W,   0,   0, -10,  10 * needlessGrowth,  10);
    testResizer(  W,   0,   0,   0,   0,   0);
    testResizer(  W,   0,   0,  10,   0, -10 * ignoredGrowth);
    testResizer(  W,   0,  10, -10,  10,  10);
    testResizer(  W,   0,  10,   0,  10,  10 * needlessGrowth);
    testResizer(  W,   0,  10,  10,  10, -10 * reversedGrowth);

    // right resizer
    testResizer(  W, H/2, -10, -10, -10,   0);
    testResizer(  W, H/2, -10,   0, -10,   0);
    testResizer(  W, H/2, -10,  10, -10,   0);
    testResizer(  W, H/2,   0, -10,   0,   0);
    testResizer(  W, H/2,   0,   0,   0,   0);
    testResizer(  W, H/2,   0,  10,   0,   0);
    testResizer(  W, H/2,  10, -10,  10,   0);
    testResizer(  W, H/2,  10,   0,  10,   0);
    testResizer(  W, H/2,  10,  10,  10,   0);

    // bottom right resizer
    testResizer(  W,   H, -10, -10, -10, -10);
    testResizer(  W,   H, -10,   0, -10 * ignoredGrowth,   0);
    testResizer(  W,   H, -10,  10, -10 * reversedGrowth,  10);
    testResizer(  W,   H,   0, -10,   0, -10 * ignoredGrowth);
    testResizer(  W,   H,   0,   0,   0,   0);
    testResizer(  W,   H,   0,  10,  10 * needlessGrowth,  10);
    testResizer(  W,   H,  10, -10,  10, -10 * reversedGrowth);
    testResizer(  W,   H,  10,   0,  10,  10 * needlessGrowth);
    testResizer(  W,   H,  10,  10,  10,  10);

    // bottom resizer
    testResizer(W/2,   H, -10, -10,   0, -10);
    testResizer(W/2,   H, -10,   0,   0,   0);
    testResizer(W/2,   H, -10,  10,   0,  10);
    testResizer(W/2,   H,   0, -10,   0, -10);
    testResizer(W/2,   H,   0,   0,   0,   0);
    testResizer(W/2,   H,   0,  10,   0,  10);
    testResizer(W/2,   H,  10, -10,   0, -10);
    testResizer(W/2,   H,  10,   0,   0,   0);
    testResizer(W/2,   H,  10,  10,   0,  10);

    // bottom left resizer
    testResizer(  0,   H, -10, -10,  10, -10 * reversedGrowth);
    testResizer(  0,   H, -10,   0,  10,  10 * needlessGrowth);
    testResizer(  0,   H, -10,  10,  10,  10);
    testResizer(  0,   H,   0, -10,   0, -10 * ignoredGrowth);
    testResizer(  0,   H,   0,   0,   0,   0);
    testResizer(  0,   H,   0,  10,  10 * needlessGrowth,  10);
    testResizer(  0,   H,  10, -10, -10, -10);
    testResizer(  0,   H,  10,   0, -10 * ignoredGrowth,   0);
    testResizer(  0,   H,  10,  10, -10 * reversedGrowth,  10);

    // left resizer
    testResizer(  0, H/2, -10, -10,  10,   0);
    testResizer(  0, H/2, -10,   0,  10,   0);
    testResizer(  0, H/2, -10,  10,  10,   0);
    testResizer(  0, H/2,   0, -10,   0,   0);
    testResizer(  0, H/2,   0,   0,   0,   0);
    testResizer(  0, H/2,   0,  10,   0,   0);
    testResizer(  0, H/2,  10, -10, -10,   0);
    testResizer(  0, H/2,  10,   0, -10,   0);
    testResizer(  0, H/2,  10,  10, -10,   0);

    // top left resizer
    testResizer(  0,   0, -10, -10,  10,  10);
    testResizer(  0,   0, -10,   0,  10,  10 * needlessGrowth);
    testResizer(  0,   0, -10,  10,  10, -10 * reversedGrowth);
    testResizer(  0,   0,   0, -10,  10 * needlessGrowth,  10);
    testResizer(  0,   0,   0,   0,   0,   0);
    testResizer(  0,   0,   0,  10,   0, -10 * ignoredGrowth);
    testResizer(  0,   0,  10, -10, -10 * reversedGrowth,  10);
    testResizer(  0,   0,  10,   0, -10 * ignoredGrowth,   0);
    testResizer(  0,   0,  10,  10, -10, -10);
 }
 SpecialPowers.pushPrefEnv({"set": [["editor.resizing.preserve_ratio", false]]}, function() {
     runTests(false);
     SpecialPowers.pushPrefEnv({"set": [["editor.resizing.preserve_ratio", true]]}, function() {
       runTests(true);
       SimpleTest.finish();
     });
   });
 });

</script>
</pre>
</body>
</html>