summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorTarun Gupta <tarun1995gupta@gmail.com>2015-07-25 06:48:08 (GMT)
committerKevin Smith <kevin.smith@isode.com>2015-08-17 11:10:49 (GMT)
commit2533374644704040ca67aba4e1240a9d6ea450c8 (patch)
treef8b707a2b1544f934c0bc626eab51fb737a63541 /test
parent0a1f7199e26523dd2693f44a5841c5434cc9000d (diff)
downloadstroke-2533374644704040ca67aba4e1240a9d6ea450c8.zip
stroke-2533374644704040ca67aba4e1240a9d6ea450c8.tar.bz2
Updates Presence in accordance with Swiften.
Updates DirectedPresenceSender, PayloadAddingPresenceSender, PresenceOracle and SubscriptionManager. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Tests added for: DirectedPresenceSenderTest, PayloadAddingPresenceSenderTest and PresenceOracle. All tests pass. Change-Id: I9ad899965f5ac32c6f21805b530c234e391be6ba
Diffstat (limited to 'test')
-rw-r--r--test/com/isode/stroke/presence/DirectedPresenceSenderTest.java153
-rw-r--r--test/com/isode/stroke/presence/PayloadAddingPresenceSenderTest.java144
-rw-r--r--test/com/isode/stroke/presence/PresenceOracleTest.java219
3 files changed, 516 insertions, 0 deletions
diff --git a/test/com/isode/stroke/presence/DirectedPresenceSenderTest.java b/test/com/isode/stroke/presence/DirectedPresenceSenderTest.java
new file mode 100644
index 0000000..a40a1a9
--- /dev/null
+++ b/test/com/isode/stroke/presence/DirectedPresenceSenderTest.java
@@ -0,0 +1,153 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.presence;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+import org.junit.Before;
+import com.isode.stroke.elements.Presence;
+import com.isode.stroke.client.DummyStanzaChannel;
+import com.isode.stroke.presence.DirectedPresenceSender;
+import com.isode.stroke.presence.StanzaChannelPresenceSender;
+import com.isode.stroke.jid.JID;
+
+public class DirectedPresenceSenderTest {
+
+ private DummyStanzaChannel channel;
+ private StanzaChannelPresenceSender stanzaChannelPresenceSender;
+ private Presence testPresence;
+ private Presence secondTestPresence;
+
+ private DirectedPresenceSender createPresenceSender() {
+ return new DirectedPresenceSender(stanzaChannelPresenceSender);
+ }
+
+ @Before
+ public void setUp() {
+ channel = new DummyStanzaChannel();
+ testPresence = new Presence();
+ testPresence.setStatus("Foo");
+ secondTestPresence = new Presence();
+ secondTestPresence.setStatus("Bar");
+ stanzaChannelPresenceSender = new StanzaChannelPresenceSender(channel);
+ }
+
+ @Test
+ public void testSendPresence() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.sendPresence(testPresence);
+
+ assertEquals(1, (channel.sentStanzas.size()));
+ Presence presence = (Presence)(channel.sentStanzas.get(0));
+ assertEquals(testPresence, presence);
+ }
+
+ @Test
+ public void testSendPresence_UndirectedPresenceWithDirectedPresenceReceivers() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.AndSendPresence);
+
+ testling.sendPresence(testPresence);
+
+ assertEquals(2, (channel.sentStanzas.size()));
+ Presence presence = (Presence)(channel.sentStanzas.get(0));
+ assertEquals(testPresence, presence);
+ presence = (Presence)(channel.sentStanzas.get(1));
+ assertEquals(testPresence.getStatus(), presence.getStatus());
+ assertEquals(new JID("alice@wonderland.lit/teaparty"), presence.getTo());
+ }
+
+ @Test
+ public void testSendPresence_DirectedPresenceWithDirectedPresenceReceivers() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.AndSendPresence);
+ channel.sentStanzas.clear();
+
+ testPresence.setTo(new JID("foo@bar.com"));
+ testling.sendPresence(testPresence);
+
+ assertEquals(1, (channel.sentStanzas.size()));
+ Presence presence = (Presence)(channel.sentStanzas.get(0));
+ assertEquals(testPresence, presence);
+ }
+
+ @Test
+ public void testAddDirectedPresenceReceiver() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.sendPresence(testPresence);
+ channel.sentStanzas.clear();
+
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.AndSendPresence);
+
+ assertEquals(1, (channel.sentStanzas.size()));
+ Presence presence = (Presence)(channel.sentStanzas.get(0));
+ assertEquals(testPresence.getStatus(), presence.getStatus());
+ assertEquals(new JID("alice@wonderland.lit/teaparty"), presence.getTo());
+ }
+
+ @Test
+ public void testAddDirectedPresenceReceiver_WithoutSendingPresence() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.sendPresence(testPresence);
+ channel.sentStanzas.clear();
+
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.DontSendPresence);
+
+ assertEquals(0, (channel.sentStanzas.size()));
+ }
+
+ @Test
+ public void testAddDirectedPresenceReceiver_AfterSendingDirectedPresence() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.sendPresence(testPresence);
+ secondTestPresence.setTo(new JID("foo@bar.com"));
+ testling.sendPresence(secondTestPresence);
+ channel.sentStanzas.clear();
+
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.AndSendPresence);
+
+ assertEquals(1, (channel.sentStanzas.size()));
+ Presence presence = (Presence)(channel.sentStanzas.get(0));
+ assertEquals(testPresence.getStatus(), presence.getStatus());
+ assertEquals(new JID("alice@wonderland.lit/teaparty"), presence.getTo());
+ }
+
+ @Test
+ public void testRemoveDirectedPresenceReceiver() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.DontSendPresence);
+
+ testling.removeDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.AndSendPresence);
+ testling.sendPresence(testPresence);
+
+ assertEquals(2, (channel.sentStanzas.size()));
+ assertEquals(((Presence)(channel.sentStanzas.get(0))).getType(), Presence.Type.Unavailable);
+ assertEquals(channel.sentStanzas.get(1), testPresence);
+ }
+
+ @Test
+ public void testRemoveDirectedPresenceReceiver_WithoutSendingPresence() {
+ DirectedPresenceSender testling = createPresenceSender();
+ testling.addDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.AndSendPresence);
+ channel.sentStanzas.clear();
+
+ testling.removeDirectedPresenceReceiver(new JID("alice@wonderland.lit/teaparty"), DirectedPresenceSender.SendPresence.DontSendPresence);
+ testling.sendPresence(testPresence);
+
+ assertEquals(1, (channel.sentStanzas.size()));
+ assertEquals(channel.sentStanzas.get(0), testPresence);
+ }
+} \ No newline at end of file
diff --git a/test/com/isode/stroke/presence/PayloadAddingPresenceSenderTest.java b/test/com/isode/stroke/presence/PayloadAddingPresenceSenderTest.java
new file mode 100644
index 0000000..fb75a99
--- /dev/null
+++ b/test/com/isode/stroke/presence/PayloadAddingPresenceSenderTest.java
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.presence;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+import org.junit.Before;
+import com.isode.stroke.elements.Presence;
+import com.isode.stroke.elements.Payload;
+import com.isode.stroke.client.DummyStanzaChannel;
+import com.isode.stroke.presence.DirectedPresenceSender;
+import com.isode.stroke.presence.StanzaChannelPresenceSender;
+import com.isode.stroke.presence.PayloadAddingPresenceSender;
+import com.isode.stroke.jid.JID;
+
+public class PayloadAddingPresenceSenderTest {
+
+ private DummyStanzaChannel stanzaChannel;
+ private StanzaChannelPresenceSender presenceSender;
+
+ private PayloadAddingPresenceSender createSender() {
+ PayloadAddingPresenceSender sender = new PayloadAddingPresenceSender(presenceSender);
+ return sender;
+ }
+
+ private class MyPayload extends Payload {
+
+ public MyPayload() {
+
+ }
+
+ public MyPayload(final String body) {
+ this.body = body;
+ }
+
+ public String body = "";
+ };
+
+ @Before
+ public void setUp() {
+ stanzaChannel = new DummyStanzaChannel();
+ presenceSender = new StanzaChannelPresenceSender(stanzaChannel);
+ }
+
+ @Test
+ public void testSetPayloadAddsPayloadOnPresenceSend() {
+ PayloadAddingPresenceSender testling = createSender();
+
+ testling.setPayload(new MyPayload("foo"));
+ testling.sendPresence(new Presence("bar"));
+
+ assertEquals(1, (stanzaChannel.sentStanzas.size()));
+ assertEquals(("bar"), stanzaChannel.getStanzaAtIndex(new Presence(), 0).getStatus());
+ assertNotNull(stanzaChannel.getStanzaAtIndex(new Presence(), 0).getPayload(new MyPayload()));
+ }
+
+ @Test
+ public void testSetNullPayloadDoesNotAddPayloadOnPresenceSend() {
+ PayloadAddingPresenceSender testling = createSender();
+
+ testling.setPayload(null);
+ testling.sendPresence(new Presence("bar"));
+
+ assertEquals(1, (stanzaChannel.sentStanzas.size()));
+ assertEquals(("bar"), stanzaChannel.getStanzaAtIndex(new Presence(), 0).getStatus());
+ assertNull(stanzaChannel.getStanzaAtIndex(new Presence(), 0).getPayload(new MyPayload()));
+ }
+
+ @Test
+ public void testSendPresenceDoesNotAlterOriginalPayload() {
+ PayloadAddingPresenceSender testling = createSender();
+
+ testling.setPayload(new MyPayload("foo"));
+ Presence presence = new Presence("bar");
+ testling.sendPresence(presence);
+
+ assertNull(presence.getPayload(new MyPayload()));
+ }
+
+ @Test
+ public void testSetPayloadAfterInitialPresenceResendsPresence() {
+ PayloadAddingPresenceSender testling = createSender();
+
+ testling.sendPresence(new Presence("bar"));
+ testling.setPayload(new MyPayload("foo"));
+
+ assertEquals(2, (stanzaChannel.sentStanzas.size()));
+ assertEquals(("bar"), stanzaChannel.getStanzaAtIndex(new Presence(), 1).getStatus());
+ assertNotNull(stanzaChannel.getStanzaAtIndex(new Presence(), 1).getPayload(new MyPayload()));
+ }
+
+ @Test
+ public void testSetPayloadAfterUnavailablePresenceDoesNotResendPresence() {
+ PayloadAddingPresenceSender testling = createSender();
+
+ testling.sendPresence(new Presence("bar"));
+
+ Presence presence = new Presence("bar");
+ presence.setType(Presence.Type.Unavailable);
+ testling.sendPresence(presence);
+
+ testling.setPayload(new MyPayload("foo"));
+
+ assertEquals(2, (stanzaChannel.sentStanzas.size()));
+ }
+
+ @Test
+ public void testSetPayloadAfterResetDoesNotResendPresence() {
+ PayloadAddingPresenceSender testling = createSender();
+ testling.sendPresence(new Presence("bar"));
+
+ testling.reset();
+ testling.setPayload(new MyPayload("foo"));
+
+ assertEquals(1, (stanzaChannel.sentStanzas.size()));
+ }
+
+ @Test
+ public void testSendDirectedPresenceIsNotResent() {
+ PayloadAddingPresenceSender testling = createSender();
+
+ testling.sendPresence(new Presence("bar"));
+ Presence directedPresence = new Presence("baz");
+ directedPresence.setTo(new JID("foo@bar.com"));
+ testling.sendPresence(directedPresence);
+ testling.setPayload(new MyPayload("foo"));
+
+ assertEquals(3, (stanzaChannel.sentStanzas.size()));
+ assertEquals(("bar"), stanzaChannel.getStanzaAtIndex(new Presence(), 2).getStatus());
+ }
+} \ No newline at end of file
diff --git a/test/com/isode/stroke/presence/PresenceOracleTest.java b/test/com/isode/stroke/presence/PresenceOracleTest.java
new file mode 100644
index 0000000..03aea04
--- /dev/null
+++ b/test/com/isode/stroke/presence/PresenceOracleTest.java
@@ -0,0 +1,219 @@
+/*
+ * Copyright (c) 2010 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.presence;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import org.junit.Test;
+import org.junit.Before;
+import com.isode.stroke.elements.Presence;
+import com.isode.stroke.elements.Payload;
+import com.isode.stroke.client.DummyStanzaChannel;
+import com.isode.stroke.presence.DirectedPresenceSender;
+import com.isode.stroke.presence.StanzaChannelPresenceSender;
+import com.isode.stroke.presence.PayloadAddingPresenceSender;
+import com.isode.stroke.presence.PresenceOracle;
+import com.isode.stroke.presence.SubscriptionManager;
+import com.isode.stroke.jid.JID;
+import com.isode.stroke.signals.SignalConnection;
+import com.isode.stroke.signals.Slot2;
+import com.isode.stroke.signals.Slot3;
+import com.isode.stroke.signals.Slot1;
+import java.util.Collection;
+import java.util.Vector;
+
+public class PresenceOracleTest {
+
+ private class SubscriptionRequestInfo {
+ public JID jid;
+ public String reason;
+
+ public SubscriptionRequestInfo() {
+ jid = new JID();
+ reason = "";
+ }
+ };
+
+ private PresenceOracle oracle_;
+ private SubscriptionManager subscriptionManager_;
+ private DummyStanzaChannel stanzaChannel_;
+ private Collection<Presence> changes = new Vector<Presence>();
+ private Collection<SubscriptionRequestInfo> subscriptionRequests = new Vector<SubscriptionRequestInfo>();
+ private JID user1;
+ private JID user1alt;
+ private JID user2;
+
+ private Presence makeOnline(final String resource, int priority) {
+ Presence presence = new Presence();
+ presence.setPriority(priority);
+ presence.setFrom(new JID("alice@wonderland.lit/" + resource));
+ return presence;
+ }
+
+ private Presence makeOffline(final String resource) {
+ Presence presence = new Presence();
+ presence.setFrom(new JID("alice@wonderland.lit" + resource));
+ presence.setType(Presence.Type.Unavailable);
+ return presence;
+ }
+
+ private void handlePresenceChange(Presence newPresence) {
+ changes.add(newPresence);
+ }
+
+ private void handlePresenceSubscriptionRequest(final JID jid, final String reason) {
+ SubscriptionRequestInfo subscriptionRequest = new SubscriptionRequestInfo();
+ subscriptionRequest.jid = jid;
+ subscriptionRequest.reason = reason;
+ subscriptionRequests.add(subscriptionRequest);
+ }
+
+ private Presence createPresence(final JID jid) {
+ Presence sentPresence = new Presence("blarb");
+ sentPresence.setFrom(jid);
+ return sentPresence;
+ }
+
+ @Before
+ public void setUp() {
+ stanzaChannel_ = new DummyStanzaChannel();
+ oracle_ = new PresenceOracle(stanzaChannel_);
+ oracle_.onPresenceChange.connect(new Slot1<Presence>() {
+ @Override
+ public void call(Presence p) {
+ handlePresenceChange(p);
+ }
+ });
+ subscriptionManager_ = new SubscriptionManager(stanzaChannel_);
+ subscriptionManager_.onPresenceSubscriptionRequest.connect(new Slot3<JID, String, Presence>() {
+ @Override
+ public void call(JID j, String s, Presence p) {
+ handlePresenceSubscriptionRequest(j, s);
+ }
+ });
+ user1 = new JID("user1@foo.com/Foo");
+ user1alt = new JID("user1@foo.com/Bar");
+ user2 = new JID("user2@bar.com/Bar");
+ }
+
+ @Test
+ public void testHighestPresenceSingle() {
+ JID bareJID = new JID("alice@wonderland.lit");
+ Presence fiveOn = makeOnline("blah", 5);
+ Presence fiveOff = makeOffline("/blah");
+ assertEquals(null, oracle_.getHighestPriorityPresence(bareJID));
+ stanzaChannel_.onPresenceReceived.emit(fiveOn);
+ assertEquals(fiveOn, oracle_.getHighestPriorityPresence(bareJID));
+ stanzaChannel_.onPresenceReceived.emit(fiveOff);
+ assertEquals(fiveOff, oracle_.getHighestPriorityPresence(bareJID));
+ }
+
+ @Test
+ public void testHighestPresenceMultiple() {
+ JID bareJID = new JID("alice@wonderland.lit");
+ Presence fiveOn = makeOnline("blah", 5);
+ Presence fiveOff = makeOffline("/blah");
+ Presence tenOn = makeOnline("bert", 10);
+ Presence tenOff = makeOffline("/bert");
+ stanzaChannel_.onPresenceReceived.emit(fiveOn);
+ stanzaChannel_.onPresenceReceived.emit(tenOn);
+ assertEquals(tenOn, oracle_.getHighestPriorityPresence(bareJID));
+ stanzaChannel_.onPresenceReceived.emit(fiveOff);
+ assertEquals(tenOn, oracle_.getHighestPriorityPresence(bareJID));
+ stanzaChannel_.onPresenceReceived.emit(fiveOn);
+ stanzaChannel_.onPresenceReceived.emit(tenOff);
+ assertEquals(fiveOn, oracle_.getHighestPriorityPresence(bareJID));
+ }
+
+ @Test
+ public void testHighestPresenceGlobal() {
+ JID bareJID = new JID("alice@wonderland.lit");
+ Presence fiveOn = makeOnline("blah", 5);
+ Presence fiveOff = makeOffline("/blah");
+ Presence tenOn = makeOnline("bert", 10);
+ Presence allOff = makeOffline("");
+ stanzaChannel_.onPresenceReceived.emit(fiveOn);
+ stanzaChannel_.onPresenceReceived.emit(tenOn);
+ stanzaChannel_.onPresenceReceived.emit(allOff);
+ assertEquals(allOff, oracle_.getHighestPriorityPresence(bareJID));
+ }
+
+ @Test
+ public void testHighestPresenceChangePriority() {
+ JID bareJID = new JID("alice@wonderland.lit");
+ Presence fiveOn = makeOnline("blah", 5);
+ Presence fiveOff = makeOffline("/blah");
+ Presence tenOn = makeOnline("bert", 10);
+ Presence tenOnThree = makeOnline("bert", 3);
+ Presence tenOff = makeOffline("/bert");
+ stanzaChannel_.onPresenceReceived.emit(fiveOn);
+ stanzaChannel_.onPresenceReceived.emit(tenOn);
+ stanzaChannel_.onPresenceReceived.emit(tenOnThree);
+ assertEquals(fiveOn, oracle_.getHighestPriorityPresence(bareJID));
+ stanzaChannel_.onPresenceReceived.emit(fiveOff);
+ assertEquals(tenOnThree, oracle_.getHighestPriorityPresence(bareJID));
+ stanzaChannel_.onPresenceReceived.emit(fiveOn);
+ assertEquals(fiveOn, oracle_.getHighestPriorityPresence(bareJID));
+ }
+
+ @Test
+ public void testReceivePresence() {
+ Presence sentPresence = createPresence(user1);
+ stanzaChannel_.onPresenceReceived.emit(sentPresence);
+
+ assertEquals(1, (changes.size()));
+ assertEquals(0, (subscriptionRequests.size()));
+ assertEquals(sentPresence, changes.toArray()[0]);
+ assertEquals(sentPresence, oracle_.getLastPresence(user1));
+ }
+
+ @Test
+ public void testReceivePresenceFromDifferentResources() {
+ Presence sentPresence1 = createPresence(user1);
+ Presence sentPresence2 = createPresence(user1alt);
+ stanzaChannel_.onPresenceReceived.emit(sentPresence1);
+ stanzaChannel_.onPresenceReceived.emit(sentPresence2);
+
+ assertEquals(sentPresence1, oracle_.getLastPresence(user1));
+ assertEquals(sentPresence2, oracle_.getLastPresence(user1alt));
+ }
+
+ @Test
+ public void testSubscriptionRequest() {
+ String reasonText = "Because I want to";
+ JID sentJID = new JID("me@example.com");
+
+ Presence sentPresence = new Presence();
+ sentPresence.setType(Presence.Type.Subscribe);
+ sentPresence.setFrom(sentJID);
+ sentPresence.setStatus(reasonText);
+ stanzaChannel_.onPresenceReceived.emit(sentPresence);
+
+ assertEquals(0, (changes.size()));
+ assertEquals(1, (subscriptionRequests.size()));
+ assertEquals(sentJID, ((SubscriptionRequestInfo)subscriptionRequests.toArray()[0]).jid);
+ assertEquals(reasonText, ((SubscriptionRequestInfo)subscriptionRequests.toArray()[0]).reason);
+ }
+
+ @Test
+ public void testReconnectResetsPresences() {
+ Presence sentPresence = createPresence(user1);
+ stanzaChannel_.onPresenceReceived.emit(sentPresence);
+ stanzaChannel_.setAvailable(false);
+ stanzaChannel_.setAvailable(true);
+
+ assertNull(oracle_.getLastPresence(user1));
+ }
+} \ No newline at end of file