|
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/. */ |
|
4 |
|
5 var rootDir = getRootDirectory(gTestPath); |
|
6 const gTestRoot = rootDir; |
|
7 |
|
8 var gTestBrowser = null; |
|
9 var gNextTest = null; |
|
10 var gNextTestSkip = 0; |
|
11 var gPlayPreviewPluginActualEvents = 0; |
|
12 var gPlayPreviewPluginExpectedEvents = 1; |
|
13 |
|
14 var gPlayPreviewRegistration = null; |
|
15 |
|
16 function registerPlayPreview(mimeType, targetUrl) { |
|
17 var ph = Cc["@mozilla.org/plugin/host;1"].getService(Ci.nsIPluginHost); |
|
18 ph.registerPlayPreviewMimeType(mimeType, false, targetUrl); |
|
19 |
|
20 return (gPlayPreviewRegistration = { |
|
21 unregister: function() { |
|
22 ph.unregisterPlayPreviewMimeType(mimeType); |
|
23 gPlayPreviewRegistration = null; |
|
24 } |
|
25 }); |
|
26 } |
|
27 |
|
28 function unregisterPlayPreview() { |
|
29 gPlayPreviewRegistration.unregister(); |
|
30 } |
|
31 |
|
32 Components.utils.import('resource://gre/modules/XPCOMUtils.jsm'); |
|
33 Components.utils.import("resource://gre/modules/Services.jsm"); |
|
34 |
|
35 |
|
36 function test() { |
|
37 waitForExplicitFinish(); |
|
38 registerCleanupFunction(function() { |
|
39 if (gPlayPreviewRegistration) |
|
40 gPlayPreviewRegistration.unregister(); |
|
41 Services.prefs.clearUserPref("plugins.click_to_play"); |
|
42 }); |
|
43 |
|
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); |
|
49 |
|
50 Services.prefs.setBoolPref("plugins.click_to_play", true); |
|
51 setTestPluginEnabledState(Ci.nsIPluginTag.STATE_CLICKTOPLAY); |
|
52 |
|
53 registerPlayPreview('application/x-test', 'about:'); |
|
54 prepareTest(test1a, gTestRoot + "plugin_test.html", 1); |
|
55 } |
|
56 |
|
57 function finishTest() { |
|
58 gTestBrowser.removeEventListener("load", pageLoad, true); |
|
59 gTestBrowser.removeEventListener("PluginBindingAttached", handleBindingAttached, true, true); |
|
60 gBrowser.removeCurrentTab(); |
|
61 window.focus(); |
|
62 finish(); |
|
63 } |
|
64 |
|
65 function handleBindingAttached(evt) { |
|
66 if (evt.target instanceof Ci.nsIObjectLoadingContent && |
|
67 evt.target.pluginFallbackType == Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW) |
|
68 gPlayPreviewPluginActualEvents++; |
|
69 } |
|
70 |
|
71 function pageLoad() { |
|
72 // The plugin events are async dispatched and can come after the load event |
|
73 // This just allows the events to fire before we then go on to test the states |
|
74 |
|
75 // iframe might triggers load event as well, making sure we skip some to let |
|
76 // all iframes on the page be loaded as well |
|
77 if (gNextTestSkip) { |
|
78 gNextTestSkip--; |
|
79 return; |
|
80 } |
|
81 executeSoon(gNextTest); |
|
82 } |
|
83 |
|
84 function prepareTest(nextTest, url, skip) { |
|
85 gNextTest = nextTest; |
|
86 gNextTestSkip = skip; |
|
87 gTestBrowser.contentWindow.location = url; |
|
88 } |
|
89 |
|
90 // Tests a page with normal play preview registration (1/2) |
|
91 function test1a() { |
|
92 var notificationBox = gBrowser.getNotificationBox(gTestBrowser); |
|
93 ok(!notificationBox.getNotificationWithValue("missing-plugins"), "Test 1a, Should not have displayed the missing plugin notification"); |
|
94 ok(!notificationBox.getNotificationWithValue("blocked-plugins"), "Test 1a, Should not have displayed the blocked plugin notification"); |
|
95 |
|
96 var pluginInfo = getTestPlugin(); |
|
97 ok(pluginInfo, "Should have a test plugin"); |
|
98 |
|
99 var doc = gTestBrowser.contentDocument; |
|
100 var plugin = doc.getElementById("test"); |
|
101 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent); |
|
102 is(objLoadingContent.pluginFallbackType, Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 1a, plugin fallback type should be PLUGIN_PLAY_PREVIEW"); |
|
103 ok(!objLoadingContent.activated, "Test 1a, Plugin should not be activated"); |
|
104 |
|
105 var overlay = doc.getAnonymousElementByAttribute(plugin, "class", "previewPluginContent"); |
|
106 ok(overlay, "Test 1a, the overlay div is expected"); |
|
107 |
|
108 var iframe = overlay.getElementsByClassName("previewPluginContentFrame")[0]; |
|
109 ok(iframe && iframe.localName == "iframe", "Test 1a, the overlay iframe is expected"); |
|
110 var iframeHref = iframe.contentWindow.location.href; |
|
111 ok(iframeHref == "about:", "Test 1a, the overlay about: content is expected"); |
|
112 |
|
113 var rect = iframe.getBoundingClientRect(); |
|
114 ok(rect.width == 200, "Test 1a, Plugin with id=" + plugin.id + " overlay rect should have 200px width before being replaced by actual plugin"); |
|
115 ok(rect.height == 200, "Test 1a, Plugin with id=" + plugin.id + " overlay rect should have 200px height before being replaced by actual plugin"); |
|
116 |
|
117 var e = overlay.ownerDocument.createEvent("CustomEvent"); |
|
118 e.initCustomEvent("MozPlayPlugin", true, true, null); |
|
119 overlay.dispatchEvent(e); |
|
120 var condition = function() objLoadingContent.activated; |
|
121 waitForCondition(condition, test1b, "Test 1a, Waited too long for plugin to stop play preview"); |
|
122 } |
|
123 |
|
124 // Tests that activating via MozPlayPlugin through the notification works (part 2/2) |
|
125 function test1b() { |
|
126 var plugin = gTestBrowser.contentDocument.getElementById("test"); |
|
127 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent); |
|
128 ok(objLoadingContent.activated, "Test 1b, Plugin should be activated"); |
|
129 |
|
130 is(gPlayPreviewPluginActualEvents, gPlayPreviewPluginExpectedEvents, |
|
131 "There should be exactly one PluginPlayPreview event"); |
|
132 |
|
133 unregisterPlayPreview(); |
|
134 |
|
135 prepareTest(test2, gTestRoot + "plugin_test.html"); |
|
136 } |
|
137 |
|
138 // Tests a page with a working plugin in it -- the mime type was just unregistered. |
|
139 function test2() { |
|
140 var doc = gTestBrowser.contentDocument; |
|
141 var plugin = doc.getElementById("test"); |
|
142 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent); |
|
143 ok(objLoadingContent.pluginFallbackType != Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 2, plugin fallback type should not be PLUGIN_PLAY_PREVIEW"); |
|
144 ok(!objLoadingContent.activated, "Test 2, Plugin should not be activated"); |
|
145 |
|
146 registerPlayPreview('application/x-unknown', 'about:'); |
|
147 |
|
148 prepareTest(test3, gTestRoot + "plugin_test.html"); |
|
149 } |
|
150 |
|
151 // Tests a page with a working plugin in it -- diffent play preview type is reserved. |
|
152 function test3() { |
|
153 var doc = gTestBrowser.contentDocument; |
|
154 var plugin = doc.getElementById("test"); |
|
155 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent); |
|
156 ok(objLoadingContent.pluginFallbackType != Ci.nsIObjectLoadingContent.PLUGIN_PLAY_PREVIEW, "Test 3, plugin fallback type should not be PLUGIN_PLAY_PREVIEW"); |
|
157 ok(!objLoadingContent.activated, "Test 3, Plugin should not be activated"); |
|
158 |
|
159 unregisterPlayPreview(); |
|
160 |
|
161 registerPlayPreview('application/x-test', 'about:'); |
|
162 Services.prefs.setBoolPref("plugins.click_to_play", false); |
|
163 var plugin = getTestPlugin(); |
|
164 plugin.enabledState = Ci.nsIPluginTag.STATE_ENABLED; |
|
165 prepareTest(test4, gTestRoot + "plugin_test.html"); |
|
166 } |
|
167 |
|
168 // Tests a page with a working plugin in it -- click-to-play is off |
|
169 function test4() { |
|
170 var plugin = gTestBrowser.contentDocument.getElementById("test"); |
|
171 var objLoadingContent = plugin.QueryInterface(Ci.nsIObjectLoadingContent); |
|
172 ok(objLoadingContent.activated, "Test 4, Plugin should be activated"); |
|
173 |
|
174 finishTest(); |
|
175 } |
|
176 |