test_mediaitem.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 TEST_STRING = "The quick brown fox jumps over the lazy dog. Bitches."
32 
34 var gServer;
35 
36 function read_stream(stream, count) {
37  /* assume stream has non-ASCII data */
38  var wrapper =
39  Cc["@mozilla.org/binaryinputstream;1"].
40  createInstance(Ci.nsIBinaryInputStream);
41  wrapper.setInputStream(stream);
42  /* JS methods can be called with a maximum of 65535 arguments, and input
43  streams don't have to return all the data they make .available() when
44  asked to .read() that number of bytes. */
45  var data = [];
46  while (count > 0) {
47  var bytes = wrapper.readByteArray(Math.min(65535, count));
48  data.push(String.fromCharCode.apply(null, bytes));
49  count -= bytes.length;
50  if (bytes.length == 0)
51  do_throw("Nothing read from input stream!");
52  }
53  return data.join('');
54 }
55 
56 function newTempURI( name ) {
57  var file = Components.classes["@mozilla.org/file/directory_service;1"]
58  .getService(Components.interfaces.nsIProperties)
59  .get("TmpD", Components.interfaces.nsIFile);
60  file.append(name);
61  file.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 0664);
62  var registerFileForDelete = Cc["@mozilla.org/uriloader/external-helper-app-service;1"]
63  .getService(Ci.nsPIExternalAppLauncher);
64  registerFileForDelete.deleteTemporaryFileOnExit(file);
65  var retval = newFileURI(file);
66  log( "newTempURI = " + retval.spec );
67  return retval;
68 }
69 
70 function createItem( library, url ) {
71  return library.createMediaItem(newURI(url));
72 }
73 
74 function testGet( item, attrib, value ) {
75  var test_value = item[ attrib ];
76  // for nsURI return values, you must compare spec?
77  if ( test_value.spec )
78  test_value = test_value.spec;
79  if ( value.spec )
80  value = value.spec;
81  assertEqual( test_value, value );
82 }
83 
84 function testSet( item, attrib, value ) {
85  log("testSet: " + attrib + " -> " + value);
86  item[ attrib ] = value;
87  testGet( item, attrib, value );
88 }
89 
90 function testAvailable( library, url, available, completion ) {
91  var item = createItem( library, url );
92 
93  // Make an observer to receive results of the availability test.
94  // This one is complicated because I want to chain tests.
95  var is_available_observer = {
96  _item: item,
97  _available: available,
98  _completion: completion,
99  observe: function( aSubject, aTopic, aData ) {
100  if ( aTopic == "available" ) {
101  log( "(sbIMediaItem::TestIsURIAvailable) COMPLETE " + this._item.contentSrc.spec + ": " + aTopic + " == " + aData );
102  assertEqual( aData, this._available );
103  if ( this._completion != null )
104  try {
105  this._completion();
106  } catch ( e ) {
107  assertEqual( e, null ); // Fail the tests on a throw.
108  }
109  testFinished();
110  }
111  },
112  QueryInterface: function(iid) {
113  if (!iid.equals(Components.interfaces.nsIObserver) &&
114  !iid.equals(Components.interfaces.nsISupports))
115  throw Components.results.NS_ERROR_NO_INTERFACE;
116  return this;
117  }
118  }
119 
120  // Start the test, tell the testharness to wait for us.
121  item.testIsURIAvailable( is_available_observer );
122  testPending();
123  log( "(sbIMediaItem::TestIsURIAvailable) START " + url );
124 }
125 
126 function testIsURIAvailable( ioItem ) {
127  var databaseGUID = "test_mediaitem_isuriavailable";
128  var testlib = createLibrary(databaseGUID);
129  // Async tests of availability for a (supposedly!) known url.
130  testAvailable( testlib, ioItem.contentSrc.spec, "true",
131  function() {
132  testAvailable( testlib, "http://localhost:" + gServerPort + "/test_not_available.mp3", "false",
133  function() {
134  testAvailable( testlib, "http://localhost:" + gServerPort + "/test1.mp3", "true", null);
135  }
136  );
137  }
138  );
139 }
140 
141 function testAsyncRead(ioItem) {
142  var listener = {
143  _item: ioItem,
144 
148  onDataAvailable: function LLL_onDataAvailable(request, context, inputStream,
149  sourceOffset, count) {
150  // Once we have enough bytes, read it.
151  if ((count + sourceOffset) >= TEST_STRING.length) {
152  this._value = read_stream(inputStream, (count + sourceOffset));
153  }
154  },
155 
159  onStartRequest: function LLL_onStartRequest(request, context) {
160  },
161 
165  onStopRequest: function LLL_onStopReqeust(request, context, status) {
166  // Test this and go on to the next tests.
167  assertEqual( this._value, TEST_STRING );
168  testIsURIAvailable( this._item );
169  testFinished();
170  },
174  QueryInterface: function LLL_QueryInterface(iid) {
175  if (iid.equals(Ci.nsIStreamListener) ||
176  iid.equals(Ci.nsIRequestObserver)||
177  iid.equals(Ci.nsISupports))
178  return this;
179  throw Cr.NS_ERROR_NO_INTERFACE;
180  }
181  }
182  var inputChannel = ioItem.openInputStreamAsync(listener, null);
183  assertNotEqual(inputChannel, null);
184  listener._inputChannel = inputChannel;
185  testPending();
186 }
187 
188 function runTest () {
189 
190  gServer = Cc["@mozilla.org/server/jshttp;1"]
191  .createInstance(Ci.nsIHttpServer);
192 
193  var databaseGUID = "test_mediaitem";
194  var testlib = createLibrary(databaseGUID);
195 
196  // Get an item
197  var item = testlib.getMediaItem("3E586C1A-AD99-11DB-9321-C22AB7121F49");
198 
199  // Basic tests on retrieving its info...
200  testGet( item, "library", testlib );
201  testGet( item, "isMutable", true );
202  testGet( item, "mediaCreated", 1169855962000 );
203  testGet( item, "mediaUpdated", 1169855962000 );
204  testGet( item, "contentSrc", "file:///home/steve/Hells%20Bells.mp3" );
205  testGet( item, "contentLength", 0x21C );
206  testGet( item, "contentType", "audio/mpeg" );
207 
208  // Slightly more complicated tests for setting its info
209  testSet( item, "mediaCreated", 0x1337baadf00d );
210  testSet( item, "mediaUpdated", 0x1337baadf00d );
211  var newSrc = newURI("file:///poo/MyTrack.mp3");
212  testSet( item, "contentSrc", newSrc);
213  testSet( item, "contentLength", 0xbaadf00d );
214  testSet( item, "contentType", "x-media/x-poo" );
215 
216  item.setProperty("newpropertyneverseenever", "valuevalue");
217 
218  // Open up a temp file through this interface
219  var ioItem = testlib.createMediaItem( newTempURI("test_mediaitem") );
220  assertNotEqual(ioItem, null);
221  var outputStream = ioItem.openOutputStream();
222  assertNotEqual(outputStream, null);
223 
224  // Write some junk to it and close.
225  var count = outputStream.write( TEST_STRING, TEST_STRING.length );
226  assertEqual(count, TEST_STRING.length);
227  outputStream.flush();
228  outputStream.close();
229 
230  // Reopen it for input, and see if we read the same junk back.
231  var inputStream = ioItem.openInputStream().QueryInterface(Components.interfaces.nsILineInputStream);
232  assertNotEqual(inputStream, null);
233  var out = {};
234  inputStream.readLine( out );
235  assertEqual(out.value, TEST_STRING);
236  inputStream.close();
237 
238  try {
239  gServer.start(gServerPort);
240  gServer.registerDirectory("/", getFile("."));
241 
242  // Async test, pauses the test system.
243  // This passes control to testIsURIAvailable() when it completes.
244  testAsyncRead(ioItem);
245 
246  // Shouldn't take more than 5 seconds to test all files.
247  sleep(5000);
248  }
249  finally {
250  gServer.stop(function() {});
251  }
252 }
var gServer
function sleep(ms, suppressOutput)
const Cc
function createItem(library, url)
function testGet(item, attrib, value)
function assertNotEqual(aExpected, aActual, aMessage)
function runTest()
Advanced DataRemote unit tests.
function log(s)
function testFinished()
sidebarFactory createInstance
Definition: nsSidebar.js:351
sbOSDControlService prototype QueryInterface
function testSet(item, attrib, value)
function read_stream(stream, count)
function assertEqual(aExpected, aActual, aMessage)
function getFile(fileName)
var count
Definition: test_bug7406.js:32
function testIsURIAvailable(ioItem)
function testAsyncRead(ioItem)
function newFileURI(file)
var TEST_STRING
Test file.
return null
Definition: FeedWriter.js:1143
function createLibrary(databaseGuid, databaseLocation)
Definition: test_load.js:151
function newURI(aURLString)
function url(spec)
countRef value
Definition: FeedWriter.js:1423
const Cr
const Ci
function getTestServerPortNumber()
observe data
Definition: FeedWriter.js:1329
function newTempURI(name)
function testAvailable(library, url, available, completion)
_updateTextAndScrollDataForFrame aData
var file
sbDeviceFirmwareAutoCheckForUpdate prototype observe
function testPending()