summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Clayton <alex.clayton@isode.com>2016-03-10 10:46:58 (GMT)
committerAlex Clayton <alex.clayton@isode.com>2016-03-10 10:50:34 (GMT)
commitdcc9d6b81d73f42db35c6e26a91e029a117289e1 (patch)
treed7d9ceb34c98b016b9c0030f2ff160115c201cda
parent8f112a856705b800d1a8797bec5d9396a9c00b34 (diff)
downloadstroke-dcc9d6b81d73f42db35c6e26a91e029a117289e1.zip
stroke-dcc9d6b81d73f42db35c6e26a91e029a117289e1.tar.bz2
Fix BlockListImpl logic and add unit test.
As per swiften patch 'Fix notification logic for signals in BlockListImpl' (4455c20085834098f6d9aa872db3115d466e7004). Fix the logic in the BlockListImpl class and add a unit test for it. Test-information: Unit tests pass ok. Change-Id: I739d1febb2cf728ff00c132a00adb2f7f144b739
-rw-r--r--src/com/isode/stroke/client/BlockListImpl.java24
-rw-r--r--test/com/isode/stroke/client/BlockListImplTest.java99
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 {
39 return items; 39 return items;
40 } 40 }
41 41
42 public void setItems(final Vector<JID> items) { 42 public void setItems(final Vector<JID> newItems) {
43 for (final JID jid : this.items) { 43 for (final JID jid : items) {
44 if(items.contains(jid)) { 44 if(!newItems.contains(jid)) {
45 onItemRemoved.emit(jid); 45 onItemRemoved.emit(jid);
46 } 46 }
47 } 47 }
48 48
49 for (final JID jid : items) { 49 for (final JID jid : newItems) {
50 if(this.items.contains(jid)) { 50 if(!this.items.contains(jid)) {
51 onItemAdded.emit(jid); 51 onItemAdded.emit(jid);
52 } 52 }
53 } 53 }
54 this.items = items; 54 this.items = newItems;
55 } 55 }
56 56
57 public void addItem(final JID item) { 57 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 @@
1/* Copyright (c) 2016, Isode Limited, London, England.
2 * All rights reserved.
3 *
4 * Acquisition and use of this software and related materials for any
5 * purpose requires a written license agreement from Isode Limited,
6 * or a written license from an organisation licensed by Isode Limited
7 * to grant such a license.
8 *
9 */
10package com.isode.stroke.client;
11
12import static org.junit.Assert.assertEquals;
13
14import java.util.ArrayList;
15import java.util.List;
16import java.util.Vector;
17
18import org.junit.Before;
19import org.junit.Test;
20
21import com.isode.stroke.jid.JID;
22import com.isode.stroke.signals.Slot1;
23
24public class BlockListImplTest {
25
26 private final BlockListImpl blockList_ = new BlockListImpl();
27 private final List<JID> addedJIDs_ = new ArrayList<JID>();
28 private final List<JID> removedJIDs_ = new ArrayList<JID>();
29
30 @Before
31 public void setUp() {
32 blockList_.addItem(new JID("a@example.com"));
33 blockList_.addItem(new JID("b@example.com"));
34
35 blockList_.onItemAdded.connect(new Slot1<JID>() {
36
37 @Override
38 public void call(JID jid) {
39 handleBlockListItemAdded(jid);
40 }
41
42 });
43
44 blockList_.onItemRemoved.connect(new Slot1<JID>() {
45
46 @Override
47 public void call(JID jid) {
48 handleBlockListItemRemoved(jid);
49 }
50
51 });
52 }
53
54 @Test
55 public void testSetItemsToSubset() {
56 Vector<JID> subset = new Vector<JID>();
57 subset.add(new JID("a@example.com"));
58
59 blockList_.setItems(subset);
60
61 assertEquals(0, addedJIDs_.size());
62 assertEquals(1, removedJIDs_.size());
63 }
64
65 @Test
66 public void testSetItemsToSuperset() {
67 Vector<JID> superset = new Vector<JID>();
68 superset.add(new JID("a@example.com"));
69 superset.add(new JID("b@example.com"));
70 superset.add(new JID("c@example.com"));
71
72 blockList_.setItems(superset);
73
74 assertEquals(1, addedJIDs_.size());
75 assertEquals(0, removedJIDs_.size());
76 }
77
78 @Test
79 public void testSetItemsAllDifferent() {
80 Vector<JID> newBlockList = new Vector<JID>();
81 newBlockList.add(new JID("x@example.com"));
82 newBlockList.add(new JID("y@example.com"));
83 newBlockList.add(new JID("z@example.com"));
84
85 blockList_.setItems(newBlockList);
86
87 assertEquals(3, addedJIDs_.size());
88 assertEquals(2, removedJIDs_.size());
89 }
90
91 private void handleBlockListItemAdded(JID jid) {
92 addedJIDs_.add(jid);
93 }
94
95 private void handleBlockListItemRemoved(JID jid) {
96 removedJIDs_.add(jid);
97 }
98
99}