/** * Send an instant message from the tester agent and assert reception by the tested implementation */ public void thenTestSendMessage() { String body = "This is an IM coming from the tested implementation" + " on " + new Date().toString(); // create the message net.java.sip.communicator.service.protocol.Message msg = opSetBasicIM.createMessage(body); // register a listener in the op set ImEventCollector imEvtCollector = new ImEventCollector(); opSetBasicIM.addMessageListener(imEvtCollector); // register a listener in the tester agent JoustSimMessageEventCollector jsEvtCollector = new JoustSimMessageEventCollector(); fixture.testerAgent.addConversationListener(fixture.ourUserID, jsEvtCollector); Contact testerAgentContact = opSetPresence.findContactByID(fixture.testerAgent.getIcqUIN()); opSetBasicIM.sendInstantMessage(testerAgentContact, msg); imEvtCollector.waitForEvent(10000); jsEvtCollector.waitForEvent(10000); fixture.testerAgent.removeConversationListener(fixture.ourUserID, jsEvtCollector); opSetBasicIM.removeMessageListener(imEvtCollector); // verify that the message delivered event was dispatched assertTrue( "No events delivered when sending a message", imEvtCollector.collectedEvents.size() > 0); assertTrue( "Received evt was not an instance of " + MessageDeliveredEvent.class.getName(), imEvtCollector.collectedEvents.get(0) instanceof MessageDeliveredEvent); MessageDeliveredEvent evt = (MessageDeliveredEvent) imEvtCollector.collectedEvents.get(0); assertEquals( "message destination ", evt.getDestinationContact().getAddress(), fixture.testerAgent.getIcqUIN()); assertSame("source message", msg, evt.getSourceMessage()); // verify that the message has successfully arived at the destination assertTrue( "No messages received by the tester agent", jsEvtCollector.collectedMessageInfo.size() > 0); String receivedBody = ((MessageInfo) jsEvtCollector.collectedMessageInfo.get(0)).getMessage().getMessageBody(); assertEquals("received message body", msg.getContent(), receivedBody); }
/** * When a sent message is delivered shows it in the chat conversation panel. * * @param evt the event containing details on the message delivery */ public void messageDelivered(MessageDeliveredEvent evt) { Contact contact = evt.getDestinationContact(); MetaContact metaContact = GuiActivator.getContactListService().findMetaContactByContact(contact); if (logger.isTraceEnabled()) logger.trace("MESSAGE DELIVERED to contact: " + contact.getAddress()); ChatPanel chatPanel = chatWindowManager.getContactChat(metaContact, false); if (chatPanel != null) { Message msg = evt.getSourceMessage(); ProtocolProviderService protocolProvider = contact.getProtocolProvider(); if (logger.isTraceEnabled()) logger.trace( "MESSAGE DELIVERED: process message to chat for contact: " + contact.getAddress() + " MESSAGE: " + msg.getContent()); chatPanel.addMessage( this.mainFrame.getAccountAddress(protocolProvider), this.mainFrame.getAccountDisplayName(protocolProvider), evt.getTimestamp(), Chat.OUTGOING_MESSAGE, msg.getContent(), msg.getContentType(), msg.getMessageUID(), evt.getCorrectedMessageUID()); if (evt.isSmsMessage() && !ConfigurationUtils.isSmsNotifyTextDisabled()) { chatPanel.addMessage( contact.getDisplayName(), new Date(), Chat.ACTION_MESSAGE, GuiActivator.getResources().getI18NString("service.gui.SMS_SUCCESSFULLY_SENT"), "text"); } } }
/** * Helper function used to send a message to a contact, with the given extensions attached. * * @param to The contact to send the message to. * @param toResource The resource to send the message to or null if no resource has been specified * @param message The message to send. * @param extensions The XMPP extensions that should be attached to the message before sending. * @return The MessageDeliveryEvent that resulted after attempting to send this message, so the * calling function can modify it if needed. */ private MessageDeliveredEvent sendMessage( Contact to, ContactResource toResource, Message message, PacketExtension[] extensions) { if (!(to instanceof ContactJabberImpl)) throw new IllegalArgumentException("The specified contact is not a Jabber contact." + to); assertConnected(); org.jivesoftware.smack.packet.Message msg = new org.jivesoftware.smack.packet.Message(); String toJID = null; if (toResource != null) { if (toResource.equals(ContactResource.BASE_RESOURCE)) { toJID = to.getAddress(); } else toJID = ((ContactResourceJabberImpl) toResource).getFullJid(); } if (toJID == null) { toJID = to.getAddress(); } msg.setPacketID(message.getMessageUID()); msg.setTo(toJID); for (PacketExtension ext : extensions) { msg.addExtension(ext); } if (logger.isTraceEnabled()) logger.trace("Will send a message to:" + toJID + " chat.jid=" + toJID); MessageDeliveredEvent msgDeliveryPendingEvt = new MessageDeliveredEvent(message, to, toResource); MessageDeliveredEvent[] transformedEvents = messageDeliveryPendingTransform(msgDeliveryPendingEvt); if (transformedEvents == null || transformedEvents.length == 0) return null; for (MessageDeliveredEvent event : transformedEvents) { String content = event.getSourceMessage().getContent(); if (message.getContentType().equals(HTML_MIME_TYPE)) { msg.setBody(Html2Text.extractText(content)); // Check if the other user supports XHTML messages // make sure we use our discovery manager as it caches calls if (jabberProvider.isFeatureListSupported(toJID, HTML_NAMESPACE)) { // Add the XHTML text to the message XHTMLManager.addBody(msg, OPEN_BODY_TAG + content + CLOSE_BODY_TAG); } } else { // this is plain text so keep it as it is. msg.setBody(content); } // msg.addExtension(new Version()); if (event.isMessageEncrypted() && isCarbonEnabled) { msg.addExtension(new CarbonPacketExtension.PrivateExtension()); } MessageEventManager.addNotificationsRequests(msg, true, false, false, true); String threadID = getThreadIDForAddress(toJID); if (threadID == null) threadID = nextThreadID(); msg.setThread(threadID); msg.setType(org.jivesoftware.smack.packet.Message.Type.chat); msg.setFrom(jabberProvider.getConnection().getUser()); jabberProvider.getConnection().sendPacket(msg); putJidForAddress(toJID, threadID); } return new MessageDeliveredEvent(message, to, toResource); }
/** * Send an instant message from the tester agent and assert reception by the tested implementation */ public void thenTestSendMessage() { logger.debug( "Printing Server Stored list to see if message fails are contacts in each other lists"); ContactGroup rootGroup1 = ((OperationSetPersistentPresence) opSetPresence1).getServerStoredContactListRoot(); logger.debug("=========== Server Stored Contact List 1 ================="); logger.debug( "rootGroup=" + rootGroup1.getGroupName() + " rootGroup.childContacts=" + rootGroup1.countContacts() + "rootGroup.childGroups=" + rootGroup1.countSubgroups() + "Printing rootGroupContents=\n" + rootGroup1.toString()); ContactGroup rootGroup2 = ((OperationSetPersistentPresence) opSetPresence2).getServerStoredContactListRoot(); logger.debug("=========== Server Stored Contact List 2 ================="); logger.debug( "rootGroup=" + rootGroup2.getGroupName() + " rootGroup.childContacts=" + rootGroup2.countContacts() + "rootGroup.childGroups=" + rootGroup2.countSubgroups() + "Printing rootGroupContents=\n" + rootGroup2.toString()); String body = "This is an IM coming from the tested implementation" + " on " + new Date().toString(); // create the message net.java.sip.communicator.service.protocol.Message msg = opSetBasicIM1.createMessage(body); // register a listener in the op set ImEventCollector imEvtCollector1 = new ImEventCollector(); opSetBasicIM1.addMessageListener(imEvtCollector1); // register a listener in the tester agent ImEventCollector imEvtCollector2 = new ImEventCollector(); opSetBasicIM2.addMessageListener(imEvtCollector2); Contact testerAgentContact = opSetPresence1.findContactByID(fixture.userID2); opSetBasicIM1.sendInstantMessage(testerAgentContact, msg); imEvtCollector1.waitForEvent(10000); imEvtCollector2.waitForEvent(10000); opSetBasicIM1.removeMessageListener(imEvtCollector1); opSetBasicIM2.removeMessageListener(imEvtCollector2); // verify that the message delivered event was dispatched assertTrue( "No events delivered when sending a message", imEvtCollector1.collectedEvents.size() > 0); assertTrue( "Received evt was not an instance of " + MessageDeliveredEvent.class.getName(), imEvtCollector1.collectedEvents.get(0) instanceof MessageDeliveredEvent); MessageDeliveredEvent evt = (MessageDeliveredEvent) imEvtCollector1.collectedEvents.get(0); assertEquals("message destination ", evt.getDestinationContact().getAddress(), fixture.userID2); assertSame("source message", msg, evt.getSourceMessage()); // verify that the message has successfully arived at the destination assertTrue( "No messages received by the tester agent", imEvtCollector2.collectedEvents.size() > 0); assertFalse( "Message was unable to deliver !", imEvtCollector2.collectedEvents.get(0) instanceof MessageDeliveryFailedEvent); String receivedBody = ((MessageReceivedEvent) imEvtCollector2.collectedEvents.get(0)) .getSourceMessage() .getContent(); assertEquals("received message body", msg.getContent(), receivedBody); }