/* * 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 #include #include #include 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* 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 unsortedHandlerOrder = { 7, 5, 6, 3, 2, 9, 1, 0, 4, 8 }; // handlers will add their order to this list in OnTick() AZStd::vector actualTickOrder; // create OrderedTickers AZStd::list 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 sortedOrder = unsortedHandlerOrder; AZStd::sort(sortedOrder.begin(), sortedOrder.end()); // check the order they actually fired in EXPECT_EQ(actualTickOrder, sortedOrder); }