test_metadata_artwork.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 gFileLocation = "testharness/metadatamanager/files/";
32 var gTestLibrary = createNewLibrary( "test_metadatajob" );
33 var gTestMediaItems = Cc["@songbirdnest.com/moz/xpcom/threadsafe-array;1"]
34  .createInstance(Ci.nsIMutableArray);
35 
36 // Make a copy of everything in the test file folder
37 // so that our changes don't interfere with other tests
39 
40 var gFileMetadataService = Cc["@songbirdnest.com/Songbird/MetadataManager;1"]
41  .getService(Ci.sbIMetadataManager);
42 
43 
44 function compareArray(aFirstArray, aSecondArray) {
45  if (aFirstArray.length != aSecondArray.length) {
46  return false;
47  }
48 
49  for (var aIndex = 0; aIndex < aFirstArray.length; aIndex++) {
50  if (aFirstArray[aIndex] != aSecondArray[aIndex]) {
51  return false;
52  }
53  }
54 
55  return true;
56 }
57 
58 function getImageData(imageFileName) {
59  var imageFile = testFolder.clone();
60  imageFile.append(imageFileName);
61 
62  var newMimeType = Cc["@mozilla.org/mime;1"]
63  .getService(Ci.nsIMIMEService)
64  .getTypeFromFile(imageFile);
65  var inputStream = Cc["@mozilla.org/network/file-input-stream;1"]
66  .createInstance(Ci.nsIFileInputStream);
67  gFilesToClose.push(inputStream);
68  inputStream.init(imageFile, 0x01, 0600, 0);
69  var stream = Cc["@mozilla.org/binaryinputstream;1"]
70  .createInstance(Ci.nsIBinaryInputStream);
71  stream.setInputStream(inputStream);
72  var size = inputStream.available();
73  // use a binary input stream to grab the bytes.
74  var bis = Cc["@mozilla.org/binaryinputstream;1"].
75  createInstance(Ci.nsIBinaryInputStream);
76  bis.setInputStream(inputStream);
77  var newImageData = bis.readByteArray(size);
78 
79  return [newMimeType, newImageData];
80 }
81 
82 function testWrite(testFileName, shouldPass, artType) {
83  if (typeof(artType) == "undefined") {
84  artType = Ci.sbIMetadataHandler.METADATA_IMAGE_TYPE_OTHER;
85  }
86 
87  // Grab the test file
88  var writeFile = testFolder.clone();
89  writeFile.append(testFileName);
90 
91  // Load the image data to save to the test file
92  var imageFileName = "test.png"
93  var imageMimeType;
94  var imageData;
95  [imageMimeType, imageData] = getImageData(imageFileName);
96  assertNotEqual(imageData.length, 0);
97 
98  var imageFile = testFolder.clone();
99  imageFile.append(imageFileName);
100 
101  // Save the image data to the test file
102  var localPathURI = newFileURI( writeFile );
103  var handler = gFileMetadataService.getHandlerForMediaURL(localPathURI.spec);
104 
105  try {
106  var newImageFile = newFileURI( imageFile );
107  handler.setImageData(artType, newImageFile.spec);
108  } catch (err) {
109  if (shouldPass) {
110  doThrow("Test write:" + err);
111  }
112  }
113 
114  try {
115  // now grab it from the same file and compare the results
116  var testMimeType = {};
117  var testDataSize = {};
118  var testImageData = handler.getImageData(artType, testMimeType, testDataSize);
119 
120  if (shouldPass) {
121  assertEqual(testMimeType.value,
122  imageMimeType,
123  "Mimetype not equal: " + testFileName);
124  assertTrue(compareArray(imageData, testImageData),
125  "Image data not equal: " + testFileName);
126  } else {
127  assertNotEqual(testMimeType.value,
128  imageMimeType,
129  "Mimetype is equal when it should not be: " + testFileName);
130  log("reference image length: " + imageData.length + " embedded image length: " + testImageData.length);
131  assertFalse(compareArray(imageData, testImageData),
132  "Image data is equal and should not be: " + testFileName);
133  }
134  } catch(err) {
135  if (shouldPass) {
136  doThrow("Test read: " + err);
137  }
138  }
139 }
140 
141 function testRead() {
142  // Read some artwork
143  var artFiles = [ "MP3_ID3v1v22.mp3",
144  "MP3_ID3v1v24.mp3",
145  "MP3_ID3v22.mp3",
146  "MP3_ID3v23.mp3",
147  "MP3_ID3v24.mp3",
148  "MPEG4_Audio_Apple_Lossless.m4a"];
149  var noArtFiles = [ "MP3_NoTags.mp3",
150  "MPEG4_Audio_Apple_Lossless_NoArt.m4a",
151  // "Ogg_Vorbis.ogg",
152  "TrueAudio.tta",
153  "FLAC.flac"];
154 
155  // Read files that should have album art
156  for (var index in artFiles) {
157  var readFile = testFolder.clone();
158  readFile.append(artFiles[index]);
159  var localPathURI = newFileURI( readFile );
160  var handler = gFileMetadataService.getHandlerForMediaURL(localPathURI.spec);
161  // The art for these are stored in OTHER, normally they should be stored in
162  // FRONTCOVER
163  try {
164  var mimeTypeOutparam = {};
165  var outSize = {};
166  var imageData = handler.getImageData(Ci.sbIMetadataHandler
167  .METADATA_IMAGE_TYPE_OTHER,
168  mimeTypeOutparam,
169  outSize);
170  assertTrue(outSize.value > 0);
171  assertNotEqual(mimeTypeOutparam.value, null);
172  assertNotEqual(imageData, "");
173  } catch (err) {
174  doThrow("Caught exception: " + err);
175  }
176 
177  // Try grabbing some other artwork that is not there
178  // but don't do this for .m4a files since there isn't the concept of
179  // album art "type"
180  if (artFiles[index].match("\.m4a$"))
181  continue;
182  try {
183  var mimeTypeOutparam = {};
184  var outSize = {};
185  var imageData = handler.getImageData(Ci.sbIMetadataHandler
186  .METADATA_IMAGE_TYPE_CONDUCTOR,
187  mimeTypeOutparam,
188  outSize);
189  assertEqual(outSize.value, 0);
190  assertEqual(mimeTypeOutparam.value, "");
191  assertEqual(imageData, "");
192  } catch (err) { }
193 
194  }
195  // Read files that should NOT have album art
196  for (var index in noArtFiles) {
197  var readFile = testFolder.clone();
198  readFile.append(noArtFiles[index]);
199  var localPathURI = newFileURI( readFile );
200  var handler = gFileMetadataService.getHandlerForMediaURL(localPathURI.spec);
201 
202  try {
203  var mimeTypeOutparam = {};
204  var outSize = {};
205  var imageData = handler.getImageData(Ci.sbIMetadataHandler
206  .METADATA_IMAGE_TYPE_OTHER,
207  mimeTypeOutparam,
208  outSize);
209  assertEqual(outSize.value, 0);
210  assertEqual(mimeTypeOutparam.value, "");
211  assertEqual(imageData, "");
212  } catch (err) { }
213  }
214 }
215 
216 function runTest () {
217  // First try to read some metadata
218  testRead();
219 
220  // Now for writing an image to metadata
221  testWrite("MP3_ID3v24.mp3", true);
222 // testWrite("Ogg_Vorbis.ogg", true);
223  testWrite("MPEG4_Audio_Apple_Lossless_NoArt.m4a", true,
224  Ci.sbIMetadataHandler.METADATA_IMAGE_TYPE_FRONTCOVER);
225  testWrite("TrueAudio.tta", false);
226 }
const Cc
var gFileMetadataService
function newAppRelativeFile(path)
function testRead()
var testFolder
var gTestLibrary
function assertNotEqual(aExpected, aActual, aMessage)
function log(s)
function writeFile(file, data)
function createNewLibrary(databaseGuid, databaseLocation)
sidebarFactory createInstance
Definition: nsSidebar.js:351
function assertTrue(aTest, aMessage)
function runTest()
Advanced DataRemote unit tests.
function assertEqual(aExpected, aActual, aMessage)
var gFileLocation
Test reading and writing artwork from various media formats.
function doThrow(text)
function newFileURI(file)
ExtensionSchemeMatcher prototype match
return null
Definition: FeedWriter.js:1143
function assertFalse(aTest, aMessage)
function testWrite(testFileName, shouldPass, artType)
function compareArray(aFirstArray, aSecondArray)
function getImageData(imageFileName)
const Ci
function readFile(fileName)
var gFilesToClose
function getCopyOfFolder(folder, tempName)
Copy the given folder to tempName, returning an nsIFile for the new location.
var gTestMediaItems
GstMessage gpointer data sbGStreamerMessageHandler * handler