LowLevelRosterIntegrationTest.java

  1. /**
  2.  *
  3.  * Copyright 2016-2020 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.roster;

  18. import java.util.concurrent.TimeoutException;

  19. import org.jivesoftware.smack.AbstractXMPPConnection;
  20. import org.jivesoftware.smack.packet.Presence;

  21. import org.igniterealtime.smack.inttest.AbstractSmackLowLevelIntegrationTest;
  22. import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
  23. import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;
  24. import org.igniterealtime.smack.inttest.util.IntegrationTestRosterUtil;
  25. import org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint;
  26. import org.jxmpp.jid.FullJid;

  27. public class LowLevelRosterIntegrationTest extends AbstractSmackLowLevelIntegrationTest {

  28.     public LowLevelRosterIntegrationTest(SmackIntegrationTestEnvironment environment) {
  29.         super(environment);
  30.     }

  31.     @SmackIntegrationTest
  32.     public void testPresenceEventListenersOffline(final AbstractXMPPConnection conOne,
  33.             final AbstractXMPPConnection conTwo) throws TimeoutException, Exception {
  34.         IntegrationTestRosterUtil.ensureBothAccountsAreNotInEachOthersRoster(conOne, conTwo);

  35.         final Roster rosterOne = Roster.getInstanceFor(conOne);
  36.         final Roster rosterTwo = Roster.getInstanceFor(conTwo);

  37.         rosterOne.createItem(conTwo.getUser().asBareJid(), "Con Two", null);
  38.         rosterTwo.createItem(conOne.getUser().asBareJid(), "Con One", null);

  39.         IntegrationTestRosterUtil.ensureBothAccountsAreSubscribedToEachOther(conOne, conTwo, timeout);

  40.         final SimpleResultSyncPoint offlineTriggered = new SimpleResultSyncPoint();

  41.         final AbstractPresenceEventListener presenceEventListener = new AbstractPresenceEventListener() {
  42.             @Override
  43.             public void presenceUnavailable(FullJid jid, Presence presence) {
  44.                 if (!jid.equals(conTwo.getUser())) {
  45.                     return;
  46.                 }
  47.                 offlineTriggered.signal();
  48.             }
  49.         };
  50.         rosterOne.addPresenceEventListener(presenceEventListener);

  51.         try {
  52.             // Disconnect conTwo, this should cause an 'unavailable' presence to be sent from conTwo to
  53.             // conOne.
  54.             conTwo.disconnect();

  55.             Boolean result = offlineTriggered.waitForResult(timeout);
  56.             if (!result) {
  57.                 throw new Exception("presenceUnavailable() was not called");
  58.             }
  59.         } finally {
  60.             // Clean up test fixture.
  61.             rosterOne.removePresenceEventListener(presenceEventListener);
  62.             conTwo.connect();
  63.             conTwo.login();
  64.             IntegrationTestRosterUtil.ensureBothAccountsAreNotInEachOthersRoster(conOne, conTwo);
  65.         }
  66.     }

  67. }