B2G/gecko/netwerk/test/unit/test_NetUtil.js
/**
 * Tests asynchronously writing a file using NetUtil.asyncCopy.
 *
 * @param aContractId
 *        The contract ID to use for the output stream
 * @param aDeferOpen
 *        Whether to use DEFER_OPEN in the output stream.
 */
Tests asynchronously writing a file using NetUtil.asyncCopy.
@param aContractId
The contract ID to use for the output stream
@param aDeferOpen
Whether to use DEFER_OPEN in the output stream.
Tests asynchronously writing a file using NetUtil . asyncCopy . @param aContractId The contract ID to use for the output stream @param aDeferOpen Whether to use DEFER_OPEN in the output stream .
function async_write_file(aContractId, aDeferOpen)
{
  do_test_pending();
  let file = Cc["@mozilla.org/file/directory_service;1"].
             getService(Ci.nsIProperties).
             get("ProfD", Ci.nsIFile);
  file.append("NetUtil-async-test-file.tmp");
  file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
  let ostream = Cc[aContractId].createInstance(Ci.nsIFileOutputStream);
  ostream.init(file, -1, -1, aDeferOpen ? Ci.nsIFileOutputStream.DEFER_OPEN : 0);
  const TEST_DATA = "this is a test string";
  let istream = Cc["@mozilla.org/io/string-input-stream;1"].
                createInstance(Ci.nsIStringInputStream);
  istream.setData(TEST_DATA, TEST_DATA.length);
  NetUtil.asyncCopy(istream, ostream, function(aResult) {
    do_check_true(Components.isSuccessCode(aResult));
    do_check_eq(TEST_DATA, getFileContents(file));
    do_test_finished();
    run_next_test();
  });
}
function async_write_file ( aContractId , aDeferOpen ) { do_test_pending ( ) ; let file = Cc [ "@mozilla.org/file/directory_service;1" ] . getService ( Ci . nsIProperties ) . get ( "ProfD" , Ci . nsIFile ) ; file . append ( "NetUtil-async-test-file.tmp" ) ; file . createUnique ( Ci . nsIFile . NORMAL_FILE_TYPE , 0666 ) ; let ostream = Cc [ aContractId ] . createInstance ( Ci . nsIFileOutputStream ) ; ostream . init ( file , - 1 , - 1 , aDeferOpen ? Ci . nsIFileOutputStream . DEFER_OPEN : 0 ) ; const TEST_DATA = "this is a test string" ; let istream = Cc [ "@mozilla.org/io/string-input-stream;1" ] . createInstance ( Ci . nsIStringInputStream ) ; istream . setData ( TEST_DATA , TEST_DATA . length ) ; NetUtil . asyncCopy ( istream , ostream , function ( aResult ) { do_check_true ( Components . isSuccessCode ( aResult ) ) ; do_check_eq ( TEST_DATA , getFileContents ( file ) ) ; do_test_finished ( ) ; run_next_test ( ) ; } ) ; }
Tests asynchronously writing a file using NetUtil.asyncCopy.
Tests asynchronously writing a file using NetUtil . asyncCopy .
// First, we need an output file to write to. // Then, we need an output stream to our output file. // Finally, we need an input stream to take data from. // Make sure the copy was successful! // Check the file contents. // Finish the test.