bug 841350 - register pageshow handler earlier in browser.js r=ttaubert
[gecko.git] / browser / base / content / test / browser_pluginplaypreview2.js
blob0375749f204b1f7a83004eeb8d678565b2e4b9dc
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 var rootDir = getRootDirectory(gTestPath);
6 const gTestRoot = rootDir;
8 var gTestBrowser = null;
9 var gNextTest = null;
10 var gNextTestSkip = 0;
11 var gPlayPreviewPluginActualEvents = 0;
12 var gPlayPreviewPluginExpectedEvents = 1;
14 var gPlayPreviewRegistration = null;
16 function registerPlayPreview(mimeType, targetUrl) {
17   var ph = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
18   ph.registerPlayPreviewMimeType(mimeType, false, targetUrl);
20   return (gPlayPreviewRegistration = {
21     unregister: function() {
22       ph.unregisterPlayPreviewMimeType(mimeType);
23       gPlayPreviewRegistration = null;
24     }
25   });
28 function unregisterPlayPreview() {
29   gPlayPreviewRegistration.unregister();
32 Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
33 Components.utils.import("resource://gre/modules/Services.jsm");
36 function test() {
37   waitForExplicitFinish();
38   registerCleanupFunction(function() {
39     if (gPlayPreviewRegistration)
40       gPlayPreviewRegistration.unregister();
41     Services.prefs.clearUserPref("plugins.click_to_play");
42   });
44   var newTab = gBrowser.addTab();
45   gBrowser.selectedTab = newTab;
46   gTestBrowser = gBrowser.selectedBrowser;
47   gTestBrowser.addEventListener("load", pageLoad, true);
48   gTestBrowser.addEventListener("PluginBindingAttached", handleBindingAttached, true, true);
50   Services.prefs.setBoolPref("plugins.click_to_play", true);
52   registerPlayPreview('application/x-test', 'about:');
53   prepareTest(test1a, gTestRoot + "plugin_test.html", 1);
56 function finishTest() {
57   gTestBrowser.removeEventListener("load", pageLoad, true);
58   gTestBrowser.removeEventListener("PluginBindingAttached", handleBindingAttached, true, true);
59   gBrowser.removeCurrentTab();
60   window.focus();
61   finish();
64 function handleBindingAttached(evt) {
65   if (evt.target instanceof Ci.nsIObjectLoadingContent &&
66       evt.target.pluginFallbackType == Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW)
67     gPlayPreviewPluginActualEvents++;
70 function pageLoad() {
71   // The plugin events are async dispatched and can come after the load event
72   // This just allows the events to fire before we then go on to test the states
74   // iframe might triggers load event as well, making sure we skip some to let
75   // all iframes on the page be loaded as well
76   if (gNextTestSkip) {
77     gNextTestSkip--;
78     return;
79   }
80   executeSoon(gNextTest);
83 function prepareTest(nextTest, url, skip) {
84   gNextTest = nextTest;
85   gNextTestSkip = skip;
86   gTestBrowser.contentWindow.location = url;
89 // Tests a page with normal play preview registration (1/2)
90 function test1a() {
91   var notificationBox = gBrowser.getNotificationBox(gTestBrowser);
92   ok(!notificationBox.getNotificationWithValue("missing-plugins"), "Test 1a, Should not have displayed the missing plugin notification");
93   ok(!notificationBox.getNotificationWithValue("blocked-plugins"), "Test 1a, Should not have displayed the blocked plugin notification");
95   var pluginInfo = getTestPlugin();
96   ok(pluginInfo, "Should have a test plugin");
98   var doc = gTestBrowser.contentDocument;
99   var plugin = doc.getElementById("test");
100   var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
101   is(objLoadingContent.pluginFallbackType, Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 1a, plugin fallback type should be PLUGIN_PLAY_PREVIEW");
102   ok(!objLoadingContent.activated, "Test 1a, Plugin should not be activated");
104   var overlay = doc.getAnonymousElementByAttribute(plugin, "class", "previewPluginContent");
105   ok(overlay, "Test 1a, the overlay div is expected");
107   var iframe = overlay.getElementsByClassName("previewPluginContentFrame")[0];
108   ok(iframe && iframe.localName == "iframe", "Test 1a, the overlay iframe is expected");
109   var iframeHref = iframe.contentWindow.location.href;
110   ok(iframeHref == "about:", "Test 1a, the overlay about: content is expected");
112   var rect = iframe.getBoundingClientRect();
113   ok(rect.width == 200, "Test 1a, Plugin with id=" + plugin.id + " overlay rect should have 200px width before being replaced by actual plugin");
114   ok(rect.height == 200, "Test 1a, Plugin with id=" + plugin.id + " overlay rect should have 200px height before being replaced by actual plugin");
116   var e = overlay.ownerDocument.createEvent("CustomEvent");
117   e.initCustomEvent("MozPlayPlugin", true, true, null);
118   overlay.dispatchEvent(e);
119   var condition = function() objLoadingContent.activated;
120   waitForCondition(condition, test1b, "Test 1a, Waited too long for plugin to stop play preview");
123 // Tests that activating via MozPlayPlugin through the notification works (part 2/2)
124 function test1b() {
125   var plugin = gTestBrowser.contentDocument.getElementById("test");
126   var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
127   ok(objLoadingContent.activated, "Test 1b, Plugin should be activated");
129   is(gPlayPreviewPluginActualEvents, gPlayPreviewPluginExpectedEvents,
130      "There should be exactly one PluginPlayPreview event");
132   unregisterPlayPreview();
134   prepareTest(test2, gTestRoot + "plugin_test.html");
137 // Tests a page with a working plugin in it -- the mime type was just unregistered.
138 function test2() {
139   var doc = gTestBrowser.contentDocument;
140   var plugin = doc.getElementById("test");
141   var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
142   ok(objLoadingContent.pluginFallbackType != Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 2, plugin fallback type should not be PLUGIN_PLAY_PREVIEW");
143   ok(!objLoadingContent.activated, "Test 2, Plugin should not be activated");
145   registerPlayPreview('application/x-unknown', 'about:');
147   prepareTest(test3, gTestRoot + "plugin_test.html");
150 // Tests a page with a working plugin in it -- diffent play preview type is reserved.
151 function test3() {
152   var doc = gTestBrowser.contentDocument;
153   var plugin = doc.getElementById("test");
154   var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
155   ok(objLoadingContent.pluginFallbackType != Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 3, plugin fallback type should not be PLUGIN_PLAY_PREVIEW");
156   ok(!objLoadingContent.activated, "Test 3, Plugin should not be activated");
158   unregisterPlayPreview();
160   registerPlayPreview('application/x-test', 'about:');
161   Services.prefs.setBoolPref("plugins.click_to_play", false);
162   prepareTest(test4, gTestRoot + "plugin_test.html");
165 // Tests a page with a working plugin in it -- click-to-play is off
166 function test4() {
167   var plugin = gTestBrowser.contentDocument.getElementById("test");
168   var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
169   ok(objLoadingContent.activated, "Test 4, Plugin should be activated");
171   finishTest();