lleventfilter_test.cpp
上传用户:king477883
上传日期:2021-03-01
资源大小:9553k
文件大小:12k
源码类别:
游戏引擎
开发平台:
C++ Builder
- /**
- * @file lleventfilter_test.cpp
- * @author Nat Goodspeed
- * @date 2009-03-06
- * @brief Test for lleventfilter.
- *
- * $LicenseInfo:firstyear=2009&license=viewergpl$
- *
- * Copyright (c) 2009-2010, Linden Research, Inc.
- *
- * Second Life Viewer Source Code
- * The source code in this file ("Source Code") is provided by Linden Lab
- * to you under the terms of the GNU General Public License, version 2.0
- * ("GPL"), unless you have obtained a separate licensing agreement
- * ("Other License"), formally executed by you and Linden Lab. Terms of
- * the GPL can be found in doc/GPL-license.txt in this distribution, or
- * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
- *
- * There are special exceptions to the terms and conditions of the GPL as
- * it is applied to this Source Code. View the full text of the exception
- * in the file doc/FLOSS-exception.txt in this software distribution, or
- * online at
- * http://secondlifegrid.net/programs/open_source/licensing/flossexception
- *
- * By copying, modifying or distributing this software, you acknowledge
- * that you have read and understood your obligations described above,
- * and agree to abide by those obligations.
- *
- * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
- * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
- * COMPLETENESS OR PERFORMANCE.
- * $/LicenseInfo$
- */
- // Precompiled header
- #include "linden_common.h"
- // associated header
- #include "lleventfilter.h"
- // STL headers
- // std headers
- // external library headers
- // other Linden headers
- #include "../test/lltut.h"
- #include "stringize.h"
- #include "listener.h"
- #include "tests/wrapllerrs.h"
- /*****************************************************************************
- * Test classes
- *****************************************************************************/
- // Strictly speaking, we're testing LLEventTimeoutBase rather than the
- // production LLEventTimeout (using LLTimer) because we don't want every test
- // run to pause for some number of seconds until we reach a real timeout. But
- // as we've carefully put all functionality except actual LLTimer calls into
- // LLEventTimeoutBase, that should suffice. We're not not not trying to test
- // LLTimer here.
- class TestEventTimeout: public LLEventTimeoutBase
- {
- public:
- TestEventTimeout():
- mElapsed(true)
- {}
- TestEventTimeout(LLEventPump& source):
- LLEventTimeoutBase(source),
- mElapsed(true)
- {}
- // test hook
- void forceTimeout(bool timeout=true) { mElapsed = timeout; }
- protected:
- virtual void setCountdown(F32 seconds) { mElapsed = false; }
- virtual bool countdownElapsed() const { return mElapsed; }
- private:
- bool mElapsed;
- };
- /*****************************************************************************
- * TUT
- *****************************************************************************/
- namespace tut
- {
- struct filter_data
- {
- // The resemblance between this test data and that in llevents_tut.cpp
- // is not coincidental.
- filter_data():
- pumps(LLEventPumps::instance()),
- mainloop(pumps.obtain("mainloop")),
- listener0("first"),
- listener1("second")
- {}
- LLEventPumps& pumps;
- LLEventPump& mainloop;
- Listener listener0;
- Listener listener1;
- void check_listener(const std::string& desc, const Listener& listener, const LLSD& got)
- {
- ensure_equals(STRINGIZE(listener << ' ' << desc),
- listener.getLastEvent(), got);
- }
- };
- typedef test_group<filter_data> filter_group;
- typedef filter_group::object filter_object;
- filter_group filtergrp("lleventfilter");
- template<> template<>
- void filter_object::test<1>()
- {
- set_test_name("LLEventMatching");
- LLEventPump& driver(pumps.obtain("driver"));
- listener0.reset(0);
- // Listener isn't derived from LLEventTrackable specifically to test
- // various connection-management mechanisms. But that means we have a
- // couple of transient Listener objects, one of which is listening to
- // a persistent LLEventPump. Capture those connections in local
- // LLTempBoundListener instances so they'll disconnect
- // on destruction.
- LLTempBoundListener temp1(
- listener0.listenTo(driver));
- // Construct a pattern LLSD: desired Event must have a key "foo"
- // containing string "bar"
- LLEventMatching filter(driver, LLSD().insert("foo", "bar"));
- listener1.reset(0);
- LLTempBoundListener temp2(
- listener1.listenTo(filter));
- driver.post(1);
- check_listener("direct", listener0, LLSD(1));
- check_listener("filtered", listener1, LLSD(0));
- // Okay, construct an LLSD map matching the pattern
- LLSD data;
- data["foo"] = "bar";
- data["random"] = 17;
- driver.post(data);
- check_listener("direct", listener0, data);
- check_listener("filtered", listener1, data);
- }
- template<> template<>
- void filter_object::test<2>()
- {
- set_test_name("LLEventTimeout::actionAfter()");
- LLEventPump& driver(pumps.obtain("driver"));
- TestEventTimeout filter(driver);
- listener0.reset(0);
- LLTempBoundListener temp1(
- listener0.listenTo(filter));
- // Use listener1.call() as the Action for actionAfter(), since it
- // already provides a way to sense the call
- listener1.reset(0);
- // driver --> filter --> listener0
- filter.actionAfter(20,
- boost::bind(&Listener::call, boost::ref(listener1), LLSD("timeout")));
- // Okay, (fake) timer is ticking. 'filter' can only sense the timer
- // when we pump mainloop. Do that right now to take the logic path
- // before either the anticipated event arrives or the timer expires.
- mainloop.post(17);
- check_listener("no timeout 1", listener1, LLSD(0));
- // Expected event arrives...
- driver.post(1);
- check_listener("event passed thru", listener0, LLSD(1));
- // Should have canceled the timer. Verify that by asserting that the
- // time has expired, then pumping mainloop again.
- filter.forceTimeout();
- mainloop.post(17);
- check_listener("no timeout 2", listener1, LLSD(0));
- // Verify chained actionAfter() calls, that is, that a second
- // actionAfter() resets the timer established by the first
- // actionAfter().
- filter.actionAfter(20,
- boost::bind(&Listener::call, boost::ref(listener1), LLSD("timeout")));
- // Since our TestEventTimeout class isn't actually manipulating time
- // (quantities of seconds), only a bool "elapsed" flag, sense that by
- // forcing the flag between actionAfter() calls.
- filter.forceTimeout();
- // Pumping mainloop here would result in a timeout (as we'll verify
- // below). This state simulates a ticking timer that has not yet timed
- // out. But now, before a mainloop event lets 'filter' recognize
- // timeout on the previous actionAfter() call, pretend we're pushing
- // that timeout farther into the future.
- filter.actionAfter(20,
- boost::bind(&Listener::call, boost::ref(listener1), LLSD("timeout")));
- // Look ma, no timeout!
- mainloop.post(17);
- check_listener("no timeout 3", listener1, LLSD(0));
- // Now let the updated actionAfter() timer expire.
- filter.forceTimeout();
- // Notice the timeout.
- mainloop.post(17);
- check_listener("timeout", listener1, LLSD("timeout"));
- // Timing out cancels the timer. Verify that.
- listener1.reset(0);
- filter.forceTimeout();
- mainloop.post(17);
- check_listener("no timeout 4", listener1, LLSD(0));
- // Reset the timer and then cancel() it.
- filter.actionAfter(20,
- boost::bind(&Listener::call, boost::ref(listener1), LLSD("timeout")));
- // neither expired nor satisified
- mainloop.post(17);
- check_listener("no timeout 5", listener1, LLSD(0));
- // cancel
- filter.cancel();
- // timeout!
- filter.forceTimeout();
- mainloop.post(17);
- check_listener("no timeout 6", listener1, LLSD(0));
- }
- template<> template<>
- void filter_object::test<3>()
- {
- set_test_name("LLEventTimeout::eventAfter()");
- LLEventPump& driver(pumps.obtain("driver"));
- TestEventTimeout filter(driver);
- listener0.reset(0);
- LLTempBoundListener temp1(
- listener0.listenTo(filter));
- filter.eventAfter(20, LLSD("timeout"));
- // Okay, (fake) timer is ticking. 'filter' can only sense the timer
- // when we pump mainloop. Do that right now to take the logic path
- // before either the anticipated event arrives or the timer expires.
- mainloop.post(17);
- check_listener("no timeout 1", listener0, LLSD(0));
- // Expected event arrives...
- driver.post(1);
- check_listener("event passed thru", listener0, LLSD(1));
- // Should have canceled the timer. Verify that by asserting that the
- // time has expired, then pumping mainloop again.
- filter.forceTimeout();
- mainloop.post(17);
- check_listener("no timeout 2", listener0, LLSD(1));
- // Set timer again.
- filter.eventAfter(20, LLSD("timeout"));
- // Now let the timer expire.
- filter.forceTimeout();
- // Notice the timeout.
- mainloop.post(17);
- check_listener("timeout", listener0, LLSD("timeout"));
- // Timing out cancels the timer. Verify that.
- listener0.reset(0);
- filter.forceTimeout();
- mainloop.post(17);
- check_listener("no timeout 3", listener0, LLSD(0));
- }
- template<> template<>
- void filter_object::test<4>()
- {
- set_test_name("LLEventTimeout::errorAfter()");
- WrapLL_ERRS capture;
- LLEventPump& driver(pumps.obtain("driver"));
- TestEventTimeout filter(driver);
- listener0.reset(0);
- LLTempBoundListener temp1(
- listener0.listenTo(filter));
- filter.errorAfter(20, "timeout");
- // Okay, (fake) timer is ticking. 'filter' can only sense the timer
- // when we pump mainloop. Do that right now to take the logic path
- // before either the anticipated event arrives or the timer expires.
- mainloop.post(17);
- check_listener("no timeout 1", listener0, LLSD(0));
- // Expected event arrives...
- driver.post(1);
- check_listener("event passed thru", listener0, LLSD(1));
- // Should have canceled the timer. Verify that by asserting that the
- // time has expired, then pumping mainloop again.
- filter.forceTimeout();
- mainloop.post(17);
- check_listener("no timeout 2", listener0, LLSD(1));
- // Set timer again.
- filter.errorAfter(20, "timeout");
- // Now let the timer expire.
- filter.forceTimeout();
- // Notice the timeout.
- std::string threw;
- try
- {
- mainloop.post(17);
- }
- catch (const WrapLL_ERRS::FatalException& e)
- {
- threw = e.what();
- }
- ensure_contains("errorAfter() timeout exception", threw, "timeout");
- // Timing out cancels the timer. Verify that.
- listener0.reset(0);
- filter.forceTimeout();
- mainloop.post(17);
- check_listener("no timeout 3", listener0, LLSD(0));
- }
- } // namespace tut
- /*****************************************************************************
- * Link dependencies
- *****************************************************************************/
- #include "llsdutil.cpp"