// BEGIN FLOCK GPL
//
// Copyright Flock Inc. 2005-2009
// http://flock.com
//
// This file may be used under the terms of the
// GNU General Public License Version 2 or later (the "GPL"),
// http://www.gnu.org/licenses/gpl.html
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// END FLOCK GPL

// This file tests a few hardcoded Media RSS feeds

do_import_script("netwerk/test/httpserver/httpd.js");

var httpserv;
var responses = 0;

// :::::::::::::::::: All Tests ::::::::::::::::::::

const tests = [
  {
    file: "enclosure.xml",
    verify: verify_enclosure
  },
/*  {
    file: "mediarss.xml",
    verify: verify_enclosure
  }*/
];

function verify_generic(aMediaItems) {
  // Make sure that for each items, important fields are not null
  for each (mediaItem in aMediaItems) {
    do_check_true(mediaItem.title && mediaItem.title.length > 0);
    do_check_true(mediaItem.thumbnail && mediaItem.thumbnail.length > 0);
    do_check_true(mediaItem.midSizePhoto && mediaItem.midSizePhoto.length > 0);
  }
}

function verify_enclosure(aResult) {
  do_check_eq(aResult.length, 4);
}

// :::::::::::::::::: Some Helpers ::::::::::::::::::::

function readFile(aFilename) {
  var file = do_get_file("flock/base/photo/tests/unit/" + aFilename);

  var data = "";
  var fstream = Components.classes["@mozilla.org/network/file-input-stream;1"]
                          .createInstance(Components.interfaces.nsIFileInputStream);
  var sstream = Components.classes["@mozilla.org/scriptableinputstream;1"]
                          .createInstance(Components.interfaces.nsIScriptableInputStream);
  fstream.init(file, -1, 0, 0);
  sstream.init(fstream);

  var str = sstream.read(4096);
  while (str.length > 0) {
    data += str;
    str = sstream.read(4096);
  }

  sstream.close();
  fstream.close();

  return data;
}

function metaHandler(aFileName) {
  return function feedHandler(metadata, response) {
    var data = readFile(aFileName);
    response.bodyOutputStream.write(data, data.length);
  }
}

function doMediaRSSSearch(aURL, aVerify) {
  var listener = {
    onSuccess: function(aSubject, aTopic) {
      var result = [];
      aSubject.QueryInterface(Components.interfaces.nsISimpleEnumerator);
      while (aSubject.hasMoreElements()) {
        result.push(aSubject.getNext());
      }
      verify_generic(result);
      if (aVerify) {
        aVerify(result);
      }
      do_test_finished();
      responses++;
      if (responses == tests.length) {
        httpserv.stop();
      }
    },
    onError: function(aFlockError, aTopic) {
      httpserv.stop();
      do_test_finished();
      do_throw("flockRSSMediaService reported an error");
    }
  }
  var query = "special:" + encodeURIComponent(aURL);
  dump(query + "\n");
  var service = Components.classes["@flock.com/?photo-rss-media;1"]
                .getService(Components.interfaces.flockIMediaWebService);
  service.search(listener, query, 100, 1, 1);
}

// :::::::::::::::::: Start the tests ::::::::::::::::::::

function run_test() {
  dump("Starting...\n");
  httpserv = new nsHttpServer();
  for each (test in tests) {
    var fileName = test.file;
    httpserv.registerPathHandler("/" + fileName, metaHandler(fileName));
  }

  httpserv.start(4444);

  for each (test in tests) {
    do_test_pending();
    doMediaRSSSearch("http://localhost:4444/" + test.file, test.verify);
  }
}
