michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: var rootDir = getRootDirectory(gTestPath); michael@0: const gTestRoot = rootDir; michael@0: michael@0: var gTestBrowser = null; michael@0: var gNextTest = null; michael@0: var gNextTestSkip = 0; michael@0: var gPlayPreviewPluginActualEvents = 0; michael@0: var gPlayPreviewPluginExpectedEvents = 1; michael@0: michael@0: var gPlayPreviewRegistration = null; michael@0: michael@0: function registerPlayPreview(mimeType, targetUrl) { michael@0: var ph = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost); michael@0: ph.registerPlayPreviewMimeType(mimeType, false, targetUrl); michael@0: michael@0: return (gPlayPreviewRegistration = { michael@0: unregister: function() { michael@0: ph.unregisterPlayPreviewMimeType(mimeType); michael@0: gPlayPreviewRegistration = null; michael@0: } michael@0: }); michael@0: } michael@0: michael@0: function unregisterPlayPreview() { michael@0: gPlayPreviewRegistration.unregister(); michael@0: } michael@0: michael@0: Components.utils.import('resource://gre/modules/XPCOMUtils.jsm'); michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: registerCleanupFunction(function() { michael@0: if (gPlayPreviewRegistration) michael@0: gPlayPreviewRegistration.unregister(); michael@0: Services.prefs.clearUserPref("plugins.click_to_play"); michael@0: }); michael@0: michael@0: var newTab = gBrowser.addTab(); michael@0: gBrowser.selectedTab = newTab; michael@0: gTestBrowser = gBrowser.selectedBrowser; michael@0: gTestBrowser.addEventListener("load", pageLoad, true); michael@0: gTestBrowser.addEventListener("PluginBindingAttached", handleBindingAttached, true, true); michael@0: michael@0: Services.prefs.setBoolPref("plugins.click_to_play", true); michael@0: setTestPluginEnabledState(Ci.nsIPluginTag.STATE_CLICKTOPLAY); michael@0: michael@0: registerPlayPreview('application/x-test', 'about:'); michael@0: prepareTest(test1a, gTestRoot + "plugin_test.html", 1); michael@0: } michael@0: michael@0: function finishTest() { michael@0: gTestBrowser.removeEventListener("load", pageLoad, true); michael@0: gTestBrowser.removeEventListener("PluginBindingAttached", handleBindingAttached, true, true); michael@0: gBrowser.removeCurrentTab(); michael@0: window.focus(); michael@0: finish(); michael@0: } michael@0: michael@0: function handleBindingAttached(evt) { michael@0: if (evt.target instanceof Ci.nsIObjectLoadingContent && michael@0: evt.target.pluginFallbackType == Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW) michael@0: gPlayPreviewPluginActualEvents++; michael@0: } michael@0: michael@0: function pageLoad() { michael@0: // The plugin events are async dispatched and can come after the load event michael@0: // This just allows the events to fire before we then go on to test the states michael@0: michael@0: // iframe might triggers load event as well, making sure we skip some to let michael@0: // all iframes on the page be loaded as well michael@0: if (gNextTestSkip) { michael@0: gNextTestSkip--; michael@0: return; michael@0: } michael@0: executeSoon(gNextTest); michael@0: } michael@0: michael@0: function prepareTest(nextTest, url, skip) { michael@0: gNextTest = nextTest; michael@0: gNextTestSkip = skip; michael@0: gTestBrowser.contentWindow.location = url; michael@0: } michael@0: michael@0: // Tests a page with normal play preview registration (1/2) michael@0: function test1a() { michael@0: var notificationBox = gBrowser.getNotificationBox(gTestBrowser); michael@0: ok(!notificationBox.getNotificationWithValue("missing-plugins"), "Test 1a, Should not have displayed the missing plugin notification"); michael@0: ok(!notificationBox.getNotificationWithValue("blocked-plugins"), "Test 1a, Should not have displayed the blocked plugin notification"); michael@0: michael@0: var pluginInfo = getTestPlugin(); michael@0: ok(pluginInfo, "Should have a test plugin"); michael@0: michael@0: var doc = gTestBrowser.contentDocument; michael@0: var plugin = doc.getElementById("test"); michael@0: var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent); michael@0: is(objLoadingContent.pluginFallbackType, Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 1a, plugin fallback type should be PLUGIN_PLAY_PREVIEW"); michael@0: ok(!objLoadingContent.activated, "Test 1a, Plugin should not be activated"); michael@0: michael@0: var overlay = doc.getAnonymousElementByAttribute(plugin, "class", "previewPluginContent"); michael@0: ok(overlay, "Test 1a, the overlay div is expected"); michael@0: michael@0: var iframe = overlay.getElementsByClassName("previewPluginContentFrame")[0]; michael@0: ok(iframe && iframe.localName == "iframe", "Test 1a, the overlay iframe is expected"); michael@0: var iframeHref = iframe.contentWindow.location.href; michael@0: ok(iframeHref == "about:", "Test 1a, the overlay about: content is expected"); michael@0: michael@0: var rect = iframe.getBoundingClientRect(); michael@0: ok(rect.width == 200, "Test 1a, Plugin with id=" + plugin.id + " overlay rect should have 200px width before being replaced by actual plugin"); michael@0: ok(rect.height == 200, "Test 1a, Plugin with id=" + plugin.id + " overlay rect should have 200px height before being replaced by actual plugin"); michael@0: michael@0: var e = overlay.ownerDocument.createEvent("CustomEvent"); michael@0: e.initCustomEvent("MozPlayPlugin", true, true, null); michael@0: overlay.dispatchEvent(e); michael@0: var condition = function() objLoadingContent.activated; michael@0: waitForCondition(condition, test1b, "Test 1a, Waited too long for plugin to stop play preview"); michael@0: } michael@0: michael@0: // Tests that activating via MozPlayPlugin through the notification works (part 2/2) michael@0: function test1b() { michael@0: var plugin = gTestBrowser.contentDocument.getElementById("test"); michael@0: var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent); michael@0: ok(objLoadingContent.activated, "Test 1b, Plugin should be activated"); michael@0: michael@0: is(gPlayPreviewPluginActualEvents, gPlayPreviewPluginExpectedEvents, michael@0: "There should be exactly one PluginPlayPreview event"); michael@0: michael@0: unregisterPlayPreview(); michael@0: michael@0: prepareTest(test2, gTestRoot + "plugin_test.html"); michael@0: } michael@0: michael@0: // Tests a page with a working plugin in it -- the mime type was just unregistered. michael@0: function test2() { michael@0: var doc = gTestBrowser.contentDocument; michael@0: var plugin = doc.getElementById("test"); michael@0: var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent); michael@0: ok(objLoadingContent.pluginFallbackType != Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 2, plugin fallback type should not be PLUGIN_PLAY_PREVIEW"); michael@0: ok(!objLoadingContent.activated, "Test 2, Plugin should not be activated"); michael@0: michael@0: registerPlayPreview('application/x-unknown', 'about:'); michael@0: michael@0: prepareTest(test3, gTestRoot + "plugin_test.html"); michael@0: } michael@0: michael@0: // Tests a page with a working plugin in it -- diffent play preview type is reserved. michael@0: function test3() { michael@0: var doc = gTestBrowser.contentDocument; michael@0: var plugin = doc.getElementById("test"); michael@0: var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent); michael@0: ok(objLoadingContent.pluginFallbackType != Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 3, plugin fallback type should not be PLUGIN_PLAY_PREVIEW"); michael@0: ok(!objLoadingContent.activated, "Test 3, Plugin should not be activated"); michael@0: michael@0: unregisterPlayPreview(); michael@0: michael@0: registerPlayPreview('application/x-test', 'about:'); michael@0: Services.prefs.setBoolPref("plugins.click_to_play", false); michael@0: var plugin = getTestPlugin(); michael@0: plugin.enabledState = Ci.nsIPluginTag.STATE_ENABLED; michael@0: prepareTest(test4, gTestRoot + "plugin_test.html"); michael@0: } michael@0: michael@0: // Tests a page with a working plugin in it -- click-to-play is off michael@0: function test4() { michael@0: var plugin = gTestBrowser.contentDocument.getElementById("test"); michael@0: var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent); michael@0: ok(objLoadingContent.activated, "Test 4, Plugin should be activated"); michael@0: michael@0: finishTest(); michael@0: } michael@0: