gecko/services/sync/tests/unit/test_xmpp.js

107 lines
3.1 KiB
JavaScript
Raw Normal View History

var Cu = Components.utils;
Cu.import( "resource://weave/xmpp/xmppClient.js" );
2008-06-04 12:14:28 -07:00
function LOG(aMsg) {
2008-06-04 14:02:47 -07:00
dump("TEST_XMPP: " + aMsg + "\n");
2008-06-04 12:14:28 -07:00
}
2008-06-04 17:00:02 -07:00
var serverUrl = "http://localhost:5280/http-poll";
var jabberDomain = "localhost";
var timer = Cc["@mozilla.org/timer;1"].createInstance( Ci.nsITimer );
var threadManager = Cc["@mozilla.org/thread-manager;1"].getService();
2008-06-04 12:14:28 -07:00
function run_test() {
2008-05-12 10:18:20 -07:00
// FIXME: this test hangs when you don't have a server, disabling for now
return;
/* First, just see if we can connect: */
2008-06-04 17:00:02 -07:00
var transport = new HTTPPollingTransport(serverUrl, false, 4000);
var auth = new PlainAuthenticator();
2008-06-04 14:02:47 -07:00
var alice = new XmppClient("alice", jabberDomain, "iamalice",
2008-06-04 17:00:02 -07:00
transport, auth);
2008-06-04 12:14:28 -07:00
// test connection
2008-06-04 14:02:47 -07:00
LOG("connecting");
alice.connect( jabberDomain );
alice.waitForConnection();
2008-06-04 12:14:28 -07:00
do_check_eq( alice._connectionStatus, alice.CONNECTED);
2008-06-04 14:02:47 -07:00
LOG("connected");
2008-06-04 12:14:28 -07:00
2008-06-04 14:02:47 -07:00
// test disconnection
LOG("disconnecting");
2008-06-04 12:14:28 -07:00
alice.disconnect();
2008-06-04 14:02:47 -07:00
do_check_eq( alice._connectionStatus, alice.NOT_CONNECTED);
2008-06-04 12:14:28 -07:00
LOG("disconnected");
2008-06-04 14:02:47 -07:00
// test re-connection
LOG("reconnecting");
2008-06-04 12:14:28 -07:00
alice.connect( jabberDomain );
alice.waitForConnection();
2008-06-04 14:02:47 -07:00
LOG("reconnected");
2008-06-04 12:14:28 -07:00
do_check_eq( alice._connectionStatus, alice.CONNECTED);
2008-06-04 14:02:47 -07:00
alice.disconnect();
2008-06-04 17:00:02 -07:00
/*
2008-06-04 12:14:28 -07:00
// test connection failure
alice.connect( "bad domain" );
alice.waitForConnection();
do_check_eq( alice._connectionStatus, alice.FAILED );
// re-connect and move on
alice.connect( jabberDomain );
alice.waitForConnection();
do_check_eq( alice._connectionStatus, alice.CONNECTED);
2008-05-12 10:18:20 -07:00
// The talking-to-myself test:
var testIsOver = false;
var sometext = "bla bla how you doin bla";
var transport2 = new HTTPPollingTransport( serverUrl, false, 4000 );
var auth2 = new PlainAuthenticator();
var bob = new XmppClient( "bob", jabberDomain, "iambob", transport2, auth2 );
// Timer that will make the test fail if message is not received after
// a certain amount of time
var timerResponder = {
notify: function( timer ) {
testIsOver = true;
do_throw( "Timed out waiting for message." );
}
};
timer.initWithCallback( timerResponder, 20000, timer.TYPE_ONE_SHOT );
2008-05-12 10:18:20 -07:00
// Handler that listens for the incoming message:
var aliceMessageHandler = {
handle: function( msgText, from ) {
dump( "Alice got a message.\n" );
do_check_eq( msgText, sometext );
do_check_eq( from, "bob@" + jabberDomain );
timer.cancel();
testIsOver = true;
}
};
alice.registerMessageHandler( aliceMessageHandler );
// Start both clients
bob.connect( jabberDomain );
bob.waitForConnection();
do_check_neq( bob._connectionStatus, bob.FAILED );
alice.connect( jabberDomain );
alice.waitForConnection();
do_check_neq( alice._connectionStatus, alice.FAILED );
// Send the message
bob.sendMessage( "alice@" + jabberDomain, sometext );
// Wait until either the message is received, or the timeout expires.
var currentThread = threadManager.currentThread;
while( !testIsOver ) {
currentThread.processNextEvent( true );
}
alice.disconnect();
2008-06-04 14:02:47 -07:00
bob.disconnect();
*/
};