test_dynamicplaylist.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/sbProperties.jsm");
32 
33 function runTest () {
34  // disabled for now, see bug 20531
35  return;
37  testUpdate();
38  testPodcast();
39 }
40 
42 
43 var playlist1 = <>
44 #EXTM3U
45 http://localhost:{PORT_NUMBER}/test1.mp3
46 http://localhost:{PORT_NUMBER}/test2.mp3
47 </>.toString();
48 
49 var playlist2 = <>
50 #EXTM3U
51 http://localhost:{PORT_NUMBER}/test2.mp3
52 http://localhost:{PORT_NUMBER}/test3.mp3
53 </>.toString();
54 
55 function testRegistration() {
56 
57  var dps = Cc["@songbirdnest.com/Songbird/Library/DynamicPlaylistService;1"]
58  .getService(Ci.sbIDynamicPlaylistService);
59  var libraryManager = Cc["@songbirdnest.com/Songbird/library/Manager;1"]
60  .getService(Ci.sbILibraryManager);
61  var library1 = createLibrary("test_dynamicplaylist1", null, false);
62  library1.clear();
63 
64  // Start with no lists scheduled
65  assertFalse(dps.scheduledLists.hasMoreElements());
66 
67  // Register an empty library, should be empty
68  libraryManager.registerLibrary(library1, false);
69  assertFalse(dps.scheduledLists.hasMoreElements());
70 
71  // Create a new list, should get scheduled
72  var uri = newURI("http://foo.com");
73  var dest = Cc["@mozilla.org/file/directory_service;1"]
74  .getService(Ci.nsIProperties)
75  .get("TmpD", Ci.nsIFile);
76  var list = dps.createList(library1, uri, 60, dest);
77 
78  assertTrue(list.QueryInterface(Ci.sbIDynamicMediaList));
79  assertEqual(list.getProperty(SBProperties.isSubscription), "1");
80  assertEqual(list.getProperty(SBProperties.base + "subscriptionURL"), uri.spec);
81  assertEqual(list.getProperty(SBProperties.base + "subscriptionInterval"), "60");
82  assertEqual(list.getProperty(SBProperties.destination), newFileURI(dest).spec);
83 
84  var scheduled = dps.scheduledLists.getNext();
85  assertTrue(scheduled, list);
86 
87  // Unregister the library, list should be unscheduled
88  libraryManager.unregisterLibrary(library1);
89  assertFalse(dps.scheduledLists.hasMoreElements());
90 
91  // Re-register library, list should be scheduled again
92  libraryManager.registerLibrary(library1, false);
93  assertTrue(dps.scheduledLists.getNext(), list);
94 
95  // Delete list, should be unregistered
96  library1.remove(list);
97  assertFalse(dps.scheduledLists.hasMoreElements());
98 
99  libraryManager.unregisterLibrary(library1);
100 }
101 
102 function testUpdate() {
103 
104  var libraryManager = Cc["@songbirdnest.com/Songbird/library/Manager;1"]
105  .getService(Ci.sbILibraryManager);
106  var library1 = createLibrary("test_dynamicplaylist1", null, false);
107  library1.clear();
108  libraryManager.registerLibrary(library1, false);
109 
110  var playlistFile = getFile("test_dynamicplaylist_playlist.m3u");
111  writeFile(playlistFile, playlist1);
112 
113  var server = Cc["@mozilla.org/server/jshttp;1"]
114  .createInstance(Ci.nsIHttpServer);
115 
116  try {
117  server.start(PORT_NUMBER);
118  server.registerDirectory("/", getFile("."));
119 
120  var dps = Cc["@songbirdnest.com/Songbird/Library/DynamicPlaylistService;1"]
121  .getService(Ci.sbIDynamicPlaylistService);
122 
123  var dest = Cc["@mozilla.org/file/directory_service;1"]
124  .getService(Ci.nsIProperties)
125  .get("TmpD", Ci.nsIFile);
126 
127  var url = "http://localhost:" + PORT_NUMBER +
128  "/test_dynamicplaylist_playlist.m3u";
129 
130  var list = dps.createList(library1, newURI(url), 60, dest);
131 
132  // Write the first playlist and then update the subscription
133  writeFile(playlistFile, playlist1);
134 
135  list.update();
136 
137  var sleepCount = 0;
138  while (list.length < 2 && sleepCount++ < 20) {
139  sleep(1000);
140  }
141 
142  // Check the contents of the list
143  assertEqual(list.length, 2);
144 
145  // try to wait for a bit until we succeed at getting the track names
146  for (sleepCount = 0; sleepCount < 20; ++sleepCount) {
147  if (list.getItemByIndex(0).getProperty(SBProperties.trackName) == "test1 title" &&
148  list.getItemByIndex(1).getProperty(SBProperties.trackName) == "test2 title")
149  {
150  break;
151  }
152  sleep(1000);
153  }
154  assertEqual(list.getItemByIndex(0).getProperty(SBProperties.trackName), "test1 title");
155  assertEqual(list.getItemByIndex(1).getProperty(SBProperties.trackName), "test2 title");
156 
157  // Update the playlist file and update again
158  writeFile(playlistFile, playlist2);
159  list.update();
160 
161  sleepCount = 0;
162  while (list.length < 3 && sleepCount++ < 20) {
163  sleep(1000);
164  }
165 
166  // Check the contents of the list
167  assertEqual(list.length, 3);
168  // try to wait for a bit until we succeed at getting the track names
169  for (sleepCount = 0; sleepCount < 20; ++sleepCount) {
170  if (list.getItemByIndex(0).getProperty(SBProperties.trackName) == "test1 title" &&
171  list.getItemByIndex(1).getProperty(SBProperties.trackName) == "test2 title" &&
172  list.getItemByIndex(2).getProperty(SBProperties.trackName) == "test3 title")
173  {
174  break;
175  }
176  sleep(1000);
177  }
178  assertEqual(list.getItemByIndex(0).getProperty(SBProperties.trackName), "test1 title");
179  assertEqual(list.getItemByIndex(1).getProperty(SBProperties.trackName), "test2 title");
180  assertEqual(list.getItemByIndex(2).getProperty(SBProperties.trackName), "test3 title");
181 
182  // TODO: How can we check to see if these files were downloaded?
183  }
184  finally {
185  server.stop(function() {});
186 
187  libraryManager.unregisterLibrary(library1);
188  }
189 }
190 
191 function testPodcast() {
192  // Create a test library.
193  var libraryManager = Cc["@songbirdnest.com/Songbird/library/Manager;1"]
194  .getService(Ci.sbILibraryManager);
195  var library1 = createLibrary("test_dynamicplaylist1", null, false);
196  library1.clear();
197  libraryManager.registerLibrary(library1, false);
198 
199  // Create and validate a podcast.
200  var dps = Cc["@songbirdnest.com/Songbird/Library/DynamicPlaylistService;1"]
201  .getService(Ci.sbIDynamicPlaylistService);
202  var uri = newURI("http://foo.com");
203  var podcast = dps.createPodcast(library1, uri);
204  assertTrue(podcast);
205  assertEqual(podcast.getProperty(SBProperties.customType), "podcast");
206 
207  // Clean up.
208  libraryManager.unregisterLibrary(library1);
209 }
210 
211 function writeFile(file, data) {
212  // file is nsIFile, data is a string
213  var foStream = Cc["@mozilla.org/network/file-output-stream;1"]
214  .createInstance(Ci.nsIFileOutputStream);
215 
216  foStream.init(file, 0x02 | 0x08 | 0x20, 0664, 0); // write, create, truncate
217  foStream.write(data, data.length);
218  foStream.close();
219 }
function sleep(ms, suppressOutput)
var playlist2
const Cc
function testUpdate()
function writeFile(file, data)
function assertTrue(aTest, aMessage)
function assertEqual(aExpected, aActual, aMessage)
function server(port, basePath)
Definition: httpd.js:4758
function getFile(fileName)
var libraryManager
function newFileURI(file)
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
function testPodcast()
function assertFalse(aTest, aMessage)
function url(spec)
var uri
Definition: FeedWriter.js:1135
function testRegistration()
const Ci
function getTestServerPortNumber()
observe data
Definition: FeedWriter.js:1329
var PORT_NUMBER
function runTest()
Advanced DataRemote unit tests.
var file