/src/mozilla-central/xpcom/tests/gtest/TestEventPriorities.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "nsIThreadManager.h" |
8 | | #include "nsCOMPtr.h" |
9 | | #include "nsIRunnable.h" |
10 | | #include "nsXPCOM.h" |
11 | | #include "nsThreadUtils.h" |
12 | | #include "gtest/gtest.h" |
13 | | |
14 | | #include <functional> |
15 | | |
16 | | using namespace mozilla; |
17 | | |
18 | | class TestEvent final : public Runnable, nsIRunnablePriority |
19 | | { |
20 | | public: |
21 | | explicit TestEvent(int* aCounter, std::function<void()>&& aCheck, uint32_t aPriority = nsIRunnablePriority::PRIORITY_NORMAL) |
22 | | : Runnable("TestEvent") |
23 | | , mCounter(aCounter) |
24 | | , mCheck(std::move(aCheck)) |
25 | | , mPriority(aPriority) |
26 | 0 | { |
27 | 0 | } |
28 | | |
29 | | NS_DECL_ISUPPORTS_INHERITED |
30 | | |
31 | | NS_IMETHOD GetPriority(uint32_t* aPriority) override |
32 | 0 | { |
33 | 0 | *aPriority = mPriority; |
34 | 0 | return NS_OK; |
35 | 0 | } |
36 | | |
37 | | NS_IMETHODIMP Run() override |
38 | 0 | { |
39 | 0 | (*mCounter)++; |
40 | 0 | mCheck(); |
41 | 0 | return NS_OK; |
42 | 0 | } |
43 | | |
44 | | private: |
45 | 0 | ~TestEvent() {} |
46 | | |
47 | | int* mCounter; |
48 | | std::function<void()> mCheck; |
49 | | uint32_t mPriority; |
50 | | }; |
51 | | |
52 | | NS_IMPL_ISUPPORTS_INHERITED(TestEvent, |
53 | | Runnable, |
54 | | nsIRunnablePriority) |
55 | | |
56 | | TEST(EventPriorities, IdleAfterNormal) |
57 | 0 | { |
58 | 0 | int normalRan = 0, idleRan = 0; |
59 | 0 |
|
60 | 0 | RefPtr<TestEvent> evNormal = new TestEvent(&normalRan, [&] { ASSERT_EQ(idleRan, 0); }); |
61 | 0 | RefPtr<TestEvent> evIdle = new TestEvent(&idleRan, [&] { ASSERT_EQ(normalRan, 3); }); |
62 | 0 |
|
63 | 0 | NS_IdleDispatchToCurrentThread(do_AddRef(evIdle)); |
64 | 0 | NS_IdleDispatchToCurrentThread(do_AddRef(evIdle)); |
65 | 0 | NS_IdleDispatchToCurrentThread(do_AddRef(evIdle)); |
66 | 0 | NS_DispatchToMainThread(evNormal); |
67 | 0 | NS_DispatchToMainThread(evNormal); |
68 | 0 | NS_DispatchToMainThread(evNormal); |
69 | 0 |
|
70 | 0 | MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return normalRan == 3 && idleRan == 3; })); |
71 | 0 | } |
72 | | |
73 | | TEST(EventPriorities, InterleaveHighNormal) |
74 | 0 | { |
75 | 0 | int normalRan = 0, highRan = 0; |
76 | 0 |
|
77 | 0 | RefPtr<TestEvent> evNormal = new TestEvent(&normalRan, [&] { |
78 | 0 | ASSERT_TRUE(abs(normalRan - highRan) <= 1); |
79 | 0 | }); |
80 | 0 | RefPtr<TestEvent> evHigh = new TestEvent(&highRan, [&] { |
81 | 0 | ASSERT_TRUE(abs(normalRan - highRan) <= 1); |
82 | 0 | }, |
83 | 0 | nsIRunnablePriority::PRIORITY_HIGH); |
84 | 0 |
|
85 | 0 | NS_DispatchToMainThread(evNormal); |
86 | 0 | NS_DispatchToMainThread(evNormal); |
87 | 0 | NS_DispatchToMainThread(evNormal); |
88 | 0 | NS_DispatchToMainThread(evHigh); |
89 | 0 | NS_DispatchToMainThread(evHigh); |
90 | 0 | NS_DispatchToMainThread(evHigh); |
91 | 0 |
|
92 | 0 | MOZ_ALWAYS_TRUE(SpinEventLoopUntil([&]() { return normalRan == 3 && highRan == 3; })); |
93 | 0 | } |