//
// 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 is intended to be run through xpcshell via command line:
//
//   xpcshell -f testWebServices.js
//

const OBS  = Components.classes["@mozilla.org/observer-service;1"]
                       .getService(Components.interfaces.nsIObserverService);
const RDFS = Components.classes['@mozilla.org/rdf/rdf-service;1']
                       .getService(Components.interfaces.nsIRDFService);

function DEBUG(x) {
  dump("[testWebServices.js]: "+x+"\n");
}

// Returns an array of all flockIWebService's listed by the category manager
function getAllWebServices() {
  DEBUG("getAllWebServices()");
  var cm = Components.classes['@mozilla.org/categorymanager;1']
                     .getService(Components.interfaces.nsICategoryManager);
  var webServices = new Array();
  var categoryEnum = cm.enumerateCategory('flockWebService');
  while (categoryEnum.hasMoreElements()) {
    try {
      var entry = categoryEnum.getNext()
                              .QueryInterface(Components.interfaces.nsISupportsCString);
      if (!entry) { continue; }
      var contractID = cm.getCategoryEntry('flockWebService', entry.data);
      DEBUG('found service: '+entry.data+' with contractID: '+contractID);
      var svc = Components.classes[contractID]
                          .getService(Components.interfaces.flockIWebService);
      webServices.push([contractID, svc]);
    } catch (ex) { 
      DEBUG(ex);
    }
  }
  return webServices;
}

function test_flockIWebService(ws) {
  DEBUG("test_flockIWebService(['"+ws[0]+"',...])");
  try {
    var acEnum = ws[1].getAccountUrns();
    while (acEnum.hasMore()) {
      var ac = acEnum.getNext();
    }
  } catch (ex) {
    DEBUG(ex);
  }
}

function initialize() {
  DEBUG("initialize()");
  RDFS.GetDataSourceBlocking('rdf:flock-favorites');
  OBS.notifyObservers(null, "profile-after-change", "");
}

function doTests() {
  DEBUG("doTests()");
  var webServices = getAllWebServices();
  for (var i = 0; i < webServices.length; i++) {
    var ws = webServices[i];
    test_flockIWebService(ws);
    try {
      ws[1].QueryInterface(Components.interfaces.flockIBlogWebService);
      test_flockIBlogWebService(ws);
    } catch (ex) { }
    try {
      ws[1].QueryInterface(Components.interfaces.flockISocialWebService);
      test_flockISocialWebService(ws);
    } catch (ex) { }
  }
}

function main() {
  DEBUG("main()");
  var observer = {
    called : false,
    QueryInterface : function(iid) {
      if (!iid.equals(Components.interfaces.nsISupports) &&
          !iid.equals(Components.interfaces.nsIObserver))
      {
        throw Components.results.NS_ERROR_NO_INTERFACE;
      }
      return this;
    },
    observe : function(aSubject, aTopic, aData) {
      this.called = true;
      doTests();
    }
  };
  DEBUG(" waiting for "user-interaction-inactive"...");
  OBS.addObserver(observer, "user-interaction-inactive", false);
  while (!observer.called) {
    // wait
  }
}

main();
