use of org.jivesoftware.smack.Chat in project Smack by igniterealtime.
the class MultiUserChatTest method testPrivateChat.
public void testPrivateChat() {
try {
// User2 joins the new room
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2");
getConnection(0).getChatManager().addChatListener(new ChatManagerListener() {
public void chatCreated(Chat chat2, boolean createdLocally) {
assertEquals("Sender of chat is incorrect", room + "/testbot2", chat2.getParticipant());
try {
chat2.sendMessage("ACK");
} catch (XMPPException e) {
fail(e.getMessage());
}
}
});
// Start a private chat with another participant
Chat chat = muc2.createPrivateChat(room + "/testbot", null);
StanzaCollector collector = chat.createCollector();
chat.sendMessage("Hello there");
Message response = (Message) collector.nextResult(2000);
assertNotNull("No response", response);
assertEquals("Sender of response is incorrect", room + "/testbot", response.getFrom());
assertEquals("Body of response is incorrect", "ACK", response.getBody());
// User2 leaves the room
muc2.leave();
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.jivesoftware.smack.Chat in project Smack by igniterealtime.
the class XHTMLExtensionTest method testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage.
/**
* Low level API test.
* 1. User_1 will send a message with XHTML to user_2
* 2. User_2 will receive the message and iterate over the XHTML bodies to check if everything
* is fine
* 3. User_1 will wait several seconds for an ACK from user_2, if none is received then
* something is wrong
*/
public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final StanzaCollector chat2 = getConnection(1).createStanzaCollector(new ThreadFilter(chat1.getThreadID()));
// User1 creates a message to send to user2
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("Hey John, this is my new green!!!!");
// Create a XHTMLExtension Package and add it to the message
XHTMLExtension xhtmlExtension = new XHTMLExtension();
xhtmlExtension.addBody("<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
msg.addExtension(xhtmlExtension);
// User1 sends the message that contains the XHTML to user2
try {
chat1.sendMessage(msg);
} catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
Packet packet = chat2.nextResult(2000);
Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try {
xhtmlExtension = (XHTMLExtension) message.getExtension("html", "http://jabber.org/protocol/xhtml-im");
assertNotNull("Message without extension \"http://jabber.org/protocol/xhtml-im\"", xhtmlExtension);
assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
for (Iterator<String> it = xhtmlExtension.getBodies(); it.hasNext(); ) {
String body = it.next();
System.out.println(body);
}
} catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
}
use of org.jivesoftware.smack.Chat in project Smack by igniterealtime.
the class XHTMLExtensionTest method testSendSimpleXHTMLMessage.
/**
* Low level API test.
* This is a simple test to use with an XMPP client and check if the client receives the message
* 1. User_1 will send a message with formatted text (XHTML) to user_2
*/
public void testSendSimpleXHTMLMessage() {
// User1 creates a chat with user2
Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// User1 creates a message to send to user2
Message msg = new Message();
msg.setSubject("Any subject you want");
msg.setBody("Hey John, this is my new green!!!!");
// Create a XHTMLExtension Package and add it to the message
XHTMLExtension xhtmlExtension = new XHTMLExtension();
xhtmlExtension.addBody("<body><p style='font-size:large'>Hey John, this is my new <span style='color:green'>green</span><em>!!!!</em></p></body>");
msg.addExtension(xhtmlExtension);
// User1 sends the message that contains the XHTML to user2
try {
chat1.sendMessage(msg);
Thread.sleep(200);
} catch (Exception e) {
fail("An error occured sending the message with XHTML");
}
}
Aggregations