diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-07-14 15:15:09 (GMT) |
---|---|---|
committer | Nick Hudson <nick.hudson@isode.com> | 2015-07-22 16:34:21 (GMT) |
commit | 9ead0fdcca595df9dc3f4143122776b398dbe405 (patch) | |
tree | bf82bac84b07431ea61a5da5865bdc07a3afb53c /test | |
parent | 2341df7c72714df988b39cb2019562987ea28cbb (diff) | |
download | stroke-9ead0fdcca595df9dc3f4143122776b398dbe405.zip stroke-9ead0fdcca595df9dc3f4143122776b398dbe405.tar.bz2 |
Completes MUC in accordance with Swiften.
Updates MUC to be an abstract class.
Updates MUCBookmark, MUCManager, DirectedPresenceSender.
Adds MUCImpl.
License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Test-Information:
Test updated for MUC, in accordance with Swiften, which passes.
Added MockMUC too.
Change-Id: If5aa672e8adc093162d3c9890c8437d3edadea68
Diffstat (limited to 'test')
-rw-r--r-- | test/com/isode/stroke/muc/MUCTest.java | 137 | ||||
-rw-r--r-- | test/com/isode/stroke/muc/MockMUC.java | 146 |
2 files changed, 269 insertions, 14 deletions
diff --git a/test/com/isode/stroke/muc/MUCTest.java b/test/com/isode/stroke/muc/MUCTest.java index 4b845c0..3de840c 100644 --- a/test/com/isode/stroke/muc/MUCTest.java +++ b/test/com/isode/stroke/muc/MUCTest.java @@ -26,10 +26,14 @@ import com.isode.stroke.elements.MUCOccupant; import com.isode.stroke.elements.MUCOwnerPayload; import com.isode.stroke.elements.MUCUserPayload; import com.isode.stroke.elements.Presence; +import com.isode.stroke.elements.CapsInfo; +import com.isode.stroke.elements.VCard; import com.isode.stroke.jid.JID; import com.isode.stroke.presence.DirectedPresenceSender; import com.isode.stroke.presence.StanzaChannelPresenceSender; import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.base.ByteArray; +import com.isode.stroke.signals.Slot2; /** * Unit tests for MUC @@ -38,14 +42,15 @@ import com.isode.stroke.queries.IQRouter; public class MUCTest { private DummyStanzaChannel channel; private IQRouter router; - MUCRegistry mucRegistry; - StanzaChannelPresenceSender stanzaChannelPresenceSender; - DirectedPresenceSender presenceSender; + private MUCRegistry mucRegistry; + private StanzaChannelPresenceSender stanzaChannelPresenceSender; + private DirectedPresenceSender presenceSender; private static class JoinResult { - String nick; + String nick = ""; ErrorPayload error; }; - private Vector<JoinResult> joinResults; + private Vector<JoinResult> joinResults = new Vector<JoinResult>(); + private int nickChanges; @Before public void setUp() { @@ -54,6 +59,7 @@ public class MUCTest { mucRegistry = new MUCRegistry(); stanzaChannelPresenceSender = new StanzaChannelPresenceSender(channel); presenceSender = new DirectedPresenceSender(stanzaChannelPresenceSender); + nickChanges = 0; } @Test @@ -92,6 +98,20 @@ public class MUCTest { } @Test + public void testJoin_NoPresenceChangeDuringJoinDoesNotResendAfterJoinSuccess() { + MUC testling = createMUC(new JID("foo@bar.com")); + testling.joinAs("Alice"); + + receivePresence(new JID("foo@bar.com/Rabbit"), "Here"); + + assertEquals(1, channel.sentStanzas.size()); + Presence p = channel.getStanzaAtIndex(new Presence(), 0); + assertTrue(p != null); + assertEquals(new JID("foo@bar.com/Alice"), p.getTo()); + assertEquals("", p.getStatus()); + } + + @Test public void testCreateInstant() { MUC testling = createMUC(new JID("rabbithole@wonderland.lit")); testling.joinAs("Alice"); @@ -120,14 +140,13 @@ public class MUCTest { Presence initialPresence = new Presence(); initialPresence.setStatus(""); - //TODO: after vcard is ported this can be uncommented - /*VCard vcard = new VCard(); - vcard.setPhoto(createByteArray("15c30080ae98ec48be94bf0e191d43edd06e500a")); + VCard vcard = new VCard(); + vcard.setPhoto(new ByteArray("15c30080ae98ec48be94bf0e191d43edd06e500a")); initialPresence.addPayload(vcard); - CapsInfo caps = boost::make_shared<CapsInfo>(); + CapsInfo caps = new CapsInfo(); caps.setNode("http://swift.im"); caps.setVersion("p2UP0DrcVgKM6jJqYN/B92DKK0o="); - initialPresence.addPayload(caps);*/ + initialPresence.addPayload(caps); channel.sendPresence(initialPresence); @@ -136,8 +155,8 @@ public class MUCTest { Presence serverRespondsLocked = new Presence(); serverRespondsLocked.setFrom(new JID("test@rooms.swift.im/Test")); serverRespondsLocked.setTo(new JID("test@swift.im/6913d576d55f0b67")); - //serverRespondsLocked.addPayload(vcard); - //serverRespondsLocked.addPayload(caps); + serverRespondsLocked.addPayload(vcard); + serverRespondsLocked.addPayload(caps); serverRespondsLocked.setStatus(""); MUCUserPayload mucPayload = new MUCUserPayload(); MUCItem myItem = new MUCItem(); @@ -153,11 +172,94 @@ public class MUCTest { assertTrue(iq.getPayload(new MUCOwnerPayload()) != null); assertTrue(iq.getPayload(new MUCOwnerPayload()).getForm() != null); assertEquals(Form.Type.SUBMIT_TYPE, iq.getPayload(new MUCOwnerPayload()).getForm().getType()); -} + } + + @Test + public void testNicknameChange() { + MUC testling = createMUC(new JID("foo@bar.com")); + // Join as Rabbit + testling.joinAs("Rabbit"); + + // Rabbit joins + Presence rabbitJoins = new Presence(); + rabbitJoins.setTo(new JID("test@swift.im/6913d576d55f0b67")); + rabbitJoins.setFrom(new JID(testling.getJID().toString() + "/Rabbit")); + channel.onPresenceReceived.emit(rabbitJoins); + assertEquals(true, testling.hasOccupant("Rabbit")); + // Alice joins + Presence aliceJoins = new Presence(); + aliceJoins.setTo(new JID("test@swift.im/6913d576d55f0b67")); + aliceJoins.setFrom(new JID(testling.getJID().toString() + "/Alice")); + channel.onPresenceReceived.emit(aliceJoins); + assertEquals(true, testling.hasOccupant("Alice")); + + // Change nick to Dodo + testling.changeNickname("Dodo"); + Presence stanza = channel.getStanzaAtIndex(new Presence(), 1); + assertNotNull(stanza); + assertEquals("Dodo", stanza.getTo().getResource()); + + // Alice changes nick to Alice2 + stanza = new Presence(); + stanza.setFrom(new JID("foo@bar.com/Alice")); + stanza.setTo(router.getJID()); + stanza.setType(Presence.Type.Unavailable); + MUCUserPayload mucPayload = new MUCUserPayload(); + MUCItem myItem = new MUCItem(); + myItem.affiliation = MUCOccupant.Affiliation.Member; + myItem.nick = "Alice2"; + myItem.role = MUCOccupant.Role.Participant; + mucPayload.addItem(myItem); + mucPayload.addStatusCode(new MUCUserPayload.StatusCode(303)); + stanza.addPayload(mucPayload); + channel.onPresenceReceived.emit(stanza); + assertEquals(1, nickChanges); + assertEquals(false, testling.hasOccupant("Alice")); + assertEquals(true, testling.hasOccupant("Alice2")); + + // We (Rabbit) change nick to Robot + stanza = new Presence(); + stanza.setFrom(new JID("foo@bar.com/Rabbit")); + stanza.setTo(router.getJID()); + stanza.setType(Presence.Type.Unavailable); + mucPayload = new MUCUserPayload(); + myItem.affiliation = MUCOccupant.Affiliation.Member; + myItem.nick = "Robot"; + myItem.role = MUCOccupant.Role.Participant; + mucPayload.addItem(myItem); + mucPayload.addStatusCode(new MUCUserPayload.StatusCode(303)); + stanza.addPayload(mucPayload); + channel.onPresenceReceived.emit(stanza); + assertEquals(2, nickChanges); + assertEquals(false, testling.hasOccupant("Rabbit")); + assertEquals(true, testling.hasOccupant("Robot")); + } + + /*void testJoin_Success() { + MUC::ref testling = createMUC(JID("foo@bar.com")); + testling.onJoinFinished.connect(boost::bind(&MUCTest::handleJoinFinished, this, _1, _2)); + testling.joinAs("Alice"); + receivePresence(JID("foo@bar.com/Rabbit"), "Here"); + + CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(joinResults.size())); + CPPUNIT_ASSERT_EQUAL(std::string("Alice"), joinResults[0].nick); + CPPUNIT_ASSERT(joinResults[0].error); + } + + void testJoin_Fail() { + //CPPUNIT_ASSERT(!mucRegistry.isMUC(JID("foo@bar.com"))); + }*/ private MUC createMUC(JID jid) { - return new MUC(channel, router, presenceSender, jid, mucRegistry); + MUC muc = new MUCImpl(channel, router, presenceSender, jid, mucRegistry); + muc.onOccupantNicknameChanged.connect(new Slot2<String, String>() { + @Override + public void call(String s1, String s2) { + handleOccupantNicknameChanged(s1, s2); + } + }); + return muc; } private void handleJoinFinished(String nick, ErrorPayload error) { @@ -170,6 +272,13 @@ public class MUCTest { private void receivePresence(JID jid, String status) { Presence p = new Presence(status); p.setFrom(jid); + //MUCUserPayload mucUserPayload = new MUCUserPayload(); + //mucUserPayload.addItem(item); + //p.addPayload(mucUserPayload); channel.onPresenceReceived.emit(p); } + + private void handleOccupantNicknameChanged(final String s1, final String s2) { + nickChanges++; + } } diff --git a/test/com/isode/stroke/muc/MockMUC.java b/test/com/isode/stroke/muc/MockMUC.java new file mode 100644 index 0000000..1b695d2 --- /dev/null +++ b/test/com/isode/stroke/muc/MockMUC.java @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2013 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.muc; + +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Vector; + +import com.isode.stroke.client.StanzaChannel; +import com.isode.stroke.elements.ErrorPayload; +import com.isode.stroke.elements.CapsInfo; +import com.isode.stroke.elements.Form; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.MUCAdminPayload; +import com.isode.stroke.elements.MUCDestroyPayload; +import com.isode.stroke.elements.MUCInvitationPayload; +import com.isode.stroke.elements.MUCItem; +import com.isode.stroke.elements.MUCOccupant; +import com.isode.stroke.elements.MUCOwnerPayload; +import com.isode.stroke.elements.MUCPayload; +import com.isode.stroke.elements.MUCUserPayload; +import com.isode.stroke.elements.Message; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.jid.JID; +import com.isode.stroke.jid.JID.CompareType; +import com.isode.stroke.presence.DirectedPresenceSender; +import com.isode.stroke.queries.GenericRequest; +import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.signals.Signal; +import com.isode.stroke.signals.Signal1; +import com.isode.stroke.signals.Signal2; +import com.isode.stroke.signals.Signal3; +import com.isode.stroke.signals.SignalConnection; +import com.isode.stroke.signals.Slot1; +import com.isode.stroke.signals.Slot2; + +public class MockMUC extends MUC { + + private JID ownMUCJID = new JID(); + private Map<String, MUCOccupant> occupants_ = new HashMap<String, MUCOccupant>(); + + public MockMUC(JID muc) { + ownMUCJID = muc; + } + + /** + * Cause a user to appear to have entered the room. For testing only. + */ + public void insertOccupant(final MUCOccupant occupant) { + occupants_.put(occupant.getNick(), occupant); + onOccupantJoined.emit(occupant); + } + + /** + * Returns the (bare) JID of the MUC. + */ + public JID getJID() { + return ownMUCJID.toBare(); + } + + /** + * Returns if the room is unlocked and other people can join the room. + * @return True if joinable by others; false otherwise. + */ + public boolean isUnlocked() { + return true; + } + + public void joinAs(final String nick) {} + public void joinWithContextSince(final String nick, final Date since) {} + /*public void queryRoomInfo(); */ + /*public void queryRoomItems(); */ + /*public String getCurrentNick(); */ + public Map<String, MUCOccupant> getOccupants() { + return occupants_; + } + + public void changeNickname(final String newNickname) {} + public void part() {} + /*public void handleIncomingMessage(Message::ref message); */ + /** Expose public so it can be called when e.g. user goes offline */ + public void handleUserLeft(LeavingType l) {} + + /** + * Get occupant information. + */ + public MUCOccupant getOccupant(final String nick) { + return occupants_.get(nick); + } + public boolean hasOccupant(final String nick){ + return occupants_.containsKey(nick); + } + + public void kickOccupant(final JID jid) {} + + public void changeOccupantRole(final JID jid, MUCOccupant.Role newRole) { + String resource = jid.getResource(); + if(occupants_.containsKey(resource)) { + MUCOccupant old = occupants_.get(resource); + occupants_.remove(resource); + occupants_.put(resource, new MUCOccupant(old.getNick(), newRole, old.getAffiliation())); + onOccupantRoleChanged.emit(resource, occupants_.get(resource), old.getRole()); + } + } + + public void requestAffiliationList(MUCOccupant.Affiliation aff) {} + + public void changeAffiliation(final JID jid, MUCOccupant.Affiliation newAffilation) { + String resource = jid.getResource(); + if(occupants_.containsKey(resource)) { + MUCOccupant old = occupants_.get(resource); + occupants_.remove(resource); + occupants_.put(resource, new MUCOccupant(old.getNick(), old.getRole(), newAffilation)); + onOccupantAffiliationChanged.emit(resource, newAffilation, old.getAffiliation()); + } + } + + public void changeSubject(final String subject) {} + public void requestConfigurationForm() {} + public void configureRoom(Form f) {} + public void cancelConfigureRoom() {} + public void destroyRoom() {} + /** Send an invite for the person to join the MUC */ + public void invitePerson(final JID person, final String reason, boolean isImpromptu, boolean isReuseChat) {} + public void setCreateAsReservedIfNew() {} + public void setPassword(final String password) {} + + protected boolean isFromMUC(final JID j) { + return (ownMUCJID.compare(j, JID.CompareType.WithoutResource) == 0); + } + + protected String getOwnNick() { + return ownMUCJID.getResource(); + } +}
\ No newline at end of file |