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

function waitForPluginVisibility(browser, shouldBeVisible, errorMessage) {
  return new Promise((resolve, reject) => {
    let windowUtils = window.windowUtils;
    let lastTransactionId = windowUtils.lastTransactionId;
    let listener = async event => {
      let visibility = await SpecialPowers.spawn(browser, [], async function() {
        let doc = content.document;
        let plugin = doc.getElementById("testplugin");
        return XPCNativeWrapper.unwrap(plugin).nativeWidgetIsVisible();
      });

      if (visibility == shouldBeVisible) {
        window.removeEventListener("MozAfterPaint", listener);
        resolve();
      } else if (event && event.transactionId > lastTransactionId) {
        // We want to allow for one failed check since we call listener
        // directly, but if we get a MozAfterPaint notification and we
        // still don't have the correct visibility, that's likely a
        // problem.
        reject(new Error("MozAfterPaint had a mismatched plugin visibility"));
      }
    };
    window.addEventListener("MozAfterPaint", listener);
    listener(null);
  });
}

// tests that we get plugin updates when we flip between tabs that
// have the same plugin in the same position in the page.

add_task(async function() {
  let result, tabSwitchedPromise;

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

  let testTab = gBrowser.selectedTab;
  let pluginTab1 = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    gTestRoot + "plugin_test.html"
  );
  let pluginTab2 = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    gTestRoot + "plugin_test.html"
  );

  result = await SpecialPowers.spawn(
    pluginTab1.linkedBrowser,
    [],
    async function() {
      let doc = content.document;
      let plugin = doc.getElementById("testplugin");
      return !!plugin;
    }
  );
  is(result, true, "plugin1 is loaded");

  result = await SpecialPowers.spawn(
    pluginTab2.linkedBrowser,
    [],
    async function() {
      let doc = content.document;
      let plugin = doc.getElementById("testplugin");
      return !!plugin;
    }
  );
  is(result, true, "plugin2 is loaded");

  // plugin tab 2 should be selected
  is(gBrowser.selectedTab == pluginTab2, true, "plugin2 is selected");

  await waitForPluginVisibility(
    pluginTab1.linkedBrowser,
    false,
    "plugin1 should be hidden"
  );

  await waitForPluginVisibility(
    pluginTab2.linkedBrowser,
    true,
    "plugin2 should be visible"
  );

  // select plugin1 tab
  tabSwitchedPromise = waitTabSwitched();
  gBrowser.selectedTab = pluginTab1;
  await tabSwitchedPromise;

  await waitForPluginVisibility(
    pluginTab1.linkedBrowser,
    true,
    "plugin1 should be visible"
  );

  await waitForPluginVisibility(
    pluginTab2.linkedBrowser,
    false,
    "plugin2 should be hidden"
  );

  // select plugin2 tab
  tabSwitchedPromise = waitTabSwitched();
  gBrowser.selectedTab = pluginTab2;
  await tabSwitchedPromise;

  await waitForPluginVisibility(
    pluginTab1.linkedBrowser,
    false,
    "plugin1 should be hidden"
  );

  await waitForPluginVisibility(
    pluginTab2.linkedBrowser,
    true,
    "plugin2 should be visible"
  );

  // select test tab
  tabSwitchedPromise = waitTabSwitched();
  gBrowser.selectedTab = testTab;
  await tabSwitchedPromise;

  await waitForPluginVisibility(
    pluginTab1.linkedBrowser,
    false,
    "plugin1 should be hidden"
  );

  await waitForPluginVisibility(
    pluginTab2.linkedBrowser,
    false,
    "plugin2 should be hidden"
  );

  gBrowser.removeTab(pluginTab1);
  gBrowser.removeTab(pluginTab2);
});