summaryrefslogtreecommitdiffstats
path: root/dom/browser-element/mochitest/browserElement_SetVisible.js
blob: dac8d7e427cd51a8a75b5e6b114bd728d4354ed6 (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
/* Any copyright is dedicated to the public domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test the setVisible property for mozbrowser
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("untriaged");
browserElementTestHelpers.setEnabledPref(true);

var iframeScript = function() {
  content.document.addEventListener("visibilitychange", function() {
    sendAsyncMessage('test:visibilitychange', {
      hidden: content.document.hidden
    });
  }, false);
}

function runTest() {
  var mm;
  var numEvents = 0;
  var iframe1 = document.createElement('iframe');
  iframe1.setAttribute('mozbrowser', 'true');
  iframe1.src = 'data:text/html,1';

  document.body.appendChild(iframe1);

  function recvVisibilityChanged(msg) {
    msg = SpecialPowers.wrap(msg);
    numEvents++;
    if (numEvents === 1) {
      ok(true, 'iframe recieved visibility changed');
      ok(msg.json.hidden === true, 'hidden attribute correctly set');
      iframe1.setVisible(false);
      iframe1.setVisible(true);
    } else if (numEvents === 2) {
      ok(msg.json.hidden === false, 'hidden attribute correctly set');
      // Allow some time in case we generate too many events
      setTimeout(function() {
        mm.removeMessageListener('test:visibilitychange', recvVisibilityChanged);
        SimpleTest.finish();
      }, 100);
    } else {
      ok(false, 'Too many visibilitychange events');
    }
  }

  function iframeLoaded() {
    testGetVisible();
  }

  function testGetVisible() {
    iframe1.setVisible(false);
    iframe1.getVisible().onsuccess = function(evt) {
      ok(evt.target.result === false, 'getVisible() responds false after setVisible(false)');

      iframe1.setVisible(true);
      iframe1.getVisible().onsuccess = function(evt) {
        ok(evt.target.result === true, 'getVisible() responds true after setVisible(true)');
        testVisibilityChanges();
      };
    };
  }

  function testVisibilityChanges() {
    mm = SpecialPowers.getBrowserFrameMessageManager(iframe1);
    mm.addMessageListener('test:visibilitychange', recvVisibilityChanged);
    mm.loadFrameScript('data:,(' + iframeScript.toString() + ')();', false);
    iframe1.setVisible(false);
  }

  iframe1.addEventListener('mozbrowserloadend', iframeLoaded);
}

addEventListener('testready', runTest);