Search in sources :

Example 26 with Presence

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

the class PresenceTest method testMultipleResources.

/**
     * User1 is connected from 2 resources. User1 adds User0 to his roster. Ensure
     * that presences are correctly retrieved for User1. User1 logs off from one resource
     * and ensure that presences are still correct for User1.
     */
public void testMultipleResources() throws Exception {
    // Create another connection for the same user of connection 1
    ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
    XMPPTCPConnection conn4 = new XMPPConnection(connectionConfiguration);
    conn4.connect();
    conn4.login(getUsername(1), getPassword(1), "Home");
    // Add a new roster entry
    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)).isAvailable())) {
        Thread.sleep(100);
    }
    // Check that a presence is returned for the new contact
    Presence presence = roster.getPresence(getBareJID(1));
    assertTrue("Returned an offline Presence for an existing user", presence.isAvailable());
    presence = roster.getPresenceResource(getBareJID(1) + "/Home");
    assertTrue("Returned an offline Presence for Home resource", presence.isAvailable());
    presence = roster.getPresenceResource(getFullJID(1));
    assertTrue("Returned an offline Presence for Smack resource", presence.isAvailable());
    Iterator<Presence> presences = roster.getPresences(getBareJID(1));
    assertTrue("Returned an offline Presence for an existing user", presence.isAvailable());
    assertNotNull("No presence was found for user1", presences);
    assertTrue("No presence was found for user1", presences.hasNext());
    presences.next();
    assertTrue("Only one presence was found for user1", presences.hasNext());
    // User1 logs out from one resource
    conn4.disconnect();
    // Wait up to 1 second
    Thread.sleep(700);
    // Check that a presence is returned for the new contact
    presence = roster.getPresence(getBareJID(1));
    assertTrue("Returned a null Presence for an existing user", presence.isAvailable());
    presence = roster.getPresenceResource(getFullJID(1));
    assertTrue("Returned a null Presence for Smack resource", presence.isAvailable());
    presence = roster.getPresenceResource(getBareJID(1) + "/Home");
    assertTrue("Returned a Presence for no longer connected resource", !presence.isAvailable());
    presences = roster.getPresences(getBareJID(1));
    assertNotNull("No presence was found for user1", presences);
    Presence value = presences.next();
    assertTrue("No presence was found for user1", value.isAvailable());
    assertFalse("More than one presence was found for user1", presences.hasNext());
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Example 27 with Presence

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

the class PresenceTest method testNotAvailablePresence.

/**
     * User1 logs from 2 resources but only one is available. User0 sends a message
     * to the full JID of the unavailable resource. User1 in the not available resource
     * should receive the message.
     * TODO Fix this in Wildfire but before check if XMPP spec requests this feature
     */
