diff options
author | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-06-09 10:08:25 (GMT) |
---|---|---|
committer | Tarun Gupta <tarun1995gupta@gmail.com> | 2015-06-14 13:30:22 (GMT) |
commit | 2180f81bd45045021d98c3a2e0649f10680596f9 (patch) | |
tree | 872db3fdeb890a232b5a1dc3b823a1e6692b5aa0 /test/com | |
parent | 9518f8b9d6924e95e4ad839f40866560bd7a7878 (diff) | |
download | stroke-2180f81bd45045021d98c3a2e0649f10680596f9.zip stroke-2180f81bd45045021d98c3a2e0649f10680596f9.tar.bz2 |
Add Avatar Functionality.
Adds AvatarManager, AvatarManagerImpl, AvatarStorage and different AvatarProviders including DummyAvatarProvider, NullAvatarProvider and OfflineAvatarProvider.
Also adds VCardAvatarManager and VCardUpdateAvatarManager.
Updates VCard Element, so that it does not return null on calling getPhoto() and thereby produce Null Pointer Exceptions.
License:
This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details.
Test-Information:
Tests are added for AvatarManagerImpl, VCardAvatarManager and VCardUpdateAvatarManager, which passes.
Tests for CombinedAvatarProvider will be added soon.
Change-Id: Ia6c0f82ae496427dc0cd11841487f6c53fd0fe1c
Diffstat (limited to 'test/com')
3 files changed, 544 insertions, 0 deletions
diff --git a/test/com/isode/stroke/avatars/AvatarManagerImplTest.java b/test/com/isode/stroke/avatars/AvatarManagerImplTest.java new file mode 100644 index 0000000..7ee10fd --- /dev/null +++ b/test/com/isode/stroke/avatars/AvatarManagerImplTest.java @@ -0,0 +1,148 @@ +/* + * Copyright (c) 2014 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.avatars; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertEquals; +import org.junit.Before; +import org.junit.Test; +import java.util.Vector; +import com.isode.stroke.elements.VCardUpdate; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.VCard; +import com.isode.stroke.avatars.VCardUpdateAvatarManager; +import com.isode.stroke.avatars.VCardAvatarManager; +import com.isode.stroke.avatars.AvatarMemoryStorage; +import com.isode.stroke.vcards.VCardMemoryStorage; +import com.isode.stroke.vcards.VCardManager; +import com.isode.stroke.muc.MUCRegistry; +import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.client.DummyStanzaChannel; +import com.isode.stroke.crypto.JavaCryptoProvider; +import com.isode.stroke.crypto.CryptoProvider; +import com.isode.stroke.stringcodecs.Hexify; +import com.isode.stroke.jid.JID; +import com.isode.stroke.base.ByteArray; +import com.isode.stroke.signals.SignalConnection; +import com.isode.stroke.signals.Slot1; + +public class AvatarManagerImplTest { + + private JID ownerJID; + private DummyStanzaChannel stanzaChannel; + private IQRouter iqRouter; + private CryptoProvider crypto; + private VCardMemoryStorage vcardStorage; + private VCardManager vcardManager; + private AvatarMemoryStorage avatarStorage; + private DummyMUCRegistry mucRegistry; + private AvatarManagerImpl avatarManager; + + public class DummyMUCRegistry extends MUCRegistry { + + public boolean isMUC(JID jid) { + return mucs_.contains(jid); + } + + public Vector<JID> mucs_ = new Vector<JID>(); + } + + @Before + public void setUp() { + ownerJID = new JID("owner@domain.com/theowner"); + stanzaChannel = new DummyStanzaChannel(); + iqRouter = new IQRouter(stanzaChannel); + crypto = new JavaCryptoProvider(); + vcardStorage = new VCardMemoryStorage(crypto); + vcardManager = new VCardManager(ownerJID, iqRouter, vcardStorage); + avatarStorage = new AvatarMemoryStorage(); + mucRegistry = new DummyMUCRegistry(); + avatarManager = new AvatarManagerImpl(vcardManager, stanzaChannel, avatarStorage, crypto, mucRegistry); + } + + @Test + public void testGetSetAvatar() { + /* initially we have no knowledge of the user or their avatar */ + JID personJID = new JID("person@domain.com/theperson"); + ByteArray avatar = avatarManager.getAvatar(personJID.toBare()); + assertTrue(avatar.getSize() == 0); + + /* notify the 'owner' JID that our avatar has changed */ + + ByteArray fullAvatar = new ByteArray("abcdefg"); + VCardUpdate vcardUpdate = new VCardUpdate(); + vcardUpdate.setPhotoHash(Hexify.hexify(crypto.getSHA1Hash(fullAvatar))); + Presence presence = new Presence(); + presence.setTo(ownerJID); + presence.setFrom(personJID); + presence.setType(Presence.Type.Available); + presence.addPayload(vcardUpdate); + stanzaChannel.onPresenceReceived.emit(presence); + + /* reply to the avatar request with our new avatar */ + + assertEquals(1, stanzaChannel.sentStanzas.size()); + IQ request = (IQ)(stanzaChannel.sentStanzas.get(0)); + stanzaChannel.sentStanzas.remove(stanzaChannel.sentStanzas.lastElement()); + assertNotNull(request); + VCard vcard = request.getPayload(new VCard()); + assertNotNull(vcard); + + IQ reply = new IQ(IQ.Type.Result); + reply.setTo(request.getFrom()); + reply.setFrom(request.getTo()); + reply.setID(request.getID()); + vcard.setPhoto(fullAvatar); + reply.addPayload(vcard); + stanzaChannel.onIQReceived.emit(reply); + + /* check hash through avatarManager that it received the correct photo */ + + ByteArray reportedAvatar = avatarManager.getAvatar(personJID.toBare()); + assertEquals(fullAvatar.toString(), reportedAvatar.toString()); + + /* send new presence to notify of blank avatar */ + + vcardUpdate = new VCardUpdate(); + presence = new Presence(); + presence.setTo(ownerJID); + presence.setFrom(personJID); + presence.setType(Presence.Type.Available); + presence.addPayload(vcardUpdate); + stanzaChannel.onPresenceReceived.emit(presence); + + /* reply to the avatar request with our EMPTY avatar */ + + assertEquals(1, stanzaChannel.sentStanzas.size()); + request = (IQ)(stanzaChannel.sentStanzas.get(0)); + stanzaChannel.sentStanzas.remove(stanzaChannel.sentStanzas.lastElement()); + assertNotNull(request); + vcard = request.getPayload(new VCard()); + assertNotNull(vcard); + + ByteArray blankAvatar = new ByteArray(""); + reply = new IQ(IQ.Type.Result); + reply.setTo(request.getFrom()); + reply.setFrom(request.getTo()); + reply.setID(request.getID()); + vcard.setPhoto(blankAvatar); + reply.addPayload(vcard); + stanzaChannel.onIQReceived.emit(reply); + + /* check hash through avatarManager that it received the correct photo */ + + reportedAvatar = avatarManager.getAvatar(personJID.toBare()); + assertEquals(blankAvatar.toString(), reportedAvatar.toString()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/avatars/VCardAvatarManagerTest.java b/test/com/isode/stroke/avatars/VCardAvatarManagerTest.java new file mode 100755 index 0000000..56121ac --- /dev/null +++ b/test/com/isode/stroke/avatars/VCardAvatarManagerTest.java @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2010-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.avatars; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import org.junit.Before; +import org.junit.Test; +import java.util.Vector; +import com.isode.stroke.avatars.VCardAvatarManager; +import com.isode.stroke.elements.VCard; +import com.isode.stroke.vcards.VCardMemoryStorage; +import com.isode.stroke.avatars.AvatarMemoryStorage; +import com.isode.stroke.vcards.VCardManager; +import com.isode.stroke.muc.MUCRegistry; +import com.isode.stroke.jid.JID; +import com.isode.stroke.base.ByteArray; +import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.client.DummyStanzaChannel; +import com.isode.stroke.crypto.JavaCryptoProvider; +import com.isode.stroke.crypto.CryptoProvider; +import com.isode.stroke.stringcodecs.Hexify; +import com.isode.stroke.signals.SignalConnection; +import com.isode.stroke.signals.Slot1; +import com.isode.stroke.elements.IQ; + +public class VCardAvatarManagerTest { + + private class DummyMUCRegistry extends MUCRegistry { + + private Vector<JID> mucs_ = new Vector<JID>(); + + public boolean isMUC(JID jid) { + if(mucs_.contains(jid)) + return true; + else + return false; + } + } + + private JID ownJID; + private DummyStanzaChannel stanzaChannel; + private IQRouter iqRouter; + private DummyMUCRegistry mucRegistry; + private AvatarMemoryStorage avatarStorage; + private VCardManager vcardManager; + private VCardMemoryStorage vcardStorage; + private ByteArray avatar1; + private String avatar1Hash; + private Vector<JID> changes; + private JID user1; + private JID user2; + private CryptoProvider crypto; + private SignalConnection onAvatarChangedConnection; + + @Before + public void setUp() { + crypto = new JavaCryptoProvider(); + ownJID = new JID("foo@fum.com/bum"); + stanzaChannel = new DummyStanzaChannel(); + stanzaChannel.setAvailable(true); + iqRouter = new IQRouter(stanzaChannel); + mucRegistry = new DummyMUCRegistry(); + avatarStorage = new AvatarMemoryStorage(); + vcardStorage = new VCardMemoryStorage(crypto); + vcardManager = new VCardManager(ownJID, iqRouter, vcardStorage); + avatar1 = new ByteArray("abcdefg"); + avatar1Hash = Hexify.hexify(crypto.getSHA1Hash(avatar1)); + changes = new Vector<JID>(); + user1 = new JID("user1@bar.com/bla"); + user2 = new JID("user2@foo.com/baz"); + } + + private VCardAvatarManager createManager() { + VCardAvatarManager result = new VCardAvatarManager(vcardManager, avatarStorage, crypto, mucRegistry); + onAvatarChangedConnection = result.onAvatarChanged.connect(new Slot1<JID>() { + + public void call(JID j1) { + handleAvatarChanged(j1); + } + }); + return result; + } + + private void storeVCardWithPhoto(JID jid, ByteArray avatar) { + VCard vcard = new VCard(); + vcard.setPhoto(avatar); + vcardStorage.setVCard(jid, vcard); + } + + private void storeEmptyVCard(JID jid) { + VCard vcard = new VCard(); + vcardStorage.setVCard(jid, vcard); + } + + private void handleAvatarChanged(JID jid) { + changes.add(jid); + } + + private void sendVCardResult() { + VCard vcard = new VCard(); + vcard.setFullName("Foo Bar"); + stanzaChannel.onIQReceived.emit(IQ.createResult(new JID("baz@fum.com/dum"), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID(), vcard)); + } + + @Test + public void testGetAvatarHashKnownAvatar() { + VCardAvatarManager testling = createManager(); + storeVCardWithPhoto(user1.toBare(), avatar1); + avatarStorage.addAvatar(avatar1Hash, avatar1); + + String result = testling.getAvatarHash(user1); + assertNotNull(result); + assertEquals(avatar1Hash, result); + } + + @Test + public void testGetAvatarHashEmptyAvatar() { + VCardAvatarManager testling = createManager(); + storeEmptyVCard(user1.toBare()); + + String result = testling.getAvatarHash(user1); + assertNotNull(result); + assertEquals("", result); + } + + @Test + public void testGetAvatarHashUnknownAvatarKnownVCardStoresAvatar() { + VCardAvatarManager testling = createManager(); + storeVCardWithPhoto(user1.toBare(), avatar1); + + String result = testling.getAvatarHash(user1); + assertNotNull(result); + assertEquals(avatar1Hash, result); + assertTrue(avatarStorage.hasAvatar(avatar1Hash)); + assertEquals(avatar1, avatarStorage.getAvatar(avatar1Hash)); + } + + @Test + public void testGetAvatarHashUnknownAvatarUnknownVCard() { + VCardAvatarManager testling = createManager(); + + String result = testling.getAvatarHash(user1); + + assertNotNull(result); + assertEquals("", result); + } + + @Test + public void testGetAvatarHashKnownAvatarUnknownVCard() { + VCardAvatarManager testling = createManager(); + avatarStorage.setAvatarForJID(user1, avatar1Hash); + + String result = testling.getAvatarHash(user1); + + assertNotNull(result); + assertEquals("", result); + } + + @Test + public void testVCardUpdateTriggersUpdate() { + VCardAvatarManager testling = createManager(); + vcardManager.requestVCard(user1); + sendVCardResult(); + + assertEquals(1, changes.size()); + } +}
\ No newline at end of file diff --git a/test/com/isode/stroke/avatars/VCardUpdateAvatarManagerTest.java b/test/com/isode/stroke/avatars/VCardUpdateAvatarManagerTest.java new file mode 100755 index 0000000..1030d5d --- /dev/null +++ b/test/com/isode/stroke/avatars/VCardUpdateAvatarManagerTest.java @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2010-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.avatars; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import org.junit.Before; +import org.junit.Test; +import java.util.Vector; +import com.isode.stroke.elements.VCardUpdate; +import com.isode.stroke.elements.Presence; +import com.isode.stroke.elements.IQ; +import com.isode.stroke.elements.VCard; +import com.isode.stroke.avatars.VCardUpdateAvatarManager; +import com.isode.stroke.avatars.VCardAvatarManager; +import com.isode.stroke.avatars.AvatarMemoryStorage; +import com.isode.stroke.vcards.VCardMemoryStorage; +import com.isode.stroke.vcards.VCardManager; +import com.isode.stroke.muc.MUCRegistry; +import com.isode.stroke.queries.IQRouter; +import com.isode.stroke.client.DummyStanzaChannel; +import com.isode.stroke.crypto.JavaCryptoProvider; +import com.isode.stroke.crypto.CryptoProvider; +import com.isode.stroke.stringcodecs.Hexify; +import com.isode.stroke.jid.JID; +import com.isode.stroke.base.ByteArray; +import com.isode.stroke.signals.SignalConnection; +import com.isode.stroke.signals.Slot1; + +public class VCardUpdateAvatarManagerTest { + + private class DummyMUCRegistry extends MUCRegistry { + + private Vector<JID> mucs_ = new Vector<JID>(); + + public boolean isMUC(JID jid) { + return mucs_.contains(jid); + } + } + + private JID ownJID; + private DummyStanzaChannel stanzaChannel; + private IQRouter iqRouter; + private DummyMUCRegistry mucRegistry; + private AvatarMemoryStorage avatarStorage; + private VCardManager vcardManager; + private VCardMemoryStorage vcardStorage; + private ByteArray avatar1; + private String avatar1Hash; + private Vector<JID> changes; + private JID user1; + private JID user2; + private CryptoProvider crypto; + private SignalConnection onAvatarChangedConnection; + + @Before + public void setUp() { + crypto = new JavaCryptoProvider(); + ownJID = new JID("foo@fum.com/bum"); + stanzaChannel = new DummyStanzaChannel(); + stanzaChannel.setAvailable(true); + iqRouter = new IQRouter(stanzaChannel); + mucRegistry = new DummyMUCRegistry(); + avatarStorage = new AvatarMemoryStorage(); + vcardStorage = new VCardMemoryStorage(crypto); + vcardManager = new VCardManager(ownJID, iqRouter, vcardStorage); + avatar1 = new ByteArray("abcdefg"); + avatar1Hash = Hexify.hexify(crypto.getSHA1Hash(avatar1)); + changes = new Vector<JID>(); + user1 = new JID("user1@bar.com/bla"); + user2 = new JID("user2@foo.com/baz"); + } + + private VCardUpdateAvatarManager createManager() { + VCardUpdateAvatarManager result = new VCardUpdateAvatarManager(vcardManager, stanzaChannel, avatarStorage, crypto, mucRegistry); + onAvatarChangedConnection = result.onAvatarChanged.connect(new Slot1<JID>() { + + public void call(JID j1) { + handleAvatarChanged(j1); + } + }); + return result; + } + + private Presence createPresenceWithPhotoHash(JID jid, String hash) { + Presence presence = new Presence(); + presence.setFrom(jid); + presence.addPayload(new VCardUpdate(hash)); + return presence; + } + + private IQ createVCardResult(ByteArray avatar) { + VCard vcard = new VCard(); + if (!avatar.isEmpty()) { + vcard.setPhoto(avatar); + } + return IQ.createResult(new JID("baz@fum.com"), stanzaChannel.sentStanzas.get(0).getTo(), stanzaChannel.sentStanzas.get(0).getID(), vcard); + } + + private void handleAvatarChanged(JID jid) { + changes.add(jid); + } + + @Test + public void testUpdate_NewHashNewVCardRequestsVCard() { + VCardUpdateAvatarManager testling = createManager(); + stanzaChannel.onPresenceReceived.emit(createPresenceWithPhotoHash(user1, avatar1Hash)); + + assertEquals(1, stanzaChannel.sentStanzas.size()); + assertTrue(stanzaChannel.isRequestAtIndex(0, user1.toBare(), IQ.Type.Get, new VCard())); + } + + @Test + public void testUpdate_NewHashStoresAvatarAndEmitsNotificationOnVCardReceive() { + VCardUpdateAvatarManager testling = createManager(); + stanzaChannel.onPresenceReceived.emit(createPresenceWithPhotoHash(user1, avatar1Hash)); + stanzaChannel.onIQReceived.emit(createVCardResult(avatar1)); + + assertEquals(1, changes.size()); + assertEquals(user1.toBare(), changes.get(0)); + String hash = testling.getAvatarHash(user1.toBare()); + assertNotNull(hash); + assertEquals(avatar1Hash, hash); + assertTrue(avatarStorage.hasAvatar(avatar1Hash)); + assertEquals(avatar1, avatarStorage.getAvatar(avatar1Hash)); + } + + @Test + public void testUpdate_KnownHash() { + VCardUpdateAvatarManager testling = createManager(); + stanzaChannel.onPresenceReceived.emit(createPresenceWithPhotoHash(user1, avatar1Hash)); + stanzaChannel.onIQReceived.emit(createVCardResult(avatar1)); + changes.clear(); + stanzaChannel.sentStanzas.clear(); + + stanzaChannel.onPresenceReceived.emit(createPresenceWithPhotoHash(user1, avatar1Hash)); + + assertEquals(0, stanzaChannel.sentStanzas.size()); + assertEquals(0, changes.size()); + } + + @Test + public void testUpdate_KnownHashFromDifferentUserDoesNotRequestVCardButTriggersNotification() { + VCardUpdateAvatarManager testling = createManager(); + stanzaChannel.onPresenceReceived.emit(createPresenceWithPhotoHash(user1, avatar1Hash)); + stanzaChannel.onIQReceived.emit(createVCardResult(avatar1)); + changes.clear(); + stanzaChannel.sentStanzas.clear(); + + stanzaChannel.onPresenceReceived.emit(createPresenceWithPhotoHash(user2, avatar1Hash)); + assertEquals(0, stanzaChannel.sentStanzas.size()); + assertEquals(1, changes.size()); + assertEquals(user2.toBare(), changes.get(0)); + String hash = testling.getAvatarHash(user2.toBare()); + assertNotNull(hash); + assertEquals(avatar1Hash, hash); + } + + + @Test + public void testVCardWithEmptyPhoto() { + VCardUpdateAvatarManager testling = createManager(); + vcardManager.requestVCard(new JID("foo@bar.com")); + stanzaChannel.onIQReceived.emit(createVCardResult(new ByteArray())); + + assertTrue(!avatarStorage.hasAvatar(Hexify.hexify(crypto.getSHA1Hash(new ByteArray())))); + String hash = testling.getAvatarHash(new JID("foo@bar.com")); + assertNotNull(hash); + assertEquals("", hash); + } + + @Test + public void testStanzaChannelReset_ClearsHash() { + VCardUpdateAvatarManager testling = createManager(); + stanzaChannel.onPresenceReceived.emit(createPresenceWithPhotoHash(user1, avatar1Hash)); + stanzaChannel.onIQReceived.emit(createVCardResult(avatar1)); + changes.clear(); + stanzaChannel.sentStanzas.clear(); + + stanzaChannel.setAvailable(false); + stanzaChannel.setAvailable(true); + + assertEquals(1, changes.size()); + assertEquals(user1.toBare(), changes.get(0)); + String hash = testling.getAvatarHash(user1.toBare()); + assertNull(hash); + } + + @Test + public void testStanzaChannelReset_ReceiveHashAfterResetUpdatesHash() { + VCardUpdateAvatarManager testling = createManager(); + stanzaChannel.onPresenceReceived.emit(createPresenceWithPhotoHash(user1, avatar1Hash)); + stanzaChannel.onIQReceived.emit(createVCardResult(avatar1)); + changes.clear(); + stanzaChannel.sentStanzas.clear(); + + stanzaChannel.setAvailable(false); + stanzaChannel.setAvailable(true); + stanzaChannel.onPresenceReceived.emit(createPresenceWithPhotoHash(user1, avatar1Hash)); + + assertEquals(2, changes.size()); + assertEquals(user1.toBare(), changes.get(1)); + String hash = testling.getAvatarHash(user1.toBare()); + assertNotNull(hash); + assertEquals(avatar1Hash, hash); + } +}
\ No newline at end of file |