test_mediapages.js
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
31 Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
32 Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
33 
34 var pageMgr = null;
35 
36 function runTest () {
37 
38  log("Testing MediaPageManager Service:");
39 
40  pageMgr = Cc["@songbirdnest.com/Songbird/MediaPageManager;1"]
41  .getService(Ci.sbIMediaPageManager);
42  assertTrue(pageMgr,
43  "Manager service could not be retrieved!");
44 
46 
47  log("OK");
48 }
49 
50 const DEFAULTPAGE1 = "chrome://songbird/content/mediapages/filtersPage.xul"
51 const DEFAULTPAGE2 = "chrome://songbird/content/mediapages/playlistPage.xul"
52 const EXTENSIONPAGE = "chrome://songbird-test-media-page/content/testMediaPage.xul"
53 const EXTENSIONPAGEDOWNLOADS = "chrome://songbird-test-media-page/content/testMediaPage.xul?downloads"
54 const EXTENSIONPAGELIBRARY = "chrome://songbird-test-media-page/content/testMediaPage.xul?library"
55 const EXTENSIONPAGEVIDEO = "chrome://songbird-test-media-page/content/testMediaPage.xul?video"
56 
57 const BADURL = "http://non/registered/url.xul";
58 const URL1 = "http://fake/url1.xul";
59 const URL2 = "http://fake/url2.xul";
60 const URL3 = "http://fake/url3.xul";
61 
63  // Create our temp library
64  var library1 = createLibrary("test_MediaPages", null, true);
65 
66  // Create list of type "simple"
67  var list1 = library1.createMediaList("simple");
68 
69  {
70  // Verify that the built-in pages have been registered
71  // and that giving a list that has no default returns the global default
72  // page
73  var pageInfo = pageMgr.getPage(library1);
74  assertTrue(pageInfo, "no media pages found for library");
75  assertEqual(pageInfo.contentUrl, DEFAULTPAGE2,
76  "built-in page not found");
77 
78  pageInfo = pageMgr.getPage(list1);
79  assertEqual(pageInfo.contentUrl, DEFAULTPAGE2,
80  "built-in page not found");
81 
82  // Set a default for the list
83  list1.setProperty(SBProperties.defaultMediaPageURL, BADURL);
84 
85  // Verify that giving a list that has an obsolete default returns the
86  // global default page
87  pageInfo = pageMgr.getPage(list1);
88  assertEqual(pageInfo.contentUrl, DEFAULTPAGE2,
89  "setting a default page to a BADURL should give DEFAULT"
90  );
91  var defaultInfo2 = pageInfo;
92 
93  // Now verify that setting the media page for our library to the filterlist
94  // works
95  list1.setProperty(SBProperties.defaultMediaPageURL, DEFAULTPAGE1);
96  pageInfo = pageMgr.getPage(list1);
98  pageInfo.contentUrl, DEFAULTPAGE1,
99  "setting playlist mediapage to filterlist should work"
100  );
101  var defaultInfo1 = pageInfo;
102 
103  // try setting a custom type
104  list1.setProperty(SBProperties.customType, "download");
105  assertEqual(list1.getProperty(SBProperties.customType), "download",
106  "customType should be set to download.");
107 
108  var pages = ArrayConverter.JSArray(pageMgr.getAvailablePages(list1))
109  .map(function(page)
110  page.QueryInterface(Ci.sbIMediaPageInfo));
111  assertSetsEqual(pages.map(function(page) page.contentUrl),
112  [DEFAULTPAGE1,
113  DEFAULTPAGE2,
114  EXTENSIONPAGE,
115  EXTENSIONPAGEDOWNLOADS,
116  EXTENSIONPAGEVIDEO]);
117 
118  // now test that setting the opt-out works
119  list1.setProperty(SBProperties.onlyCustomMediaPages, "1");
120  pages = ArrayConverter.JSArray(pageMgr.getAvailablePages(list1))
121  .map(function(page)
122  page.QueryInterface(Ci.sbIMediaPageInfo));
123  assertSetsEqual(pages.map(function(page) page.contentUrl),
124  [DEFAULTPAGE1,
125  DEFAULTPAGE2,
126  EXTENSIONPAGEDOWNLOADS,
127  EXTENSIONPAGEVIDEO]);
128 
129  // unregister default pages and verify that nothing remains
130  // we're using details about the implementation on unregisterPage here :(
131  for each (let url in [DEFAULTPAGE1,
132  DEFAULTPAGE2,
133  EXTENSIONPAGE,
134  EXTENSIONPAGEDOWNLOADS,
135  EXTENSIONPAGELIBRARY,
136  EXTENSIONPAGEVIDEO])
137  {
138  pageMgr.unregisterPage({contentUrl: url});
139  }
140 
141  var enumerator = pageMgr.getAvailablePages();
142  assertFalse(enumerator.hasMoreElements(),
143  { toString: function() {
144  /* use an object with a toString() to delay evaluating enumerator.getNext()
145  * until we're sure there will be at least one element
146  * but we still want to get it to get a more useful error out of it
147  */
148  return "after unregistering the one built-in page and the extension, " +
149  "the list should be empty: " +
150  enumerator.getNext()
151  .QueryInterface(Ci.sbIMediaPageInfo)
152  .contentUrl;
153  }
154  }
155  );
156  }
157 
158  // Tabula rasa
159  {
160  library1.clear();
161  list1 = library1.createMediaList("simple");
162 
163  // Set up a match for all simple lists
164  var page_simpletype = pageMgr.registerPage("MatchSimpleType",
165  URL2, null,
166  function(aList) { return (aList.type == "simple"); } );
167 
168  // Verify that matching works :
169  // Step 1, only a type match is registered, must match
170  pageInfo = pageMgr.getPage(list1);
171  assertEqual(pageInfo.contentUrl, URL2);
172 
173  // Set up a match for all lists again
174  var page_matchall = pageMgr.registerPage("MatchAll",
175  URL1, null,
176  function(aList) { return true; } );
177 
178  // Verify that matching works:
179  // Step 2, type(*) + global
180  pageInfo = pageMgr.getPage(list1);
181  assertEqual(pageInfo.contentUrl, URL2);
182 
183  // Create a list of type "smart"
184  var list2 = library1.createMediaList("smart");
185 
186  // Verify that matching works:
187  // Step 3, non matching type + global(*)
188  pageInfo = pageMgr.getPage(list2);
189  assertEqual(pageInfo.contentUrl, URL1);
190 
191  // Set up a match for all smart lists
192  var page_smarttype = pageMgr.registerPage("MatchSmartType",
193  URL3, null,
194  function(aList) { return (aList.type == "smart"); } );
195 
196  // Verify that matching works:
197  // Step 4, type + global(*) + type
198  pageInfo = pageMgr.getPage(list2);
199  assertEqual(pageInfo.contentUrl, URL1);
200 
201  // Set the default for the smart list to the smart page
202  list2.setProperty(SBProperties.defaultMediaPageURL, URL3);
203 
204  // Verify that matching works:
205  // Step 5, bypass search, use list default, check that its match works
206  pageInfo = pageMgr.getPage(list2);
207  assertEqual(pageInfo.contentUrl, URL3);
208 
209  // Set the default for the smart list to the simple page (non-matching page
210  // on purpose)
211  list2.setProperty(SBProperties.defaultMediaPageURL, URL2);
212 
213  // Verify that matching works:
214  // Step 6, check that list default is dropped when it does not match
215  pageInfo = pageMgr.getPage(list2);
216  assertEqual(pageInfo.contentUrl, URL1);
217  }
218 
219  // Tabula rasa
220  {
221  library1.clear();
222  list1 = library1.createMediaList("simple");
223  list2 = library1.createMediaList("smart");
224 
225  // Save page_smarttype as user state for list2
226  pageMgr.setPage(list2, page_smarttype);
227 
228  // Verify that matching works:
229  // Step 7, bypass search, use saved state, check that its match works
230  pageInfo = pageMgr.getPage(list2);
231  assertEqual(pageInfo.contentUrl, URL3);
232 
233  // Save page_simpletype as user state for list2 (non-matching page on purpose)
234  pageMgr.setPage(list2, page_simpletype);
235 
236  // Verify that matching works:
237  // Step 8, check that saved state is dropped when it does not match
238  pageInfo = pageMgr.getPage(list2);
239  assertEqual(pageInfo.contentUrl, URL1);
240 
241  // Verify that the internal page list really looks like what we
242  // expected all along
243  enumerator = pageMgr.getAvailablePages();
244  // 0 elements ? no !
245  assertEqual(enumerator.hasMoreElements(), true);
246  // element 0 is type match = simple
247  assertEqual(enumerator.getNext().contentUrl, URL2);
248  // 1 element ? no !
249  assertEqual(enumerator.hasMoreElements(), true);
250  // element 1 is global match
251  assertEqual(enumerator.getNext().contentUrl, URL1);
252  // 2 elements ? no !
253  assertEqual(enumerator.hasMoreElements(), true);
254  // element 2 is type match = smart
255  assertEqual(enumerator.getNext().contentUrl, URL3);
256  // any more element ? no !
257  assertEqual(enumerator.hasMoreElements(), false);
258 
259  // Now that we checked matching priorities based on registration order,
260  // check that multiple matches work when listing pages
261 
262  // This should return the simple type match and the global match
263  enumerator = pageMgr.getAvailablePages(list1);
264  // perform list check
265  assertEqual(enumerator.hasMoreElements(), true);
266  assertEqual(enumerator.getNext().contentUrl, URL2);
267  assertEqual(enumerator.hasMoreElements(), true);
268  assertEqual(enumerator.getNext().contentUrl, URL1);
269  assertEqual(enumerator.hasMoreElements(), false);
270 
271  // This should return the global match and the smart type match
272  enumerator = pageMgr.getAvailablePages(list2);
273  // perform list check
274  assertEqual(enumerator.hasMoreElements(), true);
275  assertEqual(enumerator.getNext().contentUrl, URL1);
276  assertEqual(enumerator.hasMoreElements(), true);
277  assertEqual(enumerator.getNext().contentUrl, URL3);
278  assertEqual(enumerator.hasMoreElements(), false);
279 
280  // Verify that unregistering pages works with multiple pages
281 
282  // Step 1, remove 2nd of 3 items
283  pageMgr.unregisterPage(page_matchall);
284  // perform list check
285  enumerator = pageMgr.getAvailablePages();
286  assertEqual(enumerator.hasMoreElements(), true);
287  assertEqual(enumerator.getNext().contentUrl, URL2);
288  assertEqual(enumerator.hasMoreElements(), true);
289  assertEqual(enumerator.getNext().contentUrl, URL3);
290  assertEqual(enumerator.hasMoreElements(), false);
291 
292  // Step 2, remove 2nd of 2 items
293  pageMgr.unregisterPage(page_smarttype);
294  // perform list check again
295  enumerator = pageMgr.getAvailablePages();
296  assertEqual(enumerator.hasMoreElements(), true);
297  assertEqual(enumerator.getNext().contentUrl, URL2);
298  assertEqual(enumerator.hasMoreElements(), false);
299 
300  // Step 3, remove last item
301  pageMgr.unregisterPage(page_simpletype);
302  // perform list check again
303  enumerator = pageMgr.getAvailablePages();
304  assertEqual(enumerator.hasMoreElements(), false);
305  }
306 
307  library1.clear();
308 
309 }
310 
311 
312 function createLibrary(databaseGuid, databaseLocation) {
313 
314  var directory;
315  if (databaseLocation) {
316  directory = databaseLocation.QueryInterface(Ci.nsIFileURL).file;
317  }
318  else {
319  directory = Cc["@mozilla.org/file/directory_service;1"].
320  getService(Ci.nsIProperties).
321  get("ProfD", Ci.nsIFile);
322  directory.append("db");
323  }
324 
325  var file = directory.clone();
326  file.append(databaseGuid + ".db");
327 
328  var libraryFactory =
329  Cc["@songbirdnest.com/Songbird/Library/LocalDatabase/LibraryFactory;1"]
330  .getService(Ci.sbILibraryFactory);
331  var hashBag = Cc["@mozilla.org/hash-property-bag;1"].
332  createInstance(Ci.nsIWritablePropertyBag2);
333  hashBag.setPropertyAsInterface("databaseFile", file);
334  var library = libraryFactory.createLibrary(hashBag);
335  try {
336  library.clear();
337  }
338  catch(e) {
339  }
340 
341  return library;
342 }
const URL2
const Cc
function log(s)
const URL1
sidebarFactory createInstance
Definition: nsSidebar.js:351
function assertTrue(aTest, aMessage)
function assertSetsEqual(s1, s2)
function assertEqual(aExpected, aActual, aMessage)
getService(Ci.sbIFaceplateManager)
const DEFAULTPAGE1
function createLibrary(databaseGuid, databaseLocation)
return null
Definition: FeedWriter.js:1143
var pageMgr
function assertFalse(aTest, aMessage)
function url(spec)
function runTest()
Advanced DataRemote unit tests.
const Ci
const URL3
var file
function testMediaPageManager()