Search in sources :

Example 21 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class MessageTest method testOfflineStorageWithNegativePriority.

/**
     * User0 is connected from 1 resource with a negative priority presence. User1
     * sends a message to the bare JID of User0. Messages should be stored offline.
     * User0 then changes the priority presence to a positive value. Check that
     * offline messages were delivered to the user.
     *
     * @throws Exception if an error occurs.
     */
public void testOfflineStorageWithNegativePriority() throws Exception {
    // Set this connection with negative priority
    Presence presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    presence.setPriority(-1);
    getConnection(0).sendStanza(presence);
    // Let the server process the change in presences
    Thread.sleep(200);
    // User0 listen for incoming traffic
    StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    // User1 sends a message to the bare JID of User0
    Chat chat = getConnection(1).getChatManager().createChat(getBareJID(0), null);
    chat.sendMessage("Test 1");
    chat.sendMessage("Test 2");
    // Check that messages were sent to resource with highest priority
    Message message = (Message) collector.nextResult(2000);
    assertNull("Messages were not stored offline", message);
    // Set this connection with positive priority
    presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    presence.setPriority(1);
    getConnection(0).sendStanza(presence);
    // Let the server process the change in presences
    Thread.sleep(200);
    message = (Message) collector.nextResult(2000);
    assertNotNull("Offline messages were not delivered", message);
    assertEquals("Test 1", message.getBody());
    message = (Message) collector.nextResult(1000);
    assertNotNull(message);
    assertEquals("Test 2", message.getBody());
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Example 22 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class MessageTest method testHighestPriority.

/**
     * User0 is connected from 2 resources. User0 is available in both resources
     * but with different priority presence values. User1 sends a message to the
     * bare JID of User0. Check that the resource with highest priority will get
     * the messages.
     *
     * @throws Exception if an error occurs.
     */
public void testHighestPriority() throws Exception {
    // Create another connection for the same user of connection 1
    ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
    XMPPTCPConnection conn3 = new XMPPConnection(connectionConfiguration);
    conn3.connect();
    conn3.login(getUsername(0), getPassword(0), "Home");
    // Set this connection as highest priority
    Presence presence = new Presence(Presence.Type.available);
    presence.setPriority(10);
    conn3.sendStanza(presence);
    // Set this connection as highest priority
    presence = new Presence(Presence.Type.available);
    presence.setPriority(5);
    getConnection(0).sendStanza(presence);
    // Let the server process the change in presences
    Thread.sleep(200);
    // User0 listen in both connected clients
    StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    StanzaCollector coll3 = conn3.createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    // User1 sends a message to the bare JID of User0 
    Chat chat = getConnection(1).getChatManager().createChat(getBareJID(0), null);
    chat.sendMessage("Test 1");
    chat.sendMessage("Test 2");
    // Check that messages were sent to resource with highest priority
    Message message = (Message) collector.nextResult(2000);
    assertNull("Resource with lowest priority got the message", message);
    message = (Message) coll3.nextResult(2000);
    assertNotNull(message);
    assertEquals("Test 1", message.getBody());
    message = (Message) coll3.nextResult(1000);
    assertNotNull(message);
    assertEquals("Test 2", message.getBody());
    conn3.disconnect();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Example 23 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class MessageTest method testDirectPresence.

/**
     * Will a user recieve a message from another after only sending the user a directed presence,
     * or will Wildfire intercept for offline storage?
     *
     * User1 becomes lines. User0 never sent an available presence to the server but
     * instead sent one to User1. User1 sends a message to User0. Should User0 get the
     * message?
     */
public void testDirectPresence() {
    getConnection(1).sendStanza(new Presence(Presence.Type.available));
    Presence presence = new Presence(Presence.Type.available);
    presence.setTo(getBareJID(1));
    getConnection(0).sendStanza(presence);
    StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    try {
        getConnection(1).getChatManager().createChat(getBareJID(0), null).sendMessage("Test 1");
    } catch (XMPPException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
    Message message = (Message) collector.nextResult(2500);
    assertNotNull("Message not recieved from remote user", message);
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Example 24 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class MessageTest method testMostRecentActive.

/**
     * User0 is connected from 2 resources. User0 is available in both resources
     * with same priority presence values and same show values. User1 sends a message to the
     * bare JID of User0. Check that the resource with most recent activity will get
     * the messages.
     *
     * @throws Exception if an error occurs.
     */
public void testMostRecentActive() throws Exception {
    // Create another connection for the same user of connection 1
    ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
    XMPPTCPConnection conn3 = new XMPPConnection(connectionConfiguration);
    conn3.connect();
    conn3.login(getUsername(0), getPassword(0), "Home");
    // Set this connection as highest priority
    Presence presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    presence.setPriority(10);
    conn3.sendStanza(presence);
    // Set this connection as highest priority
    presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    presence.setPriority(10);
    getConnection(0).sendStanza(presence);
    connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
    XMPPTCPConnection conn4 = new XMPPConnection(connectionConfiguration);
    conn4.connect();
    conn4.login(getUsername(0), getPassword(0), "Home2");
    presence = new Presence(Presence.Type.available);
    presence.setMode(Presence.Mode.available);
    presence.setPriority(4);
    getConnection(0).sendStanza(presence);
    // Let the server process the change in presences
    Thread.sleep(200);
    // User0 listen in both connected clients
    StanzaCollector collector = getConnection(0).createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    StanzaCollector coll3 = conn3.createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    StanzaCollector coll4 = conn4.createStanzaCollector(new MessageTypeFilter(Message.Type.chat));
    // Send a message from this resource to indicate most recent activity 
    conn3.sendStanza(new Message("admin@" + getXMPPServiceDomain()));
    // User1 sends a message to the bare JID of User0
    Chat chat = getConnection(1).getChatManager().createChat(getBareJID(0), null);
    chat.sendMessage("Test 1");
    chat.sendMessage("Test 2");
    // Check that messages were sent to resource with highest priority
    Message message = (Message) collector.nextResult(2000);
    assertNull("Resource with oldest activity got the message", message);
    message = (Message) coll4.nextResult(2000);
    assertNull(message);
    message = (Message) coll3.nextResult(2000);
    assertNotNull(message);
    assertEquals("Test 1", message.getBody());
    message = (Message) coll3.nextResult(1000);
    assertNotNull(message);
    assertEquals("Test 2", message.getBody());
    conn3.disconnect();
    conn4.disconnect();
}
Also used : MessageTypeFilter(org.jivesoftware.smack.filter.MessageTypeFilter) Message(org.jivesoftware.smack.packet.Message) Presence(org.jivesoftware.smack.packet.Presence)

Example 25 with Presence

use of org.jivesoftware.smack.packet.Presence in project Smack by igniterealtime.

the class PresenceTest method testOfflineStatusPresence.

/**
     * User1 logs in, then sets offline presence information (presence with status text). User2
     * logs in and checks to see if offline presence is returned.
     *
     * @throws Exception if an exception occurs.
     */
public void testOfflineStatusPresence() throws Exception {
    // Add a new roster entry for other user.
    Roster roster = getConnection(0).getRoster();
    roster.createEntry(getBareJID(1), "gato1", null);
    // Wait up to 2 seconds
    long initial = System.currentTimeMillis();
    while (System.currentTimeMillis() - initial < 2000 && (roster.getPresence(getBareJID(1)).getType().equals(Presence.Type.unavailable))) {
        Thread.sleep(100);
    }
    // Sign out of conn1 with status
    Presence offlinePresence = new Presence(Presence.Type.unavailable);
    offlinePresence.setStatus("Offline test");
    getConnection(1).disconnect(offlinePresence);
    // Wait 500 ms
    Thread.sleep(500);
    Presence presence = getConnection(0).getRoster().getPresence(getBareJID(1));
    assertEquals("Offline presence status not received.", "Offline test", presence.getStatus());
    // Sign out of conn0.
    getConnection(0).disconnect();
    // See if conneciton 0 can get offline status.
    XMPPTCPConnection con0 = getConnection(0);
    con0.connect();
    con0.login(getUsername(0), getUsername(0));
    // Wait 500 ms
    Thread.sleep(500);
    presence = con0.getRoster().getPresence(getBareJID(1));
    assertTrue("Offline presence status not received after logout.", "Offline test".equals(presence.getStatus()));
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Aggregations

Presence (org.jivesoftware.smack.packet.Presence)89 Message (org.jivesoftware.smack.packet.Message)17 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)10 StanzaCollector (org.jivesoftware.smack.StanzaCollector)7 AndFilter (org.jivesoftware.smack.filter.AndFilter)7 Jid (org.jxmpp.jid.Jid)7 Resourcepart (org.jxmpp.jid.parts.Resourcepart)7 UserPresence (jetbrains.communicator.core.users.UserPresence)6 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)5 MUCInitialPresence (org.jivesoftware.smackx.muc.packet.MUCInitialPresence)5 Test (org.junit.Test)5 BareJid (org.jxmpp.jid.BareJid)5 EntityFullJid (org.jxmpp.jid.EntityFullJid)5 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 WaitFor (jetbrains.communicator.util.WaitFor)4 AccountItem (com.xabber.android.data.account.AccountItem)3 ClientInfo (com.xabber.android.data.extension.capability.ClientInfo)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 XMPPException (org.jivesoftware.smack.XMPPException)3