browser/base/content/test/plugins/browser_pluginplaypreview.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 var rootDir = getRootDirectory(gTestPath);
michael@0 6 const gTestRoot = rootDir;
michael@0 7
michael@0 8 var gTestBrowser = null;
michael@0 9 var gNextTest = null;
michael@0 10 var gNextTestSkip = 0;
michael@0 11 var gPlayPreviewPluginActualEvents = 0;
michael@0 12 var gPlayPreviewPluginExpectedEvents = 1;
michael@0 13
michael@0 14 var gPlayPreviewRegistration = null;
michael@0 15
michael@0 16 function registerPlayPreview(mimeType, targetUrl) {
michael@0 17
michael@0 18 function StreamConverterFactory() {}
michael@0 19 StreamConverterFactory.prototype = {
michael@0 20 QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]),
michael@0 21 _targetConstructor: null,
michael@0 22
michael@0 23 register: function register(targetConstructor) {
michael@0 24 this._targetConstructor = targetConstructor;
michael@0 25 var proto = targetConstructor.prototype;
michael@0 26 var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 27 registrar.registerFactory(proto.classID, proto.classDescription,
michael@0 28 proto.contractID, this);
michael@0 29 },
michael@0 30
michael@0 31 unregister: function unregister() {
michael@0 32 var proto = this._targetConstructor.prototype;
michael@0 33 var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 34 registrar.unregisterFactory(proto.classID, this);
michael@0 35 this._targetConstructor = null;
michael@0 36 },
michael@0 37
michael@0 38 // nsIFactory
michael@0 39 createInstance: function createInstance(aOuter, iid) {
michael@0 40 if (aOuter !== null)
michael@0 41 throw Cr.NS_ERROR_NO_AGGREGATION;
michael@0 42 return (new (this._targetConstructor)).QueryInterface(iid);
michael@0 43 },
michael@0 44
michael@0 45 // nsIFactory
michael@0 46 lockFactory: function lockFactory(lock) {
michael@0 47 // No longer used as of gecko 1.7.
michael@0 48 throw Cr.NS_ERROR_NOT_IMPLEMENTED;
michael@0 49 }
michael@0 50 };
michael@0 51
michael@0 52 function OverlayStreamConverter() {}
michael@0 53 OverlayStreamConverter.prototype = {
michael@0 54 QueryInterface: XPCOMUtils.generateQI([
michael@0 55 Ci.nsISupports,
michael@0 56 Ci.nsIStreamConverter,
michael@0 57 Ci.nsIStreamListener,
michael@0 58 Ci.nsIRequestObserver
michael@0 59 ]),
michael@0 60
michael@0 61 classID: Components.ID('{4c6030f7-e20a-264f-0f9b-ada3a9e97384}'),
michael@0 62 classDescription: 'overlay-test-data Component',
michael@0 63 contractID: '@mozilla.org/streamconv;1?from=application/x-moz-playpreview&to=*/*',
michael@0 64
michael@0 65 // nsIStreamConverter::convert
michael@0 66 convert: function(aFromStream, aFromType, aToType, aCtxt) {
michael@0 67 throw Cr.NS_ERROR_NOT_IMPLEMENTED;
michael@0 68 },
michael@0 69
michael@0 70 // nsIStreamConverter::asyncConvertData
michael@0 71 asyncConvertData: function(aFromType, aToType, aListener, aCtxt) {
michael@0 72 var isValidRequest = false;
michael@0 73 try {
michael@0 74 var request = aCtxt;
michael@0 75 request.QueryInterface(Ci.nsIChannel);
michael@0 76 var spec = request.URI.spec;
michael@0 77 var expectedSpec = 'data:application/x-moz-playpreview;,' + mimeType;
michael@0 78 isValidRequest = (spec == expectedSpec);
michael@0 79 } catch (e) { }
michael@0 80 if (!isValidRequest)
michael@0 81 throw Cr.NS_ERROR_NOT_IMPLEMENTED;
michael@0 82
michael@0 83 // Store the listener passed to us
michael@0 84 this.listener = aListener;
michael@0 85 },
michael@0 86
michael@0 87 // nsIStreamListener::onDataAvailable
michael@0 88 onDataAvailable: function(aRequest, aContext, aInputStream, aOffset, aCount) {
michael@0 89 // Do nothing since all the data loading is handled by the viewer.
michael@0 90 ok(false, "onDataAvailable should not be called");
michael@0 91 },
michael@0 92
michael@0 93 // nsIRequestObserver::onStartRequest
michael@0 94 onStartRequest: function(aRequest, aContext) {
michael@0 95
michael@0 96 // Setup the request so we can use it below.
michael@0 97 aRequest.QueryInterface(Ci.nsIChannel);
michael@0 98 // Cancel the request so the viewer can handle it.
michael@0 99 aRequest.cancel(Cr.NS_BINDING_ABORTED);
michael@0 100
michael@0 101 // Create a new channel that is viewer loaded as a resource.
michael@0 102 var ioService = Services.io;
michael@0 103 var channel = ioService.newChannel(targetUrl, null, null);
michael@0 104 channel.asyncOpen(this.listener, aContext);
michael@0 105 },
michael@0 106
michael@0 107 // nsIRequestObserver::onStopRequest
michael@0 108 onStopRequest: function(aRequest, aContext, aStatusCode) {
michael@0 109 // Do nothing.
michael@0 110 }
michael@0 111 };
michael@0 112
michael@0 113 var ph = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost);
michael@0 114 ph.registerPlayPreviewMimeType(mimeType, true); // ignoring CTP rules
michael@0 115
michael@0 116 var factory = new StreamConverterFactory();
michael@0 117 factory.register(OverlayStreamConverter);
michael@0 118
michael@0 119 return (gPlayPreviewRegistration = {
michael@0 120 unregister: function() {
michael@0 121 ph.unregisterPlayPreviewMimeType(mimeType);
michael@0 122 factory.unregister();
michael@0 123 gPlayPreviewRegistration = null;
michael@0 124 }
michael@0 125 });
michael@0 126 }
michael@0 127
michael@0 128 function unregisterPlayPreview() {
michael@0 129 gPlayPreviewRegistration.unregister();
michael@0 130 }
michael@0 131
michael@0 132 Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
michael@0 133 Components.utils.import("resource://gre/modules/Services.jsm");
michael@0 134
michael@0 135
michael@0 136 function test() {
michael@0 137 waitForExplicitFinish();
michael@0 138 registerCleanupFunction(function() {
michael@0 139 if (gPlayPreviewRegistration)
michael@0 140 gPlayPreviewRegistration.unregister();
michael@0 141 Services.prefs.clearUserPref("plugins.click_to_play");
michael@0 142 });
michael@0 143
michael@0 144 var newTab = gBrowser.addTab();
michael@0 145 gBrowser.selectedTab = newTab;
michael@0 146 gTestBrowser = gBrowser.selectedBrowser;
michael@0 147 gTestBrowser.addEventListener("load", pageLoad, true);
michael@0 148 gTestBrowser.addEventListener("PluginBindingAttached", handleBindingAttached, true, true);
michael@0 149
michael@0 150 setTestPluginEnabledState(Ci.nsIPluginTag.STATE_ENABLED);
michael@0 151 registerPlayPreview('application/x-test', 'about:');
michael@0 152 prepareTest(test1a, gTestRoot + "plugin_test.html", 1);
michael@0 153 }
michael@0 154
michael@0 155 function finishTest() {
michael@0 156 gTestBrowser.removeEventListener("load", pageLoad, true);
michael@0 157 gTestBrowser.removeEventListener("PluginBindingAttached", handleBindingAttached, true, true);
michael@0 158 gBrowser.removeCurrentTab();
michael@0 159 window.focus();
michael@0 160 finish();
michael@0 161 }
michael@0 162
michael@0 163 function handleBindingAttached(evt) {
michael@0 164 if (evt.target instanceof Ci.nsIObjectLoadingContent &&
michael@0 165 evt.target.pluginFallbackType == Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW)
michael@0 166 gPlayPreviewPluginActualEvents++;
michael@0 167 }
michael@0 168
michael@0 169 function pageLoad() {
michael@0 170 // The plugin events are async dispatched and can come after the load event
michael@0 171 // This just allows the events to fire before we then go on to test the states
michael@0 172
michael@0 173 // iframe might triggers load event as well, making sure we skip some to let
michael@0 174 // all iframes on the page be loaded as well
michael@0 175 if (gNextTestSkip) {
michael@0 176 gNextTestSkip--;
michael@0 177 return;
michael@0 178 }
michael@0 179 executeSoon(gNextTest);
michael@0 180 }
michael@0 181
michael@0 182 function prepareTest(nextTest, url, skip) {
michael@0 183 gNextTest = nextTest;
michael@0 184 gNextTestSkip = skip;
michael@0 185 gTestBrowser.contentWindow.location = url;
michael@0 186 }
michael@0 187
michael@0 188 // Tests a page with normal play preview registration (1/2)
michael@0 189 function test1a() {
michael@0 190 var notificationBox = gBrowser.getNotificationBox(gTestBrowser);
michael@0 191 ok(!notificationBox.getNotificationWithValue("missing-plugins"), "Test 1a, Should not have displayed the missing plugin notification");
michael@0 192 ok(!notificationBox.getNotificationWithValue("blocked-plugins"), "Test 1a, Should not have displayed the blocked plugin notification");
michael@0 193
michael@0 194 var pluginInfo = getTestPlugin();
michael@0 195 ok(pluginInfo, "Should have a test plugin");
michael@0 196
michael@0 197 var doc = gTestBrowser.contentDocument;
michael@0 198 var plugin = doc.getElementById("test");
michael@0 199 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
michael@0 200 is(objLoadingContent.pluginFallbackType, Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 1a, plugin fallback type should be PLUGIN_PLAY_PREVIEW");
michael@0 201 ok(!objLoadingContent.activated, "Test 1a, Plugin should not be activated");
michael@0 202
michael@0 203 var overlay = doc.getAnonymousElementByAttribute(plugin, "class", "previewPluginContent");
michael@0 204 ok(overlay, "Test 1a, the overlay div is expected");
michael@0 205
michael@0 206 var iframe = overlay.getElementsByClassName("previewPluginContentFrame")[0];
michael@0 207 ok(iframe && iframe.localName == "iframe", "Test 1a, the overlay iframe is expected");
michael@0 208 var iframeHref = iframe.contentWindow.location.href;
michael@0 209 ok(iframeHref == "about:", "Test 1a, the overlay about: content is expected");
michael@0 210
michael@0 211 var rect = iframe.getBoundingClientRect();
michael@0 212 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 213 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 214
michael@0 215 var e = overlay.ownerDocument.createEvent("CustomEvent");
michael@0 216 e.initCustomEvent("MozPlayPlugin", true, true, null);
michael@0 217 overlay.dispatchEvent(e);
michael@0 218 var condition = function() objLoadingContent.activated;
michael@0 219 waitForCondition(condition, test1b, "Test 1a, Waited too long for plugin to stop play preview");
michael@0 220 }
michael@0 221
michael@0 222 // Tests that activating via MozPlayPlugin through the notification works (part 2/2)
michael@0 223 function test1b() {
michael@0 224 var plugin = gTestBrowser.contentDocument.getElementById("test");
michael@0 225 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
michael@0 226 ok(objLoadingContent.activated, "Test 1b, Plugin should be activated");
michael@0 227
michael@0 228 is(gPlayPreviewPluginActualEvents, gPlayPreviewPluginExpectedEvents,
michael@0 229 "There should be exactly one PluginPlayPreview event");
michael@0 230
michael@0 231 unregisterPlayPreview();
michael@0 232
michael@0 233 prepareTest(test2, gTestRoot + "plugin_test.html");
michael@0 234 }
michael@0 235
michael@0 236 // Tests a page with a working plugin in it -- the mime type was just unregistered.
michael@0 237 function test2() {
michael@0 238 var plugin = gTestBrowser.contentDocument.getElementById("test");
michael@0 239 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
michael@0 240 ok(objLoadingContent.activated, "Test 2, Plugin should be activated");
michael@0 241
michael@0 242 registerPlayPreview('application/x-unknown', 'about:');
michael@0 243
michael@0 244 prepareTest(test3, gTestRoot + "plugin_test.html");
michael@0 245 }
michael@0 246
michael@0 247 // Tests a page with a working plugin in it -- diffent play preview type is reserved.
michael@0 248 function test3() {
michael@0 249 var plugin = gTestBrowser.contentDocument.getElementById("test");
michael@0 250 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
michael@0 251 ok(objLoadingContent.activated, "Test 3, Plugin should be activated");
michael@0 252
michael@0 253 unregisterPlayPreview();
michael@0 254
michael@0 255 registerPlayPreview('application/x-test', 'about:');
michael@0 256 Services.prefs.setBoolPref("plugins.click_to_play", true);
michael@0 257 setTestPluginEnabledState(Ci.nsIPluginTag.STATE_CLICKTOPLAY);
michael@0 258 prepareTest(test4a, gTestRoot + "plugin_test.html", 1);
michael@0 259 }
michael@0 260
michael@0 261 // Test a fallback to the click-to-play
michael@0 262 function test4a() {
michael@0 263 var doc = gTestBrowser.contentDocument;
michael@0 264 var plugin = doc.getElementById("test");
michael@0 265 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
michael@0 266 is(objLoadingContent.pluginFallbackType, Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 4a, plugin fallback type should be PLUGIN_PLAY_PREVIEW");
michael@0 267 ok(!objLoadingContent.activated, "Test 4a, Plugin should not be activated");
michael@0 268
michael@0 269 var overlay = doc.getAnonymousElementByAttribute(plugin, "class", "previewPluginContent");
michael@0 270 ok(overlay, "Test 4a, the overlay div is expected");
michael@0 271
michael@0 272 var e = overlay.ownerDocument.createEvent("CustomEvent");
michael@0 273 e.initCustomEvent("MozPlayPlugin", true, true, true);
michael@0 274 overlay.dispatchEvent(e);
michael@0 275 var condition = function() objLoadingContent.pluginFallbackType == Ci.nsIObjectLoadingContent.PLUGIN_CLICK_TO_PLAY;
michael@0 276 waitForCondition(condition, test4b, "Test 4a, Waited too long for plugin to stop play preview");
michael@0 277 }
michael@0 278
michael@0 279 function test4b() {
michael@0 280 var doc = gTestBrowser.contentDocument;
michael@0 281 var plugin = doc.getElementById("test");
michael@0 282 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
michael@0 283 ok(objLoadingContent.pluginFallbackType != Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 4b, plugin fallback type should not be PLUGIN_PLAY_PREVIEW");
michael@0 284 ok(!objLoadingContent.activated, "Test 4b, Plugin should not be activated");
michael@0 285
michael@0 286 prepareTest(test5a, gTestRoot + "plugin_test.html", 1);
michael@0 287 }
michael@0 288
michael@0 289 // Test a bypass of the click-to-play
michael@0 290 function test5a() {
michael@0 291 var doc = gTestBrowser.contentDocument;
michael@0 292 var plugin = doc.getElementById("test");
michael@0 293 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
michael@0 294 is(objLoadingContent.pluginFallbackType, Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 5a, plugin fallback type should be PLUGIN_PLAY_PREVIEW");
michael@0 295 ok(!objLoadingContent.activated, "Test 5a, Plugin should not be activated");
michael@0 296
michael@0 297 var overlay = doc.getAnonymousElementByAttribute(plugin, "class", "previewPluginContent");
michael@0 298 ok(overlay, "Test 5a, the overlay div is expected");
michael@0 299
michael@0 300 var e = overlay.ownerDocument.createEvent("CustomEvent");
michael@0 301 e.initCustomEvent("MozPlayPlugin", true, true, false);
michael@0 302 overlay.dispatchEvent(e);
michael@0 303 var condition = function() objLoadingContent.activated;
michael@0 304 waitForCondition(condition, test5b, "Test 5a, Waited too long for plugin to stop play preview");
michael@0 305 }
michael@0 306
michael@0 307 function test5b() {
michael@0 308 var doc = gTestBrowser.contentDocument;
michael@0 309 var plugin = doc.getElementById("test");
michael@0 310 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent);
michael@0 311 ok(objLoadingContent.activated, "Test 5b, Plugin should be activated");
michael@0 312
michael@0 313 finishTest();
michael@0 314 }
michael@0 315

mercurial