function runReadGalleriesTest(expectedGalleryCount, expectSucceed,
                              customReadDirectoryCallback,
                              customReadDirectoryErrorCallback,
                              customGotGalleriesCallback) {
  var galleries;
  var readEntriesResults = [];
  var readDirectoryCallback;
  var readDirectoryErrorCallback;
  var gotGalleriesCallback;

  if (customReadDirectoryCallback && customReadDirectoryErrorCallback &&
      customGotGalleriesCallback) {
    chrome.test.assertEq(typeof(customReadDirectoryCallback), 'function');
    chrome.test.assertEq(typeof(customReadDirectoryErrorCallback), 'function');
    chrome.test.assertEq(typeof(customGotGalleriesCallback), 'function');
    readDirectoryCallback = customReadDirectoryCallback;
    readDirectoryErrorCallback = customReadDirectoryErrorCallback;
    gotGalleriesCallback = customGotGalleriesCallback;
  } else {
    chrome.test.assertTrue(!customReadDirectoryCallback &&
                           !customReadDirectoryErrorCallback &&
                           !customGotGalleriesCallback);
    readDirectoryCallback = defaultReadDirectoryCallback;
    readDirectoryErrorCallback = defaultReadDirectoryErrorCallback;
    gotGalleriesCallback = defaultGotGalleriesCallback;
  }
  chrome.mediaGalleries.getMediaFileSystems(testGalleries);

  function testGalleries(results) {
    gotGalleriesCallback(results);
    chrome.test.assertEq(expectedGalleryCount, results.length,
                         "Gallery count mismatch");
    if (expectedGalleryCount == 0) {
      chrome.test.succeed();
      return;
    }

    for (var i = 0; i < results.length; i++) {
      var dirReader = results[i].root.createReader();
      dirReader.readEntries(readDirectoryCallback, readDirectoryErrorCallback);
    }
  }

  function defaultGotGalleriesCallback(entries) {
    galleries = entries;
  }

  function defaultReadDirectoryCallback(entries) {
    var result = "";
    if (!expectSucceed) {
      result = "Unexpected readEntries success";
    }
    readEntriesResults.push(result);
    checkReadEntriesFinished();
  }

  function defaultReadDirectoryErrorCallback(err) {
    var result = "";
    if (expectSucceed) {
      result = "Unexpected readEntries failure: " + err;
    }
    readEntriesResults.push(result);
    checkReadEntriesFinished();
  }

  function checkReadEntriesFinished() {
    if (readEntriesResults.length != galleries.length)
      return;
    var success = true;
    for (var i = 0; i < readEntriesResults.length; i++) {
      if (readEntriesResults[i]) {
        success = false;
      }
    }
    if (success) {
      chrome.test.succeed();
      return;
    }
    chrome.test.fail(readEntriesResults);
  }
}
// If passed in, all 3 function arguments need to be valid.
If passed in, all 3 function arguments need to be valid.
If passed in all 3 function arguments need to be valid .
function runReadGalleriesTest(expectedGalleryCount, expectSucceed,
                              customReadDirectoryCallback,
                              customReadDirectoryErrorCallback,
                              customGotGalleriesCallback) {
  var galleries;
  var readEntriesResults = [];
  var readDirectoryCallback;
  var readDirectoryErrorCallback;
  var gotGalleriesCallback;
  if (customReadDirectoryCallback && customReadDirectoryErrorCallback &&
      customGotGalleriesCallback) {
    chrome.test.assertEq(typeof(customReadDirectoryCallback), 'function');
    chrome.test.assertEq(typeof(customReadDirectoryErrorCallback), 'function');
    chrome.test.assertEq(typeof(customGotGalleriesCallback), 'function');
    readDirectoryCallback = customReadDirectoryCallback;
    readDirectoryErrorCallback = customReadDirectoryErrorCallback;
    gotGalleriesCallback = customGotGalleriesCallback;
  } else {
    chrome.test.assertTrue(!customReadDirectoryCallback &&
                           !customReadDirectoryErrorCallback &&
                           !customGotGalleriesCallback);
    readDirectoryCallback = defaultReadDirectoryCallback;
    readDirectoryErrorCallback = defaultReadDirectoryErrorCallback;
    gotGalleriesCallback = defaultGotGalleriesCallback;
  }
  chrome.mediaGalleries.getMediaFileSystems(testGalleries);
  function testGalleries(results) {
    gotGalleriesCallback(results);
    chrome.test.assertEq(expectedGalleryCount, results.length,
                         "Gallery count mismatch");
    if (expectedGalleryCount == 0) {
      chrome.test.succeed();
      return;
    }
    for (var i = 0; i < results.length; i++) {
      var dirReader = results[i].root.createReader();
      dirReader.readEntries(readDirectoryCallback, readDirectoryErrorCallback);
    }
  }
  function defaultGotGalleriesCallback(entries) {
    galleries = entries;
  }
  function defaultReadDirectoryCallback(entries) {
    var result = "";
    if (!expectSucceed) {
      result = "Unexpected readEntries success";
    }
    readEntriesResults.push(result);
    checkReadEntriesFinished();
  }
  function defaultReadDirectoryErrorCallback(err) {
    var result = "";
    if (expectSucceed) {
      result = "Unexpected readEntries failure: " + err;
    }
    readEntriesResults.push(result);
    checkReadEntriesFinished();
  }
  function checkReadEntriesFinished() {
    if (readEntriesResults.length != galleries.length)
      return;
    var success = true;
    for (var i = 0; i < readEntriesResults.length; i++) {
      if (readEntriesResults[i]) {
        success = false;
      }
    }
    if (success) {
      chrome.test.succeed();
      return;
    }
    chrome.test.fail(readEntriesResults);
  }
}
function runReadGalleriesTest ( expectedGalleryCount , expectSucceed , customReadDirectoryCallback , customReadDirectoryErrorCallback , customGotGalleriesCallback ) { var galleries ; var readEntriesResults = [ ] ; var readDirectoryCallback ; var readDirectoryErrorCallback ; var gotGalleriesCallback ; if ( customReadDirectoryCallback && customReadDirectoryErrorCallback && customGotGalleriesCallback ) { chrome . test . assertEq ( typeof ( customReadDirectoryCallback ) , 'function' ) ; chrome . test . assertEq ( typeof ( customReadDirectoryErrorCallback ) , 'function' ) ; chrome . test . assertEq ( typeof ( customGotGalleriesCallback ) , 'function' ) ; readDirectoryCallback = customReadDirectoryCallback ; readDirectoryErrorCallback = customReadDirectoryErrorCallback ; gotGalleriesCallback = customGotGalleriesCallback ; } else { chrome . test . assertTrue ( ! customReadDirectoryCallback && ! customReadDirectoryErrorCallback && ! customGotGalleriesCallback ) ; readDirectoryCallback = defaultReadDirectoryCallback ; readDirectoryErrorCallback = defaultReadDirectoryErrorCallback ; gotGalleriesCallback = defaultGotGalleriesCallback ; } chrome . mediaGalleries . getMediaFileSystems ( testGalleries ) ; function testGalleries ( results ) { gotGalleriesCallback ( results ) ; chrome . test . assertEq ( expectedGalleryCount , results . length , "Gallery count mismatch" ) ; if ( expectedGalleryCount == 0 ) { chrome . test . succeed ( ) ; return ; } for ( var i = 0 ; i < results . length ; i ++ ) { var dirReader = results [ i ] . root . createReader ( ) ; dirReader . readEntries ( readDirectoryCallback , readDirectoryErrorCallback ) ; } } function defaultGotGalleriesCallback ( entries ) { galleries = entries ; } function defaultReadDirectoryCallback ( entries ) { var result = "" ; if ( ! expectSucceed ) { result = "Unexpected readEntries success" ; } readEntriesResults . push ( result ) ; checkReadEntriesFinished ( ) ; } function defaultReadDirectoryErrorCallback ( err ) { var result = "" ; if ( expectSucceed ) { result = "Unexpected readEntries failure: " + err ; } readEntriesResults . push ( result ) ; checkReadEntriesFinished ( ) ; } function checkReadEntriesFinished ( ) { if ( readEntriesResults . length != galleries . length ) return ; var success = true ; for ( var i = 0 ; i < readEntriesResults . length ; i ++ ) { if ( readEntriesResults [ i ] ) { success = false ; } } if ( success ) { chrome . test . succeed ( ) ; return ; } chrome . test . fail ( readEntriesResults ) ; } }
If passed in, all 3 function arguments need to be valid.
If passed in all 3 function arguments need to be valid .