head_mediamanager.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-2009 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 __defineGetter__("Application", function() {
32  delete this.Application;
33  return this.Application = Cc["@mozilla.org/fuel/application;1"]
34  .getService(Ci.fuelIApplication);
35 });
36 
37 
38 Components.utils.import('resource://gre/modules/XPCOMUtils.jsm');
39 
43 var SB_NS = "http://songbirdnest.com/data/1.0#";
44 var SB_MEDIAFILEMANAGER = "@songbirdnest.com/Songbird/media-manager/file;1";
45 
46 var SB_MM_PROP_FOLDER = "media-folder";
47 var SB_MM_PROP_FMTFILE = "file-format";
48 var SB_MM_PROP_FMTDIR = "dir-format";
49 
50 
51 // An array of what our test results should be
52 var gResultInformation = [
53  { originalFileName: "TestFile1.mp3",
54  expectedFileName: "1 - Sample.mp3",
55  expectedFolder: "Managed/Songbird/Unit Test Classics",
56  expectedAction: Ci.sbIMediaFileManager.MANAGE_COPY |
57  Ci.sbIMediaFileManager.MANAGE_RENAME },
58  { originalFileName: "TestFile2.mp3",
59  expectedFileName: "2 - Sample.mp3",
60  expectedFolder: "Managed/Songbird/Unit Test Classics",
61  expectedAction: Ci.sbIMediaFileManager.MANAGE_COPY |
62  Ci.sbIMediaFileManager.MANAGE_RENAME },
63  { originalFileName: "TestFile3.mp3",
64  expectedFileName: "3 - Sample.mp3",
65  expectedFolder: "Managed/Songbird/Unit Test Classics",
66  expectedAction: Ci.sbIMediaFileManager.MANAGE_COPY |
67  Ci.sbIMediaFileManager.MANAGE_RENAME },
68 
69  { originalFileName: "TestFile4.mp3",
70  expectedFileName: "1 - TestFile4.mp3",
71  expectedFolder: "Managed/Unknown Artist/Unknown Album",
72  expectedAction: Ci.sbIMediaFileManager.MANAGE_COPY |
73  Ci.sbIMediaFileManager.MANAGE_RENAME },
74 
75  { originalFileName: "TestFile5.mp3",
76  expectedFileName: "1 - Sample.mp3",
77  expectedFolder: "Managed/Songbird/Unknown Album",
78  expectedAction: Ci.sbIMediaFileManager.MANAGE_COPY |
79  Ci.sbIMediaFileManager.MANAGE_RENAME },
80 
81  { originalFileName: "TestFile6.mp3",
82  expectedFileName: "1 - Sample.mp3",
83  expectedFolder: "Managed/Unknown Artist/Unit Test Classics",
84  expectedAction: Ci.sbIMediaFileManager.MANAGE_COPY |
85  Ci.sbIMediaFileManager.MANAGE_RENAME },
86 
87  { originalFileName: "TestFile7.mp3",
88  expectedFileName: "01 - Sample7.mp3",
89  expectedFolder: "Managed/Songbird/Unit Test Classics",
90  expectedAction: Ci.sbIMediaFileManager.MANAGE_COPY |
91  Ci.sbIMediaFileManager.MANAGE_RENAME },
92 
93  { originalFileName: "TestFile8.mp3",
94  expectedFileName: "11 - Sample8.mp3",
95  expectedFolder: "Managed/Songbird/Unit Test Classics",
96  expectedAction: Ci.sbIMediaFileManager.MANAGE_MOVE |
97  Ci.sbIMediaFileManager.MANAGE_RENAME }
98 ];
99 
100 // Test library to use.
101 var gTestLibrary;
102 // Items we add to the library to test our file management
103 var gTestMediaItems;
104 // We use this in many places for creating our file paths
105 var gFileLocation = "testharness/mediamanager/files/";
106 
107 
112 function getMediaManagerProperties () {
113 
114  var separator = "/";
115  if (getPlatform() == "Windows_NT") {
116  separator = "\\";
117  }
118  var managedFolder = testFolder.clone();
119  managedFolder.append("Managed");
120  // Create the folder
121  managedFolder.create(Ci.nsIFile.DIRECTORY_TYPE, 0777);
122 
123  var properties = Cc["@mozilla.org/hash-property-bag;1"]
124  .createInstance(Ci.nsIWritablePropertyBag2);;
125 
126  properties.setPropertyAsInterface(SB_MM_PROP_FOLDER, managedFolder);
127  properties.setPropertyAsAString(SB_MM_PROP_FMTFILE,
128  SB_NS + "trackNumber," +
129  " - ," +
130  SB_NS + "trackName");
131  properties.setPropertyAsAString(SB_MM_PROP_FMTDIR,
132  SB_NS + "artistName," +
133  separator + "," +
134  SB_NS + "albumName");
135 
136  return properties;
137 }
138 
143 function addItemsToLibrary(aLibrary) {
144  var toAdd = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
145  .createInstance(Ci.nsIMutableArray);
146  var propertyArray = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
147  .createInstance(Ci.nsIMutableArray);
148  var ioService = Cc["@mozilla.org/network/io-service;1"]
149  .getService(Ci.nsIIOService);
150  for (var i = 0; i < gResultInformation.length; i++) {
151  // Set up the item
152  var newFile = testFolder.clone();
153  newFile = appendPathToDirectory(newFile,
154  gResultInformation[i].originalFileName);
155  toAdd.appendElement(ioService.newFileURI(newFile), false);
156 
157  // Setup default properties for this item
158  var props = Cc["@songbirdnest.com/Songbird/Properties/MutablePropertyArray;1"]
159  .createInstance(Ci.sbIMutablePropertyArray);
160  props.appendProperty(SB_NS + "contentLength", i + 1);
161  props.appendProperty(SB_NS + "trackNumber", i + 1);
162  propertyArray.appendElement(props, false);
163  }
164 
165  aLibrary.batchCreateMediaItems(toAdd, propertyArray);
166 }
167 
178 function checkItem(aMediaItem, aResultInformationIndex, aShouldHaveOriginal) {
179  // First get the current path from the item
180  var current = aMediaItem.contentSrc;
181  if (!(current instanceof Ci.nsIFileURL)) {
182  log("item [" + current.spec + "] is not a file URL!");
183  return false;
184  }
185  current = current.file;
186 
187  // Now put together the expected path for the item
188  var expected = testFolder.clone();
189  var expFolder = gResultInformation[aResultInformationIndex].expectedFolder;
190  expected = appendPathToDirectory(expected, expFolder);
191  expected.append(gResultInformation[aResultInformationIndex].expectedFileName);
192 
193  // Ensure that the file has been copied to its new location with proper
194  // filename and folder path.
195  if (!current.equals(expected)) {
196  log("current value ["+ current.path +
197  "] is not expectd value [" + expected.path + "]!");
198  return false;
199  }
200 
201  // Now check that the original file is still in place or not
202  var original = testFolder.clone();
203  var origName = gResultInformation[aResultInformationIndex].originalFileName;
204  original = appendPathToDirectory(original, origName);
205 
206  if (!original.exists() && aShouldHaveOriginal) {
207  log("original doesn't exist, but expected!");
208  return false;
209  } else if (original.exists() && !aShouldHaveOriginal) {
210  log("original exists, but expected to be removed!");
211  return false;
212  }
213 
214  // All tests passed so the file has been organized correctly.
215  return true;
216 }
217 
221 function checkDeletedItem(aMediaItem) {
222  var fileURI = aMediaItem.contentSrc.QueryInterface(Ci.nsIFileURL);
223  if (fileURI instanceof Ci.nsIFileURL) {
224  var file = fileURI.file;
225  return !file.exists();
226  }
227  return false;
228 }
229 
233 function TestMediaListListener() {
234  this._added = Components.classes["@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
235  .createInstance(Components.interfaces.nsIMutableArray);
236 }
237 TestMediaListListener.prototype = {
238  _added: null,
239  _retval: false,
240 
241  get added() {
242  return this._added;
243  },
244 
245  onItemAdded: function onItemAdded(list, item, index) {
246  this._added.appendElement( item, false );
247  //this._added.push({list: list, item: item, index: index});
248  return this._retval;
249  },
250 
251  onBeforeItemRemoved: function onBeforeItemRemoved(list, item, index) {
252  return this._retval;
253  },
254 
255  onAfterItemRemoved: function onAfterItemRemoved(list, item, index) {
256  return this._retval;
257  },
258 
259  onItemUpdated: function onItemUpdated(list, item, properties) {
260  return this._retval;
261  },
262 
263  onItemMoved: function onItemMoved(list, fromIndex, toIndex) {
264  return this._retval;
265  },
266 
267  onBatchBegin: function onBatchBegin(list) {
268  },
269 
270  onBatchEnd: function onBatchEnd(list) {
271  },
272  onBeforeListCleared: function onBeforeListCleared() {
273  return this._retval;
274  },
275  onListCleared: function onListCleared() {
276  return this._retval;
277  },
278  QueryInterface: XPCOMUtils.generateQI([Ci.sbIMediaListListener])
279 }
280 
281 
285 function getCopyOfFolder(folder, tempName) {
286  assertNotEqual(folder, null);
287  var tempFolder = getTempFolder();
288  folder.copyTo(tempFolder, tempName);
289  folder = tempFolder.clone();
290  folder.append(tempName);
291  assertEqual(folder.exists(), true);
292  return folder;
293 }
294 
298 function appendPathToDirectory(directory, path) {
299  var nodes = path.split("/");
300  for ( var i = 0, end = nodes.length; i < end; i++ )
301  {
302  directory.append( nodes[ i ] );
303  }
304  return directory;
305 }
306 
311 var gTempFolder = null;
312 function getTempFolder() {
313  if (gTempFolder) {
314  return gTempFolder;
315  }
316  gTempFolder = Components.classes["@mozilla.org/file/directory_service;1"]
317  .getService(Components.interfaces.nsIProperties)
318  .get("TmpD", Components.interfaces.nsIFile);
319  gTempFolder.append("songbird_mediamanager_tests.tmp");
320  gTempFolder.createUnique(Components.interfaces.nsIFile.DIRECTORY_TYPE, 0777);
321  return gTempFolder;
322 }
323 
328 function removeTempFolder() {
329  if (gTempFolder && gTempFolder.exists()) {
330  gTempFolder.remove(true);
331  } else {
332  log("\n\n\nMedia Manager Test may not have performed cleanup. Temp files may exist.\n\n\n");
333  }
334 }
335 
339 function removeTestLibraries() {
340  var libraryManager = Cc["@songbirdnest.com/Songbird/library/Manager;1"].
341  getService(Ci.sbILibraryManager);
342 
343  // Since unregisterLibrary does not clear or delete the library we have
344  // to clear it so that subsequent createLibrary calls with the same guid do
345  // not have items in it.
346  gTestLibrary.clear();
347  libraryManager.unregisterLibrary(gTestLibrary);
348 }
const Cc
removeTestLibraries()
var testFolder
var Application
Definition: sbAboutDRM.js:37
function getPlatform()
function assertNotEqual(aExpected, aActual, aMessage)
function log(s)
sbOSDControlService prototype QueryInterface
function removeTempFolder()
var ioService
function assertEqual(aExpected, aActual, aMessage)
var gFileLocation
Test reading and writing artwork from various media formats.
getService(Ci.sbIFaceplateManager)
var libraryManager
const SB_NS
__defineGetter__("Application", function(){delete this.Application;return this.Application=Cc["@mozilla.org/fuel/application;1"].getService(Ci.fuelIApplication);})
return null
Definition: FeedWriter.js:1143
var expected
function getTempFolder()
const Ci
function getCopyOfFolder(folder, tempName)
Copy the given folder to tempName, returning an nsIFile for the new location.
var gTestMediaItems
_getSelectedPageStyle s i
function TestMediaListListener()
var gTestLibrary
var file