diff options
-rw-r--r-- | src/com/isode/stroke/client/BlockListImpl.java | 24 | ||||
-rw-r--r-- | test/com/isode/stroke/client/BlockListImplTest.java | 99 |
2 files changed, 111 insertions, 12 deletions
diff --git a/src/com/isode/stroke/client/BlockListImpl.java b/src/com/isode/stroke/client/BlockListImpl.java index a85f86c..e8a4619 100644 --- a/src/com/isode/stroke/client/BlockListImpl.java +++ b/src/com/isode/stroke/client/BlockListImpl.java @@ -39,19 +39,19 @@ public class BlockListImpl extends BlockList { return items; } - public void setItems(final Vector<JID> items) { - for (final JID jid : this.items) { - if(items.contains(jid)) { - onItemRemoved.emit(jid); - } - } + public void setItems(final Vector<JID> newItems) { + for (final JID jid : items) { + if(!newItems.contains(jid)) { + onItemRemoved.emit(jid); + } + } - for (final JID jid : items) { - if(this.items.contains(jid)) { - onItemAdded.emit(jid); - } - } - this.items = items; + for (final JID jid : newItems) { + if(!this.items.contains(jid)) { + onItemAdded.emit(jid); + } + } + this.items = newItems; } public void addItem(final JID item) { diff --git a/test/com/isode/stroke/client/BlockListImplTest.java b/test/com/isode/stroke/client/BlockListImplTest.java new file mode 100644 index 0000000..74eeecb --- /dev/null +++ b/test/com/isode/stroke/client/BlockListImplTest.java @@ -0,0 +1,99 @@ +/* Copyright (c) 2016, Isode Limited, London, England. + * All rights reserved. + * + * Acquisition and use of this software and related materials for any + * purpose requires a written license agreement from Isode Limited, + * or a written license from an organisation licensed by Isode Limited + * to grant such a license. + * + */ +package com.isode.stroke.client; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import java.util.Vector; + +import org.junit.Before; +import org.junit.Test; + +import com.isode.stroke.jid.JID; +import com.isode.stroke.signals.Slot1; + +public class BlockListImplTest { + + private final BlockListImpl blockList_ = new BlockListImpl(); + private final List<JID> addedJIDs_ = new ArrayList<JID>(); + private final List<JID> removedJIDs_ = new ArrayList<JID>(); + + @Before + public void setUp() { + blockList_.addItem(new JID("a@example.com")); + blockList_.addItem(new JID("b@example.com")); + + blockList_.onItemAdded.connect(new Slot1<JID>() { + + @Override + public void call(JID jid) { + handleBlockListItemAdded(jid); + } + + }); + + blockList_.onItemRemoved.connect(new Slot1<JID>() { + + @Override + public void call(JID jid) { + handleBlockListItemRemoved(jid); + } + + }); + } + + @Test + public void testSetItemsToSubset() { + Vector<JID> subset = new Vector<JID>(); + subset.add(new JID("a@example.com")); + + blockList_.setItems(subset); + + assertEquals(0, addedJIDs_.size()); + assertEquals(1, removedJIDs_.size()); + } + + @Test + public void testSetItemsToSuperset() { + Vector<JID> superset = new Vector<JID>(); + superset.add(new JID("a@example.com")); + superset.add(new JID("b@example.com")); + superset.add(new JID("c@example.com")); + + blockList_.setItems(superset); + + assertEquals(1, addedJIDs_.size()); + assertEquals(0, removedJIDs_.size()); + } + + @Test + public void testSetItemsAllDifferent() { + Vector<JID> newBlockList = new Vector<JID>(); + newBlockList.add(new JID("x@example.com")); + newBlockList.add(new JID("y@example.com")); + newBlockList.add(new JID("z@example.com")); + + blockList_.setItems(newBlockList); + + assertEquals(3, addedJIDs_.size()); + assertEquals(2, removedJIDs_.size()); + } + + private void handleBlockListItemAdded(JID jid) { + addedJIDs_.add(jid); + } + + private void handleBlockListItemRemoved(JID jid) { + removedJIDs_.add(jid); + } + +} |