summaryrefslogtreecommitdiffstats
path: root/dom/plugins/test/mochitest/test_object.html
blob: 093333ea5e0e9adf0982805150b6c9276ef5774d (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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
<!DOCTYPE html>
<html>
  <head>
    <title>Plugin instantiation</title>
    <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
    <script type="application/javascript" src="/tests/SimpleTest/SpecialPowers.js"></script>
    <script type="application/javascript" src="plugin-utils.js"></script>
    <meta charset="utf-8">
  <body onload="onLoad()">
    <script class="testbody" type="text/javascript;version=1.8">

      "use strict";
      SimpleTest.waitForExplicitFinish();

      setTestPluginEnabledState(SpecialPowers.Ci.nsIPluginTag.STATE_ENABLED, "Test Plug-in");

      // This can go away once embed also is on WebIDL
      let OBJLC = SpecialPowers.Ci.nsIObjectLoadingContent;

      // Use string modes in this test to make the test easier to read/debug.
      // nsIObjectLoadingContent refers to this as "type", but I am using "mode"
      // in the test to avoid confusing with content-type.
      let prettyModes = {};
      prettyModes[OBJLC.TYPE_LOADING] = "loading";
      prettyModes[OBJLC.TYPE_IMAGE] = "image";
      prettyModes[OBJLC.TYPE_PLUGIN] = "plugin";
      prettyModes[OBJLC.TYPE_DOCUMENT] = "document";
      prettyModes[OBJLC.TYPE_NULL] = "none";

      let body = document.body;
      // A single-pixel white png
      let testPNG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3AoIFiETNqbNRQAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAAACklEQVQIHWP4DwABAQEANl9ngAAAAABJRU5ErkJggg==';
      // An empty, but valid, SVG
      let testSVG = 'data:image/svg+xml,<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" width="100" height="100"></svg>';
      // executeSoon wrapper to count pending callbacks
      let pendingCalls = 0;
      let afterPendingCalls = false;

      function runWhenDone(func) {
        if (!pendingCalls)
          func();
        else
          afterPendingCalls = func;
      }
      function runSoon(func) {
        pendingCalls++;
        SimpleTest.executeSoon(function() {
          func();
          if (--pendingCalls < 1 && afterPendingCalls)
            afterPendingCalls();
        });
      }
      function src(obj, state, uri) {
        // If we have a running plugin, src changing should always throw it out,
        // even if it causes us to load the same plugin again.
        if (uri && runningPlugin(obj, state)) {
          if (!state.oldPlugins)
            state.oldPlugins = [];
          try {
            state.oldPlugins.push(obj.getObjectValue());
          } catch (e) {
            ok(false, "Running plugin but cannot call getObjectValue?");
          }
        }

        var srcattr;
        if (state.tagName == "object")
          srcattr = "data";
        else if (state.tagName == "embed")
          srcattr = "src";
        else
          ok(false, "Internal test fail: Why are we setting the src of an applet");

        // Plugins should always go away immediately on src/data change
        state.initialPlugin = false;
        if (uri === null) {
          removeAttr(obj, srcattr);
          // TODO Bug 767631 - we don't trigger loadObject on UnsetAttr :(
          forceReload(obj, state);
        } else {
          setAttr(obj, srcattr, uri);
        }
      }
      // We have to be careful not to reach past the nsObjectLoadingContent
      // prototype to touch generic element attributes, as this will try to
      // spawn the plugin, breaking our ability to test for that.
      function getAttr(obj, attr) {
        return document.body.constructor.prototype.getAttribute.call(obj, attr);
      }
      function setAttr(obj, attr, val) {
        return document.body.constructor.prototype.setAttribute.call(obj, attr, val);
      }
      function hasAttr(obj, attr) {
        return document.body.constructor.prototype.hasAttribute.call(obj, attr);
      }
      function removeAttr(obj, attr) {
        return document.body.constructor.prototype.removeAttribute.call(obj, attr);
      }
      function setDisplayed(obj, display) {
        if (display)
          removeAttr(obj, 'style');
        else
          setAttr(obj, 'style', "display: none;");
      }
      function displayed(obj) {
        // Hacky, but that's all we use style for.
        return !hasAttr(obj, 'style');
      }
      function actualType(obj, state) {
        return state.getActualType.call(obj);
      }
      function getMode(obj, state) {
        return prettyModes[state.getDisplayedType.call(obj)];
      }
      function runningPlugin(obj, state) {
        return state.getHasRunningPlugin.call(obj);
      }

      // TODO this is hacky and might hide some failures, but is needed until
      // Bug 767635 lands -- which itself will probably mean tweaking this test.
      function forceReload(obj, state) {
        let attr;
        if (state.tagName == "object")
          attr = "data";
        else if (state.tagName == "embed")
          attr = "src";

        if (attr && hasAttr(obj, attr)) {
          src(obj, state, getAttr(obj, attr));
        } else if (body.contains(obj)) {
          body.appendChild(obj);
        } else {
          // Out of document nodes without data attributes simply can't be
          // reloaded currently. Bug 767635
        }
      };

      // Make a list of combinations of sub-lists, e.g.:
      // [ [a, b], [c, d] ]
      // ->
      // [ [a, c], [a, d], [b, c], [b, d] ]
      function eachList() {
        let all = [];
        if (!arguments.length)
          return all;
        let list = Array.prototype.slice.call(arguments, 0);
        for (let c of list[0]) {
          if (list.length > 1) {
            for (let x of eachList.apply(this,list.slice(1))) {
              all.push((c.length ? [c] : []).concat(x));
            }
          } else if (c.length) {
            all.push([c]);
          }
        }
        return all;
      }

      let states = {
        svg: function(obj, state) {
          removeAttr(obj, "type");
          src(obj, state, testSVG);
          state.noChannel = false;
          state.expectedType = "image/svg";
          // SVGs are actually image-like subdocuments
          state.expectedMode = "document";
        },
        image: function(obj, state) {
          removeAttr(obj, "type");
          src(obj, state, testPNG);
          state.noChannel = false;
          state.expectedMode = "image";
          state.expectedType = "image/png";
        },
        plugin: function(obj, state) {
          removeAttr(obj, "type");
          src(obj, state, "data:application/x-test,foo");
          state.noChannel = false;
          state.expectedType = "application/x-test";
          state.expectedMode = "plugin";
        },
        pluginExtension: function(obj, state) {
          src(obj, state, "./fake_plugin.tst");
          state.expectedMode = "plugin";
          state.pluginExtension = true;
          state.noChannel = false;
        },
        document: function(obj, state) {
          removeAttr(obj, "type");
          src(obj, state, "data:text/plain,I am a document");
          state.noChannel = false;
          state.expectedType = "text/plain";
          state.expectedMode = "document";
        },
        fallback: function(obj, state) {
          removeAttr(obj, "type");
          state.expectedType = "application/x-unknown";
          state.expectedMode = "none";
          state.noChannel = true;
          src(obj, state, null);
        },
        addToDoc: function(obj, state) {
          body.appendChild(obj);
        },
        removeFromDoc: function(obj, state) {
          if (body.contains(obj))
            body.removeChild(obj);
        },
        // Set the proper type
        setType: function(obj, state) {
          if (state.expectedType) {
            state.badType = false;
            setAttr(obj, 'type', state.expectedType);
            forceReload(obj, state);
          }
        },
        // Set an improper type
        setWrongType: function(obj, state) {
          // This should break no-channel-plugins but nothing else
          state.badType = true;
          setAttr(obj, 'type', "application/x-unknown");
          forceReload(obj, state);
        },
        // Set a plugin type
        setPluginType: function(obj, state) {
          // If an object/embed has a type set to a plugin type, it should not
          // use the channel type.
          state.badType = false;
          setAttr(obj, 'type', 'application/x-test');
          state.expectedType = "application/x-test";
          state.expectedMode = "plugin";
          forceReload(obj, state);
        },
        noChannel: function(obj, state) {
          src(obj, state, null);
          state.noChannel = true;
          state.pluginExtension = false;
        },
        displayNone: function(obj, state) {
          setDisplayed(obj, false);
        },
        displayInherit: function(obj, state) {
          setDisplayed(obj, true);
        }
      };


      function testObject(obj, state) {
        // If our test combination both sets noChannel but no explicit type
        // it shouldn't load ever.
        let expectedMode = state.expectedMode;
        let actualMode = getMode(obj, state);

        if (state.noChannel && !getAttr(obj, 'type')) {
          // Some combinations of test both set no type and no channel. This is
          // worth testing with the various combinations, but shouldn't load.
          expectedMode = "none";
        }

        // Embed tags should always try to load a plugin by type or extension
        // before falling back to opening a channel. See bug 803159
        if (state.tagName == "embed" &&
            (getAttr(obj, 'type') == "application/x-test" || state.pluginExtension)) {
          state.noChannel = true;
        }

        // with state.loading, unless we're loading with no channel, these types
        // should still be in loading state pending a channel.
        if (state.loading && (expectedMode == "image" || expectedMode == "document" ||
                             (expectedMode == "plugin" && !state.initialPlugin && !state.noChannel))) {
          expectedMode = "loading";
        }

        // With the exception of plugins with a proper type, nothing should
        // load without a channel
        if (state.noChannel && (expectedMode != "plugin" || state.badType) &&
            body.contains(obj)) {
          expectedMode = "none";
        }

        // embed tags should reject documents, except for SVG images which
        // render as such
        if (state.tagName == "embed" && expectedMode == "document" &&
            actualType(obj, state) != "image/svg+xml") {
          expectedMode = "none";
        }

        // Embeds with a plugin type should skip opening a channel prior to
        // loading, taking only type into account.
        if (state.tagName == 'embed' && getAttr(obj, 'type') == 'application/x-test' &&
            body.contains(obj)) {
          expectedMode = "plugin";
        }

        if (!body.contains(obj)
            && (!state.loading || expectedMode != "image")
            && (!state.initialPlugin || expectedMode != "plugin")) {
          // Images are handled by nsIImageLoadingContent so we dont track
          // their state change as they're detached and reattached. All other
          // types switch to state "loading", and are completely unloaded
          expectedMode = "loading";
        }

        is(actualMode, expectedMode, "check loaded mode");

        // If we're a plugin, check that we spawned successfully. state.loading
        // is set if we haven't had an event loop since applying state, in which
        // case the plugin would not have stopped yet if it was initially a
        // plugin.
        let shouldBeSpawnable = expectedMode == "plugin" && displayed(obj);
        let shouldSpawn = shouldBeSpawnable && (!state.loading || state.initialPlugin);
        let didSpawn = runningPlugin(obj, state);
        is(didSpawn, !!shouldSpawn, "check plugin spawned is " + !!shouldSpawn);

        // If we are a plugin, scripting should work. If we're not spawned we
        // should spawn synchronously.
        let scripted = false;
        try {
          let x = obj.getObjectValue();
          scripted = true;
        } catch(e) {}
        is(scripted, shouldBeSpawnable, "check plugin scriptability");

        // If this tag previously had other spawned plugins, make sure it
        // respawned between then and now
        if (state.oldPlugins && didSpawn) {
          let didRespawn = false;
          for (let oldp of state.oldPlugins) {
            // If this returns false or throws, it's not the same plugin
            try {
              didRespawn = !obj.checkObjectValue(oldp);
            } catch (e) {
              didRespawn = true;
            }
          }
          is(didRespawn, true, "Plugin should have re-spawned since old state ("+state.oldPlugins.length+")");
        }
      }

      let total = 0;
      let test_modes = {
        // Just apply from_state then to_state
        "immediate": function(obj, from_state, to_state, state) {
          for (let from of from_state)
            states[from](obj, state);
          for (let to of to_state)
            states[to](obj, state);

          // We don't spin the event loop between applying to_state and
          // running tests, so some types are still loading
          state.loading = true;
          info("["+(++total)+"] Testing [ " + from_state + " ] -> [ " + to_state + " ] / " + state.tagName + " / immediate");
          testObject(obj, state);

          if (body.contains(obj))
            body.removeChild(obj);

        },
        // Apply states, spin event loop, run tests.
        "cycle": function(obj, from_state, to_state, state) {
          for (let from of from_state)
            states[from](obj, state);
          for (let to of to_state)
            states[to](obj, state);
          // Because re-appending to the document creates a script blocker, but
          // plugins spawn asynchronously, we need to return to the event loop
          // twice to ensure the plugin has been given a chance to lazily spawn.
          runSoon(function() { runSoon(function() {
            info("["+(++total)+"] Testing [ " + from_state + " ] -> [ " + to_state + " ] / " + state.tagName + " / cycle");
            testObject(obj, state);

            if (body.contains(obj))
              body.removeChild(obj);
          }); });
        },
        // Apply initial state, spin event loop, apply final state, spin event
        // loop again.
        "cycleboth": function(obj, from_state, to_state, state) {
          for (let from of from_state) {
            states[from](obj, state);
          }
          runSoon(function() {
            for (let to of to_state) {
              states[to](obj, state);
            }
            // Because re-appending to the document creates a script blocker,
            // but plugins spawn asynchronously, we need to return to the event
            // loop twice to ensure the plugin has been given a chance to lazily
            // spawn.
            runSoon(function() { runSoon(function() {
              info("["+(++total)+"] Testing [ " + from_state + " ] -> [ " + to_state + " ] / " + state.tagName + " / cycleboth");
              testObject(obj, state);

              if (body.contains(obj))
                body.removeChild(obj);
            }); });
          });
        },
        // Apply initial state, spin event loop, apply later state, test
        // immediately
        "cyclefirst": function(obj, from_state, to_state, state) {
          for (let from of from_state) {
            states[from](obj, state);
          }
          runSoon(function() {
            state.initialPlugin = runningPlugin(obj, state);
            for (let to of to_state) {
              states[to](obj, state);
            }
            info("["+(++total)+"] Testing [ " + from_state + " ] -> [ " + to_state + " ] / " + state.tagName + " / cyclefirst");
            // We don't spin the event loop between applying to_state and
            // running tests, so some types are still loading
            state.loading = true;
            testObject(obj, state);

            if (body.contains(obj))
              body.removeChild(obj);
          });
        },
      };

      function test(testdat) {
        // FIXME bug 1291854: Change back to lets when the test is fixed.
        for (var from_state of testdat['from_states']) {
          for (var to_state of testdat['to_states']) {
            for (var mode of testdat['test_modes']) {
              for (var type of testdat['tag_types']) {
                runSoon(function () {
                  let obj = document.createElement(type);
                  obj.width = 1; obj.height = 1;
                  let state = {};
                  state.noChannel = true;
                  state.tagName = type;
                  // Part of the test checks whether a plugin spawned or not,
                  // but touching the object prototype will attempt to
                  // synchronously spawn a plugin!  We use this terrible hack to
                  // get a privileged getter for the attributes we want to touch
                  // prior to applying any attributes.
                  // TODO when embed goes away we wont need to check for
                  //      QueryInterface any longer.
                  var lookup_on = obj.QueryInterface ? obj.QueryInterface(OBJLC): obj;
                  state.getDisplayedType = SpecialPowers.do_lookupGetter(lookup_on, 'displayedType');
                  state.getHasRunningPlugin = SpecialPowers.do_lookupGetter(lookup_on, 'hasRunningPlugin');
                  state.getActualType = SpecialPowers.do_lookupGetter(lookup_on, 'actualType');
                  test_modes[mode](obj, from_state, to_state, state);
                });
              }
            }
          }
        }
      }

      function onLoad() {
        // Generic tests
        test({
          'tag_types': [ 'embed', 'object' ],
          // In all three modes
          'test_modes': [ 'immediate', 'cycle', 'cyclefirst', 'cycleboth' ],
          // Starting from a blank tag in and out of the document, a loading
          // plugin, and no-channel plugin (initial types only really have
          // odd cases with plugins)
          'from_states': [
            [ 'addToDoc' ],
            [ 'plugin' ],
            [ 'plugin', 'addToDoc' ],
            [ 'plugin', 'noChannel', 'setType', 'addToDoc' ],
            [],
          ],
          // To various combinations of loaded objects
          'to_states': eachList(
            [ 'svg', 'image', 'plugin', 'document', '' ],
            [ 'setType', 'setWrongType', 'setPluginType', '' ],
            [ 'noChannel', '' ],
            [ 'displayNone', 'displayInherit', '' ]
          )});
        // Special case test for embed tags with plugin-by-extension
        // TODO object tags should be tested here too -- they have slightly
        //      different behavior, but waiting on a file load requires a loaded
        //      event handler and wont work with just our event loop spinning.
        test({
          'tag_types': [ 'embed' ],
          'test_modes': [ 'immediate', 'cyclefirst', 'cycle', 'cycleboth' ],
          'from_states': eachList(
            [ 'svg', 'plugin', 'image', 'document' ],
            [ 'addToDoc' ]
          ),
          // Set extension along with valid ty
          'to_states': [
            [ 'pluginExtension' ]
          ]});
        // Test plugin add/remove from document with adding/removing frame, with
        // and without a channel.
        test({
          'tag_types': [ 'embed', 'object' ], // Ideally we'd test object too, but this gets exponentially long.
          'test_modes': [ 'immediate', 'cyclefirst', 'cycle' ],
          'from_states': [ [ 'displayNone', 'plugin', 'addToDoc' ],
                           [ 'displayNone', 'plugin', 'noChannel', 'addToDoc' ],
                           [ 'plugin', 'noChannel', 'addToDoc' ],
                           [ 'plugin', 'noChannel' ] ],
          'to_states': eachList(
            [ 'displayNone', '' ],
            [ 'removeFromDoc' ],
            [ 'image', 'displayNone', '' ],
            [ 'image', 'displayNone', '' ],
            [ 'addToDoc' ],
            [ 'displayInherit' ]
          )});
        runWhenDone(() => SimpleTest.finish());
      }
    </script>