summaryrefslogtreecommitdiffstats
path: root/dom/plugins/test/mochitest/browser_bug1163570.js
blob: c6a75d2ec8a719c76c0c62193d2ce75f90da5545 (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
var gTestRoot = getRootDirectory(gTestPath).replace("chrome://mochitests/content/", "http://127.0.0.1:8888/");

// simple tab load helper, pilfered from browser plugin tests
function promiseTabLoad(tab, url, eventType="load") {
  return new Promise((resolve) => {
    function handle(event) {
      if (event.originalTarget != tab.linkedBrowser.contentDocument ||
          event.target.location.href == "about:blank" ||
          (url && event.target.location.href != url)) {
        return;
      }
      tab.linkedBrowser.removeEventListener(eventType, handle, true);
      resolve(event);
    }

    tab.linkedBrowser.addEventListener(eventType, handle, true, true);
    if (url) {
      tab.linkedBrowser.loadURI(url);
    }
  });
}

// dom event listener helper
function promiseWaitForEvent(object, eventName, capturing = false, chrome = false) {
  return new Promise((resolve) => {
    function listener(event) {
      object.removeEventListener(eventName, listener, capturing, chrome);
      resolve(event);
    }
    object.addEventListener(eventName, listener, capturing, chrome);
  });
}

add_task(function* () {
  registerCleanupFunction(function () {
    window.focus();
  });
});

add_task(function* () {
  setTestPluginEnabledState(Ci.nsIPluginTag.STATE_ENABLED, "Test Plug-in");

  let pluginTab = gBrowser.selectedTab = gBrowser.addTab();
  let prefTab = gBrowser.addTab();

  yield promiseTabLoad(pluginTab, gTestRoot + "plugin_test.html");
  yield promiseTabLoad(prefTab, "about:preferences");

  yield ContentTask.spawn(gBrowser.selectedBrowser, null, function*() {
    let doc = content.document;
    let plugin = doc.getElementById("testplugin");
    Assert.ok(!!plugin, "plugin is loaded");
  });

  let ppromise = promiseWaitForEvent(window, "MozAfterPaint");
  gBrowser.selectedTab = prefTab;
  yield ppromise;

  // We're going to switch tabs using actual mouse clicks, which helps
  // reproduce this bug.
  let tabStripContainer = document.getElementById("tabbrowser-tabs");

  // diagnosis if front end layout changes
  info("-> " + tabStripContainer.tagName); // tabs
  info("-> " + tabStripContainer.firstChild.tagName); // tab
  info("-> " + tabStripContainer.childNodes[0].label); // test harness tab
  info("-> " + tabStripContainer.childNodes[1].label); // plugin tab
  info("-> " + tabStripContainer.childNodes[2].label); // preferences tab

  for (let iteration = 0; iteration < 5; iteration++) {
    ppromise = promiseWaitForEvent(window, "MozAfterPaint");
    EventUtils.synthesizeMouseAtCenter(tabStripContainer.childNodes[1], {}, window);
    yield ppromise;

    yield ContentTask.spawn(pluginTab.linkedBrowser, null, function*() {
      let doc = content.document;
      let plugin = doc.getElementById("testplugin");
      Assert.ok(XPCNativeWrapper.unwrap(plugin).nativeWidgetIsVisible(),
        "plugin is visible");
    });

    ppromise = promiseWaitForEvent(window, "MozAfterPaint");
    EventUtils.synthesizeMouseAtCenter(tabStripContainer.childNodes[2], {}, window);
    yield ppromise;

    yield ContentTask.spawn(pluginTab.linkedBrowser, null, function*() {
      let doc = content.document;
      let plugin = doc.getElementById("testplugin");
      Assert.ok(!XPCNativeWrapper.unwrap(plugin).nativeWidgetIsVisible(),
        "plugin is hidden");
    });
  }

  gBrowser.removeTab(prefTab);
  gBrowser.removeTab(pluginTab);
});