test_metadatajob_pref.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 var gTestFileLocation = "testharness/metadatamanager/files/";
32 
36 function runTest() {
37  /* SETUP */
38 
39  // Make a copy of everything in the test file folder
40  // so that any accidental changes don't interfere with other tests
41  var testFolder = getCopyOfFolder(newAppRelativeFile(gTestFileLocation), "_temp_writing_files");
42 
43  // Now find all the media files in our testing directory
44  var urls = getMediaFilesInFolder(testFolder);
45 
46  // Make sure we have files to test
47  assertEqual(urls.length > 0, true);
48 
50  // Import the test items //
52  var library = createNewLibrary( "test_metadatajob_writing_library" );
53  var items = importFilesToLibrary(urls, library);
54  assertEqual(items.length, urls.length);
55 
56  var prefSvc = Cc["@mozilla.org/preferences-service;1"]
57  .getService(Ci.nsIPrefBranch);
58  var oldWritingEnabledPref = prefSvc.getBoolPref("songbird.metadata.enableWriting");
59 
60  // While we're at it, confirm that metadata can only be written when allowed via prefs
61  prefSvc.setBoolPref("songbird.metadata.enableWriting", false);
62  try {
63  startMetadataWriteJob(items);
64  // This line should not be reached, as startMetadataWriteJob should throw NS_ERROR_NOT_AVAILABLE
65  throw new Error("MetadataJobManager does not respect enableWriting pref!");
66  } catch (e) {
67  if (Components.lastResult != Components.results.NS_ERROR_NOT_AVAILABLE) {
68  throw new Error("MetadataJobManager does not respect enableWriting pref!");
69  }
70  }
71 
72  prefSvc.setBoolPref("songbird.metadata.enableWriting", oldWritingEnabledPref);
73  // We're done, so kill all the temp files
74  testFolder.remove(true);
75  job = null;
76  gTest = null;
77 }
78 
79 
83 function getMediaFilesInFolder(folder) {
84  var scan = Cc["@songbirdnest.com/Songbird/FileScan;1"]
85  .createInstance(Ci.sbIFileScan);
86  var query = Cc["@songbirdnest.com/Songbird/FileScanQuery;1"]
87  .createInstance(Ci.sbIFileScanQuery);
88  query.setDirectory(folder.path);
89  query.setRecurse(true);
90 
91  for each (var extension in gSupportedFileExtensions) {
92  query.addFileExtension(extension);
93  }
94 
95  scan.submitQuery(query);
96 
97  log("Scanning...");
98 
99  while (query.isScanning()) {
100  sleep(1000);
101  }
102 
103  assertEqual(query.getFileCount() > 0, true);
104  var urls = query.getResultRangeAsURIStrings(0, query.getFileCount() - 1);
105 
106  fileScan.finalize();
107 
108  return urls;
109 }
110 
111 
115 function importFilesToLibrary(files, library) {
116  var items = library.batchCreateMediaItems(files, null, true);
117  assertEqual(items.length, files.length);
118  var jsItems = [];
119  for (var i = 0; i < items.length; i++) {
120  jsItems.push(items.queryElementAt(i, Ci.sbIMediaItem));
121  }
122  return jsItems;
123 }
124 
125 
129 function startMetadataWriteJob(items) {
130  var array = Components.classes["@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
131  .createInstance(Components.interfaces.nsIMutableArray);
132  for each (var item in items) {
133  array.appendElement(item, false);
134  }
135  manager = Components.classes["@songbirdnest.com/Songbird/FileMetadataService;1"]
136  .getService(Components.interfaces.sbIFileMetadataService);
137  return manager.write(array);
138 }
139 
function startMetadataWriteJob(items)
let prefSvc
function sleep(ms, suppressOutput)
const Cc
function newAppRelativeFile(path)
var testFolder
inArray array
function getMediaFilesInFolder(folder)
function log(s)
function createNewLibrary(databaseGuid, databaseLocation)
var gTestFileLocation
Test creation and execution of metadata writing jobs.
var gSupportedFileExtensions
function assertEqual(aExpected, aActual, aMessage)
function runTest()
Advanced DataRemote unit tests.
return null
Definition: FeedWriter.js:1143
const Ci
function getCopyOfFolder(folder, tempName)
Copy the given folder to tempName, returning an nsIFile for the new location.
_getSelectedPageStyle s i
function importFilesToLibrary(files, library)