Search in sources :

Example 36 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 37 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 38 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)

Example 39 with Presence

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

the class RosterSmackTest method cleanUpRoster.

/**
     * Clean up all the entries in the roster
     */
private void cleanUpRoster() {
    for (int i = 0; i < getMaxConnections(); i++) {
        // Delete all the entries from the roster
        Roster roster = getConnection(i).getRoster();
        final CountDownLatch removalLatch = new CountDownLatch(roster.getEntryCount());
        roster.addRosterListener(new RosterListener() {

            @Override
            public void presenceChanged(Presence presence) {
            }

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

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

            @Override
            public void entriesAdded(Collection<String> addresses) {
            }
        });
        for (RosterEntry entry : roster.getEntries()) {
            try {
                roster.removeEntry(entry);
            } catch (XMPPException e) {
                e.printStackTrace();
                fail(e.getMessage());
            }
        }
        try {
            removalLatch.await(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            fail(e.getMessage());
        }
    }
    assertEquals("Wrong number of entries in connection 0", 0, getConnection(0).getRoster().getEntryCount());
    assertEquals("Wrong number of entries in connection 1", 0, getConnection(1).getRoster().getEntryCount());
    assertEquals("Wrong number of entries in connection 2", 0, getConnection(2).getRoster().getEntryCount());
}
Also used : Presence(org.jivesoftware.smack.packet.Presence) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 40 with Presence

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

the class AbstractXMPPConnection method afterSuccessfulLogin.

protected void afterSuccessfulLogin(final boolean resumed) throws NotConnectedException, InterruptedException {
    // Indicate that we're now authenticated.
    this.authenticated = true;
    // will be null
    if (config.isDebuggerEnabled() && debugger != null) {
        debugger.userHasLogged(user);
    }
    callConnectionAuthenticatedListener(resumed);
    // send the initial presence.
    if (config.isSendPresence() && !resumed) {
        sendStanza(new Presence(Presence.Type.available));
    }
}
Also used : Presence(org.jivesoftware.smack.packet.Presence)

Aggregations

Presence (org.jivesoftware.smack.packet.Presence)103 Message (org.jivesoftware.smack.packet.Message)21 Resourcepart (org.jxmpp.jid.parts.Resourcepart)12 Jid (org.jxmpp.jid.Jid)11 MessageTypeFilter (org.jivesoftware.smack.filter.MessageTypeFilter)10 AccountItem (com.xabber.android.data.account.AccountItem)9 UserJid (com.xabber.android.data.entity.UserJid)9 AccountJid (com.xabber.android.data.entity.AccountJid)7 StanzaCollector (org.jivesoftware.smack.StanzaCollector)7 AndFilter (org.jivesoftware.smack.filter.AndFilter)7 UserPresence (jetbrains.communicator.core.users.UserPresence)6 MUCUser (org.jivesoftware.smackx.muc.packet.MUCUser)6 BareJid (org.jxmpp.jid.BareJid)6 ClientInfo (com.xabber.android.data.extension.capability.ClientInfo)5 ArrayList (java.util.ArrayList)5 Date (java.util.Date)5 StanzaTypeFilter (org.jivesoftware.smack.filter.StanzaTypeFilter)5 MUCInitialPresence (org.jivesoftware.smackx.muc.packet.MUCInitialPresence)5 Test (org.junit.Test)5 EntityFullJid (org.jxmpp.jid.EntityFullJid)5