head_remoteapi.js
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
35 
36 function beginWindowTest(url, continueFunction) {
37  var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"]
38  .getService(Ci.nsIWindowWatcher);
39 
40  testWindow = ww.openWindow(null, url, null, null, null);
41  testWindow.addEventListener("load", function() {
42  continueFunction.apply(this);
43  }, false);
44  testWindowFailed = false;
45  testPending();
46 }
47 
48 function setAllAccess () {
49  log("");
50  log("--------------------------------------------------------------------------------");
51  log("*********************** Setting All Access for RemoteAPI ***********************");
52  log("--------------------------------------------------------------------------------");
53  log("");
54  setRapiPref("playback_control_disable", false);
55  setRapiPref("playback_read_disable", false);
56  setRapiPref("library_read_disable", false);
57  setRapiPref("library_write_disable", false);
58 }
59 
60 function setNoAccess() {
61  log("");
62  log("--------------------------------------------------------------------------------");
63  log("*********************** Setting No Access for RemoteAPI ************************");
64  log("--------------------------------------------------------------------------------");
65  log("");
66  setRapiPref("playback_control_disable", true);
67  setRapiPref("playback_read_disable", true);
68  setRapiPref("library_read_disable", true);
69  setRapiPref("library_write_disable", true);
70 }
71 
72 function setDefaultAccess() {
73  log("");
74  log("--------------------------------------------------------------------------------");
75  log("******************** Setting Default Access for RemoteAPI **********************");
76  log("--------------------------------------------------------------------------------");
77  log("");
78  setRapiPref("playback_control_disable", false);
79  setRapiPref("playback_read_disable", false);
80  setRapiPref("library_read_disable", true);
81  setRapiPref("library_write_disable", true);
82 }
83 
84 function cleanup() {
85  var libraryManager = Cc["@songbirdnest.com/Songbird/library/Manager;1"]
86  .getService(Ci.sbILibraryManager);
87  libraryManager.mainLibrary.clear();
89 
90  // reset this
91  setRapiPref("promptForApproval", true);
92 }
93 
94 function endWindowTest(e) {
95  cleanup();
96  if (!testWindowFailed) {
97  testWindowFailed = true;
98  if (testBrowser && testListener) {
99  testBrowser.webProgress.removeProgressListener(testListener);
100  testBrowser = null;
101  testListener = null;
102  }
103  if (testWindow) {
104  testWindow.close();
105  testWindow = null;
106  }
107  if (e) {
108  fail(e);
109  }
110  else {
111  testFinished();
112  }
113  }
114 }
115 
116 function continueWindowTest(fn, parameters) {
117 
118  try {
119  fn.apply(this, parameters);
120  }
121  catch(e) {
122  endWindowTest();
123  fail(e);
124  }
125 }
126 
127 function safeSetTimeout(closure, timeout) {
128  testWindow.setTimeout(function() {
129  try {
130  closure.apply(this);
131  }
132  catch(e) {
133  endWindowTest();
134  fail(e);
135  }
136  }, timeout);
137 
138 }
139 
140 function beginRemoteAPITest(page, continueFunction) {
141 
142  // no dialogs should popup for unit tests
143  setRapiPref("promptForApproval", false);
144 
145  var port = getTestServerPortNumber();
146 
147  testServer = Cc["@mozilla.org/server/jshttp;1"]
148  .createInstance(Ci.nsIHttpServer);
149  testServer.start(port);
150  testServer.registerDirectory("/", getFile("."));
151 
152  var url = "data:application/vnd.mozilla.xul+xml," +
153  "<?xml-stylesheet href='chrome://global/skin' type='text/css'?>" +
154  "<?xml-stylesheet href='chrome://songbird/content/bindings/bindings.css' type='text/css'?>" +
155  "<window xmlns='http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul'/>";
156 
157  beginWindowTest(url, function() { setupBrowser(page, port, continueFunction); })
158 }
159 
160 function setupBrowser(page, port, continueFunction) {
161 
162  testWindow.resizeTo(200, 200);
163 
164  const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
165 
166  var document = testWindow.document;
167 
168  testBrowser = document.createElementNS(XUL_NS, "browser");
169  testBrowser.setAttribute("flex", "1");
170  document.documentElement.appendChild(testBrowser);
171 
172  var url = "http://127.0.0.1:" + port + "/" + page + "?" + Math.random();
173  testListener = new ContinuingWebProgressListener(url, continueFunction);
174  testBrowser.webProgress.addProgressListener(testListener, Ci.nsIWebProgress.NOTIFY_STATE_REQUEST);
175  testBrowser.loadURI(url);
176  testBrowserWindow = testBrowser.contentWindow;
177 }
178 
179 function endRemoteAPITest(e) {
180  testServer.stop(function() {});
181  endWindowTest(e);
182 
183  // Clear object references to avoid leaks.
184  testWindow = null;
185  testServer = null;
187  testBrowser = null;
188  testListener = null;
189 }
190 
191 function getFile(fileName) {
192  var file = Cc["@mozilla.org/file/directory_service;1"]
193  .getService(Ci.nsIProperties)
194  .get("resource:app", Ci.nsIFile);
195  file = file.clone();
196  file.append("testharness");
197  file.append("remoteapi");
198  file.append(fileName);
199  return file;
200 }
201 
202 function ContinuingWebProgressListener(url, func) {
203  this._url = url;
204  this._func = func;
205 }
206 
207 var scope = this;
208 ContinuingWebProgressListener.prototype.onStateChange =
209 function(aProgress, aRequest, aFlag, aStatus)
210 {
211  if(aFlag & Ci.nsIWebProgressListener.STATE_STOP && aRequest.name == this._url) {
212  this._func.apply(scope);
213  }
214 }
215 
216 ContinuingWebProgressListener.prototype.onLocationChange =
217 function(aProgress, aRequest, aURI) {}
218 ContinuingWebProgressListener.prototype.onProgressChange = function() {}
219 ContinuingWebProgressListener.prototype.onStatusChange = function() {}
220 ContinuingWebProgressListener.prototype.onSecurityChange = function() {}
221 ContinuingWebProgressListener.prototype.onLinkIconAvailable = function() {}
222 
223 ContinuingWebProgressListener.prototype.QueryInterface =
224 function(aIID)
225 {
226  if (aIID.equals(Ci.nsIWebProgressListener) ||
227  aIID.equals(Ci.nsISupportsWeakReference) ||
228  aIID.equals(Ci.nsISupports))
229  return this;
230  throw Cr.NS_NOINTERFACE;
231 }
232 
233 function setRapiPref(name, value) {
234  var prefs = Cc["@mozilla.org/preferences-service;1"]
235  .getService(Ci.nsIPrefService);
236  prefs = prefs.getBranch("songbird.rapi.");
237  prefs.setBoolPref(name, value);
238 }
239 
function safeSetTimeout(closure, timeout)
function beginRemoteAPITest(page, continueFunction)
const Cc
function endWindowTest(e)
function fail(aMessage)
function setNoAccess()
function ContinuingWebProgressListener(url, func)
const XUL_NS
Definition: FeedWriter.js:83
var testBrowser
function setupBrowser(page, port, continueFunction)
function log(s)
function testFinished()
var scope
function beginWindowTest(url, continueFunction)
function setAllAccess()
var testWindowFailed
var testBrowserWindow
var libraryManager
return null
Definition: FeedWriter.js:1143
function cleanup()
var testListener
function getFile(fileName)
var testWindow
Test file.
function url(spec)
var prefs
Definition: FeedWriter.js:1169
countRef value
Definition: FeedWriter.js:1423
const Cr
function setDefaultAccess()
const Ci
function endRemoteAPITest(e)
function getTestServerPortNumber()
jQuery fn
var testServer
function continueWindowTest(fn, parameters)
var file
function testPending()