You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
o3de/Code/Framework/AzCore/Tests/TickBusTest.cpp

73 lines
2.4 KiB
C++

/*
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
* its licensors.
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution (the "License"). All use of this software is governed by the License,
* or, if provided, by the license below or the license accompanying this file. Do not
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
*/
#include <AzCore/Component/TickBus.h>
#include <AzCore/Math/Random.h>
#include <AzCore/std/sort.h>
#include <AzCore/UnitTest/TestTypes.h>
using namespace AZ;
// TickBus handler with customizable tick-order.
// When ticked it pushes its tick-order into a list.
struct OrderedTicker : public TickBus::Handler
{
int m_order = TICK_DEFAULT; ///< Relative order on TickBus
AZStd::vector<int>* m_targetList = nullptr; ///< OnTick, push order into this list
///////////////////////////////////////////////////////////////////////////
// TickBus
int GetTickOrder() override { return m_order; }
void OnTick(float /*deltaTime*/, ScriptTimePoint /*time*/) override
{
if (m_targetList)
{
m_targetList->push_back(m_order);
}
}
///////////////////////////////////////////////////////////////////////////
};
class OrderedTickBus : public UnitTest::AllocatorsFixture
{};
TEST_F(OrderedTickBus, OnTick_HandlersFireInSortedOrder)
{
// arbitrary unsorted order for each handler
AZStd::vector<int> unsortedHandlerOrder = { 7, 5, 6, 3, 2, 9, 1, 0, 4, 8 };
// handlers will add their order to this list in OnTick()
AZStd::vector<int> actualTickOrder;
// create OrderedTickers
AZStd::list<OrderedTicker> tickers;
for (int order : unsortedHandlerOrder)
{
tickers.push_back();
OrderedTicker& ticker = tickers.back();
ticker.m_order = order;
ticker.m_targetList = &actualTickOrder;
ticker.TickBus::Handler::BusConnect();
}
// Tick!
TickBus::Broadcast(&TickBus::Events::OnTick, 0.f, ScriptTimePoint{});
// this is the order they should have fired in
AZStd::vector<int> sortedOrder = unsortedHandlerOrder;
AZStd::sort(sortedOrder.begin(), sortedOrder.end());
// check the order they actually fired in
EXPECT_EQ(actualTickOrder, sortedOrder);
}