xref: /aosp_15_r20/external/libchrome/base/threading/thread_restrictions_unittest.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2017 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_restrictions.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <utility>
8*635a8641SAndroid Build Coastguard Worker 
9*635a8641SAndroid Build Coastguard Worker #include "base/bind.h"
10*635a8641SAndroid Build Coastguard Worker #include "base/callback.h"
11*635a8641SAndroid Build Coastguard Worker #include "base/macros.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/test/gtest_util.h"
13*635a8641SAndroid Build Coastguard Worker #include "testing/gtest/include/gtest/gtest.h"
14*635a8641SAndroid Build Coastguard Worker 
15*635a8641SAndroid Build Coastguard Worker namespace base {
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker namespace {
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker class ThreadRestrictionsTest : public testing::Test {
20*635a8641SAndroid Build Coastguard Worker  public:
21*635a8641SAndroid Build Coastguard Worker   ThreadRestrictionsTest() = default;
~ThreadRestrictionsTest()22*635a8641SAndroid Build Coastguard Worker   ~ThreadRestrictionsTest() override {
23*635a8641SAndroid Build Coastguard Worker     internal::ResetThreadRestrictionsForTesting();
24*635a8641SAndroid Build Coastguard Worker   }
25*635a8641SAndroid Build Coastguard Worker 
26*635a8641SAndroid Build Coastguard Worker  private:
27*635a8641SAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(ThreadRestrictionsTest);
28*635a8641SAndroid Build Coastguard Worker };
29*635a8641SAndroid Build Coastguard Worker 
30*635a8641SAndroid Build Coastguard Worker }  // namespace
31*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,BlockingAllowedByDefault)32*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest, BlockingAllowedByDefault) {
33*635a8641SAndroid Build Coastguard Worker   AssertBlockingAllowed();
34*635a8641SAndroid Build Coastguard Worker }
35*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,ScopedDisallowBlocking)36*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest, ScopedDisallowBlocking) {
37*635a8641SAndroid Build Coastguard Worker   {
38*635a8641SAndroid Build Coastguard Worker     ScopedDisallowBlocking scoped_disallow_blocking;
39*635a8641SAndroid Build Coastguard Worker     EXPECT_DCHECK_DEATH({ AssertBlockingAllowed(); });
40*635a8641SAndroid Build Coastguard Worker   }
41*635a8641SAndroid Build Coastguard Worker   AssertBlockingAllowed();
42*635a8641SAndroid Build Coastguard Worker }
43*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,ScopedAllowBlocking)44*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest, ScopedAllowBlocking) {
45*635a8641SAndroid Build Coastguard Worker   ScopedDisallowBlocking scoped_disallow_blocking;
46*635a8641SAndroid Build Coastguard Worker   {
47*635a8641SAndroid Build Coastguard Worker     ScopedAllowBlocking scoped_allow_blocking;
48*635a8641SAndroid Build Coastguard Worker     AssertBlockingAllowed();
49*635a8641SAndroid Build Coastguard Worker   }
50*635a8641SAndroid Build Coastguard Worker   EXPECT_DCHECK_DEATH({ AssertBlockingAllowed(); });
51*635a8641SAndroid Build Coastguard Worker }
52*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,ScopedAllowBlockingForTesting)53*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest, ScopedAllowBlockingForTesting) {
54*635a8641SAndroid Build Coastguard Worker   ScopedDisallowBlocking scoped_disallow_blocking;
55*635a8641SAndroid Build Coastguard Worker   {
56*635a8641SAndroid Build Coastguard Worker     ScopedAllowBlockingForTesting scoped_allow_blocking_for_testing;
57*635a8641SAndroid Build Coastguard Worker     AssertBlockingAllowed();
58*635a8641SAndroid Build Coastguard Worker   }
59*635a8641SAndroid Build Coastguard Worker   EXPECT_DCHECK_DEATH({ AssertBlockingAllowed(); });
60*635a8641SAndroid Build Coastguard Worker }
61*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,BaseSyncPrimitivesAllowedByDefault)62*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest, BaseSyncPrimitivesAllowedByDefault) {}
63*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,DisallowBaseSyncPrimitives)64*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest, DisallowBaseSyncPrimitives) {
65*635a8641SAndroid Build Coastguard Worker   DisallowBaseSyncPrimitives();
66*635a8641SAndroid Build Coastguard Worker   EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
67*635a8641SAndroid Build Coastguard Worker }
68*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitives)69*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest, ScopedAllowBaseSyncPrimitives) {
70*635a8641SAndroid Build Coastguard Worker   DisallowBaseSyncPrimitives();
71*635a8641SAndroid Build Coastguard Worker   ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives;
72*635a8641SAndroid Build Coastguard Worker   internal::AssertBaseSyncPrimitivesAllowed();
73*635a8641SAndroid Build Coastguard Worker }
74*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesResetsState)75*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest, ScopedAllowBaseSyncPrimitivesResetsState) {
76*635a8641SAndroid Build Coastguard Worker   DisallowBaseSyncPrimitives();
77*635a8641SAndroid Build Coastguard Worker   { ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives; }
78*635a8641SAndroid Build Coastguard Worker   EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
79*635a8641SAndroid Build Coastguard Worker }
80*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesWithBlockingDisallowed)81*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest,
82*635a8641SAndroid Build Coastguard Worker        ScopedAllowBaseSyncPrimitivesWithBlockingDisallowed) {
83*635a8641SAndroid Build Coastguard Worker   ScopedDisallowBlocking scoped_disallow_blocking;
84*635a8641SAndroid Build Coastguard Worker   DisallowBaseSyncPrimitives();
85*635a8641SAndroid Build Coastguard Worker 
86*635a8641SAndroid Build Coastguard Worker   // This should DCHECK because blocking is not allowed in this scope
87*635a8641SAndroid Build Coastguard Worker   // and OutsideBlockingScope is not passed to the constructor.
88*635a8641SAndroid Build Coastguard Worker   EXPECT_DCHECK_DEATH(
89*635a8641SAndroid Build Coastguard Worker       { ScopedAllowBaseSyncPrimitives scoped_allow_base_sync_primitives; });
90*635a8641SAndroid Build Coastguard Worker }
91*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesOutsideBlockingScope)92*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest,
93*635a8641SAndroid Build Coastguard Worker        ScopedAllowBaseSyncPrimitivesOutsideBlockingScope) {
94*635a8641SAndroid Build Coastguard Worker   ScopedDisallowBlocking scoped_disallow_blocking;
95*635a8641SAndroid Build Coastguard Worker   DisallowBaseSyncPrimitives();
96*635a8641SAndroid Build Coastguard Worker   ScopedAllowBaseSyncPrimitivesOutsideBlockingScope
97*635a8641SAndroid Build Coastguard Worker       scoped_allow_base_sync_primitives;
98*635a8641SAndroid Build Coastguard Worker   internal::AssertBaseSyncPrimitivesAllowed();
99*635a8641SAndroid Build Coastguard Worker }
100*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesOutsideBlockingScopeResetsState)101*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest,
102*635a8641SAndroid Build Coastguard Worker        ScopedAllowBaseSyncPrimitivesOutsideBlockingScopeResetsState) {
103*635a8641SAndroid Build Coastguard Worker   DisallowBaseSyncPrimitives();
104*635a8641SAndroid Build Coastguard Worker   {
105*635a8641SAndroid Build Coastguard Worker     ScopedAllowBaseSyncPrimitivesOutsideBlockingScope
106*635a8641SAndroid Build Coastguard Worker         scoped_allow_base_sync_primitives;
107*635a8641SAndroid Build Coastguard Worker   }
108*635a8641SAndroid Build Coastguard Worker   EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
109*635a8641SAndroid Build Coastguard Worker }
110*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesForTesting)111*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest, ScopedAllowBaseSyncPrimitivesForTesting) {
112*635a8641SAndroid Build Coastguard Worker   DisallowBaseSyncPrimitives();
113*635a8641SAndroid Build Coastguard Worker   ScopedAllowBaseSyncPrimitivesForTesting
114*635a8641SAndroid Build Coastguard Worker       scoped_allow_base_sync_primitives_for_testing;
115*635a8641SAndroid Build Coastguard Worker   internal::AssertBaseSyncPrimitivesAllowed();
116*635a8641SAndroid Build Coastguard Worker }
117*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesForTestingResetsState)118*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest,
119*635a8641SAndroid Build Coastguard Worker        ScopedAllowBaseSyncPrimitivesForTestingResetsState) {
120*635a8641SAndroid Build Coastguard Worker   DisallowBaseSyncPrimitives();
121*635a8641SAndroid Build Coastguard Worker   {
122*635a8641SAndroid Build Coastguard Worker     ScopedAllowBaseSyncPrimitivesForTesting
123*635a8641SAndroid Build Coastguard Worker         scoped_allow_base_sync_primitives_for_testing;
124*635a8641SAndroid Build Coastguard Worker   }
125*635a8641SAndroid Build Coastguard Worker   EXPECT_DCHECK_DEATH({ internal::AssertBaseSyncPrimitivesAllowed(); });
126*635a8641SAndroid Build Coastguard Worker }
127*635a8641SAndroid Build Coastguard Worker 
TEST_F(ThreadRestrictionsTest,ScopedAllowBaseSyncPrimitivesForTestingWithBlockingDisallowed)128*635a8641SAndroid Build Coastguard Worker TEST_F(ThreadRestrictionsTest,
129*635a8641SAndroid Build Coastguard Worker        ScopedAllowBaseSyncPrimitivesForTestingWithBlockingDisallowed) {
130*635a8641SAndroid Build Coastguard Worker   ScopedDisallowBlocking scoped_disallow_blocking;
131*635a8641SAndroid Build Coastguard Worker   DisallowBaseSyncPrimitives();
132*635a8641SAndroid Build Coastguard Worker   // This should not DCHECK.
133*635a8641SAndroid Build Coastguard Worker   ScopedAllowBaseSyncPrimitivesForTesting
134*635a8641SAndroid Build Coastguard Worker       scoped_allow_base_sync_primitives_for_testing;
135*635a8641SAndroid Build Coastguard Worker }
136*635a8641SAndroid Build Coastguard Worker 
137*635a8641SAndroid Build Coastguard Worker }  // namespace base
138