summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Markmann <tm@ayena.de>2015-09-12 23:22:12 (GMT)
committerKevin Smith <kevin.smith@isode.com>2015-09-18 09:38:54 (GMT)
commit4455c20085834098f6d9aa872db3115d466e7004 (patch)
treee5e61ec0880f4001bc76340cd13670cd49628e7c
parent909ecaea63618e9b94c9063c94cd51aa69654b00 (diff)
downloadswift-4455c20085834098f6d9aa872db3115d466e7004.zip
swift-4455c20085834098f6d9aa872db3115d466e7004.tar.bz2
Fix notification logic for signals in BlockListImpl
The logic for calling onItemAdded and onItemRemoved signals when setting a new list of block items using BlockListImpl::setItems used to be broken. This commit fixes and documents the correct signal notification behavior Test-Information: Added a unit test which verifies the notification behavior in case of added block list items, removed block list items and a complete change of the block list. Change-Id: I3061545e25ddfc2d9d1a3c987045a58e5c9230ac
-rw-r--r--Swiften/Client/BlockListImpl.cpp20
-rw-r--r--Swiften/Client/BlockListImpl.h4
-rw-r--r--Swiften/Client/UnitTest/BlockListImplTest.cpp91
-rw-r--r--Swiften/SConscript1
4 files changed, 105 insertions, 11 deletions
diff --git a/Swiften/Client/BlockListImpl.cpp b/Swiften/Client/BlockListImpl.cpp
index 02c1c18..4abaa37 100644
--- a/Swiften/Client/BlockListImpl.cpp
+++ b/Swiften/Client/BlockListImpl.cpp
@@ -1,36 +1,38 @@
1/* 1/*
2 * Copyright (c) 2011 Isode Limited. 2 * Copyright (c) 2011-2015 Isode Limited.
3 * All rights reserved. 3 * All rights reserved.
4 * See the COPYING file for more information. 4 * See the COPYING file for more information.
5 */ 5 */
6 6
7#include <Swiften/Client/BlockListImpl.h> 7#include <Swiften/Client/BlockListImpl.h>
8 8
9#include <Swiften/Base/foreach.h>
10
11#include <algorithm> 9#include <algorithm>
12 10
11#include <Swiften/Base/foreach.h>
12
13using namespace Swift; 13using namespace Swift;
14 14
15BlockListImpl::BlockListImpl() : state(Init) { 15BlockListImpl::BlockListImpl() : state(Init) {
16 16
17} 17}
18 18
19void BlockListImpl::setItems(const std::vector<JID>& items) { 19void BlockListImpl::setItems(const std::vector<JID>& newItems) {
20 foreach (const JID& jid, this->items) { 20 // JIDs which are in the current list but not in the new list, are removed.
21 if (std::find(items.begin(), items.end(), jid) != items.end()) { 21 foreach (const JID& jid, items) {
22 if (std::find(newItems.begin(), newItems.end(), jid) == newItems.end()) {
22 onItemRemoved(jid); 23 onItemRemoved(jid);
23 } 24 }
24 } 25 }
25 26
26 foreach (const JID& jid, items) { 27 // JIDs which are in the new list but not in the current list, are added.
27 if (std::find(this->items.begin(), this->items.end(), jid) != this->items.end()) { 28 foreach (const JID& jid, newItems) {
29 if (std::find(items.begin(), items.end(), jid) == items.end()) {
28 onItemAdded(jid); 30 onItemAdded(jid);
29 } 31 }
30 } 32 }
31 this->items = items; 33 items = newItems;
32} 34}
33 35
34void BlockListImpl::addItem(const JID& item) { 36void BlockListImpl::addItem(const JID& item) {
35 if (std::find(items.begin(), items.end(), item) == items.end()) { 37 if (std::find(items.begin(), items.end(), item) == items.end()) {
36 items.push_back(item); 38 items.push_back(item);
diff --git a/Swiften/Client/BlockListImpl.h b/Swiften/Client/BlockListImpl.h
index 19359b0..e203d68 100644
--- a/Swiften/Client/BlockListImpl.h
+++ b/Swiften/Client/BlockListImpl.h
@@ -1,7 +1,7 @@
1/* 1/*
2 * Copyright (c) 2011 Isode Limited. 2 * Copyright (c) 2011-2015 Isode Limited.
3 * All rights reserved. 3 * All rights reserved.
4 * See the COPYING file for more information. 4 * See the COPYING file for more information.
5 */ 5 */
6 6
7#pragma once 7#pragma once
@@ -21,11 +21,11 @@ namespace Swift {
21 21
22 virtual const std::vector<JID>& getItems() const { 22 virtual const std::vector<JID>& getItems() const {
23 return items; 23 return items;
24 } 24 }
25 25
26 void setItems(const std::vector<JID>& items); 26 void setItems(const std::vector<JID>& newItems);
27 void addItem(const JID& item); 27 void addItem(const JID& item);
28 void removeItem(const JID& item); 28 void removeItem(const JID& item);
29 void addItems(const std::vector<JID>& items); 29 void addItems(const std::vector<JID>& items);
30 void removeItems(const std::vector<JID>& items); 30 void removeItems(const std::vector<JID>& items);
31 void removeAllItems(); 31 void removeAllItems();
diff --git a/Swiften/Client/UnitTest/BlockListImplTest.cpp b/Swiften/Client/UnitTest/BlockListImplTest.cpp
new file mode 100644
index 0000000..9e5bc1a
--- /dev/null
+++ b/Swiften/Client/UnitTest/BlockListImplTest.cpp
@@ -0,0 +1,91 @@
1/*
2 * Copyright (c) 2015 Isode Limited.
3 * All rights reserved.
4 * See the COPYING file for more information.
5 */
6
7#include <vector>
8
9#include <boost/bind.hpp>
10
11#include <cppunit/extensions/HelperMacros.h>
12#include <cppunit/extensions/TestFactoryRegistry.h>
13
14#include <Swiften/Client/BlockListImpl.h>
15#include <Swiften/JID/JID.h>
16
17using namespace Swift;
18
19class BlockListImplTest : public CppUnit::TestFixture {
20 CPPUNIT_TEST_SUITE(BlockListImplTest);
21 CPPUNIT_TEST(testSetItemsToSubset);
22 CPPUNIT_TEST(testSetItemsToSuperset);
23 CPPUNIT_TEST(testSetItemsAllDifferent);
24 CPPUNIT_TEST_SUITE_END();
25
26 public:
27
28 void testSetItemsToSubset() {
29 std::vector<JID> subset;
30 subset.push_back(JID("a@example.com"));
31
32 blockList_->setItems(subset);
33
34 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), addedJIDs_.size());
35 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), removedJIDs_.size());
36 }
37
38 void testSetItemsToSuperset() {
39 std::vector<JID> superset;
40 superset.push_back(JID("a@example.com"));
41 superset.push_back(JID("b@example.com"));
42 superset.push_back(JID("c@example.com"));
43
44 blockList_->setItems(superset);
45
46 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(1), addedJIDs_.size());
47 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(0), removedJIDs_.size());
48 }
49
50 void testSetItemsAllDifferent() {
51 std::vector<JID> newBlockList;
52 newBlockList.push_back(JID("x@example.com"));
53 newBlockList.push_back(JID("y@example.com"));
54 newBlockList.push_back(JID("z@example.com"));
55
56 blockList_->setItems(newBlockList);
57
58 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(3), addedJIDs_.size());
59 CPPUNIT_ASSERT_EQUAL(static_cast<size_t>(2), removedJIDs_.size());
60 }
61
62 void setUp() {
63 blockList_ = boost::make_shared<BlockListImpl>();
64 addedJIDs_.clear();
65 removedJIDs_.clear();
66 blockList_->addItem(JID("a@example.com"));
67 blockList_->addItem(JID("b@example.com"));
68
69 blockList_->onItemAdded.connect(boost::bind(&BlockListImplTest::handleBlockListItemAdded, this, _1));
70 blockList_->onItemRemoved.connect(boost::bind(&BlockListImplTest::handleBlockListItemRemoved, this, _1));
71 }
72
73 void tearDown() {
74 blockList_.reset();
75 }
76
77 void handleBlockListItemAdded(const JID& jid) {
78 addedJIDs_.push_back(jid);
79 }
80
81 void handleBlockListItemRemoved(const JID& jid) {
82 removedJIDs_.push_back(jid);
83 }
84
85 private:
86 boost::shared_ptr<BlockListImpl> blockList_;
87 std::vector<JID> addedJIDs_;
88 std::vector<JID> removedJIDs_;
89};
90
91CPPUNIT_TEST_SUITE_REGISTRATION(BlockListImplTest);
diff --git a/Swiften/SConscript b/Swiften/SConscript
index aff1478..9216b39 100644
--- a/Swiften/SConscript
+++ b/Swiften/SConscript
@@ -378,10 +378,11 @@ if env["SCONS_STAGE"] == "build" :
378 File("Chat/UnitTest/ChatStateNotifierTest.cpp"), 378 File("Chat/UnitTest/ChatStateNotifierTest.cpp"),
379# File("Chat/UnitTest/ChatStateTrackerTest.cpp"), 379# File("Chat/UnitTest/ChatStateTrackerTest.cpp"),
380 File("Client/UnitTest/ClientSessionTest.cpp"), 380 File("Client/UnitTest/ClientSessionTest.cpp"),
381 File("Client/UnitTest/NickResolverTest.cpp"), 381 File("Client/UnitTest/NickResolverTest.cpp"),
382 File("Client/UnitTest/ClientBlockListManagerTest.cpp"), 382 File("Client/UnitTest/ClientBlockListManagerTest.cpp"),
383 File("Client/UnitTest/BlockListImplTest.cpp"),
383 File("Compress/UnitTest/ZLibCompressorTest.cpp"), 384 File("Compress/UnitTest/ZLibCompressorTest.cpp"),
384 File("Compress/UnitTest/ZLibDecompressorTest.cpp"), 385 File("Compress/UnitTest/ZLibDecompressorTest.cpp"),
385 File("Component/UnitTest/ComponentHandshakeGeneratorTest.cpp"), 386 File("Component/UnitTest/ComponentHandshakeGeneratorTest.cpp"),
386 File("Component/UnitTest/ComponentConnectorTest.cpp"), 387 File("Component/UnitTest/ComponentConnectorTest.cpp"),
387 File("Component/UnitTest/ComponentSessionTest.cpp"), 388 File("Component/UnitTest/ComponentSessionTest.cpp"),