1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/task_scheduler/task_traits.h"
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 
9 namespace base {
10 
TEST(TaskSchedulerTaskTraitsTest,Default)11 TEST(TaskSchedulerTaskTraitsTest, Default) {
12   constexpr TaskTraits traits = {};
13   EXPECT_FALSE(traits.priority_set_explicitly());
14   EXPECT_EQ(TaskPriority::USER_VISIBLE, traits.priority());
15   EXPECT_EQ(TaskShutdownBehavior::SKIP_ON_SHUTDOWN, traits.shutdown_behavior());
16   EXPECT_FALSE(traits.may_block());
17   EXPECT_FALSE(traits.with_base_sync_primitives());
18 }
19 
TEST(TaskSchedulerTaskTraitsTest,TaskPriority)20 TEST(TaskSchedulerTaskTraitsTest, TaskPriority) {
21   constexpr TaskTraits traits = {TaskPriority::BACKGROUND};
22   EXPECT_TRUE(traits.priority_set_explicitly());
23   EXPECT_EQ(TaskPriority::BACKGROUND, traits.priority());
24   EXPECT_EQ(TaskShutdownBehavior::SKIP_ON_SHUTDOWN, traits.shutdown_behavior());
25   EXPECT_FALSE(traits.may_block());
26   EXPECT_FALSE(traits.with_base_sync_primitives());
27 }
28 
TEST(TaskSchedulerTaskTraitsTest,TaskShutdownBehavior)29 TEST(TaskSchedulerTaskTraitsTest, TaskShutdownBehavior) {
30   constexpr TaskTraits traits = {TaskShutdownBehavior::BLOCK_SHUTDOWN};
31   EXPECT_FALSE(traits.priority_set_explicitly());
32   EXPECT_EQ(TaskPriority::USER_VISIBLE, traits.priority());
33   EXPECT_EQ(TaskShutdownBehavior::BLOCK_SHUTDOWN, traits.shutdown_behavior());
34   EXPECT_FALSE(traits.may_block());
35   EXPECT_FALSE(traits.with_base_sync_primitives());
36 }
37 
TEST(TaskSchedulerTaskTraitsTest,MayBlock)38 TEST(TaskSchedulerTaskTraitsTest, MayBlock) {
39   constexpr TaskTraits traits = {MayBlock()};
40   EXPECT_FALSE(traits.priority_set_explicitly());
41   EXPECT_EQ(TaskPriority::USER_VISIBLE, traits.priority());
42   EXPECT_EQ(TaskShutdownBehavior::SKIP_ON_SHUTDOWN, traits.shutdown_behavior());
43   EXPECT_TRUE(traits.may_block());
44   EXPECT_FALSE(traits.with_base_sync_primitives());
45 }
46 
TEST(TaskSchedulerTaskTraitsTest,WithBaseSyncPrimitives)47 TEST(TaskSchedulerTaskTraitsTest, WithBaseSyncPrimitives) {
48   constexpr TaskTraits traits = {WithBaseSyncPrimitives()};
49   EXPECT_FALSE(traits.priority_set_explicitly());
50   EXPECT_EQ(TaskPriority::USER_VISIBLE, traits.priority());
51   EXPECT_EQ(TaskShutdownBehavior::SKIP_ON_SHUTDOWN, traits.shutdown_behavior());
52   EXPECT_FALSE(traits.may_block());
53   EXPECT_TRUE(traits.with_base_sync_primitives());
54 }
55 
TEST(TaskSchedulerTaskTraitsTest,MultipleTraits)56 TEST(TaskSchedulerTaskTraitsTest, MultipleTraits) {
57   constexpr TaskTraits traits = {TaskPriority::BACKGROUND,
58                                  TaskShutdownBehavior::BLOCK_SHUTDOWN,
59                                  MayBlock(), WithBaseSyncPrimitives()};
60   EXPECT_TRUE(traits.priority_set_explicitly());
61   EXPECT_EQ(TaskPriority::BACKGROUND, traits.priority());
62   EXPECT_EQ(TaskShutdownBehavior::BLOCK_SHUTDOWN, traits.shutdown_behavior());
63   EXPECT_TRUE(traits.may_block());
64   EXPECT_TRUE(traits.with_base_sync_primitives());
65 }
66 
TEST(TaskSchedulerTaskTraitsTest,Copy)67 TEST(TaskSchedulerTaskTraitsTest, Copy) {
68   constexpr TaskTraits traits = {TaskPriority::BACKGROUND,
69                                  TaskShutdownBehavior::BLOCK_SHUTDOWN,
70                                  MayBlock(), WithBaseSyncPrimitives()};
71   constexpr TaskTraits traits_copy(traits);
72   EXPECT_EQ(traits.priority_set_explicitly(),
73             traits_copy.priority_set_explicitly());
74   EXPECT_EQ(traits.priority(), traits_copy.priority());
75   EXPECT_EQ(traits.shutdown_behavior(), traits_copy.shutdown_behavior());
76   EXPECT_EQ(traits.may_block(), traits_copy.may_block());
77   EXPECT_EQ(traits.with_base_sync_primitives(),
78             traits_copy.with_base_sync_primitives());
79 }
80 
TEST(TaskSchedulerTaskTraitsTest,OverridePriority)81 TEST(TaskSchedulerTaskTraitsTest, OverridePriority) {
82   constexpr TaskTraits left = {TaskPriority::BACKGROUND};
83   constexpr TaskTraits right = {TaskPriority::USER_BLOCKING};
84   constexpr TaskTraits overridden = TaskTraits::Override(left, right);
85   EXPECT_TRUE(overridden.priority_set_explicitly());
86   EXPECT_EQ(TaskPriority::USER_BLOCKING, overridden.priority());
87   EXPECT_FALSE(overridden.shutdown_behavior_set_explicitly());
88   EXPECT_EQ(TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
89             overridden.shutdown_behavior());
90   EXPECT_FALSE(overridden.may_block());
91   EXPECT_FALSE(overridden.with_base_sync_primitives());
92 }
93 
TEST(TaskSchedulerTaskTraitsTest,OverrideShutdownBehavior)94 TEST(TaskSchedulerTaskTraitsTest, OverrideShutdownBehavior) {
95   constexpr TaskTraits left = {TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN};
96   constexpr TaskTraits right = {TaskShutdownBehavior::BLOCK_SHUTDOWN};
97   constexpr TaskTraits overridden = TaskTraits::Override(left, right);
98   EXPECT_FALSE(overridden.priority_set_explicitly());
99   EXPECT_EQ(TaskPriority::USER_VISIBLE, overridden.priority());
100   EXPECT_TRUE(overridden.shutdown_behavior_set_explicitly());
101   EXPECT_EQ(TaskShutdownBehavior::BLOCK_SHUTDOWN,
102             overridden.shutdown_behavior());
103   EXPECT_FALSE(overridden.may_block());
104   EXPECT_FALSE(overridden.with_base_sync_primitives());
105 }
106 
TEST(TaskSchedulerTaskTraitsTest,OverrideMayBlock)107 TEST(TaskSchedulerTaskTraitsTest, OverrideMayBlock) {
108   {
109     constexpr TaskTraits left = {MayBlock()};
110     constexpr TaskTraits right = {};
111     constexpr TaskTraits overridden = TaskTraits::Override(left, right);
112     EXPECT_FALSE(overridden.priority_set_explicitly());
113     EXPECT_EQ(TaskPriority::USER_VISIBLE, overridden.priority());
114     EXPECT_FALSE(overridden.shutdown_behavior_set_explicitly());
115     EXPECT_EQ(TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
116               overridden.shutdown_behavior());
117     EXPECT_TRUE(overridden.may_block());
118     EXPECT_FALSE(overridden.with_base_sync_primitives());
119   }
120   {
121     constexpr TaskTraits left = {};
122     constexpr TaskTraits right = {MayBlock()};
123     constexpr TaskTraits overridden = TaskTraits::Override(left, right);
124     EXPECT_FALSE(overridden.priority_set_explicitly());
125     EXPECT_EQ(TaskPriority::USER_VISIBLE, overridden.priority());
126     EXPECT_FALSE(overridden.shutdown_behavior_set_explicitly());
127     EXPECT_EQ(TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
128               overridden.shutdown_behavior());
129     EXPECT_TRUE(overridden.may_block());
130     EXPECT_FALSE(overridden.with_base_sync_primitives());
131   }
132 }
133 
TEST(TaskSchedulerTaskTraitsTest,OverrideWithBaseSyncPrimitives)134 TEST(TaskSchedulerTaskTraitsTest, OverrideWithBaseSyncPrimitives) {
135   {
136     constexpr TaskTraits left = {WithBaseSyncPrimitives()};
137     constexpr TaskTraits right = {};
138     constexpr TaskTraits overridden = TaskTraits::Override(left, right);
139     EXPECT_FALSE(overridden.priority_set_explicitly());
140     EXPECT_EQ(TaskPriority::USER_VISIBLE, overridden.priority());
141     EXPECT_FALSE(overridden.shutdown_behavior_set_explicitly());
142     EXPECT_EQ(TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
143               overridden.shutdown_behavior());
144     EXPECT_FALSE(overridden.may_block());
145     EXPECT_TRUE(overridden.with_base_sync_primitives());
146   }
147   {
148     constexpr TaskTraits left = {};
149     constexpr TaskTraits right = {WithBaseSyncPrimitives()};
150     constexpr TaskTraits overridden = TaskTraits::Override(left, right);
151     EXPECT_FALSE(overridden.priority_set_explicitly());
152     EXPECT_EQ(TaskPriority::USER_VISIBLE, overridden.priority());
153     EXPECT_FALSE(overridden.shutdown_behavior_set_explicitly());
154     EXPECT_EQ(TaskShutdownBehavior::SKIP_ON_SHUTDOWN,
155               overridden.shutdown_behavior());
156     EXPECT_FALSE(overridden.may_block());
157     EXPECT_TRUE(overridden.with_base_sync_primitives());
158   }
159 }
160 
TEST(TaskSchedulerTaskTraitsTest,OverrideMultipleTraits)161 TEST(TaskSchedulerTaskTraitsTest, OverrideMultipleTraits) {
162   constexpr TaskTraits left = {MayBlock(), TaskPriority::BACKGROUND,
163                                TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN};
164   constexpr TaskTraits right = {WithBaseSyncPrimitives(),
165                                 TaskPriority::USER_BLOCKING};
166   constexpr TaskTraits overridden = TaskTraits::Override(left, right);
167   EXPECT_TRUE(overridden.priority_set_explicitly());
168   EXPECT_EQ(right.priority(), overridden.priority());
169   EXPECT_TRUE(overridden.shutdown_behavior_set_explicitly());
170   EXPECT_EQ(left.shutdown_behavior(), overridden.shutdown_behavior());
171   EXPECT_TRUE(overridden.may_block());
172   EXPECT_TRUE(overridden.with_base_sync_primitives());
173 }
174 
175 }  // namespace base
176