summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-11-24 18:09:37 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-11-24 18:09:37 (GMT)
commitc7fb528f0d80f9d96a2d84444352386d5d4288dc (patch)
treebc52f4729e948c5021ab322e6ad739f315d41140 /SwifTools/Idle/UnitTest
parentea951bec1c4b84b739a9f435e0d17dbb99e4d724 (diff)
downloadswift-contrib-c7fb528f0d80f9d96a2d84444352386d5d4288dc.zip
swift-contrib-c7fb528f0d80f9d96a2d84444352386d5d4288dc.tar.bz2
Added SwifTools/Idle module.
Diffstat (limited to 'SwifTools/Idle/UnitTest')
-rw-r--r--SwifTools/Idle/UnitTest/ActualIdleDetectorTest.cpp164
-rw-r--r--SwifTools/Idle/UnitTest/SConscript5
2 files changed, 169 insertions, 0 deletions
diff --git a/SwifTools/Idle/UnitTest/ActualIdleDetectorTest.cpp b/SwifTools/Idle/UnitTest/ActualIdleDetectorTest.cpp
new file mode 100644
index 0000000..99a7099
--- /dev/null
+++ b/SwifTools/Idle/UnitTest/ActualIdleDetectorTest.cpp
@@ -0,0 +1,164 @@
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <boost/bind.hpp>
+
+#include "SwifTools/Idle/ActualIdleDetector.h"
+#include "SwifTools/Idle/IdleQuerier.h"
+#include "Swiften/Base/foreach.h"
+#include "Swiften/Network/TimerFactory.h"
+#include "Swiften/Network/Timer.h"
+
+using namespace Swift;
+
+class ActualIdleDetectorTest : public CppUnit::TestFixture {
+ CPPUNIT_TEST_SUITE(ActualIdleDetectorTest);
+ CPPUNIT_TEST(testDestructor);
+ CPPUNIT_TEST(testHandleTick_Idle);
+ CPPUNIT_TEST(testHandleTick_Idle_AlreadyIdle);
+ CPPUNIT_TEST(testHandleTick_NotIdle);
+ CPPUNIT_TEST(testHandleTick_NotIdle_AlreadyNotIdle);
+ CPPUNIT_TEST_SUITE_END();
+
+ public:
+ void setUp() {
+ querier = new MockIdleQuerier();
+ timerFactory = new MockTimerFactory();
+ idleEvents.clear();
+ }
+
+ void tearDown() {
+ delete timerFactory;
+ delete querier;
+ }
+
+ void testDestructor() {
+ ActualIdleDetector* testling = createDetector();
+ testling->setIdleTimeSeconds(15);
+ delete testling;
+
+ querier->idleTime = 15;
+ timerFactory->updateTime(15000);
+
+ CPPUNIT_ASSERT_EQUAL(0U, idleEvents.size());
+ }
+
+ void testHandleTick_Idle() {
+ std::auto_ptr<ActualIdleDetector> testling(createDetector());
+ testling->setIdleTimeSeconds(15);
+ querier->idleTime = 15;
+
+ timerFactory->updateTime(15000);
+
+ CPPUNIT_ASSERT_EQUAL(1U, idleEvents.size());
+ CPPUNIT_ASSERT(idleEvents[0]);
+ }
+
+ void testHandleTick_Idle_AlreadyIdle() {
+ std::auto_ptr<ActualIdleDetector> testling(createDetector());
+ testling->setIdleTimeSeconds(15);
+ querier->idleTime = 15;
+ timerFactory->updateTime(15000);
+
+ querier->idleTime = 30;
+ timerFactory->updateTime(30000);
+
+ CPPUNIT_ASSERT_EQUAL(1U, idleEvents.size());
+ CPPUNIT_ASSERT(idleEvents[0]);
+ }
+
+ void testHandleTick_NotIdle() {
+ std::auto_ptr<ActualIdleDetector> testling(createDetector());
+ testling->setIdleTimeSeconds(15);
+ querier->idleTime = 15;
+ timerFactory->updateTime(15000);
+
+ querier->idleTime = 5;
+ timerFactory->updateTime(30000);
+
+ CPPUNIT_ASSERT_EQUAL(2U, idleEvents.size());
+ CPPUNIT_ASSERT(idleEvents[0]);
+ CPPUNIT_ASSERT(!idleEvents[1]);
+ }
+
+ void testHandleTick_NotIdle_AlreadyNotIdle() {
+ std::auto_ptr<ActualIdleDetector> testling(createDetector());
+ testling->setIdleTimeSeconds(15);
+ querier->idleTime = 5;
+
+ timerFactory->updateTime(15000);
+
+ CPPUNIT_ASSERT_EQUAL(0U, idleEvents.size());
+ }
+
+ private:
+ ActualIdleDetector* createDetector() {
+ ActualIdleDetector* detector = new ActualIdleDetector(querier, timerFactory, 10);
+ detector->onIdleChanged.connect(boost::bind(&ActualIdleDetectorTest::handleIdle, this, _1));
+ return detector;
+ }
+
+ void handleIdle(bool b) {
+ idleEvents.push_back(b);
+ }
+
+ private:
+ struct MockIdleQuerier : public IdleQuerier {
+ MockIdleQuerier() : idleTime(0) {}
+ virtual int getIdleTimeSeconds() { return idleTime; }
+ int idleTime;
+ };
+
+ struct MockTimer : public Timer {
+ MockTimer(int interval) : interval(interval), running(false), lastTime(0) {}
+
+ virtual void start() {
+ running = true;
+ }
+
+ virtual void stop() {
+ running = false;
+ }
+
+ virtual void updateTime(int currentTime) {
+ if (lastTime == currentTime) {
+ return;
+ }
+ if (running) {
+ int time = lastTime;
+ while (time <= currentTime) {
+ onTick();
+ time += interval;
+ }
+ }
+ lastTime = currentTime;
+ }
+
+ int interval;
+ bool running;
+ int lastTime;
+ };
+
+ struct MockTimerFactory : public TimerFactory {
+ MockTimerFactory() {}
+
+ void updateTime(int milliseconds) {
+ foreach(boost::shared_ptr<MockTimer> timer, timers) {
+ timer->updateTime(milliseconds);
+ }
+ }
+
+ boost::shared_ptr<Timer> createTimer(int milliseconds) {
+ boost::shared_ptr<MockTimer> timer(new MockTimer(milliseconds));
+ timers.push_back(timer);
+ return timer;
+ }
+
+ std::vector<boost::shared_ptr<MockTimer> > timers;
+ };
+
+ MockIdleQuerier* querier;
+ MockTimerFactory* timerFactory;
+ std::vector<bool> idleEvents;
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(ActualIdleDetectorTest);
diff --git a/SwifTools/Idle/UnitTest/SConscript b/SwifTools/Idle/UnitTest/SConscript
new file mode 100644
index 0000000..f193349
--- /dev/null
+++ b/SwifTools/Idle/UnitTest/SConscript
@@ -0,0 +1,5 @@
+Import("env")
+
+env.Append(UNITTEST_SOURCES = [
+ File("ActualIdleDetectorTest.cpp")
+ ])