public void testNotAvailablePresence() throws XMPPException {
    // Change the presence to unavailable of User_1
    getConnection(1).sendStanza(new Presence(Presence.Type.unavailable));
    // User_1 will log in again using another resource (that is going to be available)
    XMPPTCPConnection conn = createConnection();
    conn.connect();
    conn.login(getUsername(1), getPassword(1), "OtherPlace");
    // Create chats between participants
    Chat chat0 = getConnection(0).getChatManager().createChat(getFullJID(1), null);
    Chat chat1 = getConnection(1).getChatManager().createChat(getBareJID(0), chat0.getThreadID(), null);
    // Test delivery of message to the presence with highest priority
    chat0.sendMessage("Hello");
    /*assertNotNull("Not available connection didn't receive message sent to full JID",
                chat1.nextMessage(2000));
        assertNull("Not available connection received an unknown message",
                chat1.nextMessage(1000));*/
    conn.disconnect();
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Example 28 with Presence

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

the class RosterSmackTest method testOfflinePresencesAfterDisconnection.

/**
     * Tests the creation of a roster and then simulates abrupt termination. Cached presences
     * must go offline. At reconnection, presences must go back to online.
     * <ol>
     *     <li> Create some entries
     *     <li> Breack the connection
     *     <li> Check offline presences
     *     <li> Whait for automatic reconnection
     *     <li> Check online presences
     * </ol>
     */
public void testOfflinePresencesAfterDisconnection() throws Exception {
    // Add a new roster entry
    Roster roster = getConnection(0).getRoster();
    roster.createEntry(getBareJID(1), "gato11", null);
    roster.createEntry(getBareJID(2), "gato12", null);
    // Wait up to 2 seconds to let the server process presence subscriptions
    long initial = System.currentTimeMillis();
    while (System.currentTimeMillis() - initial < 2000 && (!roster.getPresence(getBareJID(1)).isAvailable() || !roster.getPresence(getBareJID(2)).isAvailable())) {
        Thread.sleep(100);
    }
    Thread.sleep(200);
    // Break the connection
    getConnection(0).notifyConnectionError(new Exception("Simulated Error"));
    Presence presence = roster.getPresence(getBareJID(1));
    assertFalse("Unavailable presence not found for offline user", presence.isAvailable());
    assertEquals("Unavailable presence not found for offline user", Presence.Type.unavailable, presence.getType());
    // Reconnection should occur in 10 seconds
    Thread.sleep(12200);
    presence = roster.getPresence(getBareJID(1));
    assertTrue("Presence not found for user", presence.isAvailable());
    assertEquals("Presence should be online after a connection reconnection", Presence.Type.available, presence.getType());
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Example 29 with Presence

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

the class RosterSmackTest method testChangeNameToUnfiledEntry.

/**
     * 1. Create an unfiled entry
     * 2. Change its name
     * 3. Check that the name has been modified
     * 4. Reload the whole roster
     * 5. Check that the name has been modified
     */
public void testChangeNameToUnfiledEntry() {
    try {
        // Add a new roster entry
        Roster roster = getConnection(0).getRoster();
        CountDownLatch latch = new CountDownLatch(1);
        setupCountdown(latch, roster);
        roster.createEntry(getBareJID(1), null, null);
        waitForCountdown(latch, roster, 1);
        final CountDownLatch updateLatch = new CountDownLatch(2);
        RosterListener latchCounter = new RosterListener() {

            @Override
            public void entriesAdded(Collection<String> addresses) {
            }

            @Override
            public void entriesUpdated(Collection<String> addresses) {
                updateLatch.countDown();
            }

            @Override
            public void entriesDeleted(Collection<String> addresses) {
            }

            @Override
            public void presenceChanged(Presence presence) {
            }
        };
        roster.addRosterListener(latchCounter);
        // Change the roster entry name and check if the change was made
        for (RosterEntry entry : roster.getEntries()) {
            entry.setName("gato11");
            assertEquals("gato11", entry.getName());
        }
        // Reload the roster and check the name again
        roster.reload();
        updateLatch.await(5, TimeUnit.SECONDS);
        for (RosterEntry entry : roster.getEntries()) {
            assertEquals("gato11", entry.getName());
        }
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : Collection(java.util.Collection) Presence(org.jivesoftware.smack.packet.Presence) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 30 with Presence

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

the class RosterSmackTest method testRosterPresences.

/**
     * Test presence management.<p>
     * 
     * 1. Log in user0 from a client and user1 from 2 clients
     * 2. Create presence subscription of type BOTH between 2 users
     * 3. Check that presence is correctly delivered to both users
     * 4. User1 logs out from a client
     * 5. Check that presence for each connected resource is correct
     */
public void testRosterPresences() throws Exception {
    Presence presence;
    // Create another connection for the same user of connection 1
    ConnectionConfiguration connectionConfiguration = new ConnectionConfiguration(getHost(), getPort(), getXMPPServiceDomain());
    XMPPTCPConnection conn4 = new XMPPConnection(connectionConfiguration);
    conn4.connect();
    conn4.login(getUsername(1), getPassword(1), "Home");
    // Add a new roster entry
    Roster roster = getConnection(0).getRoster();
    roster.createEntry(getBareJID(1), "gato11", null);
    // Wait up to 2 seconds
    long initial = System.currentTimeMillis();
    while (System.currentTimeMillis() - initial < 2000 && (roster.getPresence(getBareJID(1)).getType() == Presence.Type.unavailable)) {
        Thread.sleep(100);
    }
    // Check that a presence is returned for a user
    presence = roster.getPresence(getBareJID(1));
    assertTrue("Returned a null Presence for an existing user", presence.isAvailable());
    // Check that the right presence is returned for a user+resource
    presence = roster.getPresenceResource(getUsername(1) + "@" + conn4.getXMPPServiceDomain() + "/Home");
    assertEquals("Returned the wrong Presence", "Home", StringUtils.parseResource(presence.getFrom()));
    // Check that the right presence is returned for a user+resource
    presence = roster.getPresenceResource(getFullJID(1));
    assertTrue("Presence not found for user " + getFullJID(1), presence.isAvailable());
    assertEquals("Returned the wrong Presence", "Smack", StringUtils.parseResource(presence.getFrom()));
    // Check the returned presence for a non-existent user+resource
    presence = roster.getPresenceResource("noname@" + getXMPPServiceDomain() + "/Smack");
    assertFalse("Available presence was returned for a non-existing user", presence.isAvailable());
    assertEquals("Returned Presence for a non-existing user has the incorrect type", Presence.Type.unavailable, presence.getType());
    // Check that the returned presences are correct
    Iterator<Presence> presences = roster.getPresences(getBareJID(1));
    int count = 0;
    while (presences.hasNext()) {
        count++;
        presences.next();
    }
    assertEquals("Wrong number of returned presences", count, 2);
    // Close the connection so one presence must go
    conn4.disconnect();
    // Check that the returned presences are correct
    presences = roster.getPresences(getBareJID(1));
    count = 0;
    while (presences.hasNext()) {
        count++;
        presences.next();
    }
    assertEquals("Wrong number of returned presences", count, 1);
}
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