summaryrefslogtreecommitdiffstats
path: root/dom/base/test/test_document_wireframe.html
blob: ddb6ea888d71acf32d29111b3ad5b733ce6c76ef (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Tests for document.getWireframe()</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <script>
    /**
     * This test creates some simple webpages, captures wireframes for them, and
     * then compares them against some expected wireframe structures.
     */

    /**
     * Converts some RGB values into the same uint32_t nsColor representation
     * that getWireframe uses.
     *
     * @param {Number} r
     *   Red color value.
     * @param {Number} g
     *   Green color value.
     * @param {Number} b
     *   Blue color value.
     * @returns {Number}
     *   The red, green and blue values composed in a single uint32_t-compatible
     *   value.
     */
    function nscolor(r, g, b) {
      return (255 << 24 | b << 16 | g << 8 | r) >>> 0;
    }

    const WHITE_NSCOLOR = nscolor(255, 255, 255);

    const RED_RGB = "rgb(255, 0, 0)";
    const RED_NSCOLOR = nscolor(255, 0, 0);
    const GREEN_RGB = "rgb(0, 255, 0)";
    const GREEN_NSCOLOR = nscolor(0, 255, 0);
    const BLUE_RGB = "rgb(0, 0, 255)";
    const BLUE_NSCOLOR = nscolor(0, 0, 255);
    const BLACK_RGB = "rgb(0, 0, 0)";
    const BLACK_NSCOLOR = nscolor(0, 0, 0);
    const BUILDER = "http://mochi.test:8888/document-builder.sjs?html=";
    const TEST_PATH = "http://mochi.test:8888/tests/dom/base/test/";

    /**
     * This array contains the definition of each test. Each test is an object
     * that expects two properties:
     *
     * {String} html
     *   The markup to be loaded in the page iframe that a wireframe will be
     *   generated for.
     * {Object} expectedWireframe
     *   An approximation of the wireframe that should be generated. The
     *   approximation is due to the fact that different platforms and
     *   execution environments might produce slightly different positioning
     *   of wireframe rects. We skip comparing the position of the rects, and
     *   only look at their dimensions (sometimes only one dimension if the
     *   other is potentially more variable - for example with text). Properties
     *   included in this object will do a strict comparison with the generated
     *   wireframe. Properties in the generated wireframe that are not in the
     *   expectedWireframe will be ignored.
     */
    const kTests = [{
      // Base case: a simple solid background with a single character in the
      // foreground.
      html: `
        <html>
        <style>
          body {
            width: 500px;
            height: 500px;
            background-color: ${RED_RGB};
            color: ${BLACK_RGB};
            overflow: hidden;
            font-size: 12px;
          }
        </style>
        <body>x</body>
        </html>
      `,
      expectedWireframe: {
        canvasBackground: RED_NSCOLOR,
        rects: [{
          color: BLACK_NSCOLOR,
          height: 12,
          type: "text",
        }]
      },
    }, {
      // Additional background on top of the main background.
      html: `
        <html>
        <style>
          body {
            background-color: ${RED_RGB};
            color: ${BLACK_RGB};
            overflow: hidden;
            font-size: 12px;
          }
          div {
            position: absolute;
            top: 0;
            left: 0;
            width: 20px;
            height: 20px;
            background-color: ${GREEN_RGB};
          }
        </style>
        <body>
          <div>x</div>
        </body>
        </html>
      `,
      expectedWireframe: {
        canvasBackground: RED_NSCOLOR,
        rects: [{
          color: GREEN_NSCOLOR,
          height: 20,
          width: 20,
          type: "background",
        }, {
          color: BLACK_NSCOLOR,
          height: 12,
          type: "text",
        }]
      },
    }, {
      // Image on top of the main background with another background
      // floating in the top right.
      html: `
        <html>
        <style>
          body {
            background-color: ${RED_RGB};
            color: ${BLACK_RGB};
            overflow: hidden;
            font-size: 12px;
          }
          div {
            position: absolute;
            top: 0;
            right: 0;
            width: 20px;
            height: 20px;
            background-color: ${GREEN_RGB};
          }
          img {
            position: absolute;
            top: 0;
            left: 0;
            height: 50px;
            width: 50px;
          }
        </style>
        <body>
          <img src="${TEST_PATH}/green.png"/>
          <div>x</div>
        </body>
        </html>
      `,
      expectedWireframe: {
        canvasBackground: RED_NSCOLOR,
        rects: [{
          color: 0,
          height: 50,
          width: 50,
          type: "image",
        }, {
          color: GREEN_NSCOLOR,
          height: 20,
          width: 20,
          type: "background",
        }, {
          color: BLACK_NSCOLOR,
          height: 12,
          type: "text",
        }]
      },
    }, {
      // Image on top of the main background with another background
      // floating over the image
      html: `
        <html>
        <style>
          body {
            width: 500px;
            height: 500px;
            background-color: ${RED_RGB};
            color: ${BLACK_RGB};
            overflow: hidden;
            font-size: 12px;
          }
          div {
            position: absolute;
            top: 0;
            left: 0;
            width: 20px;
            height: 20px;
            background-color: ${BLUE_RGB};
          }
          img {
            position: absolute;
            top: 0;
            left: 0;
            height: 50px;
            width: 50px;
          }
        </style>
        <body>
          <img src="${TEST_PATH}/green.png"/>
          <div>x</div>
        </body>
        </html>
      `,
      expectedWireframe: {
        canvasBackground: RED_NSCOLOR,
        rects: [{
          color: 0,
          height: 50,
          width: 50,
          type: "image",
        }, {
          color: BLUE_NSCOLOR,
          height: 20,
          width: 20,
          type: "background",
        }, {
          color: BLACK_NSCOLOR,
          height: 12,
          type: "text",
        }]
      },
    }, {
      // Bug 1759919 - Transformed items incorrectly causing us to not keep hit
      // testing stuff.
      html: `
        <!doctype html>
        <style>:root { background-color: white; } body { margin: 0 }</style>
        <div style="transform: rotate(90deg); width: 20px; height: 20px; overflow: clip;">
          <div style="width: 100%; height: 100%; background-color: blue;"></div>
        </div>
        <div style="transform: rotate(90deg); width: 20px; height: 20px; overflow: clip;">
          <div style="width: 100%; height: 100%; background-color: red;"></div>
        </div>
      `,
      expectedWireframe: {
        canvasBackground: WHITE_NSCOLOR,
        rects: [{
          color: RED_NSCOLOR,
          height: 20,
          width: 20,
          x: 0,
          y: 0,
          type: "background",
        }, {
          color: BLUE_NSCOLOR,
          height: 20,
          width: 20,
          x: 0,
          y: 20,
          type: "background",
        }],
      }
    }];

    /**
     * Returns a Promise once page has been loaded in frame.
     *
     * @param {Element} frame
     *   The iframe to load the page in.
     * @param {String} page
     *   The URL of the page to load in the frame.
     * @returns Promise
     * @resolves undefined
     *   Once the load event has fired for the frame.
     */
    function loadInIframe(frame, page) {
      return new Promise(resolve => {
        frame.addEventListener("load", resolve, { once: true });
        frame.src = page;
      });
    }

    /**
     * Compares a generated wireframe to an Object that contains some or all of
     * the expected structure of the generated wireframe.
     *
     * If the wireframe doesn't contain the expected number of rects, the
     * serialized structure of both the wireframe and approximateWireframe will
     * be dumped to stdout.
     *
     * @param {Wireframe} wireframe
     *   A wireframe generated via document.getWireframe()
     * @param {Object} approximateWireframe
     *   An object that closely resembles a wireframe but the rects in the
     *   rects property do not need to contain all of the properties expected
     *   in a WireframeTaggedRect. Skipped properties won't be checked.
     */
    function assertApproximateWireframe(wireframe, approximateWireframe) {
      is(
        wireframe.canvasBackground,
        approximateWireframe.canvasBackground,
        "Canvas backgrounds match."
      );
      is(
        wireframe.rects.length,
        approximateWireframe.rects.length,
        "Same rect count"
      );
      if (wireframe.rects.length != approximateWireframe.rects.length) {
        dump(
          "Generated wireframe: " + JSON.stringify(wireframe, null, "\t") + "\n"
        );
        dump(
          "Expected approximate wireframe: " +
            JSON.stringify(approximateWireframe, null, "\t") +
            "\n"
        );
      }

      for (let index = 0; index < approximateWireframe.length; ++index) {
        let wireframeRect = wireframe.rects[index];
        let approximationRect = approximateWireframe.rects[index];
        for (let prop of approximationRect) {
          is(
            wireframeRect[prop],
            approximationRect[prop],
            `Property ${prop} should be equal.`
          );
        }
      }
    }

    add_task(async () => {
      const iframe = document.getElementById("iframe");

      for (let testDefinition of kTests) {
        let pageURL = BUILDER + encodeURIComponent(testDefinition.html);
        await loadInIframe(iframe, pageURL);
        let wireframe = SpecialPowers.wrap(
          iframe.contentDocument
        ).getWireframe();
        assertApproximateWireframe(wireframe, testDefinition.expectedWireframe);
      }
    });
  </script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
<iframe id="iframe"></iframe>
</body>
</html>