Files
sousa-gecko/xpcom/tests/gtest/TestEventPriorities.cpp
T
Emilio Cobos Álvarez 2223a8bee2 Bug 2054311 - Sort headers in xpcom/. r=xpcom-reviewers,nika
Automatically generated with:

    cp dom/.clang-format xpcom/ && mach format xpcom/**.{cpp,h}

Manual changes:

 * nsTDependentSubstring.cpp was relying on include order, prevent that
   from being changed by clang-format.
 * Missing nsAtom.h include from nsGkAtoms.h
 * nsWindowsHelpers.h needed to keep the include order
 * MemoryPressureLevelMac.h needed mozilla/Attributes.h

Differential Revision: https://phabricator.services.mozilla.com/D311687
2026-07-13 18:24:13 +00:00

90 lines
2.6 KiB
C++

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <functional>
#include "gtest/gtest.h"
#include "mozilla/SpinEventLoopUntil.h"
#include "nsCOMPtr.h"
#include "nsIRunnable.h"
#include "nsThreadUtils.h"
#include "nsXPCOM.h"
using namespace mozilla;
class TestEvent final : public Runnable, nsIRunnablePriority {
public:
explicit TestEvent(int* aCounter, std::function<void()>&& aCheck,
uint32_t aPriority = nsIRunnablePriority::PRIORITY_NORMAL)
: Runnable("TestEvent"),
mCounter(aCounter),
mCheck(std::move(aCheck)),
mPriority(aPriority) {}
NS_DECL_ISUPPORTS_INHERITED
NS_IMETHOD GetPriority(uint32_t* aPriority) override {
*aPriority = mPriority;
return NS_OK;
}
NS_IMETHODIMP Run() override {
(*mCounter)++;
mCheck();
return NS_OK;
}
private:
~TestEvent() = default;
int* mCounter;
std::function<void()> mCheck;
uint32_t mPriority;
};
NS_IMPL_ISUPPORTS_INHERITED(TestEvent, Runnable, nsIRunnablePriority)
TEST(EventPriorities, IdleAfterNormal)
{
int normalRan = 0, idleRan = 0;
RefPtr evNormal =
MakeRefPtr<TestEvent>(&normalRan, [&] { ASSERT_EQ(idleRan, 0); });
RefPtr evIdle =
MakeRefPtr<TestEvent>(&idleRan, [&] { ASSERT_EQ(normalRan, 3); });
NS_DispatchToCurrentThreadQueue(do_AddRef(evIdle), EventQueuePriority::Idle);
NS_DispatchToCurrentThreadQueue(do_AddRef(evIdle), EventQueuePriority::Idle);
NS_DispatchToCurrentThreadQueue(do_AddRef(evIdle), EventQueuePriority::Idle);
NS_DispatchToMainThread(evNormal);
NS_DispatchToMainThread(evNormal);
NS_DispatchToMainThread(evNormal);
MOZ_ALWAYS_TRUE(
SpinEventLoopUntil("xpcom:TEST(EventPriorities, IdleAfterNormal)"_ns,
[&]() { return normalRan == 3 && idleRan == 3; }));
}
TEST(EventPriorities, HighNormal)
{
int normalRan = 0, highRan = 0;
RefPtr evNormal = MakeRefPtr<TestEvent>(
&normalRan, [&] { ASSERT_TRUE((highRan - normalRan) >= 0); });
RefPtr evHigh = MakeRefPtr<TestEvent>(
&highRan, [&] { ASSERT_TRUE((highRan - normalRan) >= 0); },
nsIRunnablePriority::PRIORITY_VSYNC);
NS_DispatchToMainThread(evNormal);
NS_DispatchToMainThread(evNormal);
NS_DispatchToMainThread(evNormal);
NS_DispatchToMainThread(evHigh);
NS_DispatchToMainThread(evHigh);
NS_DispatchToMainThread(evHigh);
MOZ_ALWAYS_TRUE(
SpinEventLoopUntil("xpcom:TEST(EventPriorities, HighNormal)"_ns,
[&]() { return normalRan == 3 && highRan == 3; }));
}