test_metadatajob_mpeg4_writing.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 var gTestFileLocation = "testharness/metadatamanager/files/";
32 var gTestLibrary = createNewLibrary( "test_metadata_mpeg4" );
33 var gTestMediaItems = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
34  .createInstance(Ci.nsIMutableArray);
35 
37 
38 var gFileMetadataService = Cc["@songbirdnest.com/Songbird/MetadataManager;1"]
39  .getService(Ci.sbIMetadataManager);
40 
41 
42 Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
43 
44 /************************************************************************/
45 /* \brief read the given file and return its contents as an array */
46 /************************************************************************/
47 function getFileData(aFileName) {
48  var file = gTestFolder.clone();
49  file.append(aFileName);
50 
51  var inputStream = Cc["@mozilla.org/network/file-input-stream;1"]
52  .createInstance(Ci.nsIFileInputStream);
53  gFilesToClose.push(inputStream);
54  inputStream.init(file, 0x01, 0600, 0);
55  var stream = Cc["@mozilla.org/binaryinputstream;1"]
56  .createInstance(Ci.nsIBinaryInputStream);
57  stream.setInputStream(inputStream);
58  var size = inputStream.available();
59  // use a binary input stream to grab the bytes.
60  return stream.readByteArray(size);
61 }
62 
63 /************************************************************************/
64 /* The list of file transformations we'll take to make the files */
65 /************************************************************************/
66 const gTestCases = [
67  {
68  source: "MPEG4_Empty.m4a",
69  ref: "MPEG4_Empty_Ref.m4a",
70  properties: {
71  artistName: "Songbird",
72  trackName: "Start From Empty"
73 
74  }
75  },
76  {
77  source: "MPEG4_Empty_Shift.m4a",
78  ref: "MPEG4_Empty_Shift_Ref.m4a",
79  properties: {
80  artistName: "Songbird",
81  trackName: "memmov"
82 
83  }
84  },
85  {
86  source: "MPEG4_Useless.m4a",
87  ref: "MPEG4_Useless_Ref.m4a",
88  properties: {
89  artistName: "Songbird",
90  trackName: "Simple Test"
91  }
92  },
93  {
94  source: "MPEG4_Expand.m4a",
95  ref: "MPEG4_Expand_Ref.m4a",
96  properties: {
97  artistName: "Songbird",
98  albumName: "MPEG4 writing unit test",
99  trackName: "expanding and shifting chunk offsets!",
100  genre: "Soundtrack",
101  year: 2009,
102  trackNumber: 42,
103  totalTracks: 54,
104  comment: "Hello, world!",
105  composerName: "Balut",
106  discNumber: 13,
107  totalDiscs: 27,
108  sampleRate: 32768, /* this doesn't actually get set in the file */
109  bitRate: 65536, /* this doesn't actually get set in the file */
110  channels: 1024 /* this doesn't actually get set in the file */
111  }
112  },
113 ];
114 
119 function runTest() {
120  /* SETUP */
121 
122  var prefSvc = Cc["@mozilla.org/preferences-service;1"]
123  .getService(Ci.nsIPrefBranch);
124  var oldWritingEnabledPref = prefSvc.getBoolPref("songbird.metadata.enableWriting");
125  gTailCallback.push(function() {
126  prefSvc.setBoolPref("songbird.metadata.enableWriting", oldWritingEnabledPref);
127  });
128  prefSvc.setBoolPref("songbird.metadata.enableWriting", true);
129 
130  // Make a copy of everything in the test file folder
131  // so that our changes don't interfere with other tests
133  "_temp_metadata_mpeg4_files");
134 
136  // Import the test items //
138  var library = createNewLibrary( "test_metadatajob_writing_library" );
139  assertTrue(library instanceof Ci.sbILibrary, "creating an invalid library");
140  var sourceURLs = [];
141  for each (let testData in gTestCases) {
142  let itemPath = gTestFolder.clone();
143  itemPath.append(testData.source);
144  sourceURLs.push(newFileURI(itemPath));
145  }
146  var items = importFilesToLibrary(sourceURLs, library);
147  assertEqual(items.length, sourceURLs.length);
148  for (let i = 0; i < items.length; ++ i) {
149  assertEqual(gTestCases[i].source,
150  items[i].contentSrc.QueryInterface(Ci.nsIURL).fileName);
151  gTestCases[i].item = items[i];
152  }
153 
154  var job = startMetadataJob(items, "read");
155  // Wait for reading to complete before continuing
156  job.addJobProgressListener(onReadComplete);
157  testPending();
158 }
159 
160 function onReadComplete(job) {
161  reportJobProgress(job, "metadatajob - mpeg4 writing - onReadComplete");
162 
163  if (job.status == Components.interfaces.sbIJobProgress.STATUS_RUNNING) {
164  // the job isn't finished yet!
165  return;
166  }
167  job.removeJobProgressListener(onReadComplete);
168 
169  var propertiesToWrite = [];
170  var items = [];
171  for each (let testData in gTestCases) {
172  items.push(testData.item);
173  for (let prop in testData.properties) {
174  log(prop + ": " + SBProperties[prop] + " -> " + testData.properties[prop]);
175  testData.item.setProperty(SBProperties[prop], testData.properties[prop]);
176  // Add the property name to the write list if we have not already done so
177  if (propertiesToWrite.indexOf(SBProperties[prop]) < 0) {
178  propertiesToWrite.push(SBProperties[prop]);
179  }
180  }
181  }
182  job = startMetadataJob(items, "write", propertiesToWrite);
183  // Wait for writing to complete before continuing
184  job.addJobProgressListener(onWriteComplete);
185 }
186 
187 function onWriteComplete(job) {
188  reportJobProgress(job, "metadatajob - mpeg4 writing - onWriteComplete");
189 
190  if (job.status == Components.interfaces.sbIJobProgress.STATUS_RUNNING) {
191  return;
192  }
193  job.removeJobProgressListener(onWriteComplete);
194 
195  for each (let testData in gTestCases) {
196  log("Comparing " + testData.source + " against " + testData.ref);
197  let changedData = getFileData(testData.source);
198  let referenceData = getFileData(testData.ref);
199  assertArraysEqual(changedData, referenceData);
200  }
201 
202  // Verify job progress reporting.
203  // NOTE: Comment these out to receive more useful debug information
204  assertEqual(gTestCases.length, job.total);
205  assertEqual(gTestCases.length, job.progress);
206  assertEqual(0, job.errorCount, "found errors in job");
207  assertEqual(job.status, Components.interfaces.sbIJobProgress.STATUS_SUCCEEDED);
208  testFinished();
209 }
210 
214 function importFilesToLibrary(files, library) {
215  if (!(files instanceof Ci.nsIArray)) {
216  files = ArrayConverter.nsIArray(files);
217  }
218  var items = library.batchCreateMediaItems(files, null, true);
219  assertEqual(items.length, files.length);
220  var jsItems = [];
221  for (var i = 0; i < items.length; i++) {
222  jsItems.push(items.queryElementAt(i, Ci.sbIMediaItem));
223  }
224  return jsItems;
225 }
226 
227 
231 function startMetadataJob(items, type, writeProperties) {
232  if (!(items instanceof Ci.nsIArray)) {
233  items = ArrayConverter.nsIArray(items);
234  }
235  manager = Components.classes["@songbirdnest.com/Songbird/FileMetadataService;1"]
236  .getService(Components.interfaces.sbIFileMetadataService);
237 
238  var job;
239  if (type == "write") {
240  job = manager.write(items, ArrayConverter.stringEnumerator(writeProperties));
241  } else {
242  job = manager.read(items);
243  }
244  return job;
245 }
246 
var gTestFileLocation
Test creation and execution of metadata writing jobs.
let prefSvc
const Cc
function newAppRelativeFile(path)
function getFileData(aFileName)
function onReadComplete(job)
function log(s)
function testFinished()
function createNewLibrary(databaseGuid, databaseLocation)
function importFilesToLibrary(files, library)
function assertTrue(aTest, aMessage)
function runTest()
Advanced DataRemote unit tests.
function assertEqual(aExpected, aActual, aMessage)
function assertArraysEqual(a1, a2)
function reportJobProgress(job, jobName)
var gTailCallback
function newFileURI(file)
return null
Definition: FeedWriter.js:1143
const Ci
var gFilesToClose
function getCopyOfFolder(folder, tempName)
Copy the given folder to tempName, returning an nsIFile for the new location.
function startMetadataJob(items, type, writeProperties)
function onWriteComplete(job)
_getSelectedPageStyle s i
var file
function testPending